source: perlconfig.c @ fe7616e

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