diff options
Diffstat (limited to 'lib/nola_web/controllers/irc_controller.ex')
-rw-r--r-- | lib/nola_web/controllers/irc_controller.ex | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/lib/nola_web/controllers/irc_controller.ex b/lib/nola_web/controllers/irc_controller.ex new file mode 100644 index 0000000..c617e78 --- /dev/null +++ b/lib/nola_web/controllers/irc_controller.ex @@ -0,0 +1,101 @@ +defmodule NolaWeb.IrcController do + use NolaWeb, :controller + + plug NolaWeb.ContextPlug + + def index(conn, params) do + network = Map.get(params, "network") + channel = if c = Map.get(params, "chan"), do: NolaWeb.reformat_chan(c) + commands = for mod <- Enum.uniq([IRC.Account.AccountPlugin] ++ IRC.Plugin.enabled()) do + if is_atom(mod) do + identifier = Module.split(mod) |> List.last |> String.replace("Plugin", "") |> Macro.underscore + {identifier, mod.irc_doc()} + end + end + |> Enum.filter(& &1) + |> Enum.filter(fn({_, doc}) -> doc end) + members = cond do + network && channel -> Enum.map(IRC.UserTrack.channel(network, channel), fn(tuple) -> IRC.UserTrack.User.from_tuple(tuple) end) + true -> + IRC.Membership.of_account(conn.assigns.account) + end + render conn, "index.html", network: network, commands: commands, channel: channel, members: members + end + + def txt(conn, %{"name" => name}) do + if String.contains?(name, ".txt") do + name = String.replace(name, ".txt", "") + data = data() + if Map.has_key?(data, name) do + lines = Enum.join(data[name], "\n") + text(conn, lines) + else + conn + |> put_status(404) + |> text("Not found") + end + else + do_txt(conn, name) + end + end + def txt(conn, _), do: do_txt(conn, nil) + + + defp do_txt(conn, nil) do + doc = Nola.IRC.TxtPlugin.irc_doc() + data = data() + main = Enum.filter(data, fn({trigger, _}) -> !String.contains?(trigger, ".") end) |> Enum.into(Map.new) + system = Enum.filter(data, fn({trigger, _}) -> String.contains?(trigger, ".") end) |> Enum.into(Map.new) + lines = Enum.reduce(main, 0, fn({_, lines}, acc) -> acc + Enum.count(lines) end) + conn + |> assign(:title, "txt") + |> render("txts.html", data: main, doc: doc, files: Enum.count(main), lines: lines, system: system) + end + + defp do_txt(conn, txt) do + data = data() + base_url = cond do + conn.assigns[:chan] -> "/#{conn.assigns.network}/#{NolaWeb.format_chan(conn.assigns.chan)}" + true -> "/-" + end + if lines = Map.get(data, txt) do + lines = Enum.map(lines, fn(line) -> + line + |> String.split("\\\\") + |> Enum.intersperse(Phoenix.HTML.Tag.tag(:br)) + end) + conn + |> assign(:breadcrumbs, [{"txt", "#{base_url}/txt"}]) + |> assign(:title, "#{txt}.txt") + |> render("txt.html", name: txt, data: lines, doc: nil) + else + conn + |> put_status(404) + |> text("Not found") + end + end + + defp data() do + dir = Application.get_env(:nola, :data_path) <> "/irc.txt/" + Path.wildcard(dir <> "/*.txt") + |> Enum.reduce(%{}, fn(path, m) -> + path = String.split(path, "/") + file = List.last(path) + key = String.replace(file, ".txt", "") + data = dir <> file + |> File.read! + |> String.split("\n") + |> Enum.reject(fn(line) -> + cond do + line == "" -> true + !line -> true + true -> false + end + end) + Map.put(m, key, data) + end) + |> Enum.sort + |> Enum.into(Map.new) + end + +end |