module IRCParser::RFCWireFormat
Internal: Implements objectification and stringification for the RFC wire format.
Constants
- MATCH_PREFIX
Internal: A regular expression which matches a n!u@h mask.
- MATCH_TAG
Internal: A regular expression which matches a tag.
- TAG_ESCAPES
Internal: The characters which need to be escaped in tag values.
Public Class Methods
Internal: Objectifies the prefix from the RFC wire format to an IRCParser::Prefix
.
token - A String containing the prefix in the RFC wire format.
# File lib/ircparser/wire/rfc.rb, line 156 def self.__objectify_prefix prefix unless MATCH_PREFIX =~ prefix raise IRCParser::Error.new(prefix), 'prefix is not a user mask or server name' end return IRCParser::Prefix.new nick: $~[:nick], user: $~[:user], host: $~[:host] end
Internal: Stringifies parameters from an Array to the RFC wire format.
parameters - An Array to stringify to the RFC wire format.
# File lib/ircparser/wire/rfc.rb, line 195 def self.__stringify_parameters parameters buffer = String.new parameters.each_with_index do |parameter, index| trailing = parameter.include? ' ' if trailing && index != parameters.size-1 raise IRCParser::Error.new(parameter), 'only the last parameter may contain spaces' end buffer += ' ' if trailing || parameter.empty? buffer += ':' buffer += parameter break end buffer += parameter end return buffer end
Internal: Stringifies the prefix from an IRCParser::Prefix
to the RFC wire format.
tags - An IRCParser::Prefix
to stringify to the RFC wire format.
# File lib/ircparser/wire/rfc.rb, line 217 def self.__stringify_prefix prefix buffer = prefix.nick buffer += "!#{prefix.user}" unless prefix.user.nil? buffer += "@#{prefix.host}" unless prefix.host.nil? return buffer end
Internal: Objectifies a message from the RFC wire format to an IRCParser::Message
.
str - A String containing a message in the RFC wire format.
# File lib/ircparser/wire/rfc.rb, line 60 def self.objectify str # Ruby really needs some kind of basic type checking. unless str.is_a? String raise IRCParser::Error.new(str), 'message is not a String' end # Split the message up into an array of tokens. tokenizer = MessageTokenizer.new str current_token = tokenizer.read_middle components = Hash.new # Have we encountered IRCv3 message tags? components[:tags] = Hash.new if !current_token.nil? && current_token[0] == '@' components[:tags] = self.__objectify_tags current_token current_token = tokenizer.read_middle end # Have we encountered the prefix of this message? if !current_token.nil? && current_token[0] == ':' components[:prefix] = self.__objectify_prefix current_token current_token = tokenizer.read_middle end # The command parameter is mandatory. unless current_token.nil? components[:command] = current_token.upcase current_token = tokenizer.read_trailing else raise IRCParser::Error.new(str), 'message is missing the command name' end # Try to parse all of the remaining parameters. components[:parameters] = Array.new while !current_token.nil? components[:parameters] << current_token current_token = tokenizer.read_trailing end return IRCParser::Message.new **components end
Internal: Stringifies a message from an IRCParser::Message
to the RFC wire format.
obj - An IRCParser::Message
to stringify to the RFC wire format.
# File lib/ircparser/wire/rfc.rb, line 106 def self.stringify obj # Ruby really needs some kind of basic type checking. unless obj.is_a? IRCParser::Message raise IRCParser::Error.new(obj), 'message is not an IRCParser::Message' end # Stringify the tags. buffer = String.new unless obj.tags.empty? buffer += '@' buffer += self.__stringify_tags obj.tags buffer += ' ' end # Stringify the prefix. unless obj.prefix.nil? buffer += ':' buffer += self.__stringify_prefix obj.prefix buffer += ' ' end # Stringify the command. buffer += obj.command # Stringify the parameters buffer += self.__stringify_parameters obj.parameters # We're done! return buffer end