source: perlconfig.c @ 421c8ef7

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