lib/net/irc.rb in net-irc-0.0.7 vs lib/net/irc.rb in net-irc-0.0.8

- old
+ new

@@ -1,25 +1,24 @@ #!ruby require "ostruct" require "socket" -require "thread" require "logger" require "monitor" module Net; end module Net::IRC - VERSION = "0.0.7" + VERSION = "0.0.8".freeze class IRCException < StandardError; end require "net/irc/constants" require "net/irc/pattern" - autoload :Message, "net/irc/message" - autoload :Client, "net/irc/client" - autoload :Server, "net/irc/server" + autoload :Message, "net/irc/message" + autoload :Client, "net/irc/client" + autoload :Server, "net/irc/server" class Prefix < String def nick extract[0] end @@ -30,30 +29,46 @@ def host extract[2] end - # Extract prefix string to [nick, user, host] Array. + # Extract Prefix String to [nick, user, host] Array. def extract - _, *ret = *self.match(/^([^\s!]+)(?:!([^\s@]+)@(\S+))?$/) + _, *ret = *self.match(/\A([^\s!]+)(?:!([^\s@]+)@(\S+))?\z/) ret end end - # Encoding to CTCP message. Prefix and postfix \x01. - def ctcp_encoding(str) - str = str.gsub(/\\/, "\\\\\\\\").gsub(/\x01/, '\a') - str = str.gsub(/\x10/, "\x10\x10").gsub(/\x00/, "\x10\x30").gsub(/\x0d/, "\x10r").gsub(/\x0a/, "\x10n") - "\x01#{str}\x01" + # Encode to CTCP message. Prefix and postfix \x01. + def ctcp_encode(str) + "\x01#{ctcp_quote(str)}\x01" end - module_function :ctcp_encoding + #alias :ctcp_encoding :ctcp_encode + module_function :ctcp_encode #, :ctcp_encoding - # Decoding to CTCP message. Remove \x01. - def ctcp_decoding(str) - str = str.gsub(/\x01/, "") - str = str.gsub(/\x10n/, "\x0a").gsub(/\x10r/, "\x0d").gsub(/\x10\x30/, "\x00").gsub(/\x10\x10/, "\x10") - str = str.gsub(/\\a/, "\x01").gsub(/\\\\/, "\\") - str + # Decode from CTCP message delimited with \x01. + def ctcp_decode(str) + ctcp_dequote(str.delete("\x01")) end - module_function :ctcp_decoding + #alias :ctcp_decoding :ctcp_decode + module_function :ctcp_decode #, :ctcp_decoding + + def ctcp_quote(str) + low_quote(str.gsub("\\", "\\\\\\\\").gsub("\x01", "\\a")) + end + module_function :ctcp_quote + + def ctcp_dequote(str) + low_dequote(str).gsub("\\a", "\x01").gsub(/\\(.|\z)/m, "\\1") + end + module_function :ctcp_dequote + + private + def low_quote(str) + str.gsub("\x10", "\x10\x10").gsub("\x00", "\x10\x30").gsub("\r", "\x10r").gsub("\n", "\x10n") + end + + def low_dequote(str) + str.gsub("\x10n", "\n").gsub("\x10r", "\r").gsub("\x10\x30", "\x00").gsub("\x10\x10", "\x10") + end end