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