Class: Butler::IRC::UserList
- Inherits:
-
Object
- Object
- Butler::IRC::UserList
- Includes:
- Enumerable
- Defined in:
- lib/butler/irc/userlist.rb
Overview
Enumerable: all known & visible users
Instance Attribute Summary collapse
-
#lock ⇒ Object
readonly
provides a global (within Butler::IRC::User) mutex.
-
#myself ⇒ Object
readonly
the user that represents the clients user.
Instance Method Summary collapse
-
#[](nick) ⇒ Object
get a user by nick (may include user prefixes).
- #channels=(channels) ⇒ Object
-
#clones(strong = false, min = 2) ⇒ Object
list clones (comparing all known users).
- #create(nick, *args) ⇒ Object
- #create_myself(nick, user, real) ⇒ Object
- #delete(user, reason) ⇒ Object
- #delete_channel(channel, reason) ⇒ Object
- #each(&block) ⇒ Object
-
#initialize(client) ⇒ UserList
constructor
A new instance of UserList.
- #map_nicks(*nicks) ⇒ Object
- #rehash(old_index, new_index) ⇒ Object
Methods included from Enumerable
Constructor Details
#initialize(client) ⇒ UserList
Returns a new instance of UserList.
27 28 29 30 31 32 33 34 |
# File 'lib/butler/irc/userlist.rb', line 27 def initialize(client) @client = client @users = {} @lost = {} @lock = Mutex.new @myself = nil @channels = nil end |
Instance Attribute Details
#lock ⇒ Object (readonly)
provides a global (within Butler::IRC::User) mutex
25 26 27 |
# File 'lib/butler/irc/userlist.rb', line 25 def lock @lock end |
#myself ⇒ Object (readonly)
the user that represents the clients user
23 24 25 |
# File 'lib/butler/irc/userlist.rb', line 23 def myself @myself end |
Instance Method Details
#[](nick) ⇒ Object
get a user by nick (may include user prefixes)
107 108 109 |
# File 'lib/butler/irc/userlist.rb', line 107 def [](nick) @users[nick.to_str.strip_user_prefixes.downcase] end |
#channels=(channels) ⇒ Object
36 37 38 39 |
# File 'lib/butler/irc/userlist.rb', line 36 def channels=(channels) raise "Can't set @channels twice" if @channels @channels = channels end |
#clones(strong = false, min = 2) ⇒ Object
list clones (comparing all known users)
76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/butler/irc/userlist.rb', line 76 def clones(strong=false, min=2) sieve = Hash.new { |h,k| h[k] = [] } if strong then each { |user| sieve["#{user.user}@#{user.host}"] << user if (user.host && user.user) } else each { |user| sieve[user.host] << user if user.host } end sieve.reject { |host, users| users.length < min } end |
#create(nick, *args) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/butler/irc/userlist.rb', line 50 def create(nick, *args) @lock.synchronize { new_user = User.new(self, @channels, nick, *args) old_user = self[nick] if old_user == new_user then old_user.update(*args) old_user elsif !old_user then @users[new_user.compare] = new_user # if old user was out of sight the new user overrides the old elsif old_user.status == :out_of_sight || old_user.status == :unknown then @users.delete(old_user.compare) @users[new_user.compare] = new_user else # Should NOT happen old_user.status = :mutated @users.delete(old_user.compare) @users[new_user.compare] = new_user raise "A user mutated #{old_user.inspect} -> #{new_user.inspect}" end } end |
#create_myself(nick, user, real) ⇒ Object
45 46 47 48 |
# File 'lib/butler/irc/userlist.rb', line 45 def create_myself(nick, user, real) @myself = create(nick, nil, nil, nil) @myself.status = :myself end |
#delete(user, reason) ⇒ Object
98 99 100 |
# File 'lib/butler/irc/userlist.rb', line 98 def delete(user, reason) @users.delete(user.to_str.downcase) end |
#delete_channel(channel, reason) ⇒ Object
102 103 104 |
# File 'lib/butler/irc/userlist.rb', line 102 def delete_channel(channel, reason) @users.each_value { |user| user.delete_channel(channel, reason) } end |
#each(&block) ⇒ Object
90 91 92 |
# File 'lib/butler/irc/userlist.rb', line 90 def each(&block) @users.each_value(&block) end |
#map_nicks(*nicks) ⇒ Object
41 42 43 |
# File 'lib/butler/irc/userlist.rb', line 41 def map_nicks(*nicks) @users.values_at(*nicks) end |
#rehash(old_index, new_index) ⇒ Object
94 95 96 |
# File 'lib/butler/irc/userlist.rb', line 94 def rehash(old_index, new_index) @users[new_index] = @users.delete(old_index) end |