Project

General

Profile

Download (2.77 KB) Statistics
| Branch: | Tag: | Revision:
require 'cyborghood/base'
require 'delegate'
require 'tmail'
require 'tmail_extra'
require 'gpgme'
require 'net/smtp'

# This class handles RFC3156 signed messages, validates them, and extract orders properly.
# Encrypted content are not implemented yet.
module CyborgHood
class Mail < Delegator
def initialize(msg)
@config = Config.instance

# unquote headers and transform into TMail object
@mail = TMail::Mail.parse(TMail::Unquoter.unquote_and_convert_to(msg, "UTF-8"))
end

def __getobj__
@mail
end

def self.normalize_new_lines(text)
text.to_s.gsub(/\r\n?/, "\n")
end

def parse
sig_check = verify_pgp_signature()
if sig_check.status == 0
logger.info "Mail content was properly signed by key #{sig_check.fingerprint}"
user = Person.find_by_fingerprint(sig_check.fingerprint)
if user.nil?
logger.info "Mail is from an unknown person"
else
logger.info "Mail is from user #{user.uid} (#{user.cn})"

signed_content = pgp_signed_part()
if signed_content.multipart?
if signed_content.parts[0].content_type == "text/plain"
command_txt = signed_content.parts[0].body
refs = signed_content.parts.collect{|p| p.dup }
end
else
command_txt = signed_content.body if signed_content.content_type == "text/plain"
refs = []
end

if command_txt
commands = []
command_txt.each_line do |line|
line.chomp!
sline = line.strip
# skip empty lines and comments
next if sline == "" or sline[0, 1] == "#"
# stop processing when detecting message signature
break if line == "-- "

commands << sline
end

return {:user => user, :commands => commands, :refs => refs}.to_ostruct
else
logger.info "Mail does not contain a proper MIME part for commands"
end
end
else
logger.info "Mail content tampered or badly signed: " + sig_check.to_s
end

nil
end

def create_reply
mail_reply = @mail.create_reply
mail_reply.from_addrs = TMail::Address.parse(@config.mail.from_address || self.to.first)
self.class.new(mail_reply.to_s)
end

def deliver
smtp_server = @config.mail.smtp_server || "localhost"
smtp_port = @config.mail.smtp_port || 25
smtp_from = @mail.from_addrs.collect{|a| a.address}.join(", ")
smtp_to = @mail.to_addrs.collect{|a| a.address}
Net::SMTP.start(smtp_server, smtp_port) do |smtp|
#smtp.send_message(@mail.to_s, smtp_from, smtp_to)
p @mail.to_s
end
end

def to_s
@mail.to_s
end
end
end
(5-5/6)