-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathirc.rb
183 lines (151 loc) · 3.74 KB
/
irc.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
require 'socket'
require 'set'
require 'time'
require './queue.rb'
class IRC
def initialize(server, port, nick, password, channel)
@logfile = File.open(CONFIG::LOGFILE % channel, "a+")
@server = server
@port = port
@nick = nick
@password = password
@channel = channel
@users = Set.new
@moderators = Set.new
@message_queue = MessageQueue.new(self)
@running = false
@message_length = CONFIG::MESSAGEMAXLENGTH
end
def connect
@running = true
@socket = TCPSocket.open(@server, @port)
send "PASS #{@password}", true
send "NICK #{@nick}"
send "JOIN #{@channel}"
end
def quit
send "QUIT" if not @running
@socket.close
end
def send(message, mute = false)
@socket.send "#{message}\n", 0
log "SENDING: #{message}", true if CONFIG::VERBOSE && !mute
end
def say(message, show = false)
return if message.empty?
log "Queuing message: #{message}"
@message_queue.add(message, show)
end
def say_raw(message)
send "PRIVMSG #{@channel} :#{CONFIG::MESSAGEPREFIX}#{message}"
end
def send_message_queue
begin
to_send = @message_queue.next(@message_length)
return if to_send.empty?
sending = to_send.join(CONFIG::MESSAGEDELIMITER)
send "PRIVMSG #{@channel} :#{CONFIG::MESSAGEPREFIX}#{sending}"
rescue => error
log error
end
end
def read_stream
Signal.trap("HUP") { @running = false } unless (RUBY_PLATFORM =~ /mswin|msys|mingw|cygwin|bccwin|wince|emc/) != nil
messages = Thread.new {
begin
while @running
sleep(CONFIG::MESSAGEDELAY)
send_message_queue
end
log "Send loop end"
rescue => error
log error
end
}
begin
while (line = @socket.readline) && @running
irc_handle line
end
log "Read loop end"
rescue => error
log "Read loop error"
log error
@running = false
end
end
def irc_handle line
log line if CONFIG::DEBUG
case line.strip
when /^PING :?(.+)$/i
log "SERVER PING"
send "PONG :#{$1}"
when /^:jtv MODE #.+ (\+|-)o (.+)$/i
handle_mod($2, $1)
when /:\w*\.?\w*\.\w* \d{3} .+ :-?\s?(.+?)\s?-?$/i
log "SERVER: #{$1}" if CONFIG::VERBOSE
when /^:.+353.*=\s#.+\s:(.+)/i
handle_userlist($1)
when /^:(.+)!.+(JOIN|PART)\s#.+/i
handle_user($1, $2)
when /^:(.+?)!.+PRIVMSG #{@nick} :(.*)/i
log "#{$1}: #{$2}", true if CONFIG::VERBOSE
else
handle_input line
end
end
def user_mod?(user)
@moderators.include?(user) || CONFIG::ADMINS.include?(user)
end
def user_broadcaster?(user)
user == @channel[1..-1] || CONFIG::ADMINS.include?(user)
end
def handle_user(user, type)
if type == "JOIN"
@users << user
elsif type == "PART"
@users.delete(user)
end
end
def handle_mod(user, type)
if type == '+'
@moderators << user
elsif type == '-'
@moderators.delete(user)
end
end
def handle_userlist(userlist)
log " [ USERLIST ]"
users = userlist.split(" ")
users.each do |user|
@users << user.strip
end
end
def log(message, tab = false)
begin
message = CONFIG::DEBUG ? message : message[0..CONFIG::LOGTRUNCATE]
message = " #{message}" if tab
puts message if CONFIG::DEBUG
@logfile.write("#{message}\n")
@logfile.flush
rescue
end
end
def moderators
@moderators
end
def users
@users
end
def channel
return @channel
end
def message_length
@message_length
end
def message_length=(length)
@message_length = length
end
def message_queue
@message_queue
end
end