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_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
84
85
86
87
88
89
90
91
92
93
94
# File 'library/blur/handling.rb', line 75

def got_channel_topic network, command
  me, name, topic = command.params
  
  if channel = network.channel_by_name(name)
    emit :topic_change, channel, topic
    channel.topic = topic
  else          
    channel = Network::Channel.new name, network, []

    if network.fish? and network.options[:fish].key? name
      keyphrase = network.options[:fish][name]
      channel.encryption = Encryption::FiSH.new keyphrase
    end
    
    emit :topic_change, channel, topic
    channel.topic = topic
    
    network.channels << channel
  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.



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'library/blur/handling.rb', line 167

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.



224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'library/blur/handling.rb', line 224

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.



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'library/blur/handling.rb', line 261

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)
        emit :user_mode, user, modes
        user.merge_modes modes
      end
    else
      emit :channel_mode, channel, modes
      channel.merge_modes 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 &Network::User.method(:new)
  
  if channel = network.channel_by_name(name)
    users.each do |user|
      user.channel = channel
      user.network = network

      channel.users << user
    end
  else
    channel = Network::Channel.new name, network, users

    if network.fish? and network.options[:fish].key? name
      keyphrase = network.options[:fish][name]
      channel.encryption = Encryption::FiSH.new keyphrase
    end

    network.channels << channel
  end
end

#got_nick(network, command) ⇒ Object

Called when a user changed nickname.

Callbacks:

Emits :user_rename with the parameters channel, user and nickname where nickname is the new name.



106
107
108
109
110
111
112
113
114
115
116
117
# File 'library/blur/handling.rb', line 106

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



187
188
189
190
191
192
193
194
195
196
197
# File 'library/blur/handling.rb', line 187

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.



97
98
99
# File 'library/blur/handling.rb', line 97

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.



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'library/blur/handling.rb', line 128

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.



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

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.



242
243
244
245
246
247
248
249
250
251
252
# File 'library/blur/handling.rb', line 242

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