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