1
0
Fork 0

- add *.log to svn:ignore

- move mail function to gpgmail module
- add parameter x-action=pgp-encrypted to mails


git-svn-id: file:///home/www/usr01/svn/gnuviechadmin/gnuviech.info/gnuviechadmin/trunk@235 a67ec6bc-e5d5-0310-a910-815c51eb3124
This commit is contained in:
Jan Dittberner 2007-07-25 16:04:40 +00:00
parent 732bb17fc5
commit d46d04567d
2 changed files with 78 additions and 45 deletions

View File

@ -19,13 +19,11 @@
#
# Version: $Id$
import smtplib, os, logging, tempfile
from email.MIMEText import MIMEText
from pyme import core
from pyme.constants.sig import mode
import os, logging, tempfile
from settings import config
from gnuviechadmin.exceptions import *
from gnuviechadmin.util import gpgmail
from subprocess import *
import sqlalchemy
@ -70,47 +68,7 @@ class BackendEntity(object):
"""This method sends a mail with the given text and subject
and signs it usign GnuPG. If a public key of the recipient is
available the mail is encrypted."""
plain = core.Data(text)
cipher = core.Data()
c = core.Context()
c.set_armor(1)
signer = config.get('common', 'mailfrom')
rcpt = config.get('common', 'mailto')
c.signers_clear()
for sigkey in [x for x in c.op_keylist_all(signer, 1)]:
if sigkey.can_sign:
c.signers_add(sigkey)
if not c.signers_enum(0):
raise Exception("No secret keys for signing available for %s." % (
signer))
keylist = []
for key in c.op_keylist_all(rcpt, 0):
valid = 0
subkey = key.subkeys
while subkey:
keyid = subkey.keyid
if keyid == None:
break
can_encrypt = subkey.can_encrypt
valid += can_encrypt
subkey = subkey.next
if valid:
keylist.append(key)
if keylist:
c.op_encrypt_sign(keylist, 1, plain, cipher)
else:
c.op_sign(plain, cipher, mode.CLEAR)
cipher.seek(0,0)
msg = MIMEText(cipher.read())
msg['Subject'] = subject
msg['From'] = signer
msg['To'] = rcpt
s = smtplib.SMTP()
s.connect()
s.sendmail(signer, [rcpt], msg.as_string())
s.close()
gpgmail.send_mail(subject, text)
def validate(self):
"""Validates whether all mandatory fields of the entity have

View File

@ -0,0 +1,75 @@
# -*- coding: UTF-8 -*-
#
# Copyright (C) 2007 by Jan Dittberner.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
# USA.
#
# Version: $Id$
import smtplib
from email.MIMEText import MIMEText
from pyme import core
from pyme.constants.sig import mode
from gnuviechadmin.backend.settings import config
def send_mail(subject, text):
"""This method sends a mail with the given text and subject
and signs it usign GnuPG. If a public key of the recipient is
available the mail is encrypted."""
plain = core.Data(text)
cipher = core.Data()
c = core.Context()
c.set_armor(1)
signer = config.get('common', 'mailfrom')
rcpt = config.get('common', 'mailto')
c.signers_clear()
for sigkey in [x for x in c.op_keylist_all(signer, 1)]:
if sigkey.can_sign:
c.signers_add(sigkey)
if not c.signers_enum(0):
raise Exception("No secret keys for signing available for %s." % (
signer))
keylist = []
for key in c.op_keylist_all(rcpt, 0):
valid = 0
subkey = key.subkeys
while subkey:
keyid = subkey.keyid
if keyid == None:
break
can_encrypt = subkey.can_encrypt
valid += can_encrypt
subkey = subkey.next
if valid:
keylist.append(key)
if keylist:
c.op_encrypt_sign(keylist, 1, plain, cipher)
else:
c.op_sign(plain, cipher, mode.CLEAR)
cipher.seek(0,0)
msg = MIMEText(cipher.read())
if keylist:
msg.set_param("x-action", "pgp-encrypted")
msg['Subject'] = subject
msg['From'] = signer
msg['To'] = rcpt
s = smtplib.SMTP()
s.connect()
s.sendmail(signer, [rcpt], msg.as_string())
s.close()