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