source: readconfig.c @ 2c8d62e

barnowl_perlaimdebianowlrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 2c8d62e was b45293f, checked in by James M. Kretchmar <kretch@mit.edu>, 22 years ago
Outgoing messages now go through the config for formatting
  • Property mode set to 100644
File size: 6.4 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#include "owl.h"
8#include <perl.h>
9#include "XSUB.h"
10
11static const char fileIdent[] = "$Id$";
12
13
14
15extern XS(boot_owl);
16
17static void owl_perl_xs_init(pTHX) {
18  char *file = __FILE__;
19  dXSUB_SYS;
20  {
21    newXS("owl::bootstrap", boot_owl, file);
22  }
23}
24
25
26int owl_readconfig(char *file) {
27  int ret;
28  PerlInterpreter *p;
29  char *codebuff, filename[1024];
30  char *embedding[5];
31  struct stat statbuff;
32
33  if (file==NULL) {
34    sprintf(filename, "%s/%s", getenv("HOME"), ".owlconf");
35  } else {
36    strcpy(filename, file);
37  }
38  embedding[0]="";
39  embedding[1]=filename;
40  embedding[2]=0;
41
42  /* create and initialize interpreter */
43  p=perl_alloc();
44  owl_global_set_perlinterp(&g, (void*)p);
45  perl_construct(p);
46
47  owl_global_set_no_have_config(&g);
48
49  ret=stat(filename, &statbuff);
50  if (ret) {
51    return(0);
52  }
53
54  ret=perl_parse(p, owl_perl_xs_init, 2, embedding, NULL);
55  if (ret) return(-1);
56
57  ret=perl_run(p);
58  if (ret) return(-1);
59
60  owl_global_set_have_config(&g);
61
62  /* create variables */
63  perl_get_sv("owl::id", TRUE);
64  perl_get_sv("owl::type", TRUE);
65  perl_get_sv("owl::direction", TRUE);
66  perl_get_sv("owl::class", TRUE);
67  perl_get_sv("owl::instance", TRUE);
68  perl_get_sv("owl::recipient", TRUE);
69  perl_get_sv("owl::sender", TRUE);
70  perl_get_sv("owl::realm", TRUE);
71  perl_get_sv("owl::opcode", TRUE);
72  perl_get_sv("owl::zsig", TRUE);
73  perl_get_sv("owl::msg", TRUE);
74  perl_get_sv("owl::time", TRUE);
75  perl_get_sv("owl::host", TRUE);
76  perl_get_av("owl::fields", TRUE);
77 
78  /* perl bootstrapping code */
79  codebuff = 
80    "                                             \n"
81    "package owl;                                 \n"
82    "                                             \n"
83    "bootstrap owl 0.01;                          \n"
84    "                                             \n"
85    "package main;                                \n";
86
87  perl_eval_pv(codebuff, FALSE);
88
89
90  /* check if we have the formatting function */
91  if (perl_get_cv("owl::format_msg", FALSE)) {
92    owl_global_set_config_format(&g, 1);
93  }
94  return(0);
95}
96
97
98/* caller is responsible for freeing returned string */
99char *owl_config_execute(char *line) {
100  STRLEN len;
101  SV *response;
102  char *out, *preout;
103
104  if (!owl_global_have_config(&g)) return NULL;
105
106  /* execute the subroutine */
107  response = perl_eval_pv(line, FALSE);
108
109  preout=SvPV(response, len);
110  /* leave enough space in case we have to add a newline */
111  out = owl_malloc(strlen(preout)+2);
112  strncpy(out, preout, len);
113  out[len] = '\0';
114  if (!strlen(out) || out[strlen(out)-1]!='\n') {
115    strcat(out, "\n");
116  }
117
118  return(out);
119}
120
121char *owl_config_getmsg(owl_message *m, int mode) {
122  /* if mode==1 we are doing message formatting.  The returned
123   * formatted message needs to be freed by the caller.
124   *
125   * if mode==0 we are just doing the message-has-been-received
126   * thing.
127  */
128
129  int i, j, len;
130  char *ptr, *ptr2;
131
132  if (!owl_global_have_config(&g)) return("");
133
134  /* set owl::msg */
135  ptr=owl_message_get_body(m);
136  len=strlen(ptr);
137  ptr2=owl_malloc(len+20);
138  memcpy(ptr2, ptr, len);
139  ptr2[len]='\0';
140  if (ptr2[len-1]!='\n') {
141    strcat(ptr2, "\n");
142  }
143  sv_setpv(perl_get_sv("owl::msg", TRUE), ptr2);
144  owl_free(ptr2);
145
146  /* set owl::zsig */
147  ptr=owl_message_get_zsig(m);
148  len=strlen(ptr);
149  if (len>0) {
150    ptr2=owl_malloc(len+20);
151    memcpy(ptr2, ptr, len);
152    ptr2[len]='\0';
153    if (ptr2[len-1]=='\n') {  /* do we really need this? */
154      ptr2[len-1]='\0';
155    }
156    sv_setpv(perl_get_sv("owl::zsig", TRUE), ptr2);
157    owl_free(ptr2);
158  } else {
159    sv_setpv(perl_get_sv("owl::zsig", TRUE), "");
160  }
161
162  /* set owl::type */
163  if (owl_message_is_type_zephyr(m)) {
164    sv_setpv(perl_get_sv("owl::type", TRUE), "zephyr");
165  } else if (owl_message_is_type_admin(m)) {
166    sv_setpv(perl_get_sv("owl::type", TRUE), "admin");
167  } else {
168    sv_setpv(perl_get_sv("owl::type", TRUE), "unknown");
169  }
170
171  /* set owl::direction */
172  if (owl_message_is_direction_in(m)) {
173    sv_setpv(perl_get_sv("owl::direction", TRUE), "in");
174  } else if (owl_message_is_direction_out(m)) {
175    sv_setpv(perl_get_sv("owl::direction", TRUE), "out");
176  } else if (owl_message_is_direction_none(m)) {
177    sv_setpv(perl_get_sv("owl::direction", TRUE), "none");
178  } else {
179    sv_setpv(perl_get_sv("owl::direction", TRUE), "unknown");
180  }
181
182  /* set everything else */
183  sv_setpv(perl_get_sv("owl::class", TRUE), owl_message_get_class(m));
184  sv_setpv(perl_get_sv("owl::instance", TRUE), owl_message_get_instance(m));
185  sv_setpv(perl_get_sv("owl::sender", TRUE), owl_message_get_sender(m));
186  sv_setpv(perl_get_sv("owl::realm", TRUE), owl_message_get_realm(m));
187  sv_setpv(perl_get_sv("owl::recipient", TRUE), owl_message_get_recipient(m));
188  sv_setpv(perl_get_sv("owl::opcode", TRUE), owl_message_get_opcode(m));
189  sv_setpv(perl_get_sv("owl::time", TRUE), owl_message_get_timestr(m));
190  sv_setpv(perl_get_sv("owl::host", TRUE), owl_message_get_hostname(m));
191  sv_setiv(perl_get_sv("owl::id", TRUE), owl_message_get_id(m));
192
193  /* free old @fields ? */
194  /* I don't think I need to do this, but ask marc to make sure */
195  /*
196  j=av_len(perl_get_av("fields", TRUE));
197  for (i=0; i<j; i++) {
198    tmpsv=av_pop(perl_get_av("fields", TRUE));
199    SvREFCNT_dec(tmpsv);
200  }
201  */
202
203  /* set owl::fields */
204  if (owl_message_is_type_zephyr(m) && owl_message_is_direction_in(m)) {
205    av_clear(perl_get_av("owl::fields", TRUE));
206    j=owl_zephyr_get_num_fields(owl_message_get_notice(m));
207    for (i=0; i<j; i++) {
208      ptr=owl_zephyr_get_field(owl_message_get_notice(m), i+1, &len);
209      ptr2=owl_malloc(len+10);
210      memcpy(ptr2, ptr, len);
211      ptr2[len]='\0';
212      av_push(perl_get_av("owl::fields", TRUE), newSVpvn(ptr2, len));
213      owl_free(ptr2);
214    }
215  }
216
217  /* for backwards compatibilty, because I'm an idiot */
218  if (owl_message_is_type_zephyr(m) && owl_message_is_direction_in(m)) {
219    av_clear(perl_get_av("fields", TRUE));
220    j=owl_zephyr_get_num_fields(owl_message_get_notice(m));
221    for (i=0; i<j; i++) {
222      ptr=owl_zephyr_get_field(owl_message_get_notice(m), i+1, &len);
223      ptr2=owl_malloc(len+10);
224      memcpy(ptr2, ptr, len);
225      ptr2[len]='\0';
226      av_push(perl_get_av("fields", TRUE), newSVpvn(ptr2, len));
227      owl_free(ptr2);
228    }
229  }
230
231  /* run the procedure corresponding to the mode */
232  if (mode==1) {
233    return(owl_config_execute("owl::format_msg();"));
234  } else {
235    ptr=owl_config_execute("owl::receive_msg();");
236    if (ptr) owl_free(ptr);
237    return(NULL);
238  }
239}
240
Note: See TracBrowser for help on using the repository browser.