|
#--
|
|
# 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
|
|
search_text_part(message)
|
|
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
|
|
|
|
private
|
|
|
|
def search_text_part(message)
|
|
@i ||= 0
|
|
if message.multipart?
|
|
message.parts.each {|part| search_text_part(part) }
|
|
else
|
|
if message.mime_type == "text/plain" and @order_txt.nil?
|
|
@order_txt = message.decoded
|
|
else
|
|
@i += 1
|
|
|
|
@shared_parameters[@i] = CommandParameter.new(message.decoded, message.mime_type)
|
|
|
|
filename = message.header['content-disposition'].filename || message.content_type_parameters['name']
|
|
@shared_parameters[filename] = CommandParameterRef.new(@i) if filename
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|