[f1e629d] | 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> |
---|
[908e388] | 7 | #define OWL_PERL |
---|
[f1e629d] | 8 | #include "owl.h" |
---|
| 9 | |
---|
[8203afd] | 10 | extern XS(boot_BarnOwl); |
---|
[908e388] | 11 | extern XS(boot_DynaLoader); |
---|
[af1920fd] | 12 | /* extern XS(boot_DBI); */ |
---|
[f1e629d] | 13 | |
---|
[c3acb0b] | 14 | static void owl_perl_xs_init(pTHX) |
---|
| 15 | { |
---|
[e19eb97] | 16 | const char *file = __FILE__; |
---|
[f1e629d] | 17 | dXSUB_SYS; |
---|
| 18 | { |
---|
[8203afd] | 19 | newXS("BarnOwl::bootstrap", boot_BarnOwl, file); |
---|
[908e388] | 20 | newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file); |
---|
[f1e629d] | 21 | } |
---|
| 22 | } |
---|
| 23 | |
---|
[d1b1cf6] | 24 | |
---|
| 25 | SV *owl_new_sv(const char * str) |
---|
| 26 | { |
---|
| 27 | SV *ret = newSVpv(str, 0); |
---|
[2693b12] | 28 | if(is_utf8_string((U8*)str, strlen(str))) { |
---|
[d1b1cf6] | 29 | SvUTF8_on(ret); |
---|
| 30 | } else { |
---|
[eea72a13] | 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); |
---|
[d1b1cf6] | 34 | } |
---|
| 35 | return ret; |
---|
| 36 | } |
---|
| 37 | |
---|
[c08c70a] | 38 | SV *owl_perlconfig_message2hashref(const owl_message *m) |
---|
[c3acb0b] | 39 | { |
---|
[1cc95709] | 40 | HV *h, *stash; |
---|
[f1e629d] | 41 | SV *hr; |
---|
[e19eb97] | 42 | const char *type; |
---|
[fa4562c] | 43 | char *ptr, *utype, *blessas; |
---|
[b0430a6] | 44 | int i, j; |
---|
[25fb825] | 45 | const owl_pair *pair; |
---|
[4542047] | 46 | const owl_filter *wrap; |
---|
[f1e629d] | 47 | |
---|
| 48 | if (!m) return &PL_sv_undef; |
---|
[f6b319c] | 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 | |
---|
[f1e629d] | 55 | h = newHV(); |
---|
| 56 | |
---|
[3ea31b6] | 57 | #define MSG2H(h,field) (void)hv_store(h, #field, strlen(#field), \ |
---|
[d1b1cf6] | 58 | owl_new_sv(owl_message_get_##field(m)), 0) |
---|
[f1e629d] | 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++) { |
---|
[5376a95] | 68 | ptr=owl_zephyr_get_field_as_utf8(owl_message_get_notice(m), i+1); |
---|
[d1b1cf6] | 69 | av_push(av_zfields, owl_new_sv(ptr)); |
---|
[b0430a6] | 70 | owl_free(ptr); |
---|
[f1e629d] | 71 | } |
---|
[3ea31b6] | 72 | (void)hv_store(h, "fields", strlen("fields"), newRV_noinc((SV*)av_zfields), 0); |
---|
[f1e629d] | 73 | |
---|
[3ea31b6] | 74 | (void)hv_store(h, "auth", strlen("auth"), |
---|
[d1b1cf6] | 75 | owl_new_sv(owl_zephyr_get_authstr(owl_message_get_notice(m))),0); |
---|
[f1e629d] | 76 | } |
---|
| 77 | |
---|
[421c8ef7] | 78 | j=owl_list_get_size(&(m->attributes)); |
---|
| 79 | for(i=0; i<j; i++) { |
---|
| 80 | pair=owl_list_get_element(&(m->attributes), i); |
---|
[3ea31b6] | 81 | (void)hv_store(h, owl_pair_get_key(pair), strlen(owl_pair_get_key(pair)), |
---|
[d1b1cf6] | 82 | owl_new_sv(owl_pair_get_value(pair)),0); |
---|
[421c8ef7] | 83 | } |
---|
| 84 | |
---|
[f1e629d] | 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 | } |
---|
[d1b1cf6] | 101 | (void)hv_store(h, "time", strlen("time"), owl_new_sv(owl_message_get_timestr(m)),0); |
---|
[d1ae4a4] | 102 | (void)hv_store(h, "unix_time", strlen("unix_time"), newSViv(m->time), 0); |
---|
[3ea31b6] | 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); |
---|
[f1e629d] | 109 | |
---|
[30678ae] | 110 | type = owl_message_get_type(m); |
---|
[8fba2ee] | 111 | if(!type || !*type) type = "generic"; |
---|
[fa4562c] | 112 | utype = owl_strdup(type); |
---|
| 113 | utype[0] = toupper(type[0]); |
---|
| 114 | blessas = owl_sprintf("BarnOwl::Message::%s", utype); |
---|
[f1e629d] | 115 | |
---|
[19bab8e] | 116 | hr = newRV_noinc((SV*)h); |
---|
[1cc95709] | 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); |
---|
[fa4562c] | 123 | owl_free(utype); |
---|
[30678ae] | 124 | owl_free(blessas); |
---|
| 125 | return hr; |
---|
[f1e629d] | 126 | } |
---|
| 127 | |
---|
[dbe8729] | 128 | SV *owl_perlconfig_curmessage2hashref(void) |
---|
[c3acb0b] | 129 | { |
---|
[f1e629d] | 130 | int curmsg; |
---|
[9e5c9f3] | 131 | const owl_view *v; |
---|
[f1e629d] | 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 | |
---|
[30678ae] | 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 |
---|
[87dfeb7] | 143 | |
---|
| 144 | This has been somewhat addressed, but is still not lossless. |
---|
[30678ae] | 145 | */ |
---|
| 146 | owl_message * owl_perlconfig_hashref2message(SV *msg) |
---|
| 147 | { |
---|
| 148 | owl_message * m; |
---|
| 149 | HE * ent; |
---|
| 150 | I32 count, len; |
---|
[e19eb97] | 151 | const char *key,*val; |
---|
[30678ae] | 152 | HV * hash; |
---|
[ad15610] | 153 | struct tm tm; |
---|
[30678ae] | 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 | } |
---|
[87dfeb7] | 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; |
---|
[712caac] | 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]"); |
---|
[87dfeb7] | 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 |
---|
[30678ae] | 208 | return m; |
---|
| 209 | } |
---|
[f1e629d] | 210 | |
---|
| 211 | /* Calls in a scalar context, passing it a hash reference. |
---|
| 212 | If return value is non-null, caller must free. */ |
---|
[c08c70a] | 213 | char *owl_perlconfig_call_with_message(const char *subname, const owl_message *m) |
---|
[c3acb0b] | 214 | { |
---|
[f1e629d] | 215 | dSP ; |
---|
[c4ba74d] | 216 | int count; |
---|
[f1e629d] | 217 | SV *msgref, *srv; |
---|
[909771e] | 218 | char *out; |
---|
[f1e629d] | 219 | |
---|
| 220 | ENTER ; |
---|
| 221 | SAVETMPS; |
---|
| 222 | |
---|
| 223 | PUSHMARK(SP) ; |
---|
| 224 | msgref = owl_perlconfig_message2hashref(m); |
---|
[19bab8e] | 225 | XPUSHs(sv_2mortal(msgref)); |
---|
[f1e629d] | 226 | PUTBACK ; |
---|
| 227 | |
---|
| 228 | count = call_pv(subname, G_SCALAR|G_EVAL|G_KEEPERR); |
---|
| 229 | |
---|
| 230 | SPAGAIN ; |
---|
| 231 | |
---|
| 232 | if (SvTRUE(ERRSV)) { |
---|
[ce6721f] | 233 | owl_function_error("Perl Error: '%s'", SvPV_nolen(ERRSV)); |
---|
[27c3a93] | 234 | /* and clear the error */ |
---|
| 235 | sv_setsv (ERRSV, &PL_sv_undef); |
---|
[f1e629d] | 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) { |
---|
[909771e] | 246 | out = owl_strdup(SvPV_nolen(srv)); |
---|
[f1e629d] | 247 | } else { |
---|
| 248 | out = NULL; |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | PUTBACK ; |
---|
| 252 | FREETMPS ; |
---|
| 253 | LEAVE ; |
---|
| 254 | |
---|
| 255 | return out; |
---|
| 256 | } |
---|
| 257 | |
---|
[25729b2] | 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 | */ |
---|
[c08c70a] | 262 | char * owl_perlconfig_message_call_method(const owl_message *m, const char *method, int argc, const char ** argv) |
---|
[25729b2] | 263 | { |
---|
| 264 | dSP; |
---|
[909771e] | 265 | unsigned int count, i; |
---|
[25729b2] | 266 | SV *msgref, *srv; |
---|
[909771e] | 267 | char *out; |
---|
[25729b2] | 268 | |
---|
| 269 | msgref = owl_perlconfig_message2hashref(m); |
---|
| 270 | |
---|
| 271 | ENTER; |
---|
| 272 | SAVETMPS; |
---|
| 273 | |
---|
| 274 | PUSHMARK(SP); |
---|
[19bab8e] | 275 | XPUSHs(sv_2mortal(msgref)); |
---|
[25729b2] | 276 | for(i=0;i<argc;i++) { |
---|
[d1b1cf6] | 277 | XPUSHs(sv_2mortal(owl_new_sv(argv[i]))); |
---|
[25729b2] | 278 | } |
---|
| 279 | PUTBACK; |
---|
| 280 | |
---|
| 281 | count = call_method(method, G_SCALAR|G_KEEPERR|G_EVAL); |
---|
| 282 | |
---|
| 283 | SPAGAIN; |
---|
| 284 | |
---|
| 285 | if(count != 1) { |
---|
[f278ff3] | 286 | fprintf(stderr, "perl returned wrong count %u\n", count); |
---|
[25729b2] | 287 | abort(); |
---|
| 288 | } |
---|
| 289 | |
---|
| 290 | if (SvTRUE(ERRSV)) { |
---|
[ce6721f] | 291 | owl_function_error("Error: '%s'", SvPV_nolen(ERRSV)); |
---|
[25729b2] | 292 | /* and clear the error */ |
---|
| 293 | sv_setsv (ERRSV, &PL_sv_undef); |
---|
| 294 | } |
---|
| 295 | |
---|
| 296 | srv = POPs; |
---|
| 297 | |
---|
| 298 | if (srv) { |
---|
[909771e] | 299 | out = owl_strdup(SvPV_nolen(srv)); |
---|
[25729b2] | 300 | } else { |
---|
| 301 | out = NULL; |
---|
| 302 | } |
---|
| 303 | |
---|
| 304 | PUTBACK; |
---|
| 305 | FREETMPS; |
---|
| 306 | LEAVE; |
---|
| 307 | |
---|
| 308 | return out; |
---|
| 309 | } |
---|
| 310 | |
---|
| 311 | |
---|
[e19eb97] | 312 | char *owl_perlconfig_initperl(const char * file, int *Pargc, char ***Pargv, char *** Penv) |
---|
[c3acb0b] | 313 | { |
---|
[d03091c] | 314 | int ret; |
---|
[f1e629d] | 315 | PerlInterpreter *p; |
---|
[4e0f545] | 316 | char *err; |
---|
[e19eb97] | 317 | const char *args[4] = {"", "-e", "0;", NULL}; |
---|
[fd8dfe7] | 318 | AV *inc; |
---|
| 319 | char *path; |
---|
[f1e629d] | 320 | |
---|
| 321 | /* create and initialize interpreter */ |
---|
[e8c6d8f] | 322 | PERL_SYS_INIT3(Pargc, Pargv, Penv); |
---|
[f1e629d] | 323 | p=perl_alloc(); |
---|
[4d86e06] | 324 | owl_global_set_perlinterp(&g, p); |
---|
[f1e629d] | 325 | perl_construct(p); |
---|
| 326 | |
---|
| 327 | owl_global_set_no_have_config(&g); |
---|
| 328 | |
---|
[fa4562c] | 329 | ret=perl_parse(p, owl_perl_xs_init, 2, (char **)args, NULL); |
---|
[f1e629d] | 330 | if (ret || SvTRUE(ERRSV)) { |
---|
[ce6721f] | 331 | err=owl_strdup(SvPV_nolen(ERRSV)); |
---|
[4e0f545] | 332 | sv_setsv(ERRSV, &PL_sv_undef); /* and clear the error */ |
---|
| 333 | return(err); |
---|
[f1e629d] | 334 | } |
---|
| 335 | |
---|
| 336 | ret=perl_run(p); |
---|
| 337 | if (ret || SvTRUE(ERRSV)) { |
---|
[ce6721f] | 338 | err=owl_strdup(SvPV_nolen(ERRSV)); |
---|
[4e0f545] | 339 | sv_setsv(ERRSV, &PL_sv_undef); /* and clear the error */ |
---|
| 340 | return(err); |
---|
[f1e629d] | 341 | } |
---|
| 342 | |
---|
| 343 | owl_global_set_have_config(&g); |
---|
| 344 | |
---|
| 345 | /* create legacy variables */ |
---|
[c415aca] | 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); |
---|
[00f9a7d] | 358 | |
---|
| 359 | if(file) { |
---|
[8203afd] | 360 | SV * cfg = get_sv("BarnOwl::configfile", TRUE); |
---|
[00f9a7d] | 361 | sv_setpv(cfg, file); |
---|
| 362 | } |
---|
| 363 | |
---|
[fd8dfe7] | 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); |
---|
[d1b1cf6] | 368 | av_store(inc, 0, owl_new_sv(path)); |
---|
[fd8dfe7] | 369 | owl_free(path); |
---|
| 370 | |
---|
| 371 | eval_pv("use BarnOwl;", FALSE); |
---|
[f1e629d] | 372 | |
---|
| 373 | if (SvTRUE(ERRSV)) { |
---|
[ce6721f] | 374 | err=owl_strdup(SvPV_nolen(ERRSV)); |
---|
[27c3a93] | 375 | sv_setsv (ERRSV, &PL_sv_undef); /* and clear the error */ |
---|
[4e0f545] | 376 | return(err); |
---|
[f1e629d] | 377 | } |
---|
| 378 | |
---|
| 379 | /* check if we have the formatting function */ |
---|
[8203afd] | 380 | if (owl_perlconfig_is_function("BarnOwl::format_msg")) { |
---|
[f1e629d] | 381 | owl_global_set_config_format(&g, 1); |
---|
| 382 | } |
---|
| 383 | |
---|
| 384 | return(NULL); |
---|
| 385 | } |
---|
| 386 | |
---|
| 387 | /* returns whether or not a function exists */ |
---|
[e19eb97] | 388 | int owl_perlconfig_is_function(const char *fn) { |
---|
[c415aca] | 389 | if (get_cv(fn, FALSE)) return(1); |
---|
[f1e629d] | 390 | else return(0); |
---|
| 391 | } |
---|
| 392 | |
---|
| 393 | /* caller is responsible for freeing returned string */ |
---|
[e19eb97] | 394 | char *owl_perlconfig_execute(const char *line) |
---|
[c3acb0b] | 395 | { |
---|
[f1e629d] | 396 | STRLEN len; |
---|
| 397 | SV *response; |
---|
[e19eb97] | 398 | const char *preout; |
---|
[65b2173] | 399 | char *out; |
---|
[f1e629d] | 400 | |
---|
| 401 | if (!owl_global_have_config(&g)) return NULL; |
---|
| 402 | |
---|
[e0096b7] | 403 | ENTER; |
---|
| 404 | SAVETMPS; |
---|
[f1e629d] | 405 | /* execute the subroutine */ |
---|
[c415aca] | 406 | response = eval_pv(line, FALSE); |
---|
[f1e629d] | 407 | |
---|
| 408 | if (SvTRUE(ERRSV)) { |
---|
[ce6721f] | 409 | owl_function_error("Perl Error: '%s'", SvPV_nolen(ERRSV)); |
---|
[27c3a93] | 410 | sv_setsv (ERRSV, &PL_sv_undef); /* and clear the error */ |
---|
[f1e629d] | 411 | } |
---|
| 412 | |
---|
| 413 | preout=SvPV(response, len); |
---|
[909771e] | 414 | if (len == 0 || preout[len - 1] != '\n') |
---|
| 415 | out = owl_sprintf("%s\n", preout); |
---|
| 416 | else |
---|
| 417 | out = owl_strdup(preout); |
---|
[e0096b7] | 418 | FREETMPS; |
---|
| 419 | LEAVE; |
---|
[f1e629d] | 420 | |
---|
| 421 | return(out); |
---|
| 422 | } |
---|
| 423 | |
---|
[c08c70a] | 424 | void owl_perlconfig_getmsg(const owl_message *m, const char *subname) |
---|
[b67ab6b] | 425 | { |
---|
| 426 | char *ptr = NULL; |
---|
| 427 | if (owl_perlconfig_is_function("BarnOwl::Hooks::_receive_msg")) { |
---|
| 428 | ptr = owl_perlconfig_call_with_message(subname?subname |
---|
| 429 | :"BarnOwl::_receive_msg_legacy_wrap", m); |
---|
[f1e629d] | 430 | } |
---|
[b67ab6b] | 431 | if (ptr) owl_free(ptr); |
---|
[0f9eca7] | 432 | } |
---|
| 433 | |
---|
| 434 | /* Called on all new messages; receivemsg is only called on incoming ones */ |
---|
[c08c70a] | 435 | void owl_perlconfig_newmsg(const owl_message *m, const char *subname) |
---|
[0f9eca7] | 436 | { |
---|
| 437 | char *ptr = NULL; |
---|
| 438 | if (owl_perlconfig_is_function("BarnOwl::Hooks::_new_msg")) { |
---|
| 439 | ptr = owl_perlconfig_call_with_message(subname?subname |
---|
| 440 | :"BarnOwl::Hooks::_new_msg", m); |
---|
| 441 | } |
---|
| 442 | if (ptr) owl_free(ptr); |
---|
[f1e629d] | 443 | } |
---|
[6922edd] | 444 | |
---|
[e19eb97] | 445 | void owl_perlconfig_new_command(const char *name) |
---|
[eb6cedc] | 446 | { |
---|
| 447 | dSP; |
---|
| 448 | |
---|
| 449 | ENTER; |
---|
| 450 | SAVETMPS; |
---|
| 451 | |
---|
| 452 | PUSHMARK(SP); |
---|
[d1b1cf6] | 453 | XPUSHs(sv_2mortal(owl_new_sv(name))); |
---|
[eb6cedc] | 454 | PUTBACK; |
---|
| 455 | |
---|
| 456 | call_pv("BarnOwl::Hooks::_new_command", G_SCALAR|G_VOID); |
---|
| 457 | |
---|
| 458 | SPAGAIN; |
---|
| 459 | |
---|
| 460 | if(SvTRUE(ERRSV)) { |
---|
| 461 | owl_function_error("%s", SvPV_nolen(ERRSV)); |
---|
| 462 | } |
---|
| 463 | |
---|
| 464 | FREETMPS; |
---|
| 465 | LEAVE; |
---|
| 466 | } |
---|
| 467 | |
---|
[0a0fb74] | 468 | char *owl_perlconfig_perlcmd(const owl_cmd *cmd, int argc, const char *const *argv) |
---|
[6922edd] | 469 | { |
---|
| 470 | int i, count; |
---|
| 471 | char * ret = NULL; |
---|
[ad15610] | 472 | SV *rv; |
---|
[6922edd] | 473 | dSP; |
---|
| 474 | |
---|
| 475 | ENTER; |
---|
| 476 | SAVETMPS; |
---|
| 477 | |
---|
| 478 | PUSHMARK(SP); |
---|
| 479 | for(i=0;i<argc;i++) { |
---|
[d1b1cf6] | 480 | XPUSHs(sv_2mortal(owl_new_sv(argv[i]))); |
---|
[6922edd] | 481 | } |
---|
| 482 | PUTBACK; |
---|
| 483 | |
---|
| 484 | count = call_sv(cmd->cmd_perl, G_SCALAR|G_EVAL); |
---|
| 485 | |
---|
| 486 | SPAGAIN; |
---|
| 487 | |
---|
| 488 | if(SvTRUE(ERRSV)) { |
---|
[ce6721f] | 489 | owl_function_error("%s", SvPV_nolen(ERRSV)); |
---|
[c4ba74d] | 490 | (void)POPs; |
---|
[6922edd] | 491 | } else { |
---|
| 492 | if(count != 1) |
---|
| 493 | croak("Perl command %s returned more than one value!", cmd->name); |
---|
[ad15610] | 494 | rv = POPs; |
---|
[6922edd] | 495 | if(SvTRUE(rv)) { |
---|
[ce6721f] | 496 | ret = owl_strdup(SvPV_nolen(rv)); |
---|
[6922edd] | 497 | } |
---|
| 498 | } |
---|
| 499 | |
---|
| 500 | FREETMPS; |
---|
| 501 | LEAVE; |
---|
| 502 | |
---|
| 503 | return ret; |
---|
| 504 | } |
---|
| 505 | |
---|
| 506 | void owl_perlconfig_cmd_free(owl_cmd *cmd) |
---|
| 507 | { |
---|
[ff13a6f] | 508 | SvREFCNT_dec(cmd->cmd_perl); |
---|
[6922edd] | 509 | } |
---|
[db8b00b] | 510 | |
---|
[9c7a701] | 511 | void owl_perlconfig_dispatch_free(owl_dispatch *d) |
---|
| 512 | { |
---|
[4d86e06] | 513 | SvREFCNT_dec(d->data); |
---|
[1895c29] | 514 | owl_free(d); |
---|
[9c7a701] | 515 | } |
---|
| 516 | |
---|
[db8b00b] | 517 | void owl_perlconfig_edit_callback(owl_editwin *e) |
---|
| 518 | { |
---|
[4d86e06] | 519 | SV *cb = owl_editwin_get_cbdata(e); |
---|
[367fbf3] | 520 | SV *text; |
---|
[ad15610] | 521 | dSP; |
---|
| 522 | |
---|
[db8b00b] | 523 | if(cb == NULL) { |
---|
| 524 | owl_function_error("Perl callback is NULL!"); |
---|
[1373d35] | 525 | return; |
---|
[db8b00b] | 526 | } |
---|
[d1b1cf6] | 527 | text = owl_new_sv(owl_editwin_get_text(e)); |
---|
[db8b00b] | 528 | |
---|
| 529 | ENTER; |
---|
| 530 | SAVETMPS; |
---|
| 531 | |
---|
| 532 | PUSHMARK(SP); |
---|
[367fbf3] | 533 | XPUSHs(sv_2mortal(text)); |
---|
[db8b00b] | 534 | PUTBACK; |
---|
| 535 | |
---|
[9364a36] | 536 | call_sv(cb, G_DISCARD|G_KEEPERR|G_EVAL); |
---|
| 537 | |
---|
| 538 | if(SvTRUE(ERRSV)) { |
---|
[ce6721f] | 539 | owl_function_error("%s", SvPV_nolen(ERRSV)); |
---|
[9364a36] | 540 | } |
---|
[db8b00b] | 541 | |
---|
| 542 | FREETMPS; |
---|
| 543 | LEAVE; |
---|
| 544 | |
---|
| 545 | SvREFCNT_dec(cb); |
---|
[a556caa] | 546 | owl_editwin_set_cbdata(e, NULL); |
---|
[db8b00b] | 547 | } |
---|
[f72f573] | 548 | |
---|
[99068d3] | 549 | void owl_perlconfig_mainloop(owl_timer *t, void *data) |
---|
[f72f573] | 550 | { |
---|
[ad15610] | 551 | dSP; |
---|
[0337203] | 552 | if (!owl_perlconfig_is_function("BarnOwl::Hooks::_mainloop_hook")) |
---|
[8203afd] | 553 | return; |
---|
[f72f573] | 554 | PUSHMARK(SP) ; |
---|
[0337203] | 555 | call_pv("BarnOwl::Hooks::_mainloop_hook", G_DISCARD|G_EVAL); |
---|
[a55abb3] | 556 | if(SvTRUE(ERRSV)) { |
---|
[ce6721f] | 557 | owl_function_error("%s", SvPV_nolen(ERRSV)); |
---|
[a55abb3] | 558 | } |
---|
[f72f573] | 559 | return; |
---|
| 560 | } |
---|
[9c7a701] | 561 | |
---|
[f36cd97] | 562 | void owl_perlconfig_dispatch(owl_dispatch *d) |
---|
[9c7a701] | 563 | { |
---|
[f36cd97] | 564 | SV *cb = d->data; |
---|
[9c7a701] | 565 | dSP; |
---|
| 566 | if(cb == NULL) { |
---|
| 567 | owl_function_error("Perl callback is NULL!"); |
---|
[f36cd97] | 568 | return; |
---|
[9c7a701] | 569 | } |
---|
| 570 | |
---|
| 571 | ENTER; |
---|
| 572 | SAVETMPS; |
---|
| 573 | |
---|
| 574 | PUSHMARK(SP); |
---|
| 575 | PUTBACK; |
---|
| 576 | |
---|
| 577 | call_sv(cb, G_DISCARD|G_KEEPERR|G_EVAL); |
---|
| 578 | |
---|
| 579 | if(SvTRUE(ERRSV)) { |
---|
[ce6721f] | 580 | owl_function_error("%s", SvPV_nolen(ERRSV)); |
---|
[9c7a701] | 581 | } |
---|
| 582 | |
---|
| 583 | FREETMPS; |
---|
| 584 | LEAVE; |
---|
| 585 | } |
---|
[1631825] | 586 | |
---|
| 587 | void owl_perlconfig_perl_timer(owl_timer *t, void *data) |
---|
| 588 | { |
---|
[5ebc202] | 589 | dSP; |
---|
[1631825] | 590 | SV *obj = data; |
---|
| 591 | |
---|
| 592 | if(!SvROK(obj)) { |
---|
| 593 | return; |
---|
| 594 | } |
---|
| 595 | |
---|
| 596 | ENTER; |
---|
| 597 | SAVETMPS; |
---|
| 598 | |
---|
| 599 | PUSHMARK(SP); |
---|
| 600 | XPUSHs(obj); |
---|
| 601 | PUTBACK; |
---|
| 602 | |
---|
| 603 | call_method("do_callback", G_DISCARD|G_KEEPERR|G_EVAL); |
---|
| 604 | |
---|
| 605 | SPAGAIN; |
---|
| 606 | |
---|
| 607 | if (SvTRUE(ERRSV)) { |
---|
| 608 | owl_function_error("Error in calback: '%s'", SvPV_nolen(ERRSV)); |
---|
| 609 | sv_setsv (ERRSV, &PL_sv_undef); |
---|
| 610 | } |
---|
| 611 | |
---|
| 612 | PUTBACK; |
---|
| 613 | FREETMPS; |
---|
| 614 | LEAVE; |
---|
| 615 | } |
---|
| 616 | |
---|
| 617 | void owl_perlconfig_perl_timer_destroy(owl_timer *t) |
---|
| 618 | { |
---|
| 619 | if(SvOK((SV*)t->data)) { |
---|
| 620 | SvREFCNT_dec((SV*)t->data); |
---|
| 621 | } |
---|
| 622 | } |
---|