From 01385cfe9572df546685ef326db8ddc00d6fe371 Mon Sep 17 00:00:00 2001 From: href Date: Thu, 2 Sep 2021 02:52:15 +0200 Subject: new plugin: user mention --- lib/lsg_irc/tell_plugin.ex | 55 +++++++++++++++++++++++--------------- lib/lsg_irc/user_mention_plugin.ex | 49 +++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 21 deletions(-) create mode 100644 lib/lsg_irc/user_mention_plugin.ex (limited to 'lib/lsg_irc') diff --git a/lib/lsg_irc/tell_plugin.ex b/lib/lsg_irc/tell_plugin.ex index a683b43..2c9e3c8 100644 --- a/lib/lsg_irc/tell_plugin.ex +++ b/lib/lsg_irc/tell_plugin.ex @@ -16,6 +16,10 @@ defmodule LSG.IRC.TellPlugin do (LSG.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) @@ -24,27 +28,13 @@ defmodule LSG.IRC.TellPlugin do {:ok, %{dets: dets}} end - def handle_info({:irc, :trigger, "tell", m = %IRC.Message{trigger: %IRC.Trigger{type: :bang, args: [nick_target | message]}}}, state) 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 + 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 @@ -90,4 +80,27 @@ defmodule LSG.IRC.TellPlugin do :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 diff --git a/lib/lsg_irc/user_mention_plugin.ex b/lib/lsg_irc/user_mention_plugin.ex new file mode 100644 index 0000000..5f7b10a --- /dev/null +++ b/lib/lsg_irc/user_mention_plugin.ex @@ -0,0 +1,49 @@ +defmodule LSG.IRC.UserMentionPlugin do + @moduledoc """ + # mention + + * **@`` ``**: notifie si possible le nick immédiatement via Telegram, SMS, ou équivalent à `!tell`. + """ + + require Logger + + def short_irc_doc, do: false + def irc_doc, do: @moduledoc + + def start_link() do + GenServer.start_link(__MODULE__, [], name: __MODULE__) + end + + def init(_) do + {:ok, _} = Registry.register(IRC.PubSub, "triggers", plugin: __MODULE__) + {:ok, nil} + end + + def handle_info({:irc, :trigger, nick, message = %IRC.Message{sender: sender, account: account, network: network, channel: channel, trigger: %IRC.Trigger{type: :at, args: content}}}, state) do + target = IRC.Account.find_always_by_nick(network, channel, nick) + if target do + telegram = IRC.Account.get_meta(target, "telegram-id") + sms = IRC.Account.get_meta(target, "sms-number") + text = "#{channel} <#{sender.nick}> #{Enum.join(content, " ")}" + + cond do + telegram -> + LSG.Telegram.send_message(telegram, "`#{channel}` <**#{sender.nick}**> #{Enum.join(content, " ")}") + sms -> + case LSG.IRC.SmsPlugin.send_sms(sms, text) do + {:error, code} -> message.replyfun("#{sender.nick}: erreur #{code} (sms)") + end + true -> + LSG.IRC.TellPlugin.tell(message, nick, content) + end + else + message.replyfun.("#{nick} m'est inconnu") + end + {:noreply, state} + end + + def handle_info(_, state) do + {:noreply, state} + end + +end -- cgit v1.2.3