summaryrefslogtreecommitdiff
path: root/lib/lsg_web/context_plug.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_web/context_plug.ex
parentbleh (diff)
pouet
Diffstat (limited to '')
-rw-r--r--lib/lsg_web/context_plug.ex81
1 files changed, 81 insertions, 0 deletions
diff --git a/lib/lsg_web/context_plug.ex b/lib/lsg_web/context_plug.ex
new file mode 100644
index 0000000..7896ace
--- /dev/null
+++ b/lib/lsg_web/context_plug.ex
@@ -0,0 +1,81 @@
+defmodule LSGWeb.ContextPlug do
+ import Plug.Conn
+ import Phoenix.Controller
+
+ def init(opts \\ []) do
+ opts || []
+ end
+
+ def call(conn, opts) do
+ account = with \
+ {:account, account_id} when is_binary(account_id) <- {:account, get_session(conn, :account)},
+ {:account, account} when not is_nil(account) <- {:account, IRC.Account.get(account_id)}
+ do
+ account
+ else
+ _ -> nil
+ end
+
+ network = Map.get(conn.params, "network")
+ network = if network == "-", do: nil, else: network
+
+ conns = IRC.Connection.get_network(network)
+ chan = if c = Map.get(conn.params, "chan") do
+ LSGWeb.reformat_chan(c)
+ end
+ chan_conn = IRC.Connection.get_network(network, chan)
+
+ memberships = if account do
+ IRC.Membership.of_account(account)
+ end
+
+ auth_required = cond do
+ Keyword.get(opts, :restrict) == :public -> false
+ account == nil -> true
+ network == nil -> false
+ Keyword.get(opts, :restrict) == :logged_in -> false
+ network && chan ->
+ !Enum.member?(memberships, {network, chan})
+ network ->
+ !Enum.any?(memberships, fn({n, _}) -> n == network end)
+ end
+
+ bot = cond do
+ network && chan && chan_conn -> chan_conn.nick
+ network && conns -> conns.nick
+ true -> nil
+ end
+
+
+ cond do
+ account && auth_required ->
+ conn
+ |> put_status(404)
+ |> text("Page not found")
+ |> halt()
+ auth_required ->
+ conn
+ |> put_status(403)
+ |> render(LSGWeb.AlcoologView, "auth.html", bot: bot, no_header: true, network: network)
+ |> halt()
+ (network && !conns) ->
+ conn
+ |> put_status(404)
+ |> text("Page not found")
+ |> halt()
+ (chan && !chan_conn) ->
+ conn
+ |> put_status(404)
+ |> text("Page not found")
+ |> halt()
+ true ->
+ conn = conn
+ |> assign(:network, network)
+ |> assign(:chan, chan)
+ |> assign(:bot, bot)
+ |> assign(:account, account)
+ |> assign(:memberships, memberships)
+ end
+ end
+
+end