fix bug #2879, 'Plain text mails treated as encrypted'
[claws.git] / src / plugins / pgpinline / pgpinline.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2012 Colin Leroy <colin@colino.net> and 
4  * the Claws Mail team
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  * 
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #  include "config.h"
23 #include "claws-features.h"
24 #endif
25
26 #ifdef USE_GPGME
27
28 #include "defs.h"
29 #include <glib.h>
30 #include <glib/gi18n.h>
31 #include <errno.h>
32 #include <gpgme.h>
33
34 #include "utils.h"
35 #include "privacy.h"
36 #include "procmime.h"
37 #include "pgpinline.h"
38 #include <plugins/pgpcore/sgpgme.h>
39 #include <plugins/pgpcore/prefs_gpg.h>
40 #include <plugins/pgpcore/passphrase.h>
41 #include "quoted-printable.h"
42 #include "base64.h"
43 #include "codeconv.h"
44 #include "plugin.h"
45
46 extern struct GPGConfig prefs_gpg;
47
48 typedef struct _PrivacyDataPGP PrivacyDataPGP;
49
50 struct _PrivacyDataPGP
51 {
52         PrivacyData     data;
53         
54         gboolean        done_sigtest;
55         gboolean        is_signed;
56         gpgme_verify_result_t   sigstatus;
57         gpgme_ctx_t     ctx;
58 };
59
60 static PrivacySystem pgpinline_system;
61
62 static gint pgpinline_check_signature(MimeInfo *mimeinfo);
63
64 static PrivacyDataPGP *pgpinline_new_privacydata()
65 {
66         PrivacyDataPGP *data;
67         gpgme_error_t err;
68
69         data = g_new0(PrivacyDataPGP, 1);
70         data->data.system = &pgpinline_system;
71         data->done_sigtest = FALSE;
72         data->is_signed = FALSE;
73         data->sigstatus = NULL;
74         if ((err = gpgme_new(&data->ctx)) != GPG_ERR_NO_ERROR) {
75                 debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
76                 return NULL;
77         }
78         
79         return data;
80 }
81
82 static void pgpinline_free_privacydata(PrivacyData *_data)
83 {
84         PrivacyDataPGP *data = (PrivacyDataPGP *) _data;
85         gpgme_release(data->ctx);
86         g_free(data);
87 }
88
89 static gchar *fp_read_noconv(FILE *fp)
90 {
91         GByteArray *array;
92         guchar buf[BUFSIZ];
93         gint n_read;
94         gchar *result = NULL;
95
96         if (!fp)
97                 return NULL;
98         array = g_byte_array_new();
99
100         while ((n_read = fread(buf, sizeof(gchar), sizeof(buf), fp)) > 0) {
101                 if (n_read < sizeof(buf) && ferror(fp))
102                         break;
103                 g_byte_array_append(array, buf, n_read);
104         }
105
106         if (ferror(fp)) {
107                 FILE_OP_ERROR("file stream", "fread");
108                 g_byte_array_free(array, TRUE);
109                 return NULL;
110         }
111
112         buf[0] = '\0';
113         g_byte_array_append(array, buf, 1);
114         result = (gchar *)array->data;
115         g_byte_array_free(array, FALSE);
116         
117         return result;
118 }
119
120 static gchar *get_part_as_string(MimeInfo *mimeinfo)
121 {
122         gchar *textdata = NULL;
123         gchar *filename = NULL;
124         FILE *fp;
125
126         cm_return_val_if_fail(mimeinfo != NULL, 0);
127         procmime_decode_content(mimeinfo);
128         
129         if (mimeinfo->content == MIMECONTENT_MEM)
130                 textdata = g_strdup(mimeinfo->data.mem);
131         else {
132                 filename = procmime_get_tmp_file_name(mimeinfo);
133                 if (procmime_get_part(filename, mimeinfo) < 0) {
134                         printf("error dumping file\n");
135                         return NULL;
136                 }
137                 fp = g_fopen(filename,"rb");
138                 if (!fp) {
139                         printf("error reading file\n");
140                         return NULL;
141                 }
142                 textdata = fp_read_noconv(fp);
143                 fclose(fp);
144                 g_unlink(filename);
145                 g_free(filename);
146         }
147
148         if (!g_utf8_validate(textdata, -1, NULL)) {
149                 gchar *tmp = NULL;
150                 codeconv_set_strict(TRUE);
151                 if (procmime_mimeinfo_get_parameter(mimeinfo, "charset")) {
152                         tmp = conv_codeset_strdup(textdata,
153                                 procmime_mimeinfo_get_parameter(mimeinfo, "charset"),
154                                 CS_UTF_8);
155                 }
156                 if (!tmp) {
157                         tmp = conv_codeset_strdup(textdata,
158                                 conv_get_locale_charset_str_no_utf8(), 
159                                 CS_UTF_8);
160                 }
161                 codeconv_set_strict(FALSE);
162                 if (!tmp) {
163                         tmp = conv_codeset_strdup(textdata,
164                                 conv_get_locale_charset_str_no_utf8(), 
165                                 CS_UTF_8);
166                 }
167                 if (tmp) {
168                         g_free(textdata);
169                         textdata = tmp;
170                 }
171         }
172
173         return textdata;        
174 }
175
176 static gchar *pgpinline_locate_armor_header(gchar *textdata, const gchar *armor_header)
177 {
178         gchar *pos;
179
180         pos = strstr(textdata, armor_header);
181         /*
182          * It's only a valid armor header if it's at the
183          * beginning of the buffer or a new line.
184          */
185         if (pos != NULL && (pos == textdata || *(pos-1) == '\n'))
186         {
187               return pos;
188         }
189         return NULL;
190 }
191
192 static gboolean pgpinline_is_signed(MimeInfo *mimeinfo)
193 {
194         PrivacyDataPGP *data = NULL;
195         const gchar *sig_indicator = "-----BEGIN PGP SIGNED MESSAGE-----";
196         gchar *textdata, *sigpos;
197         
198         cm_return_val_if_fail(mimeinfo != NULL, FALSE);
199         
200         if (procmime_mimeinfo_parent(mimeinfo) == NULL)
201                 return FALSE; /* not parent */
202         
203         if (mimeinfo->type != MIMETYPE_TEXT &&
204                 (mimeinfo->type != MIMETYPE_APPLICATION ||
205                  g_ascii_strcasecmp(mimeinfo->subtype, "pgp")))
206                 return FALSE;
207
208         /* Seal the deal. This has to be text/plain through and through. */
209         if (mimeinfo->type == MIMETYPE_APPLICATION)
210         {
211                 mimeinfo->type = MIMETYPE_TEXT;
212                 g_free(mimeinfo->subtype);
213                 mimeinfo->subtype = g_strdup("plain");
214         }
215
216         if (mimeinfo->privacy != NULL) {
217                 data = (PrivacyDataPGP *) mimeinfo->privacy;
218                 if (data->done_sigtest)
219                         return data->is_signed;
220         }
221         
222         textdata = get_part_as_string(mimeinfo);
223         if (!textdata)
224                 return FALSE;
225         
226         if ((sigpos = strstr(textdata, sig_indicator)) == NULL) {
227                 g_free(textdata);
228                 return FALSE;
229         }
230
231         if (!(sigpos == textdata) && !(sigpos[-1] == '\n')) {
232                 g_free(textdata);
233                 return FALSE;
234         }
235
236         g_free(textdata);
237
238         if (data == NULL) {
239                 data = pgpinline_new_privacydata();
240                 mimeinfo->privacy = (PrivacyData *) data;
241         }
242         data->done_sigtest = TRUE;
243         data->is_signed = TRUE;
244
245         return TRUE;
246 }
247
248 static gint pgpinline_check_signature(MimeInfo *mimeinfo)
249 {
250         PrivacyDataPGP *data = NULL;
251         gchar *textdata = NULL, *tmp = NULL;
252         gpgme_data_t plain = NULL, cipher = NULL;
253         gpgme_error_t err;
254
255         cm_return_val_if_fail(mimeinfo != NULL, 0);
256
257         if (procmime_mimeinfo_parent(mimeinfo) == NULL) {
258                 privacy_set_error(_("Incorrect part"));
259                 return 0; /* not parent */
260         }
261         if (mimeinfo->type != MIMETYPE_TEXT) {
262                 privacy_set_error(_("Not a text part"));
263                 debug_print("type %d\n", mimeinfo->type);
264                 return 0;
265         }
266         cm_return_val_if_fail(mimeinfo->privacy != NULL, 0);
267         data = (PrivacyDataPGP *) mimeinfo->privacy;
268
269         textdata = get_part_as_string(mimeinfo);
270
271         if (!textdata) {
272                 g_free(textdata);
273                 privacy_set_error(_("Couldn't get text data."));
274                 return 0;
275         }
276
277         /* gtk2: convert back from utf8 */
278         tmp = conv_codeset_strdup(textdata, CS_UTF_8,
279                         procmime_mimeinfo_get_parameter(mimeinfo, "charset"));
280         if (!tmp) {
281                 tmp = conv_codeset_strdup(textdata, CS_UTF_8,
282                         conv_get_locale_charset_str_no_utf8());
283         }
284         if (!tmp) {
285                 g_warning("Can't convert charset to anything sane\n");
286                 tmp = conv_codeset_strdup(textdata, CS_UTF_8, CS_US_ASCII);
287         }
288         g_free(textdata);
289
290         if (!tmp) {
291                 privacy_set_error(_("Couldn't convert text data to any sane charset."));
292                 return 0;
293         }
294         textdata = g_strdup(tmp);
295         g_free(tmp);
296         
297         if ((err = gpgme_new(&data->ctx)) != GPG_ERR_NO_ERROR) {
298                 debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
299                 privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
300                 g_free(textdata);
301                 return 0;
302         }
303         gpgme_set_textmode(data->ctx, 1);
304         gpgme_set_armor(data->ctx, 1);
305         
306         gpgme_data_new_from_mem(&plain, textdata, (size_t)strlen(textdata), 1);
307         gpgme_data_new(&cipher);
308
309         data->sigstatus = sgpgme_verify_signature(data->ctx, plain, NULL, cipher);
310         
311         gpgme_data_release(plain);
312         gpgme_data_release(cipher);
313         
314         g_free(textdata);
315         
316         return 0;
317 }
318
319 static SignatureStatus pgpinline_get_sig_status(MimeInfo *mimeinfo)
320 {
321         PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
322         
323         cm_return_val_if_fail(data != NULL, SIGNATURE_INVALID);
324
325         if (data->sigstatus == NULL && 
326             prefs_gpg_get_config()->auto_check_signatures)
327                 pgpinline_check_signature(mimeinfo);
328
329         return sgpgme_sigstat_gpgme_to_privacy(data->ctx, data->sigstatus);
330 }
331
332 static gchar *pgpinline_get_sig_info_short(MimeInfo *mimeinfo)
333 {
334         PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
335         
336         cm_return_val_if_fail(data != NULL, g_strdup("Error"));
337
338         if (data->sigstatus == NULL && 
339             prefs_gpg_get_config()->auto_check_signatures)
340                 pgpinline_check_signature(mimeinfo);
341         
342         return sgpgme_sigstat_info_short(data->ctx, data->sigstatus);
343 }
344
345 static gchar *pgpinline_get_sig_info_full(MimeInfo *mimeinfo)
346 {
347         PrivacyDataPGP *data = (PrivacyDataPGP *) mimeinfo->privacy;
348         
349         cm_return_val_if_fail(data != NULL, g_strdup("Error"));
350
351         return sgpgme_sigstat_info_full(data->ctx, data->sigstatus);
352 }
353
354
355
356 static gboolean pgpinline_is_encrypted(MimeInfo *mimeinfo)
357 {
358         const gchar *enc_indicator = "-----BEGIN PGP MESSAGE-----";
359         gchar *textdata;
360         
361         cm_return_val_if_fail(mimeinfo != NULL, FALSE);
362         
363         if (procmime_mimeinfo_parent(mimeinfo) == NULL)
364                 return FALSE; /* not parent */
365         
366         if (mimeinfo->type != MIMETYPE_TEXT &&
367                 (mimeinfo->type != MIMETYPE_APPLICATION ||
368                  g_ascii_strcasecmp(mimeinfo->subtype, "pgp")))
369                 return FALSE;
370         
371         /* Seal the deal. This has to be text/plain through and through. */
372         if (mimeinfo->type == MIMETYPE_APPLICATION)
373         {
374                 mimeinfo->type = MIMETYPE_TEXT;
375                 g_free(mimeinfo->subtype);
376                 mimeinfo->subtype = g_strdup("plain");
377         }
378         
379         textdata = get_part_as_string(mimeinfo);
380         if (!textdata)
381                 return FALSE;
382         
383         if (!pgpinline_locate_armor_header(textdata, enc_indicator)) {
384                 g_free(textdata);
385                 return FALSE;
386         }
387
388         g_free(textdata);
389
390         return TRUE;
391 }
392
393 static MimeInfo *pgpinline_decrypt(MimeInfo *mimeinfo)
394 {
395         MimeInfo *decinfo, *parseinfo;
396         gpgme_data_t cipher, plain;
397         FILE *dstfp;
398         gchar *fname;
399         gchar *textdata = NULL;
400         static gint id = 0;
401         const gchar *src_codeset = NULL;
402         gpgme_verify_result_t sigstat = 0;
403         PrivacyDataPGP *data = NULL;
404         gpgme_ctx_t ctx;
405         gchar *chars;
406         size_t len;
407         const gchar *begin_indicator = "-----BEGIN PGP MESSAGE-----";
408         const gchar *end_indicator = "-----END PGP MESSAGE-----";
409         gchar *pos;
410         
411         if (gpgme_new(&ctx) != GPG_ERR_NO_ERROR)
412                 return NULL;
413
414         gpgme_set_textmode(ctx, 1);
415         gpgme_set_armor(ctx, 1);
416
417         cm_return_val_if_fail(mimeinfo != NULL, NULL);
418         cm_return_val_if_fail(pgpinline_is_encrypted(mimeinfo), NULL);
419         
420         if (procmime_mimeinfo_parent(mimeinfo) == NULL ||
421             mimeinfo->type != MIMETYPE_TEXT) {
422                 gpgme_release(ctx);
423                 privacy_set_error(_("Couldn't parse mime part."));
424                 return NULL;
425         }
426
427         textdata = get_part_as_string(mimeinfo);
428         if (!textdata) {
429                 gpgme_release(ctx);
430                 privacy_set_error(_("Couldn't get text data."));
431                 return NULL;
432         }
433
434         debug_print("decrypting '%s'\n", textdata);
435         gpgme_data_new_from_mem(&cipher, textdata, (size_t)strlen(textdata), 1);
436
437         plain = sgpgme_decrypt_verify(cipher, &sigstat, ctx);
438         if (sigstat && !sigstat->signatures)
439                 sigstat = NULL;
440
441         gpgme_data_release(cipher);
442         
443         if (plain == NULL) {
444                 gpgme_release(ctx);
445                 return NULL;
446         }
447
448         fname = g_strdup_printf("%s%cplaintext.%08x",
449                 get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id);
450
451         if ((dstfp = g_fopen(fname, "wb")) == NULL) {
452                 FILE_OP_ERROR(fname, "fopen");
453                 privacy_set_error(_("Couldn't open decrypted file %s"), fname);
454                 g_free(fname);
455                 gpgme_data_release(plain);
456                 gpgme_release(ctx);
457                 return NULL;
458         }
459
460         src_codeset = procmime_mimeinfo_get_parameter(mimeinfo, "charset");
461         if (src_codeset == NULL)
462                 src_codeset = CS_ISO_8859_1;
463                 
464         if (fprintf(dstfp, "MIME-Version: 1.0\r\n"
465                         "Content-Type: text/plain; charset=%s\r\n"
466                         "Content-Transfer-Encoding: 8bit\r\n"
467                         "\r\n",
468                         src_codeset) < 0) {
469                 FILE_OP_ERROR(fname, "fprintf");
470                 privacy_set_error(_("Couldn't write to decrypted file %s"), fname);
471                 goto FILE_ERROR;
472         }
473
474         /* Store any part before encrypted text */
475         pos = pgpinline_locate_armor_header(textdata, begin_indicator);
476         if (pos != NULL && (pos - textdata) > 0) {
477             if (fwrite(textdata, 1, pos - textdata, dstfp) < pos - textdata) {
478                 FILE_OP_ERROR(fname, "fwrite");
479                 privacy_set_error(_("Couldn't write to decrypted file %s"), fname);
480                 goto FILE_ERROR;
481             }
482         }
483         
484         if (fwrite(_("\n--- Start of PGP/Inline encrypted data ---\n"), 1,
485                 strlen(_("\n--- Start of PGP/Inline encrypted data ---\n")), 
486                 dstfp) < strlen(_("\n--- Start of PGP/Inline encrypted data ---\n"))) {
487                 FILE_OP_ERROR(fname, "fwrite");
488                 privacy_set_error(_("Couldn't write to decrypted file %s"), fname);
489                 goto FILE_ERROR;
490         }
491         chars = sgpgme_data_release_and_get_mem(plain, &len);
492         if (len > 0) {
493                 if (fwrite(chars, 1, len, dstfp) < len) {
494                         FILE_OP_ERROR(fname, "fwrite");
495                         g_free(chars);
496                         privacy_set_error(_("Couldn't write to decrypted file %s"), fname);
497                         goto FILE_ERROR;
498                 }
499         }
500         g_free(chars);
501         /* Store any part after encrypted text */
502         if (fwrite(_("--- End of PGP/Inline encrypted data ---\n"), 1,
503                 strlen(_("--- End of PGP/Inline encrypted data ---\n")), 
504                 dstfp) < strlen(_("--- End of PGP/Inline encrypted data ---\n"))) {
505                         FILE_OP_ERROR(fname, "fwrite");
506                         privacy_set_error(_("Couldn't write to decrypted file %s"), fname);
507                         goto FILE_ERROR;
508         }
509         if (pos != NULL) {
510             pos = pgpinline_locate_armor_header(pos, end_indicator);
511             if (pos != NULL && *pos != '\0') {
512                 pos += strlen(end_indicator);
513                 if (fwrite(pos, 1, strlen(pos), dstfp) < strlen(pos)) {
514                         FILE_OP_ERROR(fname, "fwrite");
515                         privacy_set_error(_("Couldn't write to decrypted file %s"), fname);
516                         goto FILE_ERROR;
517                 }
518             }
519         }
520
521         if (fclose(dstfp) == EOF) {
522                 FILE_OP_ERROR(fname, "fclose");
523                 privacy_set_error(_("Couldn't close decrypted file %s"), fname);
524                 g_free(fname);
525                 gpgme_data_release(plain);
526                 gpgme_release(ctx);
527                 return NULL;
528         }
529         
530         parseinfo = procmime_scan_file(fname);
531         g_free(fname);
532         
533         if (parseinfo == NULL) {
534                 gpgme_release(ctx);
535                 privacy_set_error(_("Couldn't scan decrypted file."));
536                 return NULL;
537         }
538         decinfo = g_node_first_child(parseinfo->node) != NULL ?
539                 g_node_first_child(parseinfo->node)->data : NULL;
540                 
541         if (decinfo == NULL) {
542                 gpgme_release(ctx);
543                 privacy_set_error(_("Couldn't scan decrypted file parts."));
544                 return NULL;
545         }
546
547         g_node_unlink(decinfo->node);
548         procmime_mimeinfo_free_all(parseinfo);
549
550         decinfo->tmp = TRUE;
551
552         if (sigstat != GPGME_SIG_STAT_NONE) {
553                 if (decinfo->privacy != NULL) {
554                         data = (PrivacyDataPGP *) decinfo->privacy;
555                 } else {
556                         data = pgpinline_new_privacydata();
557                         decinfo->privacy = (PrivacyData *) data;        
558                 }
559                 data->done_sigtest = TRUE;
560                 data->is_signed = TRUE;
561                 data->sigstatus = sigstat;
562                 if (data->ctx)
563                         gpgme_release(data->ctx);
564                 data->ctx = ctx;
565         } else
566                 gpgme_release(ctx);
567
568         return decinfo;
569
570 FILE_ERROR:
571         fclose(dstfp);
572         g_free(fname);
573         gpgme_data_release(plain);
574         gpgme_release(ctx);
575         return NULL;
576 }
577
578 static gboolean pgpinline_sign(MimeInfo *mimeinfo, PrefsAccount *account, const gchar *from_addr)
579 {
580         MimeInfo *msgcontent;
581         gchar *textstr, *tmp;
582         FILE *fp;
583         gchar *sigcontent;
584         gpgme_ctx_t ctx;
585         gpgme_data_t gpgtext, gpgsig;
586         size_t len;
587         gpgme_error_t err;
588         struct passphrase_cb_info_s info;
589         gpgme_sign_result_t result = NULL;
590
591         memset (&info, 0, sizeof info);
592
593         /* get content node from message */
594         msgcontent = (MimeInfo *) mimeinfo->node->children->data;
595         if (msgcontent->type == MIMETYPE_MULTIPART) {
596                 if (!msgcontent->node->children) {
597                         debug_print("msgcontent->node->children NULL, bailing\n");
598                         privacy_set_error(_("Malformed message"));
599                         return FALSE;
600                 }
601                 msgcontent = (MimeInfo *) msgcontent->node->children->data;
602         }
603         /* get rid of quoted-printable or anything */
604         procmime_decode_content(msgcontent);
605
606         fp = my_tmpfile();
607         if (fp == NULL) {
608                 perror("my_tmpfile");
609                 privacy_set_error(_("Couldn't create temporary file."));
610                 return FALSE;
611         }
612         procmime_write_mimeinfo(msgcontent, fp);
613         rewind(fp);
614
615         /* read temporary file into memory */
616         textstr = fp_read_noconv(fp);
617         
618         fclose(fp);
619                 
620         gpgme_data_new_from_mem(&gpgtext, textstr, (size_t)strlen(textstr), 0);
621         gpgme_data_new(&gpgsig);
622         if ((err = gpgme_new(&ctx)) != GPG_ERR_NO_ERROR) {
623                 debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
624                 privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
625                 return FALSE;
626         }
627         gpgme_set_textmode(ctx, 1);
628         gpgme_set_armor(ctx, 1);
629
630         if (!sgpgme_setup_signers(ctx, account, from_addr)) {
631                 gpgme_release(ctx);
632                 return FALSE;
633         }
634
635         prefs_gpg_enable_agent(prefs_gpg_get_config()->use_gpg_agent);
636         if (!getenv("GPG_AGENT_INFO") || !prefs_gpg_get_config()->use_gpg_agent) {
637                 info.c = ctx;
638                 gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
639         }
640
641         err = gpgme_op_sign(ctx, gpgtext, gpgsig, GPGME_SIG_MODE_CLEAR);
642         if (err != GPG_ERR_NO_ERROR) {
643                 if (err == GPG_ERR_CANCELED) {
644                         /* ignore cancelled signing */
645                         privacy_reset_error();
646                         debug_print("gpgme_op_sign cancelled\n");
647                 } else {
648                         privacy_set_error(_("Data signing failed, %s"), gpgme_strerror(err));
649                         debug_print("gpgme_op_sign error : %x\n", err);
650                 }
651                 gpgme_release(ctx);
652                 return FALSE;
653         }
654         result = gpgme_op_sign_result(ctx);
655         if (result && result->signatures) {
656                 gpgme_new_signature_t sig = result->signatures;
657                 while (sig) {
658                         debug_print("valid signature: %s\n", sig->fpr);
659                         sig = sig->next;
660                 }
661         } else if (result && result->invalid_signers) {
662                 gpgme_invalid_key_t invalid = result->invalid_signers;
663                 while (invalid) {
664                         g_warning("invalid signer: %s (%s)", invalid->fpr, 
665                                 gpgme_strerror(invalid->reason));
666                         privacy_set_error(_("Data signing failed due to invalid signer: %s"), 
667                                 gpgme_strerror(invalid->reason));
668                         invalid = invalid->next;
669                 }
670                 gpgme_release(ctx);
671                 return FALSE;
672         } else {
673                 /* can't get result (maybe no signing key?) */
674                 debug_print("gpgme_op_sign_result error\n");
675                 privacy_set_error(_("Data signing failed, no results."));
676                 gpgme_release(ctx);
677                 return FALSE;
678         }
679
680
681         sigcontent = sgpgme_data_release_and_get_mem(gpgsig, &len);
682         
683         if (sigcontent == NULL || len <= 0) {
684                 g_warning("sgpgme_data_release_and_get_mem failed");
685                 privacy_set_error(_("Data signing failed, no contents."));
686                 gpgme_data_release(gpgtext);
687                 g_free(textstr);
688                 g_free(sigcontent);
689                 gpgme_release(ctx);
690                 return FALSE;
691         }
692
693         tmp = g_malloc(len+1);
694         g_memmove(tmp, sigcontent, len+1);
695         tmp[len] = '\0';
696         gpgme_data_release(gpgtext);
697         g_free(textstr);
698         g_free(sigcontent);
699
700         if (msgcontent->content == MIMECONTENT_FILE &&
701             msgcontent->data.filename != NULL) {
702                 if (msgcontent->tmp == TRUE)
703                         claws_unlink(msgcontent->data.filename);
704                 g_free(msgcontent->data.filename);
705         }
706         msgcontent->data.mem = g_strdup(tmp);
707         msgcontent->content = MIMECONTENT_MEM;
708         g_free(tmp);
709
710         /* avoid all sorts of clear-signing problems with non ascii
711          * chars
712          */
713         procmime_encode_content(msgcontent, ENC_BASE64);
714         gpgme_release(ctx);
715
716         return TRUE;
717 }
718
719 static gchar *pgpinline_get_encrypt_data(GSList *recp_names)
720 {
721         return sgpgme_get_encrypt_data(recp_names, GPGME_PROTOCOL_OpenPGP);
722 }
723
724 static const gchar *pgpinline_get_encrypt_warning(void)
725 {
726         if (prefs_gpg_should_skip_encryption_warning(pgpinline_system.id))
727                 return NULL;
728         else
729                 return _("Please note that attachments are not encrypted by "
730                  "the PGP/Inline system, nor are email headers, like Subject.");
731 }
732
733 static void pgpinline_inhibit_encrypt_warning(gboolean inhibit)
734 {
735         if (inhibit)
736                 prefs_gpg_add_skip_encryption_warning(pgpinline_system.id);
737         else
738                 prefs_gpg_remove_skip_encryption_warning(pgpinline_system.id);
739 }
740
741 static gboolean pgpinline_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
742 {
743         MimeInfo *msgcontent;
744         FILE *fp;
745         gchar *enccontent;
746         size_t len;
747         gchar *textstr, *tmp;
748         gpgme_data_t gpgtext, gpgenc;
749         gpgme_ctx_t ctx;
750         gpgme_key_t *kset = NULL;
751         gchar **fprs = g_strsplit(encrypt_data, " ", -1);
752         gpgme_error_t err;
753         gint i = 0;
754
755         while (fprs[i] && strlen(fprs[i])) {
756                 i++;
757         }
758         
759         kset = g_malloc(sizeof(gpgme_key_t)*(i+1));
760         memset(kset, 0, sizeof(gpgme_key_t)*(i+1));
761         if ((err = gpgme_new(&ctx)) != GPG_ERR_NO_ERROR) {
762                 debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
763                 privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
764                 g_free(kset);
765                 return FALSE;
766         }
767         i = 0;
768         while (fprs[i] && strlen(fprs[i])) {
769                 gpgme_key_t key;
770                 err = gpgme_get_key(ctx, fprs[i], &key, 0);
771                 if (err) {
772                         debug_print("can't add key '%s'[%d] (%s)\n", fprs[i],i, gpgme_strerror(err));
773                         privacy_set_error(_("Couldn't add GPG key %s, %s"), fprs[i], gpgme_strerror(err));
774                         g_free(kset);
775                         return FALSE;
776                 }
777                 debug_print("found %s at %d\n", fprs[i], i);
778                 kset[i] = key;
779                 i++;
780         }
781         
782
783         debug_print("Encrypting message content\n");
784
785         /* get content node from message */
786         msgcontent = (MimeInfo *) mimeinfo->node->children->data;
787         if (msgcontent->type == MIMETYPE_MULTIPART) {
788                 if (!msgcontent->node->children) {
789                         debug_print("msgcontent->node->children NULL, bailing\n");
790                         privacy_set_error(_("Malformed message"));
791                         g_free(kset);
792                         return FALSE;
793                 }
794                 msgcontent = (MimeInfo *) msgcontent->node->children->data;
795         }
796         /* get rid of quoted-printable or anything */
797         procmime_decode_content(msgcontent);
798
799         fp = my_tmpfile();
800         if (fp == NULL) {
801                 privacy_set_error(_("Couldn't create temporary file, %s"), strerror(errno));
802                 perror("my_tmpfile");
803                 g_free(kset);
804                 return FALSE;
805         }
806         procmime_write_mimeinfo(msgcontent, fp);
807         rewind(fp);
808
809         /* read temporary file into memory */
810         textstr = fp_read_noconv(fp);
811         
812         fclose(fp);
813
814         /* encrypt data */
815         gpgme_data_new_from_mem(&gpgtext, textstr, (size_t)strlen(textstr), 0);
816         gpgme_data_new(&gpgenc);
817         if ((err = gpgme_new(&ctx)) != GPG_ERR_NO_ERROR) {
818                 debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
819                 privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
820                 g_free(kset);
821                 return FALSE;
822         }
823         gpgme_set_armor(ctx, 1);
824
825         err = gpgme_op_encrypt(ctx, kset, GPGME_ENCRYPT_ALWAYS_TRUST, gpgtext, gpgenc);
826
827         enccontent = sgpgme_data_release_and_get_mem(gpgenc, &len);
828         g_free(kset);
829
830         if (enccontent == NULL || len <= 0) {
831                 g_warning("sgpgme_data_release_and_get_mem failed");
832                 privacy_set_error(_("Encryption failed, %s"), gpgme_strerror(err));
833                 gpgme_data_release(gpgtext);
834                 g_free(textstr);
835                 gpgme_release(ctx);
836                 return FALSE;
837         }
838
839         tmp = g_malloc(len+1);
840         g_memmove(tmp, enccontent, len+1);
841         tmp[len] = '\0';
842         g_free(enccontent);
843
844         gpgme_data_release(gpgtext);
845         g_free(textstr);
846
847         if (msgcontent->content == MIMECONTENT_FILE &&
848             msgcontent->data.filename != NULL) {
849                 if (msgcontent->tmp == TRUE)
850                         claws_unlink(msgcontent->data.filename);
851                 g_free(msgcontent->data.filename);
852         }
853         msgcontent->data.mem = g_strdup(tmp);
854         msgcontent->content = MIMECONTENT_MEM;
855         g_free(tmp);
856         gpgme_release(ctx);
857
858         return TRUE;
859 }
860
861 static PrivacySystem pgpinline_system = {
862         "pgpinline",                    /* id */
863         "PGP Inline",                   /* name */
864
865         pgpinline_free_privacydata,     /* free_privacydata */
866
867         pgpinline_is_signed,            /* is_signed(MimeInfo *) */
868         pgpinline_check_signature,      /* check_signature(MimeInfo *) */
869         pgpinline_get_sig_status,       /* get_sig_status(MimeInfo *) */
870         pgpinline_get_sig_info_short,   /* get_sig_info_short(MimeInfo *) */
871         pgpinline_get_sig_info_full,    /* get_sig_info_full(MimeInfo *) */
872
873         pgpinline_is_encrypted,         /* is_encrypted(MimeInfo *) */
874         pgpinline_decrypt,              /* decrypt(MimeInfo *) */
875
876         TRUE,
877         pgpinline_sign,
878
879         TRUE,
880         pgpinline_get_encrypt_data,
881         pgpinline_encrypt,
882         pgpinline_get_encrypt_warning,
883         pgpinline_inhibit_encrypt_warning,
884 };
885
886 void pgpinline_init()
887 {
888         privacy_register_system(&pgpinline_system);
889 }
890
891 void pgpinline_done()
892 {
893         privacy_unregister_system(&pgpinline_system);
894 }
895
896 struct PluginFeature *plugin_provides(void)
897 {
898         static struct PluginFeature features[] = 
899                 { {PLUGIN_PRIVACY, N_("PGP/Inline")},
900                   {PLUGIN_NOTHING, NULL}};
901         return features;
902 }
903 #endif /* USE_GPGME */