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