summaryrefslogtreecommitdiff
path: root/lib/nola_plugins/tell.ex
diff options
context:
space:
mode:
authorJordan Bracco <href@random.sh>2022-12-20 02:13:47 +0000
committerJordan Bracco <href@random.sh>2022-12-20 19:29:41 +0100
commit70b9bba56f5319361ce5a7df5c489b9c0d6905ce (patch)
treef9b4438965f4c5e3e1f3a6129904cbb9a37047f2 /lib/nola_plugins/tell.ex
parentUpdate repo URL, refs T77. (diff)
Rename to Nola
Summary: Nola rename cont. pt. 2. Refs T77. `find lib -name "*.ex" -type f | xargs sed -i '' 's/LSG/Nola/g'` Nola rename, cont. pt. 3. Refs T77. `s/:lsg/:nola/g` Nola rename, cont. pt. 4. Refs T77. Nola rename, cont. pt. 5. Refs T77. Configs. find config -type f | xargs sed -i '' 's/LSG/Nola/g' find config -type f | xargs sed -i '' 's/lsg/nola/g' BREAKING CHANGE: Config keys switch from `:lsg` to `:nola` Nola rename, the end. pt 6. Refs T77. Nola rename: The Big Move, Refs T77 Update repo URL, refs T77. Nola rename: Nola.Plugins, refs T77 Maniphest Tasks: T77 Differential Revision: https://phab.random.sh/D3
Diffstat (limited to 'lib/nola_plugins/tell.ex')
-rw-r--r--lib/nola_plugins/tell.ex106
1 files changed, 106 insertions, 0 deletions
diff --git a/lib/nola_plugins/tell.ex b/lib/nola_plugins/tell.ex
new file mode 100644
index 0000000..8978c82
--- /dev/null
+++ b/lib/nola_plugins/tell.ex
@@ -0,0 +1,106 @@
+defmodule Nola.Plugins.Tell do
+ use GenServer
+
+ @moduledoc """
+ # Tell
+
+ * **!tell `<nick>` `<message>`**: tell `message` to `nick` when they reconnect.
+ """
+
+ def irc_doc, do: @moduledoc
+ def start_link() do
+ GenServer.start_link(__MODULE__, [], name: __MODULE__)
+ end
+
+ def dets do
+ (Nola.data_path() <> "/tell.dets") |> String.to_charlist()
+ end
+
+ def tell(m, target, message) do
+ GenServer.cast(__MODULE__, {:tell, m, target, message})
+ end
+
+ def init([]) do
+ regopts = [plugin: __MODULE__]
+ {:ok, _} = Registry.register(IRC.PubSub, "account", regopts)
+ {:ok, _} = Registry.register(IRC.PubSub, "trigger:tell", regopts)
+ {:ok, dets} = :dets.open_file(dets(), [type: :bag])
+ {:ok, %{dets: dets}}
+ end
+
+ def handle_cast({:tell, m, target, message}, state) do
+ do_tell(state, m, target, message)
+ {:noreply, state}
+ end
+
+ def handle_info({:irc, :trigger, "tell", m = %IRC.Message{trigger: %IRC.Trigger{type: :bang, args: [target | message]}}}, state) do
+ do_tell(state, m, target, message)
+ {:noreply, state}
+ end
+
+ def handle_info({:account, network, channel, nick, account_id}, state) do
+ messages = :dets.lookup(state.dets, {network, channel, account_id})
+ if messages != [] do
+ strs = Enum.map(messages, fn({_, from, message, at}) ->
+ account = IRC.Account.get(from)
+ user = IRC.UserTrack.find_by_account(network, account)
+ fromnick = if user, do: user.nick, else: account.name
+ "#{nick}: <#{fromnick}> #{message}"
+ end)
+ Enum.each(strs, fn(s) -> IRC.Connection.broadcast_message(network, channel, s) end)
+ :dets.delete(state.dets, {network, channel, account_id})
+ end
+ {:noreply, state}
+ end
+
+ def handle_info({:account_change, old_id, new_id}, state) do
+ #:ets.fun2ms(fn({ {_net, _chan, target_id}, from_id, _, _} = obj) when (target_id == old_id) or (from_id == old_id) -> obj end)
+ spec = [{{{:"$1", :"$2", :"$3"}, :"$4", :_, :_}, [{:orelse, {:==, :"$3", {:const, old_id}}, {:==, :"$4", {:const, old_id}}}], [:"$_"]}]
+ Util.ets_mutate_select_each(:dets, state.dets, spec, fn(table, obj) ->
+ case obj do
+ { {net, chan, ^old_id}, from_id, message, at } = obj ->
+ :dets.delete(obj)
+ :dets.insert(table, {{net, chan, new_id}, from_id, message, at})
+ {key, ^old_id, message, at} = obj ->
+ :dets.delete(table, obj)
+ :dets.insert(table, {key, new_id, message, at})
+ _ -> :ok
+ end
+ end)
+ {:noreply, state}
+ end
+
+
+ def handle_info(info, state) do
+ {:noreply, state}
+ end
+
+ def terminate(_, state) do
+ :dets.close(state.dets)
+ :ok
+ end
+
+ defp do_tell(state, m, nick_target, message) do
+ target = IRC.Account.find_always_by_nick(m.network, m.channel, nick_target)
+ message = Enum.join(message, " ")
+ with \
+ {:target, %IRC.Account{} = target} <- {:target, target},
+ {:same, false} <- {:same, target.id == m.account.id},
+ target_user = IRC.UserTrack.find_by_account(m.network, target),
+ target_nick = if(target_user, do: target_user.nick, else: target.name),
+ present? = if(target_user, do: Map.has_key?(target_user.last_active, m.channel)),
+ {:absent, true, _} <- {:absent, !present?, target_nick},
+ {:message, message} <- {:message, message}
+ do
+ obj = { {m.network, m.channel, target.id}, m.account.id, message, NaiveDateTime.utc_now()}
+ :dets.insert(state.dets, obj)
+ m.replyfun.("will tell to #{target_nick}")
+ else
+ {:same, _} -> m.replyfun.("are you so stupid that you need a bot to tell yourself things ?")
+ {:target, _} -> m.replyfun.("#{nick_target} unknown")
+ {:absent, _, nick} -> m.replyfun.("#{nick} is here, tell yourself!")
+ {:message, _} -> m.replyfun.("can't tell without a message")
+ end
+ end
+
+end