2008-12-15 [colin] 3.6.1cvs78
[claws.git] / src / plugins / pgpmime / pgpmime.c
index 1b7110e03d29d18e4f0f572bb43067ce641d49a2..be21e5c234c34005270f23dc9b3f283ea0d3b6b1 100644 (file)
@@ -1,10 +1,10 @@
 /*
- * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2007 Hiroyuki Yamamoto & the Claws Mail team
+ * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
+ * Copyright (C) 1999-2007 the Claws Mail team
  *
  * 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 2 of the License, or
+ * 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,
@@ -13,8 +13,8 @@
  * 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.
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ * 
  */
 
 #ifdef HAVE_CONFIG_H
@@ -348,12 +348,37 @@ static MimeInfo *pgpmime_decrypt(MimeInfo *mimeinfo)
                return NULL;
        }
 
-       fprintf(dstfp, "MIME-Version: 1.0\n");
+       if (fprintf(dstfp, "MIME-Version: 1.0\n") < 0) {
+               FILE_OP_ERROR(fname, "fprintf");
+               privacy_set_error(_("Couldn't write to decrypted file %s"), fname);
+               g_free(fname);
+               gpgme_data_release(plain);
+               gpgme_release(ctx);
+               debug_print("can't open!\n");
+               return NULL;
+       }
 
-       chars = gpgme_data_release_and_get_mem(plain, &len);
-       if (len > 0)
-               fwrite(chars, len, 1, dstfp);
-       fclose(dstfp);
+       chars = sgpgme_data_release_and_get_mem(plain, &len);
+       if (len > 0) {
+               if (fwrite(chars, 1, len, dstfp) < len) {
+                       FILE_OP_ERROR(fname, "fwrite");
+                       privacy_set_error(_("Couldn't write to decrypted file %s"), fname);
+                       g_free(fname);
+                       gpgme_data_release(plain);
+                       gpgme_release(ctx);
+                       debug_print("can't open!\n");
+                       return NULL;
+               }
+       }
+       if (fclose(dstfp) == EOF) {
+               FILE_OP_ERROR(fname, "fclose");
+               privacy_set_error(_("Couldn't close decrypted file %s"), fname);
+               g_free(fname);
+               gpgme_data_release(plain);
+               gpgme_release(ctx);
+               debug_print("can't open!\n");
+               return NULL;
+       }
 
        parseinfo = procmime_scan_file(fname);
        g_free(fname);
@@ -394,7 +419,7 @@ static MimeInfo *pgpmime_decrypt(MimeInfo *mimeinfo)
        return decinfo;
 }
 
-gboolean pgpmime_sign(MimeInfo *mimeinfo, PrefsAccount *account)
+gboolean pgpmime_sign(MimeInfo *mimeinfo, PrefsAccount *account, const gchar *from_addr)
 {
        MimeInfo *msgcontent, *sigmultipart, *newinfo;
        gchar *textstr, *micalg;
@@ -472,12 +497,13 @@ gboolean pgpmime_sign(MimeInfo *mimeinfo, PrefsAccount *account)
        gpgme_set_armor(ctx, 1);
        gpgme_signers_clear (ctx);
 
-       if (!sgpgme_setup_signers(ctx, account)) {
+       if (!sgpgme_setup_signers(ctx, account, from_addr)) {
                gpgme_release(ctx);
                return FALSE;
        }
 
-       if (getenv("GPG_AGENT_INFO") && prefs_gpg_get_config()->use_agent_if_available) {
+       prefs_gpg_enable_agent(prefs_gpg_get_config()->use_gpg_agent);
+       if (getenv("GPG_AGENT_INFO") && prefs_gpg_get_config()->use_gpg_agent) {
                debug_print("GPG_AGENT_INFO environment defined, running without passphrase callback\n");
        } else {
                info.c = ctx;
@@ -530,12 +556,12 @@ gboolean pgpmime_sign(MimeInfo *mimeinfo, PrefsAccount *account)
                return FALSE;
        }
 
-       sigcontent = gpgme_data_release_and_get_mem(gpgsig, &len);
+       sigcontent = sgpgme_data_release_and_get_mem(gpgsig, &len);
        gpgme_data_release(gpgtext);
        g_free(textstr);
 
        if (sigcontent == NULL || len <= 0) {
-               g_warning("gpgme_data_release_and_get_mem failed");
+               g_warning("sgpgme_data_release_and_get_mem failed");
                privacy_set_error(_("Data signing failed, no contents."));
                return FALSE;
        }
@@ -568,6 +594,23 @@ gchar *pgpmime_get_encrypt_data(GSList *recp_names)
        return sgpgme_get_encrypt_data(recp_names, GPGME_PROTOCOL_OpenPGP);
 }
 
+static const gchar *pgpmime_get_encrypt_warning(void)
+{
+       if (prefs_gpg_should_skip_encryption_warning(pgpmime_system.id))
+               return NULL;
+       else
+               return _("Please note that email headers, like Subject, "
+                        "are not encrypted by the PGP/Mime system.");
+}
+
+static void pgpmime_inhibit_encrypt_warning(gboolean inhibit)
+{
+       if (inhibit)
+               prefs_gpg_add_skip_encryption_warning(pgpmime_system.id);
+       else
+               prefs_gpg_remove_skip_encryption_warning(pgpmime_system.id);
+}
+
 gboolean pgpmime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
 {
        MimeInfo *msgcontent, *encmultipart, *newinfo;
@@ -646,12 +689,12 @@ gboolean pgpmime_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
        
        err = gpgme_op_encrypt(ctx, kset, GPGME_ENCRYPT_ALWAYS_TRUST, gpgtext, gpgenc);
 
-       enccontent = gpgme_data_release_and_get_mem(gpgenc, &len);
+       enccontent = sgpgme_data_release_and_get_mem(gpgenc, &len);
        gpgme_data_release(gpgtext);
        g_free(textstr);
 
        if (enccontent == NULL || len <= 0) {
-               g_warning("gpgme_data_release_and_get_mem failed");
+               g_warning("sgpgme_data_release_and_get_mem failed");
                privacy_set_error(_("Encryption failed, %s"), gpgme_strerror(err));
                gpgme_release(ctx);
                return FALSE;
@@ -705,6 +748,8 @@ static PrivacySystem pgpmime_system = {
        TRUE,
        pgpmime_get_encrypt_data,
        pgpmime_encrypt,
+       pgpmime_get_encrypt_warning,
+       pgpmime_inhibit_encrypt_warning,
 };
 
 void pgpmime_init()