summaryrefslogtreecommitdiff
path: root/lib/lsg_irc/text_trigger_handler.ex
blob: e8331f51ce25c3d93b2be284bfc3f16c97b5ee00 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
defmodule LSG.IRC.TxtHandler do
  @moduledoc """
  # [txt](/irc/txt)

      !txt
          statistics, file list
      +txt <file>
          create new <file>

      !FILE
          read a random line from the file
      !FILE <index>
          read line #<index> from the file
      !FILE <query>
          return a phrase from file who matches <query>
      +FILE <text>
          add <text> in FILE
      -FILE <index>
          remove line in FILE at index <index>
  """

  def irc_doc, do: @moduledoc

  def start_link(client) do
    GenServer.start_link(__MODULE__, [client])
  end

  defstruct client: nil, triggers: %{}

  def init([client]) do
    state = %__MODULE__{client: client}
    ExIRC.Client.add_handler(client, self())
    {:ok, %__MODULE__{state | triggers: load()}}
  end

  def handle_info({:received, "!reload", _, chan}, state) do
    {:noreply, %__MODULE__{state | triggers: load()}}
  end

  def handle_info({:received, "!txt", _, chan}, state) do
    map = Enum.map(state.triggers, fn({key, data}) ->
      "#{key}: #{to_string(Enum.count(data))}"
    end)
    total = Enum.reduce(state.triggers, 0, fn({_, data}, acc) ->
      acc + Enum.count(data)
    end)
    detail = Enum.join(map, ", ")
    total = ". total: #{Enum.count(state.triggers)} fichiers, #{to_string(total)} lignes. Détail: https://sys.115ans.net/irc/txt"
    (detail<>total)
    |> String.codepoints
    |> Enum.chunk_every(440)
    |> Enum.map(&Enum.join/1)
    |> Enum.map(fn(line) -> ExIRC.Client.msg(state.client, :privmsg, chan, line) end)
    {:noreply, state}
  end

  def handle_info({:received, "!"<>trigger, _, chan}, state) do
    {trigger, opts} = clean_trigger(trigger)
    line = get_random(state.triggers, trigger, opts)
    if line do
      ExIRC.Client.msg(state.client, :privmsg, chan, line)
    end
    {:noreply, state}
  end

  def handle_info({:received, "+txt "<>trigger, _, chan}, state) do
    {trigger, _} = clean_trigger(trigger)
    if create_file(trigger) do
      ExIRC.Client.msg(state.client, :privmsg, chan, "#{trigger}.txt créé. Ajouter: `+#{trigger} …` ; Lire: `!#{trigger}`")
      {:noreply, %__MODULE__{state | triggers: load()}}
    else
      {:noreply, state}
    end
  end

  def handle_info({:received, "+"<>trigger_and_content, _, chan}, state) do
    if idx = add(state.triggers, trigger_and_content) do
      ExIRC.Client.msg(state.client, :privmsg, chan, "ajouté. (#{idx})")
      {:noreply, %__MODULE__{state | triggers: load()}}
    else
      {:noreply, state}
    end
  end

  def handle_info({:received, "-"<>trigger_and_id, _, chan}, state) do
    with \
      [trigger, id] <- String.split(trigger_and_id, " ", parts: 2),
      {trigger, _} = clean_trigger(trigger),
      data <- Map.get(state.triggers, trigger),
      {id, ""} <- Integer.parse(id),
      {text, _id} <- Enum.find(data, fn({_, idx}) -> id-1 == idx end)
    do
      data = data |> Enum.into(Map.new)
      data = Map.delete(data, text)
      ExIRC.Client.msg(state.client, :privmsg, chan, "#{trigger}.txt##{id} supprimée: #{text}")
      dump(trigger, data)
      {:noreply, %__MODULE__{state | triggers: load()}}
    else
      error ->
        IO.inspect("error " <> inspect(error))
        {:noreply, state}
    end
  end

  def handle_info(msg, state) do
    {:noreply, state}
  end

  # Load/Reloads text files from disk
  defp load() do
    dir = env()[:directory]
    Path.wildcard(dir <> "/*.txt")
    |> Enum.reduce(%{}, fn(path, m) ->
      file = Path.basename(path)
      [key, "txt"] = String.split(file, ".", parts: 2)
      data = dir <> file
      |> File.read!
      |> String.split("\n")
      |> Enum.reject(fn(line) ->
        cond do
          line == "" -> true
          !line -> true
          true -> false
        end
      end)
      |> Enum.with_index
      Map.put(m, key, data)
    end)
    |> Enum.sort
    |> Enum.into(Map.new)
  end

  defp dump(trigger, data) do
    data = data
    |> Enum.sort_by(fn({_, idx}) -> idx end)
    |> Enum.map(fn({text, _}) -> text end)
    |> Enum.join("\n")
    File.write!(env()[:directory] <> "/" <> trigger <> ".txt", data<>"\n", [])
  end

  defp get_random(triggers, trigger, []) do
    if data = Map.get(triggers, trigger) do
      {data, _idx} = Enum.random(data)
      data
    else
      nil
    end
  end

  defp get_random(triggers, trigger, [opt]) do
    arg = case Integer.parse(opt) do
      {pos, ""} -> {:index, pos}
      {_pos, _some_string} -> {:grep, opt}
      _error -> {:grep, opt}
    end
    get_with_param(triggers, trigger, arg)
  end

  defp get_with_param(triggers, trigger, {:index, pos}) do
    data = Map.get(triggers, trigger, %{})
    case Enum.find(data, fn({_, index}) -> index+1 == pos end) do
      {text, _} -> text
      _ -> nil
    end
  end

  defp get_with_param(triggers, trigger, {:grep, query}) do
    data = Map.get(triggers, trigger, %{})
    regex = Regex.compile!("#{query}", "i")
    out = Enum.filter(data, fn({txt, _}) -> Regex.match?(regex, txt) end)
    |> Enum.map(fn({txt, _}) -> txt end)
    if !Enum.empty?(out) do
      Enum.random(out)
    end
  end

  defp create_file(name) do
    File.touch!(env()[:directory] <> "/" <> name <> ".txt")
    true
  end

  defp add(triggers, trigger_and_content) do
    case String.split(trigger_and_content, " ", parts: 2) do
      [trigger, content] ->
        {trigger, _} = clean_trigger(trigger)
        if Map.has_key?(triggers, trigger) do
          File.write!(env()[:directory] <> "/" <> trigger <> ".txt", content<>"\n", [:append])
          Enum.count(triggers[trigger])+1
        end
      _ -> false
    end
  end

  # fixme: this is definitely the ugliest thing i've ever done
  defp clean_trigger(trigger) do
    [trigger | opts] = trigger
    |> String.strip
    |> String.split(" ", parts: 2)

    trigger = trigger
    |> String.downcase
    |> String.replace("à", "a")
    |> String.replace("ä", "a")
    |> String.replace("â", "a")
    |> String.replace("é", "e")
    |> String.replace("è", "e")
    |> String.replace("ê", "e")
    |> String.replace("ë", "e")
    |> String.replace("ç", "c")
    |> String.replace("ï", "i")
    |> String.replace("î", "i")
    |> String.replace(~r/[^a-z0-9]/, "")

    {trigger, opts}
  end

  defp env(), do: Application.get_env(:lsg, __MODULE__)

end