summaryrefslogtreecommitdiff
path: root/src/plugins/plugin-config.c
blob: 9c15cd620fa9ca9f14b3571003ab2bf8de802923 (plain)
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
/*
 * Copyright (c) 2003-2007 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_plugin_option *plugin_options = NULL;
struct t_plugin_option *last_plugin_option = NULL;

char *plugin_config_sections[] =
{ "plugin", NULL };

struct t_config_option *plugin_config_options[] =
{ NULL, NULL };

t_config_func_read_option *plugin_config_read_functions[] =
{ plugin_config_read_option, NULL };

t_config_func_write_options *plugin_config_write_functions[] =
{ plugin_config_write_options, NULL };


/*
 * plugin_config_search_internal: search a plugin option (internal function)
 *                                This function should not be called directly.
 */

struct t_plugin_option *
plugin_config_search_internal (char *option)
{
    struct t_plugin_option *ptr_plugin_option;
    
    for (ptr_plugin_option = plugin_options; ptr_plugin_option;
         ptr_plugin_option = ptr_plugin_option->next_option)
    {
        if (string_strcasecmp (ptr_plugin_option->name, option) == 0)
        {
            return ptr_plugin_option;
        }
    }
    
    /* plugin option not found */
    return NULL;
}

/*
 * plugin_config_search: search a plugin option
 */

struct t_plugin_option *
plugin_config_search (char *plugin_name, char *option)
{
    char *internal_option;
    struct t_plugin_option *ptr_plugin_option;
    
    internal_option = (char *)malloc (strlen (plugin_name) +
                                      strlen (option) + 2);
    if (!internal_option)
        return NULL;
    
    strcpy (internal_option, plugin_name);
    strcat (internal_option, ".");
    strcat (internal_option, option);
    
    ptr_plugin_option = plugin_config_search_internal (internal_option);
    
    free (internal_option);
    
    return ptr_plugin_option;
}

/*
 * plugin_config_find_pos: find position for a plugin option (for sorting options)
 */

struct t_plugin_option *
plugin_config_find_pos (char *name)
{
    struct t_plugin_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;
    }
    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_plugin_option *ptr_plugin_option, *pos_option;
    int rc;

    rc = 0;
    
    ptr_plugin_option = plugin_config_search_internal (option);
    if (ptr_plugin_option)
    {
        if (!value || !value[0])
        {
            /* remove option from list */
            if (ptr_plugin_option->prev_option)
                (ptr_plugin_option->prev_option)->next_option =
                    ptr_plugin_option->next_option;
            else
                plugin_options = ptr_plugin_option->next_option;
            if (ptr_plugin_option->next_option)
                (ptr_plugin_option->next_option)->prev_option =
                    ptr_plugin_option->prev_option;
            rc = 1;
        }
        else
        {
            /* replace old value by new one */
            if (ptr_plugin_option->value)
                free (ptr_plugin_option->value);
            ptr_plugin_option->value = strdup (value);
            rc = 1;
        }
    }
    else
    {
        if (value && value[0])
        {
            ptr_plugin_option = (struct t_plugin_option *)malloc (sizeof (struct t_plugin_option));
            if (ptr_plugin_option)
            {
                /* create new option */
                ptr_plugin_option->name = strdup (option);
                string_tolower (ptr_plugin_option->name);
                ptr_plugin_option->value = strdup (value);
                
                if (plugin_options)
                {
                    pos_option = plugin_config_find_pos (ptr_plugin_option->name);
                    if (pos_option)
                    {
                        /* insert option into the list (before option found) */
                        ptr_plugin_option->prev_option = pos_option->prev_option;
                        ptr_plugin_option->next_option = pos_option;
                        if (pos_option->prev_option)
                            pos_option->prev_option->next_option = ptr_plugin_option;
                        else
                            plugin_options = ptr_plugin_option;
                        pos_option->prev_option = ptr_plugin_option;
                    }
                    else
                    {
                        /* add option to the end */
                        ptr_plugin_option->prev_option = last_plugin_option;
                        ptr_plugin_option->next_option = NULL;
                        last_plugin_option->next_option = ptr_plugin_option;
                        last_plugin_option = ptr_plugin_option;
                    }
                }
                else
                {
                    ptr_plugin_option->prev_option = NULL;
                    ptr_plugin_option->next_option = NULL;
                    plugin_options = ptr_plugin_option;
                    last_plugin_option = ptr_plugin_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, char *value)
{
    char *internal_option;
    int return_code;
    
    internal_option = (char *)malloc (strlen (plugin_name) +
                                      strlen (option) + 2);
    if (!internal_option)
        return 0;
    
    strcpy (internal_option, plugin_name);
    strcat (internal_option, ".");
    strcat (internal_option, option);
    
    return_code = plugin_config_set_internal (internal_option, value);
    free (internal_option);
    
    return return_code;
}

/*
 * plugin_config_read_option: read an option in config file
 *                            Return:  0 = successful
 *                                    -1 = option not found
 *                                    -2 = bad format/value
 */

int
plugin_config_read_option (struct t_config_option *options,
                           char *option_name, char *value)
{
    char *value2;
    
    /* make C compiler happy */
    (void) options;
    
    if (option_name)
    {
        value2 = string_iconv_to_internal (NULL, value);
        plugin_config_set_internal (option_name,
                                    (value2) ? value2 : value);
        if (value2)
            free (value2);
    }
    else
    {
        /* does nothing for new [plugin] section */
    }
    
    /* all ok */
    return 0;
} 

/*
 * plugin_config_read: read WeeChat 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_sections, plugin_config_options,
                             plugin_config_read_functions,
                             plugin_config_read_option,
                             NULL,
                             WEECHAT_PLUGIN_CONFIG_NAME);
}

/*
 * plugin_config_write_options: write plugin options in configuration file
 *                              Return:  0 = successful
 *                                      -1 = write error
 */

int
plugin_config_write_options (FILE *file, char *section_name,
                             struct t_config_option *options)
{
    /* make C compiler happy */
    (void) options;
    
    struct t_plugin_option *ptr_plugin_option;
    
    string_iconv_fprintf (file, "\n[%s]\n", section_name);
    
    for (ptr_plugin_option = plugin_options; ptr_plugin_option;
         ptr_plugin_option = ptr_plugin_option->next_option)
    {
        string_iconv_fprintf (file, "%s = \"%s\"\n",
                              ptr_plugin_option->name,
                              ptr_plugin_option->value);
    }
    
    /* all ok */
    return 0;
}

/*
 * plugin_config_write: write WeeChat configuration file
 *                      return:  0 if ok
 *                             < 0 if error
 */

int
plugin_config_write ()
{
    weechat_log_printf (_("Saving plugins configuration to disk\n"));
    return config_file_write (plugin_config_sections, plugin_config_options,
                              plugin_config_write_functions,
                              WEECHAT_PLUGIN_CONFIG_NAME);
}