9d53d883ed085f8ebc380e16ac1a1395c5865554
[claws.git] / src / common / template.c
1 /*
2  * Claws Mail templates subsystem 
3  * Copyright (C) 2001 Alexander Barinov
4  * Copyright (C) 2001-2018 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 #include "defs.h"
22
23 #include <glib.h>
24 #include <glib/gi18n.h>
25 #include <stdio.h>
26 #include <sys/stat.h>
27 #include <ctype.h>
28
29 #include "utils.h"
30 #include "template.h"
31 #include "codeconv.h"
32
33 static GSList *template_list;
34
35 static Template *template_load(gchar *filename)
36 {
37         Template *tmpl;
38         FILE *fp;
39         gchar buf[BUFFSIZE];
40         gint bytes_read;
41
42         if ((fp = g_fopen(filename, "rb")) == NULL) {
43                 FILE_OP_ERROR(filename, "fopen");
44                 return NULL;
45         }
46
47         tmpl = g_new(Template, 1);
48         tmpl->load_filename = g_strdup(filename);;
49         tmpl->name = NULL;
50         tmpl->subject = NULL;
51         tmpl->from = NULL;
52         tmpl->to = NULL;
53         tmpl->cc = NULL;        
54         tmpl->bcc = NULL;
55         tmpl->replyto = NULL;
56         tmpl->value = NULL;
57
58         while (fgets(buf, sizeof(buf), fp) != NULL) {
59                 if (buf[0] == '\n')
60                         break;
61                 else if (!g_ascii_strncasecmp(buf, "Name:", 5))
62                         tmpl->name = g_strdup(g_strstrip(buf + 5));
63                 else if (!g_ascii_strncasecmp(buf, "From:", 5))
64                         tmpl->from = g_strdup(g_strstrip(buf + 5));
65                 else if (!g_ascii_strncasecmp(buf, "To:", 3))
66                         tmpl->to = g_strdup(g_strstrip(buf + 3));
67                 else if (!g_ascii_strncasecmp(buf, "Cc:", 3))
68                         tmpl->cc = g_strdup(g_strstrip(buf + 3));
69                 else if (!g_ascii_strncasecmp(buf, "Bcc:", 4))
70                         tmpl->bcc = g_strdup(g_strstrip(buf + 4));                                              
71                 else if (!g_ascii_strncasecmp(buf, "Reply-To:", 9))
72                         tmpl->replyto = g_strdup(g_strstrip(buf + 9));                                          
73                 else if (!g_ascii_strncasecmp(buf, "Subject:", 8))
74                         tmpl->subject = g_strdup(g_strstrip(buf + 8));
75         }
76
77         if (!tmpl->name) {
78                 g_warning("wrong template format");
79                 template_free(tmpl);
80                 fclose(fp);
81                 return NULL;
82         }
83
84         if ((bytes_read = fread(buf, 1, sizeof(buf), fp)) == 0) {
85                 if (ferror(fp)) {
86                         FILE_OP_ERROR(filename, "fread");
87                         template_free(tmpl);
88                         fclose(fp);
89                         return NULL;
90                 }
91         }
92         fclose(fp);
93         tmpl->value = g_strndup(buf, bytes_read);
94
95         return tmpl;
96 }
97
98 void template_free(Template *tmpl)
99 {
100         g_free(tmpl->load_filename);
101         g_free(tmpl->name);
102         g_free(tmpl->subject);
103         g_free(tmpl->from);
104         g_free(tmpl->to);
105         g_free(tmpl->cc);
106         g_free(tmpl->bcc);              
107         g_free(tmpl->replyto);          
108         g_free(tmpl->value);
109         g_free(tmpl);
110 }
111
112 static void template_clear_config(GSList *tmpl_list)
113 {
114         GSList *cur;
115         Template *tmpl;
116
117         for (cur = tmpl_list; cur != NULL; cur = cur->next) {
118                 tmpl = (Template *)cur->data;
119                 template_free(tmpl);
120         }
121         g_slist_free(tmpl_list);
122 }
123
124 static gint tmpl_compare(gconstpointer tmpl1, gconstpointer tmpl2)
125 {
126         gchar *basename1, *basename2;
127         long filenum1, filenum2;
128         gint ret = 0;
129
130         if ((Template *)tmpl1 == NULL || (Template *)tmpl2 == NULL)
131                 return 0;
132
133         if (((Template *)tmpl1)->load_filename == NULL || ((Template *)tmpl2)->load_filename == NULL)
134                 return 0;
135
136         basename1 = g_path_get_basename(((Template *)tmpl1)->load_filename);
137         basename2 = g_path_get_basename(((Template *)tmpl2)->load_filename);
138         filenum1 = atol(basename1);
139         filenum2 = atol(basename2);
140         g_free(basename1);
141         g_free(basename2);
142
143         if (filenum1 == 0 || filenum2 == 0)
144                 return 0;
145
146         if (filenum1 < filenum2)
147                 ret = -1;
148         else
149                 if (filenum1 > filenum2)
150                         ret = 1;
151                         
152         return ret;
153 }
154
155 GSList *template_read_config(void)
156 {
157         const gchar *path;
158         gchar *filename;
159         GDir *dir;
160         const gchar *dir_name;
161         GStatBuf s;
162         Template *tmpl;
163         GSList *tmpl_list = NULL;
164
165         path = get_template_dir();
166         debug_print("%s:%d reading templates dir %s\n",
167                     __FILE__, __LINE__, path);
168
169         if (!is_dir_exist(path)) {
170                 if (make_dir(path) < 0)
171                         return NULL;
172         }
173
174         if ((dir = g_dir_open(path, 0, NULL)) == NULL) {
175                 g_warning("failed to open directory: '%s'", path);
176                 return NULL;
177         }
178
179         while ((dir_name = g_dir_read_name(dir)) != NULL) {
180                 filename = g_strconcat(path, G_DIR_SEPARATOR_S,
181                                        dir_name, NULL);
182
183                 if (g_stat(filename, &s) != 0 || !S_ISREG(s.st_mode) ) {
184                         debug_print("%s:%d %s is not an ordinary file\n",
185                                     __FILE__, __LINE__, filename);
186                         continue;
187                 }
188
189                 tmpl = template_load(filename);
190                 if (tmpl)
191                         tmpl_list = g_slist_insert_sorted(tmpl_list, tmpl, tmpl_compare);
192
193                 g_free(filename);
194         }
195
196         g_dir_close(dir);
197
198         return tmpl_list;
199 }
200
201 #define TRY(func) { \
202 if (!(func)) \
203 { \
204         g_warning("Failed to write template to file"); \
205         if (fp) fclose(fp); \
206         if (new) claws_unlink(new); \
207         g_free(new); \
208         g_free(filename); \
209         return; \
210 } \
211 }
212
213 #define TRY_NO_CLOSE(func) { \
214 if (!(func)) \
215 { \
216         g_warning("Failed to write template to file"); \
217         if (new) claws_unlink(new); \
218         g_free(new); \
219         g_free(filename); \
220         return; \
221 } \
222 }
223
224 static void template_write_config(GSList *tmpl_list)
225 {
226         const gchar *path;
227         GSList *cur;
228         Template *tmpl;
229         FILE *fp;
230         gint tmpl_num;
231
232         debug_print("%s:%d writing templates\n", __FILE__, __LINE__);
233
234         path = get_template_dir();
235
236         if (!is_dir_exist(path)) {
237                 if (is_file_exist(path)) {
238                         g_warning("file '%s' already exists", path);
239                         return;
240                 }
241                 if (make_dir(path) < 0)
242                         return;
243         }
244
245         for (cur = tmpl_list, tmpl_num = 1; cur != NULL;
246              cur = cur->next, tmpl_num++) {
247                 gchar *filename, *new = NULL;
248
249                 tmpl = cur->data;
250
251                 filename = g_strconcat(path, G_DIR_SEPARATOR_S,
252                                        itos(tmpl_num), NULL);
253
254                 if (is_file_exist(filename)) {
255                         new = g_strconcat(filename, ".new", NULL);
256                 }
257
258                 if ((fp = g_fopen(new?new:filename, "wb")) == NULL) {
259                         FILE_OP_ERROR(new?new:filename, "fopen");
260                         g_free(new);
261                         g_free(filename);
262                         return;
263                 }
264
265                 TRY(fprintf(fp, "Name: %s\n", tmpl->name) > 0);
266                 if (tmpl->subject && *tmpl->subject != '\0')
267                         TRY(fprintf(fp, "Subject: %s\n", tmpl->subject) > 0);
268                 if (tmpl->from && *tmpl->from != '\0')
269                         TRY(fprintf(fp, "From: %s\n", tmpl->from) > 0);
270                 if (tmpl->to && *tmpl->to != '\0')
271                         TRY(fprintf(fp, "To: %s\n", tmpl->to) > 0);
272                 if (tmpl->cc && *tmpl->cc != '\0')
273                         TRY(fprintf(fp, "Cc: %s\n", tmpl->cc) > 0);
274                 if (tmpl->bcc && *tmpl->bcc != '\0')
275                         TRY(fprintf(fp, "Bcc: %s\n", tmpl->bcc) > 0);
276                 if (tmpl->replyto && *tmpl->replyto != '\0')
277                         TRY(fprintf(fp, "Reply-To: %s\n", tmpl->replyto) > 0);
278
279                 TRY(fputs("\n", fp) != EOF);
280
281                 if (tmpl->value && *tmpl->value != '\0') {
282                         TRY(fwrite(tmpl->value, sizeof(gchar), strlen(tmpl->value), fp) == strlen(tmpl->value));
283                 } else {
284                         TRY(fwrite("", sizeof(gchar), 1, fp) == 1);
285                 }
286                 TRY_NO_CLOSE(fclose(fp) != EOF);
287
288                 if (new) {
289                         if (rename_force(new, filename) < 0) {
290                                 FILE_OP_ERROR(new, "rename");
291                         }
292                 }
293                 g_free(new);
294                 g_free(filename);
295         }
296         
297         /* remove other templates */
298         while (TRUE) {
299                 gchar *filename = g_strconcat(path, G_DIR_SEPARATOR_S,
300                                        itos(tmpl_num), NULL);
301                 if (is_file_exist(filename)) {
302                         debug_print("removing old template %d\n", tmpl_num);
303                         claws_unlink(filename);
304                         g_free(filename);
305                 } else {
306                         g_free(filename);
307                         break;
308                 }
309                 tmpl_num++;
310         }
311 }
312
313 GSList *template_get_config(void)
314 {
315         if (!template_list)
316                 template_list = template_read_config();
317
318         return template_list;
319 }
320
321 void template_set_config(GSList *tmpl_list)
322 {
323         template_clear_config(template_list);
324         template_write_config(tmpl_list);
325         template_list = tmpl_list;
326 }