Module: Net::IRC::Client::ChannelManager

Defined in:
lib/net/irc/client/channel_manager.rb

Instance Method Summary collapse

Instance Method Details

#init_channel(channel) ⇒ Object

For managing channel



136
137
138
139
140
141
# File 'lib/net/irc/client/channel_manager.rb', line 136

def init_channel(channel)
  @channels[channel] ||= {
    :modes => [],
    :users => [],
  }
end

#on_join(m) ⇒ Object

For managing channel



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/net/irc/client/channel_manager.rb', line 86

def on_join(m)
  nick    = m.prefix.nick
  channel = m[0]

  @channels.synchronize do
    init_channel(channel)

    @channels[channel][:users] << nick
    @channels[channel][:users].uniq!
  end
end

#on_kick(m) ⇒ Object

For managing channel



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/net/irc/client/channel_manager.rb', line 66

def on_kick(m)
  users = m[1].split(",")

  @channels.synchronize do
    m[0].split(",").each do |chan|
      init_channel(chan)
      info = @channels[chan]
      if info
        users.each do |nick|
          info[:users].delete(nick)
          info[:modes].delete_if {|u|
            u[1] == nick
          }
        end
      end
    end
  end
end

#on_mode(m) ⇒ Object

For managing channel



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/net/irc/client/channel_manager.rb', line 116

def on_mode(m)
  channel = m[0]
  @channels.synchronize do
    init_channel(channel)

    modes = @server_config.mode_parser.parse(m)
    modes[:negative].each do |mode|
      @channels[channel][:modes].delete(mode)
    end

    modes[:positive].each do |mode|
      @channels[channel][:modes] << mode
    end

    @channels[channel][:modes].uniq!
    [modes[:negative], modes[:positive]]
  end
end

#on_nick(m) ⇒ Object

For managing channel



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/net/irc/client/channel_manager.rb', line 99

def on_nick(m)
  oldnick = m.prefix.nick
  newnick = m[0]

  @channels.synchronize do
    @channels.each do |channel, info|
      info[:users].map! {|i|
        (i == oldnick) ? newnick : i
      }
      info[:modes].map! {|mode, target|
        (target == oldnick) ? [mode, newnick] : [mode, target]
      }
    end
  end
end

#on_part(m) ⇒ Object

For managing channel



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/net/irc/client/channel_manager.rb', line 35

def on_part(m)
  nick    = m.prefix.nick
  channel = m[0]
  init_channel(channel)

  @channels.synchronize do
    info = @channels[channel]
    if info
      info[:users].delete(nick)
      info[:modes].delete_if {|u|
        u[1] == nick
      }
    end
  end
end

#on_quit(m) ⇒ Object

For managing channel



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/net/irc/client/channel_manager.rb', line 52

def on_quit(m)
  nick = m.prefix.nick

  @channels.synchronize do
    @channels.each do |channel, info|
      info[:users].delete(nick)
      info[:modes].delete_if {|u|
        u[1] == nick
      }
    end
  end
end

#on_rpl_namreply(m) ⇒ Object

For managing channel



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/net/irc/client/channel_manager.rb', line 4

def on_rpl_namreply(m)
  type    = m[1]
  channel = m[2]
  init_channel(channel)

  @channels.synchronize do
    m[3].split(" ").each do |u|
      _, mode, nick = *u.match(/\A([@+]?)(.+)/)

      @channels[channel][:users] << nick
      @channels[channel][:users].uniq!

      op = @server_config.mode_parser.mark_to_op(mode)
      if op
        @channels[channel][:modes] << [op, nick]
      end
    end

    case type
    when "@" # secret
      @channels[channel][:modes] << [:s, nil]
    when "*" # private
      @channels[channel][:modes] << [:p, nil]
    when "=" # public
    end

    @channels[channel][:modes].uniq!
  end
end