Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
PTriviaBot - Bot.rb

Bot.rb

Caricato da: Piero Tofy
Scarica il programma completo

  1. # Bot class
  2. # This class implements most of the behavior of the chat bot
  3.  
  4. require './IrcSocket'
  5. require './QuizEngine'
  6. require './User'
  7. require './Language'
  8. include PLanguageSupport
  9.  
  10. module PTriviaBot
  11.  
  12. class Bot
  13.   # Constructor
  14.   def initialize(name, adminpassword)
  15.     @name = name
  16.     @channel = ""
  17.     @adminpassword = adminpassword
  18.  
  19.     # Users hash table (dynamically computed each time) {nickname => User}
  20.     @users = {}
  21.   end
  22.  
  23.   def connect(server, port, channel, username, password)
  24.     begin
  25.       @channel = channel
  26.       @username = username
  27.       @password = password
  28.  
  29.       puts t(:connecting_to,server,port)
  30.       @sock = IrcSocket.new(server, port)
  31.     rescue Exception=>e
  32.       puts e
  33.     else
  34.       # We're in!
  35.       @sock.authenticate(@username)
  36.       sleep 5
  37.       @sock.joinchannel(@channel)
  38.  
  39.       exitSignalReceived = false
  40.  
  41.       while !exitSignalReceived
  42.         incoming = @sock.readline.chomp
  43.        
  44.         puts t(:parsing,incoming)
  45.  
  46.         parseincomingmessage(incoming)
  47.  
  48.         #Notice message?
  49.         if result = /:([^!]+)![^ ]+ NOTICE #{@username} :([^$]+)$/.match(incoming)
  50.           sender = result[1]
  51.           msg = result[2]
  52.           puts t(:notice_message_from,sender,msg)
  53.  
  54.           noticereceived(sender, msg)
  55.         end
  56.        
  57.         #Private message?
  58.         if result = /:([^!]+)![^ ]+ PRIVMSG #{@username} :([^$]+)$/.match(incoming)
  59.           sender = result[1]
  60.           msg = result[2].chomp.upcase
  61.           puts t(:private_message_from, sender, msg)
  62.  
  63.           privatemessagereceived(sender, msg)
  64.         end
  65.       end
  66.  
  67.       sleep 5
  68.       @sock.close
  69.     end
  70.   end
  71.  
  72.   def parseincomingmessage(incoming)
  73.      #Need to authenticate? This user needs to be created in advance by an administrator
  74.     if incoming =~ /This nickname is registered/
  75.       @sock.sendmessage("NickServ", "IDENTIFY " + @password)
  76.       puts t(:authenticating)
  77.     end
  78.  
  79.     #Did we fail to join the channel?
  80.     if incoming =~ /451 JOIN :You have not registered/
  81.       sleep 2
  82.       @sock.joinchannel(@channel)
  83.     end
  84.  
  85.     #Ping? Pong!
  86.     if result = /PING ([^$ ]+)[ ]*[^$]*$/.match(incoming)
  87.       host = result[1]
  88.       @sock.puts("PONG " + host)
  89.       puts "PONG " + host
  90.     end
  91.  
  92.     #User disconnect
  93.     # :pierotofy!uiedrosuplfgj@131.212.77.65 QUIT :Connection closed
  94.     if result = /:([^!]+)![^ ]+ QUIT/.match(incoming)
  95.       puts t(:disconnected,result[1])
  96.       @users.delete(result[1]) if (@users.has_key?(result[1]))
  97.     end
  98.  
  99.     #User join
  100.     #:lightIRC_3305!uiedrosuplfgj@131.212.77.65 JOIN :#pierotofy.it
  101.     if result = /:([^!]+)![^ ]+ JOIN/.match(incoming)
  102.       puts t(:joined, result[1])
  103.       @users.store(result[1],User.new(result[1])) unless (@users.has_key?(result[1]))
  104.  
  105.       @sock.retrieveuserinfo(result[1])
  106.     end
  107.  
  108.     #User change nick
  109.     #:lightIRC_3305!uiedrosuplfgj@131.212.77.65 NICK pierotofy
  110.     if result = /:([^!]+)![^ ]+ NICK ([^$]+)$/.match(incoming)
  111.       puts t(:changed_nick_into, result[1],result[2])
  112.       @users.delete(result[1]) if (@users.has_key?(result[1]))
  113.       @users.store(result[2],User.new(result[2]))
  114.  
  115.       @sock.retrieveuserinfo(result[2])
  116.     end
  117.  
  118.     #Getting a list of users?
  119.     if result = /:([^ ]+) 353 #{@username} = ([^ ]+) :([^$]+)$/.match(incoming)
  120.       puts t(:parsing_list_of_users,result[3])
  121.  
  122.       result[3].split.each do |nick|
  123.         nick = nick.sub(/[@]/, '')
  124.  
  125.         @users.store(nick,User.new(nick)) unless (@users.has_key?(nick))
  126.         @sock.retrieveuserinfo(nick)
  127.  
  128.         puts t(:retrieving_user_info, nick)
  129.       end
  130.     end
  131.  
  132.     if result = /:([^!]+)![^ ]+ PRIVMSG #{@channel} :([^$]+)$/.match(incoming)
  133.       sender = result[1]
  134.       msg = result[2].chomp
  135.  
  136.       channelmessagereceived(sender, msg)
  137.     end
  138.   end
  139.  
  140.   def channelmessagereceived(sender, msg)
  141.    
  142.   end
  143.  
  144.   def privatemessagereceived(sender, msg)
  145.     if result = /AUTH ([^$]+)$/.match(msg)
  146.       if result[1] == @adminpassword.upcase
  147.         if @users.has_key?(sender)
  148.           @users[sender].is_admin = true
  149.  
  150.           @sock.sendnotice(sender, t(:you_are_now_authenticated))
  151.           puts t(:user_authenticated,sender)
  152.         end
  153.       end
  154.     end
  155.   end
  156.  
  157.   def noticereceived(sender, msg)
  158.     # Is an user logged in with the nickserv?
  159.     if result = /([^ ]+) is currently online./.match(msg)
  160.        nick = result[1]
  161.        userregistered(nick)
  162.     end
  163.   end
  164.  
  165.   def userregistered(nick)
  166.     @users[nick].registered = true if @users.has_key?(nick)
  167.  
  168.     puts t(:nick_is_registered, nick)
  169.   end
  170.  
  171.   attr_accessor :name;
  172. end
  173. end