lib/net/irc.rb in net-irc-0.0.2 vs lib/net/irc.rb in net-irc-0.0.3

- old
+ new

@@ -7,11 +7,11 @@ require "monitor" module Net; end module Net::IRC - VERSION = "0.0.2" + VERSION = "0.0.3" class IRCException < StandardError; end module PATTERN # :nodoc: # letter = %x41-5A / %x61-7A ; A-Z / a-z # digit = %x30-39 ; 0-9 @@ -127,12 +127,12 @@ RPL_MOTD = '372' RPL_ENDOFMOTD = '376' RPL_YOUREOPER = '381' RPL_REHASHING = '382' RPL_YOURESERVICE = '383' - RPL_TIM = '391' - RPL_ = '392' + RPL_TIME = '391' + RPL_USERSSTART = '392' RPL_USERS = '393' RPL_ENDOFUSERS = '394' RPL_NOUSERS = '395' RPL_TRACELINK = '200' RPL_TRACECONNECTING = '201' @@ -199,11 +199,11 @@ ERR_ALREADYREGISTRED = '462' ERR_NOPERMFORHOST = '463' ERR_PASSWDMISMATCH = '464' ERR_YOUREBANNEDCREEP = '465' ERR_YOUWILLBEBANNED = '466' - ERR_KEYSE = '467' + ERR_KEYSET = '467' ERR_CHANNELISFULL = '471' ERR_UNKNOWNMODE = '472' ERR_INVITEONLYCHAN = '473' ERR_BANNEDFROMCHAN = '474' ERR_BADCHANNELKEY = '475' @@ -289,12 +289,11 @@ USERHOST = 'USERHOST' ISON = 'ISON' end COMMANDS = Constants.constants.inject({}) {|r,i| # :nodoc: - r[Constants.const_get(i)] = i - r + r.update(Constants.const_get(i) => i) } class Prefix < String def nick extract[0] @@ -455,13 +454,13 @@ # Connect to server and start loop. def start @socket = TCPSocket.open(@host, @port) on_connected - post PASS, @opts.pass if @opts.pass - post NICK, @opts.nick - post USER, @opts.user, "0", "*", @opts.real + post PASS, @opts.pass if @opts.pass + post NICK, @opts.nick + post USER, @opts.user, "0", "*", @opts.real while l = @socket.gets begin @log.debug "RECEIVE: #{l.chomp}" m = Message.parse(l) next if on_message(m) === true @@ -669,12 +668,13 @@ # # include Net::IRC::Constans # post PRIVMSG, "#channel", "foobar" def post(command, *params) m = Message.new(nil, command, params.map {|s| - s.gsub(/[\r\n]/, " ") + s ? s.gsub(/[\r\n]/, " ") : "" }) + @log.debug "SEND: #{m.to_s.chomp}" @socket << m end end # Client @@ -767,17 +767,15 @@ while l = @socket.gets begin @log.debug "RECEIVE: #{l.chomp}" m = Message.parse(l) next if on_message(m) === true - if m.command == QUIT - on_quit if respond_to?(:on_quit) - break - else - name = "on_#{(COMMANDS[m.command.upcase] || m.command).downcase}" - send(name, m) if respond_to?(name) - end + + name = "on_#{(COMMANDS[m.command.upcase] || m.command).downcase}" + send(name, m) if respond_to?(name) + + break if m.command == QUIT rescue Message::InvalidMessage @log.error "MessageParse: " + l.inspect end end rescue IOError @@ -804,18 +802,17 @@ # Set @nick. def on_nick(m) @nick = m.params[0] end - # Default USER callback. - # Set @user, @real, @host and call inital_message. + # Set @user, @real, @host and call initial_message. def on_user(m) @user, @real = m.params[0], m.params[3] @host = @socket.peeraddr[2] @prefix = Prefix.new("#{@nick}!#{@user}@#{@host}") - inital_message + initial_message end # Call when socket connected. def on_connected end @@ -827,10 +824,15 @@ # Catch all messages. # If this method return true, aother callback will not be called. def on_message(m) end + # Default PING callback. Response PONG. + def on_ping(m) + post server_name, PONG, m.params[0] + end + # Do nothing. # This is for avoiding error on calling super. # So you can always call super at subclass. def method_missing(name, *args) end @@ -850,10 +852,10 @@ finish end # Call when client connected. # Send RPL_WELCOME sequence. If you want to customize, override this method at subclass. - def inital_message + def initial_message post server_name, RPL_WELCOME, @nick, "Welcome to the Internet Relay Network #{@prefix}" post server_name, RPL_YOURHOST, @nick, "Your host is #{server_name}, running version #{server_version}" post server_name, RPL_CREATED, @nick, "This server was created #{Time.now}" post server_name, RPL_MYINFO, @nick, "#{server_name} #{server_version} #{avaiable_user_modes} #{avaiable_channel_modes}" end