source: perlconfig.c @ 6a5f0c3

Last change on this file since 6a5f0c3 was 6a5f0c3, checked in by Anders Kaseorg <andersk@mit.edu>, 10 years ago
owl_message_set_attribute: Take a destroy function for the value In the case of constant strings, and ZNotice_t fields other than the body, this lets us avoid wasting memory on unnecessary string copies. Signed-off-by: Anders Kaseorg <andersk@mit.edu>
  • Property mode set to 100644
File size: 14.9 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
71static void owl_perlconfig_store_attribute(GQuark key_id, gpointer data, gpointer h)
72{
73  (void)hv_store(h, g_quark_to_string(key_id), strlen(data),
74                 owl_new_sv(data), 0);
75}
76
77CALLER_OWN SV *owl_perlconfig_message2hashref(const owl_message *m)
78{
79  HV *h, *stash;
80  SV *hr;
81  const char *type;
82  char *ptr, *utype, *blessas;
83  const char *f;
84  const owl_filter *wrap;
85
86  if (!m) return &PL_sv_undef;
87  wrap = owl_global_get_filter(&g, "wordwrap");
88  if(!wrap) {
89      owl_function_error("wrap filter is not defined");
90      return &PL_sv_undef;
91  }
92
93  h = newHV();
94
95#define MSG2H(h,field) (void)hv_store(h, #field, strlen(#field),        \
96                                      owl_new_sv(owl_message_get_##field(m)), 0)
97
98  if (owl_message_is_type_zephyr(m) && owl_message_is_direction_in(m)) {
99    /* Handle zephyr-specific fields... */
100    AV *av_zfields = newAV();
101    if (owl_message_get_notice(m)) {
102      for (f = owl_zephyr_first_raw_field(owl_message_get_notice(m)); f != NULL;
103           f = owl_zephyr_next_raw_field(owl_message_get_notice(m), f)) {
104        ptr = owl_zephyr_field_as_utf8(owl_message_get_notice(m), f);
105        av_push(av_zfields, owl_new_sv(ptr));
106        g_free(ptr);
107      }
108      (void)hv_store(h, "auth", strlen("auth"),
109                     owl_new_sv(owl_zephyr_get_authstr(owl_message_get_notice(m))), 0);
110    } else {
111      /* Incoming zephyrs without a ZNotice_t are pseudo-logins. To appease
112       * existing styles, put in bogus 'auth' and 'fields' keys. */
113      (void)hv_store(h, "auth", strlen("auth"), owl_new_sv("NO"), 0);
114    }
115    (void)hv_store(h, "fields", strlen("fields"), newRV_noinc((SV*)av_zfields), 0);
116  }
117
118  g_datalist_foreach(&((owl_message *)m)->attributes, owl_perlconfig_store_attribute, h);
119 
120  MSG2H(h, type);
121  MSG2H(h, direction);
122  MSG2H(h, class);
123  MSG2H(h, instance);
124  MSG2H(h, sender);
125  MSG2H(h, realm);
126  MSG2H(h, recipient);
127  MSG2H(h, opcode);
128  MSG2H(h, hostname);
129  MSG2H(h, body);
130  MSG2H(h, login);
131  MSG2H(h, zsig);
132  MSG2H(h, zwriteline);
133  if (owl_message_get_header(m)) {
134    MSG2H(h, header); 
135  }
136  (void)hv_store(h, "time", strlen("time"), owl_new_sv(owl_message_get_timestr(m)),0);
137  (void)hv_store(h, "unix_time", strlen("unix_time"), newSViv(m->time), 0);
138  (void)hv_store(h, "id", strlen("id"), newSViv(owl_message_get_id(m)),0);
139  (void)hv_store(h, "deleted", strlen("deleted"), newSViv(owl_message_is_delete(m)),0);
140  (void)hv_store(h, "private", strlen("private"), newSViv(owl_message_is_private(m)),0);
141  (void)hv_store(h, "should_wordwrap",
142                 strlen("should_wordwrap"), newSViv(
143                                                    owl_filter_message_match(wrap, m)),0);
144
145  type = owl_message_get_type(m);
146  if(!type || !*type) type = "generic";
147  utype = g_strdup(type);
148  utype[0] = toupper(type[0]);
149  blessas = g_strdup_printf("BarnOwl::Message::%s", utype);
150
151  hr = newRV_noinc((SV*)h);
152  stash =  gv_stashpv(blessas,0);
153  if(!stash) {
154    owl_function_error("No such class: %s for message type %s", blessas, owl_message_get_type(m));
155    stash = gv_stashpv("BarnOwl::Message", 1);
156  }
157  hr = sv_bless(hr,stash);
158  g_free(utype);
159  g_free(blessas);
160  return hr;
161}
162
163CALLER_OWN SV *owl_perlconfig_curmessage2hashref(void)
164{
165  int curmsg;
166  const owl_view *v;
167  v=owl_global_get_current_view(&g);
168  if (owl_view_get_size(v) < 1) {
169    return &PL_sv_undef;
170  }
171  curmsg=owl_global_get_curmsg(&g);
172  return owl_perlconfig_message2hashref(owl_view_get_element(v, curmsg));
173}
174
175/* XXX TODO: Messages should round-trip properly between
176   message2hashref and hashref2message. Currently we lose
177   zephyr-specific properties stored in the ZNotice_t
178 */
179CALLER_OWN owl_message *owl_perlconfig_hashref2message(SV *msg)
180{
181  owl_message * m;
182  HE * ent;
183  I32 len;
184  const char *key,*val;
185  HV * hash;
186  struct tm tm;
187
188  hash = (HV*)SvRV(msg);
189
190  m = g_slice_new(owl_message);
191  owl_message_init(m);
192
193  hv_iterinit(hash);
194  while((ent = hv_iternext(hash))) {
195    key = hv_iterkey(ent, &len);
196    val = SvPV_nolen(hv_iterval(hash, ent));
197    if (!strcmp(key, "direction")) {
198      owl_message_set_direction(m, owl_message_parse_direction(val));
199    } else if(!strcmp(key, "private")) {
200      SV * v = hv_iterval(hash, ent);
201      if(SvTRUE(v)) {
202        owl_message_set_isprivate(m);
203      }
204    } else if (!strcmp(key, "hostname")) {
205      owl_message_set_hostname(m, val);
206    } else if (!strcmp(key, "time")) {
207      g_free(m->timestr);
208      m->timestr = g_strdup(val);
209      strptime(val, "%a %b %d %T %Y", &tm);
210      m->time = mktime(&tm);
211    } else {
212      owl_message_set_attribute(m, key, g_strdup(val), g_free);
213    }
214  }
215  if(owl_message_is_type_admin(m)) {
216    if(!owl_message_get_attribute_value(m, "adminheader"))
217      owl_message_set_attribute(m, "adminheader", "", NULL);
218  }
219  return m;
220}
221
222/* Calls in a scalar context, passing it a hash reference.
223   If return value is non-null, caller must free. */
224CALLER_OWN char *owl_perlconfig_call_with_message(const char *subname, const owl_message *m)
225{
226  SV *msgref, *rv;
227  char *out = NULL;
228
229  msgref = owl_perlconfig_message2hashref(m);
230
231  OWL_PERL_CALL((call_pv(subname, G_SCALAR|G_EVAL))
232                ,
233                XPUSHs(sv_2mortal(msgref));
234                ,
235                "Perl Error: '%s'"
236                ,
237                false
238                ,
239                false
240                ,
241                rv = POPs;
242                if (rv && SvPOK(rv))
243                  out = g_strdup(SvPV_nolen(rv));
244                );
245  return out;
246}
247
248
249/* Calls a method on a perl object representing a message.
250   If the return value is non-null, the caller must free it.
251 */
252CALLER_OWN char *owl_perlconfig_message_call_method(const owl_message *m, const char *method, int argc, const char **argv)
253{
254  SV *msgref, *rv;
255  char *out = NULL;
256  int i;
257
258  msgref = owl_perlconfig_message2hashref(m);
259
260  OWL_PERL_CALL(call_method(method, G_SCALAR|G_EVAL)
261                ,
262                XPUSHs(sv_2mortal(msgref));
263                OWL_PERL_PUSH_ARGS(i, argc, argv);
264                ,
265                "Perl Error: '%s'"
266                ,
267                false
268                ,
269                false
270                ,
271                rv = POPs;
272                if (rv && SvPOK(rv))
273                  out = g_strdup(SvPV_nolen(rv));
274                );
275  return out;
276}
277
278/* caller must free result, if not NULL */
279CALLER_OWN char *owl_perlconfig_initperl(const char *file, int *Pargc, char ***Pargv, char ***Penv)
280{
281  int ret;
282  PerlInterpreter *p;
283  char *err;
284  const char *args[4] = {"", "-e", "0;", NULL};
285  const char *dlerr;
286  AV *inc;
287  char *path;
288
289  /* create and initialize interpreter */
290  PERL_SYS_INIT3(Pargc, Pargv, Penv);
291  p=perl_alloc();
292  owl_global_set_perlinterp(&g, p);
293  perl_construct(p);
294
295  PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
296
297  owl_global_set_no_have_config(&g);
298
299  ret=perl_parse(p, owl_perl_xs_init, 2, (char **)args, NULL);
300  if (ret || SvTRUE(ERRSV)) {
301    err=g_strdup(SvPV_nolen(ERRSV));
302    sv_setsv(ERRSV, &PL_sv_undef);     /* and clear the error */
303    return(err);
304  }
305
306  ret=perl_run(p);
307  if (ret || SvTRUE(ERRSV)) {
308    err=g_strdup(SvPV_nolen(ERRSV));
309    sv_setsv(ERRSV, &PL_sv_undef);     /* and clear the error */
310    return(err);
311  }
312
313  owl_global_set_have_config(&g);
314
315  /* create legacy variables */
316  get_sv("BarnOwl::id", TRUE);
317  get_sv("BarnOwl::class", TRUE);
318  get_sv("BarnOwl::instance", TRUE);
319  get_sv("BarnOwl::recipient", TRUE);
320  get_sv("BarnOwl::sender", TRUE);
321  get_sv("BarnOwl::realm", TRUE);
322  get_sv("BarnOwl::opcode", TRUE);
323  get_sv("BarnOwl::zsig", TRUE);
324  get_sv("BarnOwl::msg", TRUE);
325  get_sv("BarnOwl::time", TRUE);
326  get_sv("BarnOwl::host", TRUE);
327  get_av("BarnOwl::fields", TRUE);
328
329  if(file) {
330    SV * cfg = get_sv("BarnOwl::configfile", TRUE);
331    sv_setpv(cfg, file);
332  }
333
334  sv_setpv(get_sv("BarnOwl::VERSION", TRUE), version);
335
336  /* Add the system lib path to @INC */
337  inc = get_av("INC", 0);
338  path = g_build_filename(owl_get_datadir(), "lib", NULL);
339  av_unshift(inc, 1);
340  av_store(inc, 0, owl_new_sv(path));
341  g_free(path);
342
343  /* Load up perl-Glib. */
344  eval_pv("use Glib;", FALSE);
345
346  /* Now, before BarnOwl tries to use them, get the relevant function pointers out. */
347  dlerr = owl_closure_init();
348  if (dlerr) {
349    return g_strdup(dlerr);
350  }
351
352  /* And now it's safe to import BarnOwl. */
353  eval_pv("use BarnOwl;", FALSE);
354
355  if (SvTRUE(ERRSV)) {
356    err=g_strdup(SvPV_nolen(ERRSV));
357    sv_setsv (ERRSV, &PL_sv_undef);     /* and clear the error */
358    return(err);
359  }
360
361  /* check if we have the formatting function */
362  if (owl_perlconfig_is_function("BarnOwl::format_msg")) {
363    owl_global_set_config_format(&g, 1);
364  }
365
366  return(NULL);
367}
368
369/* returns whether or not a function exists */
370int owl_perlconfig_is_function(const char *fn) {
371  if (get_cv(fn, FALSE)) return(1);
372  else return(0);
373}
374
375/* caller is responsible for freeing returned string */
376CALLER_OWN char *owl_perlconfig_execute(const char *line)
377{
378  STRLEN len;
379  SV *response;
380  char *out;
381
382  if (!owl_global_have_config(&g)) return NULL;
383
384  ENTER;
385  SAVETMPS;
386  /* execute the subroutine */
387  response = eval_pv(line, FALSE);
388
389  if (SvTRUE(ERRSV)) {
390    owl_function_error("Perl Error: '%s'", SvPV_nolen(ERRSV));
391    sv_setsv (ERRSV, &PL_sv_undef);     /* and clear the error */
392  }
393
394  out = g_strdup(SvPV(response, len));
395  FREETMPS;
396  LEAVE;
397
398  return(out);
399}
400
401void owl_perlconfig_getmsg(const owl_message *m, const char *subname)
402{
403  char *ptr = NULL;
404  if (owl_perlconfig_is_function("BarnOwl::Hooks::_receive_msg")) {
405    ptr = owl_perlconfig_call_with_message(subname?subname
406                                           :"BarnOwl::_receive_msg_legacy_wrap", m);
407  }
408  g_free(ptr);
409}
410
411/* Called on all new messages; receivemsg is only called on incoming ones */
412void owl_perlconfig_newmsg(const owl_message *m, const char *subname)
413{
414  char *ptr = NULL;
415  if (owl_perlconfig_is_function("BarnOwl::Hooks::_new_msg")) {
416    ptr = owl_perlconfig_call_with_message(subname?subname
417                                           :"BarnOwl::Hooks::_new_msg", m);
418  }
419  g_free(ptr);
420}
421
422void owl_perlconfig_new_command(const char *name)
423{
424  OWL_PERL_CALL(call_pv("BarnOwl::Hooks::_new_command", G_VOID|G_EVAL);
425                ,
426                XPUSHs(sv_2mortal(owl_new_sv(name)));
427                ,
428                "Perl Error: '%s'"
429                ,
430                false
431                ,
432                true
433                ,
434                );
435}
436
437CALLER_OWN char *owl_perlconfig_perl_call(const char *method, int argc, const char *const *argv)
438{
439  SV *rv;
440  char *out = NULL;
441  int i;
442  OWL_PERL_CALL(call_pv(method, G_SCALAR|G_EVAL)
443                ,
444                OWL_PERL_PUSH_ARGS(i, argc, argv);
445                ,
446                "Perl Error: '%s'"
447                ,
448                false
449                ,
450                false
451                ,
452                rv = POPs;
453                if (rv && SvPOK(rv))
454                  out = g_strdup(SvPV_nolen(rv));
455                );
456  return out;
457}
458
459int owl_perlconfig_perl_call_int(const char *method, int argc, const char *const *argv)
460{
461  SV *rv;
462  int ret = -1;
463  int i;
464  OWL_PERL_CALL(call_pv(method, G_SCALAR|G_EVAL)
465                ,
466                OWL_PERL_PUSH_ARGS(i, argc, argv);
467                ,
468                "Perl Error: '%s'"
469                ,
470                false
471                ,
472                false
473                ,
474                rv = POPs;
475                if (rv && SvIOK(rv))
476                  ret = SvIV(rv);
477                );
478  return ret;
479}
480
481bool owl_perlconfig_perl_call_bool(const char *method, int argc, const char *const *argv)
482{
483  SV *rv;
484  bool ret = false;
485  int i;
486  OWL_PERL_CALL(call_pv(method, G_SCALAR|G_EVAL)
487                ,
488                OWL_PERL_PUSH_ARGS(i, argc, argv);
489                ,
490                "Perl Error: '%s'"
491                ,
492                false
493                ,
494                false
495                ,
496                rv = POPs;
497                if (rv)
498                  ret = SvTRUE(rv);
499                );
500  return ret;
501}
502
503void owl_perlconfig_perl_call_norv(const char *method, int argc, const char *const *argv)
504{
505  int i;
506  OWL_PERL_CALL(call_pv(method, G_DISCARD|G_EVAL)
507                ,
508                OWL_PERL_PUSH_ARGS(i, argc, argv);
509                ,
510                "Perl Error: '%s'"
511                ,
512                false
513                ,
514                true
515                ,
516                );
517}
518
519/* caller must free the result */
520CALLER_OWN char *owl_perlconfig_perlcmd(const owl_cmd *cmd, int argc, const char *const *argv)
521{
522  int i;
523  SV* rv;
524  char *out = NULL;
525
526  OWL_PERL_CALL(call_sv(cmd->cmd_perl, G_SCALAR|G_EVAL)
527                ,
528                OWL_PERL_PUSH_ARGS(i, argc, argv);
529                ,
530                "Perl Error: '%s'"
531                ,
532                false
533                ,
534                false
535                ,
536                rv = POPs;
537                if (rv && SvPOK(rv))
538                  out = g_strdup(SvPV_nolen(rv));
539                );
540  return out;
541}
542
543void owl_perlconfig_cmd_cleanup(owl_cmd *cmd)
544{
545  SvREFCNT_dec(cmd->cmd_perl);
546}
547
548void owl_perlconfig_edit_callback(owl_editwin *e, bool success)
549{
550  SV *cb = owl_editwin_get_cbdata(e);
551  SV *text = owl_new_sv(owl_editwin_get_text(e));
552
553  if (cb == NULL) {
554    owl_function_error("Perl callback is NULL!");
555    return;
556  }
557
558  OWL_PERL_CALL(call_sv(cb, G_DISCARD|G_EVAL)
559                ,
560                XPUSHs(sv_2mortal(text));
561                XPUSHs(sv_2mortal(newSViv(success)));
562                ,
563                "Perl Error: '%s'"
564                ,
565                false
566                ,
567                true
568                ,
569                );
570}
571
572void owl_perlconfig_dec_refcnt(void *data)
573{
574  SV *v = data;
575  SvREFCNT_dec(v);
576}
Note: See TracBrowser for help on using the repository browser.