summaryrefslogtreecommitdiff
path: root/lib/irc/user_track.ex
diff options
context:
space:
mode:
Diffstat (limited to 'lib/irc/user_track.ex')
-rw-r--r--lib/irc/user_track.ex43
1 files changed, 28 insertions, 15 deletions
diff --git a/lib/irc/user_track.ex b/lib/irc/user_track.ex
index 4b1ee67..1b51266 100644
--- a/lib/irc/user_track.ex
+++ b/lib/irc/user_track.ex
@@ -20,7 +20,7 @@ defmodule IRC.UserTrack do
def clear_network(network) do
op(fn(ets) ->
spec = [
- {{:_, :"$1", :_, :_, :_, :_, :_, :_, :_, :_, :_},
+ {{:_, :"$1", :_, :_, :_, :_, :_, :_, :_, :_, :_, :_},
[
{:==, :"$1", {:const, network}}
], [:"$_"]}
@@ -61,22 +61,22 @@ defmodule IRC.UserTrack do
defmodule Id, do: use EntropyString
defmodule User do
- defstruct [:id, :account, :network, :nick, {:nicks, []}, :username, :host, :realname, {:privileges, %{}}, {:last_active, %{}}]
+ defstruct [:id, :account, :network, :nick, {:nicks, []}, :username, :host, :realname, {:privileges, %{}}, {:last_active, %{}}, {:options, %{}}]
def to_tuple(u = %__MODULE__{}) do
- {u.id || IRC.UserTrack.Id.large_id, u.network, u.account, String.downcase(u.nick), u.nick, u.nicks || [], u.username, u.host, u.realname, u.privileges, u.last_active}
+ {u.id || IRC.UserTrack.Id.large_id, u.network, u.account, String.downcase(u.nick), u.nick, u.nicks || [], u.username, u.host, u.realname, u.privileges, u.last_active, u.options}
end
#tuple size: 11
- def from_tuple({id, network, account, _downcased_nick, nick, nicks, username, host, realname, privs, last_active}) do
- struct = %__MODULE__{id: id, account: account, network: network, nick: nick, nicks: nicks, username: username, host: host, realname: realname, privileges: privs, last_active: last_active}
+ def from_tuple({id, network, account, _downcased_nick, nick, nicks, username, host, realname, privs, last_active, opts}) do
+ struct = %__MODULE__{id: id, account: account, network: network, nick: nick, nicks: nicks, username: username, host: host, realname: realname, privileges: privs, last_active: last_active, options: opts}
end
end
def find_by_account(%IRC.Account{id: id}) do
#iex(15)> :ets.fun2ms(fn(obj = {_, net, acct, _, _, _, _, _, _}) when net == network and acct == account -> obj end)
spec = [
- {{:_, :_, :"$2", :_, :_, :_, :_, :_, :_, :_, :_},
+ {{:_, :_, :"$2", :_, :_, :_, :_, :_, :_, :_, :_, :_},
[
{:==, :"$2", {:const, id}}
], [:"$_"]}
@@ -91,7 +91,7 @@ defmodule IRC.UserTrack do
def find_by_account(network, %IRC.Account{id: id}) do
#iex(15)> :ets.fun2ms(fn(obj = {_, net, acct, _, _, _, _, _, _}) when net == network and acct == account -> obj end)
spec = [
- {{:_, :"$1", :"$2", :_, :_, :_, :_, :_, :_, :_, :_},
+ {{:_, :"$1", :"$2", :_, :_, :_, :_, :_, :_, :_, :_, :_},
[
{:andalso, {:==, :"$1", {:const, network}},
{:==, :"$2", {:const, id}}}
@@ -100,7 +100,10 @@ defmodule IRC.UserTrack do
case :ets.select(@ets, spec) do
results = [_r | _] ->
results
- |> Enum.sort_by(fn({_, _, _, _, _, _, _, _, _, _, actives}) ->
+ |> Enum.reject(fn({_, net, _, _, _, _, _, _, _, _, actives, opts}) -> net == "matrix" end)
+ |> Enum.reject(fn({_, net, _, _, _, _, _, _, _, _, actives, opts}) -> net == "telegram" end)
+ |> Enum.reject(fn({_, _, _, _, _, _, _, _, _, _, actives, opts}) -> Map.get(opts, :puppet) end)
+ |> Enum.sort_by(fn({_, _, _, _, _, _, _, _, _, _, actives, _}) ->
Map.get(actives, nil)
end, {:desc, NaiveDateTime})
|> List.first
@@ -117,14 +120,14 @@ defmodule IRC.UserTrack do
def merge_account(old_id, new_id) do
#iex(15)> :ets.fun2ms(fn(obj = {_, net, acct, _, _, _, _, _, _}) when net == network and acct == account -> obj end)
spec = [
- {{:_, :_, :"$1", :_, :_, :_, :_, :_, :_, :_, :_},
+ {{:_, :_, :"$1", :_, :_, :_, :_, :_, :_, :_, :_, :_},
[
{:==, :"$1", {:const, old_id}}
], [:"$_"]}
]
- Enum.each(:ets.select(@ets, spec), fn({id, net, _, downcased_nick, nick, nicks, username, host, realname, privs, active}) ->
+ Enum.each(:ets.select(@ets, spec), fn({id, net, _, downcased_nick, nick, nicks, username, host, realname, privs, active, opts}) ->
Storage.op(fn(ets) ->
- :ets.insert(@ets, {id, net, new_id, downcased_nick, nick, nicks, username, host, realname, privs, active})
+ :ets.insert(@ets, {id, net, new_id, downcased_nick, nick, nicks, username, host, realname, privs, active, opts})
end)
end)
end
@@ -139,7 +142,7 @@ defmodule IRC.UserTrack do
end
def find_by_nick(network, nick) do
- case :ets.match(@ets, {:"$1", network, :_, String.downcase(nick), :_, :_, :_, :_, :_, :_, :_}) do
+ case :ets.match(@ets, {:"$1", network, :_, String.downcase(nick), :_, :_, :_, :_, :_, :_, :_, :_}) do
[[id]] -> lookup(id)
_ ->
nil
@@ -165,13 +168,24 @@ defmodule IRC.UserTrack do
end
def channel(network, channel) do
- Enum.filter(to_list(), fn({_, network, _, _, _, _, _, _, _, channels, _}) ->
+ Enum.filter(to_list(), fn({_, network, _, _, _, _, _, _, _, channels, _, _}) ->
Map.get(channels, channel)
end)
end
# TODO
- def connected(sender = %{nick: nick}) do
+ def connected(network, nick, user, host, account_id, opts \\ %{}) do
+ if account = IRC.Account.get(account_id) do
+ user = %User{id: IRC.UserTrack.Id.large_id, account: account_id, network: network, nick: nick, username: user, host: host, privileges: %{}, options: opts}
+ Storage.op(fn(ets) ->
+ :ets.insert(ets, User.to_tuple(user))
+ end)
+
+ IRC.Connection.publish_event(network, %{type: :connect, user_id: user.id, account_id: user.account})
+ :ok
+ else
+ :error
+ end
end
def joined(c, s), do: joined(c,s,[])
@@ -302,5 +316,4 @@ defmodule IRC.UserTrack do
defp userchans(%{privileges: privileges}) do
for({chan, _} <- privileges, do: chan)
end
-
end