#!/usr/bin/ruby ## ## an experimental irc bot ## require 'socket' channel = '#novus' server = 'irc.freenode.net' port = 6667 username = 'rubybot' hostname = 'hackdmz.net' servername = 'ircd.hackdmz.net' realname = ':rubybot' nick = 'Boogers' TCPSocket.open(server, port) do |sock| sock.puts "USER #{username} #{hostname} #{servername} #{realname}\r\n" sock.puts "Nick #{nick}\r\n" sock.puts "JOIN #{channel}\r\n" while !sock.closed? line = sock.readline.chomp puts "#{line}\n" # Support PING sock.puts "PONG #{$1}\r\n" if line =~ /PING :(.*)/ # !help list commands sock.puts "PRIVMSG #{channel} :I support: [ !help !say !do !lfm_last !nick !uptime ]\r\n" if line =~ /.*?PRIVMSG #{channel} :!help/ # !say echo what people !say me to do sock.puts "PRIVMSG #{channel} :#{$1}\r\n" if line =~ /.*?PRIVMSG #{nick} :!say (.*)/ # !do sock.puts "PRIVMSG #{channel} :\001ACTION #{$1}\001\r\n" if line =~ /.*?PRIVMSG #{nick} :!do (.*)/ # !lfm_last Try to get the nowlistening track of a lastfm user if line =~ /.*?PRIVMSG #{channel} :!lfm_last (.*)/ user = $1 page = %x[wget -O - http://www.last.fm/user/'#{user}'] flag = 0 page.each_line do |line| if flag == 0 if line =~ /(.*)<\/a> .* (.*)<\/a>/ sock.puts "PRIVMSG #{channel} :Latest track for #{user}: #{$1} - #{$2}\r\n" flag = 1 end end end sock.puts "PRIVMSG #{channel} :Unable to find last.fm information for #{user}.\r\n" if flag == 0 end # !uptime if line =~ /:(.*)!.*?PRIVMSG #{channel} :!uptime/ user = $1 uptime = %x[uptime] sock.puts "PRIVMSG #{channel} :#{user}: #{uptime}\r\n" end # !nick functionality if line =~ /.*?PRIVMSG #{channel} :!nick (.*)/ sock.puts "Nick #{$1}\r\n" nick = $1 end end end