Project

General

Profile

Download (3.81 KB) Statistics
| Branch: | Tag: | Revision:
#!/usr/bin/ruby -Ku

# http://www.ruby-doc.org/stdlib/libdoc/net/imap/rdoc/index.html

$: << "./lib"

#require 'socket'
#require 'fileutils'
#require 'tempfile'
require 'shellwords'
require 'cyborghood/imap'
require 'cyborghood/mail'
require 'cyborghood/objects'

#Socket.gethostname

#
# TODO:
# - should be able to handle encrypted messages for user to send sensitive data (postman would need a GPG key too)
#

class CommandParser
def self.run(order)
order.commands.each do |cmd|
logger.info "Executing command: #{cmd}"
begin
execute_cmd(order.user, cmd, order.refs)
rescue
logger.info "Command failed: " + $!
end
end
end

private

def self.execute_cmd(user, cmdstr, refs)
cmdline = Shellwords.shellwords(cmdstr)
subsys = cmdline.shift

ok = true
case subsys.upcase
when "DNS"
case cmdline.shift.upcase
when "INFO"
if cmdline.empty?
list = CyborgHood::DnsDomain.find_by_manager(user)
logger.info "User is manager of the following zones: " + list.collect{|z| z.cn }.sort.join(", ")
else
ok = false
end
when "GET"
case cmdline.shift.upcase
when "ZONE"
zone = cmdline.shift.downcase
dom = CyborgHood::DnsDomain.new(zone)
logger.info "User requesting zone content for '#{zone}'"
if dom.hosted?
if dom.managed_by? user
logger.info "User is manager of the zone"
else
logger.info "User is not allowed to manage the zone"
end
else
logger.info "Zone not hosted"
end
else
ok = false
end
when "SET"
else
ok = false
end
else
ok = false
end

if not ok
logger.info "Command not recognized: #{cmdstr}"
end
end
end

module CyborgHood
# not yet ready to be a real Cyborg
class Postman #< Cyborg
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

@stop_asap = false

logger.info "Bot '#{self.human_name}' loaded"
end

def run
imap = IMAP.new(@config.imap)
imap.check_mail do |msg|
if @stop_asap
logger.info "Bot was asked to stop..."
break
end

mail = Mail.new(msg)
logger.info "Received mail with ID '#{mail.message_id}': #{mail.from_addrs} -> #{mail.to_addrs} (#{mail.subject})"

# ignore mails not signed
unless mail.is_pgp_signed?
logger.info "Mail not signed or not RFC3156 compliant, ignoring..."
next
end

logger.debug "Proper signed 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
if order.nil?
logger.info "Mail is invalid, ignoring..."
next
end

CommandParser.run(order)

mail_reply = mail.create_reply
mail_reply.body = "COUCOU"
mail_reply.deliver
end
end

def ask_to_stop
@stop_asap = true
end
end
end

bot = CyborgHood::Postman.new

trap('INT') do
bot.ask_to_stop
end
trap('TERM') do
bot.ask_to_stop
end

bot.run
(3-3/3)