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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
|
/*
* 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/>.
*/
/* script.c: script interface for WeeChat plugins */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include "../weechat-plugin.h"
#include "script.h"
#include "script-callback.h"
#define SCRIPT_OPTION_CHECK_LICENSE "check_license"
int script_option_check_license = 0;
/*
* script_config_read: read script configuration
*/
void
script_config_read (struct t_weechat_plugin *weechat_plugin)
{
char *string;
string = weechat_config_get_plugin (SCRIPT_OPTION_CHECK_LICENSE);
if (!string)
{
weechat_config_set_plugin (SCRIPT_OPTION_CHECK_LICENSE, "on");
string = weechat_config_get_plugin (SCRIPT_OPTION_CHECK_LICENSE);
}
if (string && (weechat_config_string_to_boolean (string) > 0))
script_option_check_license = 1;
else
script_option_check_license = 0;
}
/*
* script_config_cb: callback called when config option is changed
*/
int
script_config_cb (void *data, char *type, char *option, char *value)
{
/* make C compiler happy */
(void) type;
(void) option;
(void) value;
script_config_read (data);
return WEECHAT_RC_OK;
}
/*
* script_init: initialize script
*/
void
script_init (struct t_weechat_plugin *weechat_plugin,
int (*callback_command)(void *data,
struct t_gui_buffer *buffer,
int argc, char **argv,
char **argv_eol),
int (*callback_completion)(void *data, char *completion,
struct t_gui_buffer *buffer,
struct t_weelist *list),
int (*callback_signal_debug_dump)(void *data, char *signal,
char *type_data,
void *signal_data),
int (*callback_load_file)(void *data, char *filename))
{
char *string, *completion = "list|listfull|load|autoload|reload|unload %f";
int length;
/* read script configuration */
script_config_read (weechat_plugin);
/* add hook for config option */
length = strlen (weechat_plugin->name) + 32;
string = (char *)malloc (length * sizeof (char));
if (string)
{
snprintf (string, length, "%s.%s",
weechat_plugin->name, SCRIPT_OPTION_CHECK_LICENSE);
weechat_hook_config ("plugin", string,
&script_config_cb, weechat_plugin);
free (string);
}
/* create directories in WeeChat home */
weechat_mkdir_home (weechat_plugin->name, 0644);
length = strlen (weechat_plugin->name) + strlen ("/autoload") + 1;
string = (char *)malloc (length * sizeof (char));
if (string)
{
snprintf (string, length, "%s/autoload", weechat_plugin->name);
weechat_mkdir_home (string, 0644);
free (string);
}
/* add command */
length = strlen (completion) + strlen (weechat_plugin->name) + 16;
string = (char *)malloc (length * sizeof (char));
if (string)
{
snprintf (string, length, "%s|%%(%s_script)",
completion, weechat_plugin->name);
}
weechat_hook_command (weechat_plugin->name,
_("list/load/unload scripts"),
_("[list [name]] | [listfull [name]] "
"[load filename] | [autoload] | "
"[reload] | [unload [name]]"),
_("filename: script (file) to load\n"
"name: a script name\n\n"
"Without argument, this command "
"lists all loaded scripts."),
(string) ? string : completion,
callback_command, NULL);
if (string)
free (string);
/* add completion */
length = strlen (weechat_plugin->name) + 16;
string = (char *)malloc (length * sizeof (char));
if (string)
{
snprintf (string, length, "%s_script", weechat_plugin->name);
weechat_hook_completion (string, callback_completion, NULL);
free (string);
}
/* add signal for "debug_dump" */
weechat_hook_signal ("debug_dump", callback_signal_debug_dump, NULL);
/* autoload scripts */
script_auto_load (weechat_plugin, callback_load_file);
}
/*
* script_ptr2str: convert pointer to string for usage in a script
* (any language)
* WARNING: result has to be free() after use
*/
char *
script_ptr2str (void *pointer)
{
char pointer_str[128];
if (!pointer)
return strdup ("");
snprintf (pointer_str, sizeof (pointer_str) - 1,
"0x%x", (unsigned int)pointer);
return strdup (pointer_str);
}
/*
* script_str2ptr: convert stirng to pointer for usage outside script
*/
void *
script_str2ptr (char *pointer_str)
{
unsigned int value;
if (!pointer_str || (pointer_str[0] != '0') || (pointer_str[1] != 'x'))
return NULL;
sscanf (pointer_str + 2, "%x", &value);
return (void *)value;
}
/*
* script_auto_load: auto-load all scripts in a directory
*/
void
script_auto_load (struct t_weechat_plugin *weechat_plugin,
int (*callback)(void *data, char *filename))
{
char *dir_home, *dir_name;
int dir_length;
/* build directory, adding WeeChat home */
dir_home = weechat_info_get ("weechat_dir");
if (!dir_home)
return;
dir_length = strlen (dir_home) + strlen (weechat_plugin->name) + 16;
dir_name = (char *)malloc (dir_length * sizeof (char));
if (!dir_name)
return;
snprintf (dir_name, dir_length,
"%s/%s/autoload", dir_home, weechat_plugin->name);
weechat_exec_on_files (dir_name, NULL, callback);
free (dir_name);
}
/*
* script_search: search a script in list
*/
struct t_plugin_script *
script_search (struct t_weechat_plugin *weechat_plugin,
struct t_plugin_script *scripts, char *name)
{
struct t_plugin_script *ptr_script;
for (ptr_script = scripts; ptr_script;
ptr_script = ptr_script->next_script)
{
if (weechat_strcasecmp (ptr_script->name, name) == 0)
return ptr_script;
}
/* script not found */
return NULL;
}
/*
* script_search_full_name: search the full path name of a script
*/
char *
script_search_full_name (struct t_weechat_plugin *weechat_plugin,
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 * sizeof (char));
if (final_name)
{
snprintf (final_name, length, "%s%s", dir_home, filename + 1);
return final_name;
}
return NULL;
}
dir_home = weechat_info_get ("weechat_dir");
if (dir_home)
{
/* try WeeChat user's autoload dir */
length = strlen (dir_home) + strlen (weechat_plugin->name) + 8 +
strlen (filename) + 16;
final_name = (char *)malloc (length * sizeof (char));
if (final_name)
{
snprintf (final_name, length,
"%s/%s/autoload/%s",
dir_home, weechat_plugin->name, filename);
if ((stat (final_name, &st) == 0) && (st.st_size > 0))
return final_name;
free (final_name);
}
/* try WeeChat language user's dir */
length = strlen (dir_home) + strlen (weechat_plugin->name) +
strlen (filename) + 16;
final_name = (char *)malloc (length * sizeof (char));
if (final_name)
{
snprintf (final_name, length,
"%s/%s/%s", dir_home, weechat_plugin->name, filename);
if ((stat (final_name, &st) == 0) && (st.st_size > 0))
return final_name;
free (final_name);
}
/* try WeeChat user's dir */
length = strlen (dir_home) + strlen (filename) + 16;
final_name = (char *)malloc (length * sizeof (char));
if (final_name)
{
snprintf (final_name, length,
"%s/%s", dir_home, filename);
if ((stat (final_name, &st) == 0) && (st.st_size > 0))
return final_name;
free (final_name);
}
}
/* try WeeChat system dir */
dir_system = weechat_info_get ("weechat_sharedir");
if (dir_system)
{
length = strlen (dir_system) + strlen (weechat_plugin->name) +
strlen (filename) + 16;
final_name = (char *)malloc (length * sizeof (char));
if (final_name)
{
snprintf (final_name,length,
"%s/%s/%s", dir_system, weechat_plugin->name, filename);
if ((stat (final_name, &st) == 0) && (st.st_size > 0))
return final_name;
free (final_name);
}
}
return strdup (filename);
}
/*
* script_add: add a script to list of scripts
*/
struct t_plugin_script *
script_add (struct t_weechat_plugin *weechat_plugin,
struct t_plugin_script **scripts,
char *filename, char *name, char *author, char *version,
char *license, char *description, char *shutdown_func,
char *charset)
{
struct t_plugin_script *new_script;
if (strchr (name, ' '))
{
weechat_printf (NULL,
_("%s: error loading script \"%s\" (bad name, spaces "
"are forbidden)"),
weechat_plugin->name, name);
return NULL;
}
if (script_option_check_license
&& (weechat_strcmp_ignore_chars (weechat_plugin->license, license,
"0123456789-.,/\\()[]{}", 0) != 0))
{
weechat_printf (NULL,
_("%s%s: warning, license \"%s\" for script \"%s\" "
"differs from plugin license (\"%s\")"),
weechat_prefix ("error"), weechat_plugin->name,
license, name, weechat_plugin->license);
}
new_script = (struct t_plugin_script *)malloc (sizeof (struct t_plugin_script));
if (new_script)
{
new_script->filename = strdup (filename);
new_script->interpreter = NULL;
new_script->name = strdup (name);
new_script->author = strdup (author);
new_script->version = strdup (version);
new_script->license = strdup (license);
new_script->description = strdup (description);
new_script->shutdown_func = (shutdown_func) ?
strdup (shutdown_func) : NULL;
new_script->charset = (charset) ? strdup (charset) : NULL;
new_script->callbacks = NULL;
/* add new script to list */
if (*scripts)
(*scripts)->prev_script = new_script;
new_script->prev_script = NULL;
new_script->next_script = *scripts;
*scripts = new_script;
return new_script;
}
weechat_printf (NULL,
_("%s: error loading script \"%s\" (not enough memory)"),
weechat_plugin->name, name);
return NULL;
}
/*
* script_remove: remove a script from list of scripts
*/
void
script_remove (struct t_weechat_plugin *weechat_plugin,
struct t_plugin_script **scripts,
struct t_plugin_script *script)
{
struct t_script_callback *ptr_script_callback;
for (ptr_script_callback = script->callbacks; ptr_script_callback;
ptr_script_callback = ptr_script_callback->next_callback)
{
/* unhook */
if (ptr_script_callback->hook)
{
weechat_unhook (ptr_script_callback->hook);
}
/* free config file */
if (ptr_script_callback->config_file
&& !ptr_script_callback->config_section
&& !ptr_script_callback->config_option)
{
if (weechat_config_boolean (weechat_config_get_weechat ("plugins_save_config_on_unload")))
weechat_config_write (ptr_script_callback->config_file);
weechat_config_free (ptr_script_callback->config_file);
}
/* remove bar item */
if (ptr_script_callback->bar_item)
weechat_bar_item_remove (ptr_script_callback->bar_item);
}
/* remove all callbacks created by this script */
script_callback_remove_all (script);
/* 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);
if (script->charset)
free (script->charset);
/* remove script from list */
if (script->prev_script)
(script->prev_script)->next_script = script->next_script;
else
*scripts = script->next_script;
if (script->next_script)
(script->next_script)->prev_script = script->prev_script;
/* free script */
free (script);
}
/*
* script_completion: complete with list of scripts
*/
void
script_completion (struct t_weechat_plugin *weechat_plugin,
struct t_weelist *list,
struct t_plugin_script *scripts)
{
struct t_plugin_script *ptr_script;
for (ptr_script = scripts; ptr_script;
ptr_script = ptr_script->next_script)
{
weechat_list_add (list, ptr_script->name, WEECHAT_LIST_POS_SORT);
}
}
/*
* script_display_list: print list of scripts
*/
void
script_display_list (struct t_weechat_plugin *weechat_plugin,
struct t_plugin_script *scripts,
char *name, int full)
{
struct t_plugin_script *ptr_script;
weechat_printf (NULL, "");
weechat_printf (NULL,
/* TRANSLATORS: %s is language (for example "perl") */
_("%s scripts loaded:"),
weechat_plugin->name);
if (scripts)
{
for (ptr_script = scripts; ptr_script;
ptr_script = ptr_script->next_script)
{
if (!name || (weechat_strcasestr (ptr_script->name, name)))
{
weechat_printf (NULL,
" %s%s%s v%s - %s",
weechat_color ("color_chat_buffer"),
ptr_script->name,
weechat_color ("color_chat"),
ptr_script->version,
ptr_script->description);
if (full)
{
weechat_printf (NULL,
_(" file: %s"),
ptr_script->filename);
weechat_printf (NULL,
_(" written by \"%s\", license: %s"),
ptr_script->author,
ptr_script->license);
}
}
}
}
else
weechat_printf (NULL, _(" (none)"));
}
/*
* script_print_log: print script infos in log (usually for crash dump)
*/
void
script_print_log (struct t_weechat_plugin *weechat_plugin,
struct t_plugin_script *scripts)
{
struct t_plugin_script *ptr_script;
struct t_script_callback *ptr_script_callback;
weechat_log_printf ("");
weechat_log_printf ("***** \"%s\" plugin dump *****",
weechat_plugin->name);
for (ptr_script = scripts; ptr_script;
ptr_script = ptr_script->next_script)
{
weechat_log_printf ("");
weechat_log_printf ("[script %s (addr:0x%x)]", ptr_script->name, ptr_script);
weechat_log_printf (" filename. . . . . . : '%s'", ptr_script->filename);
weechat_log_printf (" interpreter . . . . : 0x%x", ptr_script->interpreter);
weechat_log_printf (" name. . . . . . . . : '%s'", ptr_script->name);
weechat_log_printf (" author. . . . . . . : '%s'", ptr_script->author);
weechat_log_printf (" version . . . . . . : '%s'", ptr_script->version);
weechat_log_printf (" license . . . . . . : '%s'", ptr_script->license);
weechat_log_printf (" description . . . . : '%s'", ptr_script->description);
weechat_log_printf (" shutdown_func . . . : '%s'", ptr_script->shutdown_func);
weechat_log_printf (" charset . . . . . . : '%s'", ptr_script->charset);
weechat_log_printf (" callbacks . . . . . : 0x%x", ptr_script->callbacks);
weechat_log_printf (" prev_script . . . . : 0x%x", ptr_script->prev_script);
weechat_log_printf (" next_script . . . . : 0x%x", ptr_script->next_script);
for (ptr_script_callback = ptr_script->callbacks; ptr_script_callback;
ptr_script_callback = ptr_script_callback->next_callback)
{
script_callback_print_log (weechat_plugin, ptr_script_callback);
}
}
weechat_log_printf ("");
weechat_log_printf ("***** End of \"%s\" plugin dump *****",
weechat_plugin->name);
}
|