Class: IRC::Client::Modules::Base

Inherits:
IRC::Client::Module show all
Defined in:
lib/failirc/client/modules/Base.rb

Defined Under Namespace

Modules: Utils

Constant Summary collapse

@@version =
'0.0.1'
@@supported =
{}

Instance Attribute Summary

Attributes inherited from IRC::Client::Module

#client

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from IRC::Client::Module

#finalize

Constructor Details

#initialize(client) ⇒ Base

Returns a new instance of Base.



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
# File 'lib/failirc/client/modules/Base.rb', line 54

def initialize (client)
    @aliases = {
        :input => {
            :NUMERIC => /^:([^ ]+)\s+(\d{3})\s+(.+)/,

            :PING => /^PING( |$)/i,

            :JOIN => /^:.+?\s+JOIN\s+:./i,

            :PRIVMSG => /^:.+?\s+PRIVMSG\s+.+?\s+:/i,
        },
    }

    @events = {
        :custom => {
            :connect => self.method(:connect),
            :numeric => self.method(:received_numeric),

            :join => self.method(:join),
            :who  => self.method(:who),

            :message => self.method(:send_message),

            :quit => self.method(:do_quit),
        },

        :input => {
            :NUMERIC => self.method(:numeric),

            :PING => self.method(:pong),

            :JOIN => self.method(:joined),

            :PRIVMSG => self.method(:message),
        },
    }

    super(client)
end

Class Method Details

.supportedObject



46
47
48
# File 'lib/failirc/client/modules/Base.rb', line 46

def self.supported
    @@supported
end

.versionObject



42
43
44
# File 'lib/failirc/client/modules/Base.rb', line 42

def self.version
    @@version
end

Instance Method Details

#connect(server) ⇒ Object



135
136
137
138
139
140
141
142
# File 'lib/failirc/client/modules/Base.rb', line 135

def connect (server)
    if server.password
        server.send :raw, "PASS #{server.password}"
    end

    server.send :raw, "NICK #{client.nick}"
    server.send :raw, "USER #{client.user} * * :#{client.realName}"
end

#descriptionObject



50
51
52
# File 'lib/failirc/client/modules/Base.rb', line 50

def description
    "Base-#{Base.version}"
end

#do_quit(server, message) ⇒ Object



348
349
350
351
352
353
354
# File 'lib/failirc/client/modules/Base.rb', line 348

def do_quit (server, message)
    if message
        server.send :raw, "QUIT #{message}"
    else
        server.send :raw, "QUIT"
    end
end

#join(server, channel, password = nil) ⇒ Object



207
208
209
210
211
212
213
214
215
216
# File 'lib/failirc/client/modules/Base.rb', line 207

def join (server, channel, password=nil)
    text = "JOIN #{channel}"

    if password
        text << " #{password}"
    end

    server.send :raw, text
    client.execute :who, server, channel
end

#joined(server, string) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/failirc/client/modules/Base.rb', line 218

def joined (server, string)
    match = string.match(/^:(.+?)\s+JOIN\s+:(.+)$/i)

    if !match
        return
    end

    mask    = Mask.parse(match[1])
    channel = match[2]

    if mask.nick == server.nick
        if !server.channels[channel]
            server.channels[channel] = Channel.new(server, channel)
        end
    else
        if !server.clients[mask.nick] || server.clients[mask.nick].mask != mask
            server.clients[mask.nick] = Client.new(server, mask)
        end

        if server.channels[channel]
            server.channels[channel].add(server.clients[mask.nick])
        end
    end
end

#message(server, string) ⇒ Object



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/failirc/client/modules/Base.rb', line 247

def message (server, string)
    match = string.match(/:(.+?)\s+PRIVMSG\s+(.+)\s+:(.*)$/i)

    if !match
        return
    end

    from = Mask.parse(match[1])

    if !server.clients[from.nick] || server.clients[from.nick] != from
        from = server.clients[from.nick] = Client.new(server, from)
    else
        from = server.clients[from.nick]
    end

    to = match[2]

    if Utils::Channel::isValid(to)
        to   = server.channels[to]
        from = to.user from
    else
        to = client
    end

    message = match[3]

    if match = message.match(/^\x01([^ ]*)( (.*?))?\x01$/)
        client.execute :ctcp, server, :message, from, to, match[1], match[3]
    else
        client.execute :message, server, :input, from, to, message
    end
end

#notice(server, string) ⇒ Object



296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/failirc/client/modules/Base.rb', line 296

def notice (server, string)
    match = string.match(/:(.+?)\s+NOTICE\s+(.+)\s+:(.*)$/i)

    if !match
        return
    end

    from = Mask.parse(match[1])

    if !server.clients[from.nick] || server.clients[from.nick] != from
        from = server.clients[from.nick] = Client.new(server, from)
    else
        from = server.clients[from.nick]
    end

    to = match[2]

    if Utils::Channel::isValid(to)
        to   = server.channels[to]
        from = to.user from
    else
        to = client
    end

    message = match[3]

    if match = message.match(/^\x01([^ ]*)( (.*?))?\x01$/)
        client.execute :ctcp, server, :notice, from, to, match[1], match[3]
    else
        client.execute :message, server, :input, from, to, message
    end
end

#numeric(server, string) ⇒ Object



152
153
154
155
156
157
158
# File 'lib/failirc/client/modules/Base.rb', line 152

def numeric (server, string)
    match = string.match(/^:[^ ]+\s+(\d{3})\s+.+?(\s+(.*))?$/)

    if match
        client.dispatcher.execute :numeric, server, match[1].to_i, match[3]
    end
end

#pong(server, string) ⇒ Object



144
145
146
147
148
149
150
# File 'lib/failirc/client/modules/Base.rb', line 144

def pong (server, string)
    match = string.match(/PING\s+(.*)$/)

    if match
        server.send :raw, "PONG #{match[1]}"
    end
end

#received_numeric(server, number, message) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/failirc/client/modules/Base.rb', line 160

def received_numeric (server, number, message)
    case number

    # stuff supported by the server
    when 5
        message.split(/\s+/).each {|support|
            support = support.split(/=/)

            Base.supported[support[0]] = support[1]
        }

    # end of MOTD or no MOTD, hence concluded connection.
    when 376, 422
        client.dispatcher.execute :connected, server

    # WHO reply
    when 352
        match = message.match(/(.+?)\s+(.+?)\s+(.+?)\s+.+?\s+(.+?)\s+.+?\s+:\d+\s+(.+)$/)

        if !match
            return
        end

        channel  = match[1]
        mask     = Mask.new(match[4], match[2], match[3])
        realName = match[5]

        if !server.channels[channel]
            server.channels[channel] = Channel.new(server, channel)
        end

        channel = server.channels[channel]

        if mask.nick == server.nick
            return
        end

        if !server.clients[mask.nick] || server.clients[mask.nick].mask != mask
            server.clients[mask.nick]          = Client.new(server, mask)
            server.clients[mask.nick].realName = realName
        end

        channel.add(server.clients[mask.nick])

    end
end

#send_ctcp(server, kind, chain, from, to, type, message, level = nil) ⇒ Object



344
345
346
# File 'lib/failirc/client/modules/Base.rb', line 344

def send_ctcp (server, kind, chain, from, to, type, message, level=nil)

end

#send_message(server, chain, from, to, message) ⇒ Object



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/failirc/client/modules/Base.rb', line 280

def send_message (server, chain, from, to, message)
    if chain != :output
        return
    end

    if to.is_a?(Channel)
        name = to.name
    elsif to.is_a?(Client)
        name = to.nick
    else
        name = to.to_s
    end

    server.send :raw, "PRIVMSG #{name} :#{message}"
end

#send_notice(server, chain, from, to, message, level = nil) ⇒ Object



330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/failirc/client/modules/Base.rb', line 330

def send_notice (server, chain, from, to, message, level=nil)
    if chain != :output
        return
    end

    if to.is_a?(Channel)
        name = "#{level}#{to.name}"
    elsif to.is_a?(Client)
        name = to.nick
    end

    server.send :raw, "NOTICE #{name} :#{message}"
end

#who(server, channel) ⇒ Object



243
244
245
# File 'lib/failirc/client/modules/Base.rb', line 243

def who (server, channel)
    server.send :raw, "WHO #{channel}"
end