SHARE
TWEET

Untitled

a guest May 31st, 2017 548 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. require 'socket'
  2. require 'functionality.rb'
  3.  
  4. class IRCBot
  5.   include Functionality
  6.  
  7.   def initialize a="irc.freenode.net", p=6667, n="ruBot", c
  8.     @addr = a
  9.     @port = p
  10.     @nick = n
  11.     @chan = c
  12.     @usrs = {}
  13.     @email = "amenon94@gmail.com"
  14.     @nickpass = "testing"
  15.   end
  16.  
  17.   def connect
  18.     @sock = TCPSocket.open(@addr, @port)
  19.     send "NICK #{ @nick }"
  20.     send "USER ruBot 0 * :Ruby Bot"
  21.     send "JOIN #{ @chan }"
  22.     self.identify_nick
  23.   end
  24.  
  25.   def send s
  26.     puts "--> #{ s }"
  27.     @sock.puts s
  28.   end
  29.  
  30.   def parse_server_request req
  31.    
  32.     case req.strip
  33.     when /^PING :(.+)$/i
  34.       puts ">>> SERVER PING"
  35.       send "PONG :#{$1}"
  36.      
  37.     when /^:(.+)!.*\sPRIVMSG\s#{ @chan }\s:(.+)$/i #IRC Messages
  38.       puts "#{$1}: #{$2}"
  39.       user = $1
  40.       if /^!([a-zA-Z0-9]+):\s(.+)$/i.match($2)
  41.         puts "Received Method Call to: #{$1}, with args: #{$2}"
  42.         arguments = $2.split("!,:")
  43.         arguments.each { |arg| arg.strip! }
  44.         self.__send__($1.to_sym, user, arguments )
  45.       end
  46.      
  47.     when /^.*319\s.+\s(.+)\s:(?:.+\s)*(@|\+|~)?#{ @chan }.*$/i #IRC WHOIS Channel Section Reply
  48.       puts "#{$1}, #{$2}"
  49.       case $2
  50.       when "@"
  51.         @usrs[$1] = "o"
  52.       when "~"
  53.         @usrs[$1] = "o"
  54.       when "+"
  55.         @usrs[$1] = "v"
  56.       else
  57.         @usrs[$1] = "r"
  58.       end
  59.      
  60.     when /^.*352.*\s(.+)\s(?:H|G)(r?)\*?(@|\+|~)*d?.*$/i #IRC WHO Reply
  61.       case $3
  62.       when "@"
  63.         @usrs[$1] = "o"
  64.       when "~"
  65.         @usrs[$1] = "o"
  66.       when "+"
  67.         @usrs[$1] = "v"
  68.       else
  69.         @usrs[$1] = "r"
  70.       end
  71.       if $2 == "r" && (@usrs[$1] != "v" || @usrs[$1] != "o")
  72.         send "MODE #{ @chan } +v #{ $1 }"
  73.       end
  74.      
  75.     when /^:.*\s353\s.*\s(?:=|\*|@)\s#{ @chan }\s:(.+)$/i #IRC NAMES Reply
  76.       users = $1.split(" ")
  77.       users.each { |user|
  78.         case user[0...1]
  79.         when "@"
  80.           @usrs[(user[1..-1])] = "o"
  81.         when "~"
  82.           @usrs[(user[1..-1])] = "o"
  83.         when "+"
  84.           @usrs[(user[1..-1])] = "v"
  85.         else
  86.           @usrs[(user)] = "r"
  87.         end
  88.       }
  89.      
  90.     when /^:(.+)!.*\sJOIN.*$/i
  91.       puts ">>> USER #{ $1 } JOINED"
  92.       send "WHO #{ $1 }"
  93.       #send "MODE #{ @chan } +v #{ $1 }" #autovoice (unfiltered)
  94.      
  95.     when /^:(.+)!.*\sPART.*$/i #IRC PART Message
  96.       puts ">>> USER #{ $1 } LEFT"
  97.       @usrs.delete($1)
  98.      
  99.     when /^:(.+)!.*\sQUIT.*$/i #IRC QUIT Message
  100.       puts ">>> USER #{ $1 } QUIT"
  101.       @usrs.delete($1)
  102.      
  103.     when /^:.+!.*\sMODE\s#{ @chan }\s(\+|-)(o|v)\s(.+)$/i #IRC MODE Change
  104.       send "WHOIS #{ $3 }"
  105.      
  106.     when /^:(.+)!.*\sNICK\s:(.+)$/i #IRC NICK Change
  107.       puts ">>>NICK #{$1} --> #{$2}"
  108.       @usrs[$2] = @usrs[$1]
  109.       @usrs.delete($1)
  110.     end
  111.   end
  112.  
  113.   def enter_main_loop
  114.     while true
  115.       msg = select([@sock, $stdin], nil, nil, nil)
  116.       next unless msg
  117.      
  118.       for s in msg[0]
  119.         if s == $stdin
  120.           return if $stdin.eof?
  121.           s = $stdin.gets
  122.          
  123.           send "PRIVMSG #{ @chan } :#{ s }"
  124.         elsif s == @sock
  125.           return if @sock.eof?
  126.           s = @sock.gets
  127.          
  128.           Thread.new {parse_server_request s}.join
  129.         end
  130.       end
  131.      
  132.     end
  133.   end
  134.  
  135.   def close
  136.     send "PART #{ @chan } :ruBot Session closed"
  137.     send "QUIT"
  138.   end
  139. end
  140.  
  141. bot = IRCBot.new("irc.awesomeirc.net", 6667, "asQuirreL_", "#asqbotadmin")
  142. bot.connect
  143.  
  144. begin
  145.   bot.enter_main_loop
  146. rescue Interrupt
  147. rescue Exception
  148.   retry
  149. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top