ruby pastebin

Posted by b0nger on Sun 27th Dec 05:35 (modification of post by view diff)
download | new post

  1. # Tem IRC Bot 0.1
  2. # Requirements: Ruby 1.9+. Ruby Gems module
  3.  
  4. # TODO/ChangeLog:
  5. #  * MOTD Parser. - Done :D
  6. #  * Ping for all situations (different IRCds use different ping methods). - Got a PING that works with most common setups.
  7. #  * Create fallback nick solution (recv 433 command) - Done.
  8. #  * Globalize socket interface - Done.
  9. #  * Add support for basic connection information messages and refine NOTICE AUTH regex for more IRCds - Done.
  10. #  * Add common IRC command functions (join, part, PRIVMSG, action) - Done.
  11. #  * Make PRIVMSG command wrapper - Done.
  12. #  * Add optional modules support
  13. #  * Add special delimiter support (for CTCP, ACTION and other similiar queries) - Done.
  14.  
  15.  
  16. # Needed Modules.
  17. require 'socket'
  18. require 'rubygems'
  19.  
  20. # Optional Modules.
  21. require 'tem_modules/twitter_tem.rb' # Adds twitter commands, need to edit twitter_tem.rb and must have twitter gem installed.
  22.  
  23.  
  24. # Bot Connection Information (Needs revising).
  25. $hostname = 'irc.monkeyserv.info'
  26. $port = 6667
  27. # Client Information.
  28. $nick = "Tem"
  29. $fallback_nick = "Temmy"
  30. $ident1 = "lool"
  31. $ident2 = "ffdg"
  32. $realname = "lala"
  33.  
  34. # Extra Data.
  35. $main_channel = "#monkey" # Joined on bot connect.
  36.  
  37. # Technical variables.
  38. $SPECIAL = 001.chr # ASCII Delimiter for CTCP.
  39.  
  40.  
  41. $s = TCPSocket.open($hostname, $port)
  42.  
  43. def send_details()
  44.         $s.print "NICK #{$nick}\r\n"
  45.         $s.print "USER #{$ident1} 8 * :#{$ident2} #{$realname}\r\n"
  46. end
  47.  
  48. def send_pong(line)
  49.         pong_data = line.length+1
  50.         $s.print "PONG #{pong_data}\r\n"
  51. end
  52.  
  53. def join(chan)
  54.         $s.print "JOIN #{chan}\r\n"
  55. end
  56.  
  57. def part(chan)
  58.         $s.print "PART #{chan}\r\n"
  59. end
  60.  
  61. def privmsg(subject, msg)
  62.         $s.print "PRIVMSG #{subject} :#{msg}\r\n"
  63. end
  64.  
  65. def action(subject, action_)
  66.         $s.print "PRIVMSG #{subject} :#{$SPECIAL}ACTION #{action_}#{$SPECIAL}\r\n"
  67. end
  68.  
  69. def module_needed(subject, module_name)
  70.         $s.print "PRIVMSG #{subject} :You need to correctly configure and load the following module: #{module_name} to use this command.\r\n"
  71. end
  72.  
  73. def quit_msg(msg)
  74.         $s.print "QUIT :#{msg}"
  75. end
  76.  
  77. # Make the initial connection.
  78.  
  79. send_details()
  80.  
  81. # Main program loop
  82.  
  83. while line = $s.gets
  84.         # Connection Ping handler (should be used to decide ping method, needs work)
  85.         if line =~ /PING/
  86.                 send_pong(line)
  87.                 puts "PING Recieved, Sent PONG."
  88.         elsif line =~ /PRIVMSG/
  89.                 # Always handle PRIVMSG first, incase of containers.
  90.                 # Lets seperate the msg from the command.
  91.                 priv_msg = line.split(" ")
  92.                 # Strip the :
  93.                 priv_msg[3][0] = ""
  94.                 if priv_msg[3] =~ /^\./ # Possible command
  95.                         command_parameters = priv_msg[4,priv_msg.length - 1].join(" ")
  96.                         priv_msg[3][0] = "" # Strip the . from the command.
  97.                         command = priv_msg[3]
  98.                         subject = priv_msg[2]
  99.                         puts "Command Recieved: #{command} Command Parameters: #{command_parameters}"
  100.                         if command == "hi"
  101.                                         privmsg(subject, "Hi!")
  102.                         elsif command == "quit"
  103.                                         privmsg(subject, "Exiting Gracefully..")
  104.                                         quit_msg("Byeeee From Tem :D")
  105.                                         break;
  106.                         elsif command == "do"
  107.                                         action(subject, command_parameters)
  108.                         elsif command == "twitter"
  109.                                         # Test for module loaded.
  110.                                         define_ = defined? Twitter
  111.                                         if nil != define_
  112.                                                 # Module seems loaded. 
  113.                                                 twit_instance = Twit.new(command_parameters)
  114.                                         else
  115.                                                 module_needed(subject, "twitter")
  116.                                         end
  117.                         end
  118.                 else
  119.                         full_cmd = priv_msg[3,priv_msg.length - 1].join(" ")
  120.                         puts "Message Recieved: #{full_cmd}"
  121.                 end
  122.         # Use nick fallback if nick is in use (prevents connection)
  123.         elsif line =~ /433[\s](\*)?[\s]+#{$nick}[\s]+(\:)?/
  124.                 $s.print "NICK #{$fallback_nick}\r\n"
  125.                 $s.print "USER #{$ident1} 8 * :#{$ident2} #{$realname}\r\n"
  126.                 # For this run, change $nick to the $fallback_nick for bot to keep going.
  127.                 $nick = $fallback_nick
  128.         # Connection Information parser.
  129.         elsif line =~ /00(1|2|3|4|5)[\s]+#{$nick}[\s]+(\:)?/ # Seems plausibly the messages.
  130.                 # The 00# numbers can give us a sure server real hostname, we'll use 002 to grab it.
  131.                 if line =~ /002[\s]#{$nick}[\s]\:/
  132.                         real_host = line.split(/002/)
  133.                         real_host[0][0] = ""; # Chop off the : at the start of the host.
  134.                         $server_real_host = real_host[0];
  135.                 end
  136.         # Second Connection Information parser.
  137.         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.
  138.                 # Check for certain ones that dont use a :
  139.                 if line =~ /#{$nick}[\s]\:/
  140.                         connect_info = line.split(/#{$nick}[\s]\:/);
  141.                 else
  142.                         connect_info = line.split(/#{$nick}/);
  143.                 end
  144.                 puts "Server Information Recieved: #{connect_info[1]}"
  145.         # MOTD Parser (needs to be passed to the mining engine.)
  146.         elsif line =~ /375[\s]+#{$nick}[\s]+\:/
  147.                 # Create own loop to grab entire MOTD before letting main loop continue.
  148.                 $motd = "";
  149.                 puts "Processing MOTD.."
  150.                 while line = $s.gets
  151.                         # Check for end of MOTD.
  152.                         if line =~ /376[\s]+#{$nick}[\s]+\:/
  153.                                 break # Return control to main loop.
  154.                         end
  155.                         # Process each line of the MOTD.
  156.                         if line =~ /372[\s]+#{$nick}[\s]+\:/
  157.                                 $motd = $motd + line # Add the line to the MOTD global string. 
  158.                         end
  159.                 end
  160.                 puts "MOTD Parsed successfully."
  161.                 # Put in the call to join a channel now connection seems clear.
  162.                 join($main_channel)    
  163.         # Connection NOTICE parser.
  164.         elsif line =~ /NOTICE/
  165.                 if line =~ /NOTICE[\s]+(AUTH|Auth)([\s]\:)?/
  166.                         notice = line.split(/(Auth|AUTH)[\s]\:/);
  167.                         puts "Auth Notice Recieved: #{notice[2]}"
  168.                 else
  169.                         notice = line.split(/NOTICE[\s]+#{$nick}[\s]+\:/);
  170.                         puts "Notice recieved: #{notice[1]}"
  171.                 end
  172.         # Unknown line? just protocol info?
  173.         else
  174.                 puts line.chop
  175.         end
  176. end
  177.  
  178. $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.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


I'm Human
Remember me



Captcha required for posting