Mercurial > hg
annotate mcabber/src/settings.c @ 521:cc8c969ab6e5
"/status" changes
- No hardcoded status message
- If there is no user-defined status message, the current message is kept
- "/status $status -" clears the status message
- Auto-away improvement
author | Mikael Berthe <mikael@lilotux.net> |
---|---|
date | Sat, 19 Nov 2005 16:30:49 +0100 |
parents | f8f3c7493457 |
children | 8b3db0b555a1 |
rev | line source |
---|---|
279 | 1 /* |
2 * settings.c -- Configuration stuff | |
393 | 3 * |
279 | 4 * Copyright (C) 2005 Mikael Berthe <bmikael@lists.lilotux.net> |
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"); |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
92 return -1; |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
93 } |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
94 } |
362
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
341
diff
changeset
|
95 // Check configuration file permissions |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
341
diff
changeset
|
96 // 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
|
97 checkset_perm(filename, TRUE); |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
341
diff
changeset
|
98 // 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
|
99 sprintf(filename, "%s/.mcabber/", home); |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
341
diff
changeset
|
100 checkset_perm(filename, FALSE); |
341
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
101 g_free(filename); |
362
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
341
diff
changeset
|
102 } else { |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
341
diff
changeset
|
103 if ((fp = fopen(filename, "r")) == NULL) { |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
341
diff
changeset
|
104 perror("fopen (cfg_file())"); |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
341
diff
changeset
|
105 return -1; |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
341
diff
changeset
|
106 } |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
341
diff
changeset
|
107 // Check configuration file permissions (see above) |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
341
diff
changeset
|
108 checkset_perm(filename, TRUE); |
341
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
109 } |
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 buf = g_new(char, 512); |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
112 |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
113 while (fgets(buf+1, 511, fp) != NULL) { |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
114 // 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
|
115 line = buf+1; |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
116 ln++; |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
117 |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
118 // Strip leading spaces |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
119 while (isspace(*line)) |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
120 line++; |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
121 |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
122 // Make eol point to the last char of the line |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
123 for (eol = line ; *eol ; eol++) |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
124 ; |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
125 if (eol > line) |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
126 eol--; |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
127 |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
128 // Strip trailing spaces |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
129 while (eol > line && isspace(*eol)) |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
130 *eol-- = 0; |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
131 |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
132 // Ignore empty lines and comments |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
133 if ((*line == '\n') || (*line == '\0') || (*line == '#')) |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
134 continue; |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
135 |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
136 if ((strchr(line, '=') != NULL)) { |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
137 // Only accept the set, alias and bind commands |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
138 if (strncmp(line, "set ", 4) && |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
139 strncmp(line, "bind ", 5) && |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
140 strncmp(line, "alias ", 6)) { |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
141 scr_LogPrint(LPRINT_LOGNORM, |
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
142 "Error in configuration file (l. %d): bad command", ln); |
341
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
143 err++; |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
144 continue; |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
145 } |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
146 *(--line) = '/'; // Set the leading '/' to build a command line |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
147 process_command(line); // Process the command |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
148 } else { |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
149 scr_LogPrint(LPRINT_LOGNORM, |
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
150 "Error in configuration file (l. %d): no assignment", ln); |
341
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
151 err++; |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
152 } |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
153 } |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
154 g_free(buf); |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
155 fclose(fp); |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
156 return err; |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
157 } |
dea407d53fe6
Improve configuration file reading
Mikael Berthe <mikael@lilotux.net>
parents:
337
diff
changeset
|
158 |
280
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
159 // parse_assigment(assignment, pkey, pval) |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
160 // Read assignment and split it to key, value |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
161 // |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
162 // 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
|
163 // 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
|
164 // |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
165 // 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
|
166 // to NULL and return FALSE. |
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 // The called 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
|
169 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
|
170 { |
293
d0295e735768
Assignment lvalue (key, option name, alias...) cannnot contain spaces
Mikael Berthe <mikael@lilotux.net>
parents:
288
diff
changeset
|
171 char *key, *val, *t, *p; |
280
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
172 |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
173 *pkey = *pval = NULL; |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
174 |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
175 key = assignment; |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
176 // Remove leading spaces in option name |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
177 while ((!isalnum(*key)) && (*key != '=') && *key) { |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
178 //if (!isblank(*key)) |
293
d0295e735768
Assignment lvalue (key, option name, alias...) cannnot contain spaces
Mikael Berthe <mikael@lilotux.net>
parents:
288
diff
changeset
|
179 // scr_LogPrint("Error in assignment parsing!"); |
280
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
180 key++; |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
181 } |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
182 if (!*key) return FALSE; // Empty assignment |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
183 |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
184 if (*key == '=') { |
293
d0295e735768
Assignment lvalue (key, option name, alias...) cannnot contain spaces
Mikael Berthe <mikael@lilotux.net>
parents:
288
diff
changeset
|
185 //scr_LogPrint("Cannot parse assignment!"); |
280
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
186 return FALSE; |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
187 } |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
188 // Ok, key points to the option name |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
189 |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
190 for (val = key+1 ; *val && (*val != '=') ; val++) |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
191 if (!isalnum(*val) && !isblank(*val) && (*val != '_') && (*val != '-')) { |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
192 // 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
|
193 //scr_LogPrint("Error in assignment parsing!"); |
280
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
194 return FALSE; |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
195 } |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
196 // Remove trailing spaces in option name: |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
197 for (t = val-1 ; t > key && isblank(*t) ; t--) |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
198 ; |
293
d0295e735768
Assignment lvalue (key, option name, alias...) cannnot contain spaces
Mikael Berthe <mikael@lilotux.net>
parents:
288
diff
changeset
|
199 // Check for embedded whitespace characters |
d0295e735768
Assignment lvalue (key, option name, alias...) cannnot contain spaces
Mikael Berthe <mikael@lilotux.net>
parents:
288
diff
changeset
|
200 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
|
201 if (isblank(*p)) { |
d0295e735768
Assignment lvalue (key, option name, alias...) cannnot contain spaces
Mikael Berthe <mikael@lilotux.net>
parents:
288
diff
changeset
|
202 //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
|
203 // " (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
|
204 return FALSE; |
d0295e735768
Assignment lvalue (key, option name, alias...) cannnot contain spaces
Mikael Berthe <mikael@lilotux.net>
parents:
288
diff
changeset
|
205 } |
d0295e735768
Assignment lvalue (key, option name, alias...) cannnot contain spaces
Mikael Berthe <mikael@lilotux.net>
parents:
288
diff
changeset
|
206 } |
d0295e735768
Assignment lvalue (key, option name, alias...) cannnot contain spaces
Mikael Berthe <mikael@lilotux.net>
parents:
288
diff
changeset
|
207 |
280
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
208 *pkey = g_strndup(key, t+1-key); |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
209 |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
210 if (!*val) return FALSE; // Not an assignment |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
211 |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
212 // Remove leading and trailing spaces in option value: |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
213 for (val++; *val && isblank(*val) ; val++) ; |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
214 for (t = val ; *t ; t++) ; |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
215 for (t-- ; t >= val && isblank(*t) ; t--) ; |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
216 |
282
87d6ac21cd1b
Fix "Cannot unset option" bug
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
217 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
|
218 |
337
3a25be278864
Values can be enclosed by quotes in assignments
Mikael Berthe <mikael@lilotux.net>
parents:
336
diff
changeset
|
219 // 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
|
220 // removed and whitespace is not stripped |
3a25be278864
Values can be enclosed by quotes in assignments
Mikael Berthe <mikael@lilotux.net>
parents:
336
diff
changeset
|
221 if ((t>val) && (*val == '"' && *t == '"')) { |
3a25be278864
Values can be enclosed by quotes in assignments
Mikael Berthe <mikael@lilotux.net>
parents:
336
diff
changeset
|
222 val++; |
3a25be278864
Values can be enclosed by quotes in assignments
Mikael Berthe <mikael@lilotux.net>
parents:
336
diff
changeset
|
223 t--; |
3a25be278864
Values can be enclosed by quotes in assignments
Mikael Berthe <mikael@lilotux.net>
parents:
336
diff
changeset
|
224 } |
280
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
225 *pval = g_strndup(val, t+1-val); |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
226 return TRUE; |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
227 } |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
228 |
281
f562b9af2de7
Add "const" specifier in prototypes
Mikael Berthe <mikael@lilotux.net>
parents:
280
diff
changeset
|
229 void settings_set(guint type, const gchar *key, const gchar *value) |
279 | 230 { |
231 GSList **plist; | |
232 GSList *sptr; | |
233 T_setting *setting; | |
234 | |
235 plist = get_list_ptr(type); | |
236 if (!plist) return; | |
237 | |
238 sptr = settings_find(*plist, key); | |
239 if (sptr) { | |
240 // The setting has been found. We will update it or delete it. | |
241 setting = (T_setting*)sptr->data; | |
242 if (setting->value) | |
243 g_free(setting->value); | |
244 if (!value) { | |
245 // Let's remove the setting | |
246 g_free(setting->name); | |
247 *plist = g_slist_delete_link(*plist, sptr); | |
248 } else { | |
249 // Let's update the setting | |
250 setting->value = g_strdup(value); | |
251 } | |
252 } else if (value) { | |
253 setting = g_new(T_setting, 1); | |
254 setting->name = g_strdup(key); | |
255 setting->value = g_strdup(value); | |
256 *plist = g_slist_append(*plist, setting); | |
257 } | |
258 } | |
259 | |
281
f562b9af2de7
Add "const" specifier in prototypes
Mikael Berthe <mikael@lilotux.net>
parents:
280
diff
changeset
|
260 void settings_del(guint type, const gchar *key) |
279 | 261 { |
262 settings_set(type, key, NULL); | |
263 } | |
264 | |
281
f562b9af2de7
Add "const" specifier in prototypes
Mikael Berthe <mikael@lilotux.net>
parents:
280
diff
changeset
|
265 const gchar *settings_get(guint type, const gchar *key) |
279 | 266 { |
267 GSList **plist; | |
268 GSList *sptr; | |
269 T_setting *setting; | |
270 | |
271 plist = get_list_ptr(type); | |
272 sptr = settings_find(*plist, key); | |
273 if (!sptr) return NULL; | |
274 | |
275 setting = (T_setting*)sptr->data; | |
276 return setting->value; | |
277 } | |
278 | |
281
f562b9af2de7
Add "const" specifier in prototypes
Mikael Berthe <mikael@lilotux.net>
parents:
280
diff
changeset
|
279 int settings_get_int(guint type, const gchar *key) |
279 | 280 { |
281
f562b9af2de7
Add "const" specifier in prototypes
Mikael Berthe <mikael@lilotux.net>
parents:
280
diff
changeset
|
281 const gchar *setval = settings_get(type, key); |
279 | 282 |
283 if (setval) return atoi(setval); | |
284 return 0; | |
285 } | |
286 | |
288 | 287 // Return the command the key is bound to, or NULL. |
288 const gchar *isbound(int key) | |
289 { | |
290 gchar asciikey[16]; | |
291 g_snprintf(asciikey, 15, "%d", key); | |
292 return settings_get(SETTINGS_TYPE_BINDING, asciikey); | |
293 } | |
294 | |
294
871e53769084
Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents:
293
diff
changeset
|
295 // settings_get_status_msg(status) |
871e53769084
Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents:
293
diff
changeset
|
296 // 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
|
297 // - 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
|
298 // return this message |
871e53769084
Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents:
293
diff
changeset
|
299 // - 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
|
300 // generic user message), it is returned |
521 | 301 // - 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
|
302 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
|
303 { |
871e53769084
Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents:
293
diff
changeset
|
304 const gchar *rstatus = settings_opt_get("message"); |
871e53769084
Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents:
293
diff
changeset
|
305 |
871e53769084
Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents:
293
diff
changeset
|
306 if (rstatus) return rstatus; |
871e53769084
Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents:
293
diff
changeset
|
307 |
871e53769084
Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents:
293
diff
changeset
|
308 switch(status) { |
871e53769084
Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents:
293
diff
changeset
|
309 case available: |
521 | 310 rstatus = settings_opt_get("message_avail"); |
294
871e53769084
Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents:
293
diff
changeset
|
311 break; |
871e53769084
Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents:
293
diff
changeset
|
312 |
871e53769084
Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents:
293
diff
changeset
|
313 case freeforchat: |
521 | 314 rstatus = settings_opt_get("message_free"); |
294
871e53769084
Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents:
293
diff
changeset
|
315 break; |
871e53769084
Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents:
293
diff
changeset
|
316 |
871e53769084
Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents:
293
diff
changeset
|
317 case dontdisturb: |
521 | 318 rstatus = settings_opt_get("message_dnd"); |
294
871e53769084
Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents:
293
diff
changeset
|
319 break; |
871e53769084
Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents:
293
diff
changeset
|
320 |
871e53769084
Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents:
293
diff
changeset
|
321 case notavail: |
521 | 322 rstatus = settings_opt_get("message_notavail"); |
294
871e53769084
Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents:
293
diff
changeset
|
323 break; |
871e53769084
Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents:
293
diff
changeset
|
324 |
871e53769084
Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents:
293
diff
changeset
|
325 case away: |
521 | 326 rstatus = settings_opt_get("message_away"); |
294
871e53769084
Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents:
293
diff
changeset
|
327 break; |
871e53769084
Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents:
293
diff
changeset
|
328 |
521 | 329 default: // offline, invisible |
330 break; | |
294
871e53769084
Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents:
293
diff
changeset
|
331 } |
871e53769084
Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents:
293
diff
changeset
|
332 return rstatus; |
871e53769084
Allow one status message per Jabber status
Mikael Berthe <mikael@lilotux.net>
parents:
293
diff
changeset
|
333 } |