Module: Blur::Client::Handling

Included in:
Blur::Client
Defined in:
library/blur/handling.rb

Overview

The Handling module is the very core of the IRC-part in Blur.

When the client receives a parsed message instance, it immediately starts looking for a got_(the message name) method inside the client, which is implemented in this module.

Implementing a handler

Implementing a handler is very, very easy.

All you need to do is define a method named got_(message you want to implement) that accepts 2 parameters, network and message.

You can then do whatever you need to do with the message instance, you can access the parameters of it through Network::message#[].

Don’t forget that this module is inside the clients scope, so you can access all instance-variables and methods.

Examples:

# RPL_WHOISUSER
# <nick> <user> <host> * :<real name>
def got_whois_user network, message
  puts "nick: #{message.parameters[0]} user: #{message.parameters[1]} host: #{message.parameters[2]} …"
end

See Also:

Instance Method Summary collapse

Instance Method Details

#got_005(network, message) ⇒ Object

Called when the network announces its ISUPPORT parameters.



244
245
246
247
248
# File 'library/blur/handling.rb', line 244

def got_005 network, message
  params = message.parameters[1..-2]

  network.isupport.parse *params
end

#got_channel_topic(network, message) ⇒ Object Also known as: got_332

Called when a channel topic was changed.

Callbacks:

Emits :topic_change with the parameters channel and topic.



74
75
76
77
78
79
80
81
82
# File 'library/blur/handling.rb', line 74

def got_channel_topic network, message
  _, channel_name, topic = message.parameters
  
  if channel = find_or_create_channel(channel_name, network)
    emit :channel_topic, channel, topic

    channel.topic = topic
  end
end

#got_end_of_motd(network, message) ⇒ Object Also known as: got_422, got_376

Called when the MOTD was received, which also means it is ready.

Callbacks:

Emits :connection_ready with the parameter network.

Automatically joins the channels specified in :channels.



39
40
41
42
43
44
45
# File 'library/blur/handling.rb', line 39

def got_end_of_motd network, message
  emit :connection_ready, network
  
  network.options['channels'].each do |channel|
    network.join channel
  end
end

#got_join(network, message) ⇒ Object

Called when a user joined a channel.

Callbacks:

Emits :user_entered with the parameters channel and user.



143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'library/blur/handling.rb', line 143

def got_join network, message
  channel_name = message.parameters[0]

  user = find_or_create_user message.prefix.nick, network
  user.name = message.prefix.user
  user.host = message.prefix.host
  
  if channel = find_or_create_channel(channel_name, network)
    _user_join_channel user, channel

    emit :user_entered, channel, user
  end
end

#got_kick(network, message) ⇒ Object

Called when a user was kicked from a channel.

Callbacks:

Emits :user_kicked with the parameters kicker, channel, kickee and reason.

kicker is the user that kicked kickee.



198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'library/blur/handling.rb', line 198

def got_kick network, message
  name, target, reason = message.parameters
  
  if channel = network.channels[name]
    if kicker = network.users[message.prefix.nick]
      if kickee = network.users[target]
        _user_part_channel kickee, channel
        
        emit :user_kicked, kicker, channel, kickee, reason
      end
    end
  end
end

#got_mode(network, message) ⇒ Object

Called when a channel or a users flags was altered.

Callbacks:

When it’s channel modes:

Emits :channel_mode with the parameters channel and modes.

When it’s user modes:

Emits :user_mode with the parameters user and modes.



235
236
237
238
239
240
241
# File 'library/blur/handling.rb', line 235

def got_mode network, message
  name, modes, limit, nick, mask = message.parameters

  if channel = network.channels[name]
    # FIXME
  end
end

#got_name_reply(network, message) ⇒ Object Also known as: got_353

Called when the namelist of a channel was received.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'library/blur/handling.rb', line 48

def got_name_reply network, message
  name  = message.parameters[2] # Channel name.
  nicks = message.parameters[3].split.map do |nick|
    # Slice the nick if the first character is a user mode prefix.
    if network.user_prefixes.include? nick.chr
      nick.slice! 0
    end

    nick
  end
  
  if channel = find_or_create_channel(name, network)
    users = nicks.map{|nick| find_or_create_user nick, network }
    users.each do |user|
      user.channels << channel
      channel.users << user unless channel.users.include? user
    end

    emit :channel_who_reply, channel
  end
end

#got_nick(network, message) ⇒ Object

Called when a user changed nickname.

Callbacks:

Emits :user_rename with the parameters channel, user and new_nick



95
96
97
98
99
100
101
102
103
104
# File 'library/blur/handling.rb', line 95

def got_nick network, message
  old_nick = message.prefix.nick
  
  if user = network.users.delete(old_nick)
    new_nick = message.parameters[0]
    emit :user_rename, user, new_nick
    user.nick = new_nick
    network.users[new_nick] = user
  end
end

#got_part(network, message) ⇒ Object

Called when a user left a channel.

Callbacks:

Emits :user_left with the parameters channel and user.



161
162
163
164
165
166
167
168
169
170
171
# File 'library/blur/handling.rb', line 161

def got_part network, message
  channel_name = message.parameters[0]
  
  if channel = network.channels[channel_name]
    if user = network.users[message.prefix.nick]
      _user_part_channel user, channel
      
      emit :user_left, channel, user
    end
  end
end

#got_ping(network, message) ⇒ Object

Called when the server needs to verify that we’re alive.



85
86
87
88
89
# File 'library/blur/handling.rb', line 85

def got_ping network, message
  network.transmit :PONG, message.parameters[0]

  emit :network_ping, message.parameters[0]
end

#got_privmsg(network, message) ⇒ Object

Note:

Messages are contained as strings.

Called when a message was received (both channel and private messages).

Callbacks:

When it’s a channel message:

Emits :message with the parameters user, channel and message.

When it’s a private message:

Emits :private_message with the parameters user and message.



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'library/blur/handling.rb', line 115

def got_privmsg network, message
  return unless message.prefix.nick # Ignore all server privmsgs
  name, msg = message.parameters

  if channel = network.channels[name]
    unless user = network.users[message.prefix.nick]
      user = User.new message.prefix.nick, network
    end

    user.name = message.prefix.user
    user.host = message.prefix.host

    emit :message, user, channel, msg
  else # This is a private message
    unless user = network.users[message.prefix.nick]
      user = User.new message.prefix.nick, network
      user.name = message.prefix.user
      user.host = message.prefix.host
    end

    emit :private_message, user, msg
  end
end

#got_quit(network, message) ⇒ Object

Called when a user disconnected from a network.

Callbacks:

Emits :user_quit with the parameters channel and user.



177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'library/blur/handling.rb', line 177

def got_quit network, message
  nick = message.prefix.nick
  reason = message.parameters[2]
  
  if user = network.users[nick]
    user.channels.each do |channel|
      channel.users.delete user
    end

    emit :user_quit, user, reason
    network.users.delete nick
  end
end

#got_topic(network, message) ⇒ Object

Called when a topic was changed for a channel.

Callbacks:

Emits :topic with the parameters user, channel and topic.



216
217
218
219
220
221
222
223
224
225
226
# File 'library/blur/handling.rb', line 216

def got_topic network, message
  channel_name, topic = message.parameters
  
  if channel = network.channels[channel_name]
    if user = network.users[message.prefix.nick]
      emit :topic, user, channel, topic
    end
    
    channel.topic = topic
  end
end