Revision 5f013d0a
Added by Marc Dequènes over 15 years ago
- ID 5f013d0abba04f65924a85d974bd7dd3cc20a6b8
lib/cyborghood/base.rb | ||
---|---|---|
VERSION = "0.2.0~dev"
|
||
|
||
bindtextdomain(PRODUCT, {:path => File.join(APP_ROOT, "locale"), :charset => "UTF-8"})
|
||
bindtextdomain(PRODUCT + "_" + self.human_name, {:path => File.join(APP_ROOT, "locale"), :charset => "UTF-8"OB})
|
||
textdomain(PRODUCT + "_" + self.human_name)
|
||
textdomain(PRODUCT)
|
||
#textdomain_to(self.class, PRODUCT + "_" + self.human_name)
|
||
|
||
# severities: # :dangerous :unrecoverable :ignorable
|
||
# categories:
|
postman | ||
---|---|---|
|
||
#Socket.gethostname
|
||
|
||
|
||
module CyborgHood
|
||
# not yet ready to be a real Cyborg
|
||
class Postman #< Cyborg
|
||
include GetText
|
||
bindtextdomain(PRODUCT + "_" + self.human_name, {:path => File.join(APP_ROOT, "locale"), :charset => "UTF-8"})
|
||
textdomain(PRODUCT + "_" + self.human_name)
|
||
|
||
def initialize
|
||
# load config
|
||
Config.load(self.human_name.downcase)
|
||
@config = Config.instance
|
||
|
||
ldap_config = @config.ldap
|
||
ldap_config.logger = logger
|
||
ActiveLdap::Base.establish_connection(ldap_config.marshal_dump)
|
||
|
||
# setup logs
|
||
unless @config.log.nil?
|
||
logger.output_level(@config.log.console_level) unless @config.log.console_level.nil?
|
||
logger.log_to_file(@config.log.file) unless @config.log.file.nil?
|
||
end
|
||
|
||
@current_thread = Thread.current
|
||
@stop_asap = false
|
||
@waiting = false
|
||
|
||
logger.info "Bot '#{self.human_name}' loaded"
|
||
end
|
||
|
||
def run
|
||
imap = IMAP.new(@config.imap)
|
||
until @stop_asap
|
||
t = Time.now.to_i
|
||
logger.debug "Starting mail check"
|
||
check_mails(imap)
|
||
logger.debug "Mail check finished"
|
||
t2 = Time.now.to_i
|
||
sleep_time = @config.imap.min_check_interval - (t2 - t)
|
||
if sleep_time > 0
|
||
logger.debug "Having a break before new check..."
|
||
@waiting = true
|
||
begin
|
||
sleep(sleep_time)
|
||
rescue
|
||
end
|
||
@waiting = false
|
||
end
|
||
end
|
||
logger.info "Bot was asked to stop..." if @stop_asap
|
||
logger.info "Bot terminating"
|
||
end
|
||
|
||
def check_mails(imap)
|
||
imap.check_mail do |msg|
|
||
if @stop_asap
|
||
logger.info "Bot was asked to stop..."
|
||
break
|
||
end
|
||
|
||
mail = Mail.new(msg.content)
|
||
logger.info "Received mail with ID '#{mail.message_id}': #{mail.from_addrs} -> #{mail.to_addrs} (#{mail.subject})"
|
||
|
||
# ignore mails not signed or encrypted
|
||
unless mail.is_pgp_signed? or mail.is_pgp_encrypted?
|
||
logger.info "Mail not signed/encrypted or not RFC3156 compliant, ignoring..."
|
||
msg.delete
|
||
next
|
||
end
|
||
|
||
logger.debug "RFC3156 content detected"
|
||
begin
|
||
order = mail.parse
|
||
rescue CyberError => e
|
||
case e.severity
|
||
when :dangerous
|
||
logger.fatal " (#{e.message})"
|
||
exit 2
|
||
when :unrecoverable
|
||
logger.error "Internal processing error, skipping mail (#{e.message})"
|
||
next
|
||
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)
|
||
else
|
||
logger.debug "No user preference for langage, using english"
|
||
set_locale("en")
|
||
end
|
||
else
|
||
set_locale("en")
|
||
end
|
||
|
||
unless order.ok
|
||
if order.warn_sender
|
||
logger.info "Sending reply for rejected message"
|
||
mail_reply = mail.create_simple_reject_reply("A message (ID: #{mail.message_id}), apparently from you," +
|
||
" was rejected for the following reason:\n #{_(order.message)}")
|
||
mail_reply.deliver
|
||
end
|
||
msg.delete
|
||
next
|
||
end
|
||
|
||
logger.debug "Message accepted, processing orders..."
|
||
result_list = CommandParser.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|
|
||
reply_txt << "> #{result.cmd}\n"
|
||
reply_txt << "#{result.message}\n"
|
||
reply_attachments += result.refs unless result.refs.nil?
|
||
end
|
||
|
||
# create mail
|
||
logger.debug "Preparing mail"
|
||
mail_reply = mail.create_reply
|
||
if reply_attachments.empty?
|
||
transcript_part = mail_reply
|
||
else
|
||
mail_reply.set_content_type("multipart", "mixed", {'boundary' => TMail.new_boundary})
|
||
parts = []
|
||
|
||
p = CyborgHood::Mail.new
|
||
transcript_part = p
|
||
mail_reply.parts << p
|
||
|
||
reply_attachments.each do |attachment|
|
||
p = CyborgHood::Mail.new
|
||
p.set_content_type("text", "plain", {'charset' => "utf-8"})
|
||
p.set_disposition("attachment", {'filename' => attachment.filename})
|
||
p.quoted_printable_body = attachment.content
|
||
mail_reply.parts << p
|
||
end
|
||
end
|
||
# insert transcript
|
||
transcript_part.set_content_type("text", "plain", {'charset' => 'utf-8', 'format' => 'flowed'})
|
||
transcript_part.set_disposition("inline")
|
||
transcript_part.quoted_printable_body = reply_txt
|
||
|
||
# send reply
|
||
logger.debug "Sending mail"
|
||
mail_reply.sign_and_crypt(order.user.keyFingerPrint)
|
||
mail_reply.deliver
|
||
|
||
logger.info "Message processed completely, deleting"
|
||
msg.delete
|
||
end
|
||
end
|
||
|
||
def ask_to_stop
|
||
@stop_asap = true
|
||
Thread.critical = true
|
||
@current_thread.raise if @waiting
|
||
Thread.critical = false
|
||
end
|
||
end
|
||
|
||
class CommandParser
|
||
include GetText
|
||
textdomain(PRODUCT + "_Postman")
|
||
|
||
def self.run(order)
|
||
result_list = []
|
||
... | ... | |
end
|
||
end
|
||
end
|
||
|
||
# not yet ready to be a real Cyborg
|
||
class Postman #< Cyborg
|
||
include GetText
|
||
|
||
def initialize
|
||
# load config
|
||
Config.load(self.human_name.downcase)
|
||
@config = Config.instance
|
||
|
||
ldap_config = @config.ldap
|
||
ldap_config.logger = logger
|
||
ActiveLdap::Base.establish_connection(ldap_config.marshal_dump)
|
||
|
||
# setup logs
|
||
unless @config.log.nil?
|
||
logger.output_level(@config.log.console_level) unless @config.log.console_level.nil?
|
||
logger.log_to_file(@config.log.file) unless @config.log.file.nil?
|
||
end
|
||
|
||
@current_thread = Thread.current
|
||
@stop_asap = false
|
||
@waiting = false
|
||
|
||
logger.info "Bot '#{self.human_name}' loaded"
|
||
end
|
||
|
||
def run
|
||
imap = IMAP.new(@config.imap)
|
||
until @stop_asap
|
||
t = Time.now.to_i
|
||
logger.debug "Starting mail check"
|
||
check_mails(imap)
|
||
logger.debug "Mail check finished"
|
||
t2 = Time.now.to_i
|
||
sleep_time = @config.imap.min_check_interval - (t2 - t)
|
||
if sleep_time > 0
|
||
logger.debug "Having a break before new check..."
|
||
@waiting = true
|
||
begin
|
||
sleep(sleep_time)
|
||
rescue
|
||
end
|
||
@waiting = false
|
||
end
|
||
end
|
||
logger.info "Bot was asked to stop..." if @stop_asap
|
||
logger.info "Bot terminating"
|
||
end
|
||
|
||
def check_mails(imap)
|
||
imap.check_mail do |msg|
|
||
if @stop_asap
|
||
logger.info "Bot was asked to stop..."
|
||
break
|
||
end
|
||
|
||
mail = Mail.new(msg.content)
|
||
logger.info "Received mail with ID '#{mail.message_id}': #{mail.from_addrs} -> #{mail.to_addrs} (#{mail.subject})"
|
||
|
||
# ignore mails not signed or encrypted
|
||
unless mail.is_pgp_signed? or mail.is_pgp_encrypted?
|
||
logger.info "Mail not signed/encrypted or not RFC3156 compliant, ignoring..."
|
||
msg.delete
|
||
next
|
||
end
|
||
|
||
logger.debug "RFC3156 content detected"
|
||
begin
|
||
order = mail.parse
|
||
rescue CyberError => e
|
||
case e.severity
|
||
when :dangerous
|
||
logger.fatal " (#{e.message})"
|
||
exit 2
|
||
when :unrecoverable
|
||
logger.error "Internal processing error, skipping mail (#{e.message})"
|
||
next
|
||
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
|
||
locale = order.user.preferredLanguage
|
||
else
|
||
logger.debug "No user preference for langage, using english"
|
||
locale = "en"
|
||
end
|
||
else
|
||
locale = "en"
|
||
end
|
||
clear_cache
|
||
|
||
unless order.ok
|
||
if order.warn_sender
|
||
logger.info "Sending reply for rejected message"
|
||
mail_reply = mail.create_simple_reject_reply("A message (ID: #{mail.message_id}), apparently from you," +
|
||
" was rejected for the following reason:\n #{_(order.message)}")
|
||
mail_reply.deliver
|
||
end
|
||
msg.delete
|
||
next
|
||
end
|
||
|
||
logger.debug "Message accepted, processing orders..."
|
||
result_list = CommandParser.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|
|
||
reply_txt << "> #{result.cmd}\n"
|
||
reply_txt << "#{result.message}\n"
|
||
reply_attachments += result.refs unless result.refs.nil?
|
||
end
|
||
|
||
# create mail
|
||
logger.debug "Preparing mail"
|
||
mail_reply = mail.create_reply
|
||
if reply_attachments.empty?
|
||
transcript_part = mail_reply
|
||
else
|
||
mail_reply.set_content_type("multipart", "mixed", {'boundary' => TMail.new_boundary})
|
||
parts = []
|
||
|
||
p = CyborgHood::Mail.new
|
||
transcript_part = p
|
||
mail_reply.parts << p
|
||
|
||
reply_attachments.each do |attachment|
|
||
p = CyborgHood::Mail.new
|
||
p.set_content_type("text", "plain", {'charset' => "utf-8"})
|
||
p.set_disposition("attachment", {'filename' => attachment.filename})
|
||
p.quoted_printable_body = attachment.content
|
||
mail_reply.parts << p
|
||
end
|
||
end
|
||
# insert transcript
|
||
transcript_part.set_content_type("text", "plain", {'charset' => 'utf-8', 'format' => 'flowed'})
|
||
transcript_part.set_disposition("inline")
|
||
transcript_part.quoted_printable_body = reply_txt
|
||
|
||
# send reply
|
||
logger.debug "Sending mail"
|
||
mail_reply.sign_and_crypt(order.user.keyFingerPrint)
|
||
mail_reply.deliver
|
||
|
||
logger.info "Message processed completely, deleting"
|
||
msg.delete
|
||
end
|
||
end
|
||
|
||
def ask_to_stop
|
||
@stop_asap = true
|
||
Thread.critical = true
|
||
@current_thread.raise if @waiting
|
||
Thread.critical = false
|
||
end
|
||
end
|
||
end
|
||
|
||
bot = CyborgHood::Postman.new
|
Also available in: Unified diff
[fix] gettext is working now, nevertheless the domain selection is ugly