source: perlconfig.c @ f7a61ef

debianrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since f7a61ef was f7a61ef, checked in by Nelson Elhage <nelhage@mit.edu>, 15 years ago
Remove the unused owl_perlconfig_get_hashkeys
  • Property mode set to 100644
File size: 11.7 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) (void)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    (void)hv_store(h, "fields", strlen("fields"), newRV_noinc((SV*)av_zfields), 0);
62
63    (void)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    (void)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  (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);
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 = 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  SV *msgref, *srv;
185  char *out;
186 
187  ENTER ;
188  SAVETMPS;
189 
190  PUSHMARK(SP) ;
191  msgref = owl_perlconfig_message2hashref(m);
192  XPUSHs(sv_2mortal(msgref));
193  PUTBACK ;
194 
195  count = call_pv(subname, G_SCALAR|G_EVAL|G_KEEPERR);
196 
197  SPAGAIN ;
198
199  if (SvTRUE(ERRSV)) {
200    owl_function_error("Perl Error: '%s'", SvPV_nolen(ERRSV));
201    /* and clear the error */
202    sv_setsv (ERRSV, &PL_sv_undef);
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) {
213    out = owl_strdup(SvPV_nolen(srv));
214  } else {
215    out = NULL;
216  }
217 
218  PUTBACK ;
219  FREETMPS ;
220  LEAVE ;
221
222  return out;
223}
224
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;
232  unsigned int count, i;
233  SV *msgref, *srv;
234  char *out;
235
236  msgref = owl_perlconfig_message2hashref(m);
237
238  ENTER;
239  SAVETMPS;
240
241  PUSHMARK(SP);
242  XPUSHs(sv_2mortal(msgref));
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)) {
258    owl_function_error("Error: '%s'", SvPV_nolen(ERRSV));
259    /* and clear the error */
260    sv_setsv (ERRSV, &PL_sv_undef);
261  }
262
263  srv = POPs;
264
265  if (srv) {
266    out = owl_strdup(SvPV_nolen(srv));
267  } else {
268    out = NULL;
269  }
270
271  PUTBACK;
272  FREETMPS;
273  LEAVE;
274
275  return out;
276}
277
278
279char *owl_perlconfig_initperl(char * file)
280{
281  int ret;
282  PerlInterpreter *p;
283  char *err;
284  char *args[4] = {"", "-e", "0;", NULL};
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
293
294  ret=perl_parse(p, owl_perl_xs_init, 2, args, NULL);
295  if (ret || SvTRUE(ERRSV)) {
296    err=owl_strdup(SvPV_nolen(ERRSV));
297    sv_setsv(ERRSV, &PL_sv_undef);     /* and clear the error */
298    return(err);
299  }
300
301  ret=perl_run(p);
302  if (ret || SvTRUE(ERRSV)) {
303    err=owl_strdup(SvPV_nolen(ERRSV));
304    sv_setsv(ERRSV, &PL_sv_undef);     /* and clear the error */
305    return(err);
306  }
307
308  owl_global_set_have_config(&g);
309
310  /* create legacy variables */
311  perl_get_sv("BarnOwl::id", TRUE);
312  perl_get_sv("BarnOwl::class", TRUE);
313  perl_get_sv("BarnOwl::instance", TRUE);
314  perl_get_sv("BarnOwl::recipient", TRUE);
315  perl_get_sv("BarnOwl::sender", TRUE);
316  perl_get_sv("BarnOwl::realm", TRUE);
317  perl_get_sv("BarnOwl::opcode", TRUE);
318  perl_get_sv("BarnOwl::zsig", TRUE);
319  perl_get_sv("BarnOwl::msg", TRUE);
320  perl_get_sv("BarnOwl::time", TRUE);
321  perl_get_sv("BarnOwl::host", TRUE);
322  perl_get_av("BarnOwl::fields", TRUE);
323
324  if(file) {
325    SV * cfg = get_sv("BarnOwl::configfile", TRUE);
326    sv_setpv(cfg, file);
327  }
328
329  perl_eval_pv(owl_perlwrap_codebuff, FALSE);
330
331  if (SvTRUE(ERRSV)) {
332    err=owl_strdup(SvPV_nolen(ERRSV));
333    sv_setsv (ERRSV, &PL_sv_undef);     /* and clear the error */
334    return(err);
335  }
336
337  /* check if we have the formatting function */
338  if (owl_perlconfig_is_function("BarnOwl::format_msg")) {
339    owl_global_set_config_format(&g, 1);
340  }
341
342  return(NULL);
343}
344
345/* returns whether or not a function exists */
346int owl_perlconfig_is_function(char *fn) {
347  if (perl_get_cv(fn, FALSE)) return(1);
348  else return(0);
349}
350
351/* caller is responsible for freeing returned string */
352char *owl_perlconfig_execute(char *line)
353{
354  STRLEN len;
355  SV *response;
356  char *out, *preout;
357
358  if (!owl_global_have_config(&g)) return NULL;
359
360  /* execute the subroutine */
361  response = perl_eval_pv(line, FALSE);
362
363  if (SvTRUE(ERRSV)) {
364    owl_function_error("Perl Error: '%s'", SvPV_nolen(ERRSV));
365    sv_setsv (ERRSV, &PL_sv_undef);     /* and clear the error */
366  }
367
368  preout=SvPV(response, len);
369  if (len == 0 || preout[len - 1] != '\n')
370    out = owl_sprintf("%s\n", preout);
371  else
372    out = owl_strdup(preout);
373
374  return(out);
375}
376
377void owl_perlconfig_getmsg(owl_message *m, char *subname)
378{
379  char *ptr = NULL;
380  if (owl_perlconfig_is_function("BarnOwl::Hooks::_receive_msg")) {
381    ptr = owl_perlconfig_call_with_message(subname?subname
382                                           :"BarnOwl::_receive_msg_legacy_wrap", m);
383  }
384  if (ptr) owl_free(ptr);
385}
386
387/* Called on all new messages; receivemsg is only called on incoming ones */
388void owl_perlconfig_newmsg(owl_message *m, char *subname)
389{
390  char *ptr = NULL;
391  if (owl_perlconfig_is_function("BarnOwl::Hooks::_new_msg")) {
392    ptr = owl_perlconfig_call_with_message(subname?subname
393                                           :"BarnOwl::Hooks::_new_msg", m);
394  }
395  if (ptr) owl_free(ptr);
396}
397
398char *owl_perlconfig_perlcmd(owl_cmd *cmd, int argc, char **argv)
399{
400  int i, count;
401  char * ret = NULL;
402  SV *rv;
403  dSP;
404
405  ENTER;
406  SAVETMPS;
407
408  PUSHMARK(SP);
409  for(i=0;i<argc;i++) {
410    SV *tmp = newSVpv(argv[i], 0);
411    SvUTF8_on(tmp);
412    XPUSHs(sv_2mortal(tmp));
413  }
414  PUTBACK;
415
416  count = call_sv(cmd->cmd_perl, G_SCALAR|G_EVAL);
417
418  SPAGAIN;
419
420  if(SvTRUE(ERRSV)) {
421    owl_function_error("%s", SvPV_nolen(ERRSV));
422    (void)POPs;
423  } else {
424    if(count != 1)
425      croak("Perl command %s returned more than one value!", cmd->name);
426    rv = POPs;
427    if(SvTRUE(rv)) {
428      ret = owl_strdup(SvPV_nolen(rv));
429    }
430  }
431
432  FREETMPS;
433  LEAVE;
434
435  return ret;
436}
437
438void owl_perlconfig_cmd_free(owl_cmd *cmd)
439{
440  SvREFCNT_dec(cmd->cmd_perl);
441}
442
443void owl_perlconfig_dispatch_free(owl_dispatch *d)
444{
445  SvREFCNT_dec(d->pfunc);
446}
447
448void owl_perlconfig_edit_callback(owl_editwin *e)
449{
450  SV *cb = (SV*)(e->cbdata);
451  SV *text;
452  dSP;
453
454  if(cb == NULL) {
455    owl_function_error("Perl callback is NULL!");
456  }
457  text = newSVpv(owl_editwin_get_text(e), 0);
458  SvUTF8_on(text);
459
460  ENTER;
461  SAVETMPS;
462
463  PUSHMARK(SP);
464  XPUSHs(sv_2mortal(text));
465  PUTBACK;
466 
467  call_sv(cb, G_DISCARD|G_KEEPERR|G_EVAL);
468
469  if(SvTRUE(ERRSV)) {
470    owl_function_error("%s", SvPV_nolen(ERRSV));
471  }
472
473  FREETMPS;
474  LEAVE;
475
476  SvREFCNT_dec(cb);
477  e->cbdata = NULL;
478}
479
480void owl_perlconfig_mainloop()
481{
482  dSP;
483  if (!owl_perlconfig_is_function("BarnOwl::Hooks::_mainloop_hook"))
484    return;
485  PUSHMARK(SP) ;
486  call_pv("BarnOwl::Hooks::_mainloop_hook", G_DISCARD|G_EVAL);
487  if(SvTRUE(ERRSV)) {
488    owl_function_error("%s", SvPV_nolen(ERRSV));
489  }
490  return;
491}
492
493void owl_perlconfig_do_dispatch(owl_dispatch *d)
494{
495  SV *cb = d->pfunc;
496  dSP;
497  if(cb == NULL) {
498    owl_function_error("Perl callback is NULL!");
499  }
500
501  ENTER;
502  SAVETMPS;
503
504  PUSHMARK(SP);
505  PUTBACK;
506 
507  call_sv(cb, G_DISCARD|G_KEEPERR|G_EVAL);
508
509  if(SvTRUE(ERRSV)) {
510    owl_function_error("%s", SvPV_nolen(ERRSV));
511  }
512
513  FREETMPS;
514  LEAVE;
515}
Note: See TracBrowser for help on using the repository browser.