Changeset 15296

Show
Ignore:
Timestamp:
07/06/08 22:33:47 (5 years ago)
Author:
cho45
Message:

Separeted some utility of managing channel state to ChannelManager?.

Location:
lang/ruby/net-irc/trunk
Files:
3 added
2 modified

Legend:

Unmodified
Added
Removed
  • lang/ruby/net-irc/trunk/lib/net/irc/client.rb

    r15294 r15296  
    8181        end 
    8282 
    83         # For managing channel 
    84         def on_rpl_namreply(m) 
    85                 type    = m[1] 
    86                 channel = m[2] 
    87                 init_channel(channel) 
    88  
    89                 @channels.synchronize do 
    90                         m[3].split(/\s+/).each do |u| 
    91                                 _, mode, nick = *u.match(/^([@+]?)(.+)/) 
    92  
    93                                 @channels[channel][:users] << nick 
    94                                 @channels[channel][:users].uniq! 
    95                                  
    96                                 op = @server_config.mode_parser.mark_to_op(mode) 
    97                                 if op 
    98                                         @channels[channel][:modes] << [op, nick] 
    99                                 end 
    100                         end 
    101  
    102                         case type 
    103                         when "@" # secret 
    104                                 @channels[channel][:modes] << [:s, nil] 
    105                         when "*" # private 
    106                                 @channels[channel][:modes] << [:p, nil] 
    107                         when "=" # public 
    108                         end 
    109  
    110                         @channels[channel][:modes].uniq! 
    111                 end 
    112         end 
    113  
    114         # For managing channel 
    115         def on_part(m) 
    116                 nick    = m.prefix.nick 
    117                 channel = m[0] 
    118                 init_channel(channel) 
    119  
    120                 @channels.synchronize do 
    121                         info = @channels[channel] 
    122                         if info 
    123                                 info[:users].delete(nick) 
    124                                 info[:modes].delete_if {|u| 
    125                                         u[1] == nick 
    126                                 } 
    127                         end 
    128                 end 
    129         end 
    130  
    131         # For managing channel 
    132         def on_quit(m) 
    133                 nick = m.prefix.nick 
    134  
    135                 @channels.synchronize do 
    136                         @channels.each do |channel, info| 
    137                                 info[:users].delete(nick) 
    138                                 info[:modes].delete_if {|u| 
    139                                         u[1] == nick 
    140                                 } 
    141                         end 
    142                 end 
    143         end 
    144  
    145         # For managing channel 
    146         def on_kick(m) 
    147                 users = m[1].split(/,/) 
    148  
    149                 @channels.synchronize do 
    150                         m[0].split(/,/).each do |chan| 
    151                                 init_channel(chan) 
    152                                 info = @channels[chan] 
    153                                 if info 
    154                                         users.each do |nick| 
    155                                                 info[:users].delete(nick) 
    156                                                 info[:modes].delete_if {|u| 
    157                                                         u[1] == nick 
    158                                                 } 
    159                                         end 
    160                                 end 
    161                         end 
    162                 end 
    163         end 
    164  
    165         # For managing channel 
    166         def on_join(m) 
    167                 nick    = m.prefix.nick 
    168                 channel = m[0] 
    169  
    170                 @channels.synchronize do 
    171                         init_channel(channel) 
    172  
    173                         @channels[channel][:users] << nick 
    174                         @channels[channel][:users].uniq! 
    175                 end 
    176         end 
    177  
    178         # For managing channel 
    179         def on_nick(m) 
    180                 oldnick = m.prefix.nick 
    181                 newnick = m[0] 
    182  
    183                 @channels.synchronize do 
    184                         @channels.each do |channel, info| 
    185                                 info[:users].map! {|i| 
    186                                         (i == oldnick) ? newnick : i 
    187                                 } 
    188                                 info[:modes].map! {|m, target| 
    189                                         (target == oldnick) ? [m, newnick] : [m, target] 
    190                                 } 
    191                         end 
    192                 end 
    193         end 
    194  
    195         # For managing channel 
    196         def on_mode(m) 
    197                 channel = m[0] 
    198                 @channels.synchronize do 
    199                         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                         end 
    205  
    206                         mode[:positive].each do |m| 
    207                                 @channels[channel][:modes] << m 
    208                         end 
    209  
    210                         @channels[channel][:modes].uniq! 
    211                         [mode[:negative], mode[:positive]] 
    212                 end 
    213         end 
    214  
    215         # For managing channel 
    216         def init_channel(channel) 
    217                 @channels[channel] ||= { 
    218                         :modes => [], 
    219                         :users => [], 
    220                 } 
    221         end 
    222  
    22383        # Do nothing. 
    22484        # This is for avoiding error on calling super. 
  • lang/ruby/net-irc/trunk/spec/net-irc_spec.rb

    r15294 r15296  
    278278        end 
    279279 
    280         it "client should manage channel mode/users correctly" do 
    281                 client = @client 
    282                 c = @client.instance_variable_get(:@channels) 
    283                 TestServerSession.instance.instance_eval do 
    284                         Thread.exclusive do 
    285                                 post client.prefix,          JOIN,   "#test" 
    286                                 post nil,                    NOTICE, "#test", "sep1" 
    287                         end 
    288                 end 
    289  
    290                 true until client_q.pop.to_s == "NOTICE #test sep1\r\n" 
    291                 c.synchronize do 
    292                         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                 end 
    298  
    299                 TestServerSession.instance.instance_eval do 
    300                         Thread.exclusive do 
    301                                 post "test1!test@localhost", JOIN,   "#test" 
    302                                 post "test2!test@localhost", JOIN,   "#test" 
    303                                 post nil,                    NOTICE, "#test", "sep2" 
    304                         end 
    305                 end 
    306  
    307                 true until client_q.pop.to_s == "NOTICE #test sep2\r\n" 
    308                 c.synchronize do 
    309                         c["#test"][:users].should      == ["foonick", "test1", "test2"] 
    310                 end 
    311  
    312                 TestServerSession.instance.instance_eval do 
    313                         Thread.exclusive do 
    314                                 post nil,                    RPL_NAMREPLY, client.prefix.nick, "@", "#test", "foo1 foo2 foo3 @foo4 +foo5" 
    315                                 post nil,                    NOTICE, "#test", "sep3" 
    316                         end 
    317                 end 
    318  
    319                 true until client_q.pop.to_s == "NOTICE #test sep3\r\n" 
    320                 c.synchronize do 
    321                         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                 end 
    326  
    327                 TestServerSession.instance.instance_eval do 
    328                         Thread.exclusive do 
    329                                 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                         end 
    336                 end 
    337  
    338                 true until client_q.pop.to_s == "NOTICE #test sep4\r\n" 
    339                 c.synchronize do 
    340                         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                 end 
    347  
    348                 TestServerSession.instance.instance_eval do 
    349                         Thread.exclusive do 
    350                                 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                         end 
    356                 end 
    357  
    358                 true until client_q.pop.to_s == "NOTICE #test sep5\r\n" 
    359                 c.synchronize do 
    360                         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                 end 
    368         end 
    369  
    370280        it "should allow lame RPL_WELCOME (not prefix but nick)" do 
    371281                client = @client