1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
|
/*
* Copyright (c) 2003-2008 by FlashCode <flashcode@flashtux.org>
* See README for License detail, AUTHORS for developers list.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/* plugin-config.c: plugin configuration */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "../core/weechat.h"
#include "../core/wee-config.h"
#include "../core/wee-hook.h"
#include "../core/wee-log.h"
#include "../core/wee-string.h"
#include "plugin-config.h"
struct t_config_file *plugin_config = NULL;
struct t_config_option *plugin_options = NULL;
struct t_config_option *last_plugin_option = NULL;
/*
* plugin_config_search_internal: search a plugin option (internal function)
* This function should not be called directly.
*/
struct t_config_option *
plugin_config_search_internal (char *option_name)
{
struct t_config_option *ptr_option;
for (ptr_option = plugin_options; ptr_option;
ptr_option = ptr_option->next_option)
{
if (string_strcasecmp (ptr_option->name, option_name) == 0)
{
return ptr_option;
}
}
/* plugin option not found */
return NULL;
}
/*
* plugin_config_search: search a plugin option
*/
struct t_config_option *
plugin_config_search (char *plugin_name, char *option_name)
{
char *internal_option;
struct t_config_option *ptr_option;
internal_option = (char *)malloc (strlen (plugin_name) +
strlen (option_name) + 2);
if (!internal_option)
return NULL;
strcpy (internal_option, plugin_name);
strcat (internal_option, ".");
strcat (internal_option, option_name);
ptr_option = plugin_config_search_internal (internal_option);
free (internal_option);
return ptr_option;
}
/*
* plugin_config_find_pos: find position for a plugin option (for sorting options)
*/
struct t_config_option *
plugin_config_find_pos (char *name)
{
struct t_config_option *ptr_option;
for (ptr_option = plugin_options; ptr_option;
ptr_option = ptr_option->next_option)
{
if (string_strcasecmp (name, ptr_option->name) < 0)
return ptr_option;
}
/* position not found (we will add to the end of list) */
return NULL;
}
/*
* plugin_config_set_internal: set value for a plugin option (internal function)
* This function should not be called directly.
* Return: 1 if ok, 0 if error
*/
int
plugin_config_set_internal (char *option, char *value)
{
struct t_config_option *ptr_option, *pos_option;
int rc;
rc = 0;
ptr_option = plugin_config_search_internal (option);
if (ptr_option)
{
if (!value || !value[0])
{
/* remove option from list */
if (ptr_option->prev_option)
(ptr_option->prev_option)->next_option =
ptr_option->next_option;
else
plugin_options = ptr_option->next_option;
if (ptr_option->next_option)
(ptr_option->next_option)->prev_option =
ptr_option->prev_option;
rc = 1;
}
else
{
/* replace old value by new one */
if (ptr_option->value)
free (ptr_option->value);
ptr_option->value = strdup (value);
rc = 1;
}
}
else
{
if (value && value[0])
{
ptr_option = (struct t_config_option *)malloc (sizeof (struct t_config_option));
if (ptr_option)
{
/* create new option */
ptr_option->name = strdup (option);
string_tolower (ptr_option->name);
ptr_option->type = CONFIG_OPTION_STRING;
ptr_option->description = NULL;
ptr_option->string_values = NULL;
ptr_option->min = 0;
ptr_option->max = 0;
ptr_option->default_value = NULL;
ptr_option->value = strdup (value);
ptr_option->callback_change = NULL;
if (plugin_options)
{
pos_option = plugin_config_find_pos (ptr_option->name);
if (pos_option)
{
/* insert option into the list (before option found) */
ptr_option->prev_option = pos_option->prev_option;
ptr_option->next_option = pos_option;
if (pos_option->prev_option)
pos_option->prev_option->next_option = ptr_option;
else
plugin_options = ptr_option;
pos_option->prev_option = ptr_option;
}
else
{
/* add option to the end */
ptr_option->prev_option = last_plugin_option;
ptr_option->next_option = NULL;
last_plugin_option->next_option = ptr_option;
last_plugin_option = ptr_option;
}
}
else
{
ptr_option->prev_option = NULL;
ptr_option->next_option = NULL;
plugin_options = ptr_option;
last_plugin_option = ptr_option;
}
rc = 1;
}
}
else
rc = 0;
}
if (rc)
hook_config_exec ("plugin", option, value);
return rc;
}
/*
* plugin_config_set: set value for a plugin option (create it if not found)
* return: 1 if ok, 0 if error
*/
int
plugin_config_set (char *plugin_name, char *option_name, char *value)
{
char *internal_option;
int return_code;
internal_option = (char *)malloc (strlen (plugin_name) +
strlen (option_name) + 2);
if (!internal_option)
return 0;
strcpy (internal_option, plugin_name);
strcat (internal_option, ".");
strcat (internal_option, option_name);
return_code = plugin_config_set_internal (internal_option, value);
free (internal_option);
return return_code;
}
/*
* plugin_config_free: free a plugin option and remove it from list
*/
void
plugin_config_free (struct t_config_option *option)
{
struct t_config_option *new_plugin_options;
/* remove option from list */
if (last_plugin_option == option)
last_plugin_option = option->prev_option;
if (option->prev_option)
{
(option->prev_option)->next_option = option->next_option;
new_plugin_options = plugin_options;
}
else
new_plugin_options = option->next_option;
if (option->next_option)
(option->next_option)->prev_option = option->prev_option;
/* free data */
if (option->name)
free (option->name);
if (option->value)
free (option->value);
free (option);
plugin_options = new_plugin_options;
}
/*
* plugin_config_free_all: free all plugin options
*/
void
plugin_config_free_all ()
{
while (plugin_options)
plugin_config_free (plugin_options);
}
/*
* plugin_config_read_option: read an option in config file
* Return: 0 = successful
* -1 = option not found
* -2 = bad format/value
*/
void
plugin_config_read_option (struct t_config_file *config_file,
char *option_name, char *value)
{
char *value2;
/* make C compiler happy */
(void) config_file;
if (option_name && value)
{
value2 = string_iconv_to_internal (NULL, value);
plugin_config_set_internal (option_name,
(value2) ? value2 : value);
if (value2)
free (value2);
}
}
/*
* plugin_config_write_options: write plugin options in configuration file
*/
void
plugin_config_write_options (struct t_config_file *config_file,
char *section_name)
{
struct t_config_option *ptr_option;
config_file_write_line (config_file, section_name, NULL);
for (ptr_option = plugin_options; ptr_option;
ptr_option = ptr_option->next_option)
{
config_file_write_line (config_file,
ptr_option->name,
"%s", ptr_option->value);
}
}
/*
* plugin_config_init: init plugins config structure
*/
void
plugin_config_init ()
{
plugin_config = config_file_new (NULL, PLUGIN_CONFIG_FILENAME);
if (plugin_config)
{
config_file_new_section (plugin_config, "plugins",
&plugin_config_read_option,
&plugin_config_write_options,
NULL);
}
}
/*
* plugin_config_read: read plugins configuration file
* return: 0 = successful
* -1 = config file file not found
* -2 = error in config file
*/
int
plugin_config_read ()
{
return config_file_read (plugin_config);
}
/*
* plugin_config_reload: read plugins configuration file
* return: 0 = successful
* -1 = config file file not found
* -2 = error in config file
*/
int
plugin_config_reload ()
{
/* remove all plugin options */
plugin_config_free_all ();
/* reload plugins config file */
return config_file_reload (plugin_config);
}
/*
* plugin_config_write: write plugins configuration file
* return: 0 if ok
* < 0 if error
*/
int
plugin_config_write ()
{
log_printf (_("Saving plugins configuration to disk"));
return config_file_write (plugin_config);
}
|