Project

General

Profile

Download (2.45 KB) Statistics
| Branch: | Tag: | Revision:
#--
# CyborgHood, a distributed system management software.
# Copyright (c) 2009-2010 Marc Dequènes (Duck) <Duck@DuckCorp.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#++

require 'cyborghood/order'

module CyborgHood
class MailOrderParser < OrderParser
def parse(message)
logger.debug "Parsing Mail"

# analyse mail parts
order_txt = nil
if message.multipart?
if message.parts[0].content_type == "text/plain"
order_txt = message.parts[0].body_normalized
i = -1
message.parts.each do |part|
i += 1
next if i == 0

@shared_parameters[i] = CommandParameter.new(part.body_normalized, part.content_type)

filename = part.header['content-disposition'].params['filename'] || part.header['content-type'].params['name']
@shared_parameters[filename] = CommandParameterRef.new(i) if filename
end
end
else
order_txt = message.body_normalized if message.content_type == "text/plain"
end
return Order.new(:error => _("Mail does not contain a proper text part for commands."), :user => @user) if order_txt.nil?

# find command lines
command_lines = order_txt.split("\n")

# remove message signature
signature_pos = command_lines.index("-- ")
command_lines.slice!(signature_pos..-1) if signature_pos

# generic parsing
super(command_lines) do |word, errors|
if word =~ /^@([a-zA-Z0-9._-]+)$/
ref = $1
ref = ref.is_numeric? ? ref.to_i : ref
d_ref, d_param = deref_param(ref)
if d_ref.nil?
errors << _("Attachment '%{ref}' not found.", :ref => ref)
d_param = nil
else
@used_refs << d_ref
end
d_param
else
CommandParameter.new(word, 'text/plain')
end
end
end
end
end
(6-6/8)