Class: Butler::IRC::Parser

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#channelsObject (readonly)

Returns the value of attribute channels.



64
65
66
# File 'lib/butler/irc/parser.rb', line 64

def channels
  @channels
end

#clientObject (readonly)

Returns the value of attribute client.



62
63
64
# File 'lib/butler/irc/parser.rb', line 62

def client
  @client
end

#commandsObject (readonly)

Returns the value of attribute commands.



65
66
67
# File 'lib/butler/irc/parser.rb', line 65

def commands
  @commands
end

#usersObject (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(message, reason1, reason2)
	if message.from && message.channel then
		message.from.delete_channel(message.channel, reason1)
		message.channel.delete_user(message.from, reason1)
		if message.from == @users.myself then
			@channels.delete(message.channel)
			@users.delete_channel(message.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 server_message(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	= Message.new(@client, symbol, raw, prefix, command, params)
	message.alter_member(:from, from)
	
	parser.create_fields(message)
	
	if message.for then
		if message.for.valid_channelname? then
			channel = @channels.create(message.for)
			message.alter_member(:channel, channel)
			message.alter_member(:for, channel)
		else
			message.alter_member(:for,	@users.create(message.for))
		end
	end
	message.alter_member(:channel, @channels.create(message.channel)) if message.channel
	
	parser.process(message, self)
	
	return message
rescue IndexError
		raise UnknownCommand, "Unknown command #{command}: #{raw.inspect}"
rescue => e
	e.extend ParseError
	e.prepend "Message: #{raw.inspect}"
	raise e
end