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