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
|
/*
* 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_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;
/* make gcc happy */
(void) plugin;
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;
}
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;
/* 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;
}
/* 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 message handlers */
ptr_handler = plugin->handlers;
while (ptr_handler)
{
ptr_arg1 = NULL;
if (ptr_handler->type == HANDLER_MESSAGE)
ptr_arg1 = ptr_handler->irc_command;
else if (ptr_handler->type == 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_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;
}
|