8d3e95cd209f0214906b5ff3caebad4d6ad5b10a
[claws.git] / src / plugins / pgpcore / pgp_utils.c
1
2 #ifdef HAVE_CONFIG_H
3 #  include "config.h"
4 #include "claws-features.h"
5 #endif
6
7 /*
8  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
9  * Copyright (C) 1999-2013 Colin Leroy <colin@colino.net> and 
10  * the Claws Mail team
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program. If not, see <http://www.gnu.org/licenses/>.
24  * 
25  */
26
27 #ifdef USE_GPGME
28
29 #include <glib.h>
30
31 #include "pgp_utils.h"
32 #include "codeconv.h"
33 #include "claws_io.h"
34
35 gchar *fp_read_noconv(FILE *fp)
36 {
37         GByteArray *array;
38         guchar buf[BUFSIZ];
39         gint n_read;
40         gchar *result = NULL;
41
42         if (!fp)
43                 return NULL;
44         array = g_byte_array_new();
45
46         while ((n_read = claws_fread(buf, sizeof(gchar), sizeof(buf), fp)) > 0) {
47                 if (n_read < sizeof(buf) && claws_ferror(fp))
48                         break;
49                 g_byte_array_append(array, buf, n_read);
50         }
51
52         if (claws_ferror(fp)) {
53                 FILE_OP_ERROR("file stream", "claws_fread");
54                 g_byte_array_free(array, TRUE);
55                 return NULL;
56         }
57
58         buf[0] = '\0';
59         g_byte_array_append(array, buf, 1);
60         result = (gchar *)array->data;
61         g_byte_array_free(array, FALSE);
62         
63         return result;
64 }
65
66 gchar *get_part_as_string(MimeInfo *mimeinfo)
67 {
68         gchar *textdata = NULL;
69         gchar *filename = NULL;
70         FILE *fp;
71
72         cm_return_val_if_fail(mimeinfo != NULL, 0);
73         procmime_decode_content(mimeinfo);
74         
75         if (mimeinfo->content == MIMECONTENT_MEM)
76                 textdata = g_strdup(mimeinfo->data.mem);
77         else {
78                 filename = procmime_get_tmp_file_name(mimeinfo);
79                 if (procmime_get_part(filename, mimeinfo) < 0) {
80                         g_warning("error dumping temporary file '%s'", filename);
81                         g_free(filename);
82                         return NULL;
83                 }
84                 fp = claws_fopen(filename,"rb");
85                 if (!fp) {
86                         g_warning("error opening temporary file '%s'", filename);
87                         g_free(filename);
88                         return NULL;
89                 }
90                 textdata = fp_read_noconv(fp);
91                 claws_fclose(fp);
92                 g_unlink(filename);
93                 g_free(filename);
94         }
95
96         if (!g_utf8_validate(textdata, -1, NULL)) {
97                 gchar *tmp = NULL;
98                 codeconv_set_strict(TRUE);
99                 if (procmime_mimeinfo_get_parameter(mimeinfo, "charset")) {
100                         tmp = conv_codeset_strdup(textdata,
101                                 procmime_mimeinfo_get_parameter(mimeinfo, "charset"),
102                                 CS_UTF_8);
103                 }
104                 if (!tmp) {
105                         tmp = conv_codeset_strdup(textdata,
106                                 conv_get_locale_charset_str_no_utf8(), 
107                                 CS_UTF_8);
108                 }
109                 codeconv_set_strict(FALSE);
110                 if (!tmp) {
111                         tmp = conv_codeset_strdup(textdata,
112                                 conv_get_locale_charset_str_no_utf8(), 
113                                 CS_UTF_8);
114                 }
115                 if (tmp) {
116                         g_free(textdata);
117                         textdata = tmp;
118                 }
119         }
120
121         return textdata;        
122 }
123
124 gchar *pgp_locate_armor_header(gchar *textdata, const gchar *armor_header)
125 {
126         gchar *pos;
127
128         pos = strstr(textdata, armor_header);
129         /*
130          * It's only a valid armor header if it's at the
131          * beginning of the buffer or a new line.
132          */
133         if (pos != NULL && (pos == textdata || *(pos-1) == '\n'))
134         {
135               return pos;
136         }
137         return NULL;
138 }
139 #endif /* USE_GPGME */