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.
-
#commands ⇒ Object
readonly
Returns the value of attribute commands.
-
#users ⇒ Object
readonly
Returns the value of attribute users.
Instance Method Summary collapse
-
#initialize(client, users, channels, *command_sets) ⇒ 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
#initialize(client, users, channels, *command_sets) ⇒ Parser
Returns a new instance of Parser.
67 68 69 70 71 72 |
# File 'lib/butler/irc/parser.rb', line 67 def initialize(client, users, channels, *command_sets) @client = client @users = users @channels = channels @commands = Commands.new(*command_sets) end |
Instance Attribute Details
#channels ⇒ Object (readonly)
Returns the value of attribute channels.
64 65 66 |
# File 'lib/butler/irc/parser.rb', line 64 def channels @channels end |
#client ⇒ Object (readonly)
Returns the value of attribute client.
62 63 64 |
# File 'lib/butler/irc/parser.rb', line 62 def client @client end |
#commands ⇒ Object (readonly)
Returns the value of attribute commands.
65 66 67 |
# File 'lib/butler/irc/parser.rb', line 65 def commands @commands end |
#users ⇒ Object (readonly)
Returns the value of attribute users.
63 64 65 |
# File 'lib/butler/irc/parser.rb', line 63 def users @users end |
Instance Method Details
#leave_channel(message, reason1, reason2) ⇒ Object
118 119 120 121 122 123 124 125 126 127 |
# File 'lib/butler/irc/parser.rb', line 118 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.
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 112 113 114 115 116 |
# File 'lib/butler/irc/parser.rb', line 77 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 |