This repository has been archived by the owner on Nov 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
bot.rb
342 lines (277 loc) · 7.39 KB
/
bot.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
require 'socket'
module Isaac
VERSION = '0.2.1'
Config = Struct.new(:server, :port, :ssl, :password, :nick, :realname, :version, :environment, :verbose)
class Bot
attr_accessor :config, :irc, :nick, :channel, :message, :user, :host, :match,
:error
def initialize(&b)
@events = {}
@config = Config.new("localhost", 6667, false, nil, "isaac", "Isaac", 'isaac', :production, false)
instance_eval(&b) if block_given?
end
def configure(&b)
b.call(@config)
end
def on(event, match=//, &block)
match = match.to_s if match.is_a? Integer
(@events[event] ||= []) << [Regexp.new(match), block]
end
def helpers(&b)
instance_eval(&b)
end
def halt
throw :halt
end
def raw(command)
@irc.message(command)
end
def msg(recipient, text)
raw("PRIVMSG #{recipient} :#{text}")
end
def action(recipient, text)
raw("PRIVMSG #{recipient} :\001ACTION #{text}\001")
end
def join(*channels)
channels.each {|channel| raw("JOIN #{channel}")}
end
def part(*channels)
channels.each {|channel| raw("PART #{channel}")}
end
def topic(channel, text)
raw("TOPIC #{channel} :#{text}")
end
def mode(channel, option)
raw("MODE #{channel} #{option}")
end
def kick(channel, user, reason=nil)
raw("KICK #{channel} #{user} :#{reason}")
end
def quit(message=nil)
command = message ? "QUIT :#{message}" : "QUIT"
raw command
end
def start
puts "Connecting to #{@config.server}:#{@config.port}" unless @config.environment == :test
@irc = IRC.new(self, @config)
@irc.connect
end
def message
@message ||= ""
end
def dispatch(event, msg=nil)
if msg
@nick, @user, @host, @channel, @error, @message =
msg.nick, msg.user, msg.host, msg.channel, msg.error, msg.message
end
if handler = find(event, message)
regexp, block = *handler
self.match = message.match(regexp).captures
invoke block
end
end
private
def find(type, message)
if events = @events[type]
events.detect {|regexp,_| message.match(regexp)}
end
end
def invoke(block)
mc = class << self; self; end
mc.send :define_method, :__isaac_event_handler, &block
# -1 splat arg, send everything
# 0 no args, send nothing
# 1 defined number of args, send only those
bargs = case block.arity <=> 0
when -1; match
when 0; []
when 1; match[0..block.arity-1]
end
catch(:halt) { __isaac_event_handler(*bargs) }
end
end
class IRC
def initialize(bot, config)
@bot, @config = bot, config
@transfered = 0
@registration = []
end
def connect
tcp_socket = TCPSocket.open(@config.server, @config.port)
if @config.ssl
begin
require 'openssl'
rescue ::LoadError
raise(RuntimeError,"unable to require 'openssl'",caller)
end
ssl_context = OpenSSL::SSL::SSLContext.new
ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE
unless @config.environment == :test
puts "Using SSL with #{@config.server}:#{@config.port}"
end
@socket = OpenSSL::SSL::SSLSocket.new(tcp_socket, ssl_context)
@socket.sync = true
@socket.connect
else
@socket = tcp_socket
end
@queue = Queue.new(@socket, @bot.config.server)
message "PASS #{@config.password}" if @config.password
message "NICK #{@config.nick}"
message "USER #{@config.nick} 0 * :#{@config.realname}"
@queue.lock
while line = @socket.gets
parse line
end
end
def parse(input)
puts "<< #{input}" if @bot.config.verbose
msg = Message.new(input)
if ("001".."004").include? msg.command
@registration << msg.command
if registered?
@queue.unlock
@bot.dispatch(:connect)
end
elsif msg.command == "PRIVMSG"
if msg.params.last == "\001VERSION\001"
message "NOTICE #{msg.nick} :\001VERSION #{@bot.config.version}\001"
end
type = msg.channel? ? :channel : :private
@bot.dispatch(type, msg)
elsif msg.error?
@bot.dispatch(:error, msg)
elsif msg.command == "PING"
@queue.unlock
message "PONG :#{msg.params.first}"
elsif msg.command == "PONG"
@queue.unlock
else
event = msg.command.downcase.to_sym
@bot.dispatch(event, msg)
end
end
def registered?
(("001".."004").to_a - @registration).empty?
end
def message(msg)
@queue << msg
end
end
class Message
attr_accessor :raw, :prefix, :command, :params
def initialize(msg=nil)
@raw = msg
parse if msg
end
def numeric_reply?
!!numeric_reply
end
def numeric_reply
@numeric_reply ||= @command.match(/^\d\d\d$/)
end
def parse
match = @raw.match(/(^:(\S+) )?(\S+)(.*)/)
_, @prefix, @command, raw_params = match.captures
raw_params.strip!
if match = raw_params.match(/:(.*)/)
@params = match.pre_match.split(" ")
@params << match[1]
else
@params = raw_params.split(" ")
end
end
def nick
return unless @prefix
@nick ||= @prefix[/^(\S+)!/, 1]
end
def user
return unless @prefix
@user ||= @prefix[/^\S+!(\S+)@/, 1]
end
def host
return unless @prefix
@host ||= @prefix[/@(\S+)$/, 1]
end
def server
return unless @prefix
return if @prefix.match(/[@!]/)
@server ||= @prefix[/^(\S+)/, 1]
end
def error?
!!error
end
def error
return @error if @error
@error = command.to_i if numeric_reply? && command[/[45]\d\d/]
end
def channel?
!!channel
end
def channel
return @channel if @channel
if regular_command? and params.first.start_with?("#")
@channel = params.first
end
end
def message
return @message if @message
if error?
@message = error.to_s
elsif regular_command?
@message = params.last
end
end
private
# This is a late night hack. Fix.
def regular_command?
%w(PRIVMSG JOIN PART QUIT).include? command
end
end
class Queue
def initialize(socket, server)
# We need server for pinging us out of an excess flood
@socket, @server = socket, server
@queue, @lock, @transfered = [], false, 0
end
def lock
@lock = true
end
def unlock
@lock, @transfered = false, 0
invoke
end
def <<(message)
@queue << message
invoke
end
private
def message_to_send?
!@lock && !@queue.empty?
end
def transfered_after_next_send
@transfered + @queue.first.size + 2 # the 2 is for \r\n
end
def exceed_limit?
transfered_after_next_send > 1472
end
def lock_and_ping
lock
@socket.print "PING :#{@server}\r\n"
end
def next_message
@queue.shift.to_s.chomp + "\r\n"
end
def invoke
while message_to_send?
if exceed_limit?
lock_and_ping; break
else
@transfered = transfered_after_next_send
@socket.print next_message
# puts ">> #{msg}" if @bot.config.verbose
end
end
end
end
end