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_BarnOwl); |
---|
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("BarnOwl::bootstrap", boot_BarnOwl, file); |
---|
24 | newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file); |
---|
25 | } |
---|
26 | } |
---|
27 | |
---|
28 | SV *owl_perlconfig_message2hashref(owl_message *m) |
---|
29 | { |
---|
30 | HV *h; |
---|
31 | SV *hr; |
---|
32 | char *ptr, *blessas, *type; |
---|
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 | type = owl_message_get_type(m); |
---|
89 | if(!type) type = "generic"; |
---|
90 | type = owl_strdup(type); |
---|
91 | type[0] = toupper(type[0]); |
---|
92 | blessas = owl_sprintf("BarnOwl::Message::%s", type); |
---|
93 | |
---|
94 | hr = sv_2mortal(newRV_noinc((SV*)h)); |
---|
95 | hr = sv_bless(hr, gv_stashpv(blessas,0)); |
---|
96 | owl_free(type); |
---|
97 | owl_free(blessas); |
---|
98 | return hr; |
---|
99 | } |
---|
100 | |
---|
101 | SV *owl_perlconfig_curmessage2hashref(void) /*noproto*/ |
---|
102 | { |
---|
103 | int curmsg; |
---|
104 | owl_view *v; |
---|
105 | v=owl_global_get_current_view(&g); |
---|
106 | if (owl_view_get_size(v) < 1) { |
---|
107 | return &PL_sv_undef; |
---|
108 | } |
---|
109 | curmsg=owl_global_get_curmsg(&g); |
---|
110 | return owl_perlconfig_message2hashref(owl_view_get_element(v, curmsg)); |
---|
111 | } |
---|
112 | |
---|
113 | /* XXX TODO: Messages should round-trip properly between |
---|
114 | message2hashref and hashref2message. Currently we lose |
---|
115 | zephyr-specific properties stored in the ZNotice_t |
---|
116 | */ |
---|
117 | owl_message * owl_perlconfig_hashref2message(SV *msg) |
---|
118 | { |
---|
119 | owl_message * m; |
---|
120 | HE * ent; |
---|
121 | I32 count, len; |
---|
122 | char *key,*val; |
---|
123 | HV * hash; |
---|
124 | |
---|
125 | hash = (HV*)SvRV(msg); |
---|
126 | |
---|
127 | m = owl_malloc(sizeof(owl_message)); |
---|
128 | owl_message_init(m); |
---|
129 | |
---|
130 | count = hv_iterinit(hash); |
---|
131 | while((ent = hv_iternext(hash))) { |
---|
132 | key = hv_iterkey(ent, &len); |
---|
133 | val = SvPV_nolen(hv_iterval(hash, ent)); |
---|
134 | if(!strcmp(key, "type")) { |
---|
135 | owl_message_set_type(m, val); |
---|
136 | } else if(!strcmp(key, "direction")) { |
---|
137 | owl_message_set_direction(m, owl_message_parse_direction(val)); |
---|
138 | } else if(!strcmp(key, "private")) { |
---|
139 | SV * v = hv_iterval(hash, ent); |
---|
140 | if(SvTRUE(v)) { |
---|
141 | owl_message_set_isprivate(m); |
---|
142 | } |
---|
143 | } else if (!strcmp(key, "hostname")) { |
---|
144 | owl_message_set_hostname(m, val); |
---|
145 | } else if (!strcmp(key, "zwriteline")) { |
---|
146 | owl_message_set_zwriteline(m, val); |
---|
147 | } else if (!strcmp(key, "time")) { |
---|
148 | m->timestr = owl_strdup(val); |
---|
149 | struct tm tm; |
---|
150 | strptime(val, "%a %b %d %T %Y", &tm); |
---|
151 | m->time = mktime(&tm); |
---|
152 | } else { |
---|
153 | owl_message_set_attribute(m, key, val); |
---|
154 | } |
---|
155 | } |
---|
156 | if(owl_message_is_type_admin(m)) { |
---|
157 | if(!owl_message_get_attribute_value(m, "adminheader")) |
---|
158 | owl_message_set_attribute(m, "adminheader", ""); |
---|
159 | } |
---|
160 | return m; |
---|
161 | } |
---|
162 | |
---|
163 | /* Calls in a scalar context, passing it a hash reference. |
---|
164 | If return value is non-null, caller must free. */ |
---|
165 | char *owl_perlconfig_call_with_message(char *subname, owl_message *m) |
---|
166 | { |
---|
167 | dSP ; |
---|
168 | int count, len; |
---|
169 | SV *msgref, *srv; |
---|
170 | char *out, *preout; |
---|
171 | |
---|
172 | ENTER ; |
---|
173 | SAVETMPS; |
---|
174 | |
---|
175 | PUSHMARK(SP) ; |
---|
176 | msgref = owl_perlconfig_message2hashref(m); |
---|
177 | XPUSHs(msgref); |
---|
178 | PUTBACK ; |
---|
179 | |
---|
180 | count = call_pv(subname, G_SCALAR|G_EVAL|G_KEEPERR); |
---|
181 | |
---|
182 | SPAGAIN ; |
---|
183 | |
---|
184 | if (SvTRUE(ERRSV)) { |
---|
185 | STRLEN n_a; |
---|
186 | owl_function_error("Perl Error: '%s'", SvPV(ERRSV, n_a)); |
---|
187 | /* and clear the error */ |
---|
188 | sv_setsv (ERRSV, &PL_sv_undef); |
---|
189 | } |
---|
190 | |
---|
191 | if (count != 1) { |
---|
192 | fprintf(stderr, "bad perl! no biscuit! returned wrong count!\n"); |
---|
193 | abort(); |
---|
194 | } |
---|
195 | |
---|
196 | srv = POPs; |
---|
197 | |
---|
198 | if (srv) { |
---|
199 | preout=SvPV(srv, len); |
---|
200 | out = owl_malloc(strlen(preout)+1); |
---|
201 | strncpy(out, preout, len); |
---|
202 | out[len] = '\0'; |
---|
203 | } else { |
---|
204 | out = NULL; |
---|
205 | } |
---|
206 | |
---|
207 | PUTBACK ; |
---|
208 | FREETMPS ; |
---|
209 | LEAVE ; |
---|
210 | |
---|
211 | return out; |
---|
212 | } |
---|
213 | |
---|
214 | |
---|
215 | /* Calls a method on a perl object representing a message. |
---|
216 | If the return value is non-null, the caller must free it. |
---|
217 | */ |
---|
218 | char * owl_perlconfig_message_call_method(owl_message *m, char *method, int argc, char ** argv) |
---|
219 | { |
---|
220 | dSP; |
---|
221 | unsigned int count, len, i; |
---|
222 | SV *msgref, *srv; |
---|
223 | char *out, *preout; |
---|
224 | |
---|
225 | msgref = owl_perlconfig_message2hashref(m); |
---|
226 | |
---|
227 | ENTER; |
---|
228 | SAVETMPS; |
---|
229 | |
---|
230 | PUSHMARK(SP); |
---|
231 | XPUSHs(msgref); |
---|
232 | for(i=0;i<argc;i++) { |
---|
233 | XPUSHs(sv_2mortal(newSVpv(argv[i], 0))); |
---|
234 | } |
---|
235 | PUTBACK; |
---|
236 | |
---|
237 | count = call_method(method, G_SCALAR|G_KEEPERR|G_EVAL); |
---|
238 | |
---|
239 | SPAGAIN; |
---|
240 | |
---|
241 | if(count != 1) { |
---|
242 | fprintf(stderr, "perl returned wrong count %d\n", count); |
---|
243 | abort(); |
---|
244 | } |
---|
245 | |
---|
246 | if (SvTRUE(ERRSV)) { |
---|
247 | STRLEN n_a; |
---|
248 | owl_function_error("Error: '%s'", SvPV(ERRSV, n_a)); |
---|
249 | /* and clear the error */ |
---|
250 | sv_setsv (ERRSV, &PL_sv_undef); |
---|
251 | } |
---|
252 | |
---|
253 | srv = POPs; |
---|
254 | |
---|
255 | if (srv) { |
---|
256 | preout=SvPV(srv, len); |
---|
257 | out = owl_malloc(strlen(preout)+1); |
---|
258 | strncpy(out, preout, len); |
---|
259 | out[len] = '\0'; |
---|
260 | } else { |
---|
261 | out = NULL; |
---|
262 | } |
---|
263 | |
---|
264 | PUTBACK; |
---|
265 | FREETMPS; |
---|
266 | LEAVE; |
---|
267 | |
---|
268 | return out; |
---|
269 | } |
---|
270 | |
---|
271 | |
---|
272 | char *owl_perlconfig_readconfig(char * file) |
---|
273 | { |
---|
274 | int ret; |
---|
275 | PerlInterpreter *p; |
---|
276 | char *err; |
---|
277 | char *args[4] = {"", "-e", "0;", NULL}; |
---|
278 | |
---|
279 | |
---|
280 | /* create and initialize interpreter */ |
---|
281 | p=perl_alloc(); |
---|
282 | owl_global_set_perlinterp(&g, (void*)p); |
---|
283 | perl_construct(p); |
---|
284 | |
---|
285 | owl_global_set_no_have_config(&g); |
---|
286 | |
---|
287 | |
---|
288 | ret=perl_parse(p, owl_perl_xs_init, 2, args, NULL); |
---|
289 | if (ret || SvTRUE(ERRSV)) { |
---|
290 | STRLEN n_a; |
---|
291 | err=owl_strdup(SvPV(ERRSV, n_a)); |
---|
292 | sv_setsv(ERRSV, &PL_sv_undef); /* and clear the error */ |
---|
293 | return(err); |
---|
294 | } |
---|
295 | |
---|
296 | ret=perl_run(p); |
---|
297 | if (ret || SvTRUE(ERRSV)) { |
---|
298 | STRLEN n_a; |
---|
299 | err=owl_strdup(SvPV(ERRSV, n_a)); |
---|
300 | sv_setsv(ERRSV, &PL_sv_undef); /* and clear the error */ |
---|
301 | return(err); |
---|
302 | } |
---|
303 | |
---|
304 | owl_global_set_have_config(&g); |
---|
305 | |
---|
306 | /* create legacy variables */ |
---|
307 | perl_get_sv("BarnOwl::id", TRUE); |
---|
308 | perl_get_sv("BarnOwl::class", TRUE); |
---|
309 | perl_get_sv("BarnOwl::instance", TRUE); |
---|
310 | perl_get_sv("BarnOwl::recipient", TRUE); |
---|
311 | perl_get_sv("BarnOwl::sender", TRUE); |
---|
312 | perl_get_sv("BarnOwl::realm", TRUE); |
---|
313 | perl_get_sv("BarnOwl::opcode", TRUE); |
---|
314 | perl_get_sv("BarnOwl::zsig", TRUE); |
---|
315 | perl_get_sv("BarnOwl::msg", TRUE); |
---|
316 | perl_get_sv("BarnOwl::time", TRUE); |
---|
317 | perl_get_sv("BarnOwl::host", TRUE); |
---|
318 | perl_get_av("BarnOwl::fields", TRUE); |
---|
319 | |
---|
320 | if(file) { |
---|
321 | SV * cfg = get_sv("BarnOwl::configfile", TRUE); |
---|
322 | sv_setpv(cfg, file); |
---|
323 | } |
---|
324 | |
---|
325 | perl_eval_pv(owl_perlwrap_codebuff, FALSE); |
---|
326 | |
---|
327 | if (SvTRUE(ERRSV)) { |
---|
328 | STRLEN n_a; |
---|
329 | err=owl_strdup(SvPV(ERRSV, n_a)); |
---|
330 | sv_setsv (ERRSV, &PL_sv_undef); /* and clear the error */ |
---|
331 | return(err); |
---|
332 | } |
---|
333 | |
---|
334 | /* check if we have the formatting function */ |
---|
335 | if (owl_perlconfig_is_function("BarnOwl::format_msg")) { |
---|
336 | owl_global_set_config_format(&g, 1); |
---|
337 | } |
---|
338 | |
---|
339 | return(NULL); |
---|
340 | } |
---|
341 | |
---|
342 | /* returns whether or not a function exists */ |
---|
343 | int owl_perlconfig_is_function(char *fn) { |
---|
344 | if (perl_get_cv(fn, FALSE)) return(1); |
---|
345 | else return(0); |
---|
346 | } |
---|
347 | |
---|
348 | /* returns 0 on success */ |
---|
349 | int owl_perlconfig_get_hashkeys(char *hashname, owl_list *l) |
---|
350 | { |
---|
351 | HV *hv; |
---|
352 | HE *he; |
---|
353 | char *key; |
---|
354 | I32 i; |
---|
355 | |
---|
356 | if (owl_list_create(l)) return(-1); |
---|
357 | hv = get_hv(hashname, FALSE); |
---|
358 | if (!hv) return(-1); |
---|
359 | i = hv_iterinit(hv); |
---|
360 | while ((he = hv_iternext(hv))) { |
---|
361 | key = hv_iterkey(he, &i); |
---|
362 | if (key) { |
---|
363 | owl_list_append_element(l, owl_strdup(key)); |
---|
364 | } |
---|
365 | } |
---|
366 | return(0); |
---|
367 | } |
---|
368 | |
---|
369 | /* caller is responsible for freeing returned string */ |
---|
370 | char *owl_perlconfig_execute(char *line) |
---|
371 | { |
---|
372 | STRLEN len; |
---|
373 | SV *response; |
---|
374 | char *out, *preout; |
---|
375 | |
---|
376 | if (!owl_global_have_config(&g)) return NULL; |
---|
377 | |
---|
378 | /* execute the subroutine */ |
---|
379 | response = perl_eval_pv(line, FALSE); |
---|
380 | |
---|
381 | if (SvTRUE(ERRSV)) { |
---|
382 | STRLEN n_a; |
---|
383 | owl_function_error("Perl Error: '%s'", SvPV(ERRSV, n_a)); |
---|
384 | sv_setsv (ERRSV, &PL_sv_undef); /* and clear the error */ |
---|
385 | } |
---|
386 | |
---|
387 | preout=SvPV(response, len); |
---|
388 | /* leave enough space in case we have to add a newline */ |
---|
389 | out = owl_malloc(strlen(preout)+2); |
---|
390 | strncpy(out, preout, len); |
---|
391 | out[len] = '\0'; |
---|
392 | if (!strlen(out) || out[strlen(out)-1]!='\n') { |
---|
393 | strcat(out, "\n"); |
---|
394 | } |
---|
395 | |
---|
396 | return(out); |
---|
397 | } |
---|
398 | |
---|
399 | char *owl_perlconfig_getmsg(owl_message *m, int mode, char *subname) |
---|
400 | { |
---|
401 | /* if mode==1 we are doing message formatting. The returned |
---|
402 | * formatted message needs to be freed by the caller. |
---|
403 | * |
---|
404 | * if mode==0 we are just doing the message-has-been-received |
---|
405 | * thing. |
---|
406 | */ |
---|
407 | if (!owl_global_have_config(&g)) return(NULL); |
---|
408 | |
---|
409 | /* run the procedure corresponding to the mode */ |
---|
410 | if (mode==1) { |
---|
411 | char *ret = NULL; |
---|
412 | ret = owl_perlconfig_call_with_message(subname?subname |
---|
413 | :"BarnOwl::_format_msg_legacy_wrap", m); |
---|
414 | if (!ret) { |
---|
415 | ret = owl_sprintf("@b([Perl Message Formatting Failed!])\n"); |
---|
416 | } |
---|
417 | return ret; |
---|
418 | } else { |
---|
419 | char *ptr = NULL; |
---|
420 | if (owl_perlconfig_is_function("BarnOwl::Hooks::receive_msg")) { |
---|
421 | ptr = owl_perlconfig_call_with_message(subname?subname |
---|
422 | :"BarnOwl::_receive_msg_legacy_wrap", m); |
---|
423 | } |
---|
424 | if (ptr) owl_free(ptr); |
---|
425 | return(NULL); |
---|
426 | } |
---|
427 | } |
---|
428 | |
---|
429 | char *owl_perlconfig_perlcmd(owl_cmd *cmd, int argc, char **argv) |
---|
430 | { |
---|
431 | int i, count; |
---|
432 | char * ret = NULL; |
---|
433 | STRLEN n_a; |
---|
434 | dSP; |
---|
435 | |
---|
436 | ENTER; |
---|
437 | SAVETMPS; |
---|
438 | |
---|
439 | PUSHMARK(SP); |
---|
440 | for(i=0;i<argc;i++) { |
---|
441 | XPUSHs(sv_2mortal(newSVpv(argv[i], 0))); |
---|
442 | } |
---|
443 | PUTBACK; |
---|
444 | |
---|
445 | count = call_sv(cmd->cmd_perl, G_SCALAR|G_EVAL); |
---|
446 | |
---|
447 | SPAGAIN; |
---|
448 | |
---|
449 | if(SvTRUE(ERRSV)) { |
---|
450 | owl_function_error("%s", SvPV(ERRSV, n_a)); |
---|
451 | POPs; |
---|
452 | } else { |
---|
453 | if(count != 1) |
---|
454 | croak("Perl command %s returned more than one value!", cmd->name); |
---|
455 | SV * rv = POPs; |
---|
456 | if(SvTRUE(rv)) { |
---|
457 | ret = owl_strdup(SvPV(rv, n_a)); |
---|
458 | } |
---|
459 | } |
---|
460 | |
---|
461 | FREETMPS; |
---|
462 | LEAVE; |
---|
463 | |
---|
464 | return ret; |
---|
465 | } |
---|
466 | |
---|
467 | void owl_perlconfig_cmd_free(owl_cmd *cmd) |
---|
468 | { |
---|
469 | SvREFCNT_dec(cmd); |
---|
470 | } |
---|
471 | |
---|
472 | void owl_perlconfig_edit_callback(owl_editwin *e) |
---|
473 | { |
---|
474 | SV *cb = (SV*)(e->cbdata); |
---|
475 | unsigned int n_a; |
---|
476 | if(cb == NULL) { |
---|
477 | owl_function_error("Perl callback is NULL!"); |
---|
478 | } |
---|
479 | |
---|
480 | dSP; |
---|
481 | |
---|
482 | ENTER; |
---|
483 | SAVETMPS; |
---|
484 | |
---|
485 | PUSHMARK(SP); |
---|
486 | XPUSHs(sv_2mortal(newSVpv(owl_editwin_get_text(e), 0))); |
---|
487 | PUTBACK; |
---|
488 | |
---|
489 | call_sv(cb, G_DISCARD|G_KEEPERR|G_EVAL); |
---|
490 | |
---|
491 | if(SvTRUE(ERRSV)) { |
---|
492 | owl_function_error("%s", SvPV(ERRSV, n_a)); |
---|
493 | } |
---|
494 | |
---|
495 | FREETMPS; |
---|
496 | LEAVE; |
---|
497 | |
---|
498 | SvREFCNT_dec(cb); |
---|
499 | e->cbdata = NULL; |
---|
500 | } |
---|
501 | |
---|
502 | void owl_perlconfig_mainloop() |
---|
503 | { |
---|
504 | if (!owl_perlconfig_is_function("BarnOwl::Hooks::mainloop_hook")) |
---|
505 | return; |
---|
506 | dSP ; |
---|
507 | PUSHMARK(SP) ; |
---|
508 | call_pv("BarnOwl::Hooks::mainloop_hook", G_DISCARD|G_EVAL); |
---|
509 | if(SvTRUE(ERRSV)) { |
---|
510 | STRLEN n_a; |
---|
511 | owl_function_error("%s", SvPV(ERRSV, n_a)); |
---|
512 | } |
---|
513 | return; |
---|
514 | } |
---|