source: perlconfig.c @ f271129

release-1.10release-1.9
Last change on this file since f271129 was f271129, checked in by Jason Gross <jgross@mit.edu>, 13 years ago
Fix up headers The additions to owl.h and some of the removals were done by Alejandro Sedeño <asedeno@mit.edu> in commit 77a0258b3919468fc9d7f7602588ac427ab36e6c. Notes: * I think owl.c lost the need for sys/time.h when we punted select() in favor of glib's main loop. * We don't actually need to include things like stdarg.h, stdio.h, glib/gstdio.h, glib-object.h. I think they get indirectly included via owl.h and/or glib.h. They're left in (or added in to) the files that use functions/types from them. * I'm not entirely sure what sys/socket.h is doing in message.c. It is there from the initial commit. I suspect it might have had something to do with the call to getnameinfo. message.c compiles without it, but http://pubs.opengroup.org/onlinepubs/009695399/functions/getnameinfo.html suggests that we're supposed to include it? *shrugs* I'm leaving it in, for now. (Rather, I'll leave one copy of the #include in.)
  • Property mode set to 100644
File size: 13.5 KB
Line 
1#define OWL_PERL
2#include "owl.h"
3#include <stdio.h>
4
5extern XS(boot_BarnOwl);
6extern XS(boot_DynaLoader);
7/* extern XS(boot_DBI); */
8
9void owl_perl_xs_init(pTHX) /* noproto */
10{
11  const char *file = __FILE__;
12  dXSUB_SYS;
13  {
14    newXS("BarnOwl::bootstrap", boot_BarnOwl, file);
15    newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
16  }
17}
18
19
20CALLER_OWN SV *owl_new_sv(const char * str)
21{
22  SV *ret = newSVpv(str, 0);
23  if (is_utf8_string((const U8 *)str, strlen(str))) {
24    SvUTF8_on(ret);
25  } else {
26    char *escape = owl_escape_highbit(str);
27    owl_function_error("Internal error! Non-UTF-8 string encountered:\n%s", escape);
28    g_free(escape);
29  }
30  return ret;
31}
32
33CALLER_OWN AV *owl_new_av(const GPtrArray *l, SV *(*to_sv)(const void *))
34{
35  AV *ret;
36  int i;
37  void *element;
38
39  ret = newAV();
40
41  for (i = 0; i < l->len; i++) {
42    element = l->pdata[i];
43    av_push(ret, to_sv(element));
44  }
45
46  return ret;
47}
48
49CALLER_OWN HV *owl_new_hv(const owl_dict *d, SV *(*to_sv)(const void *))
50{
51  HV *ret;
52  GPtrArray *keys;
53  const char *key;
54  void *element;
55  int i;
56
57  ret = newHV();
58
59  /* TODO: add an iterator-like interface to owl_dict */
60  keys = owl_dict_get_keys(d);
61  for (i = 0; i < keys->len; i++) {
62    key = keys->pdata[i];
63    element = owl_dict_find_element(d, key);
64    (void)hv_store(ret, key, strlen(key), to_sv(element), 0);
65  }
66  owl_ptr_array_free(keys, g_free);
67
68  return ret;
69}
70
71CALLER_OWN SV *owl_perlconfig_message2hashref(const owl_message *m)
72{
73  HV *h, *stash;
74  SV *hr;
75  const char *type;
76  char *ptr, *utype, *blessas;
77  int i, j;
78  const owl_pair *pair;
79  const owl_filter *wrap;
80
81  if (!m) return &PL_sv_undef;
82  wrap = owl_global_get_filter(&g, "wordwrap");
83  if(!wrap) {
84      owl_function_error("wrap filter is not defined");
85      return &PL_sv_undef;
86  }
87
88  h = newHV();
89
90#define MSG2H(h,field) (void)hv_store(h, #field, strlen(#field),        \
91                                      owl_new_sv(owl_message_get_##field(m)), 0)
92
93  if (owl_message_is_type_zephyr(m)
94      && owl_message_is_direction_in(m)) {
95    /* Handle zephyr-specific fields... */
96    AV *av_zfields;
97
98    av_zfields = newAV();
99    j=owl_zephyr_get_num_fields(owl_message_get_notice(m));
100    for (i=0; i<j; i++) {
101      ptr=owl_zephyr_get_field_as_utf8(owl_message_get_notice(m), i+1);
102      av_push(av_zfields, owl_new_sv(ptr));
103      g_free(ptr);
104    }
105    (void)hv_store(h, "fields", strlen("fields"), newRV_noinc((SV*)av_zfields), 0);
106
107    (void)hv_store(h, "auth", strlen("auth"), 
108                   owl_new_sv(owl_zephyr_get_authstr(owl_message_get_notice(m))),0);
109  }
110
111  for (i = 0; i < m->attributes->len; i++) {
112    pair = m->attributes->pdata[i];
113    (void)hv_store(h, owl_pair_get_key(pair), strlen(owl_pair_get_key(pair)),
114                   owl_new_sv(owl_pair_get_value(pair)),0);
115  }
116 
117  MSG2H(h, type);
118  MSG2H(h, direction);
119  MSG2H(h, class);
120  MSG2H(h, instance);
121  MSG2H(h, sender);
122  MSG2H(h, realm);
123  MSG2H(h, recipient);
124  MSG2H(h, opcode);
125  MSG2H(h, hostname);
126  MSG2H(h, body);
127  MSG2H(h, login);
128  MSG2H(h, zsig);
129  MSG2H(h, zwriteline);
130  if (owl_message_get_header(m)) {
131    MSG2H(h, header); 
132  }
133  (void)hv_store(h, "time", strlen("time"), owl_new_sv(owl_message_get_timestr(m)),0);
134  (void)hv_store(h, "unix_time", strlen("unix_time"), newSViv(m->time), 0);
135  (void)hv_store(h, "id", strlen("id"), newSViv(owl_message_get_id(m)),0);
136  (void)hv_store(h, "deleted", strlen("deleted"), newSViv(owl_message_is_delete(m)),0);
137  (void)hv_store(h, "private", strlen("private"), newSViv(owl_message_is_private(m)),0);
138  (void)hv_store(h, "should_wordwrap",
139                 strlen("should_wordwrap"), newSViv(
140                                                    owl_filter_message_match(wrap, m)),0);
141
142  type = owl_message_get_type(m);
143  if(!type || !*type) type = "generic";
144  utype = g_strdup(type);
145  utype[0] = toupper(type[0]);
146  blessas = g_strdup_printf("BarnOwl::Message::%s", utype);
147
148  hr = newRV_noinc((SV*)h);
149  stash =  gv_stashpv(blessas,0);
150  if(!stash) {
151    owl_function_error("No such class: %s for message type %s", blessas, owl_message_get_type(m));
152    stash = gv_stashpv("BarnOwl::Message", 1);
153  }
154  hr = sv_bless(hr,stash);
155  g_free(utype);
156  g_free(blessas);
157  return hr;
158}
159
160CALLER_OWN SV *owl_perlconfig_curmessage2hashref(void)
161{
162  int curmsg;
163  const owl_view *v;
164  v=owl_global_get_current_view(&g);
165  if (owl_view_get_size(v) < 1) {
166    return &PL_sv_undef;
167  }
168  curmsg=owl_global_get_curmsg(&g);
169  return owl_perlconfig_message2hashref(owl_view_get_element(v, curmsg));
170}
171
172/* XXX TODO: Messages should round-trip properly between
173   message2hashref and hashref2message. Currently we lose
174   zephyr-specific properties stored in the ZNotice_t
175
176   This has been somewhat addressed, but is still not lossless.
177 */
178CALLER_OWN owl_message *owl_perlconfig_hashref2message(SV *msg)
179{
180  owl_message * m;
181  HE * ent;
182  I32 len;
183  const char *key,*val;
184  HV * hash;
185  struct tm tm;
186
187  hash = (HV*)SvRV(msg);
188
189  m = g_new(owl_message, 1);
190  owl_message_init(m);
191
192  hv_iterinit(hash);
193  while((ent = hv_iternext(hash))) {
194    key = hv_iterkey(ent, &len);
195    val = SvPV_nolen(hv_iterval(hash, ent));
196    if(!strcmp(key, "type")) {
197      owl_message_set_type(m, val);
198    } else if(!strcmp(key, "direction")) {
199      owl_message_set_direction(m, owl_message_parse_direction(val));
200    } else if(!strcmp(key, "private")) {
201      SV * v = hv_iterval(hash, ent);
202      if(SvTRUE(v)) {
203        owl_message_set_isprivate(m);
204      }
205    } else if (!strcmp(key, "hostname")) {
206      owl_message_set_hostname(m, val);
207    } else if (!strcmp(key, "zwriteline")) {
208      owl_message_set_zwriteline(m, val);
209    } else if (!strcmp(key, "time")) {
210      g_free(m->timestr);
211      m->timestr = g_strdup(val);
212      strptime(val, "%a %b %d %T %Y", &tm);
213      m->time = mktime(&tm);
214    } else {
215      owl_message_set_attribute(m, key, val);
216    }
217  }
218  if(owl_message_is_type_admin(m)) {
219    if(!owl_message_get_attribute_value(m, "adminheader"))
220      owl_message_set_attribute(m, "adminheader", "");
221  }
222#ifdef HAVE_LIBZEPHYR
223  if (owl_message_is_type_zephyr(m)) {
224    ZNotice_t *n = &(m->notice);
225    n->z_kind = ACKED;
226    n->z_port = 0;
227    n->z_auth = ZAUTH_NO;
228    n->z_checked_auth = 0;
229    n->z_class = zstr(owl_message_get_class(m));
230    n->z_class_inst = zstr(owl_message_get_instance(m));
231    n->z_opcode = zstr(owl_message_get_opcode(m));
232    n->z_sender = zstr(owl_message_get_sender(m));
233    n->z_recipient = zstr(owl_message_get_recipient(m));
234    n->z_default_format = zstr("[zephyr created from perl]");
235    n->z_multinotice = zstr("[zephyr created from perl]");
236    n->z_num_other_fields = 0;
237    n->z_message = g_strdup_printf("%s%c%s", owl_message_get_zsig(m), '\0', owl_message_get_body(m));
238    n->z_message_len = strlen(owl_message_get_zsig(m)) + strlen(owl_message_get_body(m)) + 1;
239  }
240#endif
241  return m;
242}
243
244/* Calls in a scalar context, passing it a hash reference.
245   If return value is non-null, caller must free. */
246CALLER_OWN char *owl_perlconfig_call_with_message(const char *subname, const owl_message *m)
247{
248  dSP ;
249  int count;
250  SV *msgref, *srv;
251  char *out;
252 
253  ENTER ;
254  SAVETMPS;
255 
256  PUSHMARK(SP) ;
257  msgref = owl_perlconfig_message2hashref(m);
258  XPUSHs(sv_2mortal(msgref));
259  PUTBACK ;
260 
261  count = call_pv(subname, G_SCALAR|G_EVAL);
262 
263  SPAGAIN ;
264
265  if (SvTRUE(ERRSV)) {
266    owl_function_error("Perl Error: '%s'", SvPV_nolen(ERRSV));
267    /* and clear the error */
268    sv_setsv (ERRSV, &PL_sv_undef);
269  }
270
271  if (count != 1) {
272    fprintf(stderr, "bad perl!  no biscuit!  returned wrong count!\n");
273    abort();
274  }
275
276  srv = POPs;
277
278  if (srv) {
279    out = g_strdup(SvPV_nolen(srv));
280  } else {
281    out = NULL;
282  }
283 
284  PUTBACK ;
285  FREETMPS ;
286  LEAVE ;
287
288  return out;
289}
290
291
292/* Calls a method on a perl object representing a message.
293   If the return value is non-null, the caller must free it.
294 */
295CALLER_OWN char *owl_perlconfig_message_call_method(const owl_message *m, const char *method, int argc, const char **argv)
296{
297  dSP;
298  unsigned int count, i;
299  SV *msgref, *srv;
300  char *out;
301
302  msgref = owl_perlconfig_message2hashref(m);
303
304  ENTER;
305  SAVETMPS;
306
307  PUSHMARK(SP);
308  XPUSHs(sv_2mortal(msgref));
309  for(i=0;i<argc;i++) {
310    XPUSHs(sv_2mortal(owl_new_sv(argv[i])));
311  }
312  PUTBACK;
313
314  count = call_method(method, G_SCALAR|G_EVAL);
315
316  SPAGAIN;
317
318  if(count != 1) {
319    fprintf(stderr, "perl returned wrong count %u\n", count);
320    abort();
321  }
322
323  if (SvTRUE(ERRSV)) {
324    owl_function_error("Error: '%s'", SvPV_nolen(ERRSV));
325    /* and clear the error */
326    sv_setsv (ERRSV, &PL_sv_undef);
327  }
328
329  srv = POPs;
330
331  if (srv) {
332    out = g_strdup(SvPV_nolen(srv));
333  } else {
334    out = NULL;
335  }
336
337  PUTBACK;
338  FREETMPS;
339  LEAVE;
340
341  return out;
342}
343
344/* caller must free result, if not NULL */
345CALLER_OWN char *owl_perlconfig_initperl(const char *file, int *Pargc, char ***Pargv, char ***Penv)
346{
347  int ret;
348  PerlInterpreter *p;
349  char *err;
350  const char *args[4] = {"", "-e", "0;", NULL};
351  AV *inc;
352  char *path;
353
354  /* create and initialize interpreter */
355  PERL_SYS_INIT3(Pargc, Pargv, Penv);
356  p=perl_alloc();
357  owl_global_set_perlinterp(&g, p);
358  perl_construct(p);
359
360  PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
361
362  owl_global_set_no_have_config(&g);
363
364  ret=perl_parse(p, owl_perl_xs_init, 2, (char **)args, NULL);
365  if (ret || SvTRUE(ERRSV)) {
366    err=g_strdup(SvPV_nolen(ERRSV));
367    sv_setsv(ERRSV, &PL_sv_undef);     /* and clear the error */
368    return(err);
369  }
370
371  ret=perl_run(p);
372  if (ret || SvTRUE(ERRSV)) {
373    err=g_strdup(SvPV_nolen(ERRSV));
374    sv_setsv(ERRSV, &PL_sv_undef);     /* and clear the error */
375    return(err);
376  }
377
378  owl_global_set_have_config(&g);
379
380  /* create legacy variables */
381  get_sv("BarnOwl::id", TRUE);
382  get_sv("BarnOwl::class", TRUE);
383  get_sv("BarnOwl::instance", TRUE);
384  get_sv("BarnOwl::recipient", TRUE);
385  get_sv("BarnOwl::sender", TRUE);
386  get_sv("BarnOwl::realm", TRUE);
387  get_sv("BarnOwl::opcode", TRUE);
388  get_sv("BarnOwl::zsig", TRUE);
389  get_sv("BarnOwl::msg", TRUE);
390  get_sv("BarnOwl::time", TRUE);
391  get_sv("BarnOwl::host", TRUE);
392  get_av("BarnOwl::fields", TRUE);
393
394  if(file) {
395    SV * cfg = get_sv("BarnOwl::configfile", TRUE);
396    sv_setpv(cfg, file);
397  }
398
399  sv_setpv(get_sv("BarnOwl::VERSION", TRUE), OWL_VERSION_STRING);
400
401  /* Add the system lib path to @INC */
402  inc = get_av("INC", 0);
403  path = g_build_filename(owl_get_datadir(), "lib", NULL);
404  av_unshift(inc, 1);
405  av_store(inc, 0, owl_new_sv(path));
406  g_free(path);
407
408  eval_pv("use BarnOwl;", FALSE);
409
410  if (SvTRUE(ERRSV)) {
411    err=g_strdup(SvPV_nolen(ERRSV));
412    sv_setsv (ERRSV, &PL_sv_undef);     /* and clear the error */
413    return(err);
414  }
415
416  /* check if we have the formatting function */
417  if (owl_perlconfig_is_function("BarnOwl::format_msg")) {
418    owl_global_set_config_format(&g, 1);
419  }
420
421  return(NULL);
422}
423
424/* returns whether or not a function exists */
425int owl_perlconfig_is_function(const char *fn) {
426  if (get_cv(fn, FALSE)) return(1);
427  else return(0);
428}
429
430/* caller is responsible for freeing returned string */
431CALLER_OWN char *owl_perlconfig_execute(const char *line)
432{
433  STRLEN len;
434  SV *response;
435  char *out;
436
437  if (!owl_global_have_config(&g)) return NULL;
438
439  ENTER;
440  SAVETMPS;
441  /* execute the subroutine */
442  response = eval_pv(line, FALSE);
443
444  if (SvTRUE(ERRSV)) {
445    owl_function_error("Perl Error: '%s'", SvPV_nolen(ERRSV));
446    sv_setsv (ERRSV, &PL_sv_undef);     /* and clear the error */
447  }
448
449  out = g_strdup(SvPV(response, len));
450  FREETMPS;
451  LEAVE;
452
453  return(out);
454}
455
456void owl_perlconfig_getmsg(const owl_message *m, const char *subname)
457{
458  char *ptr = NULL;
459  if (owl_perlconfig_is_function("BarnOwl::Hooks::_receive_msg")) {
460    ptr = owl_perlconfig_call_with_message(subname?subname
461                                           :"BarnOwl::_receive_msg_legacy_wrap", m);
462  }
463  g_free(ptr);
464}
465
466/* Called on all new messages; receivemsg is only called on incoming ones */
467void owl_perlconfig_newmsg(const owl_message *m, const char *subname)
468{
469  char *ptr = NULL;
470  if (owl_perlconfig_is_function("BarnOwl::Hooks::_new_msg")) {
471    ptr = owl_perlconfig_call_with_message(subname?subname
472                                           :"BarnOwl::Hooks::_new_msg", m);
473  }
474  g_free(ptr);
475}
476
477void owl_perlconfig_new_command(const char *name)
478{
479  dSP;
480
481  ENTER;
482  SAVETMPS;
483
484  PUSHMARK(SP);
485  XPUSHs(sv_2mortal(owl_new_sv(name)));
486  PUTBACK;
487
488  call_pv("BarnOwl::Hooks::_new_command", G_VOID|G_EVAL);
489
490  SPAGAIN;
491
492  if(SvTRUE(ERRSV)) {
493    owl_function_error("%s", SvPV_nolen(ERRSV));
494  }
495
496  FREETMPS;
497  LEAVE;
498}
499
500/* caller must free the result */
501CALLER_OWN char *owl_perlconfig_perlcmd(const owl_cmd *cmd, int argc, const char *const *argv)
502{
503  int i, count;
504  char * ret = NULL;
505  SV *rv;
506  dSP;
507
508  ENTER;
509  SAVETMPS;
510
511  PUSHMARK(SP);
512  for(i=0;i<argc;i++) {
513    XPUSHs(sv_2mortal(owl_new_sv(argv[i])));
514  }
515  PUTBACK;
516
517  count = call_sv(cmd->cmd_perl, G_SCALAR|G_EVAL);
518
519  SPAGAIN;
520
521  if(SvTRUE(ERRSV)) {
522    owl_function_error("%s", SvPV_nolen(ERRSV));
523    (void)POPs;
524  } else {
525    if(count != 1)
526      croak("Perl command %s returned more than one value!", cmd->name);
527    rv = POPs;
528    if(SvTRUE(rv)) {
529      ret = g_strdup(SvPV_nolen(rv));
530    }
531  }
532
533  FREETMPS;
534  LEAVE;
535
536  return ret;
537}
538
539void owl_perlconfig_cmd_cleanup(owl_cmd *cmd)
540{
541  SvREFCNT_dec(cmd->cmd_perl);
542}
543
544void owl_perlconfig_edit_callback(owl_editwin *e)
545{
546  SV *cb = owl_editwin_get_cbdata(e);
547  SV *text;
548  dSP;
549
550  if(cb == NULL) {
551    owl_function_error("Perl callback is NULL!");
552    return;
553  }
554  text = owl_new_sv(owl_editwin_get_text(e));
555
556  ENTER;
557  SAVETMPS;
558
559  PUSHMARK(SP);
560  XPUSHs(sv_2mortal(text));
561  PUTBACK;
562 
563  call_sv(cb, G_DISCARD|G_EVAL);
564
565  if(SvTRUE(ERRSV)) {
566    owl_function_error("%s", SvPV_nolen(ERRSV));
567  }
568
569  FREETMPS;
570  LEAVE;
571}
572
573void owl_perlconfig_dec_refcnt(void *data)
574{
575  SV *v = data;
576  SvREFCNT_dec(v);
577}
Note: See TracBrowser for help on using the repository browser.