source: perlconfig.c @ 9364a36

barnowl_perlaimdebianrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 9364a36 was 9364a36, checked in by Nelson Elhage <nelhage@mit.edu>, 17 years ago
Catch errors in perl edit callbacks
  • Property mode set to 100644
File size: 10.1 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_owl);
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("owl::bootstrap", boot_owl, file);
24    newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
25  }
26}
27
28SV *owl_perlconfig_message2hashref(owl_message *m)  /*noproto*/
29{
30  HV *h;
31  SV *hr;
32  char *ptr, *blessas;
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  if (owl_message_is_type_zephyr(m))       blessas = "owl::Message::Zephyr";
89  else if (owl_message_is_type_aim(m))     blessas = "owl::Message::AIM";
90  else if (owl_message_is_type_jabber(m))  blessas = "owl::Message::Jabber";
91  else if (owl_message_is_type_admin(m))   blessas = "owl::Message::Admin";
92  else if (owl_message_is_type_generic(m)) blessas = "owl::Message::Generic";
93  else                                     blessas = "owl::Message";
94
95  hr = sv_2mortal(newRV_noinc((SV*)h));
96  return sv_bless(hr, gv_stashpv(blessas,0));
97}
98
99
100SV *owl_perlconfig_curmessage2hashref(void) /*noproto*/
101{
102  int curmsg;
103  owl_view *v;
104  v=owl_global_get_current_view(&g);
105  if (owl_view_get_size(v) < 1) {
106    return &PL_sv_undef;
107  }
108  curmsg=owl_global_get_curmsg(&g);
109  return owl_perlconfig_message2hashref(owl_view_get_element(v, curmsg));
110}
111
112
113/* Calls in a scalar context, passing it a hash reference.
114   If return value is non-null, caller must free. */
115char *owl_perlconfig_call_with_message(char *subname, owl_message *m)
116{
117  dSP ;
118  int count, len;
119  SV *msgref, *srv;
120  char *out, *preout;
121 
122  ENTER ;
123  SAVETMPS;
124 
125  PUSHMARK(SP) ;
126  msgref = owl_perlconfig_message2hashref(m);
127  XPUSHs(msgref);
128  PUTBACK ;
129 
130  count = call_pv(subname, G_SCALAR|G_EVAL|G_KEEPERR);
131 
132  SPAGAIN ;
133
134  if (SvTRUE(ERRSV)) {
135    STRLEN n_a;
136    owl_function_error("Perl Error: '%s'", SvPV(ERRSV, n_a));
137    /* and clear the error */
138    sv_setsv (ERRSV, &PL_sv_undef);
139  }
140
141  if (count != 1) {
142    fprintf(stderr, "bad perl!  no biscuit!  returned wrong count!\n");
143    abort();
144  }
145
146  srv = POPs;
147
148  if (srv) {
149    preout=SvPV(srv, len);
150    out = owl_malloc(strlen(preout)+1);
151    strncpy(out, preout, len);
152    out[len] = '\0';
153  } else {
154    out = NULL;
155  }
156 
157  PUTBACK ;
158  FREETMPS ;
159  LEAVE ;
160
161  return out;
162}
163
164
165/* Calls a method on a perl object representing a message.
166   If the return value is non-null, the caller must free it.
167 */
168char * owl_perlconfig_message_call_method(owl_message *m, char *method, int argc, char ** argv)
169{
170  dSP;
171  unsigned int count, len, i;
172  SV *msgref, *srv;
173  char *out, *preout;
174
175  msgref = owl_perlconfig_message2hashref(m);
176
177  ENTER;
178  SAVETMPS;
179
180  PUSHMARK(SP);
181  XPUSHs(msgref);
182  for(i=0;i<argc;i++) {
183    XPUSHs(sv_2mortal(newSVpv(argv[i], 0)));
184  }
185  PUTBACK;
186
187  count = call_method(method, G_SCALAR|G_KEEPERR|G_EVAL);
188
189  SPAGAIN;
190
191  if(count != 1) {
192    fprintf(stderr, "perl returned wrong count %d\n", count);
193    abort();
194  }
195
196  if (SvTRUE(ERRSV)) {
197    STRLEN n_a;
198    owl_function_error("Error: '%s'", SvPV(ERRSV, n_a));
199    /* and clear the error */
200    sv_setsv (ERRSV, &PL_sv_undef);
201  }
202
203  srv = POPs;
204
205  if (srv) {
206    preout=SvPV(srv, len);
207    out = owl_malloc(strlen(preout)+1);
208    strncpy(out, preout, len);
209    out[len] = '\0';
210  } else {
211    out = NULL;
212  }
213
214  PUTBACK;
215  FREETMPS;
216  LEAVE;
217
218  return out;
219}
220
221
222char *owl_perlconfig_readconfig(char * file)
223{
224  int ret;
225  PerlInterpreter *p;
226  char *err;
227  char *args[4] = {"", "-e", "0;", NULL};
228
229
230  /* create and initialize interpreter */
231  p=perl_alloc();
232  owl_global_set_perlinterp(&g, (void*)p);
233  perl_construct(p);
234
235  owl_global_set_no_have_config(&g);
236
237
238  ret=perl_parse(p, owl_perl_xs_init, 2, args, NULL);
239  if (ret || SvTRUE(ERRSV)) {
240    STRLEN n_a;
241    err=owl_strdup(SvPV(ERRSV, n_a));
242    sv_setsv(ERRSV, &PL_sv_undef);     /* and clear the error */
243    return(err);
244  }
245
246  ret=perl_run(p);
247  if (ret || SvTRUE(ERRSV)) {
248    STRLEN n_a;
249    err=owl_strdup(SvPV(ERRSV, n_a));
250    sv_setsv(ERRSV, &PL_sv_undef);     /* and clear the error */
251    return(err);
252  }
253
254  owl_global_set_have_config(&g);
255
256  /* create legacy variables */
257  perl_get_sv("owl::id", TRUE);
258  perl_get_sv("owl::class", TRUE);
259  perl_get_sv("owl::instance", TRUE);
260  perl_get_sv("owl::recipient", TRUE);
261  perl_get_sv("owl::sender", TRUE);
262  perl_get_sv("owl::realm", TRUE);
263  perl_get_sv("owl::opcode", TRUE);
264  perl_get_sv("owl::zsig", TRUE);
265  perl_get_sv("owl::msg", TRUE);
266  perl_get_sv("owl::time", TRUE);
267  perl_get_sv("owl::host", TRUE);
268  perl_get_av("owl::fields", TRUE);
269
270  if(file) {
271    SV * cfg = get_sv("owl::configfile", TRUE);
272    sv_setpv(cfg, file);
273  }
274
275  perl_eval_pv(owl_perlwrap_codebuff, FALSE);
276
277  if (SvTRUE(ERRSV)) {
278    STRLEN n_a;
279    err=owl_strdup(SvPV(ERRSV, n_a));
280    sv_setsv (ERRSV, &PL_sv_undef);     /* and clear the error */
281    return(err);
282  }
283
284  /* check if we have the formatting function */
285  if (owl_perlconfig_is_function("owl::format_msg")) {
286    owl_global_set_config_format(&g, 1);
287  }
288
289  return(NULL);
290}
291
292/* returns whether or not a function exists */
293int owl_perlconfig_is_function(char *fn) {
294  if (perl_get_cv(fn, FALSE)) return(1);
295  else return(0);
296}
297
298/* returns 0 on success */
299int owl_perlconfig_get_hashkeys(char *hashname, owl_list *l)
300{
301  HV *hv;
302  HE *he;
303  char *key;
304  I32 i;
305
306  if (owl_list_create(l)) return(-1);
307  hv = get_hv(hashname, FALSE);
308  if (!hv) return(-1);
309  i = hv_iterinit(hv);
310  while ((he = hv_iternext(hv))) {
311    key = hv_iterkey(he, &i);
312    if (key) {
313      owl_list_append_element(l, owl_strdup(key));
314    }
315  }
316  return(0);
317}
318
319/* caller is responsible for freeing returned string */
320char *owl_perlconfig_execute(char *line)
321{
322  STRLEN len;
323  SV *response;
324  char *out, *preout;
325
326  if (!owl_global_have_config(&g)) return NULL;
327
328  /* execute the subroutine */
329  response = perl_eval_pv(line, FALSE);
330
331  if (SvTRUE(ERRSV)) {
332    STRLEN n_a;
333    owl_function_error("Perl Error: '%s'", SvPV(ERRSV, n_a));
334    sv_setsv (ERRSV, &PL_sv_undef);     /* and clear the error */
335  }
336
337  preout=SvPV(response, len);
338  /* leave enough space in case we have to add a newline */
339  out = owl_malloc(strlen(preout)+2);
340  strncpy(out, preout, len);
341  out[len] = '\0';
342  if (!strlen(out) || out[strlen(out)-1]!='\n') {
343    strcat(out, "\n");
344  }
345
346  return(out);
347}
348
349char *owl_perlconfig_getmsg(owl_message *m, int mode, char *subname)
350{ 
351  /* if mode==1 we are doing message formatting.  The returned
352   * formatted message needs to be freed by the caller.
353   *
354   * if mode==0 we are just doing the message-has-been-received
355   * thing.
356   */
357  if (!owl_global_have_config(&g)) return(NULL);
358 
359  /* run the procedure corresponding to the mode */
360  if (mode==1) {
361    char *ret = NULL;
362    ret = owl_perlconfig_call_with_message(subname?subname
363                                           :"owl::_format_msg_legacy_wrap", m);
364    if (!ret) {
365      ret = owl_sprintf("@b([Perl Message Formatting Failed!])\n");
366    } 
367    return ret;
368  } else {
369    char *ptr = NULL;
370    if (owl_perlconfig_is_function("owl::receive_msg")) {
371      ptr = owl_perlconfig_call_with_message(subname?subname
372                                       :"owl::_receive_msg_legacy_wrap", m);
373    }
374    if (ptr) owl_free(ptr);
375    return(NULL);
376  }
377}
378
379char *owl_perlconfig_perlcmd(owl_cmd *cmd, int argc, char **argv)
380{
381  int i, count;
382  char * ret = NULL;
383  STRLEN n_a;
384  dSP;
385
386  ENTER;
387  SAVETMPS;
388
389  PUSHMARK(SP);
390  for(i=0;i<argc;i++) {
391    XPUSHs(sv_2mortal(newSVpv(argv[i], 0)));
392  }
393  PUTBACK;
394
395  count = call_sv(cmd->cmd_perl, G_SCALAR|G_EVAL);
396
397  SPAGAIN;
398
399  if(SvTRUE(ERRSV)) {
400    owl_function_error("%s", SvPV(ERRSV, n_a));
401    POPs;
402  } else {
403    if(count != 1)
404      croak("Perl command %s returned more than one value!", cmd->name);
405    SV * rv = POPs;
406    if(SvTRUE(rv)) {
407      ret = owl_strdup(SvPV(rv, n_a));
408    }
409  }
410
411  FREETMPS;
412  LEAVE;
413
414  return ret;
415}
416
417void owl_perlconfig_cmd_free(owl_cmd *cmd)
418{
419  SvREFCNT_dec(cmd);
420}
421
422void owl_perlconfig_edit_callback(owl_editwin *e)
423{
424  SV *cb = (SV*)(e->cbdata);
425  unsigned int n_a;
426  if(cb == NULL) {
427    owl_function_error("Perl callback is NULL!");
428  }
429
430  dSP;
431
432  ENTER;
433  SAVETMPS;
434
435  PUSHMARK(SP);
436  XPUSHs(sv_2mortal(newSVpv(owl_editwin_get_text(e), 0)));
437  PUTBACK;
438 
439  call_sv(cb, G_DISCARD|G_KEEPERR|G_EVAL);
440
441  if(SvTRUE(ERRSV)) {
442    owl_function_error("%s", SvPV(ERRSV, n_a));
443  }
444
445  FREETMPS;
446  LEAVE;
447
448  SvREFCNT_dec(cb);
449  e->cbdata = NULL;
450}
451
452void owl_perlconfig_mainloop()
453{
454  dSP ;
455  PUSHMARK(SP) ;
456  call_pv("owl::mainloop_hook", G_DISCARD|G_EVAL);
457  if(SvTRUE(ERRSV)) {
458    STRLEN n_a;
459    owl_function_error("%s", SvPV(ERRSV, n_a));
460  }
461  return;
462}
Note: See TracBrowser for help on using the repository browser.