2 fe-common-core.c : irssi
4 Copyright (C) 1999-2000 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
23 #include "modules-load.h"
24 #include "module-formats.h"
28 #include "chat-protocols.h"
30 #include "printtext.h"
34 static void sig_module_error(void *number, const char *data,
35 const char *rootmodule, const char *submodule)
37 switch (GPOINTER_TO_INT(number)) {
38 case MODULE_ERROR_ALREADY_LOADED:
39 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
40 TXT_MODULE_ALREADY_LOADED, rootmodule, submodule);
42 case MODULE_ERROR_LOAD:
43 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
44 TXT_MODULE_LOAD_ERROR, rootmodule, submodule, data);
46 case MODULE_ERROR_INVALID:
47 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
48 TXT_MODULE_INVALID, rootmodule, submodule);
53 static void sig_module_loaded(MODULE_REC *module, MODULE_FILE_REC *file)
55 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
56 TXT_MODULE_LOADED, module->name, file->name);
59 static void sig_module_unloaded(MODULE_REC *module, MODULE_FILE_REC *file)
61 if (file != NULL && file->gmodule != NULL) {
62 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
63 TXT_MODULE_UNLOADED, module->name, file->name);
67 static int module_list_sub(MODULE_REC *module, int mark_type,
71 int all_dynamic, dynamic;
73 g_string_truncate(submodules, 0);
76 for (tmp = module->files; tmp != NULL; tmp = tmp->next) {
77 MODULE_FILE_REC *file = tmp->data;
79 /* if there's dynamic and static modules mixed, we'll need
80 to specify them separately */
82 dynamic = file->gmodule != NULL;
83 if (all_dynamic != -1 && all_dynamic != dynamic) {
84 return module_list_sub(module, TRUE,
87 all_dynamic = dynamic;
90 if (submodules->len > 0)
91 g_string_append_c(submodules, ' ');
92 g_string_append(submodules, file->name);
94 g_string_append(submodules, file->gmodule == NULL ?
95 " (static)" : " (dynamic)");
102 static void cmd_load_list(void)
109 submodules = g_string_new(NULL);
111 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_MODULE_HEADER);
112 for (tmp = modules; tmp != NULL; tmp = tmp->next) {
113 MODULE_REC *rec = tmp->data;
115 dynamic = module_list_sub(rec, FALSE, submodules);
116 type = dynamic == -1 ? "mixed" :
117 dynamic ? "dynamic" : "static";
119 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
120 TXT_MODULE_LINE, rec->name, type, submodules->str);
122 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_MODULE_FOOTER);
124 g_string_free(submodules, TRUE);
127 static char **module_prefixes_get(void)
133 list = g_new(char *, 2 + 2*g_slist_length(chat_protocols));
137 for (tmp = chat_protocols; tmp != NULL; tmp = tmp->next) {
138 CHAT_PROTOCOL_REC *rec = tmp->data;
140 name = g_strdup(rec->name);
143 list[count++] = name;
144 list[count++] = g_strconcat("fe_", name, NULL);
151 static void module_prefixes_free(char **list)
155 while (*pos != NULL) {
162 /* SYNTAX: LOAD <module> [<submodule>] */
163 static void cmd_load(const char *data)
165 char *rootmodule, *submodule;
166 char **module_prefixes;
169 g_return_if_fail(data != NULL);
171 if (!cmd_get_params(data, &free_arg, 2 , &rootmodule, &submodule))
174 if (*rootmodule == '\0')
177 module_prefixes = module_prefixes_get();
178 if (*submodule == '\0')
179 module_load(rootmodule, module_prefixes);
181 module_load_sub(rootmodule, submodule,
184 module_prefixes_free(module_prefixes);
187 cmd_params_free(free_arg);
190 /* SYNTAX: UNLOAD <module> [<submodule>] */
191 static void cmd_unload(const char *data)
194 MODULE_FILE_REC *file;
195 char *rootmodule, *submodule;
198 g_return_if_fail(data != NULL);
200 if (!cmd_get_params(data, &free_arg, 2 , &rootmodule, &submodule))
202 if (*rootmodule == '\0') cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
204 module = module_find(rootmodule);
205 if (module != NULL) {
206 if (*submodule == '\0')
207 module_unload(module);
209 file = module_file_find(module, submodule);
211 module_file_unload(file);
217 if (module == NULL) {
218 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
219 TXT_MODULE_NOT_LOADED, rootmodule, submodule);
222 cmd_params_free(free_arg);
225 void fe_modules_init(void)
227 signal_add("module error", (SIGNAL_FUNC) sig_module_error);
228 signal_add("module loaded", (SIGNAL_FUNC) sig_module_loaded);
229 signal_add("module unloaded", (SIGNAL_FUNC) sig_module_unloaded);
231 command_bind("load", NULL, (SIGNAL_FUNC) cmd_load);
232 command_bind("unload", NULL, (SIGNAL_FUNC) cmd_unload);
235 void fe_modules_deinit(void)
237 signal_remove("module error", (SIGNAL_FUNC) sig_module_error);
238 signal_remove("module loaded", (SIGNAL_FUNC) sig_module_loaded);
239 signal_remove("module unloaded", (SIGNAL_FUNC) sig_module_unloaded);
241 command_unbind("load", (SIGNAL_FUNC) cmd_load);
242 command_unbind("unload", (SIGNAL_FUNC) cmd_unload);
245 #else /* !HAVE_GMODULE */
247 static void cmd_load(const char *data)
249 printtext(NULL, NULL, MSGLEVEL_CLIENTERROR,
250 "Dynamic modules loading not supported");
253 void fe_modules_init(void)
255 command_bind("load", NULL, (SIGNAL_FUNC) cmd_load);
258 void fe_modules_deinit(void)
260 command_unbind("load", (SIGNAL_FUNC) cmd_load);