Project

General

Profile

« Previous | Next » 

Revision 34bef607

Added by Marc Dequènes over 13 years ago

  • ID 34bef607d2fb02b95306234d4e8fd03d7450fa92

[evol] cleanup and preliminary work on the Postman (and Needle-ization)

View differences:

bin/postman
# to allow in-place run for test
$: << File.join(File.dirname(__FILE__), "..", "lib")
#require 'socket'
require 'cyborghood'
require 'cyborghood/imap'
require 'cyborghood/mail'
require 'cyborghood/mail_order'
require 'cyborghood/command_runner'
#Socket.gethostname
require 'cyborghood/cyborg'
require 'cyborghood-postman/mail'
require 'cyborghood-postman/mail_order'
module CyborgHood
# example for specific validation rules of the bot-specific config file
#class Config
# protected
# class PostmanValidator < CyborgHoodValidator
# def validate_hook_in(value, rule, path, msg_list)
# msg_list << "Youhou !!!"
# end
# end
#end
module PostmanHome
module PostmanLand
include I18nTranslation
bindtextdomain("cyborghood_postman", {:path => Config::L10N_DIR, :charset => "UTF-8"})
# not yet ready to be a real Cyborg
class Postman #< Cyborg
include I18nTranslation
class Postman < Cyborg
# load config before modules are included
Config.load(self.human_name)
def initialize
# load config
Config.load(self.human_name.downcase)
@config = Config.instance
# setup logs
unless @config.log.nil?
logger.output_level(@config.log.console_level) unless @config.log.console_level.nil?
unless @config.log.path.nil?
if File.directory? @config.log.path
logger.log_to_file(File.join(@config.log.path, "ch_#{self.class.human_name}.log"))
else
logger.fatal "Log path does not exist or is not a directory, exiting"
exit 1
end
end
end
include BotNet
@imap = IMAP.new(@config.imap, @config.imap.min_check_interval)
def setup
super
logger.info "Bot '#{self.human_name}' loaded"
define_interface "0.1~"
end
def run
logger.info "Bot starting"
@imap.check_mails do |msg|
include I18nTranslation
def start_work
self.services.imap.check_mails do |msg|
begin
process_message(msg)
# Postman is not yet ready to process messages using the botnet
#process_message(msg)
rescue CyberError => e
raise
rescue
......
true
end
end
logger.info "Bot terminating"
end
def ask_to_stop
logger.info "Bot was asked to stop..."
@imap.stop_mail_check
super do
self.services.imap.stop_mail_check
end
end
def ready_to_stop?
super and not self.services.imap.connected?
end
private
......
end
end
bot = CyborgHood::PostmanHome::Postman.new
reg = Needle::Registry.new
reg.define do |b|
b.require 'cyborghood', CyborgHood
b.require 'cyborghood-postman/land', CyborgHood::PostmanLand
b.bot { CyborgHood::PostmanLand::Postman.new(b.postman_land) }
end
bot = reg.bot
trap('INT') do
bot.ask_to_stop

Also available in: Unified diff