c54952d17f4f5e23035e61390178c9620e2d625d
[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 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 #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         const gchar *begin_indicator = "-----BEGIN PGP MESSAGE-----";
383         const gchar *end_indicator = "-----END PGP MESSAGE-----";
384         gchar *pos;
385         
386         if (gpgme_new(&ctx) != GPG_ERR_NO_ERROR)
387                 return NULL;
388
389         gpgme_set_textmode(ctx, 1);
390         gpgme_set_armor(ctx, 1);
391
392         g_return_val_if_fail(mimeinfo != NULL, NULL);
393         g_return_val_if_fail(pgpinline_is_encrypted(mimeinfo), NULL);
394         
395         if (procmime_mimeinfo_parent(mimeinfo) == NULL ||
396             mimeinfo->type != MIMETYPE_TEXT) {
397                 gpgme_release(ctx);
398                 privacy_set_error(_("Couldn't parse mime part."));
399                 return NULL;
400         }
401
402         textdata = get_part_as_string(mimeinfo);
403         if (!textdata) {
404                 gpgme_release(ctx);
405                 privacy_set_error(_("Couldn't get text data."));
406                 return NULL;
407         }
408
409         debug_print("decrypting '%s'\n", textdata);
410         gpgme_data_new_from_mem(&cipher, textdata, (size_t)strlen(textdata), 1);
411
412         plain = sgpgme_decrypt_verify(cipher, &sigstat, ctx);
413         if (sigstat && !sigstat->signatures)
414                 sigstat = NULL;
415
416         gpgme_data_release(cipher);
417         
418         if (plain == NULL) {
419                 gpgme_release(ctx);
420                 return NULL;
421         }
422
423         fname = g_strdup_printf("%s%cplaintext.%08x",
424                 get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id);
425
426         if ((dstfp = g_fopen(fname, "wb")) == NULL) {
427                 FILE_OP_ERROR(fname, "fopen");
428                 privacy_set_error(_("Couldn't open decrypted file %s"), fname);
429                 g_free(fname);
430                 gpgme_data_release(plain);
431                 gpgme_release(ctx);
432                 return NULL;
433         }
434
435         src_codeset = procmime_mimeinfo_get_parameter(mimeinfo, "charset");
436         if (src_codeset == NULL)
437                 src_codeset = CS_ISO_8859_1;
438                 
439         fprintf(dstfp, "MIME-Version: 1.0\r\n"
440                         "Content-Type: text/plain; charset=%s\r\n"
441                         "Content-Transfer-Encoding: 8bit\r\n"
442                         "\r\n",
443                         src_codeset);
444
445         /* Store any part before encrypted text */
446         pos = strstr(textdata, begin_indicator);
447         if (pos != NULL && (pos - textdata) > 0) {
448             fwrite(textdata, pos - textdata, 1, dstfp);
449         }
450         
451         fwrite(_("\n--- Start of PGP/Inline encrypted data ---\n"), 
452                 strlen(_("\n--- Start of PGP/Inline encrypted data ---\n")), 
453                 1, dstfp);
454         chars = sgpgme_data_release_and_get_mem(plain, &len);
455         if (len > 0)
456                 fwrite(chars, len, 1, dstfp);
457
458         /* Store any part after encrypted text */
459         fwrite(_("--- End of PGP/Inline encrypted data ---\n"), 
460                 strlen(_("--- End of PGP/Inline encrypted data ---\n")), 
461                 1, dstfp);
462         if (pos != NULL) {
463             pos = strstr(pos, end_indicator);
464             if (pos != NULL && *pos != '\0') {
465                 pos += strlen(end_indicator);
466                 fwrite(pos, strlen(pos), 1, dstfp);
467             }
468         }
469
470         fclose(dstfp);
471         
472         parseinfo = procmime_scan_file(fname);
473         g_free(fname);
474         
475         if (parseinfo == NULL) {
476                 gpgme_release(ctx);
477                 privacy_set_error(_("Couldn't scan decrypted file."));
478                 return NULL;
479         }
480         decinfo = g_node_first_child(parseinfo->node) != NULL ?
481                 g_node_first_child(parseinfo->node)->data : NULL;
482                 
483         if (decinfo == NULL) {
484                 gpgme_release(ctx);
485                 privacy_set_error(_("Couldn't scan decrypted file parts."));
486                 return NULL;
487         }
488
489         g_node_unlink(decinfo->node);
490         procmime_mimeinfo_free_all(parseinfo);
491
492         decinfo->tmp = TRUE;
493
494         if (sigstat != GPGME_SIG_STAT_NONE) {
495                 if (decinfo->privacy != NULL) {
496                         data = (PrivacyDataPGP *) decinfo->privacy;
497                 } else {
498                         data = pgpinline_new_privacydata();
499                         decinfo->privacy = (PrivacyData *) data;        
500                 }
501                 data->done_sigtest = TRUE;
502                 data->is_signed = TRUE;
503                 data->sigstatus = sigstat;
504                 if (data->ctx)
505                         gpgme_release(data->ctx);
506                 data->ctx = ctx;
507         } else
508                 gpgme_release(ctx);
509
510         return decinfo;
511 }
512
513 static gboolean pgpinline_sign(MimeInfo *mimeinfo, PrefsAccount *account)
514 {
515         MimeInfo *msgcontent;
516         gchar *textstr, *tmp;
517         FILE *fp;
518         gchar *sigcontent;
519         gpgme_ctx_t ctx;
520         gpgme_data_t gpgtext, gpgsig;
521         size_t len;
522         gpgme_error_t err;
523         struct passphrase_cb_info_s info;
524         gpgme_sign_result_t result = NULL;
525
526         memset (&info, 0, sizeof info);
527
528         /* get content node from message */
529         msgcontent = (MimeInfo *) mimeinfo->node->children->data;
530         if (msgcontent->type == MIMETYPE_MULTIPART)
531                 msgcontent = (MimeInfo *) msgcontent->node->children->data;
532
533         /* get rid of quoted-printable or anything */
534         procmime_decode_content(msgcontent);
535
536         fp = my_tmpfile();
537         if (fp == NULL) {
538                 perror("my_tmpfile");
539                 privacy_set_error(_("Couldn't create temporary file."));
540                 return FALSE;
541         }
542         procmime_write_mimeinfo(msgcontent, fp);
543         rewind(fp);
544
545         /* read temporary file into memory */
546         textstr = fp_read_noconv(fp);
547         
548         fclose(fp);
549                 
550         gpgme_data_new_from_mem(&gpgtext, textstr, (size_t)strlen(textstr), 0);
551         gpgme_data_new(&gpgsig);
552         if ((err = gpgme_new(&ctx)) != GPG_ERR_NO_ERROR) {
553                 debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
554                 privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
555                 return FALSE;
556         }
557         gpgme_set_textmode(ctx, 1);
558         gpgme_set_armor(ctx, 1);
559
560         if (!sgpgme_setup_signers(ctx, account)) {
561                 gpgme_release(ctx);
562                 return FALSE;
563         }
564
565         prefs_gpg_enable_agent(prefs_gpg_get_config()->use_gpg_agent);
566         if (!getenv("GPG_AGENT_INFO") || !prefs_gpg_get_config()->use_gpg_agent) {
567                 info.c = ctx;
568                 gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
569         }
570
571         err = gpgme_op_sign(ctx, gpgtext, gpgsig, GPGME_SIG_MODE_CLEAR);
572         if (err != GPG_ERR_NO_ERROR) {
573                 if (err == GPG_ERR_CANCELED) {
574                         /* ignore cancelled signing */
575                         privacy_reset_error();
576                         debug_print("gpgme_op_sign cancelled\n");
577                 } else {
578                         privacy_set_error(_("Data signing failed, %s"), gpgme_strerror(err));
579                         debug_print("gpgme_op_sign error : %x\n", err);
580                 }
581                 gpgme_release(ctx);
582                 return FALSE;
583         }
584         result = gpgme_op_sign_result(ctx);
585         if (result && result->signatures) {
586                 gpgme_new_signature_t sig = result->signatures;
587                 while (sig) {
588                         debug_print("valid signature: %s\n", sig->fpr);
589                         sig = sig->next;
590                 }
591         } else if (result && result->invalid_signers) {
592                 gpgme_invalid_key_t invalid = result->invalid_signers;
593                 while (invalid) {
594                         g_warning("invalid signer: %s (%s)", invalid->fpr, 
595                                 gpgme_strerror(invalid->reason));
596                         privacy_set_error(_("Data signing failed due to invalid signer: %s"), 
597                                 gpgme_strerror(invalid->reason));
598                         invalid = invalid->next;
599                 }
600                 gpgme_release(ctx);
601                 return FALSE;
602         } else {
603                 /* can't get result (maybe no signing key?) */
604                 debug_print("gpgme_op_sign_result error\n");
605                 privacy_set_error(_("Data signing failed, no results."));
606                 gpgme_release(ctx);
607                 return FALSE;
608         }
609
610
611         sigcontent = sgpgme_data_release_and_get_mem(gpgsig, &len);
612         
613         if (sigcontent == NULL || len <= 0) {
614                 g_warning("sgpgme_data_release_and_get_mem failed");
615                 privacy_set_error(_("Data signing failed, no contents."));
616                 gpgme_data_release(gpgtext);
617                 g_free(textstr);
618                 g_free(sigcontent);
619                 gpgme_release(ctx);
620                 return FALSE;
621         }
622
623         tmp = g_malloc(len+1);
624         g_memmove(tmp, sigcontent, len+1);
625         tmp[len] = '\0';
626         gpgme_data_release(gpgtext);
627         g_free(textstr);
628         g_free(sigcontent);
629
630         if (msgcontent->content == MIMECONTENT_FILE &&
631             msgcontent->data.filename != NULL) {
632                 if (msgcontent->tmp == TRUE)
633                         g_unlink(msgcontent->data.filename);
634                 g_free(msgcontent->data.filename);
635         }
636         msgcontent->data.mem = g_strdup(tmp);
637         msgcontent->content = MIMECONTENT_MEM;
638         g_free(tmp);
639
640         /* avoid all sorts of clear-signing problems with non ascii
641          * chars
642          */
643         procmime_encode_content(msgcontent, ENC_BASE64);
644         gpgme_release(ctx);
645
646         return TRUE;
647 }
648
649 static gchar *pgpinline_get_encrypt_data(GSList *recp_names)
650 {
651         return sgpgme_get_encrypt_data(recp_names, GPGME_PROTOCOL_OpenPGP);
652 }
653
654 static const gchar *pgpinline_get_encrypt_warning(void)
655 {
656         if (prefs_gpg_should_skip_encryption_warning(pgpinline_system.id))
657                 return NULL;
658         else
659                 return _("Please note that attachments are not encrypted by "
660                  "the PGP/Inline system, nor are email headers, like Subject.");
661 }
662
663 static void pgpinline_inhibit_encrypt_warning(gboolean inhibit)
664 {
665         if (inhibit)
666                 prefs_gpg_add_skip_encryption_warning(pgpinline_system.id);
667         else
668                 prefs_gpg_remove_skip_encryption_warning(pgpinline_system.id);
669 }
670
671 static gboolean pgpinline_encrypt(MimeInfo *mimeinfo, const gchar *encrypt_data)
672 {
673         MimeInfo *msgcontent;
674         FILE *fp;
675         gchar *enccontent;
676         size_t len;
677         gchar *textstr, *tmp;
678         gpgme_data_t gpgtext, gpgenc;
679         gpgme_ctx_t ctx;
680         gpgme_key_t *kset = NULL;
681         gchar **fprs = g_strsplit(encrypt_data, " ", -1);
682         gpgme_error_t err;
683         gint i = 0;
684
685         while (fprs[i] && strlen(fprs[i])) {
686                 i++;
687         }
688         
689         kset = g_malloc(sizeof(gpgme_key_t)*(i+1));
690         memset(kset, 0, sizeof(gpgme_key_t)*(i+1));
691         if ((err = gpgme_new(&ctx)) != GPG_ERR_NO_ERROR) {
692                 debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
693                 privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
694                 return FALSE;
695         }
696         i = 0;
697         while (fprs[i] && strlen(fprs[i])) {
698                 gpgme_key_t key;
699                 err = gpgme_get_key(ctx, fprs[i], &key, 0);
700                 if (err) {
701                         debug_print("can't add key '%s'[%d] (%s)\n", fprs[i],i, gpgme_strerror(err));
702                         privacy_set_error(_("Couldn't add GPG key %s, %s"), fprs[i], gpgme_strerror(err));
703                         return FALSE;
704                 }
705                 debug_print("found %s at %d\n", fprs[i], i);
706                 kset[i] = key;
707                 i++;
708         }
709         
710
711         debug_print("Encrypting message content\n");
712
713         /* get content node from message */
714         msgcontent = (MimeInfo *) mimeinfo->node->children->data;
715         if (msgcontent->type == MIMETYPE_MULTIPART)
716                 msgcontent = (MimeInfo *) msgcontent->node->children->data;
717
718         /* get rid of quoted-printable or anything */
719         procmime_decode_content(msgcontent);
720
721         fp = my_tmpfile();
722         if (fp == NULL) {
723                 privacy_set_error(_("Couldn't create temporary file, %s"), strerror(errno));
724                 perror("my_tmpfile");
725                 return FALSE;
726         }
727         procmime_write_mimeinfo(msgcontent, fp);
728         rewind(fp);
729
730         /* read temporary file into memory */
731         textstr = fp_read_noconv(fp);
732         
733         fclose(fp);
734
735         /* encrypt data */
736         gpgme_data_new_from_mem(&gpgtext, textstr, (size_t)strlen(textstr), 0);
737         gpgme_data_new(&gpgenc);
738         if ((err = gpgme_new(&ctx)) != GPG_ERR_NO_ERROR) {
739                 debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
740                 privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
741                 return FALSE;
742         }
743         gpgme_set_armor(ctx, 1);
744
745         err = gpgme_op_encrypt(ctx, kset, GPGME_ENCRYPT_ALWAYS_TRUST, gpgtext, gpgenc);
746
747         enccontent = sgpgme_data_release_and_get_mem(gpgenc, &len);
748
749         if (enccontent == NULL || len <= 0) {
750                 g_warning("sgpgme_data_release_and_get_mem failed");
751                 privacy_set_error(_("Encryption failed, %s"), gpgme_strerror(err));
752                 gpgme_data_release(gpgtext);
753                 g_free(textstr);
754                 gpgme_release(ctx);
755                 return FALSE;
756         }
757
758         tmp = g_malloc(len+1);
759         g_memmove(tmp, enccontent, len+1);
760         tmp[len] = '\0';
761         g_free(enccontent);
762
763         gpgme_data_release(gpgtext);
764         g_free(textstr);
765
766         if (msgcontent->content == MIMECONTENT_FILE &&
767             msgcontent->data.filename != NULL) {
768                 if (msgcontent->tmp == TRUE)
769                         g_unlink(msgcontent->data.filename);
770                 g_free(msgcontent->data.filename);
771         }
772         msgcontent->data.mem = g_strdup(tmp);
773         msgcontent->content = MIMECONTENT_MEM;
774         g_free(tmp);
775         gpgme_release(ctx);
776
777         return TRUE;
778 }
779
780 static PrivacySystem pgpinline_system = {
781         "pgpinline",                    /* id */
782         "PGP Inline",                   /* name */
783
784         pgpinline_free_privacydata,     /* free_privacydata */
785
786         pgpinline_is_signed,            /* is_signed(MimeInfo *) */
787         pgpinline_check_signature,      /* check_signature(MimeInfo *) */
788         pgpinline_get_sig_status,       /* get_sig_status(MimeInfo *) */
789         pgpinline_get_sig_info_short,   /* get_sig_info_short(MimeInfo *) */
790         pgpinline_get_sig_info_full,    /* get_sig_info_full(MimeInfo *) */
791
792         pgpinline_is_encrypted,         /* is_encrypted(MimeInfo *) */
793         pgpinline_decrypt,              /* decrypt(MimeInfo *) */
794
795         TRUE,
796         pgpinline_sign,
797
798         TRUE,
799         pgpinline_get_encrypt_data,
800         pgpinline_encrypt,
801         pgpinline_get_encrypt_warning,
802         pgpinline_inhibit_encrypt_warning,
803 };
804
805 void pgpinline_init()
806 {
807         privacy_register_system(&pgpinline_system);
808 }
809
810 void pgpinline_done()
811 {
812         privacy_unregister_system(&pgpinline_system);
813 }
814
815 struct PluginFeature *plugin_provides(void)
816 {
817         static struct PluginFeature features[] = 
818                 { {PLUGIN_PRIVACY, N_("PGP/Inline")},
819                   {PLUGIN_NOTHING, NULL}};
820         return features;
821 }
822 #endif /* USE_GPGME */