Class: Butler::IRC::Parser
- Inherits:
-
Object
- Object
- Butler::IRC::Parser
- Defined in:
- lib/butler/irc/parser.rb,
lib/butler/irc/parser/commands.rb
Overview
Parses messages, automatically converts provides a parser that automatically connects users and channels regarding who myself is (out_of_sight, back_in_sight for users) allows creation of dialogs from privmsg and notice messages
Defined Under Namespace
Modules: Expressions, ParseError Classes: Command, Commands, InvalidMessageFormat, UnknownCommand
Constant Summary collapse
- RMessage =
/\A(?::([^ \0]+) )?([A-Za-z\d]+|\d{3})(?: (.*))?\z/
- RHostmask =
/(#{Expressions::Nick})(?:(?:!(#{Expressions::User}))?@(#{Expressions::Host}))?/
Instance Attribute Summary collapse
-
#channels ⇒ Object
readonly
Returns the value of attribute channels.
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#users ⇒ Object
readonly
Returns the value of attribute users.
Instance Method Summary collapse
-
#initialize(client, users, channels) ⇒ Parser
constructor
A new instance of Parser.
- #leave_channel(message, reason1, reason2) ⇒ Object
-
#server_message(raw) ⇒ Object
parses an incomming message and returns a Message object from which you can easily access parsed data.
Constructor Details
Instance Attribute Details
#channels ⇒ Object (readonly)
Returns the value of attribute channels.
60 61 62 |
# File 'lib/butler/irc/parser.rb', line 60 def channels @channels end |
#client ⇒ Object (readonly)
Returns the value of attribute client.
58 59 60 |
# File 'lib/butler/irc/parser.rb', line 58 def client @client end |
#users ⇒ Object (readonly)
Returns the value of attribute users.
59 60 61 |
# File 'lib/butler/irc/parser.rb', line 59 def users @users end |
Instance Method Details
#leave_channel(message, reason1, reason2) ⇒ Object
113 114 115 116 117 118 119 120 121 122 |
# File 'lib/butler/irc/parser.rb', line 113 def leave_channel(, reason1, reason2) if .from && .channel then .from.delete_channel(.channel, reason1) .channel.delete_user(.from, reason1) if .from == @users.myself then @channels.delete(.channel) @users.delete_channel(.channel, reason2) end end end |
#server_message(raw) ⇒ Object
parses an incomming message and returns a Message object from which you can easily access parsed data. Expects the newlines to be already chomped off.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/butler/irc/parser.rb', line 72 def (raw) prefix, command, params, symbol, from = nil # Basic analysis of the message raise InvalidMessageFormat, raw unless matched = raw.match(RMessage) prefix, command, params = *matched.captures command.downcase! # Parse prefix if possible (<nick>!<user>@<host>) from = @users.create(*matched.captures) if prefix and matched = prefix.match(RHostmask) # in depth analyzis of the message parser = @commands[command] symbol = parser.symbol = Message.new(@client, symbol, raw, prefix, command, params) .alter_member(:from, from) parser.create_fields() if .for then if .for.valid_channelname? then channel = @channels.create(.for) .alter_member(:channel, channel) .alter_member(:for, channel) else .alter_member(:for, @users.create(.for)) end end .alter_member(:channel, @channels.create(.channel)) if .channel parser.process(, self) return rescue IndexError raise UnknownCommand, "Unknown command #{command}: #{raw.inspect}" rescue => e e.extend ParseError e.prepend "Message: #{raw.inspect}" raise e end |