summaryrefslogtreecommitdiff
path: root/lib/lsg_irc/say_plugin.ex
diff options
context:
space:
mode:
authorhref <href@random.sh>2020-07-07 21:39:10 +0200
committerhref <href@random.sh>2020-07-07 21:39:51 +0200
commitd6ee134a5957e299c3ad59011df320b3c41e6e61 (patch)
tree29567e6635466f8a3415a935b3cc8a777019f5bc /lib/lsg_irc/say_plugin.ex
parentbleh (diff)
pouet
Diffstat (limited to '')
-rw-r--r--lib/lsg_irc/say_plugin.ex71
1 files changed, 71 insertions, 0 deletions
diff --git a/lib/lsg_irc/say_plugin.ex b/lib/lsg_irc/say_plugin.ex
new file mode 100644
index 0000000..6a4f547
--- /dev/null
+++ b/lib/lsg_irc/say_plugin.ex
@@ -0,0 +1,71 @@
+defmodule LSG.IRC.SayPlugin do
+
+ def irc_doc do
+ """
+ # say
+
+ Say something...
+
+ * **!say `<channel>` `<text>`** say something on `channel`
+ * **!asay `<channel>` `<text>`** same but anonymously
+
+ You must be a member of the channel.
+ """
+ end
+
+ def start_link() do
+ GenServer.start_link(__MODULE__, [])
+ end
+
+ def init([]) do
+ {:ok, _} = Registry.register(IRC.PubSub, "trigger:say", [])
+ {:ok, _} = Registry.register(IRC.PubSub, "trigger:asay", [])
+ {:ok, _} = Registry.register(IRC.PubSub, "message:private", [])
+ {:ok, nil}
+ end
+
+ def handle_info({:irc, :trigger, "say", m = %{trigger: %{type: :bang, args: [target | text]}}}, state) do
+ text = Enum.join(text, " ")
+ say_for(m.account, target, text, true)
+ {:noreply, state}
+ end
+
+ def handle_info({:irc, :trigger, "asay", m = %{trigger: %{type: :bang, args: [target | text]}}}, state) do
+ text = Enum.join(text, " ")
+ say_for(m.account, target, text, false)
+ {:noreply, state}
+ end
+
+ def handle_info({:irc, :text, m = %{text: "say "<>rest}}, state) do
+ case String.split(rest, " ", parts: 2) do
+ [target, text] -> say_for(m.account, target, text, true)
+ _ -> nil
+ end
+ {:noreply, state}
+ end
+
+ def handle_info({:irc, :text, m = %{text: "asay "<>rest}}, state) do
+ case String.split(rest, " ", parts: 2) do
+ [target, text] -> say_for(m.account, target, text, false)
+ _ -> nil
+ end
+ {:noreply, state}
+ end
+
+ def handle_info(_, state) do
+ {:noreply, state}
+ end
+
+ defp say_for(account, target, text, with_nick?) do
+ for {net, chan} <- IRC.Membership.of_account(account) do
+ chan2 = String.replace(chan, "#", "")
+ if (target == "#{net}/#{chan}" || target == "#{net}/#{chan2}" || target == chan || target == chan2) do
+ user = IRC.UserTrack.find_by_account(net, account)
+ nick = if(user, do: user.nick, else: account.name)
+ prefix = if(with_nick?, do: "<#{nick}> ", else: "")
+ IRC.Connection.broadcast_message(net, chan, "#{prefix}#{text}")
+ end
+ end
+ end
+
+end