3c47a6d7832119d9bc0ad0abe025e1c8fbe72b9d
[silc.git] / apps / irssi / src / perl / perl-fe.c
1 /*
2  perl-core.c : irssi
3
4     Copyright (C) 1999-2001 Timo Sirainen
5
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.
10
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.
15
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
19 */
20
21 #include "module-fe.h"
22 #include "modules.h"
23 #include "module-formats.h"
24 #include "signals.h"
25 #include "commands.h"
26 #include "levels.h"
27
28 #include "printtext.h"
29 #include "completion.h"
30
31 #include "perl-core.h"
32
33 static void cmd_script(const char *data, SERVER_REC *server, void *item)
34 {
35         if (*data == '\0')
36                 data = "list";
37
38         command_runsub("script", data, server, item);
39 }
40
41 static void cmd_script_exec(const char *data)
42 {
43         PERL_SCRIPT_REC *script;
44         GHashTable *optlist;
45         char *code;
46         void *free_arg;
47
48         if (!cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS |
49                             PARAM_FLAG_GETREST,
50                             "script exec", &optlist, &code))
51                 return;
52
53         if (*code == '\0')
54                 cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
55
56         script = perl_script_load_data(code);
57         if (script != NULL &&
58             g_hash_table_lookup(optlist, "permanent") == NULL) {
59                 /* not a permanent script, unload immediately */
60                 perl_script_unload(script);
61         }
62
63
64         cmd_params_free(free_arg);
65 }
66
67 static void cmd_script_load(const char *data)
68 {
69         PERL_SCRIPT_REC *script;
70         char *fname, *path;
71         void *free_arg;
72
73         if (!cmd_get_params(data, &free_arg, 1, &path))
74                 return;
75
76         if (*path == '\0')
77                 cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
78
79         fname = perl_script_get_path(path);
80         if (fname == NULL) {
81                 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
82                             TXT_SCRIPT_NOT_FOUND, data);
83         } else {
84                 script = perl_script_load_file(fname);
85                 if (script != NULL) {
86                         printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
87                                     TXT_SCRIPT_LOADED,
88                                     script->name, script->path);
89                 }
90                 g_free(fname);
91         }
92         cmd_params_free(free_arg);
93 }
94
95 static void cmd_script_unload(const char *data)
96 {
97         PERL_SCRIPT_REC *script;
98         char *name;
99         void *free_arg;
100
101         if (!cmd_get_params(data, &free_arg, 1, &name))
102                 return;
103
104         if (*name == '\0')
105                 cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
106
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);
112         } else {
113                 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
114                             TXT_SCRIPT_UNLOADED, script->name);
115                 perl_script_unload(script);
116         }
117         cmd_params_free(free_arg);
118 }
119
120 static void cmd_script_reset(const char *data)
121 {
122         perl_scripts_deinit();
123         perl_scripts_init();
124 }
125
126 static void cmd_script_list(void)
127 {
128         GSList *tmp;
129         GString *data;
130
131         if (perl_scripts == NULL) {
132                 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
133                             TXT_NO_SCRIPTS_LOADED);
134                 return;
135         }
136
137         printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
138                     TXT_SCRIPT_LIST_HEADER);
139
140         data = g_string_new(NULL);
141         for (tmp = perl_scripts; tmp != NULL; tmp = tmp->next) {
142                 PERL_SCRIPT_REC *rec = tmp->data;
143
144                 if (rec->path != NULL)
145                         g_string_assign(data, rec->path);
146                 else {
147                         g_string_assign(data, rec->data);
148                         if (data->len > 50) {
149                                 g_string_truncate(data, 50);
150                                 g_string_append(data, " ...");
151                         }
152                 }
153
154                 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
155                             TXT_SCRIPT_LIST_LINE, rec->name, data->str);
156         }
157         g_string_free(data, TRUE);
158
159         printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
160                     TXT_SCRIPT_LIST_FOOTER);
161 }
162
163 static void sig_script_error(PERL_SCRIPT_REC *script, const char *error)
164 {
165         printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
166                     TXT_SCRIPT_ERROR, script == NULL ? "??" : script->name);
167
168         printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "%[-s]%s", error);
169 }
170
171 static void sig_complete_load(GList **list, WINDOW_REC *window,
172                               const char *word, const char *line,
173                               int *want_space)
174 {
175         char *user_dir;
176
177         if (*line != '\0')
178                 return;
179
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));
184         g_free(user_dir);
185
186         if (*list != NULL) {
187                 *want_space = FALSE;
188                 signal_stop();
189         }
190 }
191
192 static GList *script_complete(const char *name)
193 {
194         GSList *tmp;
195         GList *list;
196         int len;
197
198         list = NULL;
199         len = strlen(name);
200         for (tmp = perl_scripts; tmp != NULL; tmp = tmp->next) {
201                 PERL_SCRIPT_REC *rec = tmp->data;
202
203                 if (strncmp(rec->name, name, len) == 0)
204                         list = g_list_append(list, g_strdup(rec->name));
205         }
206
207         return list;
208 }
209
210 static void sig_complete_unload(GList **list, WINDOW_REC *window,
211                                 const char *word, const char *line,
212                                 int *want_space)
213 {
214         if (*line != '\0')
215                 return;
216
217         /* completing script parameter for /SCRIPT UNLOAD */
218         *list = script_complete(word);
219         if (*list != NULL)
220                 signal_stop();
221 }
222
223 void fe_perl_init(void)
224 {
225         theme_register(feperl_formats);
226
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");
234
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);
238
239         perl_core_print_script_error(FALSE);
240         module_register("perl", "fe");
241 }
242
243 void fe_perl_deinit(void)
244 {
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);
251
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);
255
256         perl_core_print_script_error(TRUE);
257 }