root/lib/cyborghood-postman/mail_order.rb @ 685a6991
e7315259 | Marc Dequènes (Duck) | #--
|
|
# CyborgHood, a distributed system management software.
|
|||
364e4a96 | Marc Dequènes (Duck) | # Copyright (c) 2009-2011 Marc Dequènes (Duck) <Duck@DuckCorp.org>
|
|
e7315259 | Marc Dequènes (Duck) | #
|
|
# 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
|
|||
3d7bbe55 | Marc Dequènes (Duck) | class MailOrderParser < OrderParser
|
|
def parse(message)
|
|||
cb0d68fd | Marc Dequènes (Duck) | logger.debug "Parsing Mail"
|
|
3d7bbe55 | Marc Dequènes (Duck) | ||
# analyse mail parts
|
|||
cb0d68fd | Marc Dequènes (Duck) | order_txt = nil
|
|
if message.multipart?
|
|||
if message.parts[0].content_type == "text/plain"
|
|||
18970c0e | Marc Dequènes (Duck) | order_txt = message.parts[0].body_normalized
|
|
c671241f | Marc Dequènes (Duck) | i = -1
|
|
message.parts.each do |part|
|
|||
cb0d68fd | Marc Dequènes (Duck) | i += 1
|
|
3d7bbe55 | Marc Dequènes (Duck) | next if i == 0
|
|
c671241f | Marc Dequènes (Duck) | ||
18970c0e | Marc Dequènes (Duck) | @shared_parameters[i] = CommandParameter.new(part.body_normalized, part.content_type)
|
|
c671241f | Marc Dequènes (Duck) | ||
3d7bbe55 | Marc Dequènes (Duck) | filename = part.header['content-disposition'].params['filename'] || part.header['content-type'].params['name']
|
|
@shared_parameters[filename] = CommandParameterRef.new(i) if filename
|
|||
cb0d68fd | Marc Dequènes (Duck) | end
|
|
end
|
|||
else
|
|||
18970c0e | Marc Dequènes (Duck) | order_txt = message.body_normalized if message.content_type == "text/plain"
|
|
cb0d68fd | Marc Dequènes (Duck) | end
|
|
3d7bbe55 | Marc Dequènes (Duck) | return Order.new(:error => _("Mail does not contain a proper text part for commands."), :user => @user) if order_txt.nil?
|
|
cb0d68fd | Marc Dequènes (Duck) | ||
3d7bbe55 | Marc Dequènes (Duck) | # find command lines
|
|
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
|
|||
3d7bbe55 | Marc Dequènes (Duck) | # generic parsing
|
|
super(command_lines) do |word, errors|
|
|||
cb0d68fd | Marc Dequènes (Duck) | if word =~ /^@([a-zA-Z0-9._-]+)$/
|
|
ref = $1
|
|||
3d7bbe55 | Marc Dequènes (Duck) | ref = ref.is_numeric? ? ref.to_i : ref
|
|
d_ref, d_param = deref_param(ref)
|
|||
cb0d68fd | Marc Dequènes (Duck) | if d_ref.nil?
|
|
eb6e0359 | Marc Dequènes (Duck) | errors << _("Attachment '%{ref}' not found.", :ref => ref)
|
|
3d7bbe55 | Marc Dequènes (Duck) | d_param = nil
|
|
cb0d68fd | Marc Dequènes (Duck) | else
|
|
3d7bbe55 | Marc Dequènes (Duck) | @used_refs << d_ref
|
|
cb0d68fd | Marc Dequènes (Duck) | end
|
|
d_param
|
|||
else
|
|||
3d7bbe55 | Marc Dequènes (Duck) | CommandParameter.new(word, 'text/plain')
|
|
cb0d68fd | Marc Dequènes (Duck) | end
|
|
end
|
|||
end
|
|||
end
|
|||
end
|