source: perlconfig.c @ 9c01a5e

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