source: perlconfig.c @ 40c6657

debianrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 40c6657 was 1895c29, checked in by Nelson Elhage <nelhage@mit.edu>, 16 years ago
Fix owl_select_remove_dispatch bugs. Fix two bugs in owl_select_remove_dispatch: (1) Removing a dispatch from that dispatch's callback was bad, because it mutated the dispatch list while it was being traversed. Fix that by noting if we're in the middle of a dispatch, and deferring cleanup until later. (2) Calling owl_free on dispatches in select.c is a strange API, since it doesn't allocate them in the first place. Since we now have destructors, make freeing the dispatch their job.
  • Property mode set to 100644
File size: 12.2 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
10static const char fileIdent[] = "$Id$";
11
12extern char *owl_perlwrap_codebuff;
13
[8203afd]14extern XS(boot_BarnOwl);
[908e388]15extern XS(boot_DynaLoader);
[af1920fd]16/* extern XS(boot_DBI); */
[f1e629d]17
[c3acb0b]18static void owl_perl_xs_init(pTHX)
19{
[f1e629d]20  char *file = __FILE__;
21  dXSUB_SYS;
22  {
[8203afd]23    newXS("BarnOwl::bootstrap", boot_BarnOwl, file);
[908e388]24    newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
[f1e629d]25  }
26}
27
[30678ae]28SV *owl_perlconfig_message2hashref(owl_message *m)
[c3acb0b]29{
[1cc95709]30  HV *h, *stash;
[f1e629d]31  SV *hr;
[30678ae]32  char *ptr, *blessas, *type;
[b0430a6]33  int i, j;
[421c8ef7]34  owl_pair *pair;
[f6b319c]35  owl_filter *wrap;
[f1e629d]36
37  if (!m) return &PL_sv_undef;
[f6b319c]38  wrap = owl_global_get_filter(&g, "wordwrap");
39  if(!wrap) {
40      owl_function_error("wrap filter is not defined");
41      return &PL_sv_undef;
42  }
43
[f1e629d]44  h = newHV();
45
[3ea31b6]46#define MSG2H(h,field) (void)hv_store(h, #field, strlen(#field),        \
47                                      newSVpv(owl_message_get_##field(m),0), 0)
[f1e629d]48
49  if (owl_message_is_type_zephyr(m)
50      && owl_message_is_direction_in(m)) {
51    /* Handle zephyr-specific fields... */
52    AV *av_zfields;
53
54    av_zfields = newAV();
55    j=owl_zephyr_get_num_fields(owl_message_get_notice(m));
56    for (i=0; i<j; i++) {
[5376a95]57      ptr=owl_zephyr_get_field_as_utf8(owl_message_get_notice(m), i+1);
[b0430a6]58      av_push(av_zfields, newSVpvn(ptr, strlen(ptr)));
59      owl_free(ptr);
[f1e629d]60    }
[3ea31b6]61    (void)hv_store(h, "fields", strlen("fields"), newRV_noinc((SV*)av_zfields), 0);
[f1e629d]62
[3ea31b6]63    (void)hv_store(h, "auth", strlen("auth"), 
64                   newSVpv(owl_zephyr_get_authstr(owl_message_get_notice(m)),0),0);
[f1e629d]65  }
66
[421c8ef7]67  j=owl_list_get_size(&(m->attributes));
68  for(i=0; i<j; i++) {
69    pair=owl_list_get_element(&(m->attributes), i);
[3ea31b6]70    (void)hv_store(h, owl_pair_get_key(pair), strlen(owl_pair_get_key(pair)),
71                   newSVpv(owl_pair_get_value(pair),0),0);
[421c8ef7]72  }
73 
[f1e629d]74  MSG2H(h, type);
75  MSG2H(h, direction);
76  MSG2H(h, class);
77  MSG2H(h, instance);
78  MSG2H(h, sender);
79  MSG2H(h, realm);
80  MSG2H(h, recipient);
81  MSG2H(h, opcode);
82  MSG2H(h, hostname);
83  MSG2H(h, body);
84  MSG2H(h, login);
85  MSG2H(h, zsig);
86  MSG2H(h, zwriteline);
87  if (owl_message_get_header(m)) {
88    MSG2H(h, header); 
89  }
[3ea31b6]90  (void)hv_store(h, "time", strlen("time"), newSVpv(owl_message_get_timestr(m),0),0);
91  (void)hv_store(h, "id", strlen("id"), newSViv(owl_message_get_id(m)),0);
92  (void)hv_store(h, "deleted", strlen("deleted"), newSViv(owl_message_is_delete(m)),0);
93  (void)hv_store(h, "private", strlen("private"), newSViv(owl_message_is_private(m)),0);
94  (void)hv_store(h, "should_wordwrap",
95                 strlen("should_wordwrap"), newSViv(
96                                                    owl_filter_message_match(wrap, m)),0);
[f1e629d]97
[30678ae]98  type = owl_message_get_type(m);
[8fba2ee]99  if(!type || !*type) type = "generic";
[30678ae]100  type = owl_strdup(type);
101  type[0] = toupper(type[0]);
102  blessas = owl_sprintf("BarnOwl::Message::%s", type);
[f1e629d]103
[19bab8e]104  hr = newRV_noinc((SV*)h);
[1cc95709]105  stash =  gv_stashpv(blessas,0);
106  if(!stash) {
107    owl_function_error("No such class: %s for message type %s", blessas, owl_message_get_type(m));
108    stash = gv_stashpv("BarnOwl::Message", 1);
109  }
110  hr = sv_bless(hr,stash);
[30678ae]111  owl_free(type);
112  owl_free(blessas);
113  return hr;
[f1e629d]114}
115
[c3acb0b]116SV *owl_perlconfig_curmessage2hashref(void) /*noproto*/
117{
[f1e629d]118  int curmsg;
119  owl_view *v;
120  v=owl_global_get_current_view(&g);
121  if (owl_view_get_size(v) < 1) {
122    return &PL_sv_undef;
123  }
124  curmsg=owl_global_get_curmsg(&g);
125  return owl_perlconfig_message2hashref(owl_view_get_element(v, curmsg));
126}
127
[30678ae]128/* XXX TODO: Messages should round-trip properly between
129   message2hashref and hashref2message. Currently we lose
130   zephyr-specific properties stored in the ZNotice_t
131 */
132owl_message * owl_perlconfig_hashref2message(SV *msg)
133{
134  owl_message * m;
135  HE * ent;
136  I32 count, len;
137  char *key,*val;
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  }
175  return m;
176}
[f1e629d]177
178/* Calls in a scalar context, passing it a hash reference.
179   If return value is non-null, caller must free. */
[c3acb0b]180char *owl_perlconfig_call_with_message(char *subname, owl_message *m)
181{
[f1e629d]182  dSP ;
[c4ba74d]183  int count;
[f1e629d]184  SV *msgref, *srv;
[909771e]185  char *out;
[f1e629d]186 
187  ENTER ;
188  SAVETMPS;
189 
190  PUSHMARK(SP) ;
191  msgref = owl_perlconfig_message2hashref(m);
[19bab8e]192  XPUSHs(sv_2mortal(msgref));
[f1e629d]193  PUTBACK ;
194 
195  count = call_pv(subname, G_SCALAR|G_EVAL|G_KEEPERR);
196 
197  SPAGAIN ;
198
199  if (SvTRUE(ERRSV)) {
[ce6721f]200    owl_function_error("Perl Error: '%s'", SvPV_nolen(ERRSV));
[27c3a93]201    /* and clear the error */
202    sv_setsv (ERRSV, &PL_sv_undef);
[f1e629d]203  }
204
205  if (count != 1) {
206    fprintf(stderr, "bad perl!  no biscuit!  returned wrong count!\n");
207    abort();
208  }
209
210  srv = POPs;
211
212  if (srv) {
[909771e]213    out = owl_strdup(SvPV_nolen(srv));
[f1e629d]214  } else {
215    out = NULL;
216  }
217 
218  PUTBACK ;
219  FREETMPS ;
220  LEAVE ;
221
222  return out;
223}
224
[25729b2]225
226/* Calls a method on a perl object representing a message.
227   If the return value is non-null, the caller must free it.
228 */
229char * owl_perlconfig_message_call_method(owl_message *m, char *method, int argc, char ** argv)
230{
231  dSP;
[909771e]232  unsigned int count, i;
[25729b2]233  SV *msgref, *srv;
[909771e]234  char *out;
[25729b2]235
236  msgref = owl_perlconfig_message2hashref(m);
237
238  ENTER;
239  SAVETMPS;
240
241  PUSHMARK(SP);
[19bab8e]242  XPUSHs(sv_2mortal(msgref));
[25729b2]243  for(i=0;i<argc;i++) {
244    XPUSHs(sv_2mortal(newSVpv(argv[i], 0)));
245  }
246  PUTBACK;
247
248  count = call_method(method, G_SCALAR|G_KEEPERR|G_EVAL);
249
250  SPAGAIN;
251
252  if(count != 1) {
253    fprintf(stderr, "perl returned wrong count %d\n", count);
254    abort();
255  }
256
257  if (SvTRUE(ERRSV)) {
[ce6721f]258    owl_function_error("Error: '%s'", SvPV_nolen(ERRSV));
[25729b2]259    /* and clear the error */
260    sv_setsv (ERRSV, &PL_sv_undef);
261  }
262
263  srv = POPs;
264
265  if (srv) {
[909771e]266    out = owl_strdup(SvPV_nolen(srv));
[25729b2]267  } else {
268    out = NULL;
269  }
270
271  PUTBACK;
272  FREETMPS;
273  LEAVE;
274
275  return out;
276}
277
278
[b6c067a]279char *owl_perlconfig_initperl(char * file)
[c3acb0b]280{
[d03091c]281  int ret;
[f1e629d]282  PerlInterpreter *p;
[4e0f545]283  char *err;
[d03091c]284  char *args[4] = {"", "-e", "0;", NULL};
[f1e629d]285
286  /* create and initialize interpreter */
287  p=perl_alloc();
288  owl_global_set_perlinterp(&g, (void*)p);
289  perl_construct(p);
290
291  owl_global_set_no_have_config(&g);
292
[d03091c]293  ret=perl_parse(p, owl_perl_xs_init, 2, args, NULL);
[f1e629d]294  if (ret || SvTRUE(ERRSV)) {
[ce6721f]295    err=owl_strdup(SvPV_nolen(ERRSV));
[4e0f545]296    sv_setsv(ERRSV, &PL_sv_undef);     /* and clear the error */
297    return(err);
[f1e629d]298  }
299
300  ret=perl_run(p);
301  if (ret || SvTRUE(ERRSV)) {
[ce6721f]302    err=owl_strdup(SvPV_nolen(ERRSV));
[4e0f545]303    sv_setsv(ERRSV, &PL_sv_undef);     /* and clear the error */
304    return(err);
[f1e629d]305  }
306
307  owl_global_set_have_config(&g);
308
309  /* create legacy variables */
[c415aca]310  get_sv("BarnOwl::id", TRUE);
311  get_sv("BarnOwl::class", TRUE);
312  get_sv("BarnOwl::instance", TRUE);
313  get_sv("BarnOwl::recipient", TRUE);
314  get_sv("BarnOwl::sender", TRUE);
315  get_sv("BarnOwl::realm", TRUE);
316  get_sv("BarnOwl::opcode", TRUE);
317  get_sv("BarnOwl::zsig", TRUE);
318  get_sv("BarnOwl::msg", TRUE);
319  get_sv("BarnOwl::time", TRUE);
320  get_sv("BarnOwl::host", TRUE);
321  get_av("BarnOwl::fields", TRUE);
[00f9a7d]322
323  if(file) {
[8203afd]324    SV * cfg = get_sv("BarnOwl::configfile", TRUE);
[00f9a7d]325    sv_setpv(cfg, file);
326  }
327
[c415aca]328  eval_pv(owl_perlwrap_codebuff, FALSE);
[f1e629d]329
330  if (SvTRUE(ERRSV)) {
[ce6721f]331    err=owl_strdup(SvPV_nolen(ERRSV));
[27c3a93]332    sv_setsv (ERRSV, &PL_sv_undef);     /* and clear the error */
[4e0f545]333    return(err);
[f1e629d]334  }
335
336  /* check if we have the formatting function */
[8203afd]337  if (owl_perlconfig_is_function("BarnOwl::format_msg")) {
[f1e629d]338    owl_global_set_config_format(&g, 1);
339  }
340
341  return(NULL);
342}
343
344/* returns whether or not a function exists */
345int owl_perlconfig_is_function(char *fn) {
[c415aca]346  if (get_cv(fn, FALSE)) return(1);
[f1e629d]347  else return(0);
348}
349
350/* caller is responsible for freeing returned string */
[c3acb0b]351char *owl_perlconfig_execute(char *line)
352{
[f1e629d]353  STRLEN len;
354  SV *response;
355  char *out, *preout;
356
357  if (!owl_global_have_config(&g)) return NULL;
358
[e0096b7]359  ENTER;
360  SAVETMPS;
[f1e629d]361  /* execute the subroutine */
[c415aca]362  response = eval_pv(line, FALSE);
[f1e629d]363
364  if (SvTRUE(ERRSV)) {
[ce6721f]365    owl_function_error("Perl Error: '%s'", SvPV_nolen(ERRSV));
[27c3a93]366    sv_setsv (ERRSV, &PL_sv_undef);     /* and clear the error */
[f1e629d]367  }
368
369  preout=SvPV(response, len);
[909771e]370  if (len == 0 || preout[len - 1] != '\n')
371    out = owl_sprintf("%s\n", preout);
372  else
373    out = owl_strdup(preout);
[e0096b7]374  FREETMPS;
375  LEAVE;
[f1e629d]376
377  return(out);
378}
379
[b67ab6b]380void owl_perlconfig_getmsg(owl_message *m, char *subname)
381{
382  char *ptr = NULL;
383  if (owl_perlconfig_is_function("BarnOwl::Hooks::_receive_msg")) {
384    ptr = owl_perlconfig_call_with_message(subname?subname
385                                           :"BarnOwl::_receive_msg_legacy_wrap", m);
[f1e629d]386  }
[b67ab6b]387  if (ptr) owl_free(ptr);
[0f9eca7]388}
389
390/* Called on all new messages; receivemsg is only called on incoming ones */
391void owl_perlconfig_newmsg(owl_message *m, char *subname)
392{
393  char *ptr = NULL;
394  if (owl_perlconfig_is_function("BarnOwl::Hooks::_new_msg")) {
395    ptr = owl_perlconfig_call_with_message(subname?subname
396                                           :"BarnOwl::Hooks::_new_msg", m);
397  }
398  if (ptr) owl_free(ptr);
[f1e629d]399}
[6922edd]400
401char *owl_perlconfig_perlcmd(owl_cmd *cmd, int argc, char **argv)
402{
403  int i, count;
404  char * ret = NULL;
[ad15610]405  SV *rv;
[6922edd]406  dSP;
407
408  ENTER;
409  SAVETMPS;
410
411  PUSHMARK(SP);
412  for(i=0;i<argc;i++) {
[367fbf3]413    SV *tmp = newSVpv(argv[i], 0);
414    SvUTF8_on(tmp);
415    XPUSHs(sv_2mortal(tmp));
[6922edd]416  }
417  PUTBACK;
418
419  count = call_sv(cmd->cmd_perl, G_SCALAR|G_EVAL);
420
421  SPAGAIN;
422
423  if(SvTRUE(ERRSV)) {
[ce6721f]424    owl_function_error("%s", SvPV_nolen(ERRSV));
[c4ba74d]425    (void)POPs;
[6922edd]426  } else {
427    if(count != 1)
428      croak("Perl command %s returned more than one value!", cmd->name);
[ad15610]429    rv = POPs;
[6922edd]430    if(SvTRUE(rv)) {
[ce6721f]431      ret = owl_strdup(SvPV_nolen(rv));
[6922edd]432    }
433  }
434
435  FREETMPS;
436  LEAVE;
437
438  return ret;
439}
440
441void owl_perlconfig_cmd_free(owl_cmd *cmd)
442{
[ff13a6f]443  SvREFCNT_dec(cmd->cmd_perl);
[6922edd]444}
[db8b00b]445
[9c7a701]446void owl_perlconfig_dispatch_free(owl_dispatch *d)
447{
[f36cd97]448  SvREFCNT_dec(d->data);
[1895c29]449  owl_free(d);
[9c7a701]450}
451
[db8b00b]452void owl_perlconfig_edit_callback(owl_editwin *e)
453{
454  SV *cb = (SV*)(e->cbdata);
[367fbf3]455  SV *text;
[ad15610]456  dSP;
457
[db8b00b]458  if(cb == NULL) {
459    owl_function_error("Perl callback is NULL!");
460  }
[367fbf3]461  text = newSVpv(owl_editwin_get_text(e), 0);
462  SvUTF8_on(text);
[db8b00b]463
464  ENTER;
465  SAVETMPS;
466
467  PUSHMARK(SP);
[367fbf3]468  XPUSHs(sv_2mortal(text));
[db8b00b]469  PUTBACK;
470 
[9364a36]471  call_sv(cb, G_DISCARD|G_KEEPERR|G_EVAL);
472
473  if(SvTRUE(ERRSV)) {
[ce6721f]474    owl_function_error("%s", SvPV_nolen(ERRSV));
[9364a36]475  }
[db8b00b]476
477  FREETMPS;
478  LEAVE;
479
480  SvREFCNT_dec(cb);
481  e->cbdata = NULL;
482}
[f72f573]483
484void owl_perlconfig_mainloop()
485{
[ad15610]486  dSP;
[0337203]487  if (!owl_perlconfig_is_function("BarnOwl::Hooks::_mainloop_hook"))
[8203afd]488    return;
[f72f573]489  PUSHMARK(SP) ;
[0337203]490  call_pv("BarnOwl::Hooks::_mainloop_hook", G_DISCARD|G_EVAL);
[a55abb3]491  if(SvTRUE(ERRSV)) {
[ce6721f]492    owl_function_error("%s", SvPV_nolen(ERRSV));
[a55abb3]493  }
[f72f573]494  return;
495}
[9c7a701]496
[f36cd97]497void owl_perlconfig_dispatch(owl_dispatch *d)
[9c7a701]498{
[f36cd97]499  SV *cb = d->data;
[9c7a701]500  dSP;
501  if(cb == NULL) {
502    owl_function_error("Perl callback is NULL!");
[f36cd97]503    return;
[9c7a701]504  }
505
506  ENTER;
507  SAVETMPS;
508
509  PUSHMARK(SP);
510  PUTBACK;
511 
512  call_sv(cb, G_DISCARD|G_KEEPERR|G_EVAL);
513
514  if(SvTRUE(ERRSV)) {
[ce6721f]515    owl_function_error("%s", SvPV_nolen(ERRSV));
[9c7a701]516  }
517
518  FREETMPS;
519  LEAVE;
520}
[1631825]521
522void owl_perlconfig_perl_timer(owl_timer *t, void *data)
523{
524  SV *obj = data;
525
526  if(!SvROK(obj)) {
527    return;
528  }
529
530  dSP;
531  ENTER;
532  SAVETMPS;
533
534  PUSHMARK(SP);
535  XPUSHs(obj);
536  PUTBACK;
537
538  call_method("do_callback", G_DISCARD|G_KEEPERR|G_EVAL);
539
540  SPAGAIN;
541
542  if (SvTRUE(ERRSV)) {
543    owl_function_error("Error in calback: '%s'", SvPV_nolen(ERRSV));
544    sv_setsv (ERRSV, &PL_sv_undef);
545  }
546
547  PUTBACK;
548  FREETMPS;
549  LEAVE;
550}
551
552void owl_perlconfig_perl_timer_destroy(owl_timer *t)
553{
554  if(SvOK((SV*)t->data)) {
555    SvREFCNT_dec((SV*)t->data);
556  }
557}
Note: See TracBrowser for help on using the repository browser.