summaryrefslogtreecommitdiff
path: root/lib/web/controllers/irc_controller.ex
blob: 9de807bcbf90bb645fae9d2e6cb36eb48afc571b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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([Nola.Plugins.Account] ++ Nola.Plugins.enabled()) do
        if is_atom(mod) do
          identifier = Module.split(mod) |> List.last() |> Macro.underscore()
          if Kernel.function_exported?(mod, :irc_doc, 0), do: {identifier, mod.irc_doc()}
        end
      end
      |> Enum.filter(& &1)
      |> Enum.filter(fn {_, doc} -> doc end)

    members =
      cond do
        network && channel ->
          Enum.map(Nola.UserTrack.channel(network, channel), fn tuple ->
            Nola.UserTrack.User.from_tuple(tuple)
          end)

        true ->
          Nola.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.Plugins.Txt.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