Project

General

Profile

« Previous | Next » 

Revision cb0d68fd

Added by Marc Dequènes over 15 years ago

  • ID cb0d68fd2c857faea5a05f374b2bb4464a1191fd

[evol] commands management rework #2: moved command parsing out of the Mail class and created new classes to handle it

View differences:

bin/postman
require 'shellwords'
require 'cyborghood/imap'
require 'cyborghood/mail'
require 'cyborghood/mail_order'
require 'cyborghood/objects'
require 'cyborghood/services/dns'
require 'fileutils'
#Socket.gethostname
......
logger.debug "RFC3156 content detected"
begin
order = mail.parse
report = mail.process
rescue CyberError => e
case e.severity
when :dangerous
logger.fatal " (#{e.message})"
logger.fatal "Fatal processing error, exiting (#{e.message})"
exit 2
when :unrecoverable
logger.error "Internal processing error, skipping mail (#{e.message})"
......
when :ignorable
end
end
result_tag = order.ok ? "SUCCESS" : "FAILURE"
logger.info "Processing result: #{result_tag} (#{order.message})"
logger.info "Extra processing information: " + order.system_message if order.system_message
if order.user
if order.user.preferredLanguage
logger.debug "User preference for langage: " + order.user.preferredLanguage
set_locale(order.user.preferredLanguage)
result_tag = report.ok? ? "SUCCESS" : "FAILURE"
logger.info "Processing result: #{result_tag} (#{report.error})"
if report.user
if report.user.preferredLanguage
logger.debug "User preference for langage: " + report.user.preferredLanguage
set_locale(report.user.preferredLanguage)
else
logger.debug "No user preference for langage, using english"
set_locale("en")
......
set_locale("en")
end
unless order.ok
if order.warn_sender
unless report.ok?
if report.warn_sender
logger.info "Sending reply for rejected message"
mail_reply = mail.create_simple_reject_reply(_("Hello,") + "\n\n" + sprintf(
intro = report.user ? sprintf(_("Hello %s,"), report.user.cn) : _("Hello,")
mail_reply = mail.create_simple_reject_reply(intro + "\n\n" + sprintf(
_("A message (ID: %s), apparently from you, was rejected for the following reason:"),
mail.message_id) + "\n " + _(order.message) + "\n" + mail_signature())
mail.message_id) + "\n " + _(report.error) + "\n" + mail_signature())
mail_reply.deliver
end
msg.delete
next
end
order = MailOrder.parse(report.user, report.message)
result_tag = order.valid? ? "SUCCESS" : "FAILURE"
logger.info "Processing result: #{result_tag} (#{order.error})"
unless order.valid?
logger.info "Sending reply for rejected order"
mail_reply = mail.create_simple_reject_reply(sprintf(_("Hello %s,"), order.user.cn) + "\n\n" +
sprintf(_("An order, in a message (ID: %s) from you, was rejected for the following reason:"),
mail.message_id) + "\n " + _(order.error) + "\n" + mail_signature())
mail_reply.deliver
msg.delete
next
end
logger.debug "Message accepted, processing orders..."
result_list = CommandRunner.run(order)
# create transcript
logger.debug "Preparing reply"
reply_txt = sprintf(_("Hello %s,"), order.user.cn) + "\n\n"
reply_txt += _(order.message) + "\n\n" if order.message
reply_txt += _("Follows the transcript of your commands:") + "\n"
reply_attachments = []
result_list.each do |result|
......
def self.run(order)
result_list = []
order.commands.each do |cmd|
logger.info "Executing command: #{cmd.cmdline}"
begin
result = execute_cmd(order.user, cmd.cmdsplit, order.shared_params)
if result.nil?
if cmd.valid?
logger.info "Executing command: #{cmd.cmdline}"
begin
result = execute_cmd(order.user, cmd.cmdsplit, order.shared_parameters)
if result.nil?
result = OpenStruct.new
result.cmd = cmd.cmdline
result.ok = false
result.message = _("Command not recognized.")
result.refs = nil
end
rescue CyberError => e
result = OpenStruct.new
result.cmd = cmd.cmdline
result.ok = false
result.message = e.message.capitalize + "."
result.refs = nil
rescue
logger.warn "Command crashed: " + $!
result = OpenStruct.new
result.cmd = cmd.cmdline
result.ok = false
result.message = _("Command not recognized.")
result.message = _("Internal error. Administrator is warned.")
result.refs = nil
end
rescue CyberError => e
result = OpenStruct.new
result.cmd = cmd.cmdline
result.ok = false
result.message = e.message.capitalize + "."
result.refs = nil
rescue
logger.warn "Command crashed: " + $!
tag = result.ok ? "SUCCESS" :"FAILURE"
logger.debug "Command result: [#{tag}] #{result.message}"
else
logger.info "Detected invalid command: #{cmd.cmdline}"
result = OpenStruct.new
result.cmd = cmd.cmdline
result.ok = false
result.message = _("Internal error. Administrator is warned.")
result.message = cmd.parsing_errors.collect{|err| _(err) }.join("\n")
result.refs = nil
end
tag = result.ok ? "SUCCESS" :"FAILURE"
logger.debug "Command result: [#{tag}] #{result.message}"
result_list << result
end
result_list
......
private
def self.execute_cmd(user, cmdline, shared_params)
def self.execute_cmd(user, cmdline, shared_parameters)
subsys = cmdline.shift
result = OpenStruct.new
......
return if cmdline.empty?
content_ref = cmdline.shift
if content_ref.nil?
result.message = _("Attachment number not found.")
return result
end
part = shared_params[content_ref]
part = shared_parameters[content_ref]
unless part.type == "text/plain"
result.message = _("Attachment has wrong content-type.")
return result

Also available in: Unified diff