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