Class: ModSpox::Handlers::Whois

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

Instance Method Summary collapse

Methods inherited from Handler

#preprocess

Constructor Details

#initialize(handlers) ⇒ Whois

Returns a new instance of Whois.



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/mod_spox/handlers/Whois.rb', line 5

def initialize(handlers)
    handlers[RPL_WHOISUSER] = self
    handlers[RPL_WHOISSERVER] = self
    handlers[RPL_WHOISOPERATOR] = self
    handlers[RPL_WHOISIDLE] = self
    handlers[RPL_WHOISCHANNELS] = self
    handlers[RPL_WHOISIDENTIFIED] = self
    handlers[RPL_ENDOFWHOIS] = self
    @cache = Hash.new
    @raw = Hash.new
end

Instance Method Details

#process(string) ⇒ Object



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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/mod_spox/handlers/Whois.rb', line 17

def process(string)
    orig = string.dup
    string = string.dup
    begin
        until(string.slice(0..string.index(' ')-1) == RPL_WHOISUSER ||
                string.slice(0..string.index(' ')-1) == RPL_WHOISSERVER ||
                string.slice(0..string.index(' ')-1) == RPL_WHOISOPERATOR ||
                string.slice(0..string.index(' ')-1) == RPL_WHOISIDLE ||
                string.slice(0..string.index(' ')-1) == RPL_WHOISCHANNELS ||
                string.slice(0..string.index(' ')-1) == RPL_WHOISIDENTIFIED ||
                string.slice(0..string.index(' ')-1) == RPL_ENDOFWHOIS)
            string.slice!(0..string.index(' '))
        end
        case string.slice!(0..string.index(' ')-1)
            when RPL_WHOISUSER
                string.slice!(0)
                string.slice!(0..string.index(' '))
                nick = find_model(string.slice!(0..string.index(' ')-1))
                string.slice!(0)
                nick.username = string.slice!(0..string.index(' ')-1)
                string.slice!(0)
                nick.address = string.slice!(0..string.index(' ')-1)
                string.slice!(0..string.index(':'))
                nick.real_name = string
                nick.save_changes
                @cache[nick.nick] = Messages::Incoming::Whois.new(nick)
                @cache[nick.nick].raw_push(orig)
            when RPL_WHOISCHANNELS
                2.times{string.slice!(0..string.index(' '))}
                nick = find_model(string.slice!(0..string.index(' ')-1))
                @cache[nick.nick] = Messages::Incoming::Whois.new(nick) unless @cache[nick.nick]
                string.slice!(0..string.index(':'))
                string.split.each do |c|
                    channel = find_model(['@','+'].include?(c.slice(0)) ? c.slice(1..c.size) : c)
                    channel.add_nick(nick)
                    @cache[nick.nick].channels_push(channel)
                    if(c[0].chr == '@')
                        m = Models::NickMode.find_or_create(:nick_id => nick.pk, :channel_id => channel.pk)
                        m.set_mode('o')
                    elsif(c[0].chr == '+')
                        m = Models::NickMode.find_or_create(:nick_id => nick.pk, :channel_id => channel.pk)
                        m.set_mode('v')
                    else
                        m = Models::NickMode.find_or_create(:nick_id => nick.pk, :channel_id => channel.pk)
                        m.clear_modes
                    end
                end
                @cache[nick.nick].raw_push(orig)
            when RPL_WHOISSERVER
                2.times{string.slice!(0..string.index(' '))}
                nick = find_model(string.slice!(0..string.index(' ')-1))
                string.slice!(0)
                @cache[nick.nick] = Messages::Incoming::Whois.new(nick) unless @cache[nick.nick]
                nick.connected_to = string.slice!(0..string.index(' ')-1)
                nick.save_changes
                @cache[nick.nick].raw_push(orig)
            when RPL_WHOISIDENTIFIED
                2.times{string.slice!(0..string.index(' '))}
                nick = find_model(string.slice!(0..string.index(' ')-1))
                @cache[nick.nick] = Messages::Incoming::Whois.new(nick) unless @cache[nick.nick]
                nick.auth.services_identified = true
                @cache[nick.nick].raw_push(orig)
            when RPL_WHOISIDLE
                2.times{string.slice!(0..string.index(' '))}
                nick = find_model(string.slice!(0..string.index(' ')-1))
                string.slice!(0)
                @cache[nick.nick] = Messages::Incoming::Whois.new(nick) unless @cache[nick.nick]
                nick.seconds_idle = string.slice!(0..string.index(' ')-1).to_i
                string.slice!(0)
                nick.connected_at = Time.at(string.slice!(0..string.index(' ')-1).to_i)
                nick.save_changes
                @cache[nick.nick].raw_push(orig)
            when RPL_WHOISOPERATOR
                2.times{string.slice!(0..string.index(' '))}
                nick = find_model(string.slice!(0..string.index(' ')-1))
                string.slice!(0)
                @cache[nick.nick] = Messages::Incoming::Whois.new(nick) unless @cache[nick.nick]
                @cache[nick.nick].raw_push(orig)
            when RPL_ENDOFWHOIS
                2.times{string.slice!(0..string.index(' '))}
                nick = find_model(string.slice!(0..string.index(' ')-1))
                @cache[nick.nick] = Messages::Incoming::Whois.new(nick) unless @cache[nick.nick]
                @cache[nick.nick].raw_push(orig)
                message = @cache[nick.nick]
                message.lock
                @cache.delete(nick.nick)
                return message
            else
                Logger.error("Failed to parse WHOIS type reply. Unknown part found: #{orig}")
        end
        return nil
    rescue Object => boom
        Logger.error("Failed to parse WHOIS type reply: #{orig}")
        raise boom
    end
end