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