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 char *owl_perlwrap_codebuff; |
---|
14 | |
---|
15 | extern XS(boot_owl); |
---|
16 | |
---|
17 | static 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 | SV *owl_perlconfig_message2hashref(owl_message *m) { /*noproto*/ |
---|
26 | HV *h; |
---|
27 | SV *hr; |
---|
28 | char *ptr, *blessas; |
---|
29 | int i, j; |
---|
30 | |
---|
31 | if (!m) return &PL_sv_undef; |
---|
32 | h = newHV(); |
---|
33 | |
---|
34 | #define MSG2H(h,field) hv_store(h, #field, strlen(#field), \ |
---|
35 | newSVpv(owl_message_get_##field(m),0), 0) |
---|
36 | |
---|
37 | if (owl_message_is_type_zephyr(m) |
---|
38 | && owl_message_is_direction_in(m)) { |
---|
39 | /* Handle zephyr-specific fields... */ |
---|
40 | AV *av_zfields; |
---|
41 | |
---|
42 | av_zfields = newAV(); |
---|
43 | j=owl_zephyr_get_num_fields(owl_message_get_notice(m)); |
---|
44 | for (i=0; i<j; i++) { |
---|
45 | ptr=owl_zephyr_get_field(owl_message_get_notice(m), i+1); |
---|
46 | av_push(av_zfields, newSVpvn(ptr, strlen(ptr))); |
---|
47 | owl_free(ptr); |
---|
48 | } |
---|
49 | hv_store(h, "fields", strlen("fields"), newRV_noinc((SV*)av_zfields), 0); |
---|
50 | |
---|
51 | hv_store(h, "auth", strlen("auth"), |
---|
52 | newSVpv(owl_zephyr_get_authstr(owl_message_get_notice(m)),0),0); |
---|
53 | } |
---|
54 | |
---|
55 | MSG2H(h, type); |
---|
56 | MSG2H(h, direction); |
---|
57 | MSG2H(h, class); |
---|
58 | MSG2H(h, instance); |
---|
59 | MSG2H(h, sender); |
---|
60 | MSG2H(h, realm); |
---|
61 | MSG2H(h, recipient); |
---|
62 | MSG2H(h, opcode); |
---|
63 | MSG2H(h, hostname); |
---|
64 | MSG2H(h, body); |
---|
65 | MSG2H(h, login); |
---|
66 | MSG2H(h, zsig); |
---|
67 | MSG2H(h, zwriteline); |
---|
68 | if (owl_message_get_header(m)) { |
---|
69 | MSG2H(h, header); |
---|
70 | } |
---|
71 | hv_store(h, "time", strlen("time"), newSVpv(owl_message_get_timestr(m),0),0); |
---|
72 | hv_store(h, "id", strlen("id"), newSViv(owl_message_get_id(m)),0); |
---|
73 | hv_store(h, "deleted", strlen("deleted"), newSViv(owl_message_is_delete(m)),0); |
---|
74 | |
---|
75 | if (owl_message_is_type_zephyr(m)) blessas = "owl::Message::Zephyr"; |
---|
76 | else if (owl_message_is_type_aim(m)) blessas = "owl::Message::AIM"; |
---|
77 | else if (owl_message_is_type_admin(m)) blessas = "owl::Message::Admin"; |
---|
78 | else if (owl_message_is_type_generic(m)) blessas = "owl::Message::Generic"; |
---|
79 | else blessas = "owl::Message"; |
---|
80 | |
---|
81 | hr = sv_2mortal(newRV_noinc((SV*)h)); |
---|
82 | return sv_bless(hr, gv_stashpv(blessas,0)); |
---|
83 | } |
---|
84 | |
---|
85 | |
---|
86 | SV *owl_perlconfig_curmessage2hashref(void) { /*noproto*/ |
---|
87 | int curmsg; |
---|
88 | owl_view *v; |
---|
89 | v=owl_global_get_current_view(&g); |
---|
90 | if (owl_view_get_size(v) < 1) { |
---|
91 | return &PL_sv_undef; |
---|
92 | } |
---|
93 | curmsg=owl_global_get_curmsg(&g); |
---|
94 | return owl_perlconfig_message2hashref(owl_view_get_element(v, curmsg)); |
---|
95 | } |
---|
96 | |
---|
97 | |
---|
98 | /* Calls in a scalar context, passing it a hash reference. |
---|
99 | If return value is non-null, caller must free. */ |
---|
100 | char *owl_perlconfig_call_with_message(char *subname, owl_message *m) { |
---|
101 | dSP ; |
---|
102 | int count, len; |
---|
103 | SV *msgref, *srv; |
---|
104 | char *out, *preout; |
---|
105 | |
---|
106 | ENTER ; |
---|
107 | SAVETMPS; |
---|
108 | |
---|
109 | PUSHMARK(SP) ; |
---|
110 | msgref = owl_perlconfig_message2hashref(m); |
---|
111 | XPUSHs(msgref); |
---|
112 | PUTBACK ; |
---|
113 | |
---|
114 | count = call_pv(subname, G_SCALAR|G_EVAL|G_KEEPERR); |
---|
115 | |
---|
116 | SPAGAIN ; |
---|
117 | |
---|
118 | if (SvTRUE(ERRSV)) { |
---|
119 | STRLEN n_a; |
---|
120 | owl_function_error("Perl Error: '%s'", SvPV(ERRSV, n_a)); |
---|
121 | /* and clear the error */ |
---|
122 | sv_setsv (ERRSV, &PL_sv_undef); |
---|
123 | } |
---|
124 | |
---|
125 | if (count != 1) { |
---|
126 | fprintf(stderr, "bad perl! no biscuit! returned wrong count!\n"); |
---|
127 | abort(); |
---|
128 | } |
---|
129 | |
---|
130 | srv = POPs; |
---|
131 | |
---|
132 | if (srv) { |
---|
133 | preout=SvPV(srv, len); |
---|
134 | out = owl_malloc(strlen(preout)+1); |
---|
135 | strncpy(out, preout, len); |
---|
136 | out[len] = '\0'; |
---|
137 | } else { |
---|
138 | out = NULL; |
---|
139 | } |
---|
140 | |
---|
141 | PUTBACK ; |
---|
142 | FREETMPS ; |
---|
143 | LEAVE ; |
---|
144 | |
---|
145 | return out; |
---|
146 | } |
---|
147 | |
---|
148 | char *owl_perlconfig_readconfig(char *file) { |
---|
149 | int ret; |
---|
150 | PerlInterpreter *p; |
---|
151 | char filename[1024]; |
---|
152 | char *embedding[5]; |
---|
153 | struct stat statbuff; |
---|
154 | |
---|
155 | if (file==NULL) { |
---|
156 | sprintf(filename, "%s/%s", getenv("HOME"), ".owlconf"); |
---|
157 | } else { |
---|
158 | strcpy(filename, file); |
---|
159 | } |
---|
160 | embedding[0]=""; |
---|
161 | embedding[1]=filename; |
---|
162 | embedding[2]=0; |
---|
163 | |
---|
164 | /* create and initialize interpreter */ |
---|
165 | p=perl_alloc(); |
---|
166 | owl_global_set_perlinterp(&g, (void*)p); |
---|
167 | perl_construct(p); |
---|
168 | |
---|
169 | owl_global_set_no_have_config(&g); |
---|
170 | |
---|
171 | ret=stat(filename, &statbuff); |
---|
172 | if (ret) { |
---|
173 | return NULL; |
---|
174 | } |
---|
175 | |
---|
176 | ret=perl_parse(p, owl_perl_xs_init, 2, embedding, NULL); |
---|
177 | if (ret || SvTRUE(ERRSV)) { |
---|
178 | STRLEN n_a; |
---|
179 | char *err; |
---|
180 | err = owl_strdup(SvPV(ERRSV, n_a)); |
---|
181 | sv_setsv (ERRSV, &PL_sv_undef); /* and clear the error */ |
---|
182 | return err; |
---|
183 | } |
---|
184 | |
---|
185 | ret=perl_run(p); |
---|
186 | if (ret || SvTRUE(ERRSV)) { |
---|
187 | STRLEN n_a; |
---|
188 | char *err; |
---|
189 | err = owl_strdup(SvPV(ERRSV, n_a)); |
---|
190 | sv_setsv (ERRSV, &PL_sv_undef); /* and clear the error */ |
---|
191 | return err; |
---|
192 | } |
---|
193 | |
---|
194 | owl_global_set_have_config(&g); |
---|
195 | |
---|
196 | /* create legacy variables */ |
---|
197 | perl_get_sv("owl::id", TRUE); |
---|
198 | perl_get_sv("owl::class", TRUE); |
---|
199 | perl_get_sv("owl::instance", TRUE); |
---|
200 | perl_get_sv("owl::recipient", TRUE); |
---|
201 | perl_get_sv("owl::sender", TRUE); |
---|
202 | perl_get_sv("owl::realm", TRUE); |
---|
203 | perl_get_sv("owl::opcode", TRUE); |
---|
204 | perl_get_sv("owl::zsig", TRUE); |
---|
205 | perl_get_sv("owl::msg", TRUE); |
---|
206 | perl_get_sv("owl::time", TRUE); |
---|
207 | perl_get_sv("owl::host", TRUE); |
---|
208 | perl_get_av("owl::fields", TRUE); |
---|
209 | |
---|
210 | perl_eval_pv(owl_perlwrap_codebuff, FALSE); |
---|
211 | |
---|
212 | if (SvTRUE(ERRSV)) { |
---|
213 | STRLEN n_a; |
---|
214 | char *err; |
---|
215 | err = owl_strdup(SvPV(ERRSV, n_a)); |
---|
216 | sv_setsv (ERRSV, &PL_sv_undef); /* and clear the error */ |
---|
217 | return err; |
---|
218 | } |
---|
219 | |
---|
220 | /* check if we have the formatting function */ |
---|
221 | if (owl_perlconfig_is_function("owl::format_msg")) { |
---|
222 | owl_global_set_config_format(&g, 1); |
---|
223 | } |
---|
224 | |
---|
225 | return(NULL); |
---|
226 | } |
---|
227 | |
---|
228 | /* returns whether or not a function exists */ |
---|
229 | int owl_perlconfig_is_function(char *fn) { |
---|
230 | if (perl_get_cv(fn, FALSE)) return(1); |
---|
231 | else return(0); |
---|
232 | } |
---|
233 | |
---|
234 | /* returns 0 on success */ |
---|
235 | int owl_perlconfig_get_hashkeys(char *hashname, owl_list *l) { |
---|
236 | HV *hv; |
---|
237 | HE *he; |
---|
238 | char *key; |
---|
239 | I32 i; |
---|
240 | |
---|
241 | if (owl_list_create(l)) return(-1); |
---|
242 | hv = get_hv(hashname, FALSE); |
---|
243 | if (!hv) return(-1); |
---|
244 | i = hv_iterinit(hv); |
---|
245 | while ((he = hv_iternext(hv))) { |
---|
246 | key = hv_iterkey(he, &i); |
---|
247 | if (key) { |
---|
248 | owl_list_append_element(l, owl_strdup(key)); |
---|
249 | } |
---|
250 | } |
---|
251 | return(0); |
---|
252 | } |
---|
253 | |
---|
254 | /* caller is responsible for freeing returned string */ |
---|
255 | char *owl_perlconfig_execute(char *line) { |
---|
256 | STRLEN len; |
---|
257 | SV *response; |
---|
258 | char *out, *preout; |
---|
259 | |
---|
260 | if (!owl_global_have_config(&g)) return NULL; |
---|
261 | |
---|
262 | /* execute the subroutine */ |
---|
263 | response = perl_eval_pv(line, FALSE); |
---|
264 | |
---|
265 | if (SvTRUE(ERRSV)) { |
---|
266 | STRLEN n_a; |
---|
267 | owl_function_error("Perl Error: '%s'", SvPV(ERRSV, n_a)); |
---|
268 | sv_setsv (ERRSV, &PL_sv_undef); /* and clear the error */ |
---|
269 | } |
---|
270 | |
---|
271 | preout=SvPV(response, len); |
---|
272 | /* leave enough space in case we have to add a newline */ |
---|
273 | out = owl_malloc(strlen(preout)+2); |
---|
274 | strncpy(out, preout, len); |
---|
275 | out[len] = '\0'; |
---|
276 | if (!strlen(out) || out[strlen(out)-1]!='\n') { |
---|
277 | strcat(out, "\n"); |
---|
278 | } |
---|
279 | |
---|
280 | return(out); |
---|
281 | } |
---|
282 | |
---|
283 | char *owl_perlconfig_getmsg(owl_message *m, int mode, char *subname) { |
---|
284 | /* if mode==1 we are doing message formatting. The returned |
---|
285 | * formatted message needs to be freed by the caller. |
---|
286 | * |
---|
287 | * if mode==0 we are just doing the message-has-been-received |
---|
288 | * thing. |
---|
289 | */ |
---|
290 | if (!owl_global_have_config(&g)) return(NULL); |
---|
291 | |
---|
292 | /* run the procedure corresponding to the mode */ |
---|
293 | if (mode==1) { |
---|
294 | char *ret = NULL; |
---|
295 | ret = owl_perlconfig_call_with_message(subname?subname |
---|
296 | :"owl::_format_msg_legacy_wrap", m); |
---|
297 | if (!ret) { |
---|
298 | ret = owl_sprintf("@b([Perl Message Formatting Failed!])\n"); |
---|
299 | } |
---|
300 | return ret; |
---|
301 | } else { |
---|
302 | char *ptr = NULL; |
---|
303 | if (owl_perlconfig_is_function("owl::receive_msg")) { |
---|
304 | owl_perlconfig_call_with_message(subname?subname |
---|
305 | :"owl::_receive_msg_legacy_wrap", m); |
---|
306 | } |
---|
307 | if (ptr) owl_free(ptr); |
---|
308 | return(NULL); |
---|
309 | } |
---|
310 | } |
---|