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