1 | /* -*- mode: c; indent-tabs-mode: t; c-basic-offset: 8 -*- */ |
---|
2 | #ifdef HAVE_LIBZEPHYR |
---|
3 | #include <zephyr/zephyr.h> |
---|
4 | #endif |
---|
5 | #include <EXTERN.h> |
---|
6 | |
---|
7 | #define OWL_PERL |
---|
8 | #include "owl.h" |
---|
9 | |
---|
10 | #define SV_IS_CODEREF(sv) (SvROK((sv)) && SvTYPE(SvRV((sv))) == SVt_PVCV) |
---|
11 | |
---|
12 | /************************************************************* |
---|
13 | * NOTE |
---|
14 | ************************************************************* |
---|
15 | * These functions, when they are intended to be user-visible, |
---|
16 | * are documented in perl/lib/BarnOwl.pm. If you add functions |
---|
17 | * to this file, add the appropriate documentation there! |
---|
18 | * |
---|
19 | * If the function is simple enough, we simply define its |
---|
20 | * entire functionality here in XS. If, however, it needs |
---|
21 | * complex argument processing or something, we define a |
---|
22 | * simple version here that takes arguments in as flat a |
---|
23 | * manner as possible, to simplify the XS code, put it in |
---|
24 | * BarnOwl::Intenal::, and write a perl wrapper in BarnOwl.pm |
---|
25 | * that munges the arguments as appropriate and calls the |
---|
26 | * internal version. |
---|
27 | */ |
---|
28 | |
---|
29 | MODULE = BarnOwl PACKAGE = BarnOwl |
---|
30 | |
---|
31 | char * |
---|
32 | command(cmd, ...) |
---|
33 | const char *cmd |
---|
34 | PREINIT: |
---|
35 | char *rv = NULL; |
---|
36 | const char **argv; |
---|
37 | int i; |
---|
38 | CODE: |
---|
39 | { |
---|
40 | if (items == 1) { |
---|
41 | rv = owl_function_command(cmd); |
---|
42 | } else { |
---|
43 | argv = owl_malloc((items + 1) * sizeof *argv); |
---|
44 | argv[0] = cmd; |
---|
45 | for(i = 1; i < items; i++) { |
---|
46 | argv[i] = SvPV_nolen(ST(i)); |
---|
47 | } |
---|
48 | rv = owl_function_command_argv(argv, items); |
---|
49 | owl_free(argv); |
---|
50 | } |
---|
51 | RETVAL = rv; |
---|
52 | } |
---|
53 | OUTPUT: |
---|
54 | RETVAL |
---|
55 | CLEANUP: |
---|
56 | if (rv) owl_free(rv); |
---|
57 | |
---|
58 | SV * |
---|
59 | getcurmsg() |
---|
60 | CODE: |
---|
61 | RETVAL = owl_perlconfig_curmessage2hashref(); |
---|
62 | OUTPUT: |
---|
63 | RETVAL |
---|
64 | |
---|
65 | int |
---|
66 | getnumcols() |
---|
67 | CODE: |
---|
68 | RETVAL = owl_global_get_cols(&g); |
---|
69 | OUTPUT: |
---|
70 | RETVAL |
---|
71 | |
---|
72 | time_t |
---|
73 | getidletime() |
---|
74 | CODE: |
---|
75 | RETVAL = owl_global_get_idletime(&g); |
---|
76 | OUTPUT: |
---|
77 | RETVAL |
---|
78 | |
---|
79 | const char * |
---|
80 | zephyr_getrealm() |
---|
81 | CODE: |
---|
82 | RETVAL = owl_zephyr_get_realm(); |
---|
83 | OUTPUT: |
---|
84 | RETVAL |
---|
85 | |
---|
86 | const char * |
---|
87 | zephyr_getsender() |
---|
88 | CODE: |
---|
89 | RETVAL = owl_zephyr_get_sender(); |
---|
90 | OUTPUT: |
---|
91 | RETVAL |
---|
92 | |
---|
93 | void |
---|
94 | zephyr_zwrite(cmd,msg) |
---|
95 | const char *cmd |
---|
96 | const char *msg |
---|
97 | PREINIT: |
---|
98 | int i; |
---|
99 | CODE: |
---|
100 | i = owl_zwrite_create_and_send_from_line(cmd, msg); |
---|
101 | |
---|
102 | char * |
---|
103 | ztext_stylestrip(ztext) |
---|
104 | const char *ztext |
---|
105 | PREINIT: |
---|
106 | char *rv = NULL; |
---|
107 | CODE: |
---|
108 | rv = owl_function_ztext_stylestrip(ztext); |
---|
109 | RETVAL = rv; |
---|
110 | OUTPUT: |
---|
111 | RETVAL |
---|
112 | CLEANUP: |
---|
113 | if (rv) owl_free(rv); |
---|
114 | |
---|
115 | char * |
---|
116 | zephyr_smartstrip_user(in) |
---|
117 | const char *in |
---|
118 | PREINIT: |
---|
119 | char *rv = NULL; |
---|
120 | CODE: |
---|
121 | { |
---|
122 | rv = owl_zephyr_smartstripped_user(in); |
---|
123 | RETVAL = rv; |
---|
124 | } |
---|
125 | OUTPUT: |
---|
126 | RETVAL |
---|
127 | CLEANUP: |
---|
128 | owl_free(rv); |
---|
129 | |
---|
130 | char * |
---|
131 | zephyr_getsubs() |
---|
132 | PREINIT: |
---|
133 | char *rv = NULL; |
---|
134 | CODE: |
---|
135 | rv = owl_zephyr_getsubs(); |
---|
136 | RETVAL = rv; |
---|
137 | OUTPUT: |
---|
138 | RETVAL |
---|
139 | CLEANUP: |
---|
140 | if (rv) owl_free(rv); |
---|
141 | |
---|
142 | void queue_message(msg) |
---|
143 | SV *msg |
---|
144 | PREINIT: |
---|
145 | owl_message *m; |
---|
146 | CODE: |
---|
147 | { |
---|
148 | if(!SvROK(msg) || SvTYPE(SvRV(msg)) != SVt_PVHV) { |
---|
149 | croak("Usage: BarnOwl::queue_message($message)"); |
---|
150 | } |
---|
151 | |
---|
152 | m = owl_perlconfig_hashref2message(msg); |
---|
153 | |
---|
154 | owl_global_messagequeue_addmsg(&g, m); |
---|
155 | } |
---|
156 | |
---|
157 | void admin_message(header, body) |
---|
158 | const char *header |
---|
159 | const char *body |
---|
160 | CODE: |
---|
161 | { |
---|
162 | owl_function_adminmsg(header, body); |
---|
163 | } |
---|
164 | |
---|
165 | void start_question(line, callback) |
---|
166 | const char *line |
---|
167 | SV *callback |
---|
168 | PREINIT: |
---|
169 | CODE: |
---|
170 | { |
---|
171 | if(!SV_IS_CODEREF(callback)) |
---|
172 | croak("Callback must be a subref"); |
---|
173 | |
---|
174 | owl_function_start_question(line); |
---|
175 | |
---|
176 | SvREFCNT_inc(callback); |
---|
177 | owl_editwin_set_cbdata(owl_global_get_typwin(&g), callback); |
---|
178 | owl_editwin_set_callback(owl_global_get_typwin(&g), owl_perlconfig_edit_callback); |
---|
179 | } |
---|
180 | |
---|
181 | void start_password(line, callback) |
---|
182 | const char *line |
---|
183 | SV *callback |
---|
184 | PREINIT: |
---|
185 | CODE: |
---|
186 | { |
---|
187 | if(!SV_IS_CODEREF(callback)) |
---|
188 | croak("Callback must be a subref"); |
---|
189 | |
---|
190 | owl_function_start_password(line); |
---|
191 | |
---|
192 | SvREFCNT_inc(callback); |
---|
193 | owl_editwin_set_cbdata(owl_global_get_typwin(&g), callback); |
---|
194 | owl_editwin_set_callback(owl_global_get_typwin(&g), owl_perlconfig_edit_callback); |
---|
195 | } |
---|
196 | |
---|
197 | void start_edit_win(line, callback) |
---|
198 | const char *line |
---|
199 | SV *callback |
---|
200 | PREINIT: |
---|
201 | owl_editwin * e; |
---|
202 | char buff[1024]; |
---|
203 | CODE: |
---|
204 | { |
---|
205 | if(!SV_IS_CODEREF(callback)) |
---|
206 | croak("Callback must be a subref"); |
---|
207 | |
---|
208 | e = owl_global_get_typwin(&g); |
---|
209 | owl_editwin_new_style(e, OWL_EDITWIN_STYLE_MULTILINE, |
---|
210 | owl_global_get_msg_history(&g)); |
---|
211 | owl_editwin_clear(e); |
---|
212 | owl_editwin_set_dotsend(e); |
---|
213 | snprintf(buff, 1023, "----> %s\n", line); |
---|
214 | owl_editwin_set_locktext(e, buff); |
---|
215 | |
---|
216 | owl_global_set_typwin_active(&g); |
---|
217 | |
---|
218 | SvREFCNT_inc(callback); |
---|
219 | owl_editwin_set_cbdata(owl_global_get_typwin(&g), callback); |
---|
220 | owl_editwin_set_callback(owl_global_get_typwin(&g), owl_perlconfig_edit_callback); |
---|
221 | } |
---|
222 | |
---|
223 | |
---|
224 | const char * |
---|
225 | get_data_dir () |
---|
226 | CODE: |
---|
227 | RETVAL = owl_get_datadir(); |
---|
228 | OUTPUT: |
---|
229 | RETVAL |
---|
230 | |
---|
231 | const char * |
---|
232 | get_config_dir () |
---|
233 | CODE: |
---|
234 | RETVAL = owl_global_get_confdir(&g); |
---|
235 | OUTPUT: |
---|
236 | RETVAL |
---|
237 | |
---|
238 | void |
---|
239 | popless_text(text) |
---|
240 | const char *text |
---|
241 | CODE: |
---|
242 | { |
---|
243 | owl_function_popless_text(text); |
---|
244 | } |
---|
245 | |
---|
246 | void |
---|
247 | popless_ztext(text) |
---|
248 | const char *text |
---|
249 | CODE: |
---|
250 | { |
---|
251 | owl_fmtext fm; |
---|
252 | owl_fmtext_init_null(&fm); |
---|
253 | owl_fmtext_append_ztext(&fm, text); |
---|
254 | owl_function_popless_fmtext(&fm); |
---|
255 | owl_fmtext_free(&fm); |
---|
256 | } |
---|
257 | |
---|
258 | void |
---|
259 | error(text) |
---|
260 | const char *text |
---|
261 | CODE: |
---|
262 | { |
---|
263 | owl_function_error("%s", text); |
---|
264 | } |
---|
265 | |
---|
266 | void |
---|
267 | debug(text) |
---|
268 | const char *text |
---|
269 | CODE: |
---|
270 | { |
---|
271 | owl_function_debugmsg("%s", text); |
---|
272 | } |
---|
273 | |
---|
274 | void |
---|
275 | message(text) |
---|
276 | const char *text |
---|
277 | CODE: |
---|
278 | { |
---|
279 | owl_function_makemsg("%s", text); |
---|
280 | } |
---|
281 | |
---|
282 | void |
---|
283 | create_style(name, object) |
---|
284 | const char *name |
---|
285 | SV *object |
---|
286 | PREINIT: |
---|
287 | owl_style *s; |
---|
288 | CODE: |
---|
289 | { |
---|
290 | s = owl_malloc(sizeof(owl_style)); |
---|
291 | owl_style_create_perl(s, name, object); |
---|
292 | owl_global_add_style(&g, s); |
---|
293 | } |
---|
294 | |
---|
295 | int |
---|
296 | getnumcolors() |
---|
297 | CODE: |
---|
298 | RETVAL = owl_function_get_color_count(); |
---|
299 | OUTPUT: |
---|
300 | RETVAL |
---|
301 | |
---|
302 | void |
---|
303 | _remove_filter(filterName) |
---|
304 | const char *filterName |
---|
305 | CODE: |
---|
306 | { |
---|
307 | /* Don't delete the current view, or the 'all' filter */ |
---|
308 | if (strcmp(filterName, owl_view_get_filtname(owl_global_get_current_view(&g))) |
---|
309 | && strcmp(filterName, "all")) { |
---|
310 | owl_global_remove_filter(&g,filterName); |
---|
311 | } |
---|
312 | } |
---|
313 | |
---|
314 | char * |
---|
315 | wordwrap(in, cols) |
---|
316 | const char *in |
---|
317 | int cols |
---|
318 | PREINIT: |
---|
319 | char *rv = NULL; |
---|
320 | CODE: |
---|
321 | rv = owl_text_wordwrap(in, cols); |
---|
322 | RETVAL = rv; |
---|
323 | OUTPUT: |
---|
324 | RETVAL |
---|
325 | CLEANUP: |
---|
326 | if (rv) owl_free(rv); |
---|
327 | |
---|
328 | void |
---|
329 | add_dispatch(fd, cb) |
---|
330 | int fd |
---|
331 | SV * cb |
---|
332 | CODE: |
---|
333 | SvREFCNT_inc(cb); |
---|
334 | owl_select_add_perl_dispatch(fd, cb); |
---|
335 | |
---|
336 | void |
---|
337 | remove_dispatch(fd) |
---|
338 | int fd |
---|
339 | CODE: |
---|
340 | owl_select_remove_perl_dispatch(fd); |
---|
341 | |
---|
342 | |
---|
343 | AV* |
---|
344 | all_filters() |
---|
345 | PREINIT: |
---|
346 | AV *filters; |
---|
347 | const owl_list *fl; |
---|
348 | const owl_filter *f; |
---|
349 | int i; |
---|
350 | CODE: |
---|
351 | { |
---|
352 | fl = owl_global_get_filterlist(&g); |
---|
353 | filters = newAV(); |
---|
354 | for(i=0;i<owl_list_get_size(fl);i++) { |
---|
355 | f = owl_list_get_element(fl, i); |
---|
356 | av_push(filters, newSVpv(owl_filter_get_name(f), 0)); |
---|
357 | } |
---|
358 | RETVAL = filters; |
---|
359 | sv_2mortal((SV*)RETVAL); |
---|
360 | } |
---|
361 | OUTPUT: |
---|
362 | RETVAL |
---|
363 | |
---|
364 | AV* |
---|
365 | all_styles() |
---|
366 | PREINIT: |
---|
367 | AV *styles; |
---|
368 | owl_list l; |
---|
369 | const char *name; |
---|
370 | int i; |
---|
371 | CODE: |
---|
372 | { |
---|
373 | owl_list_create(&l); |
---|
374 | owl_global_get_style_names(&g, &l); |
---|
375 | styles = newAV(); |
---|
376 | for(i=0;i<owl_list_get_size(&l);i++) { |
---|
377 | name = owl_list_get_element(&l, i); |
---|
378 | av_push(styles, newSVpv(name, 0)); |
---|
379 | } |
---|
380 | RETVAL = styles; |
---|
381 | sv_2mortal((SV*)RETVAL); |
---|
382 | } |
---|
383 | OUTPUT: |
---|
384 | RETVAL |
---|
385 | CLEANUP: |
---|
386 | owl_list_free_all(&l, owl_free); |
---|
387 | |
---|
388 | |
---|
389 | AV* |
---|
390 | all_variables() |
---|
391 | PREINIT: |
---|
392 | AV *vars; |
---|
393 | owl_list l; |
---|
394 | const char *name; |
---|
395 | int i; |
---|
396 | CODE: |
---|
397 | { |
---|
398 | owl_list_create(&l); |
---|
399 | owl_dict_get_keys(owl_global_get_vardict(&g), &l); |
---|
400 | vars = newAV(); |
---|
401 | for(i=0;i<owl_list_get_size(&l);i++) { |
---|
402 | name = owl_list_get_element(&l, i); |
---|
403 | av_push(vars, newSVpv(name, 0)); |
---|
404 | } |
---|
405 | RETVAL = vars; |
---|
406 | sv_2mortal((SV*)RETVAL); |
---|
407 | } |
---|
408 | OUTPUT: |
---|
409 | RETVAL |
---|
410 | CLEANUP: |
---|
411 | owl_list_free_all(&l, owl_free); |
---|
412 | |
---|
413 | MODULE = BarnOwl PACKAGE = BarnOwl::Internal |
---|
414 | |
---|
415 | |
---|
416 | void |
---|
417 | new_command(name, func, summary, usage, description) |
---|
418 | char *name |
---|
419 | SV *func |
---|
420 | char *summary |
---|
421 | char *usage |
---|
422 | char *description |
---|
423 | PREINIT: |
---|
424 | owl_cmd cmd; |
---|
425 | CODE: |
---|
426 | { |
---|
427 | if(!SV_IS_CODEREF(func)) { |
---|
428 | croak("Command function must be a coderef!"); |
---|
429 | } |
---|
430 | SvREFCNT_inc(func); |
---|
431 | cmd.name = name; |
---|
432 | cmd.cmd_perl = func; |
---|
433 | cmd.summary = summary; |
---|
434 | cmd.usage = usage; |
---|
435 | cmd.description = description; |
---|
436 | cmd.validctx = OWL_CTX_ANY; |
---|
437 | cmd.cmd_aliased_to = NULL; |
---|
438 | cmd.cmd_args_fn = NULL; |
---|
439 | cmd.cmd_v_fn = NULL; |
---|
440 | cmd.cmd_i_fn = NULL; |
---|
441 | cmd.cmd_ctxargs_fn = NULL; |
---|
442 | cmd.cmd_ctxv_fn = NULL; |
---|
443 | cmd.cmd_ctxi_fn = NULL; |
---|
444 | owl_cmddict_add_cmd(owl_global_get_cmddict(&g), &cmd); |
---|
445 | } |
---|
446 | |
---|
447 | void |
---|
448 | new_variable_string(name, ival, summ, desc) |
---|
449 | const char * name |
---|
450 | const char * ival |
---|
451 | const char * summ |
---|
452 | const char * desc |
---|
453 | CODE: |
---|
454 | owl_variable_dict_newvar_string(owl_global_get_vardict(&g), |
---|
455 | name, |
---|
456 | summ, |
---|
457 | desc, |
---|
458 | ival); |
---|
459 | |
---|
460 | void |
---|
461 | new_variable_int(name, ival, summ, desc) |
---|
462 | const char * name |
---|
463 | int ival |
---|
464 | const char * summ |
---|
465 | const char * desc |
---|
466 | CODE: |
---|
467 | owl_variable_dict_newvar_int(owl_global_get_vardict(&g), |
---|
468 | name, |
---|
469 | summ, |
---|
470 | desc, |
---|
471 | ival); |
---|
472 | |
---|
473 | void |
---|
474 | new_variable_bool(name, ival, summ, desc) |
---|
475 | const char * name |
---|
476 | int ival |
---|
477 | const char * summ |
---|
478 | const char * desc |
---|
479 | CODE: |
---|
480 | owl_variable_dict_newvar_bool(owl_global_get_vardict(&g), |
---|
481 | name, |
---|
482 | summ, |
---|
483 | desc, |
---|
484 | ival); |
---|
485 | |
---|
486 | IV |
---|
487 | add_timer(after, interval, cb) |
---|
488 | int after |
---|
489 | int interval |
---|
490 | SV *cb |
---|
491 | PREINIT: |
---|
492 | SV *ref; |
---|
493 | owl_timer *t; |
---|
494 | CODE: |
---|
495 | ref = sv_rvweaken(newSVsv(cb)); |
---|
496 | t = owl_select_add_timer(after, |
---|
497 | interval, |
---|
498 | owl_perlconfig_perl_timer, |
---|
499 | owl_perlconfig_perl_timer_destroy, |
---|
500 | ref); |
---|
501 | owl_function_debugmsg("Created timer %p", t); |
---|
502 | RETVAL = (IV)t; |
---|
503 | OUTPUT: |
---|
504 | RETVAL |
---|
505 | |
---|
506 | void |
---|
507 | remove_timer(timer) |
---|
508 | IV timer |
---|
509 | PREINIT: |
---|
510 | owl_timer *t; |
---|
511 | CODE: |
---|
512 | t = (owl_timer*)timer; |
---|
513 | owl_function_debugmsg("Freeing timer %p", t); |
---|
514 | owl_select_remove_timer(t); |
---|
515 | |
---|
516 | MODULE = BarnOwl PACKAGE = BarnOwl::Editwin |
---|
517 | |
---|
518 | int |
---|
519 | replace(count, string) |
---|
520 | int count; |
---|
521 | const char *string; |
---|
522 | CODE: |
---|
523 | RETVAL = owl_editwin_replace(owl_global_get_typwin(&g), count, string); |
---|
524 | OUTPUT: |
---|
525 | RETVAL |
---|
526 | |
---|
527 | int |
---|
528 | point_move(delta) |
---|
529 | int delta; |
---|
530 | CODE: |
---|
531 | RETVAL = owl_editwin_point_move(owl_global_get_typwin(&g), delta); |
---|
532 | OUTPUT: |
---|
533 | RETVAL |
---|
534 | |
---|
535 | int |
---|
536 | replace_region(string) |
---|
537 | const char *string; |
---|
538 | CODE: |
---|
539 | RETVAL = owl_editwin_replace_region(owl_global_get_typwin(&g), string); |
---|
540 | OUTPUT: |
---|
541 | RETVAL |
---|
542 | |
---|
543 | char * |
---|
544 | get_region() |
---|
545 | PREINIT: |
---|
546 | char *region; |
---|
547 | CODE: |
---|
548 | region = owl_editwin_get_region(owl_global_get_typwin(&g)); |
---|
549 | RETVAL = region; |
---|
550 | OUTPUT: |
---|
551 | RETVAL |
---|
552 | CLEANUP: |
---|
553 | owl_free(region); |
---|
554 | |
---|
555 | SV * |
---|
556 | save_excursion(sub) |
---|
557 | SV *sub; |
---|
558 | PROTOTYPE: & |
---|
559 | PREINIT: |
---|
560 | int count; |
---|
561 | owl_editwin_excursion *x; |
---|
562 | CODE: |
---|
563 | { |
---|
564 | x = owl_editwin_begin_excursion(owl_global_get_typwin(&g)); |
---|
565 | PUSHMARK(SP); |
---|
566 | count = call_sv(sub, G_SCALAR|G_EVAL|G_NOARGS); |
---|
567 | SPAGAIN; |
---|
568 | owl_editwin_end_excursion(owl_global_get_typwin(&g), x); |
---|
569 | |
---|
570 | if(SvTRUE(ERRSV)) { |
---|
571 | croak(NULL); |
---|
572 | } |
---|
573 | |
---|
574 | if(count == 1) |
---|
575 | RETVAL = SvREFCNT_inc(POPs); |
---|
576 | else |
---|
577 | XSRETURN_UNDEF; |
---|
578 | |
---|
579 | } |
---|
580 | OUTPUT: |
---|
581 | RETVAL |
---|
582 | |
---|
583 | int |
---|
584 | current_column() |
---|
585 | CODE: |
---|
586 | RETVAL = owl_editwin_current_column(owl_global_get_typwin(&g)); |
---|
587 | OUTPUT: |
---|
588 | RETVAL |
---|
589 | |
---|
590 | int |
---|
591 | point() |
---|
592 | CODE: |
---|
593 | RETVAL = owl_editwin_get_point(owl_global_get_typwin(&g)); |
---|
594 | OUTPUT: |
---|
595 | RETVAL |
---|
596 | |
---|
597 | int |
---|
598 | mark() |
---|
599 | CODE: |
---|
600 | RETVAL = owl_editwin_get_mark(owl_global_get_typwin(&g)); |
---|
601 | OUTPUT: |
---|
602 | RETVAL |
---|