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 | |
---|
10 | static const char fileIdent[] = "$Id$"; |
---|
11 | |
---|
12 | extern char *owl_perlwrap_codebuff; |
---|
13 | |
---|
14 | extern XS(boot_owl); |
---|
15 | extern XS(boot_DynaLoader); |
---|
16 | // extern XS(boot_DBI); |
---|
17 | |
---|
18 | static 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 | |
---|
28 | SV *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 | |
---|
100 | SV *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. */ |
---|
115 | char *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 | |
---|
164 | |
---|
165 | /* Calls a method on a perl object representing a message. |
---|
166 | If the return value is non-null, the caller must free it. |
---|
167 | */ |
---|
168 | char * owl_perlconfig_message_call_method(owl_message *m, char *method, int argc, char ** argv) |
---|
169 | { |
---|
170 | dSP; |
---|
171 | unsigned int count, len, i; |
---|
172 | SV *msgref, *srv; |
---|
173 | char *out, *preout; |
---|
174 | |
---|
175 | msgref = owl_perlconfig_message2hashref(m); |
---|
176 | |
---|
177 | ENTER; |
---|
178 | SAVETMPS; |
---|
179 | |
---|
180 | PUSHMARK(SP); |
---|
181 | XPUSHs(msgref); |
---|
182 | for(i=0;i<argc;i++) { |
---|
183 | XPUSHs(sv_2mortal(newSVpv(argv[i], 0))); |
---|
184 | } |
---|
185 | PUTBACK; |
---|
186 | |
---|
187 | count = call_method(method, G_SCALAR|G_KEEPERR|G_EVAL); |
---|
188 | |
---|
189 | SPAGAIN; |
---|
190 | |
---|
191 | if(count != 1) { |
---|
192 | fprintf(stderr, "perl returned wrong count %d\n", count); |
---|
193 | abort(); |
---|
194 | } |
---|
195 | |
---|
196 | if (SvTRUE(ERRSV)) { |
---|
197 | STRLEN n_a; |
---|
198 | owl_function_error("Error: '%s'", SvPV(ERRSV, n_a)); |
---|
199 | /* and clear the error */ |
---|
200 | sv_setsv (ERRSV, &PL_sv_undef); |
---|
201 | } |
---|
202 | |
---|
203 | srv = POPs; |
---|
204 | |
---|
205 | if (srv) { |
---|
206 | preout=SvPV(srv, len); |
---|
207 | out = owl_malloc(strlen(preout)+1); |
---|
208 | strncpy(out, preout, len); |
---|
209 | out[len] = '\0'; |
---|
210 | } else { |
---|
211 | out = NULL; |
---|
212 | } |
---|
213 | |
---|
214 | PUTBACK; |
---|
215 | FREETMPS; |
---|
216 | LEAVE; |
---|
217 | |
---|
218 | return out; |
---|
219 | } |
---|
220 | |
---|
221 | |
---|
222 | char *owl_perlconfig_readconfig(void) |
---|
223 | { |
---|
224 | int ret; |
---|
225 | PerlInterpreter *p; |
---|
226 | char *err; |
---|
227 | char *args[4] = {"", "-e", "0;", NULL}; |
---|
228 | |
---|
229 | |
---|
230 | /* create and initialize interpreter */ |
---|
231 | p=perl_alloc(); |
---|
232 | owl_global_set_perlinterp(&g, (void*)p); |
---|
233 | perl_construct(p); |
---|
234 | |
---|
235 | owl_global_set_no_have_config(&g); |
---|
236 | |
---|
237 | |
---|
238 | ret=perl_parse(p, owl_perl_xs_init, 2, args, NULL); |
---|
239 | if (ret || SvTRUE(ERRSV)) { |
---|
240 | STRLEN n_a; |
---|
241 | err=owl_strdup(SvPV(ERRSV, n_a)); |
---|
242 | sv_setsv(ERRSV, &PL_sv_undef); /* and clear the error */ |
---|
243 | return(err); |
---|
244 | } |
---|
245 | |
---|
246 | ret=perl_run(p); |
---|
247 | if (ret || SvTRUE(ERRSV)) { |
---|
248 | STRLEN n_a; |
---|
249 | err=owl_strdup(SvPV(ERRSV, n_a)); |
---|
250 | sv_setsv(ERRSV, &PL_sv_undef); /* and clear the error */ |
---|
251 | return(err); |
---|
252 | } |
---|
253 | |
---|
254 | owl_global_set_have_config(&g); |
---|
255 | |
---|
256 | /* create legacy variables */ |
---|
257 | perl_get_sv("owl::id", TRUE); |
---|
258 | perl_get_sv("owl::class", TRUE); |
---|
259 | perl_get_sv("owl::instance", TRUE); |
---|
260 | perl_get_sv("owl::recipient", TRUE); |
---|
261 | perl_get_sv("owl::sender", TRUE); |
---|
262 | perl_get_sv("owl::realm", TRUE); |
---|
263 | perl_get_sv("owl::opcode", TRUE); |
---|
264 | perl_get_sv("owl::zsig", TRUE); |
---|
265 | perl_get_sv("owl::msg", TRUE); |
---|
266 | perl_get_sv("owl::time", TRUE); |
---|
267 | perl_get_sv("owl::host", TRUE); |
---|
268 | perl_get_av("owl::fields", TRUE); |
---|
269 | |
---|
270 | perl_eval_pv(owl_perlwrap_codebuff, FALSE); |
---|
271 | |
---|
272 | if (SvTRUE(ERRSV)) { |
---|
273 | STRLEN n_a; |
---|
274 | err=owl_strdup(SvPV(ERRSV, n_a)); |
---|
275 | sv_setsv (ERRSV, &PL_sv_undef); /* and clear the error */ |
---|
276 | return(err); |
---|
277 | } |
---|
278 | |
---|
279 | /* check if we have the formatting function */ |
---|
280 | if (owl_perlconfig_is_function("owl::format_msg")) { |
---|
281 | owl_global_set_config_format(&g, 1); |
---|
282 | } |
---|
283 | |
---|
284 | return(NULL); |
---|
285 | } |
---|
286 | |
---|
287 | /* returns whether or not a function exists */ |
---|
288 | int owl_perlconfig_is_function(char *fn) { |
---|
289 | if (perl_get_cv(fn, FALSE)) return(1); |
---|
290 | else return(0); |
---|
291 | } |
---|
292 | |
---|
293 | /* returns 0 on success */ |
---|
294 | int owl_perlconfig_get_hashkeys(char *hashname, owl_list *l) |
---|
295 | { |
---|
296 | HV *hv; |
---|
297 | HE *he; |
---|
298 | char *key; |
---|
299 | I32 i; |
---|
300 | |
---|
301 | if (owl_list_create(l)) return(-1); |
---|
302 | hv = get_hv(hashname, FALSE); |
---|
303 | if (!hv) return(-1); |
---|
304 | i = hv_iterinit(hv); |
---|
305 | while ((he = hv_iternext(hv))) { |
---|
306 | key = hv_iterkey(he, &i); |
---|
307 | if (key) { |
---|
308 | owl_list_append_element(l, owl_strdup(key)); |
---|
309 | } |
---|
310 | } |
---|
311 | return(0); |
---|
312 | } |
---|
313 | |
---|
314 | /* caller is responsible for freeing returned string */ |
---|
315 | char *owl_perlconfig_execute(char *line) |
---|
316 | { |
---|
317 | STRLEN len; |
---|
318 | SV *response; |
---|
319 | char *out, *preout; |
---|
320 | |
---|
321 | if (!owl_global_have_config(&g)) return NULL; |
---|
322 | |
---|
323 | /* execute the subroutine */ |
---|
324 | response = perl_eval_pv(line, FALSE); |
---|
325 | |
---|
326 | if (SvTRUE(ERRSV)) { |
---|
327 | STRLEN n_a; |
---|
328 | owl_function_error("Perl Error: '%s'", SvPV(ERRSV, n_a)); |
---|
329 | sv_setsv (ERRSV, &PL_sv_undef); /* and clear the error */ |
---|
330 | } |
---|
331 | |
---|
332 | preout=SvPV(response, len); |
---|
333 | /* leave enough space in case we have to add a newline */ |
---|
334 | out = owl_malloc(strlen(preout)+2); |
---|
335 | strncpy(out, preout, len); |
---|
336 | out[len] = '\0'; |
---|
337 | if (!strlen(out) || out[strlen(out)-1]!='\n') { |
---|
338 | strcat(out, "\n"); |
---|
339 | } |
---|
340 | |
---|
341 | return(out); |
---|
342 | } |
---|
343 | |
---|
344 | char *owl_perlconfig_getmsg(owl_message *m, int mode, char *subname) |
---|
345 | { |
---|
346 | /* if mode==1 we are doing message formatting. The returned |
---|
347 | * formatted message needs to be freed by the caller. |
---|
348 | * |
---|
349 | * if mode==0 we are just doing the message-has-been-received |
---|
350 | * thing. |
---|
351 | */ |
---|
352 | if (!owl_global_have_config(&g)) return(NULL); |
---|
353 | |
---|
354 | /* run the procedure corresponding to the mode */ |
---|
355 | if (mode==1) { |
---|
356 | char *ret = NULL; |
---|
357 | ret = owl_perlconfig_call_with_message(subname?subname |
---|
358 | :"owl::_format_msg_legacy_wrap", m); |
---|
359 | if (!ret) { |
---|
360 | ret = owl_sprintf("@b([Perl Message Formatting Failed!])\n"); |
---|
361 | } |
---|
362 | return ret; |
---|
363 | } else { |
---|
364 | char *ptr = NULL; |
---|
365 | if (owl_perlconfig_is_function("owl::receive_msg")) { |
---|
366 | ptr = owl_perlconfig_call_with_message(subname?subname |
---|
367 | :"owl::_receive_msg_legacy_wrap", m); |
---|
368 | } |
---|
369 | if (ptr) owl_free(ptr); |
---|
370 | return(NULL); |
---|
371 | } |
---|
372 | } |
---|
373 | |
---|
374 | char *owl_perlconfig_perlcmd(owl_cmd *cmd, int argc, char **argv) |
---|
375 | { |
---|
376 | int i, count; |
---|
377 | char * ret = NULL; |
---|
378 | STRLEN n_a; |
---|
379 | dSP; |
---|
380 | |
---|
381 | ENTER; |
---|
382 | SAVETMPS; |
---|
383 | |
---|
384 | PUSHMARK(SP); |
---|
385 | for(i=0;i<argc;i++) { |
---|
386 | XPUSHs(sv_2mortal(newSVpv(argv[i], 0))); |
---|
387 | } |
---|
388 | PUTBACK; |
---|
389 | |
---|
390 | count = call_sv(cmd->cmd_perl, G_SCALAR|G_EVAL); |
---|
391 | |
---|
392 | SPAGAIN; |
---|
393 | |
---|
394 | if(SvTRUE(ERRSV)) { |
---|
395 | owl_function_error("%s", SvPV(ERRSV, n_a)); |
---|
396 | POPs; |
---|
397 | } else { |
---|
398 | if(count != 1) |
---|
399 | croak("Perl command %s returned more than one value!", cmd->name); |
---|
400 | SV * rv = POPs; |
---|
401 | if(SvTRUE(rv)) { |
---|
402 | ret = owl_strdup(SvPV(rv, n_a)); |
---|
403 | } |
---|
404 | } |
---|
405 | |
---|
406 | FREETMPS; |
---|
407 | LEAVE; |
---|
408 | |
---|
409 | return ret; |
---|
410 | } |
---|
411 | |
---|
412 | void owl_perlconfig_cmd_free(owl_cmd *cmd) |
---|
413 | { |
---|
414 | SvREFCNT_dec(cmd); |
---|
415 | } |
---|
416 | |
---|
417 | void owl_perlconfig_edit_callback(owl_editwin *e) |
---|
418 | { |
---|
419 | SV *cb = (SV*)(e->cbdata); |
---|
420 | if(cb == NULL) { |
---|
421 | owl_function_error("Perl callback is NULL!"); |
---|
422 | } |
---|
423 | |
---|
424 | dSP; |
---|
425 | |
---|
426 | ENTER; |
---|
427 | SAVETMPS; |
---|
428 | |
---|
429 | PUSHMARK(SP); |
---|
430 | XPUSHs(sv_2mortal(newSVpv(owl_editwin_get_text(e), 0))); |
---|
431 | PUTBACK; |
---|
432 | |
---|
433 | call_sv(cb, G_DISCARD); |
---|
434 | |
---|
435 | FREETMPS; |
---|
436 | LEAVE; |
---|
437 | |
---|
438 | SvREFCNT_dec(cb); |
---|
439 | e->cbdata = NULL; |
---|
440 | } |
---|
441 | |
---|
442 | void owl_perlconfig_mainloop() |
---|
443 | { |
---|
444 | dSP ; |
---|
445 | PUSHMARK(SP) ; |
---|
446 | call_pv("owl::mainloop_hook", G_DISCARD|G_EVAL); |
---|
447 | if(SvTRUE(ERRSV)) { |
---|
448 | STRLEN n_a; |
---|
449 | owl_function_error("%s", SvPV(ERRSV, n_a)); |
---|
450 | } |
---|
451 | return; |
---|
452 | } |
---|