#!/usr/bin/env ruby require 'net/http' #====================================================================== # * IRC #---------------------------------------------------------------------- # Basic Internet Relay Chat Class #====================================================================== class IRC #------------------------------------------------------------ # Constants #------------------------------------------------------------ VERSION = 'Net::IRC v0.1' #------------------------------------------------------------ # Public Instance Variables #------------------------------------------------------------ attr_reader :network, :port, :nick, :name, :realname, :socket #------------------------------------------------------------ # Connect Alias #------------------------------------------------------------ def self.connect(*args) return self.new(*args) end #------------------------------------------------------------ # Initialize #------------------------------------------------------------ def initialize(network, port = 6667, nick = 'Ruby', name = nil, realname = nil) # Set global variables @network = network @port = port @nick = nick @name = name || nick @realname = realname || nick # Connect to network on port @socket = TCPSocket.new(@network, @port) # Send nick and user data @socket.puts("NICK #{@nick}\r\n") @socket.puts("USER #{@name} 0 0 #{@realname}\r\n") # Update until socket closes Thread.new do until @socket.eof? update end end end #------------------------------------------------------------ # Join Channel #------------------------------------------------------------ def join(channel) @socket.puts("JOIN #{channel}\r\n") end #------------------------------------------------------------ # Say to Channel #------------------------------------------------------------ def say(channel, text) @socket.puts("PRIVMSG #{channel} :#{text}\r\n") end #------------------------------------------------------------ # Part from Channel #------------------------------------------------------------ def part(channel, text) @socket.puts("PART #{channel} :#{text}\r\n") end #------------------------------------------------------------ # Quit #------------------------------------------------------------ def quit(reason) @socket.puts("QUIT :#{reason}") end #------------------------------------------------------------ # Update #------------------------------------------------------------ def update # Split incoming packet data = @socket.gets.split(' ') # Choose command case data[0] # Ping when 'PING' @socket.puts("PONG #{data[1]}\r\n") # Other else case data[1] # Private Message when 'PRIVMSG' # Get user and message usr = data[0].scan(/:(.*)!/)[0][0] msg = data.slice(3..data.size - 1).join(' ') msg = msg[1, msg.length - 1] # CTCP if msg[0,1] == "\x01" # Clean up and split ctcp = msg[1, msg.length - 2].split(' ') # Choose command case ctcp[0] # Version when 'VERSION' @socket.puts("NOTICE #{usr} :\x01VERSION #{VERSION}\x01") # Action when 'ACTION' # Get action contents act = ctcp.slice(1..ctcp.size - 1).join(' ') # To channel if data[2][0,1] == '#' puts "* #{usr}/#{data[2]} #{act}" # To user else puts "* #{usr} #{act}" end end # Normal message else # To channel if data[2][0,1] == '#' puts "<#{usr}/#{data[2]}> #{msg}" # To user else puts "<#{usr}> #{msg}" end end # Other else puts data.join(' ') end end end end