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 command instance, it immediately starts looking for a got_(the command 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_(command you want to implement) that accepts 2 parameters, network and command.

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

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, command
  puts "nick: #{command[0]} user: #{command[1]} host: #{command[2]} …"
end

See Also:

Instance Method Summary collapse

Instance Method Details

#got_005(network, command) ⇒ Object

Called when the network announces its ISUPPORT parameters.



260
261
262
263
264
# File 'library/blur/handling.rb', line 260

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

  network.isupport.parse *params
end

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

Called when a channel topic was changed.

Callbacks:

Emits :topic_change with the parameters channel and topic.



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

def got_channel_topic network, command
  me, name, topic = command.params
  
  if channel = find_or_create_channel(name, network)
    emit :channel_topic, channel, topic

    channel.topic = topic
  end
end

#got_end_of_motd(network, command) ⇒ 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, command
  emit :connection_ready, network
  
  network.options['channels'].each do |channel|
    network.transmit :JOIN, channel
  end
end

#got_join(network, command) ⇒ Object

Called when a user joined a channel.

Callbacks:

Emits :user_entered with the parameters channel and user.



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'library/blur/handling.rb', line 145

def got_join network, command
  name = command[0]
  user = Network::User.new command.sender.nickname
  
  if channel = network.channel_by_name(name)
    user.name = command.sender.username
    user.host = command.sender.hostname
    user.channel = channel
    user.network = network
    
    channel.users << user
    
    emit :user_entered, channel, user
  end
end

#got_kick(network, command) ⇒ 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.



202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'library/blur/handling.rb', line 202

def got_kick network, command
  name, target, reason = command.params
  
  if channel = network.channel_by_name(name)
    if kicker = channel.user_by_nick(command.sender.nickname)
      if kickee = channel.user_by_nick(target)
        channel.users.delete kickee
        
        emit :user_kicked, kicker, channel, kickee, reason
      end
    end
  end
end

#got_mode(network, command) ⇒ 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.



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'library/blur/handling.rb', line 239

def got_mode network, command
  name, modes, limit, nick, mask = command.params

  if channel = network.channel_by_name(name)
    if limit
      unless limit.numeric?
        nick = limit
      end

      if user = channel.user_by_nick(nick)
        user.merge_modes modes
        emit :user_mode, user, modes
      end
    else
      channel.merge_modes modes
      emit :channel_mode, channel, modes
    end
  end
end

#got_name_reply(network, command) ⇒ 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
69
# File 'library/blur/handling.rb', line 48

def got_name_reply network, command
  name  = command[2]
  users = command[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

    Network::User.new nick
  end
  
  if channel = find_or_create_channel(name, network)
    users.each do |user|
      user.channel = channel
      user.network = network

      channel.users << user
    end

    emit :channel_who_reply, channel
  end
end

#got_nick(network, command) ⇒ Object

Called when a user changed nickname.

Callbacks:

Emits :user_rename with the parameters channel, user, +old_nick and new_nick



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

def got_nick network, command
  nick = command.sender.nickname
  
  if channels = network.channels_with_user(nick)
    channels.each do |channel|
      if user = channel.user_by_nick(nick)
        user.nick = command[0]
        emit :user_rename, channel, user, command[0]
      end
    end
  end
end

#got_part(network, command) ⇒ Object

Called when a user left a channel.

Callbacks:

Emits :user_left with the parameters channel and user.



165
166
167
168
169
170
171
172
173
174
175
# File 'library/blur/handling.rb', line 165

def got_part network, command
  name = command[0]
  
  if channel = network.channel_by_name(name)
    if user = channel.user_by_nick(command.sender.nickname)
      channel.users.delete user
      
      emit :user_left, channel, user
    end
  end
end

#got_ping(network, command) ⇒ Object

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



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

def got_ping network, command
  network.transmit :PONG, command[0]

  emit :network_ping, command[0]
end

#got_privmsg(network, command) ⇒ 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.



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

def got_privmsg network, command
  return if command.sender.is_a? String # Ignore all server privmsgs
  name, message = command.params
  
  if channel = network.channel_by_name(name)
    if user = channel.user_by_nick(command.sender.nickname)
      user.name = command.sender.username
      user.host = command.sender.hostname

      emit :message, user, channel, message
    else
      # Odd… this shouldn't happen
    end
  else # This is a private message
    user = Network::User.new command.sender.nickname
    user.name = command.sender.username
    user.host = command.sender.hostname
    user.network = network

    emit :private_message, user, message
  end
end

#got_quit(network, command) ⇒ Object

Called when a user disconnected from a network.

Callbacks:

Emits :user_quit with the parameters channel and user.



181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'library/blur/handling.rb', line 181

def got_quit network, command
  nick = command.sender.nickname
  
  if channels = network.channels_with_user(nick)
    channels.each do |channel|
      if user = channel.user_by_nick(nick)
        channel.users.delete user 
      
        emit :user_quit, channel, user
      end
    end
  end
end

#got_topic(network, command) ⇒ Object

Called when a topic was changed for a channel.

Callbacks:

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



220
221
222
223
224
225
226
227
228
229
230
# File 'library/blur/handling.rb', line 220

def got_topic network, command
  name, topic = command.params
  
  if channel = network.channel_by_name(name)          
    if user = channel.user_by_nick(command.sender.nickname)
      emit :topic, user, channel, topic
    end
    
    channel.topic = topic
  end
end