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