summaryrefslogtreecommitdiff
path: root/lib/nola_irc/irc.ex
diff options
context:
space:
mode:
Diffstat (limited to 'lib/nola_irc/irc.ex')
-rw-r--r--lib/nola_irc/irc.ex79
1 files changed, 79 insertions, 0 deletions
diff --git a/lib/nola_irc/irc.ex b/lib/nola_irc/irc.ex
new file mode 100644
index 0000000..71d6d93
--- /dev/null
+++ b/lib/nola_irc/irc.ex
@@ -0,0 +1,79 @@
+defmodule IRC do
+
+ defmodule Message do
+ @derive {Poison.Encoder, except: [:replyfun]}
+ defstruct [:id,
+ :text,
+ {:transport, :irc},
+ :network,
+ :account,
+ :sender,
+ :channel,
+ :trigger,
+ :replyfun,
+ :at,
+ {:meta, %{}}
+ ]
+ end
+ defmodule Trigger do
+ @derive Poison.Encoder
+ defstruct [:type, :trigger, :args]
+ end
+
+ def send_message_as(account, network, channel, text, force_puppet \\ false) do
+ connection = IRC.Connection.get_network(network)
+ if connection && (force_puppet || IRC.PuppetConnection.whereis(account, connection)) do
+ IRC.PuppetConnection.start_and_send_message(account, connection, channel, text)
+ else
+ user = IRC.UserTrack.find_by_account(network, account)
+ nick = if(user, do: user.nick, else: account.name)
+ IRC.Connection.broadcast_message(network, channel, "<#{nick}> #{text}")
+ end
+ end
+
+ def register(key) do
+ case Registry.register(IRC.PubSub, key, []) do
+ {:ok, _} -> :ok
+ error -> error
+ end
+ end
+
+ def admin?(%Message{sender: sender}), do: admin?(sender)
+
+ def admin?(%{nick: nick, user: user, host: host}) do
+ for {n, u, h} <- Nola.IRC.env(:admins, []) do
+ admin_part_match?(n, nick) && admin_part_match?(u, user) && admin_part_match?(h, host)
+ end
+ |> Enum.any?
+ end
+
+ defp admin_part_match?(:_, _), do: true
+ defp admin_part_match?(a, a), do: true
+ defp admin_part_match?(_, _), do: false
+
+ @max_chars 440
+
+ def splitlong(string, max_chars \\ 440)
+
+ def splitlong(string, max_chars) when is_list(string) do
+ Enum.map(string, fn(s) -> splitlong(s, max_chars) end)
+ |> List.flatten()
+ end
+
+ def splitlong(string, max_chars) do
+ string
+ |> String.codepoints
+ |> Enum.chunk_every(max_chars)
+ |> Enum.map(&Enum.join/1)
+ end
+
+ def splitlong_with_prefix(string, prefix, max_chars \\ 440) do
+ prefix = "#{prefix} "
+ max_chars = max_chars - (length(String.codepoints(prefix)))
+ string
+ |> String.codepoints
+ |> Enum.chunk_every(max_chars)
+ |> Enum.map(fn(line) -> prefix <> Enum.join(line) end)
+ end
+
+end