addition of silc.css
[crypto.git] / apps / irssi / src / fe-common / core / fe-modules.c
1 /*
2  fe-common-core.c : irssi
3
4     Copyright (C) 1999-2000 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.h"
22 #include "modules.h"
23 #include "module-formats.h"
24 #include "signals.h"
25 #include "commands.h"
26 #include "levels.h"
27 #include "chat-protocols.h"
28
29 #include "printtext.h"
30
31 static void sig_module_error(void *number, const char *module,
32                              const char *data)
33 {
34         switch (GPOINTER_TO_INT(number)) {
35         case MODULE_ERROR_ALREADY_LOADED:
36                 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
37                             TXT_MODULE_ALREADY_LOADED, module);
38                 break;
39         case MODULE_ERROR_LOAD:
40                 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
41                             TXT_MODULE_LOAD_ERROR, module, data);
42                 break;
43         case MODULE_ERROR_INVALID:
44                 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
45                             TXT_MODULE_INVALID, module);
46                 break;
47         }
48 }
49
50 static void sig_module_loaded(MODULE_REC *rec)
51 {
52         printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
53                     TXT_MODULE_LOADED, rec->name);
54 }
55
56 static void sig_module_unloaded(MODULE_REC *rec)
57 {
58         printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
59                     TXT_MODULE_UNLOADED, rec->name);
60 }
61
62 static void cmd_load_list(void)
63 {
64         GSList *tmp;
65
66         printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_MODULE_HEADER);
67         for (tmp = modules; tmp != NULL; tmp = tmp->next) {
68                 MODULE_REC *rec = tmp->data;
69
70                 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
71                             TXT_MODULE_LINE, rec->name);
72         }
73         printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_MODULE_FOOTER);
74 }
75
76 static char **module_prefixes_get(void)
77 {
78         GSList *tmp;
79         char **list, *name;
80         int count;
81
82         list = g_new(char *, 2 + 2*g_slist_length(chat_protocols));
83         list[0] = "fe";
84
85         count = 1;
86         for (tmp = chat_protocols; tmp != NULL; tmp = tmp->next) {
87                 CHAT_PROTOCOL_REC *rec = tmp->data;
88
89                 name = g_strdup(rec->name);
90                 g_strdown(name);
91
92                 list[count++] = name;
93                 list[count++] = g_strconcat("fe_", name, NULL);
94         }
95         list[count] = NULL;
96
97         return list;
98 }
99
100 static void module_prefixes_free(char **list)
101 {
102         char **pos = list+1;
103
104         while (*pos != NULL) {
105                 g_free(*pos);
106                 pos++;
107         }
108         g_free(list);
109 }
110
111 /* SYNTAX: LOAD <module> */
112 static void cmd_load(const char *data)
113 {
114 #ifdef HAVE_GMODULE
115         char **module_prefixes;
116
117         g_return_if_fail(data != NULL);
118         if (*data == '\0')
119                 cmd_load_list();
120         else {
121                 module_prefixes = module_prefixes_get();
122                 module_load(data, module_prefixes);
123                 module_prefixes_free(module_prefixes);
124         }
125 #else
126         printtext(NULL, NULL, MSGLEVEL_CLIENTERROR,
127                   "Dynamic modules loading not supported");
128 #endif
129 }
130
131 /* SYNTAX: UNLOAD <module> */
132 static void cmd_unload(const char *data)
133 {
134         MODULE_REC *rec;
135
136         g_return_if_fail(data != NULL);
137         if (*data == '\0') cmd_return_error(CMDERR_NOT_ENOUGH_PARAMS);
138
139         rec = module_find(data);
140         if (rec != NULL) module_unload(rec);
141 }
142
143 void fe_modules_init(void)
144 {
145         signal_add("module error", (SIGNAL_FUNC) sig_module_error);
146         signal_add("module loaded", (SIGNAL_FUNC) sig_module_loaded);
147         signal_add("module unloaded", (SIGNAL_FUNC) sig_module_unloaded);
148
149         command_bind("load", NULL, (SIGNAL_FUNC) cmd_load);
150         command_bind("unload", NULL, (SIGNAL_FUNC) cmd_unload);
151 }
152
153 void fe_modules_deinit(void)
154 {
155         signal_remove("module error", (SIGNAL_FUNC) sig_module_error);
156         signal_remove("module loaded", (SIGNAL_FUNC) sig_module_loaded);
157         signal_remove("module unloaded", (SIGNAL_FUNC) sig_module_unloaded);
158
159         command_unbind("load", (SIGNAL_FUNC) cmd_load);
160         command_unbind("unload", (SIGNAL_FUNC) cmd_unload);
161 }