root/lib/cyborghood/mail.rb @ bc4894ce
bc4894ce | Marc Dequenes | require 'cyborghood/base'
|
|
require 'delegate'
|
|||
require 'tmail'
|
|||
require 'tmail_extra'
|
|||
require 'gpgme'
|
|||
# This class handles RFC3156 signed messages, validates them, and extract orders properly.
|
|||
# Encrypted content are not implemented yet.
|
|||
c427bfc7 | Marc Dequenes | module CyborgHood
|
|
class Mail < Delegator
|
|||
def initialize(msg)
|
|||
# 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 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
|
|||
end
|
|||
end
|