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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
|
/*
* Copyright (c) 2003-2006 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 2 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, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/* scripts.c: script interface for WeeChat plugins */
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include "../weechat-plugin.h"
#include "weechat-script.h"
/*
* weechat_script_auto_load: auto-load all scripts in a directory
*/
void
weechat_script_auto_load (t_weechat_plugin *plugin, char *language,
int (*callback)(t_weechat_plugin *, char *))
{
char *dir_home, *dir_name;
int dir_length;
/* build directory, adding WeeChat home */
dir_home = plugin->get_info (plugin, "weechat_dir", NULL);
if (!dir_home)
return;
dir_length = strlen (dir_home) + strlen (language) + 16;
dir_name =
(char *) malloc (dir_length * sizeof (char));
if (!dir_name)
{
free (dir_home);
return;
}
snprintf (dir_name, dir_length, "%s/%s/autoload", dir_home, language);
plugin->exec_on_files (plugin, dir_name, callback);
free (dir_name);
free (dir_home);
}
/*
* weechat_script_search: search a script in list
*/
t_plugin_script *
weechat_script_search (t_weechat_plugin *plugin,
t_plugin_script **list, char *name)
{
t_plugin_script *ptr_script;
for (ptr_script = *list; ptr_script;
ptr_script = ptr_script->next_script)
{
if (plugin->ascii_strcasecmp (plugin, ptr_script->name, name) == 0)
return ptr_script;
}
/* script not found */
return NULL;
}
/*
* weechat_script_search_full_name: search the full path name of a script
*/
char *
weechat_script_search_full_name (t_weechat_plugin *plugin,
char *language, char *filename)
{
char *final_name, *dir_home, *dir_system;
int length;
struct stat st;
if (filename[0] == '~')
{
dir_home = getenv ("HOME");
if (!dir_home)
return NULL;
length = strlen (dir_home) + strlen (filename + 1) + 1;
final_name = (char *) malloc (length);
if (final_name)
{
snprintf (final_name, length, "%s%s", dir_home, filename + 1);
return final_name;
}
return NULL;
}
/* try WeeChat user's autoload dir */
dir_home = plugin->get_info (plugin, "weechat_dir", NULL);
if (dir_home)
{
length = strlen (dir_home) + strlen (language) + 8 + strlen (filename) + 16;
final_name = (char *) malloc (length);
if (final_name)
{
snprintf (final_name, length, "%s/%s/autoload/%s", dir_home, language, filename);
if ((stat (final_name, &st) == 0) && (st.st_size > 0))
{
free (dir_home);
return final_name;
}
free (final_name);
}
free (dir_home);
}
/* try WeeChat language user's dir */
dir_home = plugin->get_info (plugin, "weechat_dir", NULL);
if (dir_home)
{
length = strlen (dir_home) + strlen (language) + strlen (filename) + 16;
final_name = (char *) malloc (length);
if (final_name)
{
snprintf (final_name, length, "%s/%s/%s", dir_home, language, filename);
if ((stat (final_name, &st) == 0) && (st.st_size > 0))
{
free (dir_home);
return final_name;
}
free (final_name);
}
free (dir_home);
}
/* try WeeChat user's dir */
dir_home = plugin->get_info (plugin, "weechat_dir", NULL);
if (dir_home)
{
length = strlen (dir_home) + strlen (filename) + 16;
final_name = (char *) malloc (length);
if (final_name)
{
snprintf (final_name, length, "%s/%s", dir_home, filename);
if ((stat (final_name, &st) == 0) && (st.st_size > 0))
{
free (dir_home);
return final_name;
}
free (final_name);
}
free (dir_home);
}
/* try WeeChat system dir */
dir_system = plugin->get_info (plugin, "weechat_sharedir", NULL);
if (dir_system)
{
length = strlen (dir_system) + strlen (dir_system) + strlen (filename) + 16;
final_name = (char *) malloc (length);
if (final_name)
{
snprintf (final_name,length, "%s/%s/%s", dir_system, language, filename);
if ((stat (final_name, &st) == 0) && (st.st_size > 0))
{
free (dir_system);
return final_name;
}
free (final_name);
}
free (dir_system);
}
return strdup(filename);
}
/*
* weechat_script_add: add a script to list of scripts
*/
t_plugin_script *
weechat_script_add (t_weechat_plugin *plugin,
t_plugin_script **script_list,
char *filename,
char *name, char *version,
char *shutdown_func, char *description)
{
t_plugin_script *new_script;
if (strchr (name, ' '))
{
plugin->print_server (plugin,
"Error: unable to load script "
"\"%s\" (bad name, spaces are forbidden)",
name);
return NULL;
}
new_script = (t_plugin_script *)malloc (sizeof (t_plugin_script));
if (new_script)
{
new_script->filename = strdup (filename);
new_script->interpreter = NULL;
new_script->name = strdup (name);
new_script->version = strdup (version);
new_script->shutdown_func = strdup (shutdown_func);
new_script->description = strdup (description);
/* add new script to list */
if ((*script_list))
(*script_list)->prev_script = new_script;
new_script->prev_script = NULL;
new_script->next_script = (*script_list);
(*script_list) = new_script;
return new_script;
}
plugin->print_server (plugin,
"Error: unable to load script "
"\"%s\" (not enough memory)",
name);
return NULL;
}
/*
* weechat_script_remove: remove a script from list of scripts
*/
void
weechat_script_remove (t_weechat_plugin *plugin,
t_plugin_script **script_list, t_plugin_script *script)
{
t_plugin_handler *ptr_handler, *next_handler;
t_plugin_modifier *ptr_modifier, *next_modifier;
/* remove all handlers pointing to script */
ptr_handler = plugin->handlers;
while (ptr_handler)
{
if ((t_plugin_script *)ptr_handler->handler_pointer == script)
{
next_handler = ptr_handler->next_handler;
plugin->handler_remove (plugin, ptr_handler);
ptr_handler = next_handler;
}
else
ptr_handler = ptr_handler->next_handler;
}
/* remove all modifiers pointing to script */
ptr_modifier = plugin->modifiers;
while (ptr_modifier)
{
if ((t_plugin_script *)ptr_modifier->modifier_pointer == script)
{
next_modifier = ptr_modifier->next_modifier;
plugin->modifier_remove (plugin, ptr_modifier);
ptr_modifier = next_modifier;
}
else
ptr_modifier = ptr_modifier->next_modifier;
}
/* free data */
if (script->filename)
free (script->filename);
if (script->name)
free (script->name);
if (script->description)
free (script->description);
if (script->version)
free (script->version);
if (script->shutdown_func)
free (script->shutdown_func);
/* remove script from list */
if (script->prev_script)
(script->prev_script)->next_script = script->next_script;
else
(*script_list) = script->next_script;
if (script->next_script)
(script->next_script)->prev_script = script->prev_script;
/* free script */
free (script);
}
/*
* weechat_script_remove_handler: remove a handler for a script
* for a msg handler, arg1=irc command, arg2=function
* for a cmd handler, arg1=command, arg2=function
*/
void
weechat_script_remove_handler (t_weechat_plugin *plugin,
t_plugin_script *script,
char *arg1, char *arg2)
{
t_plugin_handler *ptr_handler, *next_handler;
char *ptr_arg1;
/* search and remove handlers */
ptr_handler = plugin->handlers;
while (ptr_handler)
{
ptr_arg1 = NULL;
if (ptr_handler->type == PLUGIN_HANDLER_MESSAGE)
ptr_arg1 = ptr_handler->irc_command;
else if (ptr_handler->type == PLUGIN_HANDLER_COMMAND)
ptr_arg1 = ptr_handler->command;
if ((ptr_arg1)
&& ((t_plugin_script *)ptr_handler->handler_pointer == script)
&& (plugin->ascii_strcasecmp (plugin, ptr_arg1, arg1) == 0)
&& (plugin->ascii_strcasecmp (plugin, ptr_handler->handler_args, arg2) == 0))
{
next_handler = ptr_handler->next_handler;
plugin->handler_remove (plugin, ptr_handler);
ptr_handler = next_handler;
}
else
ptr_handler = ptr_handler->next_handler;
}
}
/*
* weechat_script_remove_timer_handler: remove a timer handler for a script
*/
void
weechat_script_remove_timer_handler (t_weechat_plugin *plugin,
t_plugin_script *script,
char *function)
{
t_plugin_handler *ptr_handler, *next_handler;
/* search and remove timer handlers */
ptr_handler = plugin->handlers;
while (ptr_handler)
{
if ((ptr_handler->type == PLUGIN_HANDLER_TIMER)
&& ((t_plugin_script *)ptr_handler->handler_pointer == script)
&& (plugin->ascii_strcasecmp (plugin, ptr_handler->handler_args, function) == 0))
{
next_handler = ptr_handler->next_handler;
plugin->handler_remove (plugin, ptr_handler);
ptr_handler = next_handler;
}
else
ptr_handler = ptr_handler->next_handler;
}
}
/*
* weechat_script_remove_keyboard_handler: remove a keyboard handler for a script
*/
void
weechat_script_remove_keyboard_handler (t_weechat_plugin *plugin,
t_plugin_script *script,
char *function)
{
t_plugin_handler *ptr_handler, *next_handler;
/* search and remove keyboard handlers */
ptr_handler = plugin->handlers;
while (ptr_handler)
{
if ((ptr_handler->type == PLUGIN_HANDLER_KEYBOARD)
&& ((t_plugin_script *)ptr_handler->handler_pointer == script)
&& (plugin->ascii_strcasecmp (plugin, ptr_handler->handler_args, function) == 0))
{
next_handler = ptr_handler->next_handler;
plugin->handler_remove (plugin, ptr_handler);
ptr_handler = next_handler;
}
else
ptr_handler = ptr_handler->next_handler;
}
}
/*
* weechat_script_remove_modifier: remove a modifier
* arg1=type, arg2=command, arg3=function
*/
void
weechat_script_remove_modifier (t_weechat_plugin *plugin,
t_plugin_script *script,
char *arg1, char *arg2, char *arg3)
{
t_plugin_modifier *ptr_modifier, *next_modifier;
t_plugin_modifier_type type;
char *ptr_arg2;
if (strcasecmp (arg1, PLUGIN_MODIFIER_IRC_IN_STR) == 0)
type = PLUGIN_MODIFIER_IRC_IN;
else if (strcasecmp (arg1, PLUGIN_MODIFIER_IRC_USER_STR) == 0)
type = PLUGIN_MODIFIER_IRC_USER;
else if (strcasecmp (arg1, PLUGIN_MODIFIER_IRC_OUT_STR) == 0)
type = PLUGIN_MODIFIER_IRC_OUT;
else
return;
/* search and remove modifiers */
ptr_modifier = plugin->modifiers;
while (ptr_modifier)
{
ptr_arg2 = NULL;
if (ptr_modifier->type == type)
ptr_arg2 = ptr_modifier->command;
if ((ptr_arg2)
&& ((t_plugin_script *)ptr_modifier->modifier_pointer == script)
&& (plugin->ascii_strcasecmp (plugin, ptr_arg2, arg2) == 0)
&& (plugin->ascii_strcasecmp (plugin, ptr_modifier->modifier_args, arg3) == 0))
{
next_modifier = ptr_modifier->next_modifier;
plugin->modifier_remove (plugin, ptr_modifier);
ptr_modifier = next_modifier;
}
else
ptr_modifier = ptr_modifier->next_modifier;
}
}
/*
* weechat_script_get_plugin_config: get a value of a script option
* format in file is: plugin.script.option=value
*/
char *
weechat_script_get_plugin_config (t_weechat_plugin *plugin,
t_plugin_script *script,
char *option)
{
char *option_fullname, *return_value;
option_fullname = (char *)malloc (strlen (script->name) +
strlen (option) + 2);
if (!option_fullname)
return NULL;
strcpy (option_fullname, script->name);
strcat (option_fullname, ".");
strcat (option_fullname, option);
return_value = plugin->get_plugin_config (plugin, option_fullname);
free (option_fullname);
return return_value;
}
/*
* weechat_script_set_plugin_config: set value of a script config option
* format in file is: plugin.script.option=value
*/
int
weechat_script_set_plugin_config (t_weechat_plugin *plugin,
t_plugin_script *script,
char *option, char *value)
{
char *option_fullname;
int return_code;
option_fullname = (char *)malloc (strlen (script->name) +
strlen (option) + 2);
if (!option_fullname)
return 0;
strcpy (option_fullname, script->name);
strcat (option_fullname, ".");
strcat (option_fullname, option);
return_code = plugin->set_plugin_config (plugin, option_fullname, value);
free (option_fullname);
return return_code;
}
|