Class: ModSpox::Handlers::Who

Inherits:
Handler
  • Object
show all
Defined in:
lib/mod_spox/handlers/Who.rb

Instance Method Summary collapse

Methods inherited from Handler

#preprocess

Constructor Details

#initialize(handlers) ⇒ Who

Returns a new instance of Who.



6
7
8
9
10
11
# File 'lib/mod_spox/handlers/Who.rb', line 6

def initialize(handlers)
    handlers[RPL_WHOREPLY] = self
    handlers[RPL_ENDOFWHO] = self
    @cache = Hash.new
    @raw_cache = Hash.new
end

Instance Method Details

#process(string) ⇒ Object

:host 352 spox #mod_spox ~pizza_ 12.229.112.195 punch.va.us.dal.net pizza_ H@ :5 pizza_



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
# File 'lib/mod_spox/handlers/Who.rb', line 13

def process(string)
    string = string.dup
    orig = string.dup
    begin
        until(string.slice(0..RPL_WHOREPLY.size-1) == RPL_WHOREPLY || string.slice(0..RPL_ENDOFWHO.size-1) == RPL_ENDOFWHO)
            string.slice!(0..string.index(' '))
        end
        if(string.slice(0..RPL_WHOREPLY.size-1) == RPL_WHOREPLY)
            2.times{string.slice!(0..string.index(' '))}
            location = string.slice!(0..string.index(' ')-1)
            string.slice!(0)
            username = string.slice!(0..string.index(' ')-1)
            string.slice!(0)
            host = string.slice!(0..string.index(' ')-1)
            string.slice!(0)
            server = string.slice!(0..string.index(' ')-1)
            string.slice!(0)
            nick = find_model(string.slice!(0..string.index(' ')-1))
            string.slice!(0)
            info = string.slice!(0..string.index(' ')-1)
            string.slice!(0..string.index(':'))
            hops = string.slice!(0..string.index(' ')-1)
            string.slice!(0)
            realname = string
            location = nil if location == '*'
            nick.username = username
            nick.address = location
            nick.real_name = realname
            nick.connected_to = server
            nick.away = !info.index('G').nil?
            nick.add_channel(find_model(location)) unless location.nil?
            nick.save_changes
            key = location.nil? ? nick.nick : location
            @cache[key] = Array.new unless @cache[key]
            @cache[key] << nick
            @raw_cache[key] = Array.new unless @raw_cache[key]
            @raw_cache[key] << orig
            unless(location.nil?)
                channel = find_model(location)
                channel.add_nick(nick)
                if(info.include?('+'))
                    m = Models::NickMode.find_or_create(:channel_id => channel.pk, :nick_id => nick.pk)
                    m.set_mode('v')
                elsif(info.include?('@'))
                    m = Models::NickMode.find_or_create(:channel_id => channel.pk, :nick_id => nick.pk)
                    m.set_mode('o')
                else
                    m = Models::NickMode.find_or_create(:channel_id => channel.pk, :nick_id => nick.pk)
                    m.clear_modes
                end
            end
            return nil
        else
            2.times{string.slice!(0..string.index(' '))}
            location = string.slice!(0..string.index(' ')-1)
            loc = find_model(location)
            @raw_cache[location] << orig
            message = Messages::Incoming::Who.new(@raw_cache[location].join("\n"), loc, @cache[location])
            @raw_cache.delete(location)
            @cache.delete(location)
            return message
        end
    rescue Object => boom
        Logger.error("Failed to match WHO type message: #{orig}")
        raise boom
    end
end