Advertisement
Guest User

Untitled

a guest
Jan 6th, 2012
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 6.39 KB | None | 0 0
  1.     #!/usr/local/bin/ruby
  2.      
  3.     require "socket"
  4.      
  5.     # Don't allow use of "tainted" data by potentially dangerous operations
  6.     $SAFE=1
  7.      
  8.     # The irc class, which talks to the server 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.         def send(s)
  17.             # Send a message to the irc server and print it to the screen
  18.             puts "--> #{s}"
  19.             @irc.send "#{s}\n", 0
  20.         end
  21.         def connect()
  22.             # Connect to the IRC server
  23.             @irc = TCPSocket.open(@server, @port)
  24.             send "USER rubybot didabot master :win loose"
  25.             send "NICK #{@nick}"
  26.             send "JOIN #{@channel}"
  27.         end
  28.         def evaluate(s)
  29.             # Make sure we have a valid expression (for security reasons), and
  30.             # evaluate it if we do, otherwise return an error message
  31.             if s =~ /^[-+*\/\d\s\eE.()]*$/ then
  32.                 begin
  33.                     s.untaint
  34.                     return eval(s).to_s
  35.                 rescue Exception => detail
  36.                     puts detail.message()
  37.                 end
  38.             end
  39.             return "Error"
  40.         end
  41.         def handle_server_input(s)
  42.             # This isn't at all efficient, but it shows what we can do with Ruby
  43.             case s.strip
  44.                 when /^PING :(.+)$/i
  45.                     puts "[ Server ping ]"
  46.                     send "PONG :#{$1}"
  47.                 when /^:(.+?)!(.+?)@(.+?)\sPRIVMSG\s.+\s:[\001]PING (.+)[\001]$/i
  48.                     puts "[ CTCP PING from #{$1}!#{$2}@#{$3} ]"
  49.                     send "NOTICE #{$1} :\001PING #{$4}\001"
  50.                 when /^:(.+?)!(.+?)@(.+?)\sPRIVMSG\s.+\s:[\001]VERSION[\001]$/i
  51.                     puts "[ CTCP VERSION from #{$1}!#{$2}@#{$3} ]"
  52.                     send "NOTICE #{$1} :\001VERSION DidaBot - version 0.1a\001"
  53.                 when /^:(.+?)!(.+?)@(.+?)\sPRIVMSG\s(.+)\s:EVAL (.+)$/i
  54.                     puts "[ EVAL #{$5} from #{$1}!#{$2}@#{$3} ]"
  55.                     send "PRIVMSG #{(($4==@nick)?$1:$4)} :#{evaluate($5)}"
  56.                 # when /^:([^!]+)![^@]+@[^ ]+ PRIVMSG #{@channel} :(.+)$/
  57.                 # when /^:([^!]+)![^@]+@[^ ]+ PRIVMSG #{@channel} :(!about)$/
  58.                 # when /^:dida![^@]+@[^ ]+ PRIVMSG #{@channel} :(.+)$/
  59.                 when /^:dida![^@]+@[^ ]+ PRIVMSG #{@channel} :(.+)$/
  60.                     @rawr = $1.split(/ * /)
  61.                     case @rawr[0]
  62.                             when "!kick"
  63.                                     puts "Admin command: #{@rawr[0]}"
  64.                                     send "KICK #{@channel} #{@rawr[1]} :#{@rawr.drop(1).join(" ")}"
  65.                             when "!ban"
  66.                                     puts "Admin command: #{@rawr[0]}"
  67.                                     send "MODE #{@channel} +b #{@rawr[1]}"
  68.                                     send "KICK #{@channel} #{@rawr[1]} :#{@rawr.drop(1).join(" ")}"
  69.                             when "!unban"
  70.                                     puts "Admin command: #{@rawr[0]}"
  71.                                     send "MODE #{@channel} -b #{@rawr[1]}!*@*"
  72.                             when "!op"
  73.                                     puts "Admin command: #{@rawr[0]}"
  74.                                     send "MODE #{@channel} +o #{@rawr[1]}"
  75.                             when "!voice"
  76.                                     puts "Admin command: #{@rawr[0]}"
  77.                                     send "MODE #{@channel} +v #{@rawr[1]}"
  78.                             when "!halfop"
  79.                                     puts "Admin command: #{@rawr[0]}"
  80.                                     send "MODE #{@channel} +h #{@rawr[1]}"
  81.                             when "!deop"
  82.                                     puts "Admin command: #{@rawr[0]}"
  83.                                     send "MODE #{@channel} -o #{@rawr[1]}"
  84.                             when "!devoice"
  85.                                     puts "Admin command: #{@rawr[0]}"
  86.                                     send "MODE #{@channel} -v #{@rawr[1]}"
  87.                             when "!dehalfop"
  88.                                     puts "Admin command: #{@rawr[0]}"
  89.                                     send "MODE #{@channel} -h #{@rawr[1]}"
  90.                             when "!say"
  91.                                     puts "User command: #{@rawr[0]}"
  92.                                     send "PRIVMSG #{@channel} :#{@rawr.drop(1).join(" ")}"
  93.                             else
  94.                                     puts @rawr[0]
  95.                                     puts s
  96.                     end
  97.                 when /^:([^!]+)![^@]+@[^ ]+ PRIVMSG #{@channel} :(!.+)$/
  98.                     @dat = $1.split(/ * /)
  99.                     case @dat[0]
  100.                             when "!say"
  101.                                     puts "User command: #{@dat[0]}"
  102.                                     send "PRIVMSG #{@channel} :#{@dat.drop(1).join(" ")}"
  103.                             else
  104.                                     puts @rawr[0]
  105.                                     puts s
  106.                     end
  107.                 else
  108.                     puts s
  109.             end
  110.         end
  111.         def main_loop()
  112.             # Just keep on truckin' until we disconnect
  113.             while true
  114.                 ready = select([@irc, $stdin], nil, nil, nil)
  115.                 next if !ready
  116.                 for s in ready[0]
  117.                     if s == $stdin then
  118.                         return if $stdin.eof
  119.                         s = $stdin.gets
  120.                         send s
  121.                     elsif s == @irc then
  122.                         return if @irc.eof
  123.                         s = @irc.gets
  124.                         handle_server_input(s)
  125.                     end
  126.                 end
  127.             end
  128.         end
  129.     end
  130.      
  131.     # The main program
  132.     # If we get an exception, then print it out and keep going (we do NOT want
  133.     # to disconnect unexpectedly!)
  134.     irc = IRC.new('irc.freenode.net', 6667, 'didabot', '#webdevrefinery')
  135.     irc.connect()
  136.     begin
  137.         irc.main_loop()
  138.         irc.send_message()
  139.     rescue Interrupt
  140.     rescue Exception => detail
  141.         puts detail.message()
  142.         print detail.backtrace.join("\n")
  143.         retry
  144.     end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement