source: perlconfig.c @ 12a6616

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