comparison plugins/perl/perl.c @ 6485:70d5122bc3ff

[gaim-migrate @ 6999] Removed the old event system and replaced it with a much better signal system. There will most likely be some bugs in this, but it seems to be working for now. Plugins can now generate their own signals, and other plugins can find those plugins and connect to them. This could give plugins a form of IPC. It's also useful for other things. It's rather flexible, except for the damn marshalling, but there's no way around that that I or the glib people can see. committer: Tailor Script <tailor@pidgin.im>
author Christian Hammond <chipx86@chipx86.com>
date Mon, 18 Aug 2003 01:03:43 +0000
parents 8f94cce8faa5
children cbd24b37350d
comparison
equal deleted inserted replaced
6484:5ced8e111473 6485:70d5122bc3ff
188 newXS ("GAIM::add_timeout_handler", XS_GAIM_add_timeout_handler, "GAIM"); 188 newXS ("GAIM::add_timeout_handler", XS_GAIM_add_timeout_handler, "GAIM");
189 189
190 newXS ("GAIM::play_sound", XS_GAIM_play_sound, "GAIM"); 190 newXS ("GAIM::play_sound", XS_GAIM_play_sound, "GAIM");
191 } 191 }
192 192
193 #if 0
194 #define COMPARE_EVENT(evt, sig, h) \
195 if (!strcmp(event_name, (evt))) \
196 { \
197 *signal_name = (sig); \
198 *handle = (h); \
199 return TRUE; \
200 }
201
202 static gboolean
203 convert_event_to_signal(const char *event_name, const char **signal_name,
204 void **handle)
205 {
206 void *conn_handle = gaim_connections_get_handle();
207 void *account_handle = gaim_accounts_get_handle();
208 void *conv_handle = gaim_conversations_get_handle();
209 void *blist_handle = gaim_get_blist();
210
211 COMPARE_EVENT("event_signon", "signed-on", conn_handle);
212 COMPARE_EVENT("event_signoff", "signed-off", conn_handle);
213
214 COMPARE_EVENT("event_away", "account-away", account_handle);
215 COMPARE_EVENT("event_back", "account-back", account_handle);
216 COMPARE_EVENT("event_warned", "account-warned", account_handle);
217 COMPARE_EVENT("event_set_info", "account-set-info", account_handle);
218 COMPARE_EVENT("event_connecting", "account-connecting", account_handle);
219
220 COMPARE_EVENT("event_im_recv", "received-im-msg", conv_handle);
221 COMPARE_EVENT("event_im_send", "sent-im-msg", conv_handle);
222 COMPARE_EVENT("event_chat_invited", "chat-invited", conv_handle);
223 COMPARE_EVENT("event_chat_join", "chat-joined", conv_handle);
224 COMPARE_EVENT("event_chat_leave", "chat-left", conv_handle);
225 COMPARE_EVENT("event_chat_buddy_join", "chat-buddy-joined", conv_handle);
226 COMPARE_EVENT("event_chat_buddy_leave", "chat-buddy-left", conv_handle);
227 COMPARE_EVENT("event_chat_recv", "received-chat-msg", conv_handle);
228 COMPARE_EVENT("event_chat_send", "sent-chat-msg", conv_handle);
229 COMPARE_EVENT("event_new_conversation", "conversation-created",
230 conv_handle);
231 COMPARE_EVENT("event_im_displayed_sent", "sending-im-msg", conv_handle);
232 COMPARE_EVENT("event_im_displayed_rcvd", NULL, NULL);
233 COMPARE_EVENT("event_chat_send_invite", "chat-inviting-user", conv_handle);
234 COMPARE_EVENT("event_got_typing", "buddy-typing", conv_handle);
235 COMPARE_EVENT("event_del_conversation", "deleting-conversation",
236 conv_handle);
237 COMPARE_EVENT("event_conversation_switch", "conversation-switched",
238 conv_handle);
239
240 COMPARE_EVENT("event_buddy_signon", "buddy-signed-on", blist_handle);
241 COMPARE_EVENT("event_buddy_signoff", "buddy-signed-off", blist_handle);
242 COMPARE_EVENT("event_buddy_away", "buddy-away", blist_handle);
243 COMPARE_EVENT("event_buddy_back", "buddy-back", blist_handle);
244 COMPARE_EVENT("event_buddy_idle", "buddy-idle", blist_handle);
245 COMPARE_EVENT("event_buddy_unidle", "buddy-unidle", blist_handle);
246 COMPARE_EVENT("event_blist_update", "update-idle", blist_handle);
247
248 COMPARE_EVENT("event_quit", "quitting", gaim_get_core());
249
250 *signal_name = NULL;
251 *handle = NULL;
252
253 return FALSE;
254 }
255 #endif
256
193 static char * 257 static char *
194 escape_quotes(const char *buf) 258 escape_quotes(const char *buf)
195 { 259 {
196 static char *tmp_buf = NULL; 260 static char *tmp_buf = NULL;
197 const char *i; 261 const char *i;
214 return tmp_buf; 278 return tmp_buf;
215 } 279 }
216 280
217 /* 281 /*
218 2003/02/06: execute_perl modified by Mark Doliner <mark@kingant.net> 282 2003/02/06: execute_perl modified by Mark Doliner <mark@kingant.net>
219 Pass parameters by pushing them onto the stack rather than 283 Pass parameters by pushing them onto the stack rather than
220 passing an array of strings. This way, perl scripts can 284 passing an array of strings. This way, perl scripts can
221 modify the parameters and we can get the changed values 285 modify the parameters and we can get the changed values
222 and then shoot ourselves. I mean, uh, use them. 286 and then shoot ourselves. I mean, uh, use them.
223 287
224 2001/06/14: execute_perl replaced by Martin Persson <mep@passagen.se> 288 2001/06/14: execute_perl replaced by Martin Persson <mep@passagen.se>
225 previous use of perl_eval leaked memory, replaced with 289 previous use of perl_eval leaked memory, replaced with
226 a version that uses perl_call instead 290 a version that uses perl_call instead
227 291
228 30/11/2002: execute_perl modified by Eric Timme <timothy@voidnet.com> 292 30/11/2002: execute_perl modified by Eric Timme <timothy@voidnet.com>
229 args changed to char** so that we can have preparsed 293 args changed to char** so that we can have preparsed
230 arguments again, and many headaches ensued! This essentially 294 arguments again, and many headaches ensued! This essentially
231 means we replaced one hacked method with a messier hacked 295 means we replaced one hacked method with a messier hacked
232 method out of perceived necessity. Formerly execute_perl 296 method out of perceived necessity. Formerly execute_perl
233 required a single char_ptr, and it would insert it into an 297 required a single char_ptr, and it would insert it into an
234 array of character pointers and NULL terminate the new array. 298 array of character pointers and NULL terminate the new array.
235 Now we have to pass in pre-terminated character pointer arrays 299 Now we have to pass in pre-terminated character pointer arrays
236 to accomodate functions that want to pass in multiple arguments. 300 to accomodate functions that want to pass in multiple arguments.
237 301
238 Previously arguments were preparsed because an argument list 302 Previously arguments were preparsed because an argument list
239 was constructed in the form 'arg one','arg two' and was 303 was constructed in the form 'arg one','arg two' and was
240 executed via a call like &funcname(arglist) (see .59.x), so 304 executed via a call like &funcname(arglist) (see .59.x), so
241 the arglist was magically pre-parsed because of the method. 305 the arglist was magically pre-parsed because of the method.
242 With Martin Persson's change to perl_call we now need to 306 With Martin Persson's change to perl_call we now need to
243 use a null terminated list of character pointers for arguments 307 use a null terminated list of character pointers for arguments
244 if we wish them to be parsed. Lacking a better way to allow 308 if we wish them to be parsed. Lacking a better way to allow
245 for both single arguments and many I created a NULL terminated 309 for both single arguments and many I created a NULL terminated
246 array in every function that called execute_perl and passed 310 array in every function that called execute_perl and passed
253 the array sizes? I am not comfortable enough with this 317 the array sizes? I am not comfortable enough with this
254 subject to attempt it myself and hope it to stand the test 318 subject to attempt it myself and hope it to stand the test
255 of time. 319 of time.
256 */ 320 */
257 321
258 static int 322 static int
259 execute_perl(const char *function, int argc, char **args) 323 execute_perl(const char *function, int argc, char **args)
260 { 324 {
261 int count = 0, i, ret_value = 1; 325 int count = 0, i, ret_value = 1;
262 SV *sv_args[argc]; 326 SV *sv_args[argc];
263 STRLEN na; 327 STRLEN na;
264 328
265 /* 329 /*
266 * Set up the perl environment, push arguments onto the 330 * Set up the perl environment, push arguments onto the
267 * perl stack, then call the given function 331 * perl stack, then call the given function
268 */ 332 */
269 dSP; 333 dSP;
270 ENTER; 334 ENTER;
271 SAVETMPS; 335 SAVETMPS;
349 execute_perl(scp->shutdowncallback, 1, atmp); 413 execute_perl(scp->shutdowncallback, 1, atmp);
350 414
351 g_free(scp->name); 415 g_free(scp->name);
352 g_free(scp->version); 416 g_free(scp->version);
353 g_free(scp->shutdowncallback); 417 g_free(scp->shutdowncallback);
354 g_free(scp); 418 g_free(scp);
355 419
356 break; 420 break;
357 } 421 }
358 } 422 }
359 423
412 plugin->error = g_strdup(_("GAIM::register not called with " 476 plugin->error = g_strdup(_("GAIM::register not called with "
413 "proper arguments. Consult PERL-HOWTO.")); 477 "proper arguments. Consult PERL-HOWTO."));
414 478
415 return 0; 479 return 0;
416 } 480 }
417 481
418 return ret; 482 return ret;
419 } 483 }
420 484
421 static void 485 static void
422 perl_init(void) 486 perl_init(void)
430 { 494 {
431 /* We use to function one to load a file the other to 495 /* We use to function one to load a file the other to
432 execute the string obtained from the first and holding 496 execute the string obtained from the first and holding
433 the file conents. This allows to have a realy local $/ 497 the file conents. This allows to have a realy local $/
434 without introducing temp variables to hold the old 498 without introducing temp variables to hold the old
435 value. Just a question of style:) */ 499 value. Just a question of style:) */
436 "sub load_file{" 500 "sub load_file{"
437 "my $f_name=shift;" 501 "my $f_name=shift;"
438 "local $/=undef;" 502 "local $/=undef;"
439 "open FH,$f_name or return \"__FAILED__\";" 503 "open FH,$f_name or return \"__FAILED__\";"
440 "$_=<FH>;" 504 "$_=<FH>;"
548 if (plug) { 612 if (plug) {
549 scp = g_new0(struct perlscript, 1); 613 scp = g_new0(struct perlscript, 1);
550 scp->name = g_strdup(name); 614 scp->name = g_strdup(name);
551 scp->version = g_strdup(ver); 615 scp->version = g_strdup(ver);
552 scp->shutdowncallback = g_strdup(callback); 616 scp->shutdowncallback = g_strdup(callback);
553 scp->plug = plug; 617 scp->plug = plug;
554 perl_list = g_list_append(perl_list, scp); 618 perl_list = g_list_append(perl_list, scp);
555 XST_mPV(0, plug->path); 619 XST_mPV(0, plug->path);
556 } 620 }
557 else 621 else
558 XST_mPV(0, NULL); 622 XST_mPV(0, NULL);
821 885
822 nick = SvPV(ST(0), junk); 886 nick = SvPV(ST(0), junk);
823 send = SvIV(ST(1)); 887 send = SvIV(ST(1));
824 what = SvPV(ST(2), junk); 888 what = SvPV(ST(2), junk);
825 who = SvPV(ST(3), junk); 889 who = SvPV(ST(3), junk);
826 890
827 if (!*who) who=NULL; 891 if (!*who) who=NULL;
828 892
829 switch (send) { 893 switch (send) {
830 case 0: wflags=WFLAG_SEND; break; 894 case 0: wflags=WFLAG_SEND; break;
831 case 1: wflags=WFLAG_RECV; break; 895 case 1: wflags=WFLAG_RECV; break;
832 case 2: wflags=WFLAG_SYSTEM; break; 896 case 2: wflags=WFLAG_SYSTEM; break;
833 default: wflags=WFLAG_RECV; 897 default: wflags=WFLAG_RECV;
834 } 898 }
835 899
836 c = gaim_find_conversation(nick); 900 c = gaim_find_conversation(nick);
837 901
838 if (!c) 902 if (!c)
839 c = gaim_conversation_new(GAIM_CONV_IM, NULL, nick); 903 c = gaim_conversation_new(GAIM_CONV_IM, NULL, nick);
888 if (!c) 952 if (!c)
889 c = gaim_conversation_new(GAIM_CONV_IM, gc->account, nick); 953 c = gaim_conversation_new(GAIM_CONV_IM, gc->account, nick);
890 else 954 else
891 gaim_conversation_set_account(c, gc->account); 955 gaim_conversation_set_account(c, gc->account);
892 956
893 gaim_conversation_write(c, NULL, what, -1, 957 gaim_conversation_write(c, NULL, what, -1,
894 (WFLAG_SEND | (isauto ? WFLAG_AUTO : 0)), time(NULL)); 958 (WFLAG_SEND | (isauto ? WFLAG_AUTO : 0)), time(NULL));
895 serv_send_im(gc, nick, what, -1, isauto ? IM_FLAG_AWAY : 0); 959 serv_send_im(gc, nick, what, -1, isauto ? IM_FLAG_AWAY : 0);
896 XSRETURN(0); 960 XSRETURN(0);
897 } 961 }
898 962
899 963
900 964
901 XS (XS_GAIM_print_to_chat) 965 XS (XS_GAIM_print_to_chat)
902 { 966 {
903 GaimConnection *gc; 967 GaimConnection *gc;
904 int id; 968 int id;
905 char *what; 969 char *what;
929 if (b) 993 if (b)
930 serv_chat_send(gc, id, what); 994 serv_chat_send(gc, id, what);
931 XSRETURN(0); 995 XSRETURN(0);
932 } 996 }
933 997
998 #if 0
934 static int 999 static int
935 perl_event(GaimEvent event, void *unused, va_list args) 1000 perl_event(GaimEvent event, void *unused, va_list args)
936 { 1001 {
937 char *buf[5] = { NULL, NULL, NULL, NULL, NULL }; /* Maximum of 5 args */ 1002 char *buf[5] = { NULL, NULL, NULL, NULL, NULL }; /* Maximum of 5 args */
938 void *arg1 = NULL, *arg2 = NULL, *arg3 = NULL, *arg4 = NULL, *arg5 = NULL; 1003 void *arg1 = NULL, *arg2 = NULL, *arg3 = NULL, *arg4 = NULL, *arg5 = NULL;
1144 break; 1209 break;
1145 } 1210 }
1146 1211
1147 return 0; 1212 return 0;
1148 } 1213 }
1214 #endif
1149 1215
1150 XS (XS_GAIM_add_event_handler) 1216 XS (XS_GAIM_add_event_handler)
1151 { 1217 {
1152 unsigned int junk; 1218 unsigned int junk;
1153 struct _perl_event_handlers *handler; 1219 struct _perl_event_handlers *handler;
1154 char *handle; 1220 char *handle;
1155 GaimPlugin *plug; 1221 GaimPlugin *plug;
1156 GList *p; 1222 GList *p;
1157 dXSARGS; 1223 dXSARGS;
1158 items = 0; 1224 items = 0;
1159 1225
1160 handle = SvPV(ST(0), junk); 1226 handle = SvPV(ST(0), junk);
1227
1228 gaim_debug(GAIM_DEBUG_ERROR, "perl",
1229 "Ay, sorry matey. Ye perl scripts are getting "
1230 "events no more. Argh.\n");
1161 1231
1162 for (p = gaim_plugins_get_all(); p != NULL; p = p->next) { 1232 for (p = gaim_plugins_get_all(); p != NULL; p = p->next) {
1163 plug = p->data; 1233 plug = p->data;
1164 1234
1165 if (!strcmp(handle, plug->path)) 1235 if (!strcmp(handle, plug->path))
1178 } else { 1248 } else {
1179 gaim_debug(GAIM_DEBUG_ERROR, "perl", 1249 gaim_debug(GAIM_DEBUG_ERROR, "perl",
1180 "Invalid handle (%s) registering perl event handler\n", 1250 "Invalid handle (%s) registering perl event handler\n",
1181 handle); 1251 handle);
1182 } 1252 }
1183 1253
1184 XSRETURN_EMPTY; 1254 XSRETURN_EMPTY;
1185 } 1255 }
1186 1256
1187 XS (XS_GAIM_remove_event_handler) 1257 XS (XS_GAIM_remove_event_handler)
1188 { 1258 {
1232 long timeout; 1302 long timeout;
1233 struct _perl_timeout_handlers *handler; 1303 struct _perl_timeout_handlers *handler;
1234 char *handle; 1304 char *handle;
1235 GaimPlugin *plug; 1305 GaimPlugin *plug;
1236 GList *p; 1306 GList *p;
1237 1307
1238 dXSARGS; 1308 dXSARGS;
1239 items = 0; 1309 items = 0;
1240 1310
1241 handle = SvPV(ST(0), junk); 1311 handle = SvPV(ST(0), junk);
1242 1312
1243 for (p = gaim_plugins_get_all(); p != NULL; p = p->next) { 1313 for (p = gaim_plugins_get_all(); p != NULL; p = p->next) {
1244 plug = p->data; 1314 plug = p->data;
1245 1315
1291 int count; 1361 int count;
1292 gboolean status = TRUE; 1362 gboolean status = TRUE;
1293 1363
1294 perl_construct(prober); 1364 perl_construct(prober);
1295 perl_parse(prober, xs_init, 2, argv, NULL); 1365 perl_parse(prober, xs_init, 2, argv, NULL);
1296 1366
1297 { 1367 {
1298 dSP; 1368 dSP;
1299 ENTER; 1369 ENTER;
1300 SAVETMPS; 1370 SAVETMPS;
1301 PUSHMARK(SP); 1371 PUSHMARK(SP);
1380 NULL, /**< exts */ 1450 NULL, /**< exts */
1381 1451
1382 probe_perl_plugin, /**< probe */ 1452 probe_perl_plugin, /**< probe */
1383 load_perl_plugin, /**< load */ 1453 load_perl_plugin, /**< load */
1384 unload_perl_plugin, /**< unload */ 1454 unload_perl_plugin, /**< unload */
1385 destroy_perl_plugin, /**< destroy */ 1455 destroy_perl_plugin /**< destroy */
1386 perl_event /**< broadcast */
1387 }; 1456 };
1388 1457
1389 static GaimPluginInfo info = 1458 static GaimPluginInfo info =
1390 { 1459 {
1391 2, /**< api_version */ 1460 2, /**< api_version */