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 + 3*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);
145 list[count++] = g_strconcat("fe_common_", name, NULL);
152 static void module_prefixes_free(char **list)
156 while (*pos != NULL) {
163 /* SYNTAX: LOAD <module> [<submodule>] */
164 static void cmd_load(const char *data)
166 char *rootmodule, *submodule;
167 char **module_prefixes;
170 g_return_if_fail(data != NULL);
172 if (!cmd_get_params(data, &free_arg, 2 , &rootmodule, &submodule))
175 if (*rootmodule == '\0')
178 module_prefixes = module_prefixes_get();
179 if (*submodule == '\0')
180 module_load(rootmodule, module_prefixes);
182 module_load_sub(rootmodule, submodule,
185 module_prefixes_free(module_prefixes);
188 cmd_params_free(free_arg);
191 /* SYNTAX: UNLOAD <module> [<submodule>] */
192 static void cmd_unload(const char *data)
195 MODULE_FILE_REC *file;
196 char *rootmodule, *submodule;
199 g_return_if_fail(data != NULL);
201 if (!cmd_get_params(data, &free_arg, 2 , &rootmodule, &submodule))
203 if (*rootmodule == '\0') cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
205 module = module_find(rootmodule);
206 if (module != NULL) {
207 if (*submodule == '\0')
208 module_unload(module);
210 file = module_file_find(module, submodule);
212 module_file_unload(file);
218 if (module == NULL) {
219 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
220 TXT_MODULE_NOT_LOADED, rootmodule, submodule);
223 cmd_params_free(free_arg);
226 void fe_modules_init(void)
228 signal_add("module error", (SIGNAL_FUNC) sig_module_error);
229 signal_add("module loaded", (SIGNAL_FUNC) sig_module_loaded);
230 signal_add("module unloaded", (SIGNAL_FUNC) sig_module_unloaded);
232 command_bind("load", NULL, (SIGNAL_FUNC) cmd_load);
233 command_bind("unload", NULL, (SIGNAL_FUNC) cmd_unload);
236 void fe_modules_deinit(void)
238 signal_remove("module error", (SIGNAL_FUNC) sig_module_error);
239 signal_remove("module loaded", (SIGNAL_FUNC) sig_module_loaded);
240 signal_remove("module unloaded", (SIGNAL_FUNC) sig_module_unloaded);
242 command_unbind("load", (SIGNAL_FUNC) cmd_load);
243 command_unbind("unload", (SIGNAL_FUNC) cmd_unload);
246 #else /* !HAVE_GMODULE */
248 static void cmd_load(const char *data)
250 printtext(NULL, NULL, MSGLEVEL_CLIENTERROR,
251 "Dynamic modules loading not supported");
254 void fe_modules_init(void)
256 command_bind("load", NULL, (SIGNAL_FUNC) cmd_load);
259 void fe_modules_deinit(void)
261 command_unbind("load", (SIGNAL_FUNC) cmd_load);