source: perlconfig.c @ f1a2736

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