Add a plugin method to allow updating stored passwords on master password change.
[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
34 gchar *fp_read_noconv(FILE *fp)
35 {
36         GByteArray *array;
37         guchar buf[BUFSIZ];
38         gint n_read;
39         gchar *result = NULL;
40
41         if (!fp)
42                 return NULL;
43         array = g_byte_array_new();
44
45         while ((n_read = fread(buf, sizeof(gchar), sizeof(buf), fp)) > 0) {
46                 if (n_read < sizeof(buf) && ferror(fp))
47                         break;
48                 g_byte_array_append(array, buf, n_read);
49         }
50
51         if (ferror(fp)) {
52                 FILE_OP_ERROR("file stream", "fread");
53                 g_byte_array_free(array, TRUE);
54                 return NULL;
55         }
56
57         buf[0] = '\0';
58         g_byte_array_append(array, buf, 1);
59         result = (gchar *)array->data;
60         g_byte_array_free(array, FALSE);
61         
62         return result;
63 }
64
65 gchar *get_part_as_string(MimeInfo *mimeinfo)
66 {
67         gchar *textdata = NULL;
68         gchar *filename = NULL;
69         FILE *fp;
70
71         cm_return_val_if_fail(mimeinfo != NULL, 0);
72         procmime_decode_content(mimeinfo);
73         
74         if (mimeinfo->content == MIMECONTENT_MEM)
75                 textdata = g_strdup(mimeinfo->data.mem);
76         else {
77                 filename = procmime_get_tmp_file_name(mimeinfo);
78                 if (procmime_get_part(filename, mimeinfo) < 0) {
79                         printf("error dumping file\n");
80                         return NULL;
81                 }
82                 fp = g_fopen(filename,"rb");
83                 if (!fp) {
84                         printf("error reading file\n");
85                         return NULL;
86                 }
87                 textdata = fp_read_noconv(fp);
88                 fclose(fp);
89                 g_unlink(filename);
90                 g_free(filename);
91         }
92
93         if (!g_utf8_validate(textdata, -1, NULL)) {
94                 gchar *tmp = NULL;
95                 codeconv_set_strict(TRUE);
96                 if (procmime_mimeinfo_get_parameter(mimeinfo, "charset")) {
97                         tmp = conv_codeset_strdup(textdata,
98                                 procmime_mimeinfo_get_parameter(mimeinfo, "charset"),
99                                 CS_UTF_8);
100                 }
101                 if (!tmp) {
102                         tmp = conv_codeset_strdup(textdata,
103                                 conv_get_locale_charset_str_no_utf8(), 
104                                 CS_UTF_8);
105                 }
106                 codeconv_set_strict(FALSE);
107                 if (!tmp) {
108                         tmp = conv_codeset_strdup(textdata,
109                                 conv_get_locale_charset_str_no_utf8(), 
110                                 CS_UTF_8);
111                 }
112                 if (tmp) {
113                         g_free(textdata);
114                         textdata = tmp;
115                 }
116         }
117
118         return textdata;        
119 }
120
121 gchar *pgp_locate_armor_header(gchar *textdata, const gchar *armor_header)
122 {
123         gchar *pos;
124
125         pos = strstr(textdata, armor_header);
126         /*
127          * It's only a valid armor header if it's at the
128          * beginning of the buffer or a new line.
129          */
130         if (pos != NULL && (pos == textdata || *(pos-1) == '\n'))
131         {
132               return pos;
133         }
134         return NULL;
135 }
136 #endif /* USE_GPGME */