Mercurial > hg
annotate mcabber/src/settings.c @ 1058:c0d44a9a99bc
Code cleanup
Cosmetics. Mostly get rid of "jid" variables, as it is a structure pointer
defined in libjabber/jabber.h.
author | Mikael Berthe <mikael@lilotux.net> |
---|---|
date | Thu, 30 Nov 2006 19:51:09 +0100 |
parents | b57a01ffeed6 |
children | 230dca34dbea |
rev | line source |
---|---|
279 | 1 /* |
2 * settings.c -- Configuration stuff | |
393 | 3 * |
699 | 4 * Copyright (C) 2005, 2006 Mikael Berthe <bmikael@lists.lilotux.net> |
279 | 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 (at | |
9 * your option) any later version. | |
10 * | |
11 * This program is distributed in the hope that it will be useful, but | |
12 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |
19 * USA | |
20 */ | |
21 | |
22 #include <strings.h> | |
23 #include <stdlib.h> | |
280
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
24 #include <ctype.h> |
279 | 25 |
26 #include "settings.h" | |
341
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
27 #include "commands.h" |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
28 #include "utils.h" |
378
2e6c7b1440d1
Improve debugging/logging
Mikael Berthe <mikael@lilotux.net>
parents:
374
diff
changeset
|
29 #include "logprint.h" |
279 | 30 |
31 static GSList *option; | |
32 static GSList *alias; | |
33 static GSList *binding; | |
34 | |
35 | |
36 typedef struct { | |
37 gchar *name; | |
38 gchar *value; | |
39 } T_setting; | |
40 | |
336
eb994ee40029
Make some functions static
Mikael Berthe <mikael@lilotux.net>
parents:
294
diff
changeset
|
41 static inline GSList **get_list_ptr(guint type) |
279 | 42 { |
43 if (type == SETTINGS_TYPE_OPTION) return &option; | |
44 else if (type == SETTINGS_TYPE_ALIAS) return &alias; | |
45 else if (type == SETTINGS_TYPE_BINDING) return &binding; | |
46 return NULL; | |
47 } | |
48 | |
49 // Return a pointer to the node with the requested key, or NULL if none found | |
336
eb994ee40029
Make some functions static
Mikael Berthe <mikael@lilotux.net>
parents:
294
diff
changeset
|
50 static GSList *settings_find(GSList *list, const gchar *key) |
279 | 51 { |
52 GSList *ptr; | |
393 | 53 |
279 | 54 if (!list) return NULL; |
55 | |
56 for (ptr = list ; ptr; ptr = g_slist_next(ptr)) | |
57 if (!strcasecmp(key, ((T_setting*)ptr->data)->name)) | |
58 break; | |
59 | |
60 return ptr; | |
61 } | |
62 | |
63 /* -- */ | |
64 | |
341
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
65 // cfg_read_file(filename) |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
66 // Read and parse config file "filename". If filename is NULL, |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
67 // try to open the configuration file at the default locations. |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
68 // |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
69 int cfg_read_file(char *filename) |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
70 { |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
71 FILE *fp; |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
72 char *buf; |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
73 char *line, *eol; |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
74 unsigned int ln = 0; |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
75 int err = 0; |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
76 |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
77 if (!filename) { |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
78 // Use default config file locations |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
79 char *home = getenv("HOME"); |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
80 if (!home) { |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
81 scr_LogPrint(LPRINT_LOG, "Can't find home dir!"); |
341
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
82 fprintf(stderr, "Can't find home dir!\n"); |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
83 return -1; |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
84 } |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
85 filename = g_new(char, strlen(home)+24); |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
86 sprintf(filename, "%s/.mcabber/mcabberrc", home); |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
87 if ((fp = fopen(filename, "r")) == NULL) { |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
88 // 2nd try... |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
89 sprintf(filename, "%s/.mcabberrc", home); |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
90 if ((fp = fopen(filename, "r")) == NULL) { |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
91 fprintf(stderr, "Cannot open config file!\n"); |
895 | 92 g_free(filename); |
341
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
93 return -1; |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
94 } |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
95 } |
362
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
341
diff
changeset
|
96 // Check configuration file permissions |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
341
diff
changeset
|
97 // As it could contain sensitive data, we make it user-readable only |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
341
diff
changeset
|
98 checkset_perm(filename, TRUE); |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
341
diff
changeset
|
99 // Check mcabber dir. There we just warn, we don't change the modes |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
341
diff
changeset
|
100 sprintf(filename, "%s/.mcabber/", home); |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
341
diff
changeset
|
101 checkset_perm(filename, FALSE); |
341
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
102 g_free(filename); |
362
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
341
diff
changeset
|
103 } else { |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
341
diff
changeset
|
104 if ((fp = fopen(filename, "r")) == NULL) { |
780
1dd19363c73e
cfg_read_file(): Clarify error message
Mikael Berthe <mikael@lilotux.net>
parents:
774
diff
changeset
|
105 perror("Cannot open configuration file"); |
872
a0ddc43b421e
Don't stop when there is no configuration file
Mikael Berthe <mikael@lilotux.net>
parents:
867
diff
changeset
|
106 return -2; |
362
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
341
diff
changeset
|
107 } |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
341
diff
changeset
|
108 // Check configuration file permissions (see above) |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
341
diff
changeset
|
109 checkset_perm(filename, TRUE); |
341
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
110 } |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
111 |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
112 buf = g_new(char, 512); |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
113 |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
114 while (fgets(buf+1, 511, fp) != NULL) { |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
115 // The first char is reserved to add a '/', to make a command line |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
116 line = buf+1; |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
117 ln++; |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
118 |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
119 // Strip leading spaces |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
120 while (isspace(*line)) |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
121 line++; |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
122 |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
123 // Make eol point to the last char of the line |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
124 for (eol = line ; *eol ; eol++) |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
125 ; |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
126 if (eol > line) |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
127 eol--; |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
128 |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
129 // Strip trailing spaces |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
130 while (eol > line && isspace(*eol)) |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
131 *eol-- = 0; |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
132 |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
133 // Ignore empty lines and comments |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
134 if ((*line == '\n') || (*line == '\0') || (*line == '#')) |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
135 continue; |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
136 |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
137 if ((strchr(line, '=') != NULL)) { |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
138 // Only accept the set, alias and bind commands |
967 | 139 if (strncmp(line, "set ", strlen("set ")) && |
140 strncmp(line, "bind ", strlen("bind ")) && | |
141 strncmp(line, "alias ", strlen("alias "))) { | |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
142 scr_LogPrint(LPRINT_LOGNORM, |
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
143 "Error in configuration file (l. %d): bad command", ln); |
341
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
144 err++; |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
145 continue; |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
146 } |
967 | 147 // Set the leading COMMAND_CHAR to build a command line |
148 // and process the command | |
149 *(--line) = COMMAND_CHAR; | |
150 process_command(line); | |
341
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
151 } else { |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
152 scr_LogPrint(LPRINT_LOGNORM, |
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
153 "Error in configuration file (l. %d): no assignment", ln); |
341
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
154 err++; |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
155 } |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
156 } |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
157 g_free(buf); |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
158 fclose(fp); |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
159 return err; |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
160 } |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
161 |
280
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
162 // parse_assigment(assignment, pkey, pval) |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
163 // Read assignment and split it to key, value |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
164 // |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
165 // If this is an assignment, the function will return TRUE and |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
166 // set *pkey and *pval (*pval is set to NULL if value field is empty). |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
167 // |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
168 // If this isn't a assignment (no = char), the function will set *pval |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
169 // to NULL and return FALSE. |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
170 // |
774
46304b773a44
Remove useless checks before g_free() calls
Mikael Berthe <mikael@lilotux.net>
parents:
764
diff
changeset
|
171 // The caller should g_free() *pkey and *pval (if not NULL) after use. |
281
f562b9af2de7
Add "const" specifier in prototypes
Mikael Berthe <mikael@lilotux.net>
parents:
280
diff
changeset
|
172 guint parse_assigment(gchar *assignment, const gchar **pkey, const gchar **pval) |
280
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
173 { |
293
d0295e735768
Assignment lvalue (key, option name, alias...) cannnot contain spaces
Mikael Berthe <mikael@lilotux.net>
parents:
288
diff
changeset
|
174 char *key, *val, *t, *p; |
280
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
175 |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
176 *pkey = *pval = NULL; |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
177 |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
178 key = assignment; |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
179 // Remove leading spaces in option name |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
180 while ((!isalnum(*key)) && (*key != '=') && *key) { |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
181 //if (!isblank(*key)) |
293
d0295e735768
Assignment lvalue (key, option name, alias...) cannnot contain spaces
Mikael Berthe <mikael@lilotux.net>
parents:
288
diff
changeset
|
182 // scr_LogPrint("Error in assignment parsing!"); |
280
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
183 key++; |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
184 } |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
185 if (!*key) return FALSE; // Empty assignment |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
186 |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
187 if (*key == '=') { |
293
d0295e735768
Assignment lvalue (key, option name, alias...) cannnot contain spaces
Mikael Berthe <mikael@lilotux.net>
parents:
288
diff
changeset
|
188 //scr_LogPrint("Cannot parse assignment!"); |
280
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
189 return FALSE; |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
190 } |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
191 // Ok, key points to the option name |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
192 |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
193 for (val = key+1 ; *val && (*val != '=') ; val++) |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
194 if (!isalnum(*val) && !isblank(*val) && (*val != '_') && (*val != '-')) { |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
195 // Key should only have alnum chars... |
293
d0295e735768
Assignment lvalue (key, option name, alias...) cannnot contain spaces
Mikael Berthe <mikael@lilotux.net>
parents:
288
diff
changeset
|
196 //scr_LogPrint("Error in assignment parsing!"); |
280
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
197 return FALSE; |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
198 } |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
199 // Remove trailing spaces in option name: |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
200 for (t = val-1 ; t > key && isblank(*t) ; t--) |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
201 ; |
293
d0295e735768
Assignment lvalue (key, option name, alias...) cannnot contain spaces
Mikael Berthe <mikael@lilotux.net>
parents:
288
diff
changeset
|
202 // Check for embedded whitespace characters |
d0295e735768
Assignment lvalue (key, option name, alias...) cannnot contain spaces
Mikael Berthe <mikael@lilotux.net>
parents:
288
diff
changeset
|
203 for (p = key; p < t; p++) { |
d0295e735768
Assignment lvalue (key, option name, alias...) cannnot contain spaces
Mikael Berthe <mikael@lilotux.net>
parents:
288
diff
changeset
|
204 if (isblank(*p)) { |
d0295e735768
Assignment lvalue (key, option name, alias...) cannnot contain spaces
Mikael Berthe <mikael@lilotux.net>
parents:
288
diff
changeset
|
205 //scr_LogPrint("Error in assignment parsing!" |
d0295e735768
Assignment lvalue (key, option name, alias...) cannnot contain spaces
Mikael Berthe <mikael@lilotux.net>
parents:
288
diff
changeset
|
206 // " (Name should not contain space chars)"); |
d0295e735768
Assignment lvalue (key, option name, alias...) cannnot contain spaces
Mikael Berthe <mikael@lilotux.net>
parents:
288
diff
changeset
|
207 return FALSE; |
d0295e735768
Assignment lvalue (key, option name, alias...) cannnot contain spaces
Mikael Berthe <mikael@lilotux.net>
parents:
288
diff
changeset
|
208 } |
d0295e735768
Assignment lvalue (key, option name, alias...) cannnot contain spaces
Mikael Berthe <mikael@lilotux.net>
parents:
288
diff
changeset
|
209 } |
d0295e735768
Assignment lvalue (key, option name, alias...) cannnot contain spaces
Mikael Berthe <mikael@lilotux.net>
parents:
288
diff
changeset
|
210 |
280
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
211 *pkey = g_strndup(key, t+1-key); |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
212 |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
213 if (!*val) return FALSE; // Not an assignment |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
214 |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
215 // Remove leading and trailing spaces in option value: |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
216 for (val++; *val && isblank(*val) ; val++) ; |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
217 for (t = val ; *t ; t++) ; |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
218 for (t-- ; t >= val && isblank(*t) ; t--) ; |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
219 |
282
87d6ac21cd1b
Fix "Cannot unset option" bug
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
220 if (t < val) return TRUE; // no value (variable reset for example) |
280
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
221 |
337
3a25be278864
Values can be enclosed by quotes in assignments
Mikael Berthe <mikael@lilotux.net>
parents:
336
diff
changeset
|
222 // If the value begins and ends with quotes ("), these quotes are |
3a25be278864
Values can be enclosed by quotes in assignments
Mikael Berthe <mikael@lilotux.net>
parents:
336
diff
changeset
|
223 // removed and whitespace is not stripped |
3a25be278864
Values can be enclosed by quotes in assignments
Mikael Berthe <mikael@lilotux.net>
parents:
336
diff
changeset
|
224 if ((t>val) && (*val == '"' && *t == '"')) { |
3a25be278864
Values can be enclosed by quotes in assignments
Mikael Berthe <mikael@lilotux.net>
parents:
336
diff
changeset
|
225 val++; |
3a25be278864
Values can be enclosed by quotes in assignments
Mikael Berthe <mikael@lilotux.net>
parents:
336
diff
changeset
|
226 t--; |
3a25be278864
Values can be enclosed by quotes in assignments
Mikael Berthe <mikael@lilotux.net>
parents:
336
diff
changeset
|
227 } |
280
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
228 *pval = g_strndup(val, t+1-val); |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
229 return TRUE; |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
230 } |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
231 |
281
f562b9af2de7
Add "const" specifier in prototypes
Mikael Berthe <mikael@lilotux.net>
parents:
280
diff
changeset
|
232 void settings_set(guint type, const gchar *key, const gchar *value) |
279 | 233 { |
234 GSList **plist; | |
235 GSList *sptr; | |
236 T_setting *setting; | |
237 | |
238 plist = get_list_ptr(type); | |
239 if (!plist) return; | |
240 | |
241 sptr = settings_find(*plist, key); | |
242 if (sptr) { | |
243 // The setting has been found. We will update it or delete it. | |
244 setting = (T_setting*)sptr->data; | |
774
46304b773a44
Remove useless checks before g_free() calls
Mikael Berthe <mikael@lilotux.net>
parents:
764
diff
changeset
|
245 g_free(setting->value); |
279 | 246 if (!value) { |
247 // Let's remove the setting | |
248 g_free(setting->name); | |
249 *plist = g_slist_delete_link(*plist, sptr); | |
250 } else { | |
251 // Let's update the setting | |
252 setting->value = g_strdup(value); | |
253 } | |
254 } else if (value) { | |
255 setting = g_new(T_setting, 1); | |
256 setting->name = g_strdup(key); | |
257 setting->value = g_strdup(value); | |
258 *plist = g_slist_append(*plist, setting); | |
259 } | |
260 } | |
261 | |
281
f562b9af2de7
Add "const" specifier in prototypes
Mikael Berthe <mikael@lilotux.net>
parents:
280
diff
changeset
|
262 void settings_del(guint type, const gchar *key) |
279 | 263 { |
264 settings_set(type, key, NULL); | |
265 } | |
266 | |
281
f562b9af2de7
Add "const" specifier in prototypes
Mikael Berthe <mikael@lilotux.net>
parents:
280
diff
changeset
|
267 const gchar *settings_get(guint type, const gchar *key) |
279 | 268 { |
269 GSList **plist; | |
270 GSList *sptr; | |
271 T_setting *setting; | |
272 | |
273 plist = get_list_ptr(type); | |
274 sptr = settings_find(*plist, key); | |
275 if (!sptr) return NULL; | |
276 | |
277 setting = (T_setting*)sptr->data; | |
278 return setting->value; | |
279 } | |
280 | |
281
f562b9af2de7
Add "const" specifier in prototypes
Mikael Berthe <mikael@lilotux.net>
parents:
280
diff
changeset
|
281 int settings_get_int(guint type, const gchar *key) |
279 | 282 { |
281
f562b9af2de7
Add "const" specifier in prototypes
Mikael Berthe <mikael@lilotux.net>
parents:
280
diff
changeset
|
283 const gchar *setval = settings_get(type, key); |
279 | 284 |
285 if (setval) return atoi(setval); | |
286 return 0; | |
287 } | |
288 | |
294
871e53769084
Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents:
293
diff
changeset
|
289 // settings_get_status_msg(status) |
871e53769084
Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents:
293
diff
changeset
|
290 // Return a string with the current status message: |
871e53769084
Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents:
293
diff
changeset
|
291 // - if there is a user-defined message ("message" option), |
871e53769084
Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents:
293
diff
changeset
|
292 // return this message |
871e53769084
Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents:
293
diff
changeset
|
293 // - if there is a user-defined message for the given status (and no |
871e53769084
Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents:
293
diff
changeset
|
294 // generic user message), it is returned |
521 | 295 // - if no message is found, return NULL |
294
871e53769084
Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents:
293
diff
changeset
|
296 const gchar *settings_get_status_msg(enum imstatus status) |
871e53769084
Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents:
293
diff
changeset
|
297 { |
871e53769084
Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents:
293
diff
changeset
|
298 const gchar *rstatus = settings_opt_get("message"); |
871e53769084
Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents:
293
diff
changeset
|
299 |
871e53769084
Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents:
293
diff
changeset
|
300 if (rstatus) return rstatus; |
871e53769084
Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents:
293
diff
changeset
|
301 |
871e53769084
Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents:
293
diff
changeset
|
302 switch(status) { |
871e53769084
Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents:
293
diff
changeset
|
303 case available: |
521 | 304 rstatus = settings_opt_get("message_avail"); |
294
871e53769084
Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents:
293
diff
changeset
|
305 break; |
871e53769084
Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents:
293
diff
changeset
|
306 |
871e53769084
Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents:
293
diff
changeset
|
307 case freeforchat: |
521 | 308 rstatus = settings_opt_get("message_free"); |
294
871e53769084
Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents:
293
diff
changeset
|
309 break; |
871e53769084
Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents:
293
diff
changeset
|
310 |
871e53769084
Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents:
293
diff
changeset
|
311 case dontdisturb: |
521 | 312 rstatus = settings_opt_get("message_dnd"); |
294
871e53769084
Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents:
293
diff
changeset
|
313 break; |
871e53769084
Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents:
293
diff
changeset
|
314 |
871e53769084
Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents:
293
diff
changeset
|
315 case notavail: |
521 | 316 rstatus = settings_opt_get("message_notavail"); |
294
871e53769084
Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents:
293
diff
changeset
|
317 break; |
871e53769084
Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents:
293
diff
changeset
|
318 |
871e53769084
Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents:
293
diff
changeset
|
319 case away: |
521 | 320 rstatus = settings_opt_get("message_away"); |
294
871e53769084
Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents:
293
diff
changeset
|
321 break; |
871e53769084
Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents:
293
diff
changeset
|
322 |
521 | 323 default: // offline, invisible |
324 break; | |
294
871e53769084
Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents:
293
diff
changeset
|
325 } |
871e53769084
Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents:
293
diff
changeset
|
326 return rstatus; |
871e53769084
Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents:
293
diff
changeset
|
327 } |
576 | 328 |
867
7f056c566569
Commands /alias & /bind list the key bindings and aliases
Mikael Berthe <mikael@lilotux.net>
parents:
780
diff
changeset
|
329 // settings_foreach(type, pfunction, param) |
7f056c566569
Commands /alias & /bind list the key bindings and aliases
Mikael Berthe <mikael@lilotux.net>
parents:
780
diff
changeset
|
330 // Call pfunction(param, key, value) for each setting with requested type. |
7f056c566569
Commands /alias & /bind list the key bindings and aliases
Mikael Berthe <mikael@lilotux.net>
parents:
780
diff
changeset
|
331 void settings_foreach(guint type, void (*pfunc)(void *param, char *k, char *v), |
7f056c566569
Commands /alias & /bind list the key bindings and aliases
Mikael Berthe <mikael@lilotux.net>
parents:
780
diff
changeset
|
332 void *param) |
7f056c566569
Commands /alias & /bind list the key bindings and aliases
Mikael Berthe <mikael@lilotux.net>
parents:
780
diff
changeset
|
333 { |
7f056c566569
Commands /alias & /bind list the key bindings and aliases
Mikael Berthe <mikael@lilotux.net>
parents:
780
diff
changeset
|
334 GSList **plist; |
7f056c566569
Commands /alias & /bind list the key bindings and aliases
Mikael Berthe <mikael@lilotux.net>
parents:
780
diff
changeset
|
335 GSList *ptr; |
7f056c566569
Commands /alias & /bind list the key bindings and aliases
Mikael Berthe <mikael@lilotux.net>
parents:
780
diff
changeset
|
336 T_setting *setting; |
7f056c566569
Commands /alias & /bind list the key bindings and aliases
Mikael Berthe <mikael@lilotux.net>
parents:
780
diff
changeset
|
337 |
7f056c566569
Commands /alias & /bind list the key bindings and aliases
Mikael Berthe <mikael@lilotux.net>
parents:
780
diff
changeset
|
338 plist = get_list_ptr(type); |
7f056c566569
Commands /alias & /bind list the key bindings and aliases
Mikael Berthe <mikael@lilotux.net>
parents:
780
diff
changeset
|
339 |
7f056c566569
Commands /alias & /bind list the key bindings and aliases
Mikael Berthe <mikael@lilotux.net>
parents:
780
diff
changeset
|
340 if (!*plist) return; |
7f056c566569
Commands /alias & /bind list the key bindings and aliases
Mikael Berthe <mikael@lilotux.net>
parents:
780
diff
changeset
|
341 |
7f056c566569
Commands /alias & /bind list the key bindings and aliases
Mikael Berthe <mikael@lilotux.net>
parents:
780
diff
changeset
|
342 for (ptr = *plist ; ptr; ptr = g_slist_next(ptr)) { |
7f056c566569
Commands /alias & /bind list the key bindings and aliases
Mikael Berthe <mikael@lilotux.net>
parents:
780
diff
changeset
|
343 setting = ptr->data; |
7f056c566569
Commands /alias & /bind list the key bindings and aliases
Mikael Berthe <mikael@lilotux.net>
parents:
780
diff
changeset
|
344 pfunc(param, setting->name, setting->value); |
7f056c566569
Commands /alias & /bind list the key bindings and aliases
Mikael Berthe <mikael@lilotux.net>
parents:
780
diff
changeset
|
345 } |
7f056c566569
Commands /alias & /bind list the key bindings and aliases
Mikael Berthe <mikael@lilotux.net>
parents:
780
diff
changeset
|
346 } |
7f056c566569
Commands /alias & /bind list the key bindings and aliases
Mikael Berthe <mikael@lilotux.net>
parents:
780
diff
changeset
|
347 |
1004
b57a01ffeed6
Use existing conference bookmarks
Mikael Berthe <mikael@lilotux.net>
parents:
967
diff
changeset
|
348 |
b57a01ffeed6
Use existing conference bookmarks
Mikael Berthe <mikael@lilotux.net>
parents:
967
diff
changeset
|
349 // default_muc_nickname() |
b57a01ffeed6
Use existing conference bookmarks
Mikael Berthe <mikael@lilotux.net>
parents:
967
diff
changeset
|
350 // Return the user's default nickname |
b57a01ffeed6
Use existing conference bookmarks
Mikael Berthe <mikael@lilotux.net>
parents:
967
diff
changeset
|
351 // The caller should free the string after use |
b57a01ffeed6
Use existing conference bookmarks
Mikael Berthe <mikael@lilotux.net>
parents:
967
diff
changeset
|
352 char *default_muc_nickname(void) |
b57a01ffeed6
Use existing conference bookmarks
Mikael Berthe <mikael@lilotux.net>
parents:
967
diff
changeset
|
353 { |
b57a01ffeed6
Use existing conference bookmarks
Mikael Berthe <mikael@lilotux.net>
parents:
967
diff
changeset
|
354 char *nick; |
b57a01ffeed6
Use existing conference bookmarks
Mikael Berthe <mikael@lilotux.net>
parents:
967
diff
changeset
|
355 |
b57a01ffeed6
Use existing conference bookmarks
Mikael Berthe <mikael@lilotux.net>
parents:
967
diff
changeset
|
356 // We try the "nickname" option, then the username part of the jid. |
b57a01ffeed6
Use existing conference bookmarks
Mikael Berthe <mikael@lilotux.net>
parents:
967
diff
changeset
|
357 nick = (char*)settings_opt_get("nickname"); |
b57a01ffeed6
Use existing conference bookmarks
Mikael Berthe <mikael@lilotux.net>
parents:
967
diff
changeset
|
358 if (nick) |
b57a01ffeed6
Use existing conference bookmarks
Mikael Berthe <mikael@lilotux.net>
parents:
967
diff
changeset
|
359 return g_strdup(nick); |
b57a01ffeed6
Use existing conference bookmarks
Mikael Berthe <mikael@lilotux.net>
parents:
967
diff
changeset
|
360 |
b57a01ffeed6
Use existing conference bookmarks
Mikael Berthe <mikael@lilotux.net>
parents:
967
diff
changeset
|
361 nick = g_strdup(settings_opt_get("username")); |
b57a01ffeed6
Use existing conference bookmarks
Mikael Berthe <mikael@lilotux.net>
parents:
967
diff
changeset
|
362 if (nick) { |
b57a01ffeed6
Use existing conference bookmarks
Mikael Berthe <mikael@lilotux.net>
parents:
967
diff
changeset
|
363 char *p = strchr(nick, JID_DOMAIN_SEPARATOR); |
b57a01ffeed6
Use existing conference bookmarks
Mikael Berthe <mikael@lilotux.net>
parents:
967
diff
changeset
|
364 if (p > nick) |
b57a01ffeed6
Use existing conference bookmarks
Mikael Berthe <mikael@lilotux.net>
parents:
967
diff
changeset
|
365 *p = 0; |
b57a01ffeed6
Use existing conference bookmarks
Mikael Berthe <mikael@lilotux.net>
parents:
967
diff
changeset
|
366 } |
b57a01ffeed6
Use existing conference bookmarks
Mikael Berthe <mikael@lilotux.net>
parents:
967
diff
changeset
|
367 return nick; |
b57a01ffeed6
Use existing conference bookmarks
Mikael Berthe <mikael@lilotux.net>
parents:
967
diff
changeset
|
368 } |
b57a01ffeed6
Use existing conference bookmarks
Mikael Berthe <mikael@lilotux.net>
parents:
967
diff
changeset
|
369 |
580 | 370 /* vim: set expandtab cindent cinoptions=>2\:2(0: For Vim users... */ |