source: perlconfig.c @ c4ba74d

debianrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since c4ba74d was c4ba74d, checked in by Nelson Elhage <nelhage@mit.edu>, 16 years ago
Make BarnOwl::command() accept a pre-tokenized command. If given more than one argument, BarnOwl::command now treats the arguments as an argv list to execute the command with. Previously it only accepted a single argument, so this should not change the behavior of any existing code.
  • Property mode set to 100644
File size: 12.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
10static const char fileIdent[] = "$Id$";
11
12extern char *owl_perlwrap_codebuff;
13
14extern XS(boot_BarnOwl);
15extern XS(boot_DynaLoader);
16/* extern XS(boot_DBI); */
17
18static void owl_perl_xs_init(pTHX)
19{
20  char *file = __FILE__;
21  dXSUB_SYS;
22  {
23    newXS("BarnOwl::bootstrap", boot_BarnOwl, file);
24    newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
25  }
26}
27
28SV *owl_perlconfig_message2hashref(owl_message *m)
29{
30  HV *h, *stash;
31  SV *hr;
32  char *ptr, *blessas, *type;
33  int i, j;
34  owl_pair *pair;
35  owl_filter *wrap;
36
37  if (!m) return &PL_sv_undef;
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
44  h = newHV();
45
46#define MSG2H(h,field) hv_store(h, #field, strlen(#field), \
47                              newSVpv(owl_message_get_##field(m),0), 0)
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++) {
57      ptr=owl_zephyr_get_field_as_utf8(owl_message_get_notice(m), i+1);
58      av_push(av_zfields, newSVpvn(ptr, strlen(ptr)));
59      owl_free(ptr);
60    }
61    hv_store(h, "fields", strlen("fields"), newRV_noinc((SV*)av_zfields), 0);
62
63    hv_store(h, "auth", strlen("auth"), 
64             newSVpv(owl_zephyr_get_authstr(owl_message_get_notice(m)),0),0);
65  }
66
67  j=owl_list_get_size(&(m->attributes));
68  for(i=0; i<j; i++) {
69    pair=owl_list_get_element(&(m->attributes), i);
70    hv_store(h, owl_pair_get_key(pair), strlen(owl_pair_get_key(pair)),
71             newSVpv(owl_pair_get_value(pair),0),0);
72  }
73 
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  }
90  hv_store(h, "time", strlen("time"), newSVpv(owl_message_get_timestr(m),0),0);
91  hv_store(h, "id", strlen("id"), newSViv(owl_message_get_id(m)),0);
92  hv_store(h, "deleted", strlen("deleted"), newSViv(owl_message_is_delete(m)),0);
93  hv_store(h, "private", strlen("private"), newSViv(owl_message_is_private(m)),0);
94  hv_store(h, "should_wordwrap",
95           strlen("should_wordwrap"), newSViv(
96                                              owl_filter_message_match(wrap, m)),0);
97
98  type = owl_message_get_type(m);
99  if(!type || !*type) type = "generic";
100  type = owl_strdup(type);
101  type[0] = toupper(type[0]);
102  blessas = owl_sprintf("BarnOwl::Message::%s", type);
103
104  hr = sv_2mortal(newRV_noinc((SV*)h));
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);
111  owl_free(type);
112  owl_free(blessas);
113  return hr;
114}
115
116SV *owl_perlconfig_curmessage2hashref(void) /*noproto*/
117{
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
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;
139  struct tm tm;
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}
177
178/* Calls in a scalar context, passing it a hash reference.
179   If return value is non-null, caller must free. */
180char *owl_perlconfig_call_with_message(char *subname, owl_message *m)
181{
182  dSP ;
183  int count;
184  unsigned int len;
185  SV *msgref, *srv;
186  char *out, *preout;
187 
188  ENTER ;
189  SAVETMPS;
190 
191  PUSHMARK(SP) ;
192  msgref = owl_perlconfig_message2hashref(m);
193  XPUSHs(msgref);
194  PUTBACK ;
195 
196  count = call_pv(subname, G_SCALAR|G_EVAL|G_KEEPERR);
197 
198  SPAGAIN ;
199
200  if (SvTRUE(ERRSV)) {
201    STRLEN n_a;
202    owl_function_error("Perl Error: '%s'", SvPV(ERRSV, n_a));
203    /* and clear the error */
204    sv_setsv (ERRSV, &PL_sv_undef);
205  }
206
207  if (count != 1) {
208    fprintf(stderr, "bad perl!  no biscuit!  returned wrong count!\n");
209    abort();
210  }
211
212  srv = POPs;
213
214  if (srv) {
215    preout=SvPV(srv, len);
216    out = owl_malloc(strlen(preout)+1);
217    strncpy(out, preout, len);
218    out[len] = '\0';
219  } else {
220    out = NULL;
221  }
222 
223  PUTBACK ;
224  FREETMPS ;
225  LEAVE ;
226
227  return out;
228}
229
230
231/* Calls a method on a perl object representing a message.
232   If the return value is non-null, the caller must free it.
233 */
234char * owl_perlconfig_message_call_method(owl_message *m, char *method, int argc, char ** argv)
235{
236  dSP;
237  unsigned int count, len, i;
238  SV *msgref, *srv;
239  char *out, *preout;
240
241  msgref = owl_perlconfig_message2hashref(m);
242
243  ENTER;
244  SAVETMPS;
245
246  PUSHMARK(SP);
247  XPUSHs(msgref);
248  for(i=0;i<argc;i++) {
249    XPUSHs(sv_2mortal(newSVpv(argv[i], 0)));
250  }
251  PUTBACK;
252
253  count = call_method(method, G_SCALAR|G_KEEPERR|G_EVAL);
254
255  SPAGAIN;
256
257  if(count != 1) {
258    fprintf(stderr, "perl returned wrong count %d\n", count);
259    abort();
260  }
261
262  if (SvTRUE(ERRSV)) {
263    STRLEN n_a;
264    owl_function_error("Error: '%s'", SvPV(ERRSV, n_a));
265    /* and clear the error */
266    sv_setsv (ERRSV, &PL_sv_undef);
267  }
268
269  srv = POPs;
270
271  if (srv) {
272    preout=SvPV(srv, len);
273    out = owl_malloc(strlen(preout)+1);
274    strncpy(out, preout, len);
275    out[len] = '\0';
276  } else {
277    out = NULL;
278  }
279
280  PUTBACK;
281  FREETMPS;
282  LEAVE;
283
284  return out;
285}
286
287
288char *owl_perlconfig_initperl(char * file)
289{
290  int ret;
291  PerlInterpreter *p;
292  char *err;
293  char *args[4] = {"", "-e", "0;", NULL};
294
295  /* create and initialize interpreter */
296  p=perl_alloc();
297  owl_global_set_perlinterp(&g, (void*)p);
298  perl_construct(p);
299
300  owl_global_set_no_have_config(&g);
301
302
303  ret=perl_parse(p, owl_perl_xs_init, 2, args, NULL);
304  if (ret || SvTRUE(ERRSV)) {
305    STRLEN n_a;
306    err=owl_strdup(SvPV(ERRSV, n_a));
307    sv_setsv(ERRSV, &PL_sv_undef);     /* and clear the error */
308    return(err);
309  }
310
311  ret=perl_run(p);
312  if (ret || SvTRUE(ERRSV)) {
313    STRLEN n_a;
314    err=owl_strdup(SvPV(ERRSV, n_a));
315    sv_setsv(ERRSV, &PL_sv_undef);     /* and clear the error */
316    return(err);
317  }
318
319  owl_global_set_have_config(&g);
320
321  /* create legacy variables */
322  perl_get_sv("BarnOwl::id", TRUE);
323  perl_get_sv("BarnOwl::class", TRUE);
324  perl_get_sv("BarnOwl::instance", TRUE);
325  perl_get_sv("BarnOwl::recipient", TRUE);
326  perl_get_sv("BarnOwl::sender", TRUE);
327  perl_get_sv("BarnOwl::realm", TRUE);
328  perl_get_sv("BarnOwl::opcode", TRUE);
329  perl_get_sv("BarnOwl::zsig", TRUE);
330  perl_get_sv("BarnOwl::msg", TRUE);
331  perl_get_sv("BarnOwl::time", TRUE);
332  perl_get_sv("BarnOwl::host", TRUE);
333  perl_get_av("BarnOwl::fields", TRUE);
334
335  if(file) {
336    SV * cfg = get_sv("BarnOwl::configfile", TRUE);
337    sv_setpv(cfg, file);
338  }
339
340  perl_eval_pv(owl_perlwrap_codebuff, FALSE);
341
342  if (SvTRUE(ERRSV)) {
343    STRLEN n_a;
344    err=owl_strdup(SvPV(ERRSV, n_a));
345    sv_setsv (ERRSV, &PL_sv_undef);     /* and clear the error */
346    return(err);
347  }
348
349  /* check if we have the formatting function */
350  if (owl_perlconfig_is_function("BarnOwl::format_msg")) {
351    owl_global_set_config_format(&g, 1);
352  }
353
354  return(NULL);
355}
356
357/* returns whether or not a function exists */
358int owl_perlconfig_is_function(char *fn) {
359  if (perl_get_cv(fn, FALSE)) return(1);
360  else return(0);
361}
362
363/* returns 0 on success */
364int owl_perlconfig_get_hashkeys(char *hashname, owl_list *l)
365{
366  HV *hv;
367  HE *he;
368  char *key;
369  I32 i;
370
371  if (owl_list_create(l)) return(-1);
372  hv = get_hv(hashname, FALSE);
373  if (!hv) return(-1);
374  i = hv_iterinit(hv);
375  while ((he = hv_iternext(hv))) {
376    key = hv_iterkey(he, &i);
377    if (key) {
378      owl_list_append_element(l, owl_strdup(key));
379    }
380  }
381  return(0);
382}
383
384/* caller is responsible for freeing returned string */
385char *owl_perlconfig_execute(char *line)
386{
387  STRLEN len;
388  SV *response;
389  char *out, *preout;
390
391  if (!owl_global_have_config(&g)) return NULL;
392
393  /* execute the subroutine */
394  response = perl_eval_pv(line, FALSE);
395
396  if (SvTRUE(ERRSV)) {
397    STRLEN n_a;
398    owl_function_error("Perl Error: '%s'", SvPV(ERRSV, n_a));
399    sv_setsv (ERRSV, &PL_sv_undef);     /* and clear the error */
400  }
401
402  preout=SvPV(response, len);
403  /* leave enough space in case we have to add a newline */
404  out = owl_malloc(strlen(preout)+2);
405  strncpy(out, preout, len);
406  out[len] = '\0';
407  if (!strlen(out) || out[strlen(out)-1]!='\n') {
408    strcat(out, "\n");
409  }
410
411  return(out);
412}
413
414void owl_perlconfig_getmsg(owl_message *m, char *subname)
415{
416  char *ptr = NULL;
417  if (owl_perlconfig_is_function("BarnOwl::Hooks::_receive_msg")) {
418    ptr = owl_perlconfig_call_with_message(subname?subname
419                                           :"BarnOwl::_receive_msg_legacy_wrap", m);
420  }
421  if (ptr) owl_free(ptr);
422}
423
424/* Called on all new messages; receivemsg is only called on incoming ones */
425void owl_perlconfig_newmsg(owl_message *m, char *subname)
426{
427  char *ptr = NULL;
428  if (owl_perlconfig_is_function("BarnOwl::Hooks::_new_msg")) {
429    ptr = owl_perlconfig_call_with_message(subname?subname
430                                           :"BarnOwl::Hooks::_new_msg", m);
431  }
432  if (ptr) owl_free(ptr);
433}
434
435char *owl_perlconfig_perlcmd(owl_cmd *cmd, int argc, char **argv)
436{
437  int i, count;
438  char * ret = NULL;
439  SV *rv;
440  STRLEN n_a;
441  dSP;
442
443  ENTER;
444  SAVETMPS;
445
446  PUSHMARK(SP);
447  for(i=0;i<argc;i++) {
448    SV *tmp = newSVpv(argv[i], 0);
449    SvUTF8_on(tmp);
450    XPUSHs(sv_2mortal(tmp));
451  }
452  PUTBACK;
453
454  count = call_sv(cmd->cmd_perl, G_SCALAR|G_EVAL);
455
456  SPAGAIN;
457
458  if(SvTRUE(ERRSV)) {
459    owl_function_error("%s", SvPV(ERRSV, n_a));
460    (void)POPs;
461  } else {
462    if(count != 1)
463      croak("Perl command %s returned more than one value!", cmd->name);
464    rv = POPs;
465    if(SvTRUE(rv)) {
466      ret = owl_strdup(SvPV(rv, n_a));
467    }
468  }
469
470  FREETMPS;
471  LEAVE;
472
473  return ret;
474}
475
476void owl_perlconfig_cmd_free(owl_cmd *cmd)
477{
478  SvREFCNT_dec(cmd->cmd_perl);
479}
480
481void owl_perlconfig_dispatch_free(owl_dispatch *d)
482{
483  SvREFCNT_dec(d->pfunc);
484}
485
486void owl_perlconfig_edit_callback(owl_editwin *e)
487{
488  SV *cb = (SV*)(e->cbdata);
489  SV *text;
490  unsigned int n_a;
491  dSP;
492
493  if(cb == NULL) {
494    owl_function_error("Perl callback is NULL!");
495  }
496  text = newSVpv(owl_editwin_get_text(e), 0);
497  SvUTF8_on(text);
498
499  ENTER;
500  SAVETMPS;
501
502  PUSHMARK(SP);
503  XPUSHs(sv_2mortal(text));
504  PUTBACK;
505 
506  call_sv(cb, G_DISCARD|G_KEEPERR|G_EVAL);
507
508  if(SvTRUE(ERRSV)) {
509    owl_function_error("%s", SvPV(ERRSV, n_a));
510  }
511
512  FREETMPS;
513  LEAVE;
514
515  SvREFCNT_dec(cb);
516  e->cbdata = NULL;
517}
518
519void owl_perlconfig_mainloop()
520{
521  dSP;
522  if (!owl_perlconfig_is_function("BarnOwl::Hooks::_mainloop_hook"))
523    return;
524  PUSHMARK(SP) ;
525  call_pv("BarnOwl::Hooks::_mainloop_hook", G_DISCARD|G_EVAL);
526  if(SvTRUE(ERRSV)) {
527    STRLEN n_a;
528    owl_function_error("%s", SvPV(ERRSV, n_a));
529  }
530  return;
531}
532
533void owl_perlconfig_do_dispatch(owl_dispatch *d)
534{
535  SV *cb = d->pfunc;
536  unsigned int n_a;
537  dSP;
538  if(cb == NULL) {
539    owl_function_error("Perl callback is NULL!");
540  }
541
542  ENTER;
543  SAVETMPS;
544
545  PUSHMARK(SP);
546  PUTBACK;
547 
548  call_sv(cb, G_DISCARD|G_KEEPERR|G_EVAL);
549
550  if(SvTRUE(ERRSV)) {
551    owl_function_error("%s", SvPV(ERRSV, n_a));
552  }
553
554  FREETMPS;
555  LEAVE;
556}
Note: See TracBrowser for help on using the repository browser.