Class: Butler::IRC::Channel

Inherits:
Object
  • Object
show all
Includes:
Comparable, Enumerable
Defined in:
lib/butler/irc/channel.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#join

Constructor Details

#initialize(all_users, name, topic = nil, topic_set_by = nil, topic_set_at = nil) ⇒ Channel

Create a Butler::IRC::Channel-object. Use Channel.create(<channelname>) if you can.



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/butler/irc/channel.rb', line 33

def initialize(all_users, name, topic=nil, topic_set_by=nil, topic_set_at=nil)
	name          = name.to_str.strip_user_prefixes
	raise "Invalid channelname '#{name}'" unless name.valid_channelname?
	@all_users    = all_users
	@name         = name.freeze
	@compare      = name.downcase.freeze
	@hash         = @compare.hash
	@users        = {}
	@topic        = Topic.new(topic || "", topic_set_by, topic_set_at)
	@lock         = Mutex.new
end

Instance Attribute Details

#compareObject (readonly)

the value used for comparison/sorting



26
27
28
# File 'lib/butler/irc/channel.rb', line 26

def compare
  @compare
end

#hashObject (readonly)

:nodoc:



29
30
31
# File 'lib/butler/irc/channel.rb', line 29

def hash
  @hash
end

#nameObject (readonly)

the name of the channel (immutable)



21
22
23
# File 'lib/butler/irc/channel.rb', line 21

def name
  @name
end

#topicObject (readonly)

the current topic of the channel



23
24
25
# File 'lib/butler/irc/channel.rb', line 23

def topic
  @topic
end

Instance Method Details

#<=>(other) ⇒ Object

Comparison based on lowercase channelname, -1, 0 or 1



89
90
91
# File 'lib/butler/irc/channel.rb', line 89

def <=>(other)
	@compare <=> other.to_str.downcase
end

#==(other) ⇒ Object

Comparison based on lowercase channelname



84
85
86
# File 'lib/butler/irc/channel.rb', line 84

def ==(other)
	@compare == other.to_str.downcase
end

#[](nick) ⇒ Object

Get user by nick if or nil if that user is not in this channel



100
101
102
103
# File 'lib/butler/irc/channel.rb', line 100

def [](nick)
	nick = nick.to_str.downcase
	@users.has_key?(nick) ? @all_users[nick] : nil
end

#add_user(user, reason) ⇒ Object

Add a user to the channel, see Butler::IRC::CHANNEL_SIGNALS for possible reasons (should only be used by IRC::Parser)



95
96
97
# File 'lib/butler/irc/channel.rb', line 95

def add_user(user, reason) #:nodoc:
	@users[user.to_str.downcase] ||= 0
end

#clones(strong = false, min = 2) ⇒ Object

Looks up if there are clones in the list of users min defines how many users need to have the same host (+user if strong) to appear in the list.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/butler/irc/channel.rb', line 48

def clones(strong=false, min=2)
	sieve	= Hash.new { |h,k| h[k] = [] }
	if strong then
		users.each { |user|
			sieve["#{user.user}@#{user.host}"] << user if (user.user && user.host)
		}
	else
		users = users()
		users.each { |user|
			sieve[user.host] << user if user.host
		}
	end
	sieve.select { |host, users| users.length >= min }
end

#delete_user(user, reason) ⇒ Object

Remove a user from the channel, see Butler::IRC::CHANNEL_SIGNALS for possible reasons (should only be used by Butler::IRC::Parser)



112
113
114
# File 'lib/butler/irc/channel.rb', line 112

def delete_user(user, reason) #:nodoc:
	@users.delete(user.to_str.downcase)
end

#eachObject

Iterate over all users in this channel



74
75
76
# File 'lib/butler/irc/channel.rb', line 74

def each
	@users.each_value { |user| yield @all_users[user] }
end

#eql?(other) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


130
131
132
# File 'lib/butler/irc/channel.rb', line 130

def eql?(other) #:nodoc:
	self.class == other.class && @compare == other.compare
end

#include?(nick) ⇒ Boolean

Test if user with nick is in this channel (also works with IRC::User objects)

Returns:

  • (Boolean)


106
107
108
# File 'lib/butler/irc/channel.rb', line 106

def include?(nick)
	@users.has_key?(nick.to_str.downcase)
end

#inspectObject

:nodoc:



116
117
118
# File 'lib/butler/irc/channel.rb', line 116

def inspect # :nodoc:
	"#<%s:0x%x %s (%d users)" %  [self.class, object_id, @name, length]
end

#lengthObject

Returns amount of users



79
80
81
# File 'lib/butler/irc/channel.rb', line 79

def length
	@users.length
end

#nicksObject

List of normalized (lowercased and flag-stripped) nicknames in this channel



69
70
71
# File 'lib/butler/irc/channel.rb', line 69

def nicks
	@users.keys
end

#to_sObject

Returns the (frozen!) name of the channel



121
122
123
# File 'lib/butler/irc/channel.rb', line 121

def to_s
	@name
end

#to_strObject

Returns the normalized name of the channel



126
127
128
# File 'lib/butler/irc/channel.rb', line 126

def to_str #:nodoc:
	@compare
end

#usersObject

List of users (Butler::IRC::User objects) in this channel



64
65
66
# File 'lib/butler/irc/channel.rb', line 64

def users
	@all_users.map_nicks(*@users.keys)
end