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