Mercurial > pidgin
annotate src/module.c @ 2407:2701aafc7baf
[gaim-migrate @ 2420]
argh
committer: Tailor Script <tailor@pidgin.im>
author | Eric Warmenhoven <eric@warmenhoven.org> |
---|---|
date | Mon, 01 Oct 2001 18:14:22 +0000 |
parents | 6e637ad18494 |
children | 70cb0ce6991a |
rev | line source |
---|---|
2393 | 1 /* |
2 * gaim | |
3 * | |
4 * Copyright (C) 1998-1999, Mark Spencer <markster@marko.net> | |
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 * The Plug-in plug | |
22 * | |
23 * Plugin support is currently being maintained by Mike Saraf | |
24 * msaraf@dwc.edu | |
25 * | |
26 * Well, I didn't see any work done on it for a while, so I'm going to try | |
27 * my hand at it. - Eric warmenhoven@yahoo.com | |
28 * | |
29 * Mike is my roomate. I can assure you that he's lazy :-P -- Rob rob@marko.net | |
30 * | |
31 */ | |
32 | |
33 #ifdef HAVE_CONFIG_H | |
34 #include <config.h> | |
35 #endif | |
36 | |
37 #ifdef GAIM_PLUGINS | |
38 | |
39 #include <string.h> | |
40 #include <sys/time.h> | |
41 | |
42 #include <sys/types.h> | |
43 #include <sys/stat.h> | |
44 | |
45 #include <unistd.h> | |
46 #include <stdio.h> | |
47 #include <stdlib.h> | |
48 #include "gaim.h" | |
49 | |
50 /* ------------------ Global Variables ----------------------- */ | |
51 | |
52 GList *plugins = NULL; | |
53 GList *callbacks = NULL; | |
54 | |
2405
6e637ad18494
[gaim-migrate @ 2418]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2393
diff
changeset
|
55 char *last_dir = NULL; |
6e637ad18494
[gaim-migrate @ 2418]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2393
diff
changeset
|
56 |
2393 | 57 /* --------------- Function Declarations --------------------- */ |
58 | |
59 struct gaim_plugin * load_plugin(char *); | |
60 void unload_plugin(struct gaim_plugin *p); | |
61 struct gaim_plugin *reload_plugin(struct gaim_plugin *p); | |
62 | |
63 void gaim_signal_connect(GModule *, enum gaim_event, void *, void *); | |
64 void gaim_signal_disconnect(GModule *, enum gaim_event, void *); | |
65 void gaim_plugin_unload(GModule *); | |
66 | |
67 /* --------------- Static Function Declarations ------------- */ | |
68 | |
69 static void plugin_remove_callbacks(GModule *); | |
70 | |
71 /* ------------------ Code Below ---------------------------- */ | |
72 | |
73 struct gaim_plugin *load_plugin(char *filename) | |
74 { | |
75 struct gaim_plugin *plug; | |
76 GList *c = plugins; | |
77 char *(*gaim_plugin_init)(GModule *); | |
78 char *(*cfunc)(); | |
79 char *error; | |
80 char *retval; | |
81 | |
82 if (!g_module_supported()) | |
83 return NULL; | |
84 if (filename && !strlen(filename)) | |
85 return NULL; | |
86 | |
87 while (filename && c) { | |
88 plug = (struct gaim_plugin *)c->data; | |
89 if (!strcmp(filename, g_module_name(plug->handle))) { | |
90 /* just need to reload plugin */ | |
91 return reload_plugin(plug); | |
92 } else | |
93 c = g_list_next(c); | |
94 } | |
95 plug = g_malloc(sizeof *plug); | |
96 | |
2405
6e637ad18494
[gaim-migrate @ 2418]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2393
diff
changeset
|
97 if (last_dir) |
6e637ad18494
[gaim-migrate @ 2418]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2393
diff
changeset
|
98 g_free(last_dir); |
6e637ad18494
[gaim-migrate @ 2418]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2393
diff
changeset
|
99 last_dir = g_dirname(filename); |
6e637ad18494
[gaim-migrate @ 2418]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2393
diff
changeset
|
100 |
2393 | 101 debug_printf("Loading %s\n", filename); |
102 plug->handle = g_module_open(filename, 0); | |
103 if (!plug->handle) { | |
104 error = (char *)g_module_error(); | |
105 do_error_dialog(error, _("Plugin Error")); | |
106 g_free(plug); | |
107 return NULL; | |
108 } | |
109 | |
110 if (!g_module_symbol(plug->handle, "gaim_plugin_init", (gpointer *)&gaim_plugin_init)) { | |
111 do_error_dialog(g_module_error(), _("Plugin Error")); | |
112 g_module_close(plug->handle); | |
113 g_free(plug); | |
114 return NULL; | |
115 } | |
116 | |
117 retval = (*gaim_plugin_init)(plug->handle); | |
118 debug_printf("loaded plugin returned %s\n", retval ? retval : "NULL"); | |
119 if (retval) { | |
120 plugin_remove_callbacks(plug->handle); | |
121 do_error_dialog(retval, _("Plugin Error")); | |
122 g_module_close(plug->handle); | |
123 g_free(plug); | |
124 return NULL; | |
125 } | |
126 | |
127 plugins = g_list_append(plugins, plug); | |
128 | |
129 if (g_module_symbol(plug->handle, "name", (gpointer *)&cfunc)) { | |
130 plug->name = (*cfunc)(); | |
131 } else { | |
132 plug->name = NULL; | |
133 } | |
134 | |
135 if (g_module_symbol(plug->handle, "description", (gpointer *)&cfunc)) | |
136 plug->description = (*cfunc)(); | |
137 else | |
138 plug->description = NULL; | |
139 | |
140 save_prefs(); | |
141 return plug; | |
142 } | |
143 | |
144 static void unload_gaim_plugin(struct gaim_plugin *p) | |
145 { | |
146 void (*gaim_plugin_remove)(); | |
147 | |
148 debug_printf("Unloading %s\n", g_module_name(p->handle)); | |
149 | |
150 /* Attempt to call the plugin's remove function (if there) */ | |
151 if (g_module_symbol(p->handle, "gaim_plugin_remove", (gpointer *)&gaim_plugin_remove)) | |
152 (*gaim_plugin_remove)(); | |
153 | |
154 plugin_remove_callbacks(p->handle); | |
155 | |
156 plugins = g_list_remove(plugins, p); | |
157 g_free(p); | |
158 save_prefs(); | |
159 } | |
160 | |
161 void unload_plugin(struct gaim_plugin *p) | |
162 { | |
163 GModule *handle = p->handle; | |
164 unload_gaim_plugin(p); | |
165 g_module_close(handle); | |
166 } | |
167 | |
168 static gboolean unload_timeout(gpointer handle) | |
169 { | |
170 g_module_close(handle); | |
171 return FALSE; | |
172 } | |
173 | |
174 void gaim_plugin_unload(GModule *handle) | |
175 { | |
176 g_timeout_add(5000, unload_timeout, handle); | |
177 } | |
178 | |
179 /* Do unload/load cycle of plugin. */ | |
180 struct gaim_plugin *reload_plugin(struct gaim_plugin *p) | |
181 { | |
182 char file[1024]; | |
183 GModule *handle = p->handle; | |
184 | |
185 strncpy(file, g_module_name(handle), sizeof(file)); | |
186 file[sizeof(file) - 1] = '\0'; | |
187 | |
188 debug_printf("Reloading %s\n", file); | |
189 | |
190 /* Unload */ | |
191 unload_plugin(p); | |
192 | |
193 /* Load */ | |
194 return load_plugin(file); | |
195 } | |
196 | |
197 /* Remove all callbacks associated with plugin handle */ | |
198 static void plugin_remove_callbacks(GModule *handle) | |
199 { | |
200 GList *c = callbacks; | |
201 struct gaim_callback *g; | |
202 | |
203 debug_printf("%d callbacks to search\n", g_list_length(callbacks)); | |
204 | |
205 while (c) { | |
206 g = (struct gaim_callback *)c->data; | |
207 if (g->handle == handle) { | |
208 c = g_list_next(c); | |
209 callbacks = g_list_remove(callbacks, (gpointer)g); | |
210 debug_printf("Removing callback, %d remain\n", g_list_length(callbacks)); | |
211 } else | |
212 c = g_list_next(c); | |
213 } | |
214 } | |
215 | |
216 void gaim_signal_connect(GModule *handle, enum gaim_event which, void *func, void *data) | |
217 { | |
218 struct gaim_callback *call = g_new0(struct gaim_callback, 1); | |
219 call->handle = handle; | |
220 call->event = which; | |
221 call->function = func; | |
222 call->data = data; | |
223 | |
224 callbacks = g_list_append(callbacks, call); | |
225 debug_printf("Adding callback %d\n", g_list_length(callbacks)); | |
226 } | |
227 | |
228 void gaim_signal_disconnect(GModule *handle, enum gaim_event which, void *func) | |
229 { | |
230 GList *c = callbacks; | |
231 struct gaim_callback *g = NULL; | |
232 | |
233 while (c) { | |
234 g = (struct gaim_callback *)c->data; | |
235 if (handle == g->handle && func == g->function) { | |
236 callbacks = g_list_remove(callbacks, c->data); | |
237 g_free(g); | |
238 c = callbacks; | |
239 if (c == NULL) | |
240 break; | |
241 } | |
242 c = g_list_next(c); | |
243 } | |
244 } | |
245 | |
246 #endif /* GAIM_PLUGINS */ | |
247 | |
248 static char *event_name(enum gaim_event event) | |
249 { | |
250 static char buf[128]; | |
251 switch (event) { | |
252 case event_signon: | |
253 sprintf(buf, "event_signon"); | |
254 break; | |
255 case event_signoff: | |
256 sprintf(buf, "event_signoff"); | |
257 break; | |
258 case event_away: | |
259 sprintf(buf, "event_away"); | |
260 break; | |
261 case event_back: | |
262 sprintf(buf, "event_back"); | |
263 break; | |
264 case event_im_recv: | |
265 sprintf(buf, "event_im_recv"); | |
266 break; | |
267 case event_im_send: | |
268 sprintf(buf, "event_im_send"); | |
269 break; | |
270 case event_buddy_signon: | |
271 sprintf(buf, "event_buddy_signon"); | |
272 break; | |
273 case event_buddy_signoff: | |
274 sprintf(buf, "event_buddy_signoff"); | |
275 break; | |
276 case event_buddy_away: | |
277 sprintf(buf, "event_buddy_away"); | |
278 break; | |
279 case event_buddy_back: | |
280 sprintf(buf, "event_buddy_back"); | |
281 break; | |
282 case event_buddy_idle: | |
283 sprintf(buf, "event_buddy_idle"); | |
284 break; | |
285 case event_buddy_unidle: | |
286 sprintf(buf, "event_buddy_unidle"); | |
287 break; | |
288 case event_blist_update: | |
289 sprintf(buf, "event_blist_update"); | |
290 break; | |
291 case event_chat_invited: | |
292 sprintf(buf, "event_chat_invited"); | |
293 break; | |
294 case event_chat_join: | |
295 sprintf(buf, "event_chat_join"); | |
296 break; | |
297 case event_chat_leave: | |
298 sprintf(buf, "event_chat_leave"); | |
299 break; | |
300 case event_chat_buddy_join: | |
301 sprintf(buf, "event_chat_buddy_join"); | |
302 break; | |
303 case event_chat_buddy_leave: | |
304 sprintf(buf, "event_chat_buddy_leave"); | |
305 break; | |
306 case event_chat_recv: | |
307 sprintf(buf, "event_chat_recv"); | |
308 break; | |
309 case event_chat_send: | |
310 sprintf(buf, "event_chat_send"); | |
311 break; | |
312 case event_warned: | |
313 sprintf(buf, "event_warned"); | |
314 break; | |
315 case event_error: | |
316 sprintf(buf, "event_error"); | |
317 break; | |
318 case event_quit: | |
319 sprintf(buf, "event_quit"); | |
320 break; | |
321 case event_new_conversation: | |
322 sprintf(buf, "event_new_conversation"); | |
323 break; | |
324 case event_set_info: | |
325 sprintf(buf, "event_set_info"); | |
326 break; | |
327 case event_draw_menu: | |
328 sprintf(buf, "event_draw_menu"); | |
329 break; | |
330 case event_im_displayed_sent: | |
331 sprintf(buf, "event_im_displayed_sent"); | |
332 break; | |
333 case event_im_displayed_rcvd: | |
334 sprintf(buf, "event_im_displayed_rcvd"); | |
335 break; | |
336 case event_chat_send_invite: | |
337 sprintf(buf, "event_chat_send_invite"); | |
338 break; | |
339 default: | |
340 sprintf(buf, "event_unknown"); | |
341 break; | |
342 } | |
343 return buf; | |
344 } | |
345 | |
346 int plugin_event(enum gaim_event event, void *arg1, void *arg2, void *arg3, void *arg4) | |
347 { | |
348 #ifdef USE_PERL | |
349 char buf[BUF_LONG]; | |
350 #endif | |
351 #ifdef GAIM_PLUGINS | |
352 GList *c = callbacks; | |
353 struct gaim_callback *g; | |
354 | |
355 while (c) { | |
356 void (*zero)(void *); | |
357 void (*one)(void *, void *); | |
358 void (*two)(void *, void *, void *); | |
359 void (*three)(void *, void *, void *, void *); | |
360 void (*four)(void *, void *, void *, void *, void *); | |
361 | |
362 g = (struct gaim_callback *)c->data; | |
363 if (g->event == event && g->function !=NULL) { | |
364 switch (event) { | |
365 | |
366 /* no args */ | |
367 case event_blist_update: | |
368 case event_quit: | |
369 zero = g->function; | |
370 (*zero)(g->data); | |
371 break; | |
372 | |
373 /* one arg */ | |
374 case event_signon: | |
375 case event_signoff: | |
376 case event_new_conversation: | |
377 case event_error: | |
378 one = g->function; | |
379 (*one)(arg1, g->data); | |
380 break; | |
381 | |
382 /* two args */ | |
383 case event_buddy_signon: | |
384 case event_buddy_signoff: | |
385 case event_buddy_away: | |
386 case event_buddy_back: | |
387 case event_buddy_idle: | |
388 case event_buddy_unidle: | |
389 case event_chat_leave: | |
390 case event_set_info: | |
391 case event_draw_menu: | |
392 two = g->function; | |
393 (*two)(arg1, arg2, g->data); | |
394 break; | |
395 | |
396 /* three args */ | |
397 case event_im_send: | |
398 case event_im_displayed_sent: | |
399 case event_chat_join: | |
400 case event_chat_buddy_join: | |
401 case event_chat_buddy_leave: | |
402 case event_chat_send: | |
403 case event_away: | |
404 case event_back: | |
405 case event_warned: | |
406 three = g->function; | |
407 (*three)(arg1, arg2, arg3, g->data); | |
408 break; | |
409 | |
410 /* four args */ | |
411 case event_im_recv: | |
412 case event_chat_recv: | |
413 case event_im_displayed_rcvd: | |
414 case event_chat_send_invite: | |
415 case event_chat_invited: | |
416 four = g->function; | |
417 (*four)(arg1, arg2, arg3, arg4, g->data); | |
418 break; | |
419 | |
420 default: | |
421 debug_printf("unknown event %d\n", event); | |
422 break; | |
423 } | |
424 } | |
425 c = c->next; | |
426 } | |
427 #endif /* GAIM_PLUGINS */ | |
428 #ifdef USE_PERL | |
429 switch (event) { | |
430 case event_signon: | |
431 case event_signoff: | |
432 case event_away: | |
433 case event_back: | |
434 g_snprintf(buf, sizeof buf, "%lu", (unsigned long)arg1); | |
435 break; | |
436 case event_im_recv: | |
437 g_snprintf(buf, sizeof buf, "%lu \"%s\" %s", (unsigned long)arg1, | |
438 *(char **)arg2 ? *(char **)arg2 : "(null)", | |
439 *(char **)arg3 ? *(char **)arg3 : "(null)"); | |
440 break; | |
441 case event_im_send: | |
442 g_snprintf(buf, sizeof buf, "%lu \"%s\" %s", (unsigned long)arg1, | |
443 (char *)arg2, *(char **)arg3 ? *(char **)arg3 : "(null)"); | |
444 break; | |
445 case event_buddy_signon: | |
446 case event_buddy_signoff: | |
447 case event_set_info: | |
448 case event_buddy_away: | |
449 case event_buddy_back: | |
450 case event_buddy_idle: | |
451 case event_buddy_unidle: | |
452 g_snprintf(buf, sizeof buf, "%lu \"%s\"", (unsigned long)arg1, (char *)arg2); | |
453 break; | |
454 case event_chat_invited: | |
455 g_snprintf(buf, sizeof buf, "%lu \"%s\" \"%s\" %s", (unsigned long)arg1, | |
456 (char *)arg2, (char *)arg3, arg4 ? (char *)arg4 : ""); | |
457 break; | |
458 case event_chat_join: | |
459 case event_chat_buddy_join: | |
460 case event_chat_buddy_leave: | |
461 g_snprintf(buf, sizeof buf, "%lu %d \"%s\"", (unsigned long)arg1, | |
462 (int)arg2, (char *)arg3); | |
463 break; | |
464 case event_chat_leave: | |
465 g_snprintf(buf, sizeof buf, "%lu %d", (unsigned long)arg1, (int)arg2); | |
466 break; | |
467 case event_chat_recv: | |
468 g_snprintf(buf, sizeof buf, "%lu %d \"%s\" %s", (unsigned long)arg1, | |
469 (int)arg2, (char *)arg3, (char *)arg4 ? (char *)arg4 : "(null)"); | |
470 break; | |
471 case event_chat_send_invite: | |
472 g_snprintf(buf, sizeof buf, "%lu %d \"%s\" %s", (unsigned long)arg1, | |
473 (int)arg2, (char *)arg3, *(char **)arg4 ? *(char **)arg4 : "(null)"); | |
474 break; | |
475 case event_chat_send: | |
476 g_snprintf(buf, sizeof buf, "%lu %d %s", (unsigned long)arg1, (int)arg2, | |
477 *(char **)arg3 ? *(char **)arg3 : "(null)"); | |
478 break; | |
479 case event_warned: | |
480 g_snprintf(buf, sizeof buf, "%lu \"%s\" %d", (unsigned long)arg1, | |
481 arg2 ? (char *)arg2 : "", (int)arg3); | |
482 break; | |
483 case event_quit: | |
484 buf[0] = 0; | |
485 break; | |
486 case event_new_conversation: | |
487 g_snprintf(buf, sizeof buf, "\"%s\"", (char *)arg1); | |
488 break; | |
489 case event_im_displayed_sent: | |
490 g_snprintf(buf, sizeof buf, "%lu \"%s\" %s", (unsigned long)arg1, | |
491 (char *)arg2, *(char **)arg3 ? *(char **)arg3 : "(null)"); | |
492 break; | |
493 case event_im_displayed_rcvd: | |
494 g_snprintf(buf, sizeof buf, "%lu \"%s\" %s", (unsigned long)arg1, | |
495 (char *)arg2, (char *)arg3 ? (char *)arg3 : "(null)"); | |
496 break; | |
497 default: | |
498 return 0; | |
499 } | |
500 return perl_event(event_name(event), buf); | |
501 #else | |
502 return 0; | |
503 #endif | |
504 } | |
505 | |
506 /* Calls the gaim_plugin_remove function in any loaded plugin that has one */ | |
507 #ifdef GAIM_PLUGINS | |
508 void remove_all_plugins() | |
509 { | |
510 GList *c = plugins; | |
511 struct gaim_plugin *p; | |
512 void (*gaim_plugin_remove)(); | |
513 | |
514 while (c) { | |
515 p = (struct gaim_plugin *)c->data; | |
516 if (g_module_symbol(p->handle, "gaim_plugin_remove", (gpointer *)&gaim_plugin_remove)) | |
517 (*gaim_plugin_remove)(); | |
518 g_free(p); | |
519 c = c->next; | |
520 } | |
521 } | |
522 #endif |