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 | |
---|
11 | static const char fileIdent[] = "$Id$"; |
---|
12 | |
---|
13 | extern XS(boot_owl); |
---|
14 | |
---|
15 | static 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 | |
---|
24 | int 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_av("owl::fields", TRUE); |
---|
89 | |
---|
90 | /* perl bootstrapping code */ |
---|
91 | codebuff = |
---|
92 | " \n" |
---|
93 | "package owl; \n" |
---|
94 | " \n" |
---|
95 | "bootstrap owl 0.01; \n" |
---|
96 | " \n" |
---|
97 | "package main; \n"; |
---|
98 | |
---|
99 | perl_eval_pv(codebuff, FALSE); |
---|
100 | |
---|
101 | |
---|
102 | /* check if we have the formatting function */ |
---|
103 | if (perl_get_cv("owl::format_msg", FALSE)) { |
---|
104 | owl_global_set_config_format(&g, 1); |
---|
105 | } |
---|
106 | return(0); |
---|
107 | } |
---|
108 | |
---|
109 | |
---|
110 | /* caller is responsible for freeing returned string */ |
---|
111 | char *owl_config_execute(char *line) { |
---|
112 | STRLEN len; |
---|
113 | SV *response; |
---|
114 | char *out, *preout; |
---|
115 | |
---|
116 | if (!owl_global_have_config(&g)) return NULL; |
---|
117 | |
---|
118 | /* execute the subroutine */ |
---|
119 | response = perl_eval_pv(line, FALSE); |
---|
120 | |
---|
121 | preout=SvPV(response, len); |
---|
122 | /* leave enough space in case we have to add a newline */ |
---|
123 | out = owl_malloc(strlen(preout)+2); |
---|
124 | strncpy(out, preout, len); |
---|
125 | out[len] = '\0'; |
---|
126 | if (!strlen(out) || out[strlen(out)-1]!='\n') { |
---|
127 | strcat(out, "\n"); |
---|
128 | } |
---|
129 | |
---|
130 | return(out); |
---|
131 | } |
---|
132 | |
---|
133 | char *owl_config_getmsg(owl_message *m, int mode) { |
---|
134 | /* if mode==1 we are doing message formatting. The returned |
---|
135 | * formatted message needs to be freed by the caller. |
---|
136 | * |
---|
137 | * if mode==0 we are just doing the message-has-been-received |
---|
138 | * thing. |
---|
139 | */ |
---|
140 | |
---|
141 | int i, j, len; |
---|
142 | char *ptr, *ptr2; |
---|
143 | |
---|
144 | if (!owl_global_have_config(&g)) return(""); |
---|
145 | |
---|
146 | /* set owl::msg */ |
---|
147 | ptr=owl_message_get_body(m); |
---|
148 | len=strlen(ptr); |
---|
149 | ptr2=owl_malloc(len+20); |
---|
150 | memcpy(ptr2, ptr, len); |
---|
151 | ptr2[len]='\0'; |
---|
152 | if (ptr2[len-1]!='\n') { |
---|
153 | strcat(ptr2, "\n"); |
---|
154 | } |
---|
155 | sv_setpv(perl_get_sv("owl::msg", TRUE), ptr2); |
---|
156 | owl_free(ptr2); |
---|
157 | |
---|
158 | /* set owl::zsig */ |
---|
159 | ptr=owl_message_get_zsig(m); |
---|
160 | len=strlen(ptr); |
---|
161 | if (len>0) { |
---|
162 | ptr2=owl_malloc(len+20); |
---|
163 | memcpy(ptr2, ptr, len); |
---|
164 | ptr2[len]='\0'; |
---|
165 | if (ptr2[len-1]=='\n') { /* do we really need this? */ |
---|
166 | ptr2[len-1]='\0'; |
---|
167 | } |
---|
168 | sv_setpv(perl_get_sv("owl::zsig", TRUE), ptr2); |
---|
169 | owl_free(ptr2); |
---|
170 | } else { |
---|
171 | sv_setpv(perl_get_sv("owl::zsig", TRUE), ""); |
---|
172 | } |
---|
173 | |
---|
174 | /* set owl::type */ |
---|
175 | if (owl_message_is_type_zephyr(m)) { |
---|
176 | sv_setpv(perl_get_sv("owl::type", TRUE), "zephyr"); |
---|
177 | } else if (owl_message_is_type_admin(m)) { |
---|
178 | sv_setpv(perl_get_sv("owl::type", TRUE), "admin"); |
---|
179 | } else { |
---|
180 | sv_setpv(perl_get_sv("owl::type", TRUE), "unknown"); |
---|
181 | } |
---|
182 | |
---|
183 | /* set owl::direction */ |
---|
184 | if (owl_message_is_direction_in(m)) { |
---|
185 | sv_setpv(perl_get_sv("owl::direction", TRUE), "in"); |
---|
186 | } else if (owl_message_is_direction_out(m)) { |
---|
187 | sv_setpv(perl_get_sv("owl::direction", TRUE), "out"); |
---|
188 | } else if (owl_message_is_direction_none(m)) { |
---|
189 | sv_setpv(perl_get_sv("owl::direction", TRUE), "none"); |
---|
190 | } else { |
---|
191 | sv_setpv(perl_get_sv("owl::direction", TRUE), "unknown"); |
---|
192 | } |
---|
193 | |
---|
194 | /* set everything else */ |
---|
195 | sv_setpv(perl_get_sv("owl::class", TRUE), owl_message_get_class(m)); |
---|
196 | sv_setpv(perl_get_sv("owl::instance", TRUE), owl_message_get_instance(m)); |
---|
197 | sv_setpv(perl_get_sv("owl::sender", TRUE), owl_message_get_sender(m)); |
---|
198 | sv_setpv(perl_get_sv("owl::realm", TRUE), owl_message_get_realm(m)); |
---|
199 | sv_setpv(perl_get_sv("owl::recipient", TRUE), owl_message_get_recipient(m)); |
---|
200 | sv_setpv(perl_get_sv("owl::opcode", TRUE), owl_message_get_opcode(m)); |
---|
201 | sv_setpv(perl_get_sv("owl::time", TRUE), owl_message_get_timestr(m)); |
---|
202 | sv_setpv(perl_get_sv("owl::host", TRUE), owl_message_get_hostname(m)); |
---|
203 | sv_setiv(perl_get_sv("owl::id", TRUE), owl_message_get_id(m)); |
---|
204 | |
---|
205 | /* free old @fields ? */ |
---|
206 | /* I don't think I need to do this, but ask marc to make sure */ |
---|
207 | /* |
---|
208 | j=av_len(perl_get_av("fields", TRUE)); |
---|
209 | for (i=0; i<j; i++) { |
---|
210 | tmpsv=av_pop(perl_get_av("fields", TRUE)); |
---|
211 | SvREFCNT_dec(tmpsv); |
---|
212 | } |
---|
213 | */ |
---|
214 | |
---|
215 | /* set owl::fields */ |
---|
216 | if (owl_message_is_type_zephyr(m) && owl_message_is_direction_in(m)) { |
---|
217 | av_clear(perl_get_av("owl::fields", TRUE)); |
---|
218 | j=owl_zephyr_get_num_fields(owl_message_get_notice(m)); |
---|
219 | for (i=0; i<j; i++) { |
---|
220 | ptr=owl_zephyr_get_field(owl_message_get_notice(m), i+1, &len); |
---|
221 | ptr2=owl_malloc(len+10); |
---|
222 | memcpy(ptr2, ptr, len); |
---|
223 | ptr2[len]='\0'; |
---|
224 | av_push(perl_get_av("owl::fields", TRUE), newSVpvn(ptr2, len)); |
---|
225 | owl_free(ptr2); |
---|
226 | } |
---|
227 | } |
---|
228 | |
---|
229 | /* for backwards compatibilty, because I'm an idiot */ |
---|
230 | if (owl_message_is_type_zephyr(m) && owl_message_is_direction_in(m)) { |
---|
231 | av_clear(perl_get_av("fields", TRUE)); |
---|
232 | j=owl_zephyr_get_num_fields(owl_message_get_notice(m)); |
---|
233 | for (i=0; i<j; i++) { |
---|
234 | ptr=owl_zephyr_get_field(owl_message_get_notice(m), i+1, &len); |
---|
235 | ptr2=owl_malloc(len+10); |
---|
236 | memcpy(ptr2, ptr, len); |
---|
237 | ptr2[len]='\0'; |
---|
238 | av_push(perl_get_av("fields", TRUE), newSVpvn(ptr2, len)); |
---|
239 | owl_free(ptr2); |
---|
240 | } |
---|
241 | } |
---|
242 | |
---|
243 | /* run the procedure corresponding to the mode */ |
---|
244 | if (mode==1) { |
---|
245 | return(owl_config_execute("owl::format_msg();")); |
---|
246 | } else { |
---|
247 | ptr=owl_config_execute("owl::receive_msg();"); |
---|
248 | if (ptr) owl_free(ptr); |
---|
249 | return(NULL); |
---|
250 | } |
---|
251 | } |
---|
252 | |
---|