/
channel.rb
86 lines (74 loc) · 1.61 KB
/
channel.rb
1
2
3
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
require 'thread'
require 'fiber'
require 'ponder/recipient'
module Ponder
class Channel < Recipient
def initialize(name, thaum)
super
@name = name
@users = {}
end
def name
@mutex.synchronize do
@name
end
end
def topic
if @topic
@topic
else
fiber = Fiber.current
callbacks = {}
[331, 332, 403, 442].each do |numeric|
callbacks[numeric] = @thaum.on(numeric) do |event_data|
topic = event_data[:params].match(':(.*)').captures.first
fiber.resume topic
end
end
raw "TOPIC #{@name}"
@topic = Fiber.yield
callbacks.each do |type, callback|
@thaum.callbacks[type].delete(callback)
end
@topic
end
end
def add_user(user, prefixes = [])
@mutex.synchronize do
@users[user.nick.downcase] = [user, prefixes]
end
end
def remove_user(nick)
@mutex.synchronize do
@users.delete nick.downcase
end
end
def has_user?(user_or_nick)
@mutex.synchronize do
nick = case user_or_nick
when String
user_or_nick.downcase
when User
user_or_nick.nick.downcase
end
@users.key? nick
end
end
def users
@mutex.synchronize do
@users
end
end
def find_user(nick)
@mutex.synchronize do
@users[nick.downcase]
end
end
def message(message)
raw "PRIVMSG #{@name} :#{message}"
end
def inspect
"#<Channel name=#{@name.inspect}>"
end
end
end