From 2127553ad058bfa688feb73097e604a8fb5a1899 Mon Sep 17 00:00:00 2001 From: Jordan Bracco Date: Tue, 20 Dec 2022 02:47:33 +0000 Subject: Rename IRC.Account to Nola.Account, refs T77 --- lib/irc/account.ex | 263 ----------------------------- lib/irc/connection.ex | 10 +- lib/irc/membership.ex | 10 +- lib/irc/nola_irc.ex | 2 +- lib/irc/puppet_connection.ex | 18 +- lib/irc/user_track.ex | 16 +- lib/matrix/room.ex | 4 +- lib/nola/account.ex | 263 +++++++++++++++++++++++++++++ lib/plugins/account.ex | 34 ++-- lib/plugins/alcoolog.ex | 10 +- lib/plugins/alcoolog_announcer.ex | 2 +- lib/plugins/last_fm.ex | 4 +- lib/plugins/preums.ex | 12 +- lib/plugins/quatre_cent_vingt.ex | 2 +- lib/plugins/sms.ex | 18 +- lib/plugins/tell.ex | 6 +- lib/plugins/user_mention.ex | 6 +- lib/telegram/room.ex | 8 +- lib/telegram/telegram.ex | 22 +-- lib/tmpl.ex | 2 +- lib/untappd.ex | 2 +- lib/web/context_plug.ex | 6 +- lib/web/controllers/alcoolog_controller.ex | 14 +- lib/web/controllers/open_id_controller.ex | 6 +- lib/web/controllers/page_controller.ex | 6 +- lib/web/controllers/untappd_controller.ex | 4 +- lib/web/live/chat_live.ex | 6 +- 27 files changed, 378 insertions(+), 378 deletions(-) delete mode 100644 lib/irc/account.ex create mode 100644 lib/nola/account.ex diff --git a/lib/irc/account.ex b/lib/irc/account.ex deleted file mode 100644 index a39733c..0000000 --- a/lib/irc/account.ex +++ /dev/null @@ -1,263 +0,0 @@ -defmodule IRC.Account do - alias IRC.UserTrack.User - - @moduledoc """ - Account registry.... - - Maps a network predicate: - * `{net, {:nick, nickname}}` - * `{net, {:account, account}}` - * `{net, {:mask, user@host}}` - to an unique identifier, that can be shared over multiple networks. - - If a predicate cannot be found for an existing account, a new account will be made in the database. - - To link two existing accounts from different network onto a different one, a merge operation is provided. - - """ - - # FIXME: Ensure uniqueness of name? - - @derive {Poison.Encoder, except: [:token]} - defstruct [:id, :name, :token] - @type t :: %__MODULE__{id: id(), name: String.t()} - @type id :: String.t() - - defimpl Inspect, for: __MODULE__ do - import Inspect.Algebra - - def inspect(%{id: id, name: name}, opts) do - concat(["#IRC.Account[", id, " ", name, "]"]) - end - end - - def file(base) do - to_charlist(Nola.data_path() <> "/account_#{base}.dets") - end - - defp from_struct(%__MODULE__{id: id, name: name, token: token}) do - {id, name, token} - end - - defp from_tuple({id, name, token}) do - %__MODULE__{id: id, name: name, token: token} - end - - def start_link() do - GenServer.start_link(__MODULE__, [], [name: __MODULE__]) - end - - def init(_) do - {:ok, accounts} = :dets.open_file(file("db"), []) - {:ok, meta} = :dets.open_file(file("meta"), []) - {:ok, predicates} = :dets.open_file(file("predicates"), [{:type, :set}]) - {:ok, %{accounts: accounts, meta: meta, predicates: predicates}} - end - - def get(id) do - case :dets.lookup(file("db"), id) do - [account] -> from_tuple(account) - _ -> nil - end - end - - def get_by_name(name) do - spec = [{{:_, :"$1", :_}, [{:==, :"$1", {:const, name}}], [:"$_"]}] - case :dets.select(file("db"), spec) do - [account] -> from_tuple(account) - _ -> nil - end - end - - def get_meta(%__MODULE__{id: id}, key, default \\ nil) do - case :dets.lookup(file("meta"), {id, key}) do - [{_, value}] -> (value || default) - _ -> default - end - end - - @spec find_meta_accounts(String.t()) :: [{account :: t(), value :: String.t()}, ...] - @doc "Find all accounts that have a meta of `key`." - def find_meta_accounts(key) do - spec = [{{{:"$1", :"$2"}, :"$3"}, [{:==, :"$2", {:const, key}}], [{{:"$1", :"$3"}}]}] - for {id, val} <- :dets.select(file("meta"), spec), do: {get(id), val} - end - - @doc "Find an account given a specific meta `key` and `value`." - @spec find_meta_account(String.t(), String.t()) :: t() | nil - def find_meta_account(key, value) do - #spec = [{{{:"$1", :"$2"}, :"$3"}, [:andalso, {:==, :"$2", {:const, key}}, {:==, :"$3", {:const, value}}], [:"$1"]}] - spec = [{{{:"$1", :"$2"}, :"$3"}, [{:andalso, {:==, :"$2", {:const, key}}, {:==, {:const, value}, :"$3"}}], [:"$1"]}] - case :dets.select(file("meta"), spec) do - [id] -> get(id) - _ -> nil - end - end - - def get_all_meta(%__MODULE__{id: id}) do - spec = [{{{:"$1", :"$2"}, :"$3"}, [{:==, :"$1", {:const, id}}], [{{:"$2", :"$3"}}]}] - :dets.select(file("meta"), spec) - end - - def put_user_meta(account = %__MODULE__{}, key, value) do - put_meta(account, "u:"<>key, value) - end - - def put_meta(%__MODULE__{id: id}, key, value) do - :dets.insert(file("meta"), {{id, key}, value}) - end - - def delete_meta(%__MODULE__{id: id}, key) do - :dets.delete(file("meta"), {id, key}) - end - - def all_accounts() do - :dets.traverse(file("db"), fn(obj) -> {:continue, from_tuple(obj)} end) - end - - def all_predicates() do - :dets.traverse(file("predicates"), fn(obj) -> {:continue, obj} end) - end - - def all_meta() do - :dets.traverse(file("meta"), fn(obj) -> {:continue, obj} end) - end - - def merge_account(old_id, new_id) do - if old_id != new_id do - spec = [{{:"$1", :"$2"}, [{:==, :"$2", {:const, old_id}}], [:"$1"]}] - predicates = :dets.select(file("predicates"), spec) - for pred <- predicates, do: :ok = :dets.insert(file("predicates"), {pred, new_id}) - spec = [{{{:"$1", :"$2"}, :"$3"}, [{:==, :"$1", {:const, old_id}}], [{{:"$2", :"$3"}}]}] - metas = :dets.select(file("meta"), spec) - for {k,v} <- metas do - :dets.delete(file("meta"), {{old_id, k}}) - :ok = :dets.insert(file("meta"), {{new_id, k}, v}) - end - :dets.delete(file("db"), old_id) - IRC.Membership.merge_account(old_id, new_id) - IRC.UserTrack.merge_account(old_id, new_id) - IRC.Connection.dispatch("account", {:account_change, old_id, new_id}) - IRC.Connection.dispatch("conn", {:account_change, old_id, new_id}) - end - :ok - end - - @doc "Find an account by a logged in user" - def find_by_nick(network, nick) do - do_lookup(%ExIRC.SenderInfo{nick: nick, network: network}, false) - end - - @doc "Always find an account by nickname, even if offline. Uses predicates and then account name." - def find_always_by_nick(network, chan, nick) do - with \ - nil <- find_by_nick(network, nick), - nil <- do_lookup(%User{network: network, nick: nick}, false), - nil <- get_by_name(nick) - do - nil - else - %__MODULE__{} = account -> - memberships = IRC.Membership.of_account(account) - if Enum.any?(memberships, fn({net, ch}) -> (net == network) or (chan && chan == ch) end) do - account - else - nil - end - end - end - - def find(something) do - do_lookup(something, false) - end - - def lookup(something, make_default \\ true) do - account = do_lookup(something, make_default) - if account && Map.get(something, :nick) do - IRC.Connection.dispatch("account", {:account_auth, Map.get(something, :nick), account.id}) - end - account - end - - def handle_info(_, state) do - {:noreply, state} - end - - def handle_cast(_, state) do - {:noreply, state} - end - - def handle_call(_, _, state) do - {:noreply, state} - end - - def terminate(_, state) do - for {_, dets} <- state do - :dets.sync(dets) - :dets.close(dets) - end - end - - defp do_lookup(message = %IRC.Message{account: account_id}, make_default) when is_binary(account_id) do - get(account_id) - end - - defp do_lookup(sender = %ExIRC.Who{}, make_default) do - if user = IRC.UserTrack.find_by_nick(sender) do - lookup(user, make_default) - else - #FIXME this will never work with continued lookup by other methods as Who isn't compatible - lookup_by_nick(sender, :dets.lookup(file("predicates"), {sender.network,{:nick, sender.nick}}), make_default) - end - end - - defp do_lookup(sender = %ExIRC.SenderInfo{}, make_default) do - lookup(IRC.UserTrack.find_by_nick(sender), make_default) - end - - defp do_lookup(user = %User{account: id}, make_default) when is_binary(id) do - get(id) - end - - defp do_lookup(user = %User{network: server, nick: nick}, make_default) do - lookup_by_nick(user, :dets.lookup(file("predicates"), {server,{:nick, nick}}), make_default) - end - - defp do_lookup(nil, _) do - nil - end - - defp lookup_by_nick(_, [{_, id}], _make_default) do - get(id) - end - - defp lookup_by_nick(user, _, make_default) do - #authenticate_by_host(user) - if make_default, do: new_account(user), else: nil - end - - def new_account(nick) do - id = EntropyString.large_id() - :dets.insert(file("db"), {id, nick, EntropyString.token()}) - get(id) - end - - def new_account(%{nick: nick, network: server}) do - id = EntropyString.large_id() - :dets.insert(file("db"), {id, nick, EntropyString.token()}) - :dets.insert(file("predicates"), {{server, {:nick, nick}}, id}) - get(id) - end - - def update_account_name(account = %__MODULE__{id: id}, name) do - account = %__MODULE__{account | name: name} - :dets.insert(file("db"), from_struct(account)) - get(id) - end - - def get_predicates(%__MODULE__{} = account) do - spec = [{{:"$1", :"$2"}, [{:==, :"$2", {:const, account.id}}], [:"$1"]}] - :dets.select(file("predicates"), spec) - end - -end diff --git a/lib/irc/connection.ex b/lib/irc/connection.ex index 4472466..49f5774 100644 --- a/lib/irc/connection.ex +++ b/lib/irc/connection.ex @@ -293,7 +293,7 @@ defmodule IRC.Connection do else if !Map.get(user.options, :puppet) do reply_fun = fn(text) -> irc_reply(state, {chan, sender}, text) end - account = IRC.Account.lookup(sender) + account = Nola.Account.lookup(sender) message = %IRC.Message{id: FlakeId.get(), transport: :irc, at: NaiveDateTime.utc_now(), text: text, network: state.network, account: account, sender: sender, channel: chan, replyfun: reply_fun, trigger: extract_trigger(text)} @@ -310,7 +310,7 @@ defmodule IRC.Connection do # Received a private message def handle_info({:received, text, sender}, state) do reply_fun = fn(text) -> irc_reply(state, {sender.nick, sender}, text) end - account = IRC.Account.lookup(sender) + account = Nola.Account.lookup(sender) message = %IRC.Message{id: FlakeId.get(), transport: :irc, text: text, network: state.network, at: NaiveDateTime.utc_now(), account: account, sender: sender, replyfun: reply_fun, trigger: extract_trigger(text)} message = case IRC.UserTrack.messaged(message) do @@ -322,7 +322,7 @@ defmodule IRC.Connection do end ## -- Broadcast - def handle_info({:broadcast, net, account = %IRC.Account{}, message}, state) do + def handle_info({:broadcast, net, account = %Nola.Account{}, message}, state) do if net == state.conn.network do user = IRC.UserTrack.find_by_account(net, account) if user do @@ -350,7 +350,7 @@ defmodule IRC.Connection do priv = if operator, do: [:operator], else: [] # Don't touch -- on WHO the bot joined, not the users. IRC.UserTrack.joined(channel, who, priv, false) - account = IRC.Account.lookup(who) + account = Nola.Account.lookup(who) if account do {:account, who.network, channel, who.nick, account.id} end @@ -367,7 +367,7 @@ defmodule IRC.Connection do def handle_info({:joined, channel, sender}, state) do IRC.UserTrack.joined(channel, sender, []) - account = IRC.Account.lookup(sender) + account = Nola.Account.lookup(sender) if account do dispatch("account", {:account, sender.network, channel, sender.nick, account.id}) end diff --git a/lib/irc/membership.ex b/lib/irc/membership.ex index b727dfd..25a0cfc 100644 --- a/lib/irc/membership.ex +++ b/lib/irc/membership.ex @@ -19,7 +19,7 @@ defmodule IRC.Membership do {:ok, dets} end - def of_account(%IRC.Account{id: id}) do + def of_account(%Nola.Account{id: id}) do spec = [{{{:"$1", :"$2", :"$3"}, :_}, [{:==, :"$1", {:const, id}}], [{{:"$2", :"$3"}}]}] :dets.select(dets(), spec) end @@ -33,11 +33,11 @@ defmodule IRC.Membership do end) end - def touch(%IRC.Account{id: id}, network, channel) do + def touch(%Nola.Account{id: id}, network, channel) do :dets.insert(dets(), {{id, network, channel}, NaiveDateTime.utc_now()}) end def touch(account_id, network, channel) do - if account = IRC.Account.get(account_id) do + if account = Nola.Account.get(account_id) do touch(account, network, channel) end end @@ -91,7 +91,7 @@ defmodule IRC.Membership do :dets.select(dets(), spec) end - def friends(account = %IRC.Account{id: id}) do + def friends(account = %Nola.Account{id: id}) do for({net, chan} <- of_account(account), do: members(net, chan)) |> List.flatten() |> Enum.uniq() @@ -116,7 +116,7 @@ defmodule IRC.Membership do defp expand(network, list) do for id <- list do - if account = IRC.Account.get(id) do + if account = Nola.Account.get(id) do user = IRC.UserTrack.find_by_account(network, account) nick = if(user, do: user.nick, else: account.name) {account, user, nick} diff --git a/lib/irc/nola_irc.ex b/lib/irc/nola_irc.ex index dde99af..2d355d3 100644 --- a/lib/irc/nola_irc.ex +++ b/lib/irc/nola_irc.ex @@ -13,7 +13,7 @@ defmodule Nola.IRC do [ worker(Registry, [[keys: :duplicate, name: IRC.ConnectionPubSub]], id: :registr_irc_conn), worker(IRC.Membership, []), - worker(IRC.Account, []), + worker(Nola.Account, []), worker(IRC.UserTrack.Storage, []), worker(Nola.Plugins.Account, []), supervisor(Nola.Plugins.Supervisor, [], [name: Nola.Plugins.Supervisor]), diff --git a/lib/irc/puppet_connection.ex b/lib/irc/puppet_connection.ex index 91a26b3..fd0a98e 100644 --- a/lib/irc/puppet_connection.ex +++ b/lib/irc/puppet_connection.ex @@ -12,7 +12,7 @@ defmodule IRC.PuppetConnection do DynamicSupervisor.start_link(__MODULE__, [], name: __MODULE__) end - def start_child(%IRC.Account{id: account_id}, %IRC.Connection{id: connection_id}) do + def start_child(%Nola.Account{id: account_id}, %IRC.Connection{id: connection_id}) do spec = %{id: {account_id, connection_id}, start: {IRC.PuppetConnection, :start_link, [account_id, connection_id]}, restart: :transient} DynamicSupervisor.start_child(__MODULE__, spec) end @@ -27,7 +27,7 @@ defmodule IRC.PuppetConnection do end end - def whereis(account = %IRC.Account{id: account_id}, connection = %IRC.Connection{id: connection_id}) do + def whereis(account = %Nola.Account{id: account_id}, connection = %IRC.Connection{id: connection_id}) do {:global, name} = name(account_id, connection_id) case :global.whereis_name(name) do :undefined -> nil @@ -35,11 +35,11 @@ defmodule IRC.PuppetConnection do end end - def send_message(account = %IRC.Account{id: account_id}, connection = %IRC.Connection{id: connection_id}, channel, text) do + def send_message(account = %Nola.Account{id: account_id}, connection = %IRC.Connection{id: connection_id}, channel, text) do GenServer.cast(name(account_id, connection_id), {:send_message, self(), channel, text}) end - def start_and_send_message(account = %IRC.Account{id: account_id}, connection = %IRC.Connection{id: connection_id}, channel, text) do + def start_and_send_message(account = %Nola.Account{id: account_id}, connection = %IRC.Connection{id: connection_id}, channel, text) do {:global, name} = name(account_id, connection_id) pid = whereis(account, connection) pid = if !pid do @@ -53,7 +53,7 @@ defmodule IRC.PuppetConnection do GenServer.cast(pid, {:send_message, self(), channel, text}) end - def start(account = %IRC.Account{}, connection = %IRC.Connection{}) do + def start(account = %Nola.Account{}, connection = %IRC.Connection{}) do IRC.PuppetConnection.Supervisor.start_child(account, connection) end @@ -66,7 +66,7 @@ defmodule IRC.PuppetConnection do end def init([account_id, connection_id]) do - account = %IRC.Account{} = IRC.Account.get(account_id) + account = %Nola.Account{} = Nola.Account.get(account_id) connection = %IRC.Connection{} = IRC.Connection.lookup(connection_id) Logger.metadata(puppet_conn: account.id <> "@" <> connection.id) backoff = :backoff.init(@min_backoff, @max_backoff) @@ -78,7 +78,7 @@ defmodule IRC.PuppetConnection do def handle_continue(:connect, state) do #ipv6 = if @env == :prod do # subnet = Nola.Subnet.assign(state.account_id) - # IRC.Account.put_meta(IRC.Account.get(state.account_id), "subnet", subnet) + # Nola.Account.put_meta(Nola.Account.get(state.account_id), "subnet", subnet) # ip = Pfx.host(subnet, 1) # {:ok, ipv6} = :inet_parse.ipv6_address(to_charlist(ip)) # System.cmd("add-ip6", [ip]) @@ -146,7 +146,7 @@ defmodule IRC.PuppetConnection do ExIRC.Client.msg(state.client, :privmsg, channel, text) meta = %{puppet: true, from: pid} - account = IRC.Account.get(state.account_id) + account = Nola.Account.get(state.account_id) nick = make_nick(state) sender = %ExIRC.SenderInfo{network: state.network, nick: suffix_nick(nick), user: nick, host: "puppet."} reply_fun = fn(text) -> @@ -219,7 +219,7 @@ defmodule IRC.PuppetConnection do end def make_nick(state) do - account = IRC.Account.get(state.account_id) + account = Nola.Account.get(state.account_id) user = IRC.UserTrack.find_by_account(state.network, account) base_nick = if(user, do: user.nick, else: account.name) clean_nick = case String.split(base_nick, ":", parts: 2) do diff --git a/lib/irc/user_track.ex b/lib/irc/user_track.ex index 1efa523..3f144d5 100644 --- a/lib/irc/user_track.ex +++ b/lib/irc/user_track.ex @@ -73,7 +73,7 @@ defmodule IRC.UserTrack do end end - def find_by_account(%IRC.Account{id: id}) do + def find_by_account(%Nola.Account{id: id}) do #iex(15)> :ets.fun2ms(fn(obj = {_, net, acct, _, _, _, _, _, _}) when net == network and acct == account -> obj end) spec = [ {{:_, :_, :"$2", :_, :_, :_, :_, :_, :_, :_, :_, :_}, @@ -90,7 +90,7 @@ defmodule IRC.UserTrack do nil end - def find_by_account(network, %IRC.Account{id: id}) do + def find_by_account(network, %Nola.Account{id: id}) do #iex(15)> :ets.fun2ms(fn(obj = {_, net, acct, _, _, _, _, _, _}) when net == network and acct == account -> obj end) spec = [ {{:_, :"$1", :"$2", :_, :_, :_, :_, :_, :_, :_, :_, :_}, @@ -178,7 +178,7 @@ defmodule IRC.UserTrack do # TODO def connected(network, nick, user, host, account_id, opts \\ %{}) do - if account = IRC.Account.get(account_id) do + if account = Nola.Account.get(account_id) do user = if user = find_by_nick(network, nick) do user else @@ -207,7 +207,7 @@ defmodule IRC.UserTrack do else user = %User{id: IRC.UserTrack.Id.large_id, network: sender.network, nick: nick, username: uname, host: host, privileges: %{channel => privileges}} - account = IRC.Account.lookup(user).id + account = Nola.Account.lookup(user).id user = %User{user | account: account} end user = touch_struct(user, channel) @@ -239,10 +239,10 @@ defmodule IRC.UserTrack do def messaged(%IRC.Message{network: network, account: account, channel: chan, sender: %{nick: nick}} = m) do {user, account} = if user = find_by_nick(network, nick) do - {touch_struct(user, chan), account || IRC.Account.lookup(user)} + {touch_struct(user, chan), account || Nola.Account.lookup(user)} else user = %User{network: network, nick: nick, privileges: %{}} - account = IRC.Account.lookup(user) + account = Nola.Account.lookup(user) {%User{user | account: account.id}, account} end Storage.insert(User.to_tuple(user)) @@ -256,9 +256,9 @@ defmodule IRC.UserTrack do def renamed(network, old_nick, new_nick) do if user = find_by_nick(network, old_nick) do - old_account = IRC.Account.lookup(user) + old_account = Nola.Account.lookup(user) user = %User{user | nick: new_nick, nicks: [old_nick|user.nicks]} - account = IRC.Account.lookup(user, false) || old_account + account = Nola.Account.lookup(user, false) || old_account user = %User{user | nick: new_nick, account: account.id, nicks: [old_nick|user.nicks]} Storage.insert(User.to_tuple(user)) channels = for {channel, _} <- user.privileges, do: channel diff --git a/lib/matrix/room.ex b/lib/matrix/room.ex index 12fabc8..4c5cf7e 100644 --- a/lib/matrix/room.ex +++ b/lib/matrix/room.ex @@ -172,12 +172,12 @@ defmodule Nola.Matrix.Room do end def get_account(%{user_id: user_id}, %{id: id}) do - IRC.Account.find_by_nick("matrix", user_id) + Nola.Account.find_by_nick("matrix", user_id) end defp introduce_irc_account(account_id, state) do mxid = Matrix.get_or_create_matrix_user(account_id) - account = IRC.Account.get(account_id) + account = Nola.Account.get(account_id) user = IRC.UserTrack.find_by_account(state.network, account) base_nick = if(user, do: user.nick, else: account.name) case Client.Profile.put_displayname(client(user_id: mxid), base_nick) do diff --git a/lib/nola/account.ex b/lib/nola/account.ex new file mode 100644 index 0000000..cd424ef --- /dev/null +++ b/lib/nola/account.ex @@ -0,0 +1,263 @@ +defmodule Nola.Account do + alias IRC.UserTrack.User + + @moduledoc """ + Account registry.... + + Maps a network predicate: + * `{net, {:nick, nickname}}` + * `{net, {:account, account}}` + * `{net, {:mask, user@host}}` + to an unique identifier, that can be shared over multiple networks. + + If a predicate cannot be found for an existing account, a new account will be made in the database. + + To link two existing accounts from different network onto a different one, a merge operation is provided. + + """ + + # FIXME: Ensure uniqueness of name? + + @derive {Poison.Encoder, except: [:token]} + defstruct [:id, :name, :token] + @type t :: %__MODULE__{id: id(), name: String.t()} + @type id :: String.t() + + defimpl Inspect, for: __MODULE__ do + import Inspect.Algebra + + def inspect(%{id: id, name: name}, opts) do + concat(["#Nola.Account[", id, " ", name, "]"]) + end + end + + def file(base) do + to_charlist(Nola.data_path() <> "/account_#{base}.dets") + end + + defp from_struct(%__MODULE__{id: id, name: name, token: token}) do + {id, name, token} + end + + defp from_tuple({id, name, token}) do + %__MODULE__{id: id, name: name, token: token} + end + + def start_link() do + GenServer.start_link(__MODULE__, [], [name: __MODULE__]) + end + + def init(_) do + {:ok, accounts} = :dets.open_file(file("db"), []) + {:ok, meta} = :dets.open_file(file("meta"), []) + {:ok, predicates} = :dets.open_file(file("predicates"), [{:type, :set}]) + {:ok, %{accounts: accounts, meta: meta, predicates: predicates}} + end + + def get(id) do + case :dets.lookup(file("db"), id) do + [account] -> from_tuple(account) + _ -> nil + end + end + + def get_by_name(name) do + spec = [{{:_, :"$1", :_}, [{:==, :"$1", {:const, name}}], [:"$_"]}] + case :dets.select(file("db"), spec) do + [account] -> from_tuple(account) + _ -> nil + end + end + + def get_meta(%__MODULE__{id: id}, key, default \\ nil) do + case :dets.lookup(file("meta"), {id, key}) do + [{_, value}] -> (value || default) + _ -> default + end + end + + @spec find_meta_accounts(String.t()) :: [{account :: t(), value :: String.t()}, ...] + @doc "Find all accounts that have a meta of `key`." + def find_meta_accounts(key) do + spec = [{{{:"$1", :"$2"}, :"$3"}, [{:==, :"$2", {:const, key}}], [{{:"$1", :"$3"}}]}] + for {id, val} <- :dets.select(file("meta"), spec), do: {get(id), val} + end + + @doc "Find an account given a specific meta `key` and `value`." + @spec find_meta_account(String.t(), String.t()) :: t() | nil + def find_meta_account(key, value) do + #spec = [{{{:"$1", :"$2"}, :"$3"}, [:andalso, {:==, :"$2", {:const, key}}, {:==, :"$3", {:const, value}}], [:"$1"]}] + spec = [{{{:"$1", :"$2"}, :"$3"}, [{:andalso, {:==, :"$2", {:const, key}}, {:==, {:const, value}, :"$3"}}], [:"$1"]}] + case :dets.select(file("meta"), spec) do + [id] -> get(id) + _ -> nil + end + end + + def get_all_meta(%__MODULE__{id: id}) do + spec = [{{{:"$1", :"$2"}, :"$3"}, [{:==, :"$1", {:const, id}}], [{{:"$2", :"$3"}}]}] + :dets.select(file("meta"), spec) + end + + def put_user_meta(account = %__MODULE__{}, key, value) do + put_meta(account, "u:"<>key, value) + end + + def put_meta(%__MODULE__{id: id}, key, value) do + :dets.insert(file("meta"), {{id, key}, value}) + end + + def delete_meta(%__MODULE__{id: id}, key) do + :dets.delete(file("meta"), {id, key}) + end + + def all_accounts() do + :dets.traverse(file("db"), fn(obj) -> {:continue, from_tuple(obj)} end) + end + + def all_predicates() do + :dets.traverse(file("predicates"), fn(obj) -> {:continue, obj} end) + end + + def all_meta() do + :dets.traverse(file("meta"), fn(obj) -> {:continue, obj} end) + end + + def merge_account(old_id, new_id) do + if old_id != new_id do + spec = [{{:"$1", :"$2"}, [{:==, :"$2", {:const, old_id}}], [:"$1"]}] + predicates = :dets.select(file("predicates"), spec) + for pred <- predicates, do: :ok = :dets.insert(file("predicates"), {pred, new_id}) + spec = [{{{:"$1", :"$2"}, :"$3"}, [{:==, :"$1", {:const, old_id}}], [{{:"$2", :"$3"}}]}] + metas = :dets.select(file("meta"), spec) + for {k,v} <- metas do + :dets.delete(file("meta"), {{old_id, k}}) + :ok = :dets.insert(file("meta"), {{new_id, k}, v}) + end + :dets.delete(file("db"), old_id) + IRC.Membership.merge_account(old_id, new_id) + IRC.UserTrack.merge_account(old_id, new_id) + IRC.Connection.dispatch("account", {:account_change, old_id, new_id}) + IRC.Connection.dispatch("conn", {:account_change, old_id, new_id}) + end + :ok + end + + @doc "Find an account by a logged in user" + def find_by_nick(network, nick) do + do_lookup(%ExIRC.SenderInfo{nick: nick, network: network}, false) + end + + @doc "Always find an account by nickname, even if offline. Uses predicates and then account name." + def find_always_by_nick(network, chan, nick) do + with \ + nil <- find_by_nick(network, nick), + nil <- do_lookup(%User{network: network, nick: nick}, false), + nil <- get_by_name(nick) + do + nil + else + %__MODULE__{} = account -> + memberships = IRC.Membership.of_account(account) + if Enum.any?(memberships, fn({net, ch}) -> (net == network) or (chan && chan == ch) end) do + account + else + nil + end + end + end + + def find(something) do + do_lookup(something, false) + end + + def lookup(something, make_default \\ true) do + account = do_lookup(something, make_default) + if account && Map.get(something, :nick) do + IRC.Connection.dispatch("account", {:account_auth, Map.get(something, :nick), account.id}) + end + account + end + + def handle_info(_, state) do + {:noreply, state} + end + + def handle_cast(_, state) do + {:noreply, state} + end + + def handle_call(_, _, state) do + {:noreply, state} + end + + def terminate(_, state) do + for {_, dets} <- state do + :dets.sync(dets) + :dets.close(dets) + end + end + + defp do_lookup(message = %IRC.Message{account: account_id}, make_default) when is_binary(account_id) do + get(account_id) + end + + defp do_lookup(sender = %ExIRC.Who{}, make_default) do + if user = IRC.UserTrack.find_by_nick(sender) do + lookup(user, make_default) + else + #FIXME this will never work with continued lookup by other methods as Who isn't compatible + lookup_by_nick(sender, :dets.lookup(file("predicates"), {sender.network,{:nick, sender.nick}}), make_default) + end + end + + defp do_lookup(sender = %ExIRC.SenderInfo{}, make_default) do + lookup(IRC.UserTrack.find_by_nick(sender), make_default) + end + + defp do_lookup(user = %User{account: id}, make_default) when is_binary(id) do + get(id) + end + + defp do_lookup(user = %User{network: server, nick: nick}, make_default) do + lookup_by_nick(user, :dets.lookup(file("predicates"), {server,{:nick, nick}}), make_default) + end + + defp do_lookup(nil, _) do + nil + end + + defp lookup_by_nick(_, [{_, id}], _make_default) do + get(id) + end + + defp lookup_by_nick(user, _, make_default) do + #authenticate_by_host(user) + if make_default, do: new_account(user), else: nil + end + + def new_account(nick) do + id = EntropyString.large_id() + :dets.insert(file("db"), {id, nick, EntropyString.token()}) + get(id) + end + + def new_account(%{nick: nick, network: server}) do + id = EntropyString.large_id() + :dets.insert(file("db"), {id, nick, EntropyString.token()}) + :dets.insert(file("predicates"), {{server, {:nick, nick}}, id}) + get(id) + end + + def update_account_name(account = %__MODULE__{id: id}, name) do + account = %__MODULE__{account | name: name} + :dets.insert(file("db"), from_struct(account)) + get(id) + end + + def get_predicates(%__MODULE__{} = account) do + spec = [{{:"$1", :"$2"}, [{:==, :"$2", {:const, account.id}}], [:"$1"]}] + :dets.select(file("predicates"), spec) + end + +end diff --git a/lib/plugins/account.ex b/lib/plugins/account.ex index 245710a..747fbc7 100644 --- a/lib/plugins/account.ex +++ b/lib/plugins/account.ex @@ -40,7 +40,7 @@ defmodule Nola.Plugins.Account do def handle_info({:irc, :text, m = %IRC.Message{account: account, text: "auth"}}, state) do spec = [{{:"$1", :"$2"}, [{:==, :"$2", {:const, account.id}}], [:"$1"]}] - predicates = :dets.select(IRC.Account.file("predicates"), spec) + predicates = :dets.select(Nola.Account.file("predicates"), spec) text = for {net, {key, value}} <- predicates, do: "#{net}: #{to_string(key)}: #{value}" m.replyfun.(text) {:noreply, state} @@ -57,7 +57,7 @@ defmodule Nola.Plugins.Account do end def handle_info({:irc, :text, m = %IRC.Message{account: account, text: "account"}}, state) do - account = IRC.Account.lookup(m.sender) + account = Nola.Account.lookup(m.sender) text = ["Account Id: #{account.id}", "Authenticate to this account from another network: \"auth #{account.id} #{account.token}\" to the other bot!"] m.replyfun.(text) @@ -65,7 +65,7 @@ defmodule Nola.Plugins.Account do end def handle_info({:irc, :text, m = %IRC.Message{sender: sender, text: "auth"<>_}}, state) do - #account = IRC.Account.lookup(m.sender) + #account = Nola.Account.lookup(m.sender) case String.split(m.text, " ") do ["auth", id, token] -> join_account(m, id, token) @@ -76,14 +76,14 @@ defmodule Nola.Plugins.Account do end def handle_info({:irc, :text, m = %IRC.Message{account: account, text: "set-name "<>name}}, state) do - IRC.Account.update_account_name(account, name) + Nola.Account.update_account_name(account, name) m.replyfun.("Name changed: #{name}") {:noreply, state} end def handle_info({:irc, :text, m = %IRC.Message{text: "disable-sms"}}, state) do - if IRC.Account.get_meta(m.account, "sms-number") do - IRC.Account.delete_meta(m.account, "sms-number") + if Nola.Account.get_meta(m.account, "sms-number") do + Nola.Account.delete_meta(m.account, "sms-number") m.replfyun.("SMS disabled.") else m.replyfun.("SMS already disabled.") @@ -100,8 +100,8 @@ defmodule Nola.Plugins.Account do def handle_info({:irc, :text, m = %IRC.Message{text: "enable-sms"}}, state) do code = String.downcase(EntropyString.small_id()) - IRC.Account.put_meta(m.account, "sms-validation-code", code) - IRC.Account.put_meta(m.account, "sms-validation-target", m.network) + Nola.Account.put_meta(m.account, "sms-validation-code", code) + Nola.Account.put_meta(m.account, "sms-validation-target", m.network) number = Nola.IRC.Sms.my_number() text = "To enable or change your number for SMS messaging, please send:" <> " \"enable #{code}\" to #{number}" @@ -111,9 +111,9 @@ defmodule Nola.Plugins.Account do def handle_info({:irc, :text, m = %IRC.Message{text: "enable-telegram"}}, state) do code = String.downcase(EntropyString.small_id()) - IRC.Account.delete_meta(m.account, "telegram-id") - IRC.Account.put_meta(m.account, "telegram-validation-code", code) - IRC.Account.put_meta(m.account, "telegram-validation-target", m.network) + Nola.Account.delete_meta(m.account, "telegram-id") + Nola.Account.put_meta(m.account, "telegram-validation-code", code) + Nola.Account.put_meta(m.account, "telegram-validation-target", m.network) text = "To enable or change your number for telegram messaging, please open #{Nola.Telegram.my_path()} and send:" <> " \"/enable #{code}\"" m.replyfun.(text) @@ -130,14 +130,14 @@ defmodule Nola.Plugins.Account do def handle_info({:irc, :text, m = %IRC.Message{text: "getmeta"<>_}}, state) do result = case String.split(m.text, " ") do ["getmeta"] -> - for {k, v} <- IRC.Account.get_all_meta(m.account) do + for {k, v} <- Nola.Account.get_all_meta(m.account) do case k do "u:"<>key -> "(user) #{key}: #{v}" key -> "#{key}: #{v}" end end ["getmeta", key] -> - value = IRC.Account.get_meta(m.account, key) + value = Nola.Account.get_meta(m.account, key) text = if value do "#{key}: #{value}" else @@ -153,7 +153,7 @@ defmodule Nola.Plugins.Account do def handle_info({:irc, :text, m = %IRC.Message{text: "setusermeta"<>_}}, state) do result = case String.split(m.text, " ") do ["setusermeta", key, value] -> - IRC.Account.put_user_meta(m.account, key, value) + Nola.Account.put_user_meta(m.account, key, value) "ok" _ -> "usage: setusermeta " @@ -167,10 +167,10 @@ defmodule Nola.Plugins.Account do end defp join_account(m, id, token) do - old_account = IRC.Account.lookup(m.sender) - new_account = IRC.Account.get(id) + old_account = Nola.Account.lookup(m.sender) + new_account = Nola.Account.get(id) if new_account && token == new_account.token do - case IRC.Account.merge_account(old_account.id, new_account.id) do + case Nola.Account.merge_account(old_account.id, new_account.id) do :ok -> if old_account.id == new_account.id do m.replyfun.("Already authenticated, but hello") diff --git a/lib/plugins/alcoolog.ex b/lib/plugins/alcoolog.ex index 8ef83fa..9958889 100644 --- a/lib/plugins/alcoolog.ex +++ b/lib/plugins/alcoolog.ex @@ -467,7 +467,7 @@ defmodule Nola.Plugins.Alcoolog do end def get_all_stats() do - IRC.Account.all_accounts() + Nola.Account.all_accounts() |> Enum.map(fn(account) -> {account.id, get_full_statistics(account.id)} end) |> Enum.filter(fn({_nick, status}) -> status && (status.active > 0 || status.active30m > 0) end) |> Enum.sort_by(fn({_, status}) -> status.active end, &>/2) @@ -489,7 +489,7 @@ defmodule Nola.Plugins.Alcoolog do |> Enum.sort_by(fn({_, status}) -> status.active end, &>/2) end - @spec since() :: %{IRC.Account.id() => DateTime.t()} + @spec since() :: %{Nola.Account.id() => DateTime.t()} @doc "Returns the last time the user was at 0 g/l" def since() do :ets.foldr(fn({{acct, timestamp_or_date}, _vol, current, _cl, _deg, _name, _comment, _m}, acc) -> @@ -612,7 +612,7 @@ defmodule Nola.Plugins.Alcoolog do def handle_info({:irc, :trigger, "sobre", m = %IRC.Message{trigger: %IRC.Trigger{args: args, type: :bang}}}, state) do account = case args do - [nick] -> IRC.Account.find_always_by_nick(m.network, m.channel, nick) + [nick] -> Nola.Account.find_always_by_nick(m.network, m.channel, nick) [] -> m.account end @@ -784,7 +784,7 @@ defmodule Nola.Plugins.Alcoolog do end) |> Enum.sort_by(fn({_nick, count}) -> count end, &>/2) |> Enum.map(fn({nick, count}) -> - account = IRC.Account.get(nick) + account = Nola.Account.get(nick) user = IRC.UserTrack.find_by_account(m.network, account) nick = if(user, do: user.nick, else: account.name) "#{nick}: #{Float.round(count, 4)}" @@ -864,7 +864,7 @@ defmodule Nola.Plugins.Alcoolog do def handle_info({:irc, :trigger, "alcoolisme", m = %IRC.Message{trigger: %IRC.Trigger{args: args, type: :bang}}}, state) do {account, duration} = case args do - [nick | rest] -> {IRC.Account.find_always_by_nick(m.network, m.channel, nick), rest} + [nick | rest] -> {Nola.Account.find_always_by_nick(m.network, m.channel, nick), rest} [] -> {m.account, []} end if account do diff --git a/lib/plugins/alcoolog_announcer.ex b/lib/plugins/alcoolog_announcer.ex index 167fc01..2baa999 100644 --- a/lib/plugins/alcoolog_announcer.ex +++ b/lib/plugins/alcoolog_announcer.ex @@ -234,7 +234,7 @@ defmodule Nola.Plugins.AlcoologAnnouncer do end if message do #IO.puts("#{acct}: #{message}") - account = IRC.Account.get(acct) + account = Nola.Account.get(acct) for {net, chan} <- IRC.Membership.notify_channels(account) do user = IRC.UserTrack.find_by_account(net, account) nick = if(user, do: user.nick, else: account.name) diff --git a/lib/plugins/last_fm.ex b/lib/plugins/last_fm.ex index ae4b1ee..4607cbe 100644 --- a/lib/plugins/last_fm.ex +++ b/lib/plugins/last_fm.ex @@ -90,7 +90,7 @@ defmodule Nola.Plugins.LastFm do defp irc_now_playing(nick_or_user, message, state) do nick_or_user = String.strip(nick_or_user) - id_or_user = if account = IRC.Account.get(nick_or_user) || IRC.Account.find_always_by_nick(message.network, message.channel, nick_or_user) do + id_or_user = if account = Nola.Account.get(nick_or_user) || Nola.Account.find_always_by_nick(message.network, message.channel, nick_or_user) do account.id else nick_or_user @@ -107,7 +107,7 @@ defmodule Nola.Plugins.LastFm do {:ok, map} when is_map(map) -> track = fetch_track(username, map) text = format_now_playing(map, track) - user = if account = IRC.Account.get(id_or_user) do + user = if account = Nola.Account.get(id_or_user) do user = IRC.UserTrack.find_by_account(message.network, account) if(user, do: user.nick, else: account.name) else diff --git a/lib/plugins/preums.ex b/lib/plugins/preums.ex index f7738ad..c55248d 100644 --- a/lib/plugins/preums.ex +++ b/lib/plugins/preums.ex @@ -95,10 +95,10 @@ defmodule Nola.Plugins.Preums do case key do {{net, {bork,chan}}, date} -> :dets.delete(table, key) - nick = if IRC.Account.get(nick) do + nick = if Nola.Account.get(nick) do nick else - if acct = IRC.Account.find_always_by_nick(net, nil, nick) do + if acct = Nola.Account.find_always_by_nick(net, nil, nick) do acct.id else nick @@ -108,8 +108,8 @@ defmodule Nola.Plugins.Preums do {{_net, nil}, _} -> :dets.delete(table, key) {{net, chan}, date} -> - if !IRC.Account.get(nick) do - if acct = IRC.Account.find_always_by_nick(net, chan, nick) do + if !Nola.Account.get(nick) do + if acct = Nola.Account.find_always_by_nick(net, chan, nick) do :dets.delete(table, key) :dets.insert(table, { { {net,chan}, date }, acct.id, now, perfect, text}) end @@ -142,7 +142,7 @@ defmodule Nola.Plugins.Preums do if item do {_, account_id, date, _perfect, text} = item h = "#{date.hour}:#{date.minute}:#{date.second}" - account = IRC.Account.get(account_id) + account = Nola.Account.get(account_id) user = IRC.UserTrack.find_by_account(m.network, account) nick = if(user, do: user.nick, else: account.name) m.replyfun.("preums: #{nick} à #{h}: “#{text}”") @@ -156,7 +156,7 @@ defmodule Nola.Plugins.Preums do state = handle_preums(m, state) top = topnicks(state.dets, channel, sort_by: :score) |> Enum.map(fn({account_id, {count, score}}) -> - account = IRC.Account.get(account_id) + account = Nola.Account.get(account_id) user = IRC.UserTrack.find_by_account(m.network, account) nick = if(user, do: user.nick, else: account.name) "#{nick}: #{score} (#{count})" diff --git a/lib/plugins/quatre_cent_vingt.ex b/lib/plugins/quatre_cent_vingt.ex index 59b2dea..254f5ce 100644 --- a/lib/plugins/quatre_cent_vingt.ex +++ b/lib/plugins/quatre_cent_vingt.ex @@ -65,7 +65,7 @@ defmodule Nola.Plugins.QuatreCentVingt do end def handle_info({:irc, :trigger, "420", m = %IRC.Message{trigger: %IRC.Trigger{args: [nick], type: :bang}}}, dets) do - account = IRC.Account.find_by_nick(m.network, nick) + account = Nola.Account.find_by_nick(m.network, nick) if account do text = case get_statistics_for_nick(dets, m.account.id) do {0, _} -> "#{nick} n'a jamais !420 ... honte à lui." diff --git a/lib/plugins/sms.ex b/lib/plugins/sms.ex index bc666b2..afc1eb1 100644 --- a/lib/plugins/sms.ex +++ b/lib/plugins/sms.ex @@ -10,20 +10,20 @@ defmodule Nola.Plugins.Sms do def incoming(from, "enable "<>key) do key = String.trim(key) - account = IRC.Account.find_meta_account("sms-validation-code", String.downcase(key)) + account = Nola.Account.find_meta_account("sms-validation-code", String.downcase(key)) if account do - net = IRC.Account.get_meta(account, "sms-validation-target") - IRC.Account.put_meta(account, "sms-number", from) - IRC.Account.delete_meta(account, "sms-validation-code") - IRC.Account.delete_meta(account, "sms-validation-number") - IRC.Account.delete_meta(account, "sms-validation-target") + net = Nola.Account.get_meta(account, "sms-validation-target") + Nola.Account.put_meta(account, "sms-number", from) + Nola.Account.delete_meta(account, "sms-validation-code") + Nola.Account.delete_meta(account, "sms-validation-number") + Nola.Account.delete_meta(account, "sms-validation-target") IRC.Connection.broadcast_message(net, account, "SMS Number #{from} added!") send_sms(from, "Yay! Number linked to account #{account.name}") end end def incoming(from, message) do - account = IRC.Account.find_meta_account("sms-number", from) + account = Nola.Account.find_meta_account("sms-number", from) if account do reply_fun = fn(text) -> send_sms(from, text) @@ -98,8 +98,8 @@ defmodule Nola.Plugins.Sms do def handle_info({:irc, :trigger, "sms", m = %IRC.Message{trigger: %IRC.Trigger{type: :bang, args: [nick | text]}}}, state) do with \ {:tree, false} <- {:tree, m.sender.nick == "Tree"}, - {_, %IRC.Account{} = account} <- {:account, IRC.Account.find_always_by_nick(m.network, m.channel, nick)}, - {_, number} when not is_nil(number) <- {:number, IRC.Account.get_meta(account, "sms-number")} + {_, %Nola.Account{} = account} <- {:account, Nola.Account.find_always_by_nick(m.network, m.channel, nick)}, + {_, number} when not is_nil(number) <- {:number, Nola.Account.get_meta(account, "sms-number")} do text = Enum.join(text, " ") sender = if m.channel do diff --git a/lib/plugins/tell.ex b/lib/plugins/tell.ex index 34e9f94..2f874d0 100644 --- a/lib/plugins/tell.ex +++ b/lib/plugins/tell.ex @@ -42,7 +42,7 @@ defmodule Nola.Plugins.Tell do messages = :dets.lookup(state.dets, {network, channel, account_id}) if messages != [] do strs = Enum.map(messages, fn({_, from, message, at}) -> - account = IRC.Account.get(from) + account = Nola.Account.get(from) user = IRC.UserTrack.find_by_account(network, account) fromnick = if user, do: user.nick, else: account.name "#{nick}: <#{fromnick}> #{message}" @@ -81,10 +81,10 @@ defmodule Nola.Plugins.Tell do end defp do_tell(state, m, nick_target, message) do - target = IRC.Account.find_always_by_nick(m.network, m.channel, nick_target) + target = Nola.Account.find_always_by_nick(m.network, m.channel, nick_target) message = Enum.join(message, " ") with \ - {:target, %IRC.Account{} = target} <- {:target, target}, + {:target, %Nola.Account{} = target} <- {:target, target}, {:same, false} <- {:same, target.id == m.account.id}, target_user = IRC.UserTrack.find_by_account(m.network, target), target_nick = if(target_user, do: target_user.nick, else: target.name), diff --git a/lib/plugins/user_mention.ex b/lib/plugins/user_mention.ex index a9a2d24..f26f1d6 100644 --- a/lib/plugins/user_mention.ex +++ b/lib/plugins/user_mention.ex @@ -23,10 +23,10 @@ defmodule Nola.Plugins.UserMention do nick = nick |> String.trim(":") |> String.trim(",") - target = IRC.Account.find_always_by_nick(network, channel, nick) + target = Nola.Account.find_always_by_nick(network, channel, nick) if target do - telegram = IRC.Account.get_meta(target, "telegram-id") - sms = IRC.Account.get_meta(target, "sms-number") + telegram = Nola.Account.get_meta(target, "telegram-id") + sms = Nola.Account.get_meta(target, "sms-number") text = "#{channel} <#{sender.nick}> #{Enum.join(content, " ")}" cond do diff --git a/lib/telegram/room.ex b/lib/telegram/room.ex index 49fc06d..cc10e90 100644 --- a/lib/telegram/room.ex +++ b/lib/telegram/room.ex @@ -72,7 +72,7 @@ defmodule Nola.TelegramRoom do end defp find_or_create_meta_account(from = %{"id" => user_id}, state) do - if account = IRC.Account.find_meta_account("telegram-id", user_id) do + if account = Nola.Account.find_meta_account("telegram-id", user_id) do account else first_name = Map.get(from, "first_name") @@ -84,9 +84,9 @@ defmodule Nola.TelegramRoom do username = Map.get(from, "username", first_name) account = username - |> IRC.Account.new_account() - |> IRC.Account.update_account_name(name) - |> IRC.Account.put_meta("telegram-id", user_id) + |> Nola.Account.new_account() + |> Nola.Account.update_account_name(name) + |> Nola.Account.put_meta("telegram-id", user_id) Logger.info("telegram_room: created account #{account.id} for telegram user #{user_id}") account diff --git a/lib/telegram/telegram.ex b/lib/telegram/telegram.ex index 1c6a9a9..a93f5bb 100644 --- a/lib/telegram/telegram.ex +++ b/lib/telegram/telegram.ex @@ -20,7 +20,7 @@ defmodule Nola.Telegram do end def init(chat_id) do Logger.info("Telegram session starting: #{chat_id}") - account = IRC.Account.find_meta_account("telegram-id", chat_id) + account = Nola.Account.find_meta_account("telegram-id", chat_id) account_id = if account, do: account.id {:ok, %{account: account_id}} end @@ -49,14 +49,14 @@ defmodule Nola.Telegram do # [%{"length" => 7, "offset" => 0, "type" => "bot_command"}], # "from" => %{"first_name" => "J", "id" => 2075406, "is_bot" => false, "language_code" => "en", "username" => "ahref"}, # "message_id" => 11, "text" => "/enable salope"}, "update_id" => 764148578} - account = IRC.Account.find_meta_account("telegram-validation-code", String.downcase(key)) + account = Nola.Account.find_meta_account("telegram-validation-code", String.downcase(key)) text = if account do - net = IRC.Account.get_meta(account, "telegram-validation-target") - IRC.Account.put_meta(account, "telegram-id", m["chat"]["id"]) - IRC.Account.put_meta(account, "telegram-username", m["chat"]["username"]) - IRC.Account.put_meta(account, "telegram-username", m["chat"]["username"]) - IRC.Account.delete_meta(account, "telegram-validation-code") - IRC.Account.delete_meta(account, "telegram-validation-target") + net = Nola.Account.get_meta(account, "telegram-validation-target") + Nola.Account.put_meta(account, "telegram-id", m["chat"]["id"]) + Nola.Account.put_meta(account, "telegram-username", m["chat"]["username"]) + Nola.Account.put_meta(account, "telegram-username", m["chat"]["username"]) + Nola.Account.delete_meta(account, "telegram-validation-code") + Nola.Account.delete_meta(account, "telegram-validation-target") IRC.Connection.broadcast_message(net, account, "Telegram #{m["chat"]["username"]} account added!") "Yay! Linked to account **#{account.name}**." else @@ -102,7 +102,7 @@ defmodule Nola.Telegram do #end def handle_update(%{"callback_query" => cb = %{"data" => "start-upload:"<>target, "id" => id, "message" => m = %{"message_id" => m_id, "chat" => %{"id" => chat_id}, "reply_to_message" => op}}}, t, state) do - account = IRC.Account.find_meta_account("telegram-id", chat_id) + account = Nola.Account.find_meta_account("telegram-id", chat_id) if account do target = case String.split(target, "/") do ["everywhere"] -> IRC.Membership.of_account(account) @@ -162,7 +162,7 @@ defmodule Nola.Telegram do end def handle_update(%{"message" => m = %{"chat" => %{"id" => id, "type" => "private"}, "text" => text}}, _, state) do - account = IRC.Account.find_meta_account("telegram-id", id) + account = Nola.Account.find_meta_account("telegram-id", id) if account do as_irc_message(id, text, account) end @@ -212,7 +212,7 @@ defmodule Nola.Telegram do end defp start_upload(_type, %{"message" => m = %{"chat" => %{"id" => id, "type" => "private"}}}, token, state) do - account = IRC.Account.find_meta_account("telegram-id", id) + account = Nola.Account.find_meta_account("telegram-id", id) if account do text = if(m["text"], do: m["text"], else: nil) targets = IRC.Membership.of_account(account) diff --git a/lib/tmpl.ex b/lib/tmpl.ex index 1470603..f4dac02 100644 --- a/lib/tmpl.ex +++ b/lib/tmpl.ex @@ -43,7 +43,7 @@ defmodule Tmpl do end def account_nick(%{"id" => id, "name" => name}, %{variables: %{"message" => %{"network" => network}}}) do - if user = IRC.UserTrack.find_by_account(network, %IRC.Account{id: id}) do + if user = IRC.UserTrack.find_by_account(network, %Nola.Account{id: id}) do user.nick else name diff --git a/lib/untappd.ex b/lib/untappd.ex index 7ed3e66..e603c25 100644 --- a/lib/untappd.ex +++ b/lib/untappd.ex @@ -32,7 +32,7 @@ defmodule Untappd do end def maybe_checkin(account, beer_id) do - if token = IRC.Account.get_meta(account, "untappd-token") do + if token = Nola.Account.get_meta(account, "untappd-token") do checkin(token, beer_id) else {:error, :no_token} diff --git a/lib/web/context_plug.ex b/lib/web/context_plug.ex index ebededa..7289763 100644 --- a/lib/web/context_plug.ex +++ b/lib/web/context_plug.ex @@ -9,7 +9,7 @@ defmodule NolaWeb.ContextPlug do def get_account(conn) do cond do get_session(conn, :account) -> get_session(conn, :account) - get_session(conn, :oidc_id) -> if account = IRC.Account.find_meta_account("identity-id", get_session(conn, :oidc_id)), do: account.id + get_session(conn, :oidc_id) -> if account = Nola.Account.find_meta_account("identity-id", get_session(conn, :oidc_id)), do: account.id true -> nil end end @@ -17,7 +17,7 @@ defmodule NolaWeb.ContextPlug do def call(conn, opts) do account = with \ {:account, account_id} when is_binary(account_id) <- {:account, get_account(conn)}, - {:account, account} when not is_nil(account) <- {:account, IRC.Account.get(account_id)} + {:account, account} when not is_nil(account) <- {:account, Nola.Account.get(account_id)} do account else @@ -27,7 +27,7 @@ defmodule NolaWeb.ContextPlug do network = Map.get(conn.params, "network") network = if network == "-", do: nil, else: network - oidc_account = IRC.Account.find_meta_account("identity-id", get_session(conn, :oidc_id)) + oidc_account = Nola.Account.find_meta_account("identity-id", get_session(conn, :oidc_id)) conns = IRC.Connection.get_network(network) chan = if c = Map.get(conn.params, "chan") do diff --git a/lib/web/controllers/alcoolog_controller.ex b/lib/web/controllers/alcoolog_controller.ex index 3c812c5..dc09517 100644 --- a/lib/web/controllers/alcoolog_controller.ex +++ b/lib/web/controllers/alcoolog_controller.ex @@ -17,7 +17,7 @@ defmodule NolaWeb.AlcoologController do end def nick(conn = %{assigns: %{account: account}}, params = %{"network" => network, "nick" => nick}) do - profile_account = IRC.Account.find_always_by_nick(network, nick, nick) + profile_account = Nola.Account.find_always_by_nick(network, nick, nick) days = String.to_integer(Map.get(params, "days", "180")) friend? = Enum.member?(IRC.Membership.friends(account), profile_account.id) if friend? do @@ -47,7 +47,7 @@ defmodule NolaWeb.AlcoologController do end def nick_stats_json(conn = %{assigns: %{account: account}}, params = %{"network" => network, "nick" => nick}) do - profile_account = IRC.Account.find_always_by_nick(network, nick, nick) + profile_account = Nola.Account.find_always_by_nick(network, nick, nick) friend? = Enum.member?(IRC.Membership.friends(account), profile_account.id) if friend? do stats = Nola.Plugins.Alcoolog.get_full_statistics(profile_account.id) @@ -63,7 +63,7 @@ defmodule NolaWeb.AlcoologController do end def nick_gls_json(conn = %{assigns: %{account: account}}, params = %{"network" => network, "nick" => nick}) do - profile_account = IRC.Account.find_always_by_nick(network, nick, nick) + profile_account = Nola.Account.find_always_by_nick(network, nick, nick) friend? = Enum.member?(IRC.Membership.friends(account), profile_account.id) count = String.to_integer(Map.get(params, "days", "180")) if friend? do @@ -97,7 +97,7 @@ defmodule NolaWeb.AlcoologController do def nick_volumes_json(conn = %{assigns: %{account: account}}, params = %{"network" => network, "nick" => nick}) do - profile_account = IRC.Account.find_always_by_nick(network, nick, nick) + profile_account = Nola.Account.find_always_by_nick(network, nick, nick) friend? = Enum.member?(IRC.Membership.friends(account), profile_account.id) count = String.to_integer(Map.get(params, "days", "180")) if friend? do @@ -129,7 +129,7 @@ defmodule NolaWeb.AlcoologController do end def nick_log_json(conn = %{assigns: %{account: account}}, %{"network" => network, "nick" => nick}) do - profile_account = IRC.Account.find_always_by_nick(network, nick, nick) + profile_account = Nola.Account.find_always_by_nick(network, nick, nick) friend? = Enum.member?(IRC.Membership.friends(account), profile_account.id) if friend? do history = for {{nick, ts}, points, active, cl, deg, type, descr, meta} <- Nola.Plugins.Alcoolog.nick_history(profile_account) do @@ -160,7 +160,7 @@ defmodule NolaWeb.AlcoologController do end def nick_history_json(conn = %{assigns: %{account: account}}, %{"network" => network, "nick" => nick}) do - profile_account = IRC.Account.find_always_by_nick(network, nick, nick) + profile_account = Nola.Account.find_always_by_nick(network, nick, nick) friend? = Enum.member?(IRC.Membership.friends(account), profile_account.id) if friend? do history = for {_, date, value} <- Nola.Plugs.AlcoologAnnouncer.log(profile_account) do @@ -300,7 +300,7 @@ defmodule NolaWeb.AlcoologController do end def minisync(conn, %{"user_id" => user_id, "key" => key, "value" => value}) do - account = IRC.Account.get(user_id) + account = Nola.Account.get(user_id) if account do ds = Nola.Plugins.Alcoolog.data_state() meta = Nola.Plugins.Alcoolog.get_user_meta(ds, account.id) diff --git a/lib/web/controllers/open_id_controller.ex b/lib/web/controllers/open_id_controller.ex index d3fef5d..24dc1a5 100644 --- a/lib/web/controllers/open_id_controller.ex +++ b/lib/web/controllers/open_id_controller.ex @@ -22,10 +22,10 @@ defmodule NolaWeb.OpenIdController do {:ok, %{"sub" => id, "preferred_username" => username}} <- Jason.decode(body) do if account = conn.assigns.account do - if !IRC.Account.get_meta(account, "identity-id") do # XXX: And oidc id not linked yet - IRC.Account.put_meta(account, "identity-id", id) + if !Nola.Account.get_meta(account, "identity-id") do # XXX: And oidc id not linked yet + Nola.Account.put_meta(account, "identity-id", id) end - IRC.Account.put_meta(account, "identity-username", username) + Nola.Account.put_meta(account, "identity-username", username) conn else conn diff --git a/lib/web/controllers/page_controller.ex b/lib/web/controllers/page_controller.ex index 2ac4d0a..c5d0a57 100644 --- a/lib/web/controllers/page_controller.ex +++ b/lib/web/controllers/page_controller.ex @@ -25,8 +25,8 @@ defmodule NolaWeb.PageController do def index(conn = %{assigns: %{account: account}}, _) do memberships = IRC.Membership.of_account(account) users = IRC.UserTrack.find_by_account(account) - metas = IRC.Account.get_all_meta(account) - predicates = IRC.Account.get_predicates(account) + metas = Nola.Account.get_all_meta(account) + predicates = Nola.Account.get_predicates(account) conn |> assign(:title, account.name) |> render("user.html", users: users, memberships: memberships, metas: metas, predicates: predicates) @@ -42,7 +42,7 @@ defmodule NolaWeb.PageController do def authenticate(conn, _) do 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)} + {:account, account} when not is_nil(account) <- {:account, Nola.Account.get(account_id)} do assign(conn, :account, account) else diff --git a/lib/web/controllers/untappd_controller.ex b/lib/web/controllers/untappd_controller.ex index d3a540d..e2e1596 100644 --- a/lib/web/controllers/untappd_controller.ex +++ b/lib/web/controllers/untappd_controller.ex @@ -4,10 +4,10 @@ defmodule NolaWeb.UntappdController do def callback(conn, %{"code" => code}) do 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)}, + {:account, account} when not is_nil(account) <- {:account, Nola.Account.get(account_id)}, {:ok, auth_token} <- Untappd.auth_callback(code) do - IRC.Account.put_meta(account, "untappd-token", auth_token) + Nola.Account.put_meta(account, "untappd-token", auth_token) text(conn, "OK!") else {:account, _} -> text(conn, "Error: account not found") diff --git a/lib/web/live/chat_live.ex b/lib/web/live/chat_live.ex index 88afa0b..0513cc2 100644 --- a/lib/web/live/chat_live.ex +++ b/lib/web/live/chat_live.ex @@ -6,8 +6,8 @@ defmodule NolaWeb.ChatLive do def mount(%{"network" => network, "chan" => chan}, %{"account" => account_id}, socket) do chan = NolaWeb.reformat_chan(chan) connection = IRC.Connection.get_network(network, chan) - account = IRC.Account.get(account_id) - membership = IRC.Membership.of_account(IRC.Account.get("DRgpD4fLf8PDJMLp8Dtb")) + account = Nola.Account.get(account_id) + membership = IRC.Membership.of_account(Nola.Account.get("DRgpD4fLf8PDJMLp8Dtb")) if account && connection && Enum.member?(membership, {connection.network, chan}) do {:ok, _} = Registry.register(Nola.PubSub, "#{connection.network}:events", plugin: __MODULE__) for t <- ["messages", "triggers", "outputs", "events"] do @@ -47,7 +47,7 @@ defmodule NolaWeb.ChatLive do end def handle_event("send", %{"message" => %{"text" => text}}, socket) do - account = IRC.Account.get(socket.assigns.account_id) + account = Nola.Account.get(socket.assigns.account_id) IRC.send_message_as(account, socket.assigns.network, socket.assigns.channel, text, true) {:noreply, assign(socket, :counter, socket.assigns.counter + 1)} end -- cgit v1.2.3