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
|
defmodule Irc.Shout do
alias Irc.{Connection, Parser.Line}
def shout(nick, host, target, message, opts \\ []) do
opts = opts
|> Keyword.put(:reconnect, false)
|> Keyword.put_new(:await_up_timeout, :timer.seconds(30))
|> Keyword.put_new(:quit, "bye")
do_shout(Connection.start(nick, host, opts), target, message, opts)
end
def do_shout({:ok, conn}, target, message, opts) do
mon = Process.monitor(conn)
result = with \
{:ok, _info} <- Connection.await_up(conn, Keyword.get(opts, :await_up_timeout)),
:ok <- join(conn, target),
:ok <- Connection.sendline(conn, ['PRIVMSG ', target, ' :', message])
do
:ok
else
error ->
error
end
Process.demonitor(mon, [:flush])
Connection.disconnect(conn, Keyword.get(opts, :quit), true)
Connection.flush(conn)
result
end
def do_shout(error, _, _, _) do
error
end
def join(conn, target = "#"<>_) do
case Connection.sendline(conn, ['JOIN ', target]) do
:ok -> await_join(conn, target)
error -> error
end
end
def join(conn, _) do
:ok
end
def await_join(conn, target) do
receive do
{:irc_conn_line, conn, %Irc.Parser.Line{command: "JOIN", args: [target]}} -> :ok
{:irc_conn_error, conn, reason} -> {:error, reason}
{:DOWN, _, _, conn, reason} -> {:error, {:conn_down, reason}}
after
10_000 -> :join_timeout
end
end
end
|