Class: ModSpox::Handlers::Names

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

Instance Method Summary collapse

Methods inherited from Handler

#preprocess

Constructor Details

#initialize(handlers) ⇒ Names

Returns a new instance of Names.



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

def initialize(handlers)
    handlers[RPL_NAMREPLY] = self
    handlers[RPL_ENDOFNAMES] = self
    @names = Hash.new
    @raw = Hash.new
end

Instance Method Details

#check_visibility(nicks, channel) ⇒ Object

nicks

list of nicks in channel

channel

channel nicks are in

Remove visibility from any nicks that aren’t really in the channel



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/mod_spox/handlers/Names.rb', line 72

def check_visibility(nicks, channel)
    channel.nicks.each do |nick|
        unless(nicks.include?(nick))
            channel.remove_nick(nick)
            unless(nick.botnick)
                nick.refresh
                nick.update(:visible => false) if (Models::Nick.filter(:botnick => true).first.channels & nick.channels).empty?
            end
        end
    end
end

#process(string) ⇒ Object



12
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
# File 'lib/mod_spox/handlers/Names.rb', line 12

def process(string)
    orig = string.dup
    string = string.dup
    begin
        string.slice!(0..string.index(' '))
        type = string.slice!(0..string.index(' ')-1)
        if(type == RPL_NAMREPLY)
            3.times{ string.slice!(0..string.index(' ')) }
            chan = string.slice!(0..string.index(' ')-1)
            string.slice!(0..string.index(':'))
            @names[chan] = Array.new unless @names[chan]
            @raw[chan] = Array.new unless @raw[chan]
            @raw[chan] << orig
            @names[chan] += string.split
            return nil
        else
            2.times{ string.slice!(0..string.index(' ')) }
            chan = string.slice!(0..string.index(' ')-1)
            channel = find_model(chan)
            @raw[chan] << orig if @raw[chan]
            nicks = Array.new
            ops = Array.new
            voice = Array.new
            raw = @raw[chan].dup if @raw[chan]
            @names[chan] = [] unless @names[chan].is_a?(Array)
            @names[chan].each do |n|
                nick = Models::Nick.find_or_create(:nick => n.gsub(/^[@+]/, ''))
                nick.visible = true
                nicks << nick
                if(n[0] == '@')
                    ops << nick
                    m = Models::NickMode.find_or_create(:nick_id => nick.pk, :channel_id => channel.pk)
                    m.set_mode('o')
                elsif(n[0] == '+')
                    voice << nick
                    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
                nick.add_channel(channel)
                nick.refresh
                channel.refresh
            end
            check_visibility(nicks, channel)
            @names.delete(chan)
            @raw.delete(chan)
            return Messages::Incoming::Names.new(raw, channel, nicks, ops, voice)
        end
    rescue Object => boom
        Logger.error("Failed to parse NAMES message: #{orig}")
        raise boom
    end
end