Revision 2fd36b40
Added by Marc Dequènes over 14 years ago
- ID 2fd36b409515560b0d0b84b02f065750db8a935d
bin/postman | ||
---|---|---|
logger.log_to_file(@config.log.file) unless @config.log.file.nil?
|
||
end
|
||
|
||
@current_thread = Thread.current
|
||
@stop_asap = false
|
||
@imap = IMAP.new(@config.imap, @config.imap.min_check_interval)
|
||
|
||
logger.info "Bot '#{self.human_name}' loaded"
|
||
end
|
||
|
||
def run
|
||
logger.info "Bot starting"
|
||
imap = IMAP.new(@config.imap, @config.imap.min_check_interval)
|
||
imap.check_mails do |msg|
|
||
@imap.check_mails do |msg|
|
||
process_message(msg)
|
||
end
|
||
logger.info "Bot terminating"
|
||
end
|
||
|
||
def ask_to_stop
|
||
@stop_asap = true
|
||
# wakeup sleeps
|
||
Process.kill("ALRM", $$)
|
||
logger.info "Bot was asked to stop..."
|
||
@imap.stop_mail_check
|
||
end
|
||
|
||
private
|
||
|
||
def process_message(msg)
|
||
if @stop_asap
|
||
logger.info "Bot was asked to stop..."
|
||
return false
|
||
end
|
||
|
||
mail = Mail.new(msg.content)
|
||
logger.info "Received mail with ID '#{mail.message_id}': #{mail.from_pretty} -> #{mail.to_pretty} (#{mail.subject_pretty})"
|
||
|
lib/cyborghood/imap.rb | ||
---|---|---|
end
|
||
|
||
def check_mails(&message_handler)
|
||
@stop_mail_check = false
|
||
until @stop_mail_check
|
||
t = Time.now.to_i
|
||
r = check_mails_once(&message_handler)
|
||
... | ... | |
# wait before new check either if the IMAP server does not support IDLE mode
|
||
# or if an error occured
|
||
sleep_time = @min_check_interval - (t2 - t)
|
||
if sleep_time > 0
|
||
if sleep_time > 0 and not @stop_mail_check
|
||
logger.debug "Having a break before new check..."
|
||
begin
|
||
sleep(sleep_time)
|
||
rescue
|
||
t = Time.now.to_i
|
||
while Time.now.to_i - t < sleep_time and not @stop_mail_check
|
||
sleep(1)
|
||
end
|
||
end
|
||
end
|
||
end
|
||
|
||
def stop_mail_check
|
||
@stop_mail_check = true
|
||
end
|
||
|
||
def check_inbox(&message_handler)
|
||
logger.debug "Examining INBOX"
|
||
@imap.select('INBOX')
|
||
|
||
logger.debug "Starting mail check"
|
||
@imap.search(["ALL"], "UTF-8").each do |message_id|
|
||
break if @stop_mail_check
|
||
|
||
logger.debug "*** Fetched mail ##{message_id}"
|
||
unless message_handler.call IMAPMessage.new(@imap, message_id)
|
||
@stop_mail_check = true
|
||
... | ... | |
end
|
||
logger.debug "Mail check finished"
|
||
|
||
# TODO: expunge after <n> mails processed in IDLE mode
|
||
@imap.expunge
|
||
end
|
||
|
Also available in: Unified diff
[fix] repaired bot stop action