Changeset 15296
- Timestamp:
- 07/06/08 22:33:47 (5 years ago)
- Location:
- lang/ruby/net-irc/trunk
- Files:
-
- 3 added
- 2 modified
-
lib/net/irc/client (added)
-
lib/net/irc/client.rb (modified) (1 diff)
-
lib/net/irc/client/channel_manager.rb (added)
-
spec/channel_manager_spec.rb (added)
-
spec/net-irc_spec.rb (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
lang/ruby/net-irc/trunk/lib/net/irc/client.rb
r15294 r15296 81 81 end 82 82 83 # For managing channel84 def on_rpl_namreply(m)85 type = m[1]86 channel = m[2]87 init_channel(channel)88 89 @channels.synchronize do90 m[3].split(/\s+/).each do |u|91 _, mode, nick = *u.match(/^([@+]?)(.+)/)92 93 @channels[channel][:users] << nick94 @channels[channel][:users].uniq!95 96 op = @server_config.mode_parser.mark_to_op(mode)97 if op98 @channels[channel][:modes] << [op, nick]99 end100 end101 102 case type103 when "@" # secret104 @channels[channel][:modes] << [:s, nil]105 when "*" # private106 @channels[channel][:modes] << [:p, nil]107 when "=" # public108 end109 110 @channels[channel][:modes].uniq!111 end112 end113 114 # For managing channel115 def on_part(m)116 nick = m.prefix.nick117 channel = m[0]118 init_channel(channel)119 120 @channels.synchronize do121 info = @channels[channel]122 if info123 info[:users].delete(nick)124 info[:modes].delete_if {|u|125 u[1] == nick126 }127 end128 end129 end130 131 # For managing channel132 def on_quit(m)133 nick = m.prefix.nick134 135 @channels.synchronize do136 @channels.each do |channel, info|137 info[:users].delete(nick)138 info[:modes].delete_if {|u|139 u[1] == nick140 }141 end142 end143 end144 145 # For managing channel146 def on_kick(m)147 users = m[1].split(/,/)148 149 @channels.synchronize do150 m[0].split(/,/).each do |chan|151 init_channel(chan)152 info = @channels[chan]153 if info154 users.each do |nick|155 info[:users].delete(nick)156 info[:modes].delete_if {|u|157 u[1] == nick158 }159 end160 end161 end162 end163 end164 165 # For managing channel166 def on_join(m)167 nick = m.prefix.nick168 channel = m[0]169 170 @channels.synchronize do171 init_channel(channel)172 173 @channels[channel][:users] << nick174 @channels[channel][:users].uniq!175 end176 end177 178 # For managing channel179 def on_nick(m)180 oldnick = m.prefix.nick181 newnick = m[0]182 183 @channels.synchronize do184 @channels.each do |channel, info|185 info[:users].map! {|i|186 (i == oldnick) ? newnick : i187 }188 info[:modes].map! {|m, target|189 (target == oldnick) ? [m, newnick] : [m, target]190 }191 end192 end193 end194 195 # For managing channel196 def on_mode(m)197 channel = m[0]198 @channels.synchronize do199 init_channel(channel)200 201 mode = @server_config.mode_parser.parse(m)202 mode[:negative].each do |m|203 @channels[channel][:modes].delete(m)204 end205 206 mode[:positive].each do |m|207 @channels[channel][:modes] << m208 end209 210 @channels[channel][:modes].uniq!211 [mode[:negative], mode[:positive]]212 end213 end214 215 # For managing channel216 def init_channel(channel)217 @channels[channel] ||= {218 :modes => [],219 :users => [],220 }221 end222 223 83 # Do nothing. 224 84 # This is for avoiding error on calling super. -
lang/ruby/net-irc/trunk/spec/net-irc_spec.rb
r15294 r15296 278 278 end 279 279 280 it "client should manage channel mode/users correctly" do281 client = @client282 c = @client.instance_variable_get(:@channels)283 TestServerSession.instance.instance_eval do284 Thread.exclusive do285 post client.prefix, JOIN, "#test"286 post nil, NOTICE, "#test", "sep1"287 end288 end289 290 true until client_q.pop.to_s == "NOTICE #test sep1\r\n"291 c.synchronize do292 c.should be_a_kind_of(Hash)293 c["#test"].should be_a_kind_of(Hash)294 c["#test"][:modes].should be_a_kind_of(Array)295 c["#test"][:users].should be_a_kind_of(Array)296 c["#test"][:users].should == ["foonick"]297 end298 299 TestServerSession.instance.instance_eval do300 Thread.exclusive do301 post "test1!test@localhost", JOIN, "#test"302 post "test2!test@localhost", JOIN, "#test"303 post nil, NOTICE, "#test", "sep2"304 end305 end306 307 true until client_q.pop.to_s == "NOTICE #test sep2\r\n"308 c.synchronize do309 c["#test"][:users].should == ["foonick", "test1", "test2"]310 end311 312 TestServerSession.instance.instance_eval do313 Thread.exclusive do314 post nil, RPL_NAMREPLY, client.prefix.nick, "@", "#test", "foo1 foo2 foo3 @foo4 +foo5"315 post nil, NOTICE, "#test", "sep3"316 end317 end318 319 true until client_q.pop.to_s == "NOTICE #test sep3\r\n"320 c.synchronize do321 c["#test"][:users].should == ["foonick", "test1", "test2", "foo1", "foo2", "foo3", "foo4", "foo5"]322 c["#test"][:modes].should include([:s, nil])323 c["#test"][:modes].should include([:o, "foo4"])324 c["#test"][:modes].should include([:v, "foo5"])325 end326 327 TestServerSession.instance.instance_eval do328 Thread.exclusive do329 post nil, RPL_NAMREPLY, client.prefix.nick, "@", "#test1", "foo1 foo2 foo3 @foo4 +foo5"330 post "foo4!foo@localhost", QUIT, "message"331 post "foo5!foo@localhost", PART, "#test1", "message"332 post client.prefix, KICK, "#test", "foo1", "message"333 post client.prefix, MODE, "#test", "+o", "foo2"334 post nil, NOTICE, "#test", "sep4"335 end336 end337 338 true until client_q.pop.to_s == "NOTICE #test sep4\r\n"339 c.synchronize do340 c["#test"][:users].should == ["foonick", "test1", "test2", "foo2", "foo3", "foo5"]341 c["#test1"][:users].should == ["foo1", "foo2", "foo3"]342 c["#test"][:modes].should_not include([:o, "foo4"])343 c["#test"][:modes].should include([:v, "foo5"])344 c["#test1"][:modes].should_not include([:v, "foo5"])345 c["#test"][:modes].should include([:o, "foo2"])346 end347 348 TestServerSession.instance.instance_eval do349 Thread.exclusive do350 post "foonick!test@localhost", NICK, "foonick2"351 post "foonick2!test@localhost", NICK, "foonick"352 post "foo2!test@localhost", NICK, "bar2"353 post "foo3!test@localhost", NICK, "bar3"354 post nil, NOTICE, "#test", "sep5"355 end356 end357 358 true until client_q.pop.to_s == "NOTICE #test sep5\r\n"359 c.synchronize do360 c["#test"][:users].should == ["foonick", "test1", "test2", "bar2", "bar3", "foo5"]361 c["#test1"][:users].should == ["foo1", "bar2", "bar3"]362 c["#test"][:modes].should_not include([:o, "foo4"])363 c["#test"][:modes].should include([:v, "foo5"])364 c["#test1"][:modes].should_not include([:v, "foo5"])365 c["#test"][:modes].should_not include([:o, "foo2"])366 c["#test"][:modes].should include([:o, "bar2"])367 end368 end369 370 280 it "should allow lame RPL_WELCOME (not prefix but nick)" do 371 281 client = @client
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)