root/lib/cyborghood/mail_order.rb @ 2520cf92
e7315259 | Marc Dequènes (Duck) | #--
|
|
# 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/>.
|
|||
#++
|
|||
cb0d68fd | Marc Dequènes (Duck) | 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 = {}
|
|||
c671241f | Marc Dequènes (Duck) | i = -1
|
|
message.parts.each do |part|
|
|||
cb0d68fd | Marc Dequènes (Duck) | i += 1
|
|
c671241f | Marc Dequènes (Duck) | 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
|
|||
cb0d68fd | Marc Dequènes (Duck) | end
|
|
end
|
|||
else
|
|||
order_txt = message.body if message.content_type == "text/plain"
|
|||
shared_parameters = {}
|
|||
end
|
|||
eb6e0359 | Marc Dequènes (Duck) | return new(:error => _("Mail does not contain a proper text part for commands."), :user => user) if order_txt.nil?
|
|
cb0d68fd | Marc Dequènes (Duck) | ||
command_lines = order_txt.split("\n")
|
|||
# remove message signature
|
|||
signature_pos = command_lines.index("-- ")
|
|||
command_lines.slice!(signature_pos..-1) if signature_pos
|
|||
b21668b8 | Marc Dequènes (Duck) | super(user, command_lines, shared_parameters) do |word, errors, used_refs|
|
|
cb0d68fd | Marc Dequènes (Duck) | if word =~ /^@([a-zA-Z0-9._-]+)$/
|
|
ref = $1
|
|||
50460df2 | Marc Dequènes (Duck) | param = ParameterReference.new(ref)
|
|
cb0d68fd | Marc Dequènes (Duck) | d_ref, d_param = dereference_param(shared_parameters, param)
|
|
if d_ref.nil?
|
|||
eb6e0359 | Marc Dequènes (Duck) | errors << _("Attachment '%{ref}' not found.", :ref => ref)
|
|
cb0d68fd | Marc Dequènes (Duck) | else
|
|
used_refs << d_ref
|
|||
end
|
|||
d_param
|
|||
else
|
|||
word
|
|||
end
|
|||
end
|
|||
end
|
|||
end
|
|||
end
|