source: perlconfig.c @ 86840c5

barnowl_perlaimdebianrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 86840c5 was ad15610, checked in by Nelson Elhage <nelhage@mit.edu>, 17 years ago
Applying ctl's variable shuffling patch for better ANSI C-ness. closes #18
  • Property mode set to 100644
File size: 12.0 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);
[6922edd]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
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++) {
[b0430a6]57      ptr=owl_zephyr_get_field(owl_message_get_notice(m), i+1);
58      av_push(av_zfields, newSVpvn(ptr, strlen(ptr)));
59      owl_free(ptr);
[f1e629d]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
[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);
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 
[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  }
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);
[216c734]93  hv_store(h, "private", strlen("private"), newSViv(owl_message_is_private(m)),0);
[f6b319c]94  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
104  hr = sv_2mortal(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 ;
183  int count, len;
184  SV *msgref, *srv;
185  char *out, *preout;
186 
187  ENTER ;
188  SAVETMPS;
189 
190  PUSHMARK(SP) ;
191  msgref = owl_perlconfig_message2hashref(m);
192  XPUSHs(msgref);
193  PUTBACK ;
194 
195  count = call_pv(subname, G_SCALAR|G_EVAL|G_KEEPERR);
196 
197  SPAGAIN ;
198
199  if (SvTRUE(ERRSV)) {
200    STRLEN n_a;
[27c3a93]201    owl_function_error("Perl Error: '%s'", SvPV(ERRSV, n_a));
202    /* and clear the error */
203    sv_setsv (ERRSV, &PL_sv_undef);
[f1e629d]204  }
205
206  if (count != 1) {
207    fprintf(stderr, "bad perl!  no biscuit!  returned wrong count!\n");
208    abort();
209  }
210
211  srv = POPs;
212
213  if (srv) {
214    preout=SvPV(srv, len);
215    out = owl_malloc(strlen(preout)+1);
216    strncpy(out, preout, len);
217    out[len] = '\0';
218  } else {
219    out = NULL;
220  }
221 
222  PUTBACK ;
223  FREETMPS ;
224  LEAVE ;
225
226  return out;
227}
228
[25729b2]229
230/* Calls a method on a perl object representing a message.
231   If the return value is non-null, the caller must free it.
232 */
233char * owl_perlconfig_message_call_method(owl_message *m, char *method, int argc, char ** argv)
234{
235  dSP;
236  unsigned int count, len, i;
237  SV *msgref, *srv;
238  char *out, *preout;
239
240  msgref = owl_perlconfig_message2hashref(m);
241
242  ENTER;
243  SAVETMPS;
244
245  PUSHMARK(SP);
246  XPUSHs(msgref);
247  for(i=0;i<argc;i++) {
248    XPUSHs(sv_2mortal(newSVpv(argv[i], 0)));
249  }
250  PUTBACK;
251
252  count = call_method(method, G_SCALAR|G_KEEPERR|G_EVAL);
253
254  SPAGAIN;
255
256  if(count != 1) {
257    fprintf(stderr, "perl returned wrong count %d\n", count);
258    abort();
259  }
260
261  if (SvTRUE(ERRSV)) {
262    STRLEN n_a;
263    owl_function_error("Error: '%s'", SvPV(ERRSV, n_a));
264    /* and clear the error */
265    sv_setsv (ERRSV, &PL_sv_undef);
266  }
267
268  srv = POPs;
269
270  if (srv) {
271    preout=SvPV(srv, len);
272    out = owl_malloc(strlen(preout)+1);
273    strncpy(out, preout, len);
274    out[len] = '\0';
275  } else {
276    out = NULL;
277  }
278
279  PUTBACK;
280  FREETMPS;
281  LEAVE;
282
283  return out;
284}
285
286
[b6c067a]287char *owl_perlconfig_initperl(char * file)
[c3acb0b]288{
[d03091c]289  int ret;
[f1e629d]290  PerlInterpreter *p;
[4e0f545]291  char *err;
[d03091c]292  char *args[4] = {"", "-e", "0;", NULL};
[f1e629d]293
294  /* create and initialize interpreter */
295  p=perl_alloc();
296  owl_global_set_perlinterp(&g, (void*)p);
297  perl_construct(p);
298
299  owl_global_set_no_have_config(&g);
300
[4e0f545]301
[d03091c]302  ret=perl_parse(p, owl_perl_xs_init, 2, args, NULL);
[f1e629d]303  if (ret || SvTRUE(ERRSV)) {
304    STRLEN n_a;
[4e0f545]305    err=owl_strdup(SvPV(ERRSV, n_a));
306    sv_setsv(ERRSV, &PL_sv_undef);     /* and clear the error */
307    return(err);
[f1e629d]308  }
309
310  ret=perl_run(p);
311  if (ret || SvTRUE(ERRSV)) {
312    STRLEN n_a;
[4e0f545]313    err=owl_strdup(SvPV(ERRSV, n_a));
314    sv_setsv(ERRSV, &PL_sv_undef);     /* and clear the error */
315    return(err);
[f1e629d]316  }
317
318  owl_global_set_have_config(&g);
319
320  /* create legacy variables */
[8203afd]321  perl_get_sv("BarnOwl::id", TRUE);
322  perl_get_sv("BarnOwl::class", TRUE);
323  perl_get_sv("BarnOwl::instance", TRUE);
324  perl_get_sv("BarnOwl::recipient", TRUE);
325  perl_get_sv("BarnOwl::sender", TRUE);
326  perl_get_sv("BarnOwl::realm", TRUE);
327  perl_get_sv("BarnOwl::opcode", TRUE);
328  perl_get_sv("BarnOwl::zsig", TRUE);
329  perl_get_sv("BarnOwl::msg", TRUE);
330  perl_get_sv("BarnOwl::time", TRUE);
331  perl_get_sv("BarnOwl::host", TRUE);
332  perl_get_av("BarnOwl::fields", TRUE);
[00f9a7d]333
334  if(file) {
[8203afd]335    SV * cfg = get_sv("BarnOwl::configfile", TRUE);
[00f9a7d]336    sv_setpv(cfg, file);
337  }
338
[f1e629d]339  perl_eval_pv(owl_perlwrap_codebuff, FALSE);
340
341  if (SvTRUE(ERRSV)) {
342    STRLEN n_a;
[4e0f545]343    err=owl_strdup(SvPV(ERRSV, n_a));
[27c3a93]344    sv_setsv (ERRSV, &PL_sv_undef);     /* and clear the error */
[4e0f545]345    return(err);
[f1e629d]346  }
347
348  /* check if we have the formatting function */
[8203afd]349  if (owl_perlconfig_is_function("BarnOwl::format_msg")) {
[f1e629d]350    owl_global_set_config_format(&g, 1);
351  }
352
353  return(NULL);
354}
355
356/* returns whether or not a function exists */
357int owl_perlconfig_is_function(char *fn) {
358  if (perl_get_cv(fn, FALSE)) return(1);
359  else return(0);
360}
361
362/* returns 0 on success */
[c3acb0b]363int owl_perlconfig_get_hashkeys(char *hashname, owl_list *l)
364{
[f1e629d]365  HV *hv;
366  HE *he;
367  char *key;
368  I32 i;
369
370  if (owl_list_create(l)) return(-1);
371  hv = get_hv(hashname, FALSE);
372  if (!hv) return(-1);
373  i = hv_iterinit(hv);
374  while ((he = hv_iternext(hv))) {
375    key = hv_iterkey(he, &i);
376    if (key) {
377      owl_list_append_element(l, owl_strdup(key));
378    }
379  }
380  return(0);
381}
382
383/* caller is responsible for freeing returned string */
[c3acb0b]384char *owl_perlconfig_execute(char *line)
385{
[f1e629d]386  STRLEN len;
387  SV *response;
388  char *out, *preout;
389
390  if (!owl_global_have_config(&g)) return NULL;
391
392  /* execute the subroutine */
393  response = perl_eval_pv(line, FALSE);
394
395  if (SvTRUE(ERRSV)) {
396    STRLEN n_a;
[27c3a93]397    owl_function_error("Perl Error: '%s'", SvPV(ERRSV, n_a));
398    sv_setsv (ERRSV, &PL_sv_undef);     /* and clear the error */
[f1e629d]399  }
400
401  preout=SvPV(response, len);
402  /* leave enough space in case we have to add a newline */
403  out = owl_malloc(strlen(preout)+2);
404  strncpy(out, preout, len);
405  out[len] = '\0';
406  if (!strlen(out) || out[strlen(out)-1]!='\n') {
407    strcat(out, "\n");
408  }
409
410  return(out);
411}
412
[c3acb0b]413char *owl_perlconfig_getmsg(owl_message *m, int mode, char *subname)
414{ 
[f1e629d]415  /* if mode==1 we are doing message formatting.  The returned
416   * formatted message needs to be freed by the caller.
417   *
418   * if mode==0 we are just doing the message-has-been-received
419   * thing.
420   */
421  if (!owl_global_have_config(&g)) return(NULL);
422 
423  /* run the procedure corresponding to the mode */
424  if (mode==1) {
425    char *ret = NULL;
426    ret = owl_perlconfig_call_with_message(subname?subname
[8203afd]427                                           :"BarnOwl::_format_msg_legacy_wrap", m);
[f1e629d]428    if (!ret) {
429      ret = owl_sprintf("@b([Perl Message Formatting Failed!])\n");
430    } 
431    return ret;
432  } else {
433    char *ptr = NULL;
[0337203]434    if (owl_perlconfig_is_function("BarnOwl::Hooks::_receive_msg")) {
[c0a90c2]435      ptr = owl_perlconfig_call_with_message(subname?subname
[8203afd]436                                       :"BarnOwl::_receive_msg_legacy_wrap", m);
[f1e629d]437    }
438    if (ptr) owl_free(ptr);
439    return(NULL);
440  }
441}
[6922edd]442
443char *owl_perlconfig_perlcmd(owl_cmd *cmd, int argc, char **argv)
444{
445  int i, count;
446  char * ret = NULL;
[ad15610]447  SV *rv;
[6922edd]448  STRLEN n_a;
449  dSP;
450
451  ENTER;
452  SAVETMPS;
453
454  PUSHMARK(SP);
455  for(i=0;i<argc;i++) {
456    XPUSHs(sv_2mortal(newSVpv(argv[i], 0)));
457  }
458  PUTBACK;
459
460  count = call_sv(cmd->cmd_perl, G_SCALAR|G_EVAL);
461
462  SPAGAIN;
463
464  if(SvTRUE(ERRSV)) {
[a55abb3]465    owl_function_error("%s", SvPV(ERRSV, n_a));
[6922edd]466    POPs;
467  } else {
468    if(count != 1)
469      croak("Perl command %s returned more than one value!", cmd->name);
[ad15610]470    rv = POPs;
[6922edd]471    if(SvTRUE(rv)) {
472      ret = owl_strdup(SvPV(rv, n_a));
473    }
474  }
475
476  FREETMPS;
477  LEAVE;
478
479  return ret;
480}
481
482void owl_perlconfig_cmd_free(owl_cmd *cmd)
483{
484  SvREFCNT_dec(cmd);
485}
[db8b00b]486
487void owl_perlconfig_edit_callback(owl_editwin *e)
488{
489  SV *cb = (SV*)(e->cbdata);
[9364a36]490  unsigned int n_a;
[ad15610]491  dSP;
492
[db8b00b]493  if(cb == NULL) {
494    owl_function_error("Perl callback is NULL!");
495  }
496
497  ENTER;
498  SAVETMPS;
499
500  PUSHMARK(SP);
501  XPUSHs(sv_2mortal(newSVpv(owl_editwin_get_text(e), 0)));
502  PUTBACK;
503 
[9364a36]504  call_sv(cb, G_DISCARD|G_KEEPERR|G_EVAL);
505
506  if(SvTRUE(ERRSV)) {
507    owl_function_error("%s", SvPV(ERRSV, n_a));
508  }
[db8b00b]509
510  FREETMPS;
511  LEAVE;
512
513  SvREFCNT_dec(cb);
514  e->cbdata = NULL;
515}
[f72f573]516
517void owl_perlconfig_mainloop()
518{
[ad15610]519  dSP;
[0337203]520  if (!owl_perlconfig_is_function("BarnOwl::Hooks::_mainloop_hook"))
[8203afd]521    return;
[f72f573]522  PUSHMARK(SP) ;
[0337203]523  call_pv("BarnOwl::Hooks::_mainloop_hook", G_DISCARD|G_EVAL);
[a55abb3]524  if(SvTRUE(ERRSV)) {
525    STRLEN n_a;
526    owl_function_error("%s", SvPV(ERRSV, n_a));
527  }
[f72f573]528  return;
529}
Note: See TracBrowser for help on using the repository browser.