Project

General

Profile

Download (2.37 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 MailOrder < Order
def self.parse(user, message)
logger.debug "Parsing Mail"
order_txt = nil
shared_parameters = nil
if message.multipart?
if message.parts[0].content_type == "text/plain"
order_txt = message.parts[0].body
shared_parameters = {}
i = -1
message.parts.each do |part|
i += 1
next if i == 0

shared_parameters[i] = SharedParameter.new(part.body, part.content_type)
filename = part.header['content-disposition'].params['filename'] || part.header['content-type'].params['name']

shared_parameters[filename] = ParameterReference.new(i) if filename
end
end
else
order_txt = message.body if message.content_type == "text/plain"
shared_parameters = {}
end
return new(:error => _("Mail does not contain a proper text part for commands."), :user => user) if order_txt.nil?

command_lines = order_txt.split("\n")

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

super(user, command_lines, shared_parameters) do |word, errors, used_refs|
if word =~ /^@([a-zA-Z0-9._-]+)$/
ref = $1
d_ref, d_param = dereference_param(shared_parameters, param)
if d_ref.nil?
errors << _("Attachment '%{ref}' not found.", :ref => ref)
else
used_refs << d_ref
end
d_param
else
word
end
end
end
end
end
(5-5/7)