source: perlconfig.c @ a387d12e

barnowl_perlaimdebianrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since a387d12e was 0337203, checked in by Nelson Elhage <nelhage@mit.edu>, 18 years ago
First phase of the module rewrite. Internals now (IMO) somewhat cleaner. r19586@phanatique: nelhage | 2007-03-14 20:35:39 -0400 First pass at a cleaned up perlwrap.pm * Using a new hook style * Modules loaded by BarnOwl::ModuleLoader (not yet written) reload is unimplemented for now. If possible, I'd like it to live elsewhere. r19587@phanatique: nelhage | 2007-03-14 20:36:58 -0400 Switching to the new underscore internal hook names. r19592@phanatique: nelhage | 2007-03-16 00:34:00 -0400 Actually switch to _receive_msg r19593@phanatique: nelhage | 2007-03-16 00:34:27 -0400 Some minor cleanup of perlwrap.pm. Shoving fake entries into @INC. r19594@phanatique: nelhage | 2007-03-16 00:34:47 -0400 First revision of ModuleLoader.
  • 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
36  if (!m) return &PL_sv_undef;
37  h = newHV();
38
39#define MSG2H(h,field) hv_store(h, #field, strlen(#field), \
40                              newSVpv(owl_message_get_##field(m),0), 0)
41
42  if (owl_message_is_type_zephyr(m)
43      && owl_message_is_direction_in(m)) {
44    /* Handle zephyr-specific fields... */
45    AV *av_zfields;
46
47    av_zfields = newAV();
48    j=owl_zephyr_get_num_fields(owl_message_get_notice(m));
49    for (i=0; i<j; i++) {
50      ptr=owl_zephyr_get_field(owl_message_get_notice(m), i+1);
51      av_push(av_zfields, newSVpvn(ptr, strlen(ptr)));
52      owl_free(ptr);
53    }
54    hv_store(h, "fields", strlen("fields"), newRV_noinc((SV*)av_zfields), 0);
55
56    hv_store(h, "auth", strlen("auth"), 
57             newSVpv(owl_zephyr_get_authstr(owl_message_get_notice(m)),0),0);
58  }
59
60  j=owl_list_get_size(&(m->attributes));
61  for(i=0; i<j; i++) {
62    pair=owl_list_get_element(&(m->attributes), i);
63    hv_store(h, owl_pair_get_key(pair), strlen(owl_pair_get_key(pair)),
64             newSVpv(owl_pair_get_value(pair),0),0);
65  }
66 
67  MSG2H(h, type);
68  MSG2H(h, direction);
69  MSG2H(h, class);
70  MSG2H(h, instance);
71  MSG2H(h, sender);
72  MSG2H(h, realm);
73  MSG2H(h, recipient);
74  MSG2H(h, opcode);
75  MSG2H(h, hostname);
76  MSG2H(h, body);
77  MSG2H(h, login);
78  MSG2H(h, zsig);
79  MSG2H(h, zwriteline);
80  if (owl_message_get_header(m)) {
81    MSG2H(h, header); 
82  }
83  hv_store(h, "time", strlen("time"), newSVpv(owl_message_get_timestr(m),0),0);
84  hv_store(h, "id", strlen("id"), newSViv(owl_message_get_id(m)),0);
85  hv_store(h, "deleted", strlen("deleted"), newSViv(owl_message_is_delete(m)),0);
86  hv_store(h, "private", strlen("private"), newSViv(owl_message_is_private(m)),0);
87
88  type = owl_message_get_type(m);
89  if(!type || !*type) type = "generic";
90  type = owl_strdup(type);
91  type[0] = toupper(type[0]);
92  blessas = owl_sprintf("BarnOwl::Message::%s", type);
93
94  hr = sv_2mortal(newRV_noinc((SV*)h));
95  stash =  gv_stashpv(blessas,0);
96  if(!stash) {
97    owl_function_error("No such class: %s for message type %s", blessas, owl_message_get_type(m));
98    stash = gv_stashpv("BarnOwl::Message", 1);
99  }
100  hr = sv_bless(hr,stash);
101  owl_free(type);
102  owl_free(blessas);
103  return hr;
104}
105
106SV *owl_perlconfig_curmessage2hashref(void) /*noproto*/
107{
108  int curmsg;
109  owl_view *v;
110  v=owl_global_get_current_view(&g);
111  if (owl_view_get_size(v) < 1) {
112    return &PL_sv_undef;
113  }
114  curmsg=owl_global_get_curmsg(&g);
115  return owl_perlconfig_message2hashref(owl_view_get_element(v, curmsg));
116}
117
118/* XXX TODO: Messages should round-trip properly between
119   message2hashref and hashref2message. Currently we lose
120   zephyr-specific properties stored in the ZNotice_t
121 */
122owl_message * owl_perlconfig_hashref2message(SV *msg)
123{
124  owl_message * m;
125  HE * ent;
126  I32 count, len;
127  char *key,*val;
128  HV * hash;
129
130  hash = (HV*)SvRV(msg);
131
132  m = owl_malloc(sizeof(owl_message));
133  owl_message_init(m);
134
135  count = hv_iterinit(hash);
136  while((ent = hv_iternext(hash))) {
137    key = hv_iterkey(ent, &len);
138    val = SvPV_nolen(hv_iterval(hash, ent));
139    if(!strcmp(key, "type")) {
140      owl_message_set_type(m, val);
141    } else if(!strcmp(key, "direction")) {
142      owl_message_set_direction(m, owl_message_parse_direction(val));
143    } else if(!strcmp(key, "private")) {
144      SV * v = hv_iterval(hash, ent);
145      if(SvTRUE(v)) {
146        owl_message_set_isprivate(m);
147      }
148    } else if (!strcmp(key, "hostname")) {
149      owl_message_set_hostname(m, val);
150    } else if (!strcmp(key, "zwriteline")) {
151      owl_message_set_zwriteline(m, val);
152    } else if (!strcmp(key, "time")) {
153      m->timestr = owl_strdup(val);
154      struct tm tm;
155      strptime(val, "%a %b %d %T %Y", &tm);
156      m->time = mktime(&tm);
157    } else {
158      owl_message_set_attribute(m, key, val);
159    }
160  }
161  if(owl_message_is_type_admin(m)) {
162    if(!owl_message_get_attribute_value(m, "adminheader"))
163      owl_message_set_attribute(m, "adminheader", "");
164  }
165  return m;
166}
167
168/* Calls in a scalar context, passing it a hash reference.
169   If return value is non-null, caller must free. */
170char *owl_perlconfig_call_with_message(char *subname, owl_message *m)
171{
172  dSP ;
173  int count, len;
174  SV *msgref, *srv;
175  char *out, *preout;
176 
177  ENTER ;
178  SAVETMPS;
179 
180  PUSHMARK(SP) ;
181  msgref = owl_perlconfig_message2hashref(m);
182  XPUSHs(msgref);
183  PUTBACK ;
184 
185  count = call_pv(subname, G_SCALAR|G_EVAL|G_KEEPERR);
186 
187  SPAGAIN ;
188
189  if (SvTRUE(ERRSV)) {
190    STRLEN n_a;
191    owl_function_error("Perl Error: '%s'", SvPV(ERRSV, n_a));
192    /* and clear the error */
193    sv_setsv (ERRSV, &PL_sv_undef);
194  }
195
196  if (count != 1) {
197    fprintf(stderr, "bad perl!  no biscuit!  returned wrong count!\n");
198    abort();
199  }
200
201  srv = POPs;
202
203  if (srv) {
204    preout=SvPV(srv, len);
205    out = owl_malloc(strlen(preout)+1);
206    strncpy(out, preout, len);
207    out[len] = '\0';
208  } else {
209    out = NULL;
210  }
211 
212  PUTBACK ;
213  FREETMPS ;
214  LEAVE ;
215
216  return out;
217}
218
219
220/* Calls a method on a perl object representing a message.
221   If the return value is non-null, the caller must free it.
222 */
223char * owl_perlconfig_message_call_method(owl_message *m, char *method, int argc, char ** argv)
224{
225  dSP;
226  unsigned int count, len, i;
227  SV *msgref, *srv;
228  char *out, *preout;
229
230  msgref = owl_perlconfig_message2hashref(m);
231
232  ENTER;
233  SAVETMPS;
234
235  PUSHMARK(SP);
236  XPUSHs(msgref);
237  for(i=0;i<argc;i++) {
238    XPUSHs(sv_2mortal(newSVpv(argv[i], 0)));
239  }
240  PUTBACK;
241
242  count = call_method(method, G_SCALAR|G_KEEPERR|G_EVAL);
243
244  SPAGAIN;
245
246  if(count != 1) {
247    fprintf(stderr, "perl returned wrong count %d\n", count);
248    abort();
249  }
250
251  if (SvTRUE(ERRSV)) {
252    STRLEN n_a;
253    owl_function_error("Error: '%s'", SvPV(ERRSV, n_a));
254    /* and clear the error */
255    sv_setsv (ERRSV, &PL_sv_undef);
256  }
257
258  srv = POPs;
259
260  if (srv) {
261    preout=SvPV(srv, len);
262    out = owl_malloc(strlen(preout)+1);
263    strncpy(out, preout, len);
264    out[len] = '\0';
265  } else {
266    out = NULL;
267  }
268
269  PUTBACK;
270  FREETMPS;
271  LEAVE;
272
273  return out;
274}
275
276
277char *owl_perlconfig_initperl(char * file)
278{
279  int ret;
280  PerlInterpreter *p;
281  char *err;
282  char *args[4] = {"", "-e", "0;", NULL};
283
284  /* create and initialize interpreter */
285  p=perl_alloc();
286  owl_global_set_perlinterp(&g, (void*)p);
287  perl_construct(p);
288
289  owl_global_set_no_have_config(&g);
290
291
292  ret=perl_parse(p, owl_perl_xs_init, 2, args, NULL);
293  if (ret || SvTRUE(ERRSV)) {
294    STRLEN n_a;
295    err=owl_strdup(SvPV(ERRSV, n_a));
296    sv_setsv(ERRSV, &PL_sv_undef);     /* and clear the error */
297    return(err);
298  }
299
300  ret=perl_run(p);
301  if (ret || SvTRUE(ERRSV)) {
302    STRLEN n_a;
303    err=owl_strdup(SvPV(ERRSV, n_a));
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    STRLEN n_a;
333    err=owl_strdup(SvPV(ERRSV, n_a));
334    sv_setsv (ERRSV, &PL_sv_undef);     /* and clear the error */
335    return(err);
336  }
337
338  /* check if we have the formatting function */
339  if (owl_perlconfig_is_function("BarnOwl::format_msg")) {
340    owl_global_set_config_format(&g, 1);
341  }
342
343  return(NULL);
344}
345
346/* returns whether or not a function exists */
347int owl_perlconfig_is_function(char *fn) {
348  if (perl_get_cv(fn, FALSE)) return(1);
349  else return(0);
350}
351
352/* returns 0 on success */
353int owl_perlconfig_get_hashkeys(char *hashname, owl_list *l)
354{
355  HV *hv;
356  HE *he;
357  char *key;
358  I32 i;
359
360  if (owl_list_create(l)) return(-1);
361  hv = get_hv(hashname, FALSE);
362  if (!hv) return(-1);
363  i = hv_iterinit(hv);
364  while ((he = hv_iternext(hv))) {
365    key = hv_iterkey(he, &i);
366    if (key) {
367      owl_list_append_element(l, owl_strdup(key));
368    }
369  }
370  return(0);
371}
372
373/* caller is responsible for freeing returned string */
374char *owl_perlconfig_execute(char *line)
375{
376  STRLEN len;
377  SV *response;
378  char *out, *preout;
379
380  if (!owl_global_have_config(&g)) return NULL;
381
382  /* execute the subroutine */
383  response = perl_eval_pv(line, FALSE);
384
385  if (SvTRUE(ERRSV)) {
386    STRLEN n_a;
387    owl_function_error("Perl Error: '%s'", SvPV(ERRSV, n_a));
388    sv_setsv (ERRSV, &PL_sv_undef);     /* and clear the error */
389  }
390
391  preout=SvPV(response, len);
392  /* leave enough space in case we have to add a newline */
393  out = owl_malloc(strlen(preout)+2);
394  strncpy(out, preout, len);
395  out[len] = '\0';
396  if (!strlen(out) || out[strlen(out)-1]!='\n') {
397    strcat(out, "\n");
398  }
399
400  return(out);
401}
402
403char *owl_perlconfig_getmsg(owl_message *m, int mode, char *subname)
404{ 
405  /* if mode==1 we are doing message formatting.  The returned
406   * formatted message needs to be freed by the caller.
407   *
408   * if mode==0 we are just doing the message-has-been-received
409   * thing.
410   */
411  if (!owl_global_have_config(&g)) return(NULL);
412 
413  /* run the procedure corresponding to the mode */
414  if (mode==1) {
415    char *ret = NULL;
416    ret = owl_perlconfig_call_with_message(subname?subname
417                                           :"BarnOwl::_format_msg_legacy_wrap", m);
418    if (!ret) {
419      ret = owl_sprintf("@b([Perl Message Formatting Failed!])\n");
420    } 
421    return ret;
422  } else {
423    char *ptr = NULL;
424    if (owl_perlconfig_is_function("BarnOwl::Hooks::_receive_msg")) {
425      ptr = owl_perlconfig_call_with_message(subname?subname
426                                       :"BarnOwl::_receive_msg_legacy_wrap", m);
427    }
428    if (ptr) owl_free(ptr);
429    return(NULL);
430  }
431}
432
433char *owl_perlconfig_perlcmd(owl_cmd *cmd, int argc, char **argv)
434{
435  int i, count;
436  char * ret = NULL;
437  STRLEN n_a;
438  dSP;
439
440  ENTER;
441  SAVETMPS;
442
443  PUSHMARK(SP);
444  for(i=0;i<argc;i++) {
445    XPUSHs(sv_2mortal(newSVpv(argv[i], 0)));
446  }
447  PUTBACK;
448
449  count = call_sv(cmd->cmd_perl, G_SCALAR|G_EVAL);
450
451  SPAGAIN;
452
453  if(SvTRUE(ERRSV)) {
454    owl_function_error("%s", SvPV(ERRSV, n_a));
455    POPs;
456  } else {
457    if(count != 1)
458      croak("Perl command %s returned more than one value!", cmd->name);
459    SV * rv = POPs;
460    if(SvTRUE(rv)) {
461      ret = owl_strdup(SvPV(rv, n_a));
462    }
463  }
464
465  FREETMPS;
466  LEAVE;
467
468  return ret;
469}
470
471void owl_perlconfig_cmd_free(owl_cmd *cmd)
472{
473  SvREFCNT_dec(cmd);
474}
475
476void owl_perlconfig_edit_callback(owl_editwin *e)
477{
478  SV *cb = (SV*)(e->cbdata);
479  unsigned int n_a;
480  if(cb == NULL) {
481    owl_function_error("Perl callback is NULL!");
482  }
483
484  dSP;
485
486  ENTER;
487  SAVETMPS;
488
489  PUSHMARK(SP);
490  XPUSHs(sv_2mortal(newSVpv(owl_editwin_get_text(e), 0)));
491  PUTBACK;
492 
493  call_sv(cb, G_DISCARD|G_KEEPERR|G_EVAL);
494
495  if(SvTRUE(ERRSV)) {
496    owl_function_error("%s", SvPV(ERRSV, n_a));
497  }
498
499  FREETMPS;
500  LEAVE;
501
502  SvREFCNT_dec(cb);
503  e->cbdata = NULL;
504}
505
506void owl_perlconfig_mainloop()
507{
508  if (!owl_perlconfig_is_function("BarnOwl::Hooks::_mainloop_hook"))
509    return;
510  dSP ;
511  PUSHMARK(SP) ;
512  call_pv("BarnOwl::Hooks::_mainloop_hook", G_DISCARD|G_EVAL);
513  if(SvTRUE(ERRSV)) {
514    STRLEN n_a;
515    owl_function_error("%s", SvPV(ERRSV, n_a));
516  }
517  return;
518}
Note: See TracBrowser for help on using the repository browser.