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.



270
271
272
273
274
# File 'library/blur/handling.rb', line 270

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 :topic_change, 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.



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'library/blur/handling.rb', line 155

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.



212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'library/blur/handling.rb', line 212

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.



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'library/blur/handling.rb', line 249

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



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

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)
        emit :user_rename, channel, user, user.nick, command[0]
        user.nick = 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.



175
176
177
178
179
180
181
182
183
184
185
# File 'library/blur/handling.rb', line 175

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
# File 'library/blur/handling.rb', line 86

def got_ping network, command
  network.transmit :PONG, 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.



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'library/blur/handling.rb', line 116

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

      begin
        if message[0..3] == "+OK " and channel.encrypted?
          message = channel.encryption.decrypt message[4..-1]
        end
      rescue Encryption::BadInputError
        puts "-!- FiSH: #{$!.message}"
      rescue => exception
        puts "-!- There was a problem with the FiSH encryption, disabling"

        channel.encryption = nil
      end
      
      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.



191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'library/blur/handling.rb', line 191

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.



230
231
232
233
234
235
236
237
238
239
240
# File 'library/blur/handling.rb', line 230

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