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) ⇒ Parser

Returns a new instance of Parser.



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

def initialize(client, users, channels)
	@client      = client
	@users       = users
	@channels    = channels
	@commands    = Commands.new
end

Instance Attribute Details

#channelsObject (readonly)

Returns the value of attribute channels.



60
61
62
# File 'lib/butler/irc/parser.rb', line 60

def channels
  @channels
end

#clientObject (readonly)

Returns the value of attribute client.



58
59
60
# File 'lib/butler/irc/parser.rb', line 58

def client
  @client
end

#usersObject (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(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.



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 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