From d46d04567df1c667fcfa74d1d88fdccad129d716 Mon Sep 17 00:00:00 2001
From: Jan Dittberner <jan@dittberner.info>
Date: Wed, 25 Jul 2007 16:04:40 +0000
Subject: [PATCH] - 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
---
 gnuviechadmin/backend/BackendEntity.py | 48 ++---------------
 gnuviechadmin/util/gpgmail.py          | 75 ++++++++++++++++++++++++++
 2 files changed, 78 insertions(+), 45 deletions(-)
 create mode 100644 gnuviechadmin/util/gpgmail.py

diff --git a/gnuviechadmin/backend/BackendEntity.py b/gnuviechadmin/backend/BackendEntity.py
index 291f625..71e88f7 100644
--- a/gnuviechadmin/backend/BackendEntity.py
+++ b/gnuviechadmin/backend/BackendEntity.py
@@ -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
diff --git a/gnuviechadmin/util/gpgmail.py b/gnuviechadmin/util/gpgmail.py
new file mode 100644
index 0000000..9964744
--- /dev/null
+++ b/gnuviechadmin/util/gpgmail.py
@@ -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()