diff options
Diffstat (limited to 'lib/irc/client/command')
-rw-r--r-- | lib/irc/client/command/account.ex | 18 | ||||
-rw-r--r-- | lib/irc/client/command/away.ex | 25 | ||||
-rw-r--r-- | lib/irc/client/command/chghost.ex | 15 | ||||
-rw-r--r-- | lib/irc/client/command/invite.ex | 21 | ||||
-rw-r--r-- | lib/irc/client/command/join.ex | 40 | ||||
-rw-r--r-- | lib/irc/client/command/names.ex | 22 | ||||
-rw-r--r-- | lib/irc/client/command/who.ex | 62 |
7 files changed, 203 insertions, 0 deletions
diff --git a/lib/irc/client/command/account.ex b/lib/irc/client/command/account.ex new file mode 100644 index 0000000..c5ec1a9 --- /dev/null +++ b/lib/irc/client/command/account.ex @@ -0,0 +1,18 @@ +defmodule Irc.Client.Command.Account do + alias Irc.Parser.Line + + @type t :: logout :: {:account, Irc.Mask.t} | login :: {:account, Irc.Mask.t, String.t} + + def init() do + {"ACCOUNT", nil, "account-notify"} + end + + def handle_line(%Line{command: "ACCOUNT", source: target, args: ["*"]}) do + {:event, {:account, target}} + end + + def handle_line(%Line{command: "ACCOUNT", source: target, args: [account_name]}) do + {:event, {:account, target, account_name}} + end + +end diff --git a/lib/irc/client/command/away.ex b/lib/irc/client/command/away.ex new file mode 100644 index 0000000..3b6b38e --- /dev/null +++ b/lib/irc/client/command/away.ex @@ -0,0 +1,25 @@ +defmodule Irc.Client.Command.Away do + alias Irc.Parser.Line + + @type t :: away :: {:away, Irc.Mask.t, String.t} | unaway :: {:away, Irc.Mask.t} + + def init() do + {"AWAY", :away, "away-notify"} + end + + def handle_command(:away, args, _) do + command = case args do + [] -> ['AWAY'] + [message] -> ['AWAY :', message] + end + {:send, command} + end + + def hanle_line(%Line{command: "AWAY", source: source, args: args}, _) do + case args do + [] -> {:event, {:away, source}} + [message] -> {:event, {:away, {source, message}}} + end + end + +end diff --git a/lib/irc/client/command/chghost.ex b/lib/irc/client/command/chghost.ex new file mode 100644 index 0000000..2b4adab --- /dev/null +++ b/lib/irc/client/command/chghost.ex @@ -0,0 +1,15 @@ +defmodule Irc.Client.Command.Chghost do + alias Irc.Parser.Line + + @type t :: {:chghost, old_mask :: Irc.Mask.t, new_mask :: Irc.Mask.t} + + def init() do + {"CHGHOST", nil, "chghost"} + end + + def handle_line(%Line{source: old_mask, args: [new_user, new_host]}, _) do + new_mask = %Irc.Mask{old_mask | user: new_user, host: new_host} + {:event, {:chghost, old_mask, new_mask}} + end + +end diff --git a/lib/irc/client/command/invite.ex b/lib/irc/client/command/invite.ex new file mode 100644 index 0000000..833c2fc --- /dev/null +++ b/lib/irc/client/command/invite.ex @@ -0,0 +1,21 @@ +defmodule Irc.Client.Command.Invite do + alias Irc.Parser.Line + + @type invite :: {:invite, channel :: String.t(), inviter :: String.t(), invited_nick :: String.t()} + @type invited :: {:invite, channel :: String.t, inviter :: Irc.Mask.t} + @type t :: invite | invited + + def init() do + {"INVITE", :invite, "invite-notify"} + end + + def handle_line(%Line{command: "INVITE", source: inviter, args: [invited, channel]}) do + {:event, {:invite, channel, inviter, invited}} + end + + def handle_line(%Line{command: "INVITE", source: inviter, args: [channel]}) do + {:event, {:invite, channel, inviter}} + end + +end + diff --git a/lib/irc/client/command/join.ex b/lib/irc/client/command/join.ex new file mode 100644 index 0000000..f4728c7 --- /dev/null +++ b/lib/irc/client/command/join.ex @@ -0,0 +1,40 @@ +defmodule Irc.Client.Command.Join do + alias Irc.Parser.Line + require Line + import Line + + @type t :: {:join, channel :: String.t()} | {:join, channel :: String.t(), Irc.User.t()} + + def init(_args) do + {"JOIN", :join, ["extended-join"]} + end + + # An user joined + def handle_line(line = %Line{command: "JOIN", source: source, args: [channel | args]}, conn) do + if Line.self?(line, conn) do + {:event, {:join, channel}} + else + user = Irc.User.from_mask(source) + user = case args do + [] -> user + [account, name] -> + account = if account != "*", do: account + {channel, %Irc.User{user | account: account, name: name}} + end + {:event, {:join, channel, user}} + end + end + + # Join a channel + def handle_command(:join, [channel], _conn_info) do + {:send, ['JOIN', channel]} + end + + # Outdated! + def handle_buffer(%Line{command: "JOIN", source: %Irc.Mask{nick: nick}, args: [channel]}, buffer = %{channel: channel}, %Irc.Connection{nick: nick}) do + {:finish, nil, {:event, {:join, channel}}} + end + + def handle_buffer(_, _), do: :postpone + +end diff --git a/lib/irc/client/command/names.ex b/lib/irc/client/command/names.ex new file mode 100644 index 0000000..b5a84a6 --- /dev/null +++ b/lib/irc/client/command/names.ex @@ -0,0 +1,22 @@ +defmodule Irc.Client.Command.Names do + alias Irc.Parser.Line + require Irc.Parser.Numeric + import Irc.Parser.Numeric + + def init() do + {[rpl_NAMREPLY(), rpl_ENDOFNAMES()], :names, ["userhost-in-names"]} + end + + def handle_line(%Line{command: rpl_NAMREPLY()}) do + :buffer + end + + def handle_line(%Line{command: rpl_ENDOFNAMES(), args: [_, target | _]}) do + :finish + end + + def handle_buffer(buffer = [%Line{args: [_, target | _]} | _]) do + {:event, {:names, target, buffer}} + end + +end diff --git a/lib/irc/client/command/who.ex b/lib/irc/client/command/who.ex new file mode 100644 index 0000000..1654ef6 --- /dev/null +++ b/lib/irc/client/command/who.ex @@ -0,0 +1,62 @@ +defmodule Irc.Client.Command.Who do + alias Irc.Parser.Line + alias Irc.User + use Irc.Parser.Numeric + + @type t :: {:who, target :: String.t, [Irc.User.t]} + + def init(_) do + {nil, "WHO", nil} + end + + def handle_command("WHO", [target], conn) do + args = if Irc.Connection.supports?(conn, "whox") do + " nuhs%cuhsnfdar" + end + {:buffer, %{target: target, acc: [], error: nil}, {:send, ['WHO ', target, args]}} + end + + @errors [err_NOSUCHSERVER(), rpl_TRYAGAIN()] + def handle_buffer(%Line{command: error, args: args}, buffer, _conn) when error in @errors do + #? + {:buffer, %{buffer | error: {error, args}}} + end + + def handle_buffer(%Line{command: rpl_ENDOFWHO()}, buffer, _conn) do + result = if buffer.error do + {:error, buffer.error} + else + {:ok, buffer.acc} + end + {:finish, nil, {:event, {:who, buffer.target, result}}} + end + + def handle_buffer(%Line{command: rpl_WHOREPLY(), args: args}, buffer, conn) do + case args do + [_, c, u, h, s, n, f, d | a_r] -> + {a,r} = case a_r do + [a, r] -> {a, r} + [r] -> {nil, r} + end + {modes, _} = Irc.Parser.Prefix.parse(f, conn) + chanmodes = %{buffer.target => modes} + user = %Irc.User{ + user: u, + host: h, + server: s, + nick: n, + account: a, + name: r, + channels: [buffer.target], + chanmodes: chanmodes + } + + _ -> + {:finish, nil} # ERROFFEL + end + {:buffer, %{buffer | acc: [args | buffer.acc]}} + end + + def handle_buffer(_, _), do: :postpone + +end |