summaryrefslogtreecommitdiff
path: root/lib/exirc/example_handler.ex
blob: 17017294ad04a976a2586d4deb39d0aad7b7f5fa (plain) (blame)
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
defmodule ExampleHandler do
  @moduledoc """
  This is an example event handler that you can attach to the client using
  `add_handler` or `add_handler_async`. To remove, call `remove_handler` or
  `remove_handler_async` with the pid of the handler process.
  """
  def start! do
    start_link([])
  end

  def start_link(_) do
    GenServer.start_link(__MODULE__, nil, [])
  end

  def init(_) do
    {:ok, nil}
  end

  @doc """
  Handle messages from the client

  Examples:

    def handle_info({:connected, server, port}, _state) do
      IO.puts "Connected to \#{server}:\#{port}"
    end
    def handle_info(:logged_in, _state) do
      IO.puts "Logged in!"
    end
    def handle_info(%ExIRC.Message{nick: from, cmd: "PRIVMSG", args: ["mynick", msg]}, _state) do
      IO.puts "Received a private message from \#{from}: \#{msg}"
    end
    def handle_info(%ExIRC.Message{nick: from, cmd: "PRIVMSG", args: [to, msg]}, _state) do
      IO.puts "Received a message in \#{to} from \#{from}: \#{msg}"
    end
  """
  def handle_info({:connected, server, port}, _state) do
    debug "Connected to #{server}:#{port}"
    {:noreply, nil}
  end
  def handle_info(:logged_in, _state) do
    debug "Logged in to server"
    {:noreply, nil}
  end
  def handle_info(:disconnected, _state) do
    debug "Disconnected from server"
    {:noreply, nil}
  end
  def handle_info({:joined, channel}, _state) do
    debug "Joined #{channel}"
    {:noreply, nil}
  end
  def handle_info({:joined, channel, user}, _state) do
    debug "#{user} joined #{channel}"
    {:noreply, nil}
  end
  def handle_info({:topic_changed, channel, topic}, _state) do
    debug "#{channel} topic changed to #{topic}"
    {:noreply, nil}
  end
  def handle_info({:nick_changed, nick}, _state) do
    debug "We changed our nick to #{nick}"
    {:noreply, nil}
  end
  def handle_info({:nick_changed, old_nick, new_nick}, _state) do
    debug "#{old_nick} changed their nick to #{new_nick}"
    {:noreply, nil}
  end
  def handle_info({:parted, channel}, _state) do
    debug "We left #{channel}"
    {:noreply, nil}
  end
  def handle_info({:parted, channel, sender}, _state) do
    nick = sender.nick
    debug "#{nick} left #{channel}"
    {:noreply, nil}
  end
  def handle_info({:invited, sender, channel}, _state) do
    by = sender.nick
    debug "#{by} invited us to #{channel}"
    {:noreply, nil}
  end
  def handle_info({:kicked, sender, channel}, _state) do
    by = sender.nick
    debug "We were kicked from #{channel} by #{by}"
    {:noreply, nil}
  end
  def handle_info({:kicked, nick, sender, channel}, _state) do
    by = sender.nick
    debug "#{nick} was kicked from #{channel} by #{by}"
    {:noreply, nil}
  end
  def handle_info({:received, message, sender}, _state) do
    from = sender.nick
    debug "#{from} sent us a private message: #{message}"
    {:noreply, nil}
  end
  def handle_info({:received, message, sender, channel}, _state) do
    from = sender.nick
    debug "#{from} sent a message to #{channel}: #{message}"
    {:noreply, nil}
  end
  def handle_info({:mentioned, message, sender, channel}, _state) do
    from = sender.nick
    debug "#{from} mentioned us in #{channel}: #{message}"
    {:noreply, nil}
  end
  def handle_info({:me, message, sender, channel}, _state) do
    from = sender.nick
    debug "* #{from} #{message} in #{channel}"
    {:noreply, nil}
  end
  # This is an example of how you can manually catch commands if ExIRC.Client doesn't send a specific message for it
  def handle_info(%ExIRC.Message{nick: from, cmd: "PRIVMSG", args: ["testnick", msg]}, _state) do
    debug "Received a private message from #{from}: #{msg}"
    {:noreply, nil}
  end
  def handle_info(%ExIRC.Message{nick: from, cmd: "PRIVMSG", args: [to, msg]}, _state) do
    debug "Received a message in #{to} from #{from}: #{msg}"
    {:noreply, nil}
  end
  # Catch-all for messages you don't care about
  def handle_info(msg, _state) do
    debug "Received ExIRC.Message:"
    IO.inspect msg
    {:noreply, nil}
  end

  defp debug(msg) do
    IO.puts IO.ANSI.yellow() <> msg <> IO.ANSI.reset()
  end
end