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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
defmodule Example.Bot do
use GenServer
require Logger
defmodule Config do
defstruct server: nil,
port: nil,
pass: nil,
nick: nil,
user: nil,
name: nil,
channel: nil,
client: nil
def from_params(params) when is_map(params) do
Enum.reduce(params, %Config{}, fn {k, v}, acc ->
case Map.has_key?(acc, k) do
true -> Map.put(acc, k, v)
false -> acc
end
end)
end
end
alias ExIrc.Client
alias ExIrc.SenderInfo
def start_link(%{:nick => nick} = params) when is_map(params) do
config = Config.from_params(params)
GenServer.start_link(__MODULE__, [config], name: String.to_atom(nick))
end
def init([config]) do
# Start the client and handler processes, the ExIrc supervisor is automatically started when your app runs
{:ok, client} = ExIrc.start_link!()
# Register the event handler with ExIrc
Client.add_handler client, self()
# Connect and logon to a server, join a channel and send a simple message
Logger.debug "Connecting to #{config.server}:#{config.port}"
Client.connect! client, config.server, config.port
{:ok, %Config{config | :client => client}}
end
def handle_info({:connected, server, port}, config) do
Logger.debug "Connected to #{server}:#{port}"
Logger.debug "Logging to #{server}:#{port} as #{config.nick}.."
Client.logon config.client, config.pass, config.nick, config.user, config.name
{:noreply, config}
end
def handle_info(:logged_in, config) do
Logger.debug "Logged in to #{config.server}:#{config.port}"
Logger.debug "Joining #{config.channel}.."
Client.join config.client, config.channel
{:noreply, config}
end
def handle_info(:disconnected, config) do
Logger.debug "Disconnected from #{config.server}:#{config.port}"
{:stop, :normal, config}
end
def handle_info({:joined, channel}, config) do
Logger.debug "Joined #{channel}"
Client.msg config.client, :privmsg, config.channel, "Hello world!"
{:noreply, config}
end
def handle_info({:names_list, channel, names_list}, config) do
names = String.split(names_list, " ", trim: true)
|> Enum.map(fn name -> " #{name}\n" end)
Logger.info "Users logged in to #{channel}:\n#{names}"
{:noreply, config}
end
def handle_info({:received, msg, %SenderInfo{:nick => nick}, channel}, config) do
Logger.info "#{nick} from #{channel}: #{msg}"
{:noreply, config}
end
def handle_info({:mentioned, msg, %SenderInfo{:nick => nick}, channel}, config) do
Logger.warn "#{nick} mentioned you in #{channel}"
case String.contains?(msg, "hi") do
true ->
reply = "Hi #{nick}!"
Client.msg config.client, :privmsg, config.channel, reply
Logger.info "Sent #{reply} to #{config.channel}"
false ->
:ok
end
{:noreply, config}
end
def handle_info({:received, msg, %SenderInfo{:nick => nick}}, config) do
Logger.warn "#{nick}: #{msg}"
reply = "Hi!"
Client.msg config.client, :privmsg, nick, reply
Logger.info "Sent #{reply} to #{nick}"
{:noreply, config}
end
# Catch-all for messages you don't care about
def handle_info(_msg, config) do
{:noreply, config}
end
def terminate(_, state) do
# Quit the channel and close the underlying client connection when the process is terminating
Client.quit state.client, "Goodbye, cruel world."
Client.stop! state.client
:ok
end
end
|