#include #include #include #include #include #include #define OWL_PERL #include "owl.h" static const char fileIdent[] = "$Id$"; extern char *owl_perlwrap_codebuff; extern XS(boot_BarnOwl); extern XS(boot_DynaLoader); // extern XS(boot_DBI); static void owl_perl_xs_init(pTHX) { char *file = __FILE__; dXSUB_SYS; { newXS("BarnOwl::bootstrap", boot_BarnOwl, file); newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file); } } SV *owl_perlconfig_message2hashref(owl_message *m) { HV *h, *stash; SV *hr; char *ptr, *blessas, *type; int i, j; owl_pair *pair; owl_filter *wrap; if (!m) return &PL_sv_undef; wrap = owl_global_get_filter(&g, "wordwrap"); if(!wrap) { owl_function_error("wrap filter is not defined"); return &PL_sv_undef; } h = newHV(); #define MSG2H(h,field) hv_store(h, #field, strlen(#field), \ newSVpv(owl_message_get_##field(m),0), 0) if (owl_message_is_type_zephyr(m) && owl_message_is_direction_in(m)) { /* Handle zephyr-specific fields... */ AV *av_zfields; av_zfields = newAV(); j=owl_zephyr_get_num_fields(owl_message_get_notice(m)); for (i=0; iattributes)); for(i=0; iattributes), i); hv_store(h, owl_pair_get_key(pair), strlen(owl_pair_get_key(pair)), newSVpv(owl_pair_get_value(pair),0),0); } MSG2H(h, type); MSG2H(h, direction); MSG2H(h, class); MSG2H(h, instance); MSG2H(h, sender); MSG2H(h, realm); MSG2H(h, recipient); MSG2H(h, opcode); MSG2H(h, hostname); MSG2H(h, body); MSG2H(h, login); MSG2H(h, zsig); MSG2H(h, zwriteline); if (owl_message_get_header(m)) { MSG2H(h, header); } hv_store(h, "time", strlen("time"), newSVpv(owl_message_get_timestr(m),0),0); hv_store(h, "id", strlen("id"), newSViv(owl_message_get_id(m)),0); hv_store(h, "deleted", strlen("deleted"), newSViv(owl_message_is_delete(m)),0); hv_store(h, "private", strlen("private"), newSViv(owl_message_is_private(m)),0); hv_store(h, "should_wordwrap", strlen("should_wordwrap"), newSViv( owl_filter_message_match(wrap, m)),0); type = owl_message_get_type(m); if(!type || !*type) type = "generic"; type = owl_strdup(type); type[0] = toupper(type[0]); blessas = owl_sprintf("BarnOwl::Message::%s", type); hr = sv_2mortal(newRV_noinc((SV*)h)); stash = gv_stashpv(blessas,0); if(!stash) { owl_function_error("No such class: %s for message type %s", blessas, owl_message_get_type(m)); stash = gv_stashpv("BarnOwl::Message", 1); } hr = sv_bless(hr,stash); owl_free(type); owl_free(blessas); return hr; } SV *owl_perlconfig_curmessage2hashref(void) /*noproto*/ { int curmsg; owl_view *v; v=owl_global_get_current_view(&g); if (owl_view_get_size(v) < 1) { return &PL_sv_undef; } curmsg=owl_global_get_curmsg(&g); return owl_perlconfig_message2hashref(owl_view_get_element(v, curmsg)); } /* XXX TODO: Messages should round-trip properly between message2hashref and hashref2message. Currently we lose zephyr-specific properties stored in the ZNotice_t */ owl_message * owl_perlconfig_hashref2message(SV *msg) { owl_message * m; HE * ent; I32 count, len; char *key,*val; HV * hash; struct tm tm; hash = (HV*)SvRV(msg); m = owl_malloc(sizeof(owl_message)); owl_message_init(m); count = hv_iterinit(hash); while((ent = hv_iternext(hash))) { key = hv_iterkey(ent, &len); val = SvPV_nolen(hv_iterval(hash, ent)); if(!strcmp(key, "type")) { owl_message_set_type(m, val); } else if(!strcmp(key, "direction")) { owl_message_set_direction(m, owl_message_parse_direction(val)); } else if(!strcmp(key, "private")) { SV * v = hv_iterval(hash, ent); if(SvTRUE(v)) { owl_message_set_isprivate(m); } } else if (!strcmp(key, "hostname")) { owl_message_set_hostname(m, val); } else if (!strcmp(key, "zwriteline")) { owl_message_set_zwriteline(m, val); } else if (!strcmp(key, "time")) { m->timestr = owl_strdup(val); strptime(val, "%a %b %d %T %Y", &tm); m->time = mktime(&tm); } else { owl_message_set_attribute(m, key, val); } } if(owl_message_is_type_admin(m)) { if(!owl_message_get_attribute_value(m, "adminheader")) owl_message_set_attribute(m, "adminheader", ""); } return m; } /* Calls in a scalar context, passing it a hash reference. If return value is non-null, caller must free. */ char *owl_perlconfig_call_with_message(char *subname, owl_message *m) { dSP ; int count, len; SV *msgref, *srv; char *out, *preout; ENTER ; SAVETMPS; PUSHMARK(SP) ; msgref = owl_perlconfig_message2hashref(m); XPUSHs(msgref); PUTBACK ; count = call_pv(subname, G_SCALAR|G_EVAL|G_KEEPERR); SPAGAIN ; if (SvTRUE(ERRSV)) { STRLEN n_a; owl_function_error("Perl Error: '%s'", SvPV(ERRSV, n_a)); /* and clear the error */ sv_setsv (ERRSV, &PL_sv_undef); } if (count != 1) { fprintf(stderr, "bad perl! no biscuit! returned wrong count!\n"); abort(); } srv = POPs; if (srv) { preout=SvPV(srv, len); out = owl_malloc(strlen(preout)+1); strncpy(out, preout, len); out[len] = '\0'; } else { out = NULL; } PUTBACK ; FREETMPS ; LEAVE ; return out; } /* Calls a method on a perl object representing a message. If the return value is non-null, the caller must free it. */ char * owl_perlconfig_message_call_method(owl_message *m, char *method, int argc, char ** argv) { dSP; unsigned int count, len, i; SV *msgref, *srv; char *out, *preout; msgref = owl_perlconfig_message2hashref(m); ENTER; SAVETMPS; PUSHMARK(SP); XPUSHs(msgref); for(i=0;icmd_perl, G_SCALAR|G_EVAL); SPAGAIN; if(SvTRUE(ERRSV)) { owl_function_error("%s", SvPV(ERRSV, n_a)); POPs; } else { if(count != 1) croak("Perl command %s returned more than one value!", cmd->name); rv = POPs; if(SvTRUE(rv)) { ret = owl_strdup(SvPV(rv, n_a)); } } FREETMPS; LEAVE; return ret; } void owl_perlconfig_cmd_free(owl_cmd *cmd) { SvREFCNT_dec(cmd->cmd_perl); } void owl_perlconfig_edit_callback(owl_editwin *e) { SV *cb = (SV*)(e->cbdata); unsigned int n_a; dSP; if(cb == NULL) { owl_function_error("Perl callback is NULL!"); } ENTER; SAVETMPS; PUSHMARK(SP); XPUSHs(sv_2mortal(newSVpv(owl_editwin_get_text(e), 0))); PUTBACK; call_sv(cb, G_DISCARD|G_KEEPERR|G_EVAL); if(SvTRUE(ERRSV)) { owl_function_error("%s", SvPV(ERRSV, n_a)); } FREETMPS; LEAVE; SvREFCNT_dec(cb); e->cbdata = NULL; } void owl_perlconfig_mainloop() { dSP; if (!owl_perlconfig_is_function("BarnOwl::Hooks::_mainloop_hook")) return; PUSHMARK(SP) ; call_pv("BarnOwl::Hooks::_mainloop_hook", G_DISCARD|G_EVAL); if(SvTRUE(ERRSV)) { STRLEN n_a; owl_function_error("%s", SvPV(ERRSV, n_a)); } return; }