This repository has been archived by the owner on Oct 9, 2019. It is now read-only.
/
shinybot-fiji.rb
executable file
·668 lines (559 loc) · 18.9 KB
/
shinybot-fiji.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
#!/usr/bin/env ruby
require 'socket'
require 'thread'
require 'cgi'
$botname = "shinybot"
def valid_utf8?( s )
s =~ /^(([\x00-\x7f]|[\xc0-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3})*)(.*)$/m
$3.empty?
end
def make_tinyurl(url)
tinyurl = nil
Kernel.open( "|-", "r" ) do |f|
if f
tinyurl = f.read
else
exec "curl", "-s", "http://tinyurl.com/api-create.php?url=#{CGI.escape(url)}"
end
end
tinyurl
end
def whois_for_ip( ip_string )
unless ip_string =~ /^\d+\.\d+\.\d+\.\d+$/
return "Error: malformed IP address (BUG)"
end
results = `whois #{ip_string}`
s = nil
if results =~ /^inetnum:\s+(.*)$/
s = "in range " + $1.dup
end
if results =~ /^netname:\s+(.*)$/
s ? (s += ": " + $1.dup) : (s = $1.dup)
end
if results =~ /^descr:\s+(.*)$/
s ? (s += ", " + $1.dup) : (s = $1.dup)
end
if results =~ /^country:\s+(.*)$/
s ? (s += ", " + $1.dup) : (s = $1.dup)
end
return s ? s : "No information found by whois"
end
# The following states mean:
# 0 waiting for the first MM (will respond: "do dooo bedodo")
# 1 waiting for the second MM (will respond: "do do-do do")
# 2 waiting for the third MM (will respond: "do dooo bedodo, bedodo, bedodo, be-doodle-doodle-do do do-do do")
# 3 waiting for anything at all (will repond: "/me gives <> a hard stare")
# After state 3, goes immediately to state 0
$mahna_mahnah_state = 0
$mm_re = /^ *m[a\303\240]+h?[ \-]*n[a\303\240]+h?[ \-]*m[a\303\240]+h?[ \-]*n[a\303\240]+h?/i
def gitweb_url_for_repo( object, lineno, repo )
base_git_dir = '/srv/git/'
base_gitweb_url = 'http://fiji.sc/git/?p='
git = 'git --git-dir=' + base_git_dir + repo
file = ''
action = `#{git} cat-file -t #{object} 2>/dev/null`.chomp
if action.empty?
files = `#{git} rev-parse --verify HEAD:#{object} 2>/dev/null`.chomp
if files.empty?
grep = 'grep "/' + object + '$" 2>/dev/null'
files = `#{git} ls-tree -r --name-only HEAD | #{grep}`.chomp
if files.empty? or files =~ /\n/
return nil
end
file = ';f=' + files
object = `#{git} rev-parse HEAD:#{files}`.chomp
else
file = ';f=' + object
object = files
end
action = `#{git} cat-file -t #{object} 2>/dev/null`.chomp
end
if action == 'object'
action = 'objectdiff'
end
tail = ';hb=' + `sed 's/^ref: //' < #{base_git_dir}#{repo}/HEAD`.chomp
if lineno
tail += '#l' + lineno.gsub(/^:/,'')
end
return base_gitweb_url + repo + ';a=' + action + file + ';h=' + object + tail
end
def gitweb_url( object, lineno )
repos = %w[
fiji.git ImageJA.git trakem2.git mpicbg.git bio-formats/.git
]
repos.each do |repo|
url = gitweb_url_for_repo( object, lineno, repo )
if url
return url
end
end
return nil
end
class IRCClient
attr_accessor :host, :port, :password, :realname, :nick, :channel
def initialize
# @host = "rrt.sc3d.org"
@port = "6667"
@host = "irc.freenode.net"
@password = ""
@realname = "shinybot version 0.3"
@nick = $botname
@channel = "fiji-devel"
@mutex = Mutex.new
@last_ping = nil
@last_pong = nil
@finding_a_nick = false
end
def iso_time
Time.now.gmtime.strftime "%Y%m%dT%H%M%S"
end
def date_string
Time.now.gmtime.strftime( "%Y-%m-%d" )
end
def today_log_file_name
"/data/devel/irc-bot/logs/fiji-irc-log-#{date_string}"
end
def log_line( line )
open( today_log_file_name, File::WRONLY | File::CREAT | File::APPEND, 0666 ) do |f|
f.puts line
end
end
def log_message( who, what )
log_line sprintf( "%s: %s: %s", iso_time, who, what )
end
def log_action( who, what )
log_line sprintf( "%s: %s* %s", iso_time, who, what )
end
def log_event( event )
log_line sprintf( "%s: *: %s", iso_time, event )
end
def log_name_change( who, whom )
log_line sprintf( "%s: %s# %s", iso_time, who, whom )
end
def log_no_topic( who )
log_line sprintf( "%s: %s> No topic is set", iso_time, who )
end
def log_topic( who, what )
log_line sprintf( "%s: %s> set the topic to: %s", iso_time, who, what )
end
def parse_prefix( prefix )
if prefix =~ /^([^\!]+)\!([^@]+)\@(.*)/
[ $1, $2, $3 ]
else
raise "Got malformed prefix (#{prefix})"
end
end
def send( command, *parameters )
@mutex.synchronize do
message = command + " " + parameters.join( ' ' ) + "\r\n"
log_event "==> " + message.chomp
@socket.print message
end
end
def send_message( recipient, message )
send( "PRIVMSG", recipient, ":" + message )
end
def send_last_lines( recipient, lines )
send_message recipient, "\"last\" is disabled for the moment..."
return
log_today_lines = open( today_log_file_name, "r" ) { |f| f.readlines }
filtered_lines = Array.new
log_today_lines.each do |line|
if line =~ /^........T......: ([^\*>][^: ]*): (.*)$/
nick, user, host = parse_prefix $1
filtered_lines.push "<" + nick + "> " + $2
end
end
filtered_lines[-lines..-1].each do |line|
send_message recipient, line.chomp
sleep 0.5
end
end
def get_next_line
line = @socket.gets
return unless line
line.chomp!
prefix = nil
command = nil
log_event "<== " + line
if line.gsub!( /^:(\S+)\s+(\S+)/, '' )
prefix = $1
command = $2
elsif line.gsub!( /^([^:]\S*)/, '' )
command = $1
else
raise "Couldn't parse message from the IRC server: #{line}"
end
# Now call a handler for that command:
case command
when "ERROR"
if line =~ /^\s+:(.*)/
raise $1
end
when "433" # Then try another nick
if @finding_a_nick
if @nicks_tried >= 3
# Then just exit...
@socket.close
else
@nick = @nick + "_"
@nicks_tried += 1
send( "NICK", @nick )
end
end
when "KICK"
# Just rejoin immediately...
send( "JOIN", "#" + @channel )
when "376" # That's the end of MOTD message...
@finding_a_nick = false
send( "JOIN", "#" + channel )
when "JOIN"
replynick, replyuser, replyhost = parse_prefix prefix
if ((replyhost == "gimel.esc.cam.ac.uk") || (replyhost == "82-69-166-74.dsl.in-addr.zen.co.uk") || (replyhost == "global.panaceas.org") || (replyhost == "pacific.mpi-cbg.de")) &&
(replyuser =~ /^n=([a-f0-9]{8})$/)
begin
message = nil
ip_encoded_in_username = $1.dup
a = ip_encoded_in_username.split( /(..)/ )
ip_string = [ a[1], a[3], a[5], a[7] ].collect { |e| Integer("0x"+e).to_s }.join('.')
host_results = `host #{ip_string}`
if $?.success?
if host_results =~ /\s(\S+)\. *$/
send_message( "#" + @channel, "CGI:IRC user #{replynick} is logged in from #{$1} (#{ip_string}) [" + whois_for_ip(ip_string) + "]" )
end
else
send_message( "#" + @channel, "CGI:IRC user #{replynick} is logged in from #{ip_string} [" + whois_for_ip(ip_string) + "]" )
end
rescue
end
end
when "331" # No topic is set...
if line =~ /^\s+(\S+)\s+(\S+)\s:(.*)$/
recipient = $1
channel = $2
topic = $3
log_no_topic( prefix )
end
when "332" # There is a topic set...
if line =~ /^\s+(\S+)\s+(\S+)\s+:(.*)$/
recipient = $1
channel = $2
topic = $3
log_topic( prefix, topic )
end
when "TOPIC"
if line =~ /^\s+(\S+)\s+:(.*)$/
channel = $1
topic = $2
replynick, replyuser, replyhost = parse_prefix prefix
log_topic( replynick, topic )
end
when "NICK"
if line =~ /^\s+:(.*)$/
new_name = $1
replynick, replyuser, replyhost = parse_prefix prefix
log_name_change( replynick, new_name )
end
when "PRIVMSG"
if line =~ /^\s+(\S+)\s+:(.*)/
recipient = $1
text = $2
# log_event " recipient: " + recipient
# log_event " text: " + text
if recipient == nick
replynick, replyuser, replyhost = parse_prefix prefix
replyto = replynick
elsif recipient =~ /^(\#.*)/
replynick, replyuser, replyhost = parse_prefix prefix
replyto = $1
else
raise "Unknown type of PRIVMSG, line was: #{line}"
end
if text =~ /^\x01ACTION (.*)\x01$/
unless valid_utf8?( $1 )
send( "PRIVMSG", replyto, ":" + "That isn't valid UTF-8." )
end
else
unless valid_utf8?( text )
send( "PRIVMSG", replyto, ":" + "That isn't valid UTF-8." )
end
end
if text =~ /^([dD])(u+)de(s?)([\. \!\?1]*)$/
reply = ($1 == "d") ? "s" : "S"
reply += "e" * $2.length
reply += "riousl"
reply += $3.empty? ? "y" : "ies"
reply += $4
send( "PRIVMSG", replyto, ":" + reply )
return
end
if (text =~ $mm_re) && (recipient =~ /^(\#.*)/)
if $mahna_mahnah_state == 0
send_message( replyto, "do dooo bedodo" )
$mahna_mahnah_state += 1
elsif $mahna_mahnah_state == 1
send_message( replyto, "do do-do do" )
$mahna_mahnah_state += 1
elsif $mahna_mahnah_state == 2
send_message( replyto, "do dooo bedodo, bedodo, bedodo, be-doodle-doodle-do do do-do do" )
$mahna_mahnah_state += 1
elsif $mahna_mahnah_state == 3
send_message( replyto, "\x01ACTION gives #{replynick} a hard stare.\x01" )
$mahna_mahnah_state = 0
end
return
end
if text =~ /\b([0-9a-f]{40}|[._0-9A-Za-z][-._0-9\/A-Za-z]+\.(java|c|py|rb|sh|cxx|bsh|clj|h|js|lut|svg|txt|TXT))(:\d+)?\b/
url = gitweb_url( $1, $3 )
if url
tinyurl = make_tinyurl url
send( "PRIVMSG", replyto, ":#{$1}#{$3} in Gitweb: " + tinyurl )
end
end
issue_tracker_known = false
# Fiji Bugzilla bugs
if text =~ /\b[Ff]iji(\s+[Bb]ug)?\s+#(\d+)\b/
bug_number = $2.to_i(10)
url = "http://fiji.sc/bugzilla/show_bug.cgi?id=#{bug_number}"
send( "PRIVMSG", replyto, ":Fiji bug #{bug_number} can be found here: #{url}" )
issue_tracker_known = true
end
# Fiji GitHub issues
if text =~ /\b[Ff]iji(\s+[Ii]ssue)?\s+#(\d+)\b/
issue_number = $2.to_i(10)
url = "https://github.com/fiji/fiji/issues/#{issue_number}/"
send( "PRIVMSG", replyto, ":Fiji issue #{issue_number} can be found here: #{url}" )
issue_tracker_known = true
end
# ImageJ GitHub issues
if text =~ /\b[Ii]mage[Jj](\s+[Ii]ssue)?\s+#(\d+)\b/
issue_number = $2.to_i(10)
url = "https://github.com/imagej/imagej/issues/#{issue_number}/"
send( "PRIVMSG", replyto, ":ImageJ issue #{issue_number} can be found here: #{url}" )
issue_tracker_known = true
end
# ImageJ Trac tickets
if text =~ /\b[Ii]mage[Jj](\s+[Tt]icket)?\s+#(\d+)\b/
ticket_number = $2.to_i(10)
url = "http://trac.imagej.net/ticket/#{ticket_number}"
send( "PRIVMSG", replyto, ":ImageJ ticket #{ticket_number} can be found here: #{url}" )
issue_tracker_known = true
end
# ImgLib2 GitHub issues
if text =~ /\b[Ii]mg[Ll]ib2?(\s+[Ii]ssue)?\s+#(\d+)\b/
issue_number = $2.to_i(10)
url = "https://github.com/imglib/imglib/issues/#{issue_number}/"
send( "PRIVMSG", replyto, ":ImgLib2 issue #{issue_number} can be found here: #{url}" )
issue_tracker_known = true
end
# OME Bio-Formats GitHub issues
if text =~ /\b[Bb](io)?-?[Ff](ormats?)?(\s+[Ii]ssue)?\s+#(\d+)\b/
issue_number = $4.to_i(10)
url = "https://github.com/openmicroscopy/bioformats/issues/#{issue_number}/"
send( "PRIVMSG", replyto, ":Bio-Formats issue #{issue_number} can be found here: #{url}" )
issue_tracker_known = true
end
# OME Trac tickets
if text =~ /\b[Oo][Mm][Ee](\s+[Tt]icket)?\s+#(\d+)\b/
ticket_number = $2.to_i(10)
url = "https://trac.openmicroscopy.org.uk/ome/ticket/#{ticket_number}"
send( "PRIVMSG", replyto, ":OME ticket #{ticket_number} can be found here: #{url}" )
issue_tracker_known = true
end
# SCIFIO GitHub issues
if text =~ /\b[Ss][Cc][Ii][Ff][Ii][Oo](\s+[Ii]ssue)?\s+#(\d+)\b/
issue_number = $2.to_i(10)
url = "https://github.com/scifio/scifio/issues/#{issue_number}"
send( "PRIVMSG", replyto, ":SCIFIO issue #{issue_number} can be found here: #{url}" )
issue_tracker_known = true
end
# SciJava Common GitHub issues
if text =~ /\b[Ss]ci[Jj]ava(\s+[Ii]ssue)?\s+#(\d+)\b/
issue_number = $2.to_i(10)
url = "https://github.com/scijava/scijava-common/issues/#{issue_number}"
send( "PRIVMSG", replyto, ":SciJava Common issue #{issue_number} can be found here: #{url}" )
issue_tracker_known = true
end
# Warn about unknown projects
if (not issue_tracker_known) && (text =~ /\b([Bb]ug|[Ii]ssue|[Tt]icket)\s+\d+\b/)
send( "PRIVMSG", replyto, ":Sorry, I don't know which project you mean." )
end
if text =~ /\br(\d+)\b/
revision = $1.to_i(10)
url = "http://trac.imagej.net/changeset/#{revision}"
send( "PRIVMSG", replyto, ":ImageJ changeset #{revision} can be found here: #{url}" )
end
message = nil
if (recipient == nick) && (text =~ /(^\s*|\W|^\s*y)a+rr+($|\W)/i)
send( "MODE", "#" + @channel, "+o", replynick )
return
end
if text =~ /^\!?#{@nick}[,:]?\s+(.*)\s*$/
message = $1
elsif ! (recipient =~ /^#/)
message = text
end
if message
case message
when /^ugt/i
send_message( replyto, "Universal Greeting Time: http://www.total-knowledge.com/~ilya/mips/ugt.html" )
when /^help/i
send( "PRIVMSG", replyto, ":" + "I'm logging messages for the archive at http://code.imagej.net/chatlogs/fiji-devel")
when /^last\s*$/
send_last_lines( replynick, 5 )
when /^last ([0-9]+)$/
send_last_lines( replynick, Integer($1) )
when /^.*\?\s*$/
send_message( replyto, "Sorry, I don't know." )
when /^\s*(\S+)\s*$/
send( "PRIVMSG", replyto, ":" + "I don't know how to \"#{message.gsub(/\s+.*/,'')}\"." )
else
send( "PRIVMSG", replynick, ":" + "Sorry, I didn't understand that. Try \"help\"." )
end
elsif text =~ /^\x01ACTION (.*)\x01$/
log_action( replynick, $1 )
elsif recipient =~ /^(\#.*)/
log_message( prefix, text )
else
# Ignore the message...
log_event "Ignoring that one..."
end
else
raise "Couldn't parse a PRIVMSG (#{line})"
end
when "513"
# Seem to get this if you PONG with the wrong reply number...
when "PING"
if line =~ /^\s+:(\S+)/
send( "PONG", ":" + $1 )
end
when "PONG"
if line =~ /^\s+\S+\s+:(\S+)/
begin
@last_pong = Integer( $1 )
rescue
raise "We got an unexpected PONG"
end
end
end
# log_event " prefix is: " + prefix.to_s
# log_event " command is: " + command.to_s
# log_event " line left: " + line
end
def mainloop
sleep_before_connecting = 0
loop do
sleep sleep_before_connecting
@last_ping = nil
@last_pong = nil
begin
@socket = TCPSocket.new( host, port )
rescue SignalException => e
log_event "Got signal #{e} on connecting, shutting down."
exit( -1 )
rescue Errno::ECONNREFUSED
log_event "Connection refused; we'll try again..."
log_event "Trying again in one minute..."
sleep_before_connecting = 120
next
rescue SocketError => e
log_event "Connection refused with a SocketError: " + e
log_event "Trying again in one minute..."
sleep_before_connecting = 120
next
rescue => e
log_event "An exception was caught when trying to connect: " + e
log_event "The exception type was: " + e.class.to_s
log_event "Trying again in two minutes..."
sleep_before_connecting = 240
next
end
send( "PASS", password ) unless password.empty?
@finding_a_nick = true
send( "NICK", nick )
@nicks_tried = 1
# send( "USER", nick, "localhost", host, ":" + realname )
send( "USER", nick, 0, "*", ":" + realname )
actually_quit = false
@pingthread_stop = false
@pingthread = Thread.new { pingthread }
loop do
begin
get_next_line
rescue SignalException => e
log_event "Got signal #{e}, shutting down."
@pingthread_stop = true
@pingthread.join
actually_quit = true
break
rescue Interrupt
log_event "Caught an interrupt: exiting."
@pingthread_stop = true
@pingthread.join
actually_quit = true
break
rescue SyntaxError, NameError => e
raise
rescue => e
log_event "When getting next line, got: " + e.message
log_event "Waiting for pingthread to end..."
@pingthread_stop = true
@pingthread.join
log_event "Trying again in two minutes..."
sleep_before_connecting = 120
break
end
end
begin
@socket.close
rescue Errno::ENOTCONN
log_event "Ignoring ENOTCONN on closing socket"
rescue => e
log_event "Got an exception when closing the socket: " + e
end
break if actually_quit
end
end
def pingthread
interrupt_the_read = false
close_socket = nil
loop do
sleep 60
if @last_ping
if @last_pong
if @last_pong < @last_ping
log_event "Didn't get a reply to the last PING"
log_event "We'll reconnect..."
close_socket = true
break
end
else
log_event "Got no reply to the first PING"
log_event "We'll reconnect..."
close_socket = true
break
end
end
if @pingthread_stop
log_event "We've been asked to stop by the main thread"
log_event "We'll reconnect..."
break
end
@last_ping = Time.now.to_i
send( "PING", "#{@last_ping}" )
unless @finding_a_nick
if @nick != $botname
@nick = $botname
send( "NICK", @nick )
end
end
end
if close_socket
log_event "Trying to close the socket"
@socket.close
log_event "Finished trying to close the socket"
end
end
end
client = IRCClient.new
client.mainloop