source: perlconfig.c @ d9f4a5c

barnowl_perlaimdebianrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since d9f4a5c was a55abb3, checked in by Nelson Elhage <nelhage@mit.edu>, 17 years ago
Handling errors in the perl mainloop hook better
  • Property mode set to 100644
File size: 8.9 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
164char *owl_perlconfig_readconfig(void)
165{
166  int ret;
167  PerlInterpreter *p;
168  char *err;
169  char *args[4] = {"", "-e", "0;", NULL};
170
171
172  /* create and initialize interpreter */
173  p=perl_alloc();
174  owl_global_set_perlinterp(&g, (void*)p);
175  perl_construct(p);
176
177  owl_global_set_no_have_config(&g);
178
179
180  ret=perl_parse(p, owl_perl_xs_init, 2, args, NULL);
181  if (ret || SvTRUE(ERRSV)) {
182    STRLEN n_a;
183    err=owl_strdup(SvPV(ERRSV, n_a));
184    sv_setsv(ERRSV, &PL_sv_undef);     /* and clear the error */
185    return(err);
186  }
187
188  ret=perl_run(p);
189  if (ret || SvTRUE(ERRSV)) {
190    STRLEN n_a;
191    err=owl_strdup(SvPV(ERRSV, n_a));
192    sv_setsv(ERRSV, &PL_sv_undef);     /* and clear the error */
193    return(err);
194  }
195
196  owl_global_set_have_config(&g);
197
198  /* create legacy variables */
199  perl_get_sv("owl::id", TRUE);
200  perl_get_sv("owl::class", TRUE);
201  perl_get_sv("owl::instance", TRUE);
202  perl_get_sv("owl::recipient", TRUE);
203  perl_get_sv("owl::sender", TRUE);
204  perl_get_sv("owl::realm", TRUE);
205  perl_get_sv("owl::opcode", TRUE);
206  perl_get_sv("owl::zsig", TRUE);
207  perl_get_sv("owl::msg", TRUE);
208  perl_get_sv("owl::time", TRUE);
209  perl_get_sv("owl::host", TRUE);
210  perl_get_av("owl::fields", TRUE);
211 
212  perl_eval_pv(owl_perlwrap_codebuff, FALSE);
213
214  if (SvTRUE(ERRSV)) {
215    STRLEN n_a;
216    err=owl_strdup(SvPV(ERRSV, n_a));
217    sv_setsv (ERRSV, &PL_sv_undef);     /* and clear the error */
218    return(err);
219  }
220
221  /* check if we have the formatting function */
222  if (owl_perlconfig_is_function("owl::format_msg")) {
223    owl_global_set_config_format(&g, 1);
224  }
225
226  return(NULL);
227}
228
229/* returns whether or not a function exists */
230int owl_perlconfig_is_function(char *fn) {
231  if (perl_get_cv(fn, FALSE)) return(1);
232  else return(0);
233}
234
235/* returns 0 on success */
236int owl_perlconfig_get_hashkeys(char *hashname, owl_list *l)
237{
238  HV *hv;
239  HE *he;
240  char *key;
241  I32 i;
242
243  if (owl_list_create(l)) return(-1);
244  hv = get_hv(hashname, FALSE);
245  if (!hv) return(-1);
246  i = hv_iterinit(hv);
247  while ((he = hv_iternext(hv))) {
248    key = hv_iterkey(he, &i);
249    if (key) {
250      owl_list_append_element(l, owl_strdup(key));
251    }
252  }
253  return(0);
254}
255
256/* caller is responsible for freeing returned string */
257char *owl_perlconfig_execute(char *line)
258{
259  STRLEN len;
260  SV *response;
261  char *out, *preout;
262
263  if (!owl_global_have_config(&g)) return NULL;
264
265  /* execute the subroutine */
266  response = perl_eval_pv(line, FALSE);
267
268  if (SvTRUE(ERRSV)) {
269    STRLEN n_a;
270    owl_function_error("Perl Error: '%s'", SvPV(ERRSV, n_a));
271    sv_setsv (ERRSV, &PL_sv_undef);     /* and clear the error */
272  }
273
274  preout=SvPV(response, len);
275  /* leave enough space in case we have to add a newline */
276  out = owl_malloc(strlen(preout)+2);
277  strncpy(out, preout, len);
278  out[len] = '\0';
279  if (!strlen(out) || out[strlen(out)-1]!='\n') {
280    strcat(out, "\n");
281  }
282
283  return(out);
284}
285
286char *owl_perlconfig_getmsg(owl_message *m, int mode, char *subname)
287{ 
288  /* if mode==1 we are doing message formatting.  The returned
289   * formatted message needs to be freed by the caller.
290   *
291   * if mode==0 we are just doing the message-has-been-received
292   * thing.
293   */
294  if (!owl_global_have_config(&g)) return(NULL);
295 
296  /* run the procedure corresponding to the mode */
297  if (mode==1) {
298    char *ret = NULL;
299    ret = owl_perlconfig_call_with_message(subname?subname
300                                           :"owl::_format_msg_legacy_wrap", m);
301    if (!ret) {
302      ret = owl_sprintf("@b([Perl Message Formatting Failed!])\n");
303    } 
304    return ret;
305  } else {
306    char *ptr = NULL;
307    if (owl_perlconfig_is_function("owl::receive_msg")) {
308      ptr = owl_perlconfig_call_with_message(subname?subname
309                                       :"owl::_receive_msg_legacy_wrap", m);
310    }
311    if (ptr) owl_free(ptr);
312    return(NULL);
313  }
314}
315
316char *owl_perlconfig_perlcmd(owl_cmd *cmd, int argc, char **argv)
317{
318  int i, count;
319  char * ret = NULL;
320  STRLEN n_a;
321  dSP;
322
323  ENTER;
324  SAVETMPS;
325
326  PUSHMARK(SP);
327  for(i=0;i<argc;i++) {
328    XPUSHs(sv_2mortal(newSVpv(argv[i], 0)));
329  }
330  PUTBACK;
331
332  count = call_sv(cmd->cmd_perl, G_SCALAR|G_EVAL);
333
334  SPAGAIN;
335
336  if(SvTRUE(ERRSV)) {
337    owl_function_error("%s", SvPV(ERRSV, n_a));
338    POPs;
339  } else {
340    if(count != 1)
341      croak("Perl command %s returned more than one value!", cmd->name);
342    SV * rv = POPs;
343    if(SvTRUE(rv)) {
344      ret = owl_strdup(SvPV(rv, n_a));
345    }
346  }
347
348  FREETMPS;
349  LEAVE;
350
351  return ret;
352}
353
354void owl_perlconfig_cmd_free(owl_cmd *cmd)
355{
356  SvREFCNT_dec(cmd);
357}
358
359void owl_perlconfig_edit_callback(owl_editwin *e)
360{
361  SV *cb = (SV*)(e->cbdata);
362  if(cb == NULL) {
363    owl_function_error("Perl callback is NULL!");
364  }
365
366  dSP;
367
368  ENTER;
369  SAVETMPS;
370
371  PUSHMARK(SP);
372  XPUSHs(sv_2mortal(newSVpv(owl_editwin_get_text(e), 0)));
373  PUTBACK;
374 
375  call_sv(cb, G_DISCARD);
376
377  FREETMPS;
378  LEAVE;
379
380  SvREFCNT_dec(cb);
381  e->cbdata = NULL;
382}
383
384void owl_perlconfig_mainloop()
385{
386  dSP ;
387  PUSHMARK(SP) ;
388  call_pv("owl::mainloop_hook", G_DISCARD|G_EVAL);
389  if(SvTRUE(ERRSV)) {
390    STRLEN n_a;
391    owl_function_error("%s", SvPV(ERRSV, n_a));
392  }
393  return;
394}
Note: See TracBrowser for help on using the repository browser.