source: readconfig.c @ e7cc1c3

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