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 - IrcSocket.rb

IrcSocket.rb

Caricato da: Piero Tofy
Scarica il programma completo

  1. # IRC Client library
  2. # This class enbraces the TCP Socket class and adds IRC specific methods
  3.  
  4. require 'socket'
  5.  
  6. module PTriviaBot
  7. class IrcSocket < TCPSocket
  8.   def initialize(server, port)
  9.     super(server, port)
  10.  
  11.     @_formatCodes = {
  12.       "off" => "\x0f",
  13.       "bold" => "\x02",
  14.       "color" => "\x03",
  15.       "reverse" => "\x16",
  16.       "underline" => "\x1f"
  17.     }
  18.  
  19.     @_colorCodes = { # Not defined by the IRC standard but supported by most IRC Clients
  20.       "white" => "00",
  21.       "black" => "01",
  22.       "darkblue" => "02",
  23.       "darkgreen" => "03",
  24.       "red" => "04",
  25.       "darkred" => "05",
  26.       "darkpurple" => "06",
  27.       "orange" => "07",
  28.       "yellow" => "08",
  29.       "green" => "09",
  30.       "darkcyan" => "10",
  31.       "cyan" => "11",
  32.       "blue" => "12",
  33.       "purple" => "13",
  34.       "darkgray" => "14",
  35.       "gray" => "15"
  36.     }
  37.   end
  38.  
  39.   def authenticate(username)
  40.     self.puts "NICK #{username}"
  41.     self.puts "USER #{username} 0 * #{username}"
  42.  
  43.   end
  44.  
  45.   def joinchannel(channel)
  46.     self.puts "JOIN #{channel}"
  47.   end
  48.  
  49.   def sendmessage(receiver,message)
  50.     message = formatmessage(message)
  51.     self.puts "PRIVMSG #{receiver} :#{message}"
  52.   end
  53.  
  54.   def retrieveuserinfo(nickname)
  55.     sendmessage("NickServ","info #{nickname}")
  56.   end
  57.  
  58.   def sendnotice(receiver, notice)
  59.     notice = formatmessage(notice)
  60.     self.puts "NOTICE #{receiver} :#{notice}"
  61.   end
  62.  
  63.   # This socket class accepts messages that are defined in HTML style
  64.   # converting <b>,</b> into bold, <u></u> underlined, etc.
  65.   def formatmessage(msg)
  66.      msg = msg.gsub /(.*)<b>(.*)<\/b>(.*)/,'\1' + @_formatCodes['bold'] + '\2' + @_formatCodes['bold'] + '\3' while msg=~/<b>/
  67.      msg = msg.gsub /(.*)<u>(.*)<\/u>(.*)/,'\1' + @_formatCodes['underline'] + '\2' + @_formatCodes['underline'] + '\3' while msg=~/<u>/
  68.  
  69.      # Detect font color tag
  70.      if msg =~/<font color/
  71.        @_colorCodes.each_pair do |color, code|
  72.          msg = msg.gsub /<font color="#{color}">/,"\x03" + code while msg=~/<font color="#{color}">/
  73.          msg = msg.gsub /<\/font>/,'' while msg=~/<\/font>/
  74.        end
  75.      end
  76.      
  77.     return msg
  78.   end
  79.  
  80.   def close
  81.     self.puts "QUIT Goodbye!"
  82.     sleep 2 # Give time for the message to reach destination
  83.     super
  84.   end
  85. end
  86. end