patrickanthony

random ruby irc bot

Jun 18th, 2013
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.41 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. require 'socket'
  4. # Don't allow use of "tainted" data by potentially dangerous operations
  5. $SAFE=1
  6.  
  7. # The irc class, which talks to the server
  8. # and holds the main event loop
  9. class Irc
  10.     def initialize(server, port, nick, channel)
  11.         @server = server
  12.         @port = port
  13.         @nick = nick
  14.         @channel = channel
  15.     end
  16.  
  17.     def send(s)
  18.         # sends a message to the irc server and print
  19.         # it to the screen
  20.         puts "--> #{s}"
  21.         @irc.send "#{s}\n", 0
  22.     end
  23.  
  24.     def connect()
  25.         # Connect to the irc server
  26.         @irc = TCPSocket.open(@server, @port)
  27.         send "User blah blah blah :blah blah"
  28.         send "NICK #{@nick}"
  29.         send "JOIN #{@channel}"
  30.     end
  31.  
  32.     def evaluate(s)
  33.         if s =~ /^[-+*\/\d\s\eE.()]*$/ then
  34.             begin
  35.                 s.untaint
  36.                 return eval(s).to_s
  37.             rescue Exception => detail
  38.                 puts detail.message()
  39.                 end
  40.             end
  41.             return "Error"
  42.         end
  43.  
  44. def handle_server_input(s)
  45.     case s.strip
  46.     when/^PING :(.+)$/i
  47.         puts "[ Server ping]"
  48.         send "PONG :#{$1}"
  49.     when /^:(.+?)!(.+?)@(.+?)\sPRIVMSG\s.+\s:[\001]PING (.+)[\001]$/i
  50.         puts "[ CTCP PING from #{$1}!#{$2}@#{$3} ]"
  51.         send "NOTICE #{$1} :\001VERSION Ruby-irc v0.042\001"
  52.         when /^:(.+?)!(.+?)@(.+?)\sPRIVMSG\s(.+)\s:EVAL (.+)$/i
  53.         puts "[ EVAL #{$5} from #{$1}!#{$2}@#{$3} ]"
  54.         send "PRIVMSG #{(($4==@nick)?$1:$4)} :#{evaluate($5)}"
  55.     else
  56.         puts s
  57.     end
  58. end
  59.  
  60.  
  61. def main_loop()
  62.     #Just keep on truckin
  63.     while true
  64.         ready = select([@irc, $stdin], nil, nil, nil)
  65.         next if !ready
  66.         for s in ready[0]
  67.             if s == $stdin then
  68.                 return if $stdin.eof
  69.                 s = $stdin.gets
  70.                 send s
  71.             elsif s == @irc then
  72.                 return if @irc.eof
  73.                 s = @irc.gets
  74.                 handle_server_input(s)
  75.             end
  76.             end
  77.         end
  78.     end
  79. end
  80.  
  81.  
  82. # Main Program If we get an exception print and keep going
  83. # WE DO NOT WANT TO DISCONNECT UNEXPECTEDLY!
  84. irc = Irc.new('irc.paraphysics.net', 6667, 'addyracerb0t1', '#ironflag')
  85. irc.connect()
  86. begin
  87.     irc.main_loop()
  88. rescue Interrupt
  89. rescue Exception => detail
  90.     puts detail.message()
  91.     print detail.backtrace.join("\n")
  92.     retry
  93. end
Advertisement
Add Comment
Please, Sign In to add comment