4 Copyright (C) 1999-2001 Timo Sirainen
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "module-fe.h"
23 #include "module-formats.h"
28 #include "printtext.h"
29 #include "completion.h"
31 #include "perl-core.h"
33 static void cmd_script(const char *data, SERVER_REC *server, void *item)
38 command_runsub("script", data, server, item);
41 static void cmd_script_exec(const char *data)
43 PERL_SCRIPT_REC *script;
48 if (!cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS |
50 "script exec", &optlist, &code))
54 cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
56 script = perl_script_load_data(code);
58 g_hash_table_lookup(optlist, "permanent") == NULL) {
59 /* not a permanent script, unload immediately */
60 perl_script_unload(script);
64 cmd_params_free(free_arg);
67 static void cmd_script_load(const char *data)
69 PERL_SCRIPT_REC *script;
73 if (!cmd_get_params(data, &free_arg, 1, &path))
77 cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
79 fname = perl_script_get_path(path);
81 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
82 TXT_SCRIPT_NOT_FOUND, data);
84 script = perl_script_load_file(fname);
86 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
88 script->name, script->path);
92 cmd_params_free(free_arg);
95 static void cmd_script_unload(const char *data)
97 PERL_SCRIPT_REC *script;
101 if (!cmd_get_params(data, &free_arg, 1, &name))
105 cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
107 script_fix_name(name);
108 script = perl_script_find(name);
109 if (script == NULL) {
110 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
111 TXT_SCRIPT_NOT_LOADED, name);
113 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
114 TXT_SCRIPT_UNLOADED, script->name);
115 perl_script_unload(script);
117 cmd_params_free(free_arg);
120 static void cmd_script_reset(const char *data)
122 perl_scripts_deinit();
126 static void cmd_script_list(void)
131 if (perl_scripts == NULL) {
132 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
133 TXT_NO_SCRIPTS_LOADED);
137 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
138 TXT_SCRIPT_LIST_HEADER);
140 data = g_string_new(NULL);
141 for (tmp = perl_scripts; tmp != NULL; tmp = tmp->next) {
142 PERL_SCRIPT_REC *rec = tmp->data;
144 if (rec->path != NULL)
145 g_string_assign(data, rec->path);
147 g_string_assign(data, rec->data);
148 if (data->len > 50) {
149 g_string_truncate(data, 50);
150 g_string_append(data, " ...");
154 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
155 TXT_SCRIPT_LIST_LINE, rec->name, data->str);
157 g_string_free(data, TRUE);
159 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
160 TXT_SCRIPT_LIST_FOOTER);
163 static void sig_script_error(PERL_SCRIPT_REC *script, const char *error)
165 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
166 TXT_SCRIPT_ERROR, script == NULL ? "??" : script->name);
168 printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "%[-s]%s", error);
171 static void sig_complete_load(GList **list, WINDOW_REC *window,
172 const char *word, const char *line,
180 /* completing filename parameter for /SCRIPT LOAD */
181 user_dir = g_strdup_printf("%s/scripts", get_irssi_dir());
182 *list = filename_complete(word, user_dir);
183 *list = g_list_concat(*list, filename_complete(word, SCRIPTDIR));
192 static GList *script_complete(const char *name)
200 for (tmp = perl_scripts; tmp != NULL; tmp = tmp->next) {
201 PERL_SCRIPT_REC *rec = tmp->data;
203 if (strncmp(rec->name, name, len) == 0)
204 list = g_list_append(list, g_strdup(rec->name));
210 static void sig_complete_unload(GList **list, WINDOW_REC *window,
211 const char *word, const char *line,
217 /* completing script parameter for /SCRIPT UNLOAD */
218 *list = script_complete(word);
223 void fe_perl_init(void)
225 theme_register(feperl_formats);
227 command_bind("script", NULL, (SIGNAL_FUNC) cmd_script);
228 command_bind("script exec", NULL, (SIGNAL_FUNC) cmd_script_exec);
229 command_bind("script load", NULL, (SIGNAL_FUNC) cmd_script_load);
230 command_bind("script unload", NULL, (SIGNAL_FUNC) cmd_script_unload);
231 command_bind("script reset", NULL, (SIGNAL_FUNC) cmd_script_reset);
232 command_bind("script list", NULL, (SIGNAL_FUNC) cmd_script_list);
233 command_set_options("script exec", "permanent");
235 signal_add("script error", (SIGNAL_FUNC) sig_script_error);
236 signal_add("complete command script load", (SIGNAL_FUNC) sig_complete_load);
237 signal_add("complete command script unload", (SIGNAL_FUNC) sig_complete_unload);
239 perl_core_print_script_error(FALSE);
240 module_register("perl", "fe");
243 void fe_perl_deinit(void)
245 command_unbind("script", (SIGNAL_FUNC) cmd_script);
246 command_unbind("script exec", (SIGNAL_FUNC) cmd_script_exec);
247 command_unbind("script load", (SIGNAL_FUNC) cmd_script_load);
248 command_unbind("script unload", (SIGNAL_FUNC) cmd_script_unload);
249 command_unbind("script reset", (SIGNAL_FUNC) cmd_script_reset);
250 command_unbind("script list", (SIGNAL_FUNC) cmd_script_list);
252 signal_remove("script error", (SIGNAL_FUNC) sig_script_error);
253 signal_remove("complete command script load", (SIGNAL_FUNC) sig_complete_load);
254 signal_remove("complete command script unload", (SIGNAL_FUNC) sig_complete_unload);
256 perl_core_print_script_error(TRUE);