summaryrefslogblamecommitdiff
path: root/lib/irc.ex
blob: ee44f37a243b14b04f0301babef781ff34a001bb (plain) (tree)
1
2
3
4
5
6
7
8
9
10



                      


                         


                        

                
























                                                                                            
























                                                                
   
defmodule IRC do

  defmodule Message do
    defstruct [:text,
      {:transport, :irc},
      :network,
               :account,
               :sender,
               :channel,
               :trigger,
      :replyfun,
    :at]
  end
  defmodule Trigger do
    defstruct [:type, :trigger, :args]
  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} <- Application.get_env(:lsg, :irc, [])[: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