source: perlconfig.c @ 186cdc4

barnowl_perlaimdebianrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 186cdc4 was b6c067a, checked in by Nelson Elhage <nelhage@mit.edu>, 17 years ago
Moving the default style into perl, and reorganizing things so we can bootstrap the style into place in time.
  • Property mode set to 100644
File size: 11.5 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{
[f1e629d]30  HV *h;
31  SV *hr;
[30678ae]32  char *ptr, *blessas, *type;
[b0430a6]33  int i, j;
[421c8ef7]34  owl_pair *pair;
[f1e629d]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++) {
[b0430a6]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);
[f1e629d]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
[421c8ef7]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 
[f1e629d]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);
[216c734]86  hv_store(h, "private", strlen("private"), newSViv(owl_message_is_private(m)),0);
[f1e629d]87
[30678ae]88  type = owl_message_get_type(m);
[8fba2ee]89  if(!type || !*type) type = "generic";
[30678ae]90  type = owl_strdup(type);
91  type[0] = toupper(type[0]);
92  blessas = owl_sprintf("BarnOwl::Message::%s", type);
[f1e629d]93
94  hr = sv_2mortal(newRV_noinc((SV*)h));
[30678ae]95  hr = sv_bless(hr, gv_stashpv(blessas,0));
96  owl_free(type);
97  owl_free(blessas);
98  return hr;
[f1e629d]99}
100
[c3acb0b]101SV *owl_perlconfig_curmessage2hashref(void) /*noproto*/
102{
[f1e629d]103  int curmsg;
104  owl_view *v;
105  v=owl_global_get_current_view(&g);
106  if (owl_view_get_size(v) < 1) {
107    return &PL_sv_undef;
108  }
109  curmsg=owl_global_get_curmsg(&g);
110  return owl_perlconfig_message2hashref(owl_view_get_element(v, curmsg));
111}
112
[30678ae]113/* XXX TODO: Messages should round-trip properly between
114   message2hashref and hashref2message. Currently we lose
115   zephyr-specific properties stored in the ZNotice_t
116 */
117owl_message * owl_perlconfig_hashref2message(SV *msg)
118{
119  owl_message * m;
120  HE * ent;
121  I32 count, len;
122  char *key,*val;
123  HV * hash;
124
125  hash = (HV*)SvRV(msg);
126
127  m = owl_malloc(sizeof(owl_message));
128  owl_message_init(m);
129
130  count = hv_iterinit(hash);
131  while((ent = hv_iternext(hash))) {
132    key = hv_iterkey(ent, &len);
133    val = SvPV_nolen(hv_iterval(hash, ent));
134    if(!strcmp(key, "type")) {
135      owl_message_set_type(m, val);
136    } else if(!strcmp(key, "direction")) {
137      owl_message_set_direction(m, owl_message_parse_direction(val));
138    } else if(!strcmp(key, "private")) {
139      SV * v = hv_iterval(hash, ent);
140      if(SvTRUE(v)) {
141        owl_message_set_isprivate(m);
142      }
143    } else if (!strcmp(key, "hostname")) {
144      owl_message_set_hostname(m, val);
145    } else if (!strcmp(key, "zwriteline")) {
146      owl_message_set_zwriteline(m, val);
147    } else if (!strcmp(key, "time")) {
148      m->timestr = owl_strdup(val);
149      struct tm tm;
150      strptime(val, "%a %b %d %T %Y", &tm);
151      m->time = mktime(&tm);
152    } else {
153      owl_message_set_attribute(m, key, val);
154    }
155  }
156  if(owl_message_is_type_admin(m)) {
157    if(!owl_message_get_attribute_value(m, "adminheader"))
158      owl_message_set_attribute(m, "adminheader", "");
159  }
160  return m;
161}
[f1e629d]162
163/* Calls in a scalar context, passing it a hash reference.
164   If return value is non-null, caller must free. */
[c3acb0b]165char *owl_perlconfig_call_with_message(char *subname, owl_message *m)
166{
[f1e629d]167  dSP ;
168  int count, len;
169  SV *msgref, *srv;
170  char *out, *preout;
171 
172  ENTER ;
173  SAVETMPS;
174 
175  PUSHMARK(SP) ;
176  msgref = owl_perlconfig_message2hashref(m);
177  XPUSHs(msgref);
178  PUTBACK ;
179 
180  count = call_pv(subname, G_SCALAR|G_EVAL|G_KEEPERR);
181 
182  SPAGAIN ;
183
184  if (SvTRUE(ERRSV)) {
185    STRLEN n_a;
[27c3a93]186    owl_function_error("Perl Error: '%s'", SvPV(ERRSV, n_a));
187    /* and clear the error */
188    sv_setsv (ERRSV, &PL_sv_undef);
[f1e629d]189  }
190
191  if (count != 1) {
192    fprintf(stderr, "bad perl!  no biscuit!  returned wrong count!\n");
193    abort();
194  }
195
196  srv = POPs;
197
198  if (srv) {
199    preout=SvPV(srv, len);
200    out = owl_malloc(strlen(preout)+1);
201    strncpy(out, preout, len);
202    out[len] = '\0';
203  } else {
204    out = NULL;
205  }
206 
207  PUTBACK ;
208  FREETMPS ;
209  LEAVE ;
210
211  return out;
212}
213
[25729b2]214
215/* Calls a method on a perl object representing a message.
216   If the return value is non-null, the caller must free it.
217 */
218char * owl_perlconfig_message_call_method(owl_message *m, char *method, int argc, char ** argv)
219{
220  dSP;
221  unsigned int count, len, i;
222  SV *msgref, *srv;
223  char *out, *preout;
224
225  msgref = owl_perlconfig_message2hashref(m);
226
227  ENTER;
228  SAVETMPS;
229
230  PUSHMARK(SP);
231  XPUSHs(msgref);
232  for(i=0;i<argc;i++) {
233    XPUSHs(sv_2mortal(newSVpv(argv[i], 0)));
234  }
235  PUTBACK;
236
237  count = call_method(method, G_SCALAR|G_KEEPERR|G_EVAL);
238
239  SPAGAIN;
240
241  if(count != 1) {
242    fprintf(stderr, "perl returned wrong count %d\n", count);
243    abort();
244  }
245
246  if (SvTRUE(ERRSV)) {
247    STRLEN n_a;
248    owl_function_error("Error: '%s'", SvPV(ERRSV, n_a));
249    /* and clear the error */
250    sv_setsv (ERRSV, &PL_sv_undef);
251  }
252
253  srv = POPs;
254
255  if (srv) {
256    preout=SvPV(srv, len);
257    out = owl_malloc(strlen(preout)+1);
258    strncpy(out, preout, len);
259    out[len] = '\0';
260  } else {
261    out = NULL;
262  }
263
264  PUTBACK;
265  FREETMPS;
266  LEAVE;
267
268  return out;
269}
270
271
[b6c067a]272char *owl_perlconfig_initperl(char * file)
[c3acb0b]273{
[d03091c]274  int ret;
[f1e629d]275  PerlInterpreter *p;
[4e0f545]276  char *err;
[d03091c]277  char *args[4] = {"", "-e", "0;", NULL};
[f1e629d]278
279  /* create and initialize interpreter */
280  p=perl_alloc();
281  owl_global_set_perlinterp(&g, (void*)p);
282  perl_construct(p);
283
284  owl_global_set_no_have_config(&g);
285
[4e0f545]286
[d03091c]287  ret=perl_parse(p, owl_perl_xs_init, 2, args, NULL);
[f1e629d]288  if (ret || SvTRUE(ERRSV)) {
289    STRLEN n_a;
[4e0f545]290    err=owl_strdup(SvPV(ERRSV, n_a));
291    sv_setsv(ERRSV, &PL_sv_undef);     /* and clear the error */
292    return(err);
[f1e629d]293  }
294
295  ret=perl_run(p);
296  if (ret || SvTRUE(ERRSV)) {
297    STRLEN n_a;
[4e0f545]298    err=owl_strdup(SvPV(ERRSV, n_a));
299    sv_setsv(ERRSV, &PL_sv_undef);     /* and clear the error */
300    return(err);
[f1e629d]301  }
302
303  owl_global_set_have_config(&g);
304
305  /* create legacy variables */
[8203afd]306  perl_get_sv("BarnOwl::id", TRUE);
307  perl_get_sv("BarnOwl::class", TRUE);
308  perl_get_sv("BarnOwl::instance", TRUE);
309  perl_get_sv("BarnOwl::recipient", TRUE);
310  perl_get_sv("BarnOwl::sender", TRUE);
311  perl_get_sv("BarnOwl::realm", TRUE);
312  perl_get_sv("BarnOwl::opcode", TRUE);
313  perl_get_sv("BarnOwl::zsig", TRUE);
314  perl_get_sv("BarnOwl::msg", TRUE);
315  perl_get_sv("BarnOwl::time", TRUE);
316  perl_get_sv("BarnOwl::host", TRUE);
317  perl_get_av("BarnOwl::fields", TRUE);
[00f9a7d]318
319  if(file) {
[8203afd]320    SV * cfg = get_sv("BarnOwl::configfile", TRUE);
[00f9a7d]321    sv_setpv(cfg, file);
322  }
323
[f1e629d]324  perl_eval_pv(owl_perlwrap_codebuff, FALSE);
325
326  if (SvTRUE(ERRSV)) {
327    STRLEN n_a;
[4e0f545]328    err=owl_strdup(SvPV(ERRSV, n_a));
[27c3a93]329    sv_setsv (ERRSV, &PL_sv_undef);     /* and clear the error */
[4e0f545]330    return(err);
[f1e629d]331  }
332
333  /* check if we have the formatting function */
[8203afd]334  if (owl_perlconfig_is_function("BarnOwl::format_msg")) {
[f1e629d]335    owl_global_set_config_format(&g, 1);
336  }
337
338  return(NULL);
339}
340
341/* returns whether or not a function exists */
342int owl_perlconfig_is_function(char *fn) {
343  if (perl_get_cv(fn, FALSE)) return(1);
344  else return(0);
345}
346
347/* returns 0 on success */
[c3acb0b]348int owl_perlconfig_get_hashkeys(char *hashname, owl_list *l)
349{
[f1e629d]350  HV *hv;
351  HE *he;
352  char *key;
353  I32 i;
354
355  if (owl_list_create(l)) return(-1);
356  hv = get_hv(hashname, FALSE);
357  if (!hv) return(-1);
358  i = hv_iterinit(hv);
359  while ((he = hv_iternext(hv))) {
360    key = hv_iterkey(he, &i);
361    if (key) {
362      owl_list_append_element(l, owl_strdup(key));
363    }
364  }
365  return(0);
366}
367
368/* caller is responsible for freeing returned string */
[c3acb0b]369char *owl_perlconfig_execute(char *line)
370{
[f1e629d]371  STRLEN len;
372  SV *response;
373  char *out, *preout;
374
375  if (!owl_global_have_config(&g)) return NULL;
376
377  /* execute the subroutine */
378  response = perl_eval_pv(line, FALSE);
379
380  if (SvTRUE(ERRSV)) {
381    STRLEN n_a;
[27c3a93]382    owl_function_error("Perl Error: '%s'", SvPV(ERRSV, n_a));
383    sv_setsv (ERRSV, &PL_sv_undef);     /* and clear the error */
[f1e629d]384  }
385
386  preout=SvPV(response, len);
387  /* leave enough space in case we have to add a newline */
388  out = owl_malloc(strlen(preout)+2);
389  strncpy(out, preout, len);
390  out[len] = '\0';
391  if (!strlen(out) || out[strlen(out)-1]!='\n') {
392    strcat(out, "\n");
393  }
394
395  return(out);
396}
397
[c3acb0b]398char *owl_perlconfig_getmsg(owl_message *m, int mode, char *subname)
399{ 
[f1e629d]400  /* if mode==1 we are doing message formatting.  The returned
401   * formatted message needs to be freed by the caller.
402   *
403   * if mode==0 we are just doing the message-has-been-received
404   * thing.
405   */
406  if (!owl_global_have_config(&g)) return(NULL);
407 
408  /* run the procedure corresponding to the mode */
409  if (mode==1) {
410    char *ret = NULL;
411    ret = owl_perlconfig_call_with_message(subname?subname
[8203afd]412                                           :"BarnOwl::_format_msg_legacy_wrap", m);
[f1e629d]413    if (!ret) {
414      ret = owl_sprintf("@b([Perl Message Formatting Failed!])\n");
415    } 
416    return ret;
417  } else {
418    char *ptr = NULL;
[8203afd]419    if (owl_perlconfig_is_function("BarnOwl::Hooks::receive_msg")) {
[c0a90c2]420      ptr = owl_perlconfig_call_with_message(subname?subname
[8203afd]421                                       :"BarnOwl::_receive_msg_legacy_wrap", m);
[f1e629d]422    }
423    if (ptr) owl_free(ptr);
424    return(NULL);
425  }
426}
[6922edd]427
428char *owl_perlconfig_perlcmd(owl_cmd *cmd, int argc, char **argv)
429{
430  int i, count;
431  char * ret = NULL;
432  STRLEN n_a;
433  dSP;
434
435  ENTER;
436  SAVETMPS;
437
438  PUSHMARK(SP);
439  for(i=0;i<argc;i++) {
440    XPUSHs(sv_2mortal(newSVpv(argv[i], 0)));
441  }
442  PUTBACK;
443
444  count = call_sv(cmd->cmd_perl, G_SCALAR|G_EVAL);
445
446  SPAGAIN;
447
448  if(SvTRUE(ERRSV)) {
[a55abb3]449    owl_function_error("%s", SvPV(ERRSV, n_a));
[6922edd]450    POPs;
451  } else {
452    if(count != 1)
453      croak("Perl command %s returned more than one value!", cmd->name);
454    SV * rv = POPs;
455    if(SvTRUE(rv)) {
456      ret = owl_strdup(SvPV(rv, n_a));
457    }
458  }
459
460  FREETMPS;
461  LEAVE;
462
463  return ret;
464}
465
466void owl_perlconfig_cmd_free(owl_cmd *cmd)
467{
468  SvREFCNT_dec(cmd);
469}
[db8b00b]470
471void owl_perlconfig_edit_callback(owl_editwin *e)
472{
473  SV *cb = (SV*)(e->cbdata);
[9364a36]474  unsigned int n_a;
[db8b00b]475  if(cb == NULL) {
476    owl_function_error("Perl callback is NULL!");
477  }
478
479  dSP;
480
481  ENTER;
482  SAVETMPS;
483
484  PUSHMARK(SP);
485  XPUSHs(sv_2mortal(newSVpv(owl_editwin_get_text(e), 0)));
486  PUTBACK;
487 
[9364a36]488  call_sv(cb, G_DISCARD|G_KEEPERR|G_EVAL);
489
490  if(SvTRUE(ERRSV)) {
491    owl_function_error("%s", SvPV(ERRSV, n_a));
492  }
[db8b00b]493
494  FREETMPS;
495  LEAVE;
496
497  SvREFCNT_dec(cb);
498  e->cbdata = NULL;
499}
[f72f573]500
501void owl_perlconfig_mainloop()
502{
[8203afd]503  if (!owl_perlconfig_is_function("BarnOwl::Hooks::mainloop_hook"))
504    return;
[f72f573]505  dSP ;
506  PUSHMARK(SP) ;
[8203afd]507  call_pv("BarnOwl::Hooks::mainloop_hook", G_DISCARD|G_EVAL);
[a55abb3]508  if(SvTRUE(ERRSV)) {
509    STRLEN n_a;
510    owl_function_error("%s", SvPV(ERRSV, n_a));
511  }
[f72f573]512  return;
513}
Note: See TracBrowser for help on using the repository browser.