source: perlconfig.c @ ebbeb39

release-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since ebbeb39 was 1373d35, checked in by Anders Kaseorg <andersk@mit.edu>, 14 years ago
owl_perlconfig_edit_callback: Actually give up if the callback is NULL. Signed-off-by: Anders Kaseorg <andersk@mit.edu>
  • Property mode set to 100644
File size: 14.1 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
14static void owl_perl_xs_init(pTHX)
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((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
38SV *owl_perlconfig_message2hashref(const owl_message *m)
39{
40  HV *h, *stash;
41  SV *hr;
42  const char *type;
43  char *ptr, *utype, *blessas;
44  int i, j;
45  const owl_pair *pair;
46  const owl_filter *wrap;
47
48  if (!m) return &PL_sv_undef;
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
55  h = newHV();
56
57#define MSG2H(h,field) (void)hv_store(h, #field, strlen(#field),        \
58                                      owl_new_sv(owl_message_get_##field(m)), 0)
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++) {
68      ptr=owl_zephyr_get_field_as_utf8(owl_message_get_notice(m), i+1);
69      av_push(av_zfields, owl_new_sv(ptr));
70      owl_free(ptr);
71    }
72    (void)hv_store(h, "fields", strlen("fields"), newRV_noinc((SV*)av_zfields), 0);
73
74    (void)hv_store(h, "auth", strlen("auth"), 
75                   owl_new_sv(owl_zephyr_get_authstr(owl_message_get_notice(m))),0);
76  }
77
78  j=owl_list_get_size(&(m->attributes));
79  for(i=0; i<j; i++) {
80    pair=owl_list_get_element(&(m->attributes), i);
81    (void)hv_store(h, owl_pair_get_key(pair), strlen(owl_pair_get_key(pair)),
82                   owl_new_sv(owl_pair_get_value(pair)),0);
83  }
84 
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  }
101  (void)hv_store(h, "time", strlen("time"), owl_new_sv(owl_message_get_timestr(m)),0);
102  (void)hv_store(h, "unix_time", strlen("unix_time"), newSViv(m->time), 0);
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);
109
110  type = owl_message_get_type(m);
111  if(!type || !*type) type = "generic";
112  utype = owl_strdup(type);
113  utype[0] = toupper(type[0]);
114  blessas = owl_sprintf("BarnOwl::Message::%s", utype);
115
116  hr = newRV_noinc((SV*)h);
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);
123  owl_free(utype);
124  owl_free(blessas);
125  return hr;
126}
127
128SV *owl_perlconfig_curmessage2hashref(void)
129{
130  int curmsg;
131  const owl_view *v;
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
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
143
144   This has been somewhat addressed, but is still not lossless.
145 */
146owl_message * owl_perlconfig_hashref2message(SV *msg)
147{
148  owl_message * m;
149  HE * ent;
150  I32 count, len;
151  const char *key,*val;
152  HV * hash;
153  struct tm tm;
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  }
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;
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]");
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
208  return m;
209}
210
211/* Calls in a scalar context, passing it a hash reference.
212   If return value is non-null, caller must free. */
213char *owl_perlconfig_call_with_message(const char *subname, const owl_message *m)
214{
215  dSP ;
216  int count;
217  SV *msgref, *srv;
218  char *out;
219 
220  ENTER ;
221  SAVETMPS;
222 
223  PUSHMARK(SP) ;
224  msgref = owl_perlconfig_message2hashref(m);
225  XPUSHs(sv_2mortal(msgref));
226  PUTBACK ;
227 
228  count = call_pv(subname, G_SCALAR|G_EVAL|G_KEEPERR);
229 
230  SPAGAIN ;
231
232  if (SvTRUE(ERRSV)) {
233    owl_function_error("Perl Error: '%s'", SvPV_nolen(ERRSV));
234    /* and clear the error */
235    sv_setsv (ERRSV, &PL_sv_undef);
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) {
246    out = owl_strdup(SvPV_nolen(srv));
247  } else {
248    out = NULL;
249  }
250 
251  PUTBACK ;
252  FREETMPS ;
253  LEAVE ;
254
255  return out;
256}
257
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 */
262char * owl_perlconfig_message_call_method(const owl_message *m, const char *method, int argc, const char ** argv)
263{
264  dSP;
265  unsigned int count, i;
266  SV *msgref, *srv;
267  char *out;
268
269  msgref = owl_perlconfig_message2hashref(m);
270
271  ENTER;
272  SAVETMPS;
273
274  PUSHMARK(SP);
275  XPUSHs(sv_2mortal(msgref));
276  for(i=0;i<argc;i++) {
277    XPUSHs(sv_2mortal(owl_new_sv(argv[i])));
278  }
279  PUTBACK;
280
281  count = call_method(method, G_SCALAR|G_KEEPERR|G_EVAL);
282
283  SPAGAIN;
284
285  if(count != 1) {
286    fprintf(stderr, "perl returned wrong count %u\n", count);
287    abort();
288  }
289
290  if (SvTRUE(ERRSV)) {
291    owl_function_error("Error: '%s'", SvPV_nolen(ERRSV));
292    /* and clear the error */
293    sv_setsv (ERRSV, &PL_sv_undef);
294  }
295
296  srv = POPs;
297
298  if (srv) {
299    out = owl_strdup(SvPV_nolen(srv));
300  } else {
301    out = NULL;
302  }
303
304  PUTBACK;
305  FREETMPS;
306  LEAVE;
307
308  return out;
309}
310
311
312char *owl_perlconfig_initperl(const char * file, int *Pargc, char ***Pargv, char *** Penv)
313{
314  int ret;
315  PerlInterpreter *p;
316  char *err;
317  const char *args[4] = {"", "-e", "0;", NULL};
318  AV *inc;
319  char *path;
320
321  /* create and initialize interpreter */
322  PERL_SYS_INIT3(Pargc, Pargv, Penv);
323  p=perl_alloc();
324  owl_global_set_perlinterp(&g, p);
325  perl_construct(p);
326
327  owl_global_set_no_have_config(&g);
328
329  ret=perl_parse(p, owl_perl_xs_init, 2, (char **)args, NULL);
330  if (ret || SvTRUE(ERRSV)) {
331    err=owl_strdup(SvPV_nolen(ERRSV));
332    sv_setsv(ERRSV, &PL_sv_undef);     /* and clear the error */
333    return(err);
334  }
335
336  ret=perl_run(p);
337  if (ret || SvTRUE(ERRSV)) {
338    err=owl_strdup(SvPV_nolen(ERRSV));
339    sv_setsv(ERRSV, &PL_sv_undef);     /* and clear the error */
340    return(err);
341  }
342
343  owl_global_set_have_config(&g);
344
345  /* create legacy variables */
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);
358
359  if(file) {
360    SV * cfg = get_sv("BarnOwl::configfile", TRUE);
361    sv_setpv(cfg, file);
362  }
363
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);
368  av_store(inc, 0, owl_new_sv(path));
369  owl_free(path);
370
371  eval_pv("use BarnOwl;", FALSE);
372
373  if (SvTRUE(ERRSV)) {
374    err=owl_strdup(SvPV_nolen(ERRSV));
375    sv_setsv (ERRSV, &PL_sv_undef);     /* and clear the error */
376    return(err);
377  }
378
379  /* check if we have the formatting function */
380  if (owl_perlconfig_is_function("BarnOwl::format_msg")) {
381    owl_global_set_config_format(&g, 1);
382  }
383
384  return(NULL);
385}
386
387/* returns whether or not a function exists */
388int owl_perlconfig_is_function(const char *fn) {
389  if (get_cv(fn, FALSE)) return(1);
390  else return(0);
391}
392
393/* caller is responsible for freeing returned string */
394char *owl_perlconfig_execute(const char *line)
395{
396  STRLEN len;
397  SV *response;
398  const char *preout;
399  char *out;
400
401  if (!owl_global_have_config(&g)) return NULL;
402
403  ENTER;
404  SAVETMPS;
405  /* execute the subroutine */
406  response = eval_pv(line, FALSE);
407
408  if (SvTRUE(ERRSV)) {
409    owl_function_error("Perl Error: '%s'", SvPV_nolen(ERRSV));
410    sv_setsv (ERRSV, &PL_sv_undef);     /* and clear the error */
411  }
412
413  preout=SvPV(response, len);
414  if (len == 0 || preout[len - 1] != '\n')
415    out = owl_sprintf("%s\n", preout);
416  else
417    out = owl_strdup(preout);
418  FREETMPS;
419  LEAVE;
420
421  return(out);
422}
423
424void owl_perlconfig_getmsg(const owl_message *m, const char *subname)
425{
426  char *ptr = NULL;
427  if (owl_perlconfig_is_function("BarnOwl::Hooks::_receive_msg")) {
428    ptr = owl_perlconfig_call_with_message(subname?subname
429                                           :"BarnOwl::_receive_msg_legacy_wrap", m);
430  }
431  if (ptr) owl_free(ptr);
432}
433
434/* Called on all new messages; receivemsg is only called on incoming ones */
435void owl_perlconfig_newmsg(const owl_message *m, const char *subname)
436{
437  char *ptr = NULL;
438  if (owl_perlconfig_is_function("BarnOwl::Hooks::_new_msg")) {
439    ptr = owl_perlconfig_call_with_message(subname?subname
440                                           :"BarnOwl::Hooks::_new_msg", m);
441  }
442  if (ptr) owl_free(ptr);
443}
444
445void owl_perlconfig_new_command(const char *name)
446{
447  dSP;
448
449  ENTER;
450  SAVETMPS;
451
452  PUSHMARK(SP);
453  XPUSHs(sv_2mortal(owl_new_sv(name)));
454  PUTBACK;
455
456  call_pv("BarnOwl::Hooks::_new_command", G_SCALAR|G_VOID);
457
458  SPAGAIN;
459
460  if(SvTRUE(ERRSV)) {
461    owl_function_error("%s", SvPV_nolen(ERRSV));
462  }
463
464  FREETMPS;
465  LEAVE;
466}
467
468char *owl_perlconfig_perlcmd(const owl_cmd *cmd, int argc, const char *const *argv)
469{
470  int i, count;
471  char * ret = NULL;
472  SV *rv;
473  dSP;
474
475  ENTER;
476  SAVETMPS;
477
478  PUSHMARK(SP);
479  for(i=0;i<argc;i++) {
480    XPUSHs(sv_2mortal(owl_new_sv(argv[i])));
481  }
482  PUTBACK;
483
484  count = call_sv(cmd->cmd_perl, G_SCALAR|G_EVAL);
485
486  SPAGAIN;
487
488  if(SvTRUE(ERRSV)) {
489    owl_function_error("%s", SvPV_nolen(ERRSV));
490    (void)POPs;
491  } else {
492    if(count != 1)
493      croak("Perl command %s returned more than one value!", cmd->name);
494    rv = POPs;
495    if(SvTRUE(rv)) {
496      ret = owl_strdup(SvPV_nolen(rv));
497    }
498  }
499
500  FREETMPS;
501  LEAVE;
502
503  return ret;
504}
505
506void owl_perlconfig_cmd_free(owl_cmd *cmd)
507{
508  SvREFCNT_dec(cmd->cmd_perl);
509}
510
511void owl_perlconfig_dispatch_free(owl_dispatch *d)
512{
513  SvREFCNT_dec(d->data);
514  owl_free(d);
515}
516
517void owl_perlconfig_edit_callback(owl_editwin *e)
518{
519  SV *cb = owl_editwin_get_cbdata(e);
520  SV *text;
521  dSP;
522
523  if(cb == NULL) {
524    owl_function_error("Perl callback is NULL!");
525    return;
526  }
527  text = owl_new_sv(owl_editwin_get_text(e));
528
529  ENTER;
530  SAVETMPS;
531
532  PUSHMARK(SP);
533  XPUSHs(sv_2mortal(text));
534  PUTBACK;
535 
536  call_sv(cb, G_DISCARD|G_KEEPERR|G_EVAL);
537
538  if(SvTRUE(ERRSV)) {
539    owl_function_error("%s", SvPV_nolen(ERRSV));
540  }
541
542  FREETMPS;
543  LEAVE;
544
545  SvREFCNT_dec(cb);
546  owl_editwin_set_cbdata(e, NULL);
547}
548
549void owl_perlconfig_mainloop(owl_timer *t, void *data)
550{
551  dSP;
552  if (!owl_perlconfig_is_function("BarnOwl::Hooks::_mainloop_hook"))
553    return;
554  PUSHMARK(SP) ;
555  call_pv("BarnOwl::Hooks::_mainloop_hook", G_DISCARD|G_EVAL);
556  if(SvTRUE(ERRSV)) {
557    owl_function_error("%s", SvPV_nolen(ERRSV));
558  }
559  return;
560}
561
562void owl_perlconfig_dispatch(owl_dispatch *d)
563{
564  SV *cb = d->data;
565  dSP;
566  if(cb == NULL) {
567    owl_function_error("Perl callback is NULL!");
568    return;
569  }
570
571  ENTER;
572  SAVETMPS;
573
574  PUSHMARK(SP);
575  PUTBACK;
576 
577  call_sv(cb, G_DISCARD|G_KEEPERR|G_EVAL);
578
579  if(SvTRUE(ERRSV)) {
580    owl_function_error("%s", SvPV_nolen(ERRSV));
581  }
582
583  FREETMPS;
584  LEAVE;
585}
586
587void owl_perlconfig_perl_timer(owl_timer *t, void *data)
588{
589  dSP;
590  SV *obj = data;
591
592  if(!SvROK(obj)) {
593    return;
594  }
595
596  ENTER;
597  SAVETMPS;
598
599  PUSHMARK(SP);
600  XPUSHs(obj);
601  PUTBACK;
602
603  call_method("do_callback", G_DISCARD|G_KEEPERR|G_EVAL);
604
605  SPAGAIN;
606
607  if (SvTRUE(ERRSV)) {
608    owl_function_error("Error in calback: '%s'", SvPV_nolen(ERRSV));
609    sv_setsv (ERRSV, &PL_sv_undef);
610  }
611
612  PUTBACK;
613  FREETMPS;
614  LEAVE;
615}
616
617void owl_perlconfig_perl_timer_destroy(owl_timer *t)
618{
619  if(SvOK((SV*)t->data)) {
620    SvREFCNT_dec((SV*)t->data);
621  }
622}
Note: See TracBrowser for help on using the repository browser.