source: perlconfig.c @ 73ba824

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