#!/usr/bin/ruby # Author: Yoann Guillot, 03/2007 # licence: GPLv2 require 'socket' load 'mh_resumemsg.rb' def unhandled l puts "unhandled: #{l}" end class IrcBot attr_reader :chan def initialize(chan, pass = nil) @chan, @pass = chan, pass @nick = 'pogopastebot' @nickpass = 'h1HK4urF6N' @s = TCPSocket.open('irc.ircube.net', 6667) end def close @s.shutdown @s.close end def send a return if not a or a.empty? @s.write a + "\r\n" end def pm(a, target = @chan) return if not a or a.empty? puts '> '+a @lastmsg = a send "PRIVMSG #{target} :#{a}" end def main_loop connect loop { do_things } end def connect send "user #@nick #@nick #@nick #@nick" send "nick #@nick" loop do throw :notconn unless IO.select([@s], nil, nil, 60) case @s.gets.chomp when / 376 / break when / 433 / send "nick #@nick#{rand(1000)}" when /PING (.*)/ send "PONG #$1" end end send "z login #@nick #@nickpass" send "join #@chan #@pass" end def do_things @accu ||= {} @times ||= {} cnt = 0 loop do cnt+=1 throw :timeout if cnt > 4*60 @times.keys.find_all { |sender| Time.now - @times[sender] >= 4 }.each { |sender| @times.delete(sender) pm resume("\00304#{sender}\003", @accu.delete(sender).join("\n")) } break if IO.select([@s], nil, nil, 1) end case l = @s.gets.chomp when /^PING (.*)/ send "PONG #$1" when / 404 .* :Cannot send to channel/ pm @lastmsg.gsub(/\003\d?\d?/, '') when /:tyrollus!.* PRIVMSG .* :reload/ pm 'ok', 'tyrollus' load $0 when /:tyrollus!.* PRIVMSG .* :raw (.*)/ pm 'ok', 'tyrollus' puts "#{Time.now.strftime('%d/%m %H:%M')} sending raw #$1" send $1 when /:(.*?)!.*? PRIVMSG [^#&%]\S* :(.+)/ sender, line = $1, $2 puts "from #{sender}: #{line}" @times[sender] = Time.now (@accu[sender] ||= []) << line when / (PRIVMSG|NICK|JOIN|PART|QUIT|MODE|KICK|NOTICE|372) / else puts l end end end if not defined? $started $started = true chan, pass = ARGV.shift, ARGV.shift loop do begin bot = IrcBot.new(chan, pass) bot.main_loop rescue puts $!.class, $! bot.close rescue nil sleep 300 retry end end end