| 1 | #include <stdio.h> |
|---|
| 2 | #include <stdlib.h> |
|---|
| 3 | #include <string.h> |
|---|
| 4 | #include <sys/types.h> |
|---|
| 5 | #include <sys/stat.h> |
|---|
| 6 | #include <errno.h> |
|---|
| 7 | #define OWL_PERL |
|---|
| 8 | #include "owl.h" |
|---|
| 9 | |
|---|
| 10 | extern XS(boot_BarnOwl); |
|---|
| 11 | extern XS(boot_DynaLoader); |
|---|
| 12 | /* extern XS(boot_DBI); */ |
|---|
| 13 | |
|---|
| 14 | static void owl_perl_xs_init(pTHX) |
|---|
| 15 | { |
|---|
| 16 | const char *file = __FILE__; |
|---|
| 17 | dXSUB_SYS; |
|---|
| 18 | { |
|---|
| 19 | newXS("BarnOwl::bootstrap", boot_BarnOwl, file); |
|---|
| 20 | newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file); |
|---|
| 21 | } |
|---|
| 22 | } |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | SV *owl_new_sv(const char * str) |
|---|
| 26 | { |
|---|
| 27 | SV *ret = newSVpv(str, 0); |
|---|
| 28 | if (is_utf8_string((const U8 *)str, strlen(str))) { |
|---|
| 29 | SvUTF8_on(ret); |
|---|
| 30 | } else { |
|---|
| 31 | char *escape = owl_escape_highbit(str); |
|---|
| 32 | owl_function_error("Internal error! Non-UTF-8 string encountered:\n%s", escape); |
|---|
| 33 | owl_free(escape); |
|---|
| 34 | } |
|---|
| 35 | return ret; |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | SV *owl_perlconfig_message2hashref(const owl_message *m) |
|---|
| 39 | { |
|---|
| 40 | HV *h, *stash; |
|---|
| 41 | SV *hr; |
|---|
| 42 | const char *type; |
|---|
| 43 | char *ptr, *utype, *blessas; |
|---|
| 44 | int i, j; |
|---|
| 45 | const owl_pair *pair; |
|---|
| 46 | const owl_filter *wrap; |
|---|
| 47 | |
|---|
| 48 | if (!m) return &PL_sv_undef; |
|---|
| 49 | wrap = owl_global_get_filter(&g, "wordwrap"); |
|---|
| 50 | if(!wrap) { |
|---|
| 51 | owl_function_error("wrap filter is not defined"); |
|---|
| 52 | return &PL_sv_undef; |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | h = newHV(); |
|---|
| 56 | |
|---|
| 57 | #define MSG2H(h,field) (void)hv_store(h, #field, strlen(#field), \ |
|---|
| 58 | owl_new_sv(owl_message_get_##field(m)), 0) |
|---|
| 59 | |
|---|
| 60 | if (owl_message_is_type_zephyr(m) |
|---|
| 61 | && owl_message_is_direction_in(m)) { |
|---|
| 62 | /* Handle zephyr-specific fields... */ |
|---|
| 63 | AV *av_zfields; |
|---|
| 64 | |
|---|
| 65 | av_zfields = newAV(); |
|---|
| 66 | j=owl_zephyr_get_num_fields(owl_message_get_notice(m)); |
|---|
| 67 | for (i=0; i<j; i++) { |
|---|
| 68 | ptr=owl_zephyr_get_field_as_utf8(owl_message_get_notice(m), i+1); |
|---|
| 69 | av_push(av_zfields, owl_new_sv(ptr)); |
|---|
| 70 | owl_free(ptr); |
|---|
| 71 | } |
|---|
| 72 | (void)hv_store(h, "fields", strlen("fields"), newRV_noinc((SV*)av_zfields), 0); |
|---|
| 73 | |
|---|
| 74 | (void)hv_store(h, "auth", strlen("auth"), |
|---|
| 75 | owl_new_sv(owl_zephyr_get_authstr(owl_message_get_notice(m))),0); |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | j=owl_list_get_size(&(m->attributes)); |
|---|
| 79 | for(i=0; i<j; i++) { |
|---|
| 80 | pair=owl_list_get_element(&(m->attributes), i); |
|---|
| 81 | (void)hv_store(h, owl_pair_get_key(pair), strlen(owl_pair_get_key(pair)), |
|---|
| 82 | owl_new_sv(owl_pair_get_value(pair)),0); |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | MSG2H(h, type); |
|---|
| 86 | MSG2H(h, direction); |
|---|
| 87 | MSG2H(h, class); |
|---|
| 88 | MSG2H(h, instance); |
|---|
| 89 | MSG2H(h, sender); |
|---|
| 90 | MSG2H(h, realm); |
|---|
| 91 | MSG2H(h, recipient); |
|---|
| 92 | MSG2H(h, opcode); |
|---|
| 93 | MSG2H(h, hostname); |
|---|
| 94 | MSG2H(h, body); |
|---|
| 95 | MSG2H(h, login); |
|---|
| 96 | MSG2H(h, zsig); |
|---|
| 97 | MSG2H(h, zwriteline); |
|---|
| 98 | if (owl_message_get_header(m)) { |
|---|
| 99 | MSG2H(h, header); |
|---|
| 100 | } |
|---|
| 101 | (void)hv_store(h, "time", strlen("time"), owl_new_sv(owl_message_get_timestr(m)),0); |
|---|
| 102 | (void)hv_store(h, "unix_time", strlen("unix_time"), newSViv(m->time), 0); |
|---|
| 103 | (void)hv_store(h, "id", strlen("id"), newSViv(owl_message_get_id(m)),0); |
|---|
| 104 | (void)hv_store(h, "deleted", strlen("deleted"), newSViv(owl_message_is_delete(m)),0); |
|---|
| 105 | (void)hv_store(h, "private", strlen("private"), newSViv(owl_message_is_private(m)),0); |
|---|
| 106 | (void)hv_store(h, "should_wordwrap", |
|---|
| 107 | strlen("should_wordwrap"), newSViv( |
|---|
| 108 | owl_filter_message_match(wrap, m)),0); |
|---|
| 109 | |
|---|
| 110 | type = owl_message_get_type(m); |
|---|
| 111 | if(!type || !*type) type = "generic"; |
|---|
| 112 | utype = owl_strdup(type); |
|---|
| 113 | utype[0] = toupper(type[0]); |
|---|
| 114 | blessas = owl_sprintf("BarnOwl::Message::%s", utype); |
|---|
| 115 | |
|---|
| 116 | hr = newRV_noinc((SV*)h); |
|---|
| 117 | stash = gv_stashpv(blessas,0); |
|---|
| 118 | if(!stash) { |
|---|
| 119 | owl_function_error("No such class: %s for message type %s", blessas, owl_message_get_type(m)); |
|---|
| 120 | stash = gv_stashpv("BarnOwl::Message", 1); |
|---|
| 121 | } |
|---|
| 122 | hr = sv_bless(hr,stash); |
|---|
| 123 | owl_free(utype); |
|---|
| 124 | owl_free(blessas); |
|---|
| 125 | return hr; |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | SV *owl_perlconfig_curmessage2hashref(void) |
|---|
| 129 | { |
|---|
| 130 | int curmsg; |
|---|
| 131 | const owl_view *v; |
|---|
| 132 | v=owl_global_get_current_view(&g); |
|---|
| 133 | if (owl_view_get_size(v) < 1) { |
|---|
| 134 | return &PL_sv_undef; |
|---|
| 135 | } |
|---|
| 136 | curmsg=owl_global_get_curmsg(&g); |
|---|
| 137 | return owl_perlconfig_message2hashref(owl_view_get_element(v, curmsg)); |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | /* XXX TODO: Messages should round-trip properly between |
|---|
| 141 | message2hashref and hashref2message. Currently we lose |
|---|
| 142 | zephyr-specific properties stored in the ZNotice_t |
|---|
| 143 | |
|---|
| 144 | This has been somewhat addressed, but is still not lossless. |
|---|
| 145 | */ |
|---|
| 146 | owl_message * owl_perlconfig_hashref2message(SV *msg) |
|---|
| 147 | { |
|---|
| 148 | owl_message * m; |
|---|
| 149 | HE * ent; |
|---|
| 150 | I32 count, len; |
|---|
| 151 | const char *key,*val; |
|---|
| 152 | HV * hash; |
|---|
| 153 | struct tm tm; |
|---|
| 154 | |
|---|
| 155 | hash = (HV*)SvRV(msg); |
|---|
| 156 | |
|---|
| 157 | m = owl_malloc(sizeof(owl_message)); |
|---|
| 158 | owl_message_init(m); |
|---|
| 159 | |
|---|
| 160 | count = hv_iterinit(hash); |
|---|
| 161 | while((ent = hv_iternext(hash))) { |
|---|
| 162 | key = hv_iterkey(ent, &len); |
|---|
| 163 | val = SvPV_nolen(hv_iterval(hash, ent)); |
|---|
| 164 | if(!strcmp(key, "type")) { |
|---|
| 165 | owl_message_set_type(m, val); |
|---|
| 166 | } else if(!strcmp(key, "direction")) { |
|---|
| 167 | owl_message_set_direction(m, owl_message_parse_direction(val)); |
|---|
| 168 | } else if(!strcmp(key, "private")) { |
|---|
| 169 | SV * v = hv_iterval(hash, ent); |
|---|
| 170 | if(SvTRUE(v)) { |
|---|
| 171 | owl_message_set_isprivate(m); |
|---|
| 172 | } |
|---|
| 173 | } else if (!strcmp(key, "hostname")) { |
|---|
| 174 | owl_message_set_hostname(m, val); |
|---|
| 175 | } else if (!strcmp(key, "zwriteline")) { |
|---|
| 176 | owl_message_set_zwriteline(m, val); |
|---|
| 177 | } else if (!strcmp(key, "time")) { |
|---|
| 178 | m->timestr = owl_strdup(val); |
|---|
| 179 | strptime(val, "%a %b %d %T %Y", &tm); |
|---|
| 180 | m->time = mktime(&tm); |
|---|
| 181 | } else { |
|---|
| 182 | owl_message_set_attribute(m, key, val); |
|---|
| 183 | } |
|---|
| 184 | } |
|---|
| 185 | if(owl_message_is_type_admin(m)) { |
|---|
| 186 | if(!owl_message_get_attribute_value(m, "adminheader")) |
|---|
| 187 | owl_message_set_attribute(m, "adminheader", ""); |
|---|
| 188 | } |
|---|
| 189 | #ifdef HAVE_LIBZEPHYR |
|---|
| 190 | if (owl_message_is_type_zephyr(m)) { |
|---|
| 191 | ZNotice_t *n = &(m->notice); |
|---|
| 192 | n->z_kind = ACKED; |
|---|
| 193 | n->z_port = 0; |
|---|
| 194 | n->z_auth = ZAUTH_NO; |
|---|
| 195 | n->z_checked_auth = 0; |
|---|
| 196 | n->z_class = zstr(owl_message_get_class(m)); |
|---|
| 197 | n->z_class_inst = zstr(owl_message_get_instance(m)); |
|---|
| 198 | n->z_opcode = zstr(owl_message_get_opcode(m)); |
|---|
| 199 | n->z_sender = zstr(owl_message_get_sender(m)); |
|---|
| 200 | n->z_recipient = zstr(owl_message_get_recipient(m)); |
|---|
| 201 | n->z_default_format = zstr("[zephyr created from perl]"); |
|---|
| 202 | n->z_multinotice = zstr("[zephyr created from perl]"); |
|---|
| 203 | n->z_num_other_fields = 0; |
|---|
| 204 | n->z_message = owl_sprintf("%s%c%s", owl_message_get_zsig(m), '\0', owl_message_get_body(m)); |
|---|
| 205 | n->z_message_len = strlen(owl_message_get_zsig(m)) + strlen(owl_message_get_body(m)) + 1; |
|---|
| 206 | } |
|---|
| 207 | #endif |
|---|
| 208 | return m; |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | /* Calls in a scalar context, passing it a hash reference. |
|---|
| 212 | If return value is non-null, caller must free. */ |
|---|
| 213 | char *owl_perlconfig_call_with_message(const char *subname, const owl_message *m) |
|---|
| 214 | { |
|---|
| 215 | dSP ; |
|---|
| 216 | int count; |
|---|
| 217 | SV *msgref, *srv; |
|---|
| 218 | char *out; |
|---|
| 219 | |
|---|
| 220 | ENTER ; |
|---|
| 221 | SAVETMPS; |
|---|
| 222 | |
|---|
| 223 | PUSHMARK(SP) ; |
|---|
| 224 | msgref = owl_perlconfig_message2hashref(m); |
|---|
| 225 | XPUSHs(sv_2mortal(msgref)); |
|---|
| 226 | PUTBACK ; |
|---|
| 227 | |
|---|
| 228 | count = call_pv(subname, G_SCALAR|G_EVAL|G_KEEPERR); |
|---|
| 229 | |
|---|
| 230 | SPAGAIN ; |
|---|
| 231 | |
|---|
| 232 | if (SvTRUE(ERRSV)) { |
|---|
| 233 | owl_function_error("Perl Error: '%s'", SvPV_nolen(ERRSV)); |
|---|
| 234 | /* and clear the error */ |
|---|
| 235 | sv_setsv (ERRSV, &PL_sv_undef); |
|---|
| 236 | } |
|---|
| 237 | |
|---|
| 238 | if (count != 1) { |
|---|
| 239 | fprintf(stderr, "bad perl! no biscuit! returned wrong count!\n"); |
|---|
| 240 | abort(); |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | srv = POPs; |
|---|
| 244 | |
|---|
| 245 | if (srv) { |
|---|
| 246 | out = owl_strdup(SvPV_nolen(srv)); |
|---|
| 247 | } else { |
|---|
| 248 | out = NULL; |
|---|
| 249 | } |
|---|
| 250 | |
|---|
| 251 | PUTBACK ; |
|---|
| 252 | FREETMPS ; |
|---|
| 253 | LEAVE ; |
|---|
| 254 | |
|---|
| 255 | return out; |
|---|
| 256 | } |
|---|
| 257 | |
|---|
| 258 | |
|---|
| 259 | /* Calls a method on a perl object representing a message. |
|---|
| 260 | If the return value is non-null, the caller must free it. |
|---|
| 261 | */ |
|---|
| 262 | char * owl_perlconfig_message_call_method(const owl_message *m, const char *method, int argc, const char ** argv) |
|---|
| 263 | { |
|---|
| 264 | dSP; |
|---|
| 265 | unsigned int count, i; |
|---|
| 266 | SV *msgref, *srv; |
|---|
| 267 | char *out; |
|---|
| 268 | |
|---|
| 269 | msgref = owl_perlconfig_message2hashref(m); |
|---|
| 270 | |
|---|
| 271 | ENTER; |
|---|
| 272 | SAVETMPS; |
|---|
| 273 | |
|---|
| 274 | PUSHMARK(SP); |
|---|
| 275 | XPUSHs(sv_2mortal(msgref)); |
|---|
| 276 | for(i=0;i<argc;i++) { |
|---|
| 277 | XPUSHs(sv_2mortal(owl_new_sv(argv[i]))); |
|---|
| 278 | } |
|---|
| 279 | PUTBACK; |
|---|
| 280 | |
|---|
| 281 | count = call_method(method, G_SCALAR|G_KEEPERR|G_EVAL); |
|---|
| 282 | |
|---|
| 283 | SPAGAIN; |
|---|
| 284 | |
|---|
| 285 | if(count != 1) { |
|---|
| 286 | fprintf(stderr, "perl returned wrong count %u\n", count); |
|---|
| 287 | abort(); |
|---|
| 288 | } |
|---|
| 289 | |
|---|
| 290 | if (SvTRUE(ERRSV)) { |
|---|
| 291 | owl_function_error("Error: '%s'", SvPV_nolen(ERRSV)); |
|---|
| 292 | /* and clear the error */ |
|---|
| 293 | sv_setsv (ERRSV, &PL_sv_undef); |
|---|
| 294 | } |
|---|
| 295 | |
|---|
| 296 | srv = POPs; |
|---|
| 297 | |
|---|
| 298 | if (srv) { |
|---|
| 299 | out = owl_strdup(SvPV_nolen(srv)); |
|---|
| 300 | } else { |
|---|
| 301 | out = NULL; |
|---|
| 302 | } |
|---|
| 303 | |
|---|
| 304 | PUTBACK; |
|---|
| 305 | FREETMPS; |
|---|
| 306 | LEAVE; |
|---|
| 307 | |
|---|
| 308 | return out; |
|---|
| 309 | } |
|---|
| 310 | |
|---|
| 311 | |
|---|
| 312 | char *owl_perlconfig_initperl(const char * file, int *Pargc, char ***Pargv, char *** Penv) |
|---|
| 313 | { |
|---|
| 314 | int ret; |
|---|
| 315 | PerlInterpreter *p; |
|---|
| 316 | char *err; |
|---|
| 317 | const char *args[4] = {"", "-e", "0;", NULL}; |
|---|
| 318 | AV *inc; |
|---|
| 319 | char *path; |
|---|
| 320 | |
|---|
| 321 | /* create and initialize interpreter */ |
|---|
| 322 | PERL_SYS_INIT3(Pargc, Pargv, Penv); |
|---|
| 323 | p=perl_alloc(); |
|---|
| 324 | owl_global_set_perlinterp(&g, p); |
|---|
| 325 | perl_construct(p); |
|---|
| 326 | |
|---|
| 327 | owl_global_set_no_have_config(&g); |
|---|
| 328 | |
|---|
| 329 | ret=perl_parse(p, owl_perl_xs_init, 2, (char **)args, NULL); |
|---|
| 330 | if (ret || SvTRUE(ERRSV)) { |
|---|
| 331 | err=owl_strdup(SvPV_nolen(ERRSV)); |
|---|
| 332 | sv_setsv(ERRSV, &PL_sv_undef); /* and clear the error */ |
|---|
| 333 | return(err); |
|---|
| 334 | } |
|---|
| 335 | |
|---|
| 336 | ret=perl_run(p); |
|---|
| 337 | if (ret || SvTRUE(ERRSV)) { |
|---|
| 338 | err=owl_strdup(SvPV_nolen(ERRSV)); |
|---|
| 339 | sv_setsv(ERRSV, &PL_sv_undef); /* and clear the error */ |
|---|
| 340 | return(err); |
|---|
| 341 | } |
|---|
| 342 | |
|---|
| 343 | owl_global_set_have_config(&g); |
|---|
| 344 | |
|---|
| 345 | /* create legacy variables */ |
|---|
| 346 | get_sv("BarnOwl::id", TRUE); |
|---|
| 347 | get_sv("BarnOwl::class", TRUE); |
|---|
| 348 | get_sv("BarnOwl::instance", TRUE); |
|---|
| 349 | get_sv("BarnOwl::recipient", TRUE); |
|---|
| 350 | get_sv("BarnOwl::sender", TRUE); |
|---|
| 351 | get_sv("BarnOwl::realm", TRUE); |
|---|
| 352 | get_sv("BarnOwl::opcode", TRUE); |
|---|
| 353 | get_sv("BarnOwl::zsig", TRUE); |
|---|
| 354 | get_sv("BarnOwl::msg", TRUE); |
|---|
| 355 | get_sv("BarnOwl::time", TRUE); |
|---|
| 356 | get_sv("BarnOwl::host", TRUE); |
|---|
| 357 | get_av("BarnOwl::fields", TRUE); |
|---|
| 358 | |
|---|
| 359 | if(file) { |
|---|
| 360 | SV * cfg = get_sv("BarnOwl::configfile", TRUE); |
|---|
| 361 | sv_setpv(cfg, file); |
|---|
| 362 | } |
|---|
| 363 | |
|---|
| 364 | /* Add the system lib path to @INC */ |
|---|
| 365 | inc = get_av("INC", 0); |
|---|
| 366 | path = owl_sprintf("%s/lib", owl_get_datadir()); |
|---|
| 367 | av_unshift(inc, 1); |
|---|
| 368 | av_store(inc, 0, owl_new_sv(path)); |
|---|
| 369 | owl_free(path); |
|---|
| 370 | |
|---|
| 371 | eval_pv("use BarnOwl;", FALSE); |
|---|
| 372 | |
|---|
| 373 | if (SvTRUE(ERRSV)) { |
|---|
| 374 | err=owl_strdup(SvPV_nolen(ERRSV)); |
|---|
| 375 | sv_setsv (ERRSV, &PL_sv_undef); /* and clear the error */ |
|---|
| 376 | return(err); |
|---|
| 377 | } |
|---|
| 378 | |
|---|
| 379 | /* check if we have the formatting function */ |
|---|
| 380 | if (owl_perlconfig_is_function("BarnOwl::format_msg")) { |
|---|
| 381 | owl_global_set_config_format(&g, 1); |
|---|
| 382 | } |
|---|
| 383 | |
|---|
| 384 | return(NULL); |
|---|
| 385 | } |
|---|
| 386 | |
|---|
| 387 | /* returns whether or not a function exists */ |
|---|
| 388 | int owl_perlconfig_is_function(const char *fn) { |
|---|
| 389 | if (get_cv(fn, FALSE)) return(1); |
|---|
| 390 | else return(0); |
|---|
| 391 | } |
|---|
| 392 | |
|---|
| 393 | /* caller is responsible for freeing returned string */ |
|---|
| 394 | char *owl_perlconfig_execute(const char *line) |
|---|
| 395 | { |
|---|
| 396 | STRLEN len; |
|---|
| 397 | SV *response; |
|---|
| 398 | char *out; |
|---|
| 399 | |
|---|
| 400 | if (!owl_global_have_config(&g)) return NULL; |
|---|
| 401 | |
|---|
| 402 | ENTER; |
|---|
| 403 | SAVETMPS; |
|---|
| 404 | /* execute the subroutine */ |
|---|
| 405 | response = eval_pv(line, FALSE); |
|---|
| 406 | |
|---|
| 407 | if (SvTRUE(ERRSV)) { |
|---|
| 408 | owl_function_error("Perl Error: '%s'", SvPV_nolen(ERRSV)); |
|---|
| 409 | sv_setsv (ERRSV, &PL_sv_undef); /* and clear the error */ |
|---|
| 410 | } |
|---|
| 411 | |
|---|
| 412 | out = owl_strdup(SvPV(response, len)); |
|---|
| 413 | FREETMPS; |
|---|
| 414 | LEAVE; |
|---|
| 415 | |
|---|
| 416 | return(out); |
|---|
| 417 | } |
|---|
| 418 | |
|---|
| 419 | void owl_perlconfig_getmsg(const owl_message *m, const char *subname) |
|---|
| 420 | { |
|---|
| 421 | char *ptr = NULL; |
|---|
| 422 | if (owl_perlconfig_is_function("BarnOwl::Hooks::_receive_msg")) { |
|---|
| 423 | ptr = owl_perlconfig_call_with_message(subname?subname |
|---|
| 424 | :"BarnOwl::_receive_msg_legacy_wrap", m); |
|---|
| 425 | } |
|---|
| 426 | if (ptr) owl_free(ptr); |
|---|
| 427 | } |
|---|
| 428 | |
|---|
| 429 | /* Called on all new messages; receivemsg is only called on incoming ones */ |
|---|
| 430 | void owl_perlconfig_newmsg(const owl_message *m, const char *subname) |
|---|
| 431 | { |
|---|
| 432 | char *ptr = NULL; |
|---|
| 433 | if (owl_perlconfig_is_function("BarnOwl::Hooks::_new_msg")) { |
|---|
| 434 | ptr = owl_perlconfig_call_with_message(subname?subname |
|---|
| 435 | :"BarnOwl::Hooks::_new_msg", m); |
|---|
| 436 | } |
|---|
| 437 | if (ptr) owl_free(ptr); |
|---|
| 438 | } |
|---|
| 439 | |
|---|
| 440 | void owl_perlconfig_new_command(const char *name) |
|---|
| 441 | { |
|---|
| 442 | dSP; |
|---|
| 443 | |
|---|
| 444 | ENTER; |
|---|
| 445 | SAVETMPS; |
|---|
| 446 | |
|---|
| 447 | PUSHMARK(SP); |
|---|
| 448 | XPUSHs(sv_2mortal(owl_new_sv(name))); |
|---|
| 449 | PUTBACK; |
|---|
| 450 | |
|---|
| 451 | call_pv("BarnOwl::Hooks::_new_command", G_SCALAR|G_VOID); |
|---|
| 452 | |
|---|
| 453 | SPAGAIN; |
|---|
| 454 | |
|---|
| 455 | if(SvTRUE(ERRSV)) { |
|---|
| 456 | owl_function_error("%s", SvPV_nolen(ERRSV)); |
|---|
| 457 | } |
|---|
| 458 | |
|---|
| 459 | FREETMPS; |
|---|
| 460 | LEAVE; |
|---|
| 461 | } |
|---|
| 462 | |
|---|
| 463 | char *owl_perlconfig_perlcmd(const owl_cmd *cmd, int argc, const char *const *argv) |
|---|
| 464 | { |
|---|
| 465 | int i, count; |
|---|
| 466 | char * ret = NULL; |
|---|
| 467 | SV *rv; |
|---|
| 468 | dSP; |
|---|
| 469 | |
|---|
| 470 | ENTER; |
|---|
| 471 | SAVETMPS; |
|---|
| 472 | |
|---|
| 473 | PUSHMARK(SP); |
|---|
| 474 | for(i=0;i<argc;i++) { |
|---|
| 475 | XPUSHs(sv_2mortal(owl_new_sv(argv[i]))); |
|---|
| 476 | } |
|---|
| 477 | PUTBACK; |
|---|
| 478 | |
|---|
| 479 | count = call_sv(cmd->cmd_perl, G_SCALAR|G_EVAL); |
|---|
| 480 | |
|---|
| 481 | SPAGAIN; |
|---|
| 482 | |
|---|
| 483 | if(SvTRUE(ERRSV)) { |
|---|
| 484 | owl_function_error("%s", SvPV_nolen(ERRSV)); |
|---|
| 485 | (void)POPs; |
|---|
| 486 | } else { |
|---|
| 487 | if(count != 1) |
|---|
| 488 | croak("Perl command %s returned more than one value!", cmd->name); |
|---|
| 489 | rv = POPs; |
|---|
| 490 | if(SvTRUE(rv)) { |
|---|
| 491 | ret = owl_strdup(SvPV_nolen(rv)); |
|---|
| 492 | } |
|---|
| 493 | } |
|---|
| 494 | |
|---|
| 495 | FREETMPS; |
|---|
| 496 | LEAVE; |
|---|
| 497 | |
|---|
| 498 | return ret; |
|---|
| 499 | } |
|---|
| 500 | |
|---|
| 501 | void owl_perlconfig_cmd_free(owl_cmd *cmd) |
|---|
| 502 | { |
|---|
| 503 | SvREFCNT_dec(cmd->cmd_perl); |
|---|
| 504 | } |
|---|
| 505 | |
|---|
| 506 | void owl_perlconfig_dispatch_free(owl_dispatch *d) |
|---|
| 507 | { |
|---|
| 508 | SvREFCNT_dec(d->data); |
|---|
| 509 | owl_free(d); |
|---|
| 510 | } |
|---|
| 511 | |
|---|
| 512 | void owl_perlconfig_edit_callback(owl_editwin *e) |
|---|
| 513 | { |
|---|
| 514 | SV *cb = owl_editwin_get_cbdata(e); |
|---|
| 515 | SV *text; |
|---|
| 516 | dSP; |
|---|
| 517 | |
|---|
| 518 | if(cb == NULL) { |
|---|
| 519 | owl_function_error("Perl callback is NULL!"); |
|---|
| 520 | return; |
|---|
| 521 | } |
|---|
| 522 | text = owl_new_sv(owl_editwin_get_text(e)); |
|---|
| 523 | |
|---|
| 524 | ENTER; |
|---|
| 525 | SAVETMPS; |
|---|
| 526 | |
|---|
| 527 | PUSHMARK(SP); |
|---|
| 528 | XPUSHs(sv_2mortal(text)); |
|---|
| 529 | PUTBACK; |
|---|
| 530 | |
|---|
| 531 | call_sv(cb, G_DISCARD|G_KEEPERR|G_EVAL); |
|---|
| 532 | |
|---|
| 533 | if(SvTRUE(ERRSV)) { |
|---|
| 534 | owl_function_error("%s", SvPV_nolen(ERRSV)); |
|---|
| 535 | } |
|---|
| 536 | |
|---|
| 537 | FREETMPS; |
|---|
| 538 | LEAVE; |
|---|
| 539 | |
|---|
| 540 | SvREFCNT_dec(cb); |
|---|
| 541 | owl_editwin_set_cbdata(e, NULL); |
|---|
| 542 | } |
|---|
| 543 | |
|---|
| 544 | void owl_perlconfig_mainloop(owl_timer *t, void *data) |
|---|
| 545 | { |
|---|
| 546 | dSP; |
|---|
| 547 | if (!owl_perlconfig_is_function("BarnOwl::Hooks::_mainloop_hook")) |
|---|
| 548 | return; |
|---|
| 549 | PUSHMARK(SP) ; |
|---|
| 550 | call_pv("BarnOwl::Hooks::_mainloop_hook", G_DISCARD|G_EVAL); |
|---|
| 551 | if(SvTRUE(ERRSV)) { |
|---|
| 552 | owl_function_error("%s", SvPV_nolen(ERRSV)); |
|---|
| 553 | } |
|---|
| 554 | return; |
|---|
| 555 | } |
|---|
| 556 | |
|---|
| 557 | void owl_perlconfig_dispatch(owl_dispatch *d) |
|---|
| 558 | { |
|---|
| 559 | SV *cb = d->data; |
|---|
| 560 | dSP; |
|---|
| 561 | if(cb == NULL) { |
|---|
| 562 | owl_function_error("Perl callback is NULL!"); |
|---|
| 563 | return; |
|---|
| 564 | } |
|---|
| 565 | |
|---|
| 566 | ENTER; |
|---|
| 567 | SAVETMPS; |
|---|
| 568 | |
|---|
| 569 | PUSHMARK(SP); |
|---|
| 570 | PUTBACK; |
|---|
| 571 | |
|---|
| 572 | call_sv(cb, G_DISCARD|G_KEEPERR|G_EVAL); |
|---|
| 573 | |
|---|
| 574 | if(SvTRUE(ERRSV)) { |
|---|
| 575 | owl_function_error("%s", SvPV_nolen(ERRSV)); |
|---|
| 576 | } |
|---|
| 577 | |
|---|
| 578 | FREETMPS; |
|---|
| 579 | LEAVE; |
|---|
| 580 | } |
|---|
| 581 | |
|---|
| 582 | void owl_perlconfig_perl_timer(owl_timer *t, void *data) |
|---|
| 583 | { |
|---|
| 584 | dSP; |
|---|
| 585 | SV *obj = data; |
|---|
| 586 | |
|---|
| 587 | if(!SvROK(obj)) { |
|---|
| 588 | return; |
|---|
| 589 | } |
|---|
| 590 | |
|---|
| 591 | ENTER; |
|---|
| 592 | SAVETMPS; |
|---|
| 593 | |
|---|
| 594 | PUSHMARK(SP); |
|---|
| 595 | XPUSHs(obj); |
|---|
| 596 | PUTBACK; |
|---|
| 597 | |
|---|
| 598 | call_method("do_callback", G_DISCARD|G_KEEPERR|G_EVAL); |
|---|
| 599 | |
|---|
| 600 | SPAGAIN; |
|---|
| 601 | |
|---|
| 602 | if (SvTRUE(ERRSV)) { |
|---|
| 603 | owl_function_error("Error in calback: '%s'", SvPV_nolen(ERRSV)); |
|---|
| 604 | sv_setsv (ERRSV, &PL_sv_undef); |
|---|
| 605 | } |
|---|
| 606 | |
|---|
| 607 | PUTBACK; |
|---|
| 608 | FREETMPS; |
|---|
| 609 | LEAVE; |
|---|
| 610 | } |
|---|
| 611 | |
|---|
| 612 | void owl_perlconfig_perl_timer_destroy(owl_timer *t) |
|---|
| 613 | { |
|---|
| 614 | if(SvOK((SV*)t->data)) { |
|---|
| 615 | SvREFCNT_dec((SV*)t->data); |
|---|
| 616 | } |
|---|
| 617 | } |
|---|