Project

General

Profile

Download (4.22 KB) Statistics
| Branch: | Tag: | Revision:
require 'cyborghood/base'
require 'delegate'
require 'tmail'
require 'tmail_extra'
require 'gpgme' # >= 1.0.2 needed for :always_trust sign option
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

def crypt(fingerprint)
# build a fake mail to get the generated content to be crypted
# TODO: this is buggy yet
if @mail.multipart?
parts = @mail.parts
else
# transform to multipart
single_part = TMail::Mail.new
single_part.content_type = @mail.content_type
single_part.body = @mail.body
single_part.transfer_encoding = @mail.transfer_encoding
parts = [single_part]
end
fake_mail = TMail::Mail.new
fake_mail.set_content_type("multipart", "mixed", {'boundary' => TMail.new_boundary})
parts.each {|p| fake_mail.parts << p }
clear_data = fake_mail.to_s
# remove fake headers to get only generated parts
clear_data.sub!(/^.*\n\n/, "")

# retrieve key and encrypt
gpg = GPGME::Ctx.new
key = gpg.get_key(fingerprint)
p clear_data
encrypted_data = GPGME.encrypt([key], clear_data, {:armor => true, :always_trust => true})

# build properly encrypted mail
p_pgp = TMail::Mail.new
p_pgp.set_content_type("application", "pgp-encrypted")
p_pgp.body = "Version: 1"
p_encrypted = TMail::Mail.new
p_encrypted.set_content_type("application", "octet-stream")
p_encrypted.body = encrypted_data
@mail.set_content_type("multipart", "encrypted", {'boundary' => TMail.new_boundary})
@mail.parts << p_pgp
@mail.parts << p_encrypted
end
end
end
(5-5/6)