Posted by b0nger on Sun 27th Dec 05:35 (modification of post by view diff)
download | new post
- # Tem IRC Bot 0.1
- # Requirements: Ruby 1.9+. Ruby Gems module
- # TODO/ChangeLog:
- # * MOTD Parser. - Done :D
- # * Ping for all situations (different IRCds use different ping methods). - Got a PING that works with most common setups.
- # * Create fallback nick solution (recv 433 command) - Done.
- # * Globalize socket interface - Done.
- # * Add support for basic connection information messages and refine NOTICE AUTH regex for more IRCds - Done.
- # * Add common IRC command functions (join, part, PRIVMSG, action) - Done.
- # * Make PRIVMSG command wrapper - Done.
- # * Add optional modules support
- # * Add special delimiter support (for CTCP, ACTION and other similiar queries) - Done.
- # Needed Modules.
- require 'socket'
- require 'rubygems'
- # Optional Modules.
- require 'tem_modules/twitter_tem.rb' # Adds twitter commands, need to edit twitter_tem.rb and must have twitter gem installed.
- # Bot Connection Information (Needs revising).
- $hostname = 'irc.monkeyserv.info'
- $port = 6667
- # Client Information.
- $nick = "Tem"
- $fallback_nick = "Temmy"
- $ident1 = "lool"
- $ident2 = "ffdg"
- $realname = "lala"
- # Extra Data.
- $main_channel = "#monkey" # Joined on bot connect.
- # Technical variables.
- $SPECIAL = 001.chr # ASCII Delimiter for CTCP.
- $s = TCPSocket.open($hostname, $port)
- def send_details()
- $s.print "NICK #{$nick}\r\n"
- $s.print "USER #{$ident1} 8 * :#{$ident2} #{$realname}\r\n"
- end
- def send_pong(line)
- pong_data = line.length+1
- $s.print "PONG #{pong_data}\r\n"
- end
- def join(chan)
- $s.print "JOIN #{chan}\r\n"
- end
- def part(chan)
- $s.print "PART #{chan}\r\n"
- end
- def privmsg(subject, msg)
- $s.print "PRIVMSG #{subject} :#{msg}\r\n"
- end
- def action(subject, action_)
- $s.print "PRIVMSG #{subject} :#{$SPECIAL}ACTION #{action_}#{$SPECIAL}\r\n"
- end
- def module_needed(subject, module_name)
- $s.print "PRIVMSG #{subject} :You need to correctly configure and load the following module: #{module_name} to use this command.\r\n"
- end
- def quit_msg(msg)
- $s.print "QUIT :#{msg}"
- end
- # Make the initial connection.
- send_details()
- # Main program loop
- while line = $s.gets
- # Connection Ping handler (should be used to decide ping method, needs work)
- if line =~ /PING/
- send_pong(line)
- puts "PING Recieved, Sent PONG."
- elsif line =~ /PRIVMSG/
- # Always handle PRIVMSG first, incase of containers.
- # Lets seperate the msg from the command.
- priv_msg = line.split(" ")
- # Strip the :
- priv_msg[3][0] = ""
- if priv_msg[3] =~ /^\./ # Possible command
- command_parameters = priv_msg[4,priv_msg.length - 1].join(" ")
- priv_msg[3][0] = "" # Strip the . from the command.
- command = priv_msg[3]
- subject = priv_msg[2]
- puts "Command Recieved: #{command} Command Parameters: #{command_parameters}"
- if command == "hi"
- privmsg(subject, "Hi!")
- elsif command == "quit"
- privmsg(subject, "Exiting Gracefully..")
- quit_msg("Byeeee From Tem :D")
- break;
- elsif command == "do"
- action(subject, command_parameters)
- elsif command == "twitter"
- # Test for module loaded.
- define_ = defined? Twitter
- if nil != define_
- # Module seems loaded.
- twit_instance = Twit.new(command_parameters)
- else
- module_needed(subject, "twitter")
- end
- end
- else
- full_cmd = priv_msg[3,priv_msg.length - 1].join(" ")
- puts "Message Recieved: #{full_cmd}"
- end
- # Use nick fallback if nick is in use (prevents connection)
- elsif line =~ /433[\s](\*)?[\s]+#{$nick}[\s]+(\:)?/
- $s.print "NICK #{$fallback_nick}\r\n"
- $s.print "USER #{$ident1} 8 * :#{$ident2} #{$realname}\r\n"
- # For this run, change $nick to the $fallback_nick for bot to keep going.
- $nick = $fallback_nick
- # Connection Information parser.
- elsif line =~ /00(1|2|3|4|5)[\s]+#{$nick}[\s]+(\:)?/ # Seems plausibly the messages.
- # The 00# numbers can give us a sure server real hostname, we'll use 002 to grab it.
- if line =~ /002[\s]#{$nick}[\s]\:/
- real_host = line.split(/002/)
- real_host[0][0] = ""; # Chop off the : at the start of the host.
- $server_real_host = real_host[0];
- end
- # Second Connection Information parser.
- elsif line =~ /(2(5(1|2|4|5|0)|6(5|6))|439|931|042)[\s]+#{$nick}[\s]+(\:)?/ # Some IRCds spit out some weird codes.
- # Check for certain ones that dont use a :
- if line =~ /#{$nick}[\s]\:/
- connect_info = line.split(/#{$nick}[\s]\:/);
- else
- connect_info = line.split(/#{$nick}/);
- end
- puts "Server Information Recieved: #{connect_info[1]}"
- # MOTD Parser (needs to be passed to the mining engine.)
- elsif line =~ /375[\s]+#{$nick}[\s]+\:/
- # Create own loop to grab entire MOTD before letting main loop continue.
- $motd = "";
- puts "Processing MOTD.."
- while line = $s.gets
- # Check for end of MOTD.
- if line =~ /376[\s]+#{$nick}[\s]+\:/
- break # Return control to main loop.
- end
- # Process each line of the MOTD.
- if line =~ /372[\s]+#{$nick}[\s]+\:/
- $motd = $motd + line # Add the line to the MOTD global string.
- end
- end
- puts "MOTD Parsed successfully."
- # Put in the call to join a channel now connection seems clear.
- join($main_channel)
- # Connection NOTICE parser.
- elsif line =~ /NOTICE/
- if line =~ /NOTICE[\s]+(AUTH|Auth)([\s]\:)?/
- notice = line.split(/(Auth|AUTH)[\s]\:/);
- puts "Auth Notice Recieved: #{notice[2]}"
- else
- notice = line.split(/NOTICE[\s]+#{$nick}[\s]+\:/);
- puts "Notice recieved: #{notice[1]}"
- end
- # Unknown line? just protocol info?
- else
- puts line.chop
- end
- end
- $s.close
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.