Fix possible resource leak
[claws.git] / src / autofaces.c
1
2 /*
3  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
4  * Copyright (C) 2008-2012 Ricardo Mones and 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 #ifdef HAVE_CONFIG_H
22 #  include "config.h"
23 #include "claws-features.h"
24 #endif
25
26 #include "defs.h"
27
28 #include "utils.h"
29 #include "autofaces.h"
30
31 static gint get_content_for_any_face(gchar *buf, gint len, gchar *anyname, gint maxlen)
32 {
33         FILE  *xfp;
34         gchar *xfile;
35         gint  lastc;
36
37         xfile = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, AUTOFACES_DIR,
38                             G_DIR_SEPARATOR_S, anyname, NULL);
39         buf[0] = '\0';
40         if ((xfp = g_fopen(xfile, "rb")) == NULL) {
41                 g_free(xfile);
42                 debug_print("header content file '%s' not found\n", anyname);
43                 return -1;
44         }
45         if (fgets(buf, (len < maxlen)? len: maxlen, xfp) == NULL) {
46                 fclose(xfp);
47                 g_free(xfile);
48                 g_warning("header content file '%s' read failure", anyname);
49                 return -2;
50         }
51         lastc = strlen(buf) - 1;        /* remove trailing \n */
52         buf[lastc] = (buf[lastc] == '\n')? '\0': buf[lastc];
53         fclose(xfp);
54         g_free(xfile);
55
56         return 0;
57 }
58
59 static gchar * get_any_face_filename_for_account(gchar *facetype, gchar *accountname)
60 {
61         gchar *name = NULL;
62         gchar *what = NULL;
63         if (facetype == NULL || accountname == NULL) 
64                 return NULL;
65         if (*facetype == '\0' || *accountname == '\0') 
66                 return NULL;
67         what = name = g_strdup_printf("%s.%s", facetype, accountname);
68         while (*what) {
69                 switch (*what) {
70                 case '/':
71                 case '\\':
72                 case '<':
73                 case '>':
74                 case ':':
75                 case '?':
76                 case '*':
77                         *what = '_';
78                         break;
79                 default:
80                         if (*what <= ' ') {
81                                 *what = '_';
82                         }
83                         break;
84                 }
85                 ++what;
86         }
87         return name;
88 }
89
90 gint get_default_xface(gchar *buf, gint len) {
91         return get_content_for_any_face(buf, len, "xface", MAX_XFACE_LEN);
92 }
93
94 gint get_account_xface(gchar *buf, gint len, gchar *name) {
95         gchar *filename = get_any_face_filename_for_account("xface", name);
96         if (filename) {
97                 gint result = get_content_for_any_face(buf, len, filename, MAX_XFACE_LEN);
98                 g_free(filename);
99                 return result;
100         }
101         g_warning("header xface filename invalid");
102         return -1;
103 }
104
105 gint get_default_face(gchar *buf, gint len) {
106         return get_content_for_any_face(buf, len, "face", MAX_FACE_LEN);
107 }
108
109 gint get_account_face(gchar *buf, gint len, gchar *name) {
110         gchar *filename = get_any_face_filename_for_account("face", name);
111         if (filename) {
112                 gint result = get_content_for_any_face(buf, len, filename, MAX_FACE_LEN);
113                 g_free(filename);
114                 return result;
115         }
116         g_warning("header face filename invalid");
117         return -1;
118 }
119