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