source: functions.c @ 010a951

release-1.10release-1.8release-1.9
Last change on this file since 010a951 was 010a951, checked in by David Benjamin <davidben@mit.edu>, 14 years ago
Make owl_variable's get_tostring_fn return a newly allocated string The mess with buffer lengths and whatnot is obnoxious. Note that this does change semantics slightly: instead of (optionally) filling a buffer and giving a return code for good vs no-variable/invalid, we now return NULL for missing variables and always return a string if the variable exists. This also appears to be more accurate from the perspective of the calls anyway.
  • Property mode set to 100644
File size: 95.9 KB
Line 
1#include <stdio.h>
2#include <stdlib.h>
3#include <unistd.h>
4#include <signal.h>
5#include <netinet/in.h>
6#include <string.h>
7#include <time.h>
8#include <sys/types.h>
9#include <sys/stat.h>
10#include <sys/wait.h>
11#include <errno.h>
12#include <signal.h>
13#include "owl.h"
14#include "filterproc.h"
15
16char *owl_function_command(const char *cmdbuff)
17{
18  owl_function_debugmsg("executing command: %s", cmdbuff);
19  return owl_cmddict_execute(owl_global_get_cmddict(&g), 
20                             owl_global_get_context(&g), cmdbuff);
21}
22
23char *owl_function_command_argv(const char *const *argv, int argc)
24{
25  return owl_cmddict_execute_argv(owl_global_get_cmddict(&g),
26                                  owl_global_get_context(&g),
27                                  argv, argc);
28}
29
30void owl_function_command_norv(const char *cmdbuff)
31{
32  char *rv;
33  rv=owl_function_command(cmdbuff);
34  if (rv) g_free(rv);
35}
36
37void owl_function_command_alias(const char *alias_from, const char *alias_to)
38{
39  owl_cmddict_add_alias(owl_global_get_cmddict(&g), alias_from, alias_to);
40}
41
42const owl_cmd *owl_function_get_cmd(const char *name)
43{
44  return owl_cmddict_find(owl_global_get_cmddict(&g), name);
45}
46
47void owl_function_show_commands(void)
48{
49  owl_list l;
50  owl_fmtext fm;
51
52  owl_fmtext_init_null(&fm);
53  owl_fmtext_append_bold(&fm, "Commands:  ");
54  owl_fmtext_append_normal(&fm, "(use 'show command <name>' for details)\n");
55  owl_cmddict_get_names(owl_global_get_cmddict(&g), &l);
56  owl_fmtext_append_list(&fm, &l, "\n", owl_function_cmd_describe);
57  owl_fmtext_append_normal(&fm, "\n");
58  owl_function_popless_fmtext(&fm);
59  owl_cmddict_namelist_cleanup(&l);
60  owl_fmtext_cleanup(&fm);
61}
62
63void owl_function_show_view(const char *viewname)
64{
65  const owl_view *v;
66  owl_fmtext fm;
67
68  /* we only have the one view right now */
69  v=owl_global_get_current_view(&g);
70  if (viewname && strcmp(viewname, owl_view_get_name(v))) {
71    owl_function_error("No view named '%s'", viewname);
72    return;
73  }
74
75  owl_fmtext_init_null(&fm);
76  owl_view_to_fmtext(v, &fm);
77  owl_function_popless_fmtext(&fm);
78  owl_fmtext_cleanup(&fm);
79}
80
81void owl_function_show_styles(void) {
82  owl_list l;
83  owl_fmtext fm;
84
85  owl_fmtext_init_null(&fm);
86  owl_fmtext_append_bold(&fm, "Styles:\n");
87  owl_global_get_style_names(&g, &l);
88  owl_fmtext_append_list(&fm, &l, "\n", owl_function_style_describe);
89  owl_fmtext_append_normal(&fm, "\n");
90  owl_function_popless_fmtext(&fm);
91  owl_list_cleanup(&l, g_free);
92  owl_fmtext_cleanup(&fm);
93}
94
95static void _owl_function_timer_append_fmtext(gpointer data, gpointer user_data) {
96  owl_fmtext *fm = user_data;
97  owl_timer *timer = data;
98  char *str = g_strdup_printf("- %s: in %d seconds",
99                              timer->name ? timer->name : "(unnamed)",
100                              (int)(timer->time - time(NULL)));
101  owl_fmtext_append_normal(fm, str);
102  g_free(str);
103  if (timer->interval) {
104    str = g_strdup_printf(", repeat every %d seconds", timer->interval);
105    owl_fmtext_append_normal(fm, str);
106    g_free(str);
107  }
108  owl_fmtext_append_normal(fm, "\n");
109}
110
111void owl_function_show_timers(void) {
112  owl_fmtext fm;
113  GList **timers;
114
115  owl_fmtext_init_null(&fm);
116  owl_fmtext_append_bold(&fm, "Active timers:\n");
117
118  timers = owl_global_get_timerlist(&g);
119  g_list_foreach(*timers, _owl_function_timer_append_fmtext, &fm);
120
121  owl_function_popless_fmtext(&fm);
122  owl_fmtext_cleanup(&fm);
123}
124
125char *owl_function_style_describe(const char *name) {
126  const char *desc;
127  char *s;
128  const owl_style *style;
129  style = owl_global_get_style_by_name(&g, name);
130  if (style) {
131    desc = owl_style_get_description(style);
132  } else {
133    desc = "???";
134  }
135  s = g_strdup_printf("%-20s - %s%s", name,
136                      0 == owl_style_validate(style) ? "" : "[INVALID] ",
137                      desc);
138  return s;
139}
140
141char *owl_function_cmd_describe(const char *name)
142{
143  const owl_cmd *cmd = owl_cmddict_find(owl_global_get_cmddict(&g), name);
144  if (cmd) return owl_cmd_describe(cmd);
145  else return(NULL);
146}
147
148void owl_function_show_command(const char *name)
149{
150  owl_function_help_for_command(name);
151}
152
153void owl_function_show_license(void)
154{
155  const char *text;
156
157  text=""
158    "barnowl version " OWL_VERSION_STRING "\n"
159    "Copyright (c) 2006-2011 The BarnOwl Developers. All rights reserved.\n"
160    "Copyright (c) 2004 James Kretchmar. All rights reserved.\n"
161    "\n"
162    "Redistribution and use in source and binary forms, with or without\n"
163    "modification, are permitted provided that the following conditions are\n"
164    "met:\n"
165    "\n"
166    "   * Redistributions of source code must retain the above copyright\n"
167    "     notice, this list of conditions and the following disclaimer.\n"
168    "\n"
169    "   * Redistributions in binary form must reproduce the above copyright\n"
170    "     notice, this list of conditions and the following disclaimer in\n"
171    "     the documentation and/or other materials provided with the\n"
172    "     distribution.\n"
173    "\n"
174    "   * Redistributions in any form must be accompanied by information on\n"
175    "     how to obtain complete source code for the Owl software and any\n"
176    "     accompanying software that uses the Owl software. The source code\n"
177    "     must either be included in the distribution or be available for no\n"
178    "     more than the cost of distribution plus a nominal fee, and must be\n"
179    "     freely redistributable under reasonable conditions. For an\n"
180    "     executable file, complete source code means the source code for\n"
181    "     all modules it contains. It does not include source code for\n"
182    "     modules or files that typically accompany the major components of\n"
183    "     the operating system on which the executable file runs.\n"
184    "\n"
185    "THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR\n"
186    "IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\n"
187    "WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR\n"
188    "NON-INFRINGEMENT, ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE\n"
189    "LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n"
190    "CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n"
191    "SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR\n"
192    "BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,\n"
193    "WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n"
194    "OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN\n"
195    "IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n";
196  owl_function_popless_text(text);
197}
198
199void owl_function_show_quickstart(void)
200{
201    const char *message =
202    "Move between messages with the arrow keys, and press 'r' to reply.\n"
203    "For more info, press 'h' or visit http://barnowl.mit.edu/\n\n"
204#ifdef HAVE_LIBZEPHYR
205    "@b(Zephyr:)\n"
206    "To send a message to a user, type ':zwrite @b(username)'. You can also\n"
207    "press 'z' and then type the username. To subscribe to a class, type\n"
208    "':sub @b(class)', and then type ':zwrite -c @b(class)' to send.\n\n"
209#endif
210    "@b(AIM:)\n"
211    "Log in to AIM with ':aimlogin @b(screenname)'. Use ':aimwrite @b(screenname)',\n"
212    "or 'a' and then the screen name, to send someone a message.\n\n"
213    ;
214
215    if (owl_perlconfig_is_function("BarnOwl::Hooks::_get_quickstart")) {
216        char *perlquickstart = owl_perlconfig_execute("BarnOwl::Hooks::_get_quickstart()");
217        if (perlquickstart) {
218            char *result = g_strdup_printf("%s%s", message, perlquickstart);
219            owl_function_adminmsg("BarnOwl Quickstart", result);
220            g_free(result);
221            g_free(perlquickstart);
222            return;
223        }
224    }
225    owl_function_adminmsg("BarnOwl Quickstart", message);
226}
227
228
229/* Create an admin message, append it to the global list of messages
230 * and redisplay if necessary.
231 */
232void owl_function_adminmsg(const char *header, const char *body)
233{
234  owl_message *m;
235
236  m=g_new(owl_message, 1);
237  owl_message_create_admin(m, header, body);
238 
239  /* add it to the global list and current view */
240  owl_messagelist_append_element(owl_global_get_msglist(&g), m);
241  owl_view_consider_message(owl_global_get_current_view(&g), m);
242
243  /* do followlast if necessary */
244  if (owl_global_should_followlast(&g)) owl_function_lastmsg_noredisplay();
245
246  /* redisplay etc. */
247  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
248}
249
250/* Create an outgoing zephyr message and return a pointer to it.  Does
251 * not put it on the global queue, use owl_global_messagequeue_addmsg() for
252 * that.
253 */
254owl_message *owl_function_make_outgoing_zephyr(const owl_zwrite *z)
255{
256  owl_message *m;
257
258  /* create the message */
259  m=g_new(owl_message, 1);
260  owl_message_create_from_zwrite(m, z, owl_zwrite_get_message(z));
261
262  return(m);
263}
264
265/* Create an outgoing AIM message, returns a pointer to the created
266 * message or NULL if we're not logged into AIM (and thus unable to
267 * create the message).  Does not put it on the global queue.  Use
268 * owl_global_messagequeue_addmsg() for that .
269 */
270owl_message *owl_function_make_outgoing_aim(const char *body, const char *to)
271{
272  owl_message *m;
273
274  /* error if we're not logged into aim */
275  if (!owl_global_is_aimloggedin(&g)) return(NULL);
276 
277  m=g_new(owl_message, 1);
278  owl_message_create_aim(m,
279                         owl_global_get_aim_screenname(&g),
280                         to,
281                         body,
282                         OWL_MESSAGE_DIRECTION_OUT,
283                         0);
284  return(m);
285}
286
287/* Create an outgoing loopback message and return a pointer to it.
288 * Does not append it to the global queue, use
289 * owl_global_messagequeue_addmsg() for that.
290 */
291owl_message *owl_function_make_outgoing_loopback(const char *body)
292{
293  owl_message *m;
294
295  /* create the message */
296  m=g_new(owl_message, 1);
297  owl_message_create_loopback(m, body);
298  owl_message_set_direction_out(m);
299
300  return(m);
301}
302
303void owl_function_start_edit_win(const char *line, void (*callback)(owl_editwin *), void *data, void (*cleanup)(void *))
304{
305  owl_editwin *e;
306  owl_context *ctx;
307  char *s;
308
309  /* create and setup the editwin */
310  e = owl_global_set_typwin_active(&g, OWL_EDITWIN_STYLE_MULTILINE,
311                                   owl_global_get_msg_history(&g));
312  owl_editwin_set_dotsend(e);
313  s = g_strdup_printf("----> %s\n", line);
314  owl_editwin_set_locktext(e, s);
315  g_free(s);
316
317  owl_editwin_set_cbdata(e, data, cleanup);
318  owl_editwin_set_callback(e, callback);
319  ctx = owl_editcontext_new(OWL_CTX_EDITMULTI, e, "editmulti",
320                            owl_global_deactivate_editcontext, &g);
321  owl_global_push_context_obj(&g, ctx);
322
323}
324
325static void owl_function_write_setup(const char *noun)
326{
327
328  if (!owl_global_get_lockout_ctrld(&g))
329    owl_function_makemsg("Type your %s below.  "
330                         "End with ^D or a dot on a line by itself."
331                         "  ^C will quit.", noun);
332  else
333    owl_function_makemsg("Type your %s below.  "
334                         "End with a dot on a line by itself.  ^C will quit.",
335                         noun);
336}
337
338void owl_function_zwrite_setup(owl_zwrite *z)
339{
340  /* send a ping if necessary */
341  if (owl_global_is_txping(&g)) {
342    owl_zwrite_send_ping(z);
343  }
344
345
346  owl_function_write_setup("zephyr");
347  owl_function_start_edit_win(z->zwriteline,
348                              &owl_callback_zwrite,
349                              z, (void(*)(void*))owl_zwrite_delete);
350}
351
352void owl_function_aimwrite_setup(const char *to)
353{
354  /* TODO: We probably actually want an owl_aimwrite object like
355   * owl_zwrite. */
356  char *line = g_strdup_printf("aimwrite %s", to);
357  owl_function_write_setup("message");
358  owl_function_start_edit_win(line,
359                              &owl_callback_aimwrite,
360                              g_strdup(to),
361                              g_free);
362  g_free(line);
363}
364
365void owl_function_loopwrite_setup(void)
366{
367  owl_function_write_setup("message");
368  owl_function_start_edit_win("loopwrite",
369                              &owl_callback_loopwrite,
370                              NULL, NULL);
371}
372
373void owl_callback_zwrite(owl_editwin *e) {
374  owl_zwrite *z = owl_editwin_get_cbdata(e);
375  owl_function_zwrite(z, owl_editwin_get_text(e));
376}
377
378/* send, log and display an outgoing zephyr.  If 'msg' is NULL
379 * the message is expected to be set from the zwrite line itself
380 */
381#ifdef HAVE_LIBZEPHYR
382void owl_function_zwrite(owl_zwrite *z, const char *msg)
383{
384  owl_message *m;
385  int ret;
386
387  if(strcmp(z->cmd, "zcrypt") == 0) {
388    owl_function_zcrypt(z, msg);
389    return;
390  }
391
392  /* create the zwrite and send the message */
393  owl_zwrite_populate_zsig(z);
394  if (msg) {
395    owl_zwrite_set_message(z, msg);
396  }
397  ret = owl_zwrite_send_message(z);
398  if (ret != 0) {
399    owl_function_makemsg("Error sending zephyr: %s", error_message(ret));
400    return;
401  }
402  owl_function_makemsg("Waiting for ack...");
403
404  /* If it's personal */
405  if (owl_zwrite_is_personal(z)) {
406    /* create the outgoing message */
407    m=owl_function_make_outgoing_zephyr(z);
408
409    if (m) {
410      owl_global_messagequeue_addmsg(&g, m);
411    } else {
412      owl_function_error("Could not create outgoing zephyr message");
413    }
414  }
415}
416#else
417void owl_function_zwrite(owl_zwrite *z, const char *msg) {
418}
419#endif
420
421/* send, log and display an outgoing zcrypt zephyr.  If 'msg' is NULL
422 * the message is expected to be set from the zwrite line itself
423 */
424void owl_function_zcrypt(owl_zwrite *z, const char *msg)
425{
426  char *cryptmsg;
427  owl_message *m;
428  const char *argv[7];
429  char *zcrypt;
430  int rv, status;
431  char *old_msg;
432
433  /* create the zwrite and send the message */
434  owl_zwrite_populate_zsig(z);
435  if (msg) {
436    owl_zwrite_set_message(z, msg);
437  }
438  old_msg = g_strdup(owl_zwrite_get_message(z));
439
440  zcrypt = g_strdup_printf("%s/zcrypt", owl_get_bindir());
441  argv[0] = "zcrypt";
442  argv[1] = "-E";
443  argv[2] = "-c"; argv[3] = owl_zwrite_get_class(z);
444  argv[4] = "-i"; argv[5] = owl_zwrite_get_instance(z);
445  argv[6] = NULL;
446
447  rv = call_filter(zcrypt, argv, owl_zwrite_get_message(z), &cryptmsg, &status);
448
449  g_free(zcrypt);
450
451  if (rv || status) {
452    if(cryptmsg) g_free(cryptmsg);
453    g_free(old_msg);
454    owl_function_error("Error in zcrypt, possibly no key found.  Message not sent.");
455    owl_function_beep();
456    return;
457  }
458
459  owl_zwrite_set_message_raw(z, cryptmsg);
460  owl_zwrite_set_opcode(z, "crypt");
461
462  owl_zwrite_send_message(z);
463  owl_function_makemsg("Waiting for ack...");
464
465  /* If it's personal */
466  if (owl_zwrite_is_personal(z)) {
467    /* Create the outgoing message. Restore the un-crypted message for display. */
468    owl_zwrite_set_message_raw(z, old_msg);
469    m=owl_function_make_outgoing_zephyr(z);
470    if (m) {
471      owl_global_messagequeue_addmsg(&g, m);
472    } else {
473      owl_function_error("Could not create outgoing zephyr message");
474    }
475  }
476
477  /* free the zwrite */
478  g_free(cryptmsg);
479}
480
481void owl_callback_aimwrite(owl_editwin *e) {
482  char *to = owl_editwin_get_cbdata(e);
483  owl_function_aimwrite(to, owl_editwin_get_text(e), true);
484}
485
486void owl_function_aimwrite(const char *to, const char *msg, bool unwrap)
487{
488  int ret;
489  char *format_msg;
490  owl_message *m;
491
492  /* make a formatted copy of the message */
493  format_msg = g_strdup(msg);
494  if (unwrap)
495    owl_text_wordunwrap(format_msg);
496 
497  /* send the message */
498  ret=owl_aim_send_im(to, format_msg);
499  if (!ret) {
500    owl_function_makemsg("AIM message sent.");
501  } else {
502    owl_function_error("Could not send AIM message.");
503  }
504
505  /* create the outgoing message */
506  m=owl_function_make_outgoing_aim(msg, to);
507
508  if (m) {
509    owl_global_messagequeue_addmsg(&g, m);
510  } else {
511    owl_function_error("Could not create outgoing AIM message");
512  }
513
514  g_free(format_msg);
515}
516
517void owl_function_send_aimawymsg(const char *to, const char *msg)
518{
519  int ret;
520  char *format_msg;
521  owl_message *m;
522
523  /* make a formatted copy of the message */
524  format_msg=g_strdup(msg);
525  owl_text_wordunwrap(format_msg);
526 
527  /* send the message */
528  ret=owl_aim_send_awaymsg(to, format_msg);
529  if (!ret) {
530    /* owl_function_makemsg("AIM message sent."); */
531  } else {
532    owl_function_error("Could not send AIM message.");
533  }
534
535  /* create the message */
536  m=owl_function_make_outgoing_aim(msg, to);
537  if (m) {
538    owl_global_messagequeue_addmsg(&g, m);
539  } else {
540    owl_function_error("Could not create AIM message");
541  }
542  g_free(format_msg);
543}
544
545void owl_callback_loopwrite(owl_editwin *e) {
546  owl_function_loopwrite(owl_editwin_get_text(e));
547}
548
549void owl_function_loopwrite(const char *msg)
550{
551  owl_message *min, *mout;
552
553  /* create a message and put it on the message queue.  This simulates
554   * an incoming message */
555  min=g_new(owl_message, 1);
556  mout=owl_function_make_outgoing_loopback(msg);
557
558  if (owl_global_is_displayoutgoing(&g)) {
559    owl_global_messagequeue_addmsg(&g, mout);
560  } else {
561    owl_message_delete(mout);
562  }
563
564  owl_message_create_loopback(min, msg);
565  owl_message_set_direction_in(min);
566  owl_global_messagequeue_addmsg(&g, min);
567
568  /* fake a makemsg */
569  owl_function_makemsg("loopback message sent");
570}
571
572/* If filter is non-null, looks for the next message matching
573 * that filter.  If skip_deleted, skips any deleted messages.
574 * If last_if_none, will stop at the last message in the view
575 * if no matching messages are found.  */
576void owl_function_nextmsg_full(const char *filter, int skip_deleted, int last_if_none)
577{
578  int curmsg, i, viewsize, found;
579  const owl_view *v;
580  const owl_filter *f = NULL;
581  const owl_message *m;
582
583  v=owl_global_get_current_view(&g);
584
585  if (filter) {
586    f=owl_global_get_filter(&g, filter);
587    if (!f) {
588      owl_function_error("No %s filter defined", filter);
589      return;
590    }
591  }
592
593  curmsg=owl_global_get_curmsg(&g);
594  viewsize=owl_view_get_size(v);
595  found=0;
596
597  /* just check to make sure we're in bounds... */
598  if (curmsg>viewsize-1) curmsg=viewsize-1;
599  if (curmsg<0) curmsg=0;
600
601  for (i=curmsg+1; i<viewsize; i++) {
602    m=owl_view_get_element(v, i);
603    if (skip_deleted && owl_message_is_delete(m)) continue;
604    if (f && !owl_filter_message_match(f, m)) continue;
605    found = 1;
606    break;
607  }
608
609  if (i>owl_view_get_size(v)-1) i=owl_view_get_size(v)-1;
610  if (i<0) i=0;
611
612  if (!found) {
613    owl_function_makemsg("already at last%s message%s%s%s",
614                         skip_deleted?" non-deleted":"",
615                         filter?" in ":"", filter?filter:"",
616                         owl_mainwin_is_curmsg_truncated(owl_global_get_mainwin(&g)) ?
617                         ", press Enter to scroll" : "");
618    /* if (!skip_deleted) owl_function_beep(); */
619  }
620
621  if (last_if_none || found) {
622    owl_global_set_curmsg(&g, i);
623    owl_function_calculate_topmsg(OWL_DIRECTION_DOWNWARDS);
624    owl_mainwin_redisplay(owl_global_get_mainwin(&g));
625    owl_global_set_direction_downwards(&g);
626  }
627}
628
629void owl_function_prevmsg_full(const char *filter, int skip_deleted, int first_if_none)
630{
631  int curmsg, i, found;
632  const owl_view *v;
633  const owl_filter *f = NULL;
634  const owl_message *m;
635
636  v=owl_global_get_current_view(&g);
637
638  if (filter) {
639    f=owl_global_get_filter(&g, filter);
640    if (!f) {
641      owl_function_error("No %s filter defined", filter);
642      return;
643    }
644  }
645
646  curmsg=owl_global_get_curmsg(&g);
647  found=0;
648
649  /* just check to make sure we're in bounds... */
650  if (curmsg<0) curmsg=0;
651
652  for (i=curmsg-1; i>=0; i--) {
653    m=owl_view_get_element(v, i);
654    if (skip_deleted && owl_message_is_delete(m)) continue;
655    if (f && !owl_filter_message_match(f, m)) continue;
656    found = 1;
657    break;
658  }
659
660  if (i<0) i=0;
661
662  if (!found) {
663    owl_function_makemsg("already at first%s message%s%s",
664                         skip_deleted?" non-deleted":"",
665                         filter?" in ":"", filter?filter:"");
666    /* if (!skip_deleted) owl_function_beep(); */
667  }
668
669  if (first_if_none || found) {
670    owl_global_set_curmsg(&g, i);
671    owl_function_calculate_topmsg(OWL_DIRECTION_UPWARDS);
672    owl_mainwin_redisplay(owl_global_get_mainwin(&g));
673    owl_global_set_direction_upwards(&g);
674  }
675}
676
677void owl_function_nextmsg(void)
678{
679  owl_function_nextmsg_full(NULL, 0, 1);
680}
681
682void owl_function_prevmsg(void)
683{
684  owl_function_prevmsg_full(NULL, 0, 1);
685}
686
687void owl_function_nextmsg_notdeleted(void)
688{
689  owl_function_nextmsg_full(NULL, 1, 1);
690}
691
692void owl_function_prevmsg_notdeleted(void)
693{
694  owl_function_prevmsg_full(NULL, 1, 1);
695}
696
697/* if move_after is 1, moves after the delete */
698void owl_function_deletecur(int move_after)
699{
700  int curmsg;
701  owl_view *v;
702
703  v=owl_global_get_current_view(&g);
704
705  /* bail if there's no current message */
706  if (owl_view_get_size(v) < 1) {
707    owl_function_error("No current message to delete");
708    return;
709  }
710
711  /* mark the message for deletion */
712  curmsg=owl_global_get_curmsg(&g);
713  owl_view_delete_element(v, curmsg);
714
715  if (move_after) {
716    /* move the poiner in the appropriate direction
717     * to the next undeleted msg */
718    if (owl_global_get_direction(&g)==OWL_DIRECTION_UPWARDS) {
719      owl_function_prevmsg_notdeleted();
720    } else {
721      owl_function_nextmsg_notdeleted();
722    }
723  }
724}
725
726void owl_function_undeletecur(int move_after)
727{
728  int curmsg;
729  owl_view *v;
730
731  v=owl_global_get_current_view(&g);
732 
733  if (owl_view_get_size(v) < 1) {
734    owl_function_error("No current message to undelete");
735    return;
736  }
737  curmsg=owl_global_get_curmsg(&g);
738
739  owl_view_undelete_element(v, curmsg);
740
741  if (move_after) {
742    if (owl_global_get_direction(&g)==OWL_DIRECTION_UPWARDS) {
743      if (curmsg>0) {
744        owl_function_prevmsg();
745      } else {
746        owl_function_nextmsg();
747      }
748    } else {
749      owl_function_nextmsg();
750    }
751  }
752
753  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
754}
755
756void owl_function_expunge(void)
757{
758  int curmsg;
759  const owl_message *m;
760  owl_messagelist *ml;
761  owl_view *v;
762  int lastmsgid=0;
763
764  curmsg=owl_global_get_curmsg(&g);
765  v=owl_global_get_current_view(&g);
766  ml=owl_global_get_msglist(&g);
767
768  m=owl_view_get_element(v, curmsg);
769  if (m) lastmsgid = owl_message_get_id(m);
770
771  /* expunge the message list */
772  owl_messagelist_expunge(ml);
773
774  /* update all views (we only have one right now) */
775  owl_view_recalculate(v);
776
777  /* find where the new position should be
778     (as close as possible to where we last where) */
779  curmsg = owl_view_get_nearest_to_msgid(v, lastmsgid);
780  if (curmsg>owl_view_get_size(v)-1) curmsg = owl_view_get_size(v)-1;
781  if (curmsg<0) curmsg = 0;
782  owl_global_set_curmsg(&g, curmsg);
783  owl_function_calculate_topmsg(OWL_DIRECTION_NONE);
784  /* if there are no messages set the direction to down in case we
785     delete everything upwards */
786  owl_global_set_direction_downwards(&g);
787 
788  owl_function_makemsg("Messages expunged");
789  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
790}
791
792void owl_function_firstmsg(void)
793{
794  owl_global_set_curmsg(&g, 0);
795  owl_global_set_topmsg(&g, 0);
796  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
797  owl_global_set_direction_downwards(&g);
798}
799
800void owl_function_lastmsg_noredisplay(void)
801{
802  int oldcurmsg, curmsg;
803  const owl_view *v;
804
805  v=owl_global_get_current_view(&g);
806  oldcurmsg=owl_global_get_curmsg(&g);
807  curmsg=owl_view_get_size(v)-1; 
808  if (curmsg<0) curmsg=0;
809  owl_global_set_curmsg(&g, curmsg);
810  if (oldcurmsg < curmsg) {
811    owl_function_calculate_topmsg(OWL_DIRECTION_DOWNWARDS);
812  } else if (curmsg<owl_view_get_size(v)) {
813    /* If already at the end, blank the screen and move curmsg
814     * past the end of the messages. */
815    owl_global_set_topmsg(&g, curmsg+1);
816    owl_global_set_curmsg(&g, curmsg+1);
817  } 
818  /* owl_mainwin_redisplay(owl_global_get_mainwin(&g)); */
819  owl_global_set_direction_downwards(&g);
820}
821
822void owl_function_lastmsg(void)
823{
824  owl_function_lastmsg_noredisplay();
825  owl_mainwin_redisplay(owl_global_get_mainwin(&g)); 
826}
827
828void owl_function_shift_right(void)
829{
830  owl_global_set_rightshift(&g, owl_global_get_rightshift(&g)+10);
831}
832
833void owl_function_shift_left(void)
834{
835  int shift;
836
837  shift=owl_global_get_rightshift(&g);
838  if (shift > 0) {
839    owl_global_set_rightshift(&g, MAX(shift - 10, 0));
840  } else {
841    owl_function_beep();
842    owl_function_makemsg("Already full left");
843  }
844}
845
846void owl_function_unsuball(void)
847{
848  unsuball();
849  owl_function_makemsg("Unsubscribed from all messages.");
850}
851
852
853/* Load zephyr subscriptions from the named 'file' and load zephyr's
854 * default subscriptions as well.  An error message is printed if
855 * 'file' can't be opened or if zephyr reports an error in
856 * subscribing.
857 *
858 * If 'file' is NULL, this look for the default filename
859 * $HOME/.zephyr.subs.  If the file can not be opened in this case
860 * only, no error message is printed.
861 */
862void owl_function_loadsubs(const char *file)
863{
864  int ret, ret2;
865  const char *foo;
866  char *path;
867
868  if (file==NULL) {
869    ret=owl_zephyr_loadsubs(NULL, 0);
870  } else {
871    path = owl_util_makepath(file);
872    ret=owl_zephyr_loadsubs(path, 1);
873    g_free(path);
874  }
875
876  /* for backwards compatibility for now */
877  ret2=owl_zephyr_loaddefaultsubs();
878
879  if (!owl_context_is_interactive(owl_global_get_context(&g))) return;
880
881  foo=file?file:"file";
882  if (ret==0 && ret2==0) {
883    if (!file) {
884      owl_function_makemsg("Subscribed to messages.");
885    } else {
886      owl_function_makemsg("Subscribed to messages from %s", file);
887    }
888  } else if (ret==-1) {
889    owl_function_error("Could not read %s", foo);
890  } else {
891    owl_function_error("Error subscribing to messages");
892  }
893}
894
895void owl_function_loadloginsubs(const char *file)
896{
897  int ret;
898
899  ret=owl_zephyr_loadloginsubs(file);
900
901  if (!owl_context_is_interactive(owl_global_get_context(&g))) return;
902  if (ret==0) {
903  } else if (ret==-1) {
904    owl_function_error("Could not open file for login subscriptions.");
905  } else {
906    owl_function_error("Error subscribing to login messages from file.");
907  }
908}
909
910void owl_callback_aimlogin(owl_editwin *e) {
911  char *user = owl_editwin_get_cbdata(e);
912  owl_function_aimlogin(user,
913                        owl_editwin_get_text(e));
914}
915
916void owl_function_aimlogin(const char *user, const char *passwd) {
917  int ret;
918
919  /* clear the buddylist */
920  owl_buddylist_clear(owl_global_get_buddylist(&g));
921
922  /* try to login */
923  ret=owl_aim_login(user, passwd);
924  if (ret) owl_function_makemsg("Warning: login for %s failed.\n", user);
925}
926
927void owl_function_suspend(void)
928{
929  endwin();
930  printf("\n");
931  kill(getpid(), SIGSTOP);
932
933  /* resize to reinitialize all the windows when we come back */
934  owl_command_resize();
935}
936
937void owl_function_zaway_toggle(void)
938{
939  if (!owl_global_is_zaway(&g)) {
940    owl_global_set_zaway_msg(&g, owl_global_get_zaway_msg_default(&g));
941    owl_function_zaway_on();
942  } else {
943    owl_function_zaway_off();
944  }
945}
946
947void owl_function_zaway_on(void)
948{
949  owl_global_set_zaway_on(&g);
950  owl_function_makemsg("zaway set (%s)", owl_global_get_zaway_msg(&g));
951}
952
953void owl_function_zaway_off(void)
954{
955  owl_global_set_zaway_off(&g);
956  owl_function_makemsg("zaway off");
957}
958
959void owl_function_aaway_toggle(void)
960{
961  if (!owl_global_is_aaway(&g)) {
962    owl_global_set_aaway_msg(&g, owl_global_get_aaway_msg_default(&g));
963    owl_function_aaway_on();
964  } else {
965    owl_function_aaway_off();
966  }
967}
968
969void owl_function_aaway_on(void)
970{
971  owl_global_set_aaway_on(&g);
972  /* owl_aim_set_awaymsg(owl_global_get_zaway_msg(&g)); */
973  owl_function_makemsg("AIM away set (%s)", owl_global_get_aaway_msg(&g));
974}
975
976void owl_function_aaway_off(void)
977{
978  owl_global_set_aaway_off(&g);
979  /* owl_aim_set_awaymsg(""); */
980  owl_function_makemsg("AIM away off");
981}
982
983void owl_function_quit(void)
984{
985  char *ret;
986 
987  /* zlog out if we need to */
988  if (owl_global_is_havezephyr(&g) &&
989      owl_global_is_shutdownlogout(&g)) {
990    owl_zephyr_zlog_out();
991  }
992
993  /* execute the commands in shutdown */
994  ret = owl_perlconfig_execute("BarnOwl::Hooks::_shutdown();");
995  if (ret) g_free(ret);
996
997  /* signal our child process, if any */
998  if (owl_global_get_newmsgproc_pid(&g)) {
999    kill(owl_global_get_newmsgproc_pid(&g), SIGHUP);
1000  }
1001 
1002  /* Quit AIM */
1003  if (owl_global_is_aimloggedin(&g)) {
1004    owl_aim_logout();
1005  }
1006
1007  owl_function_debugmsg("Quitting Owl");
1008  owl_select_quit_loop();
1009}
1010
1011void owl_function_calculate_topmsg(int direction)
1012{
1013  int recwinlines, topmsg, curmsg;
1014  const owl_view *v;
1015
1016  v=owl_global_get_current_view(&g);
1017  curmsg=owl_global_get_curmsg(&g);
1018  topmsg=owl_global_get_topmsg(&g);
1019  recwinlines=owl_global_get_recwin_lines(&g);
1020
1021  /*
1022  if (owl_view_get_size(v) < 1) {
1023    return;
1024  }
1025  */
1026
1027  switch (owl_global_get_scrollmode(&g)) {
1028  case OWL_SCROLLMODE_TOP:
1029    topmsg = owl_function_calculate_topmsg_top(direction, v, curmsg, topmsg, recwinlines);
1030    break;
1031  case OWL_SCROLLMODE_NEARTOP:
1032    topmsg = owl_function_calculate_topmsg_neartop(direction, v, curmsg, topmsg, recwinlines);
1033    break;
1034  case OWL_SCROLLMODE_CENTER:
1035    topmsg = owl_function_calculate_topmsg_center(direction, v, curmsg, topmsg, recwinlines);
1036    break;
1037  case OWL_SCROLLMODE_PAGED:
1038    topmsg = owl_function_calculate_topmsg_paged(direction, v, curmsg, topmsg, recwinlines, 0);
1039    break;
1040  case OWL_SCROLLMODE_PAGEDCENTER:
1041    topmsg = owl_function_calculate_topmsg_paged(direction, v, curmsg, topmsg, recwinlines, 1);
1042    break;
1043  case OWL_SCROLLMODE_NORMAL:
1044  default:
1045    topmsg = owl_function_calculate_topmsg_normal(direction, v, curmsg, topmsg, recwinlines);
1046  }
1047  owl_function_debugmsg("Calculated a topmsg of %i", topmsg);
1048  owl_global_set_topmsg(&g, topmsg);
1049}
1050
1051/* Returns what the new topmsg should be. 
1052 * Passed the last direction of movement,
1053 * the current view,
1054 * the current message number in the view,
1055 * the top message currently being displayed,
1056 * and the number of lines in the recwin.
1057 */
1058int owl_function_calculate_topmsg_top(int direction, const owl_view *v, int curmsg, int topmsg, int recwinlines)
1059{
1060  return(curmsg);
1061}
1062
1063int owl_function_calculate_topmsg_neartop(int direction, const owl_view *v, int curmsg, int topmsg, int recwinlines)
1064{
1065  if (curmsg>0 
1066      && (owl_message_get_numlines(owl_view_get_element(v, curmsg-1))
1067          <  recwinlines/2)) {
1068    return(curmsg-1);
1069  } else {
1070    return(curmsg);
1071  }
1072}
1073 
1074int owl_function_calculate_topmsg_center(int direction, const owl_view *v, int curmsg, int topmsg, int recwinlines)
1075{
1076  int i, last, lines;
1077
1078  last = curmsg;
1079  lines = 0;
1080  for (i=curmsg-1; i>=0; i--) {
1081    lines += owl_message_get_numlines(owl_view_get_element(v, i));
1082    if (lines > recwinlines/2) break;
1083    last = i;
1084  }
1085  return(last);
1086}
1087 
1088int owl_function_calculate_topmsg_paged(int direction, const owl_view *v, int curmsg, int topmsg, int recwinlines, int center_on_page)
1089{
1090  int i, last, lines, savey;
1091 
1092  /* If we're off the top of the screen, scroll up such that the
1093   * curmsg is near the botton of the screen. */
1094  if (curmsg < topmsg) {
1095    last = curmsg;
1096    lines = 0;
1097    for (i=curmsg; i>=0; i--) {
1098      lines += owl_message_get_numlines(owl_view_get_element(v, i));
1099      if (lines > recwinlines) break;
1100    last = i;
1101    }
1102    if (center_on_page) {
1103      return(owl_function_calculate_topmsg_center(direction, v, curmsg, 0, recwinlines));
1104    } else {
1105      return(last);
1106    }
1107  }
1108
1109  /* Find number of lines from top to bottom of curmsg (store in savey) */
1110  savey=0;
1111  for (i=topmsg; i<=curmsg; i++) {
1112    savey+=owl_message_get_numlines(owl_view_get_element(v, i));
1113  }
1114
1115  /* if we're off the bottom of the screen, scroll down */
1116  if (savey > recwinlines) {
1117    if (center_on_page) {
1118      return(owl_function_calculate_topmsg_center(direction, v, curmsg, 0, recwinlines));
1119    } else {
1120      return(curmsg);
1121    }
1122  }
1123
1124  /* else just stay as we are... */
1125  return(topmsg);
1126}
1127
1128int owl_function_calculate_topmsg_normal(int direction, const owl_view *v, int curmsg, int topmsg, int recwinlines)
1129{
1130  int savey, i, foo, y;
1131
1132  if (curmsg<0) return(topmsg);
1133   
1134  /* If we're off the top of the screen then center */
1135  if (curmsg<topmsg) {
1136    topmsg=owl_function_calculate_topmsg_center(direction, v, curmsg, 0, recwinlines);
1137  }
1138
1139  /* If curmsg is so far past topmsg that there are more messages than
1140     lines, skip the line counting that follows because we're
1141     certainly off screen.  */
1142  savey=curmsg-topmsg;
1143  if (savey <= recwinlines) {
1144    /* Find number of lines from top to bottom of curmsg (store in savey) */
1145    savey = 0;
1146    for (i=topmsg; i<=curmsg; i++) {
1147      savey+=owl_message_get_numlines(owl_view_get_element(v, i));
1148    }
1149  }
1150
1151  /* If we're off the bottom of the screen, set the topmsg to curmsg
1152   * and scroll upwards */
1153  if (savey > recwinlines) {
1154    topmsg=curmsg;
1155    savey=owl_message_get_numlines(owl_view_get_element(v, curmsg));
1156    direction=OWL_DIRECTION_UPWARDS;
1157  }
1158 
1159  /* If our bottom line is less than 1/4 down the screen then scroll up */
1160  if (direction == OWL_DIRECTION_UPWARDS || direction == OWL_DIRECTION_NONE) {
1161    if (savey < (recwinlines / 4)) {
1162      y=0;
1163      for (i=curmsg; i>=0; i--) {
1164        foo=owl_message_get_numlines(owl_view_get_element(v, i));
1165        /* will we run the curmsg off the screen? */
1166        if ((foo+y) >= recwinlines) {
1167          i++;
1168          if (i>curmsg) i=curmsg;
1169          break;
1170        }
1171        /* have saved 1/2 the screen space? */
1172        y+=foo;
1173        if (y > (recwinlines / 2)) break;
1174      }
1175      if (i<0) i=0;
1176      return(i);
1177    }
1178  }
1179
1180  if (direction == OWL_DIRECTION_DOWNWARDS || direction == OWL_DIRECTION_NONE) {
1181    /* If curmsg bottom line is more than 3/4 down the screen then scroll down */
1182    if (savey > ((recwinlines * 3)/4)) {
1183      y=0;
1184      /* count lines from the top until we can save 1/2 the screen size */
1185      for (i=topmsg; i<curmsg; i++) {
1186        y+=owl_message_get_numlines(owl_view_get_element(v, i));
1187        if (y > (recwinlines / 2)) break;
1188      }
1189      if (i==curmsg) {
1190        i--;
1191      }
1192      return(i+1);
1193    }
1194  }
1195
1196  return(topmsg);
1197}
1198
1199void owl_function_resize(void)
1200{
1201  owl_global_set_resize_pending(&g);
1202}
1203
1204void owl_function_debugmsg(const char *fmt, ...)
1205{
1206  FILE *file;
1207  time_t now;
1208  va_list ap;
1209  va_start(ap, fmt);
1210
1211  if (!owl_global_is_debug_fast(&g))
1212    return;
1213
1214  file = owl_global_get_debug_file_handle(&g);
1215  if (!file) /* XXX should report this */
1216    return;
1217
1218  now = time(NULL);
1219
1220  fprintf(file, "[%d -  %.24s - %lds]: ",
1221          (int) getpid(), ctime(&now), now - owl_global_get_starttime(&g));
1222  vfprintf(file, fmt, ap);
1223  putc('\n', file);
1224  fflush(file);
1225
1226  va_end(ap);
1227}
1228
1229void owl_function_beep(void)
1230{
1231  if (owl_global_is_bell(&g)) {
1232    beep();
1233  }
1234}
1235
1236int owl_function_subscribe(const char *class, const char *inst, const char *recip)
1237{
1238  int ret;
1239
1240  ret=owl_zephyr_sub(class, inst, recip);
1241  if (ret) {
1242    owl_function_error("Error subscribing.");
1243  } else {
1244    owl_function_makemsg("Subscribed.");
1245  }
1246  return(ret);
1247}
1248
1249void owl_function_unsubscribe(const char *class, const char *inst, const char *recip)
1250{
1251  int ret;
1252
1253  ret=owl_zephyr_unsub(class, inst, recip);
1254  if (ret) {
1255    owl_function_error("Error subscribing.");
1256  } else {
1257    owl_function_makemsg("Unsubscribed.");
1258  }
1259}
1260
1261static void _dirty_everything(owl_window *w) {
1262  if (!owl_window_is_realized(w))
1263    return;
1264  owl_window_dirty(w);
1265  owl_window_children_foreach(w, (GFunc)_dirty_everything, NULL);
1266}
1267
1268void owl_function_full_redisplay(void)
1269{
1270  /* Ask every widget to redraw itself. */
1271  _dirty_everything(owl_window_get_screen());
1272  /* Force ncurses to redisplay everything. */
1273  clearok(stdscr, TRUE);
1274}
1275
1276void owl_function_popless_text(const char *text)
1277{
1278  owl_popwin *pw;
1279  owl_viewwin *v;
1280
1281  if (owl_global_get_popwin(&g) || owl_global_get_viewwin(&g)) {
1282    owl_function_error("Popwin already in use.");
1283    return;
1284  }
1285  pw = owl_popwin_new();
1286  owl_global_set_popwin(&g, pw);
1287  owl_popwin_up(pw);
1288
1289  v = owl_viewwin_new_text(owl_popwin_get_content(pw), text);
1290  owl_global_set_viewwin(&g, v);
1291
1292  owl_global_push_context(&g, OWL_CTX_POPLESS, v, "popless", NULL);
1293}
1294
1295void owl_function_popless_fmtext(const owl_fmtext *fm)
1296{
1297  owl_popwin *pw;
1298  owl_viewwin *v;
1299
1300  if (owl_global_get_popwin(&g) || owl_global_get_viewwin(&g)) {
1301    owl_function_error("Popwin already in use.");
1302    return;
1303  }
1304  pw = owl_popwin_new();
1305  owl_global_set_popwin(&g, pw);
1306  owl_popwin_up(pw);
1307
1308  v = owl_viewwin_new_fmtext(owl_popwin_get_content(pw), fm);
1309  owl_global_set_viewwin(&g, v);
1310
1311  owl_global_push_context(&g, OWL_CTX_POPLESS, v, "popless", NULL);
1312}
1313
1314void owl_function_popless_file(const char *filename)
1315{
1316  owl_fmtext fm;
1317  FILE *file;
1318  char *s = NULL;
1319
1320  file=fopen(filename, "r");
1321  if (!file) {
1322    owl_function_error("Could not open file: %s", filename);
1323    return;
1324  }
1325
1326  owl_fmtext_init_null(&fm);
1327  while (owl_getline(&s, file))
1328    owl_fmtext_append_normal(&fm, s);
1329  g_free(s);
1330
1331  owl_function_popless_fmtext(&fm);
1332  owl_fmtext_cleanup(&fm);
1333  fclose(file);
1334}
1335
1336void owl_function_about(void)
1337{
1338  owl_function_popless_text(
1339    "This is barnowl version " OWL_VERSION_STRING ".\n\n"
1340    "barnowl is a fork of the Owl zephyr client, written and\n"
1341    "maintained by Alejandro Sedeno and Nelson Elhage at the\n"
1342    "Massachusetts Institute of Technology. \n"
1343    "\n"
1344    "Owl was written by James Kretchmar. The first version, 0.5, was\n"
1345    "released in March 2002.\n"
1346    "\n"
1347    "The name 'owl' was chosen in reference to the owls in the\n"
1348    "Harry Potter novels, who are tasked with carrying messages\n"
1349    "between Witches and Wizards. The name 'barnowl' was chosen\n"
1350    "because we feel our owls should live closer to our ponies.\n"
1351    "\n"
1352    "Copyright (c) 2006-2011 The BarnOwl Developers. All rights reserved.\n"
1353    "Copyright (c) 2004 James Kretchmar. All rights reserved.\n"
1354    "Copyright 2002 Massachusetts Institute of Technology\n"
1355    "\n"
1356    "This program is free software. You can redistribute it and/or\n"
1357    "modify under the terms of the Sleepycat License. Use the \n"
1358    "':show license' command to display the full license\n"
1359  );
1360}
1361
1362void owl_function_info(void)
1363{
1364  const owl_message *m;
1365  owl_fmtext fm, attrfm;
1366  const owl_view *v;
1367#ifdef HAVE_LIBZEPHYR
1368  const ZNotice_t *n;
1369#endif
1370
1371  owl_fmtext_init_null(&fm);
1372 
1373  v=owl_global_get_current_view(&g);
1374  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
1375  if (!m || owl_view_get_size(v)==0) {
1376    owl_function_error("No message selected\n");
1377    return;
1378  }
1379
1380  owl_fmtext_append_bold(&fm, "General Information:\n");
1381  owl_fmtext_appendf_normal(&fm, "  Msg Id    : %i\n", owl_message_get_id(m));
1382
1383  owl_fmtext_append_normal(&fm, "  Type      : ");
1384  owl_fmtext_append_bold(&fm, owl_message_get_type(m));
1385  owl_fmtext_append_normal(&fm, "\n");
1386
1387  if (owl_message_is_direction_in(m)) {
1388    owl_fmtext_append_normal(&fm, "  Direction : in\n");
1389  } else if (owl_message_is_direction_out(m)) {
1390    owl_fmtext_append_normal(&fm, "  Direction : out\n");
1391  } else if (owl_message_is_direction_none(m)) {
1392    owl_fmtext_append_normal(&fm, "  Direction : none\n");
1393  } else {
1394    owl_fmtext_append_normal(&fm, "  Direction : unknown\n");
1395  }
1396
1397  owl_fmtext_appendf_normal(&fm, "  Time      : %s\n", owl_message_get_timestr(m));
1398
1399  if (!owl_message_is_type_admin(m)) {
1400    owl_fmtext_appendf_normal(&fm, "  Sender    : %s\n", owl_message_get_sender(m));
1401    owl_fmtext_appendf_normal(&fm, "  Recipient : %s\n", owl_message_get_recipient(m));
1402  }
1403
1404  if (owl_message_is_type_zephyr(m)) {
1405    owl_fmtext_append_bold(&fm, "\nZephyr Specific Information:\n");
1406   
1407    owl_fmtext_appendf_normal(&fm, "  Class     : %s\n", owl_message_get_class(m));
1408    owl_fmtext_appendf_normal(&fm, "  Instance  : %s\n", owl_message_get_instance(m));
1409    owl_fmtext_appendf_normal(&fm, "  Opcode    : %s\n", owl_message_get_opcode(m));
1410#ifdef HAVE_LIBZEPHYR
1411    if (owl_message_is_direction_in(m)) {
1412      char *ptr, tmpbuff[1024];
1413      int i, j, fields, len;
1414
1415      n=owl_message_get_notice(m);
1416
1417      if (!owl_message_is_pseudo(m)) {
1418        owl_fmtext_append_normal(&fm, "  Kind      : ");
1419        if (n->z_kind==UNSAFE) {
1420          owl_fmtext_append_normal(&fm, "UNSAFE\n");
1421        } else if (n->z_kind==UNACKED) {
1422          owl_fmtext_append_normal(&fm, "UNACKED\n");
1423        } else if (n->z_kind==ACKED) {
1424          owl_fmtext_append_normal(&fm, "ACKED\n");
1425        } else if (n->z_kind==HMACK) {
1426          owl_fmtext_append_normal(&fm, "HMACK\n");
1427        } else if (n->z_kind==HMCTL) {
1428          owl_fmtext_append_normal(&fm, "HMCTL\n");
1429        } else if (n->z_kind==SERVACK) {
1430          owl_fmtext_append_normal(&fm, "SERVACK\n");
1431        } else if (n->z_kind==SERVNAK) {
1432          owl_fmtext_append_normal(&fm, "SERVNACK\n");
1433        } else if (n->z_kind==CLIENTACK) {
1434          owl_fmtext_append_normal(&fm, "CLIENTACK\n");
1435        } else if (n->z_kind==STAT) {
1436          owl_fmtext_append_normal(&fm, "STAT\n");
1437        } else {
1438          owl_fmtext_append_normal(&fm, "ILLEGAL VALUE\n");
1439        }
1440      }
1441      owl_fmtext_appendf_normal(&fm, "  Host      : %s\n", owl_message_get_hostname(m));
1442
1443      if (!owl_message_is_pseudo(m)) {
1444        owl_fmtext_append_normal(&fm, "\n");
1445        owl_fmtext_appendf_normal(&fm, "  Port      : %i\n", ntohs(n->z_port));
1446        owl_fmtext_appendf_normal(&fm, "  Auth      : %s\n", owl_zephyr_get_authstr(n));
1447
1448        /* FIXME make these more descriptive */
1449        owl_fmtext_appendf_normal(&fm, "  Checkd Ath: %i\n", n->z_checked_auth);
1450        owl_fmtext_appendf_normal(&fm, "  Multi notc: %s\n", n->z_multinotice);
1451        owl_fmtext_appendf_normal(&fm, "  Num other : %i\n", n->z_num_other_fields);
1452        owl_fmtext_appendf_normal(&fm, "  Msg Len   : %i\n", n->z_message_len);
1453
1454        fields=owl_zephyr_get_num_fields(n);
1455        owl_fmtext_appendf_normal(&fm, "  Fields    : %i\n", fields);
1456
1457        for (i=0; i<fields; i++) {
1458          ptr=owl_zephyr_get_field_as_utf8(n, i+1);
1459          len=strlen(ptr);
1460          if (len<30) {
1461            strncpy(tmpbuff, ptr, len);
1462            tmpbuff[len]='\0';
1463          } else {
1464            strncpy(tmpbuff, ptr, 30);
1465            tmpbuff[30]='\0';
1466            strcat(tmpbuff, "...");
1467          }
1468          g_free(ptr);
1469
1470          for (j=0; j<strlen(tmpbuff); j++) {
1471            if (tmpbuff[j]=='\n') tmpbuff[j]='~';
1472            if (tmpbuff[j]=='\r') tmpbuff[j]='!';
1473          }
1474
1475          owl_fmtext_appendf_normal(&fm, "  Field %i   : %s\n", i+1, tmpbuff);
1476        }
1477        owl_fmtext_appendf_normal(&fm, "  Default Fm: %s\n", n->z_default_format);
1478      }
1479
1480    }
1481#endif
1482  }
1483
1484  owl_fmtext_append_bold(&fm, "\nOwl Message Attributes:\n");
1485  owl_message_attributes_tofmtext(m, &attrfm);
1486  owl_fmtext_append_fmtext(&fm, &attrfm);
1487 
1488  owl_function_popless_fmtext(&fm);
1489  owl_fmtext_cleanup(&fm);
1490  owl_fmtext_cleanup(&attrfm);
1491}
1492
1493/* print the current message in a popup window.
1494 * Use the 'default' style regardless of whatever
1495 * style the user may be using
1496 */
1497void owl_function_curmsg_to_popwin(void)
1498{
1499  const owl_view *v;
1500  const owl_message *m;
1501  const owl_style *s;
1502  owl_fmtext fm;
1503
1504  v=owl_global_get_current_view(&g);
1505  s=owl_global_get_style_by_name(&g, "default");
1506
1507  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
1508
1509  if (!m || owl_view_get_size(v)==0) {
1510    owl_function_error("No current message");
1511    return;
1512  }
1513
1514  owl_fmtext_init_null(&fm);
1515  owl_style_get_formattext(s, &fm, m);
1516
1517  owl_function_popless_fmtext(&fm);
1518  owl_fmtext_cleanup(&fm);
1519}
1520
1521void owl_function_page_curmsg(int step)
1522{
1523  /* scroll down or up within the current message IF the message is truncated */
1524
1525  int offset, curmsg, lines;
1526  const owl_view *v;
1527  owl_message *m;
1528
1529  offset=owl_global_get_curmsg_vert_offset(&g);
1530  v=owl_global_get_current_view(&g);
1531  curmsg=owl_global_get_curmsg(&g);
1532  m=owl_view_get_element(v, curmsg);
1533  if (!m || owl_view_get_size(v)==0) return;
1534  lines=owl_message_get_numlines(m);
1535
1536  if (offset==0) {
1537    /* Bail if the curmsg isn't the last one displayed */
1538    if (curmsg != owl_mainwin_get_last_msg(owl_global_get_mainwin(&g))) {
1539      owl_function_makemsg("The entire message is already displayed");
1540      return;
1541    }
1542   
1543    /* Bail if we're not truncated */
1544    if (!owl_mainwin_is_curmsg_truncated(owl_global_get_mainwin(&g))) {
1545      owl_function_makemsg("The entire message is already displayed");
1546      return;
1547    }
1548  }
1549 
1550 
1551  /* don't scroll past the last line */
1552  if (step>0) {
1553    if (offset+step > lines-1) {
1554      owl_global_set_curmsg_vert_offset(&g, lines-1);
1555    } else {
1556      owl_global_set_curmsg_vert_offset(&g, offset+step);
1557    }
1558  }
1559
1560  /* would we be before the beginning of the message? */
1561  if (step<0) {
1562    if (offset+step<0) {
1563      owl_global_set_curmsg_vert_offset(&g, 0);
1564    } else {
1565      owl_global_set_curmsg_vert_offset(&g, offset+step);
1566    }
1567  }
1568 
1569  /* redisplay */
1570  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
1571}
1572
1573void owl_function_resize_typwin(int newsize)
1574{
1575  owl_global_set_typwin_lines(&g, newsize);
1576  owl_mainpanel_layout_contents(&g.mainpanel);
1577}
1578
1579void owl_function_mainwin_pagedown(void)
1580{
1581  int i;
1582
1583  i=owl_mainwin_get_last_msg(owl_global_get_mainwin(&g));
1584  if (i<0) return;
1585  if (owl_mainwin_is_last_msg_truncated(owl_global_get_mainwin(&g))
1586      && (owl_global_get_curmsg(&g) < i)
1587      && (i>0)) {
1588    i--;
1589  }
1590  owl_global_set_curmsg(&g, i);
1591  owl_function_nextmsg();
1592}
1593
1594void owl_function_mainwin_pageup(void)
1595{
1596  owl_global_set_curmsg(&g, owl_global_get_topmsg(&g));
1597  owl_function_prevmsg();
1598}
1599
1600void owl_function_getsubs(void)
1601{
1602  char *buff;
1603
1604  buff=owl_zephyr_getsubs();
1605
1606  if (buff) {
1607    owl_function_popless_text(buff);
1608  } else {
1609    owl_function_popless_text("Error getting subscriptions");
1610  }
1611           
1612  g_free(buff);
1613}
1614
1615void owl_function_printallvars(void)
1616{
1617  const char *name;
1618  char *var;
1619  owl_list varnames;
1620  int i, numvarnames;
1621  GString *str   = g_string_new("");
1622
1623  g_string_append_printf(str, "%-20s = %s\n", "VARIABLE", "VALUE");
1624  g_string_append_printf(str, "%-20s   %s\n",  "--------", "-----");
1625  owl_variable_dict_get_names(owl_global_get_vardict(&g), &varnames);
1626  numvarnames = owl_list_get_size(&varnames);
1627  for (i=0; i<numvarnames; i++) {
1628    name = owl_list_get_element(&varnames, i);
1629    if (name && name[0]!='_') {
1630      g_string_append_printf(str, "\n%-20s = ", name);
1631      var = owl_variable_get_tostring(owl_global_get_vardict(&g), name);
1632      if (var) {
1633        g_string_append(str, var);
1634        g_free(var);
1635      }
1636    }
1637  }
1638  g_string_append(str, "\n");
1639  owl_variable_dict_namelist_cleanup(&varnames);
1640
1641  owl_function_popless_text(str->str);
1642  g_string_free(str, true);
1643}
1644
1645void owl_function_show_variables(void)
1646{
1647  owl_list varnames;
1648  owl_fmtext fm; 
1649  int i, numvarnames;
1650  const char *varname;
1651
1652  owl_fmtext_init_null(&fm);
1653  owl_fmtext_append_bold(&fm, 
1654      "Variables: (use 'show variable <name>' for details)\n");
1655  owl_variable_dict_get_names(owl_global_get_vardict(&g), &varnames);
1656  numvarnames = owl_list_get_size(&varnames);
1657  for (i=0; i<numvarnames; i++) {
1658    varname = owl_list_get_element(&varnames, i);
1659    if (varname && varname[0]!='_') {
1660      owl_variable_describe(owl_global_get_vardict(&g), varname, &fm);
1661    }
1662  }
1663  owl_variable_dict_namelist_cleanup(&varnames);
1664  owl_function_popless_fmtext(&fm);
1665  owl_fmtext_cleanup(&fm);
1666}
1667
1668void owl_function_show_variable(const char *name)
1669{
1670  owl_fmtext fm; 
1671
1672  owl_fmtext_init_null(&fm);
1673  owl_variable_get_help(owl_global_get_vardict(&g), name, &fm);
1674  owl_function_popless_fmtext(&fm);
1675  owl_fmtext_cleanup(&fm);
1676}
1677
1678/* note: this applies to global message list, not to view.
1679 * If flag is 1, deletes.  If flag is 0, undeletes. */
1680void owl_function_delete_by_id(int id, int flag)
1681{
1682  const owl_messagelist *ml;
1683  owl_message *m;
1684  ml = owl_global_get_msglist(&g);
1685  m = owl_messagelist_get_by_id(ml, id);
1686  if (m) {
1687    if (flag == 1) {
1688      owl_message_mark_delete(m);
1689    } else if (flag == 0) {
1690      owl_message_unmark_delete(m);
1691    }
1692    owl_mainwin_redisplay(owl_global_get_mainwin(&g));
1693  } else {
1694    owl_function_error("No message with id %d: unable to mark for (un)delete",id);
1695  }
1696}
1697
1698void owl_function_delete_automsgs(void)
1699{
1700  /* mark for deletion all messages in the current view that match the
1701   * 'trash' filter */
1702
1703  int i, j, count;
1704  owl_message *m;
1705  const owl_view *v;
1706  const owl_filter *f;
1707
1708  /* get the trash filter */
1709  f=owl_global_get_filter(&g, "trash");
1710  if (!f) {
1711    owl_function_error("No trash filter defined");
1712    return;
1713  }
1714
1715  v=owl_global_get_current_view(&g);
1716
1717  count=0;
1718  j=owl_view_get_size(v);
1719  for (i=0; i<j; i++) {
1720    m=owl_view_get_element(v, i);
1721    if (owl_filter_message_match(f, m)) {
1722      count++;
1723      owl_message_mark_delete(m);
1724    }
1725  }
1726  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
1727  owl_function_makemsg("%i messages marked for deletion", count);
1728}
1729
1730void owl_function_status(void)
1731{
1732  char buff[MAXPATHLEN+1];
1733  time_t start;
1734  int up, days, hours, minutes;
1735  owl_fmtext fm;
1736
1737  owl_fmtext_init_null(&fm);
1738
1739  start=owl_global_get_starttime(&g);
1740
1741  owl_fmtext_append_normal(&fm, "General Information:\n");
1742
1743  owl_fmtext_append_normal(&fm, "  Version: ");
1744  owl_fmtext_append_normal(&fm, OWL_VERSION_STRING);
1745  owl_fmtext_append_normal(&fm, "\n");
1746
1747  owl_fmtext_append_normal(&fm, "  Startup Arguments: ");
1748  owl_fmtext_append_normal(&fm, owl_global_get_startupargs(&g));
1749  owl_fmtext_append_normal(&fm, "\n");
1750
1751  owl_fmtext_append_normal(&fm, "  Current Directory: ");
1752  if(getcwd(buff, MAXPATHLEN) == NULL) {
1753    owl_fmtext_append_normal(&fm, "<Error in getcwd>");
1754  } else {
1755    owl_fmtext_append_normal(&fm, buff);
1756  }
1757  owl_fmtext_append_normal(&fm, "\n");
1758
1759  owl_fmtext_appendf_normal(&fm, "  Startup Time: %s", ctime(&start));
1760
1761  up=owl_global_get_runtime(&g);
1762  days=up/86400;
1763  up-=days*86400;
1764  hours=up/3600;
1765  up-=hours*3600;
1766  minutes=up/60;
1767  up-=minutes*60;
1768  owl_fmtext_appendf_normal(&fm, "  Run Time: %i days %2.2i:%2.2i:%2.2i\n", days, hours, minutes, up);
1769
1770  owl_fmtext_append_normal(&fm, "\nProtocol Options:\n");
1771  owl_fmtext_append_normal(&fm, "  Zephyr included    : ");
1772  if (owl_global_is_havezephyr(&g)) {
1773    owl_fmtext_append_normal(&fm, "yes\n");
1774  } else {
1775    owl_fmtext_append_normal(&fm, "no\n");
1776  }
1777  owl_fmtext_append_normal(&fm, "  AIM included       : yes\n");
1778  owl_fmtext_append_normal(&fm, "  Loopback included  : yes\n");
1779
1780
1781  owl_fmtext_append_normal(&fm, "\nBuild Options:\n");
1782  owl_fmtext_append_normal(&fm, "  Stderr redirection : ");
1783#if OWL_STDERR_REDIR
1784  owl_fmtext_append_normal(&fm, "yes\n");
1785#else
1786  owl_fmtext_append_normal(&fm, "no\n");
1787#endif
1788 
1789
1790  owl_fmtext_append_normal(&fm, "\nAIM Status:\n");
1791  owl_fmtext_append_normal(&fm, "  Logged in: ");
1792  if (owl_global_is_aimloggedin(&g)) {
1793    owl_fmtext_append_normal(&fm, owl_global_get_aim_screenname(&g));
1794    owl_fmtext_append_normal(&fm, "\n");
1795  } else {
1796    owl_fmtext_append_normal(&fm, "(not logged in)\n");
1797  }
1798
1799  owl_fmtext_append_normal(&fm, "  Processing events: ");
1800  if (owl_global_is_doaimevents(&g)) {
1801    owl_fmtext_append_normal(&fm, "yes\n");
1802  } else {
1803    owl_fmtext_append_normal(&fm, "no\n");
1804  }
1805
1806  owl_function_popless_fmtext(&fm);
1807  owl_fmtext_cleanup(&fm);
1808}
1809
1810void owl_function_show_term(void)
1811{
1812  owl_fmtext fm;
1813
1814  owl_fmtext_init_null(&fm);
1815  owl_fmtext_appendf_normal(&fm, "Terminal Lines: %i\nTerminal Columns: %i\n",
1816          owl_global_get_lines(&g),
1817          owl_global_get_cols(&g));
1818
1819  if (owl_global_get_hascolors(&g)) {
1820    owl_fmtext_append_normal(&fm, "Color: Yes\n");
1821    owl_fmtext_appendf_normal(&fm, "Number of color pairs: %i\n", owl_global_get_colorpairs(&g));
1822    owl_fmtext_appendf_normal(&fm, "Can change colors: %s\n", can_change_color() ? "yes" : "no");
1823  } else {
1824    owl_fmtext_append_normal(&fm, "Color: No\n");
1825  }
1826
1827  owl_function_popless_fmtext(&fm);
1828  owl_fmtext_cleanup(&fm);
1829}
1830
1831/* if type = 0 then normal reply.
1832 * if type = 1 then it's a reply to sender
1833 * if enter = 0 then allow the command to be edited
1834 * if enter = 1 then don't wait for editing
1835 */
1836void owl_function_reply(int type, int enter)
1837{
1838  char *buff=NULL;
1839  const owl_message *m;
1840  const owl_filter *f;
1841 
1842  if (owl_view_get_size(owl_global_get_current_view(&g))==0) {
1843    owl_function_error("No message selected");
1844  } else {
1845    char *cmd;
1846   
1847    m=owl_view_get_element(owl_global_get_current_view(&g), owl_global_get_curmsg(&g));
1848    if (!m) {
1849      owl_function_error("No message selected");
1850      return;
1851    }
1852
1853    /* first check if we catch the reply-lockout filter */
1854    f=owl_global_get_filter(&g, "reply-lockout");
1855    if (f) {
1856      if (owl_filter_message_match(f, m)) {
1857        owl_function_error("Sorry, replies to this message have been disabled by the reply-lockout filter");
1858        return;
1859      }
1860    }
1861
1862    /* then check if it's a question and just bring up the command prompt */
1863    if (owl_message_is_question(m)) {
1864      owl_function_start_command("");
1865      return;
1866    }
1867
1868    if((type == 0 &&
1869        (cmd=owl_perlconfig_message_call_method(m, "replycmd", 0, NULL))) ||
1870       (type == 1 &&
1871        (cmd=owl_perlconfig_message_call_method(m, "replysendercmd", 0, NULL)))) {
1872      buff = cmd;
1873    }
1874
1875    if(!buff) {
1876        owl_function_error("I don't know how to reply to that message.");
1877        return;
1878    }
1879
1880    if (enter) {
1881      owl_history *hist = owl_global_get_cmd_history(&g);
1882      owl_history_store(hist, buff);
1883      owl_history_reset(hist);
1884      owl_function_command_norv(buff);
1885    } else {
1886      owl_function_start_command(buff);
1887    }
1888    g_free(buff);
1889  }
1890}
1891
1892void owl_function_zlocate(int argc, const char *const *argv, int auth)
1893{
1894  owl_fmtext fm;
1895  char *ptr;
1896  char *result;
1897  int i;
1898
1899  owl_fmtext_init_null(&fm);
1900
1901  for (i=0; i<argc; i++) {
1902    ptr = long_zuser(argv[i]);
1903    result = owl_zephyr_zlocate(ptr, auth);
1904    owl_fmtext_append_normal(&fm, result);
1905    g_free(result);
1906    g_free(ptr);
1907  }
1908
1909  owl_function_popless_fmtext(&fm);
1910  owl_fmtext_cleanup(&fm);
1911}
1912
1913void owl_callback_command(owl_editwin *e)
1914{
1915  char *rv;
1916  const char *line = owl_editwin_get_text(e);
1917
1918  rv = owl_function_command(line);
1919   if (rv) {
1920    owl_function_makemsg("%s", rv);
1921    g_free(rv);
1922  }
1923}
1924
1925void owl_function_start_command(const char *line)
1926{
1927  owl_editwin *tw;
1928  owl_context *ctx;
1929
1930  tw = owl_global_set_typwin_active(&g, OWL_EDITWIN_STYLE_ONELINE, owl_global_get_cmd_history(&g));
1931
1932  owl_editwin_set_locktext(tw, "command: ");
1933
1934  owl_editwin_insert_string(tw, line);
1935
1936  ctx = owl_editcontext_new(OWL_CTX_EDITLINE, tw, "editline",
1937                            owl_global_deactivate_editcontext, &g);
1938  owl_global_push_context_obj(&g, ctx);
1939  owl_editwin_set_callback(tw, owl_callback_command);
1940}
1941
1942owl_editwin *owl_function_start_question(const char *line)
1943{
1944  owl_editwin *tw;
1945  owl_context *ctx;
1946
1947  tw = owl_global_set_typwin_active(&g, OWL_EDITWIN_STYLE_ONELINE, owl_global_get_cmd_history(&g));
1948
1949  owl_editwin_set_locktext(tw, line);
1950
1951  ctx = owl_editcontext_new(OWL_CTX_EDITRESPONSE, tw, "editresponse",
1952                            owl_global_deactivate_editcontext, &g);
1953  owl_global_push_context_obj(&g, ctx);
1954  return tw;
1955}
1956
1957owl_editwin *owl_function_start_password(const char *line)
1958{
1959  owl_editwin *tw;
1960  owl_context *ctx;
1961
1962  tw = owl_global_set_typwin_active(&g, OWL_EDITWIN_STYLE_ONELINE, NULL);
1963
1964  owl_editwin_set_echochar(tw, '*');
1965
1966  owl_editwin_set_locktext(tw, line);
1967
1968  ctx = owl_editcontext_new(OWL_CTX_EDITRESPONSE, tw, "editresponse",
1969                            owl_global_deactivate_editcontext, &g);
1970  owl_global_push_context_obj(&g, ctx);
1971  return tw;
1972}
1973
1974char *owl_function_exec(int argc, const char *const *argv, const char *buff, int type)
1975{
1976  /* if type == 1 display in a popup
1977   * if type == 2 display an admin messages
1978   * if type == 0 return output
1979   * else display in a popup
1980   */
1981  const char *redirect = " 2>&1 < /dev/null";
1982  char *newbuff;
1983  char *out;
1984  FILE *p;
1985
1986#if OWL_STDERR_REDIR
1987  redirect = " < /dev/null";
1988#endif
1989
1990  if (argc<2) {
1991    owl_function_error("Wrong number of arguments to the exec command");
1992    return NULL;
1993  }
1994
1995  buff = skiptokens(buff, 1);
1996  newbuff = g_strdup_printf("%s%s", buff, redirect);
1997
1998  if (type == OWL_OUTPUT_POPUP) {
1999    owl_popexec_new(newbuff);
2000  } else {
2001    p = popen(newbuff, "r");
2002    out = owl_slurp(p);
2003    pclose(p);
2004   
2005    if (type == OWL_OUTPUT_RETURN) {
2006      g_free(newbuff);
2007      return out;
2008    } else if (type == OWL_OUTPUT_ADMINMSG) {
2009      owl_function_adminmsg(buff, out);
2010    }
2011    g_free(out);
2012  }
2013  g_free(newbuff);
2014  return NULL;
2015}
2016
2017char *owl_function_perl(int argc, const char *const *argv, const char *buff, int type)
2018{
2019  /* if type == 1 display in a popup
2020   * if type == 2 display an admin messages
2021   * if type == 0 return output
2022   * else display in a popup
2023   */
2024  char *perlout;
2025
2026  if (argc<2) {
2027    owl_function_error("Wrong number of arguments to perl command");
2028    return NULL;
2029  }
2030
2031  /* consume first token (argv[0]) */
2032  buff = skiptokens(buff, 1);
2033
2034  perlout = owl_perlconfig_execute(buff);
2035  if (perlout) { 
2036    if (type == OWL_OUTPUT_POPUP) {
2037      owl_function_popless_text(perlout);
2038    } else if (type == OWL_OUTPUT_ADMINMSG) {
2039      owl_function_adminmsg(buff, perlout);
2040    } else if (type == OWL_OUTPUT_RETURN) {
2041      return perlout;
2042    }
2043    g_free(perlout);
2044  }
2045  return NULL;
2046}
2047
2048/* Change the filter associated with the current view.
2049 * This also figures out which message in the new filter
2050 * should have the pointer.
2051 */
2052void owl_function_change_currentview_filter(const char *filtname)
2053{
2054  owl_view *v;
2055  owl_filter *f;
2056  int curid=-1, newpos, curmsg;
2057  const owl_message *curm=NULL;
2058
2059  v=owl_global_get_current_view(&g);
2060
2061  curmsg=owl_global_get_curmsg(&g);
2062  if (curmsg==-1) {
2063    owl_function_debugmsg("Hit the curmsg==-1 case in change_view");
2064  } else {
2065    curm=owl_view_get_element(v, curmsg);
2066    if (curm) {
2067      curid=owl_message_get_id(curm);
2068      owl_view_save_curmsgid(v, curid);
2069    }
2070  }
2071
2072  f=owl_global_get_filter(&g, filtname);
2073  if (!f) {
2074    owl_function_error("Unknown filter %s", filtname);
2075    return;
2076  }
2077
2078  owl_view_new_filter(v, f);
2079
2080  /* Figure out what to set the current message to.
2081   * - If the view we're leaving has messages in it, go to the closest message
2082   *   to the last message pointed to in that view.
2083   * - If the view we're leaving is empty, try to restore the position
2084   *   from the last time we were in the new view.  */
2085  if (curm) {
2086    newpos = owl_view_get_nearest_to_msgid(v, curid);
2087  } else {
2088    newpos = owl_view_get_nearest_to_saved(v);
2089  }
2090
2091  owl_global_set_curmsg(&g, newpos);
2092  owl_function_calculate_topmsg(OWL_DIRECTION_DOWNWARDS);
2093  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2094  owl_global_set_direction_downwards(&g);
2095}
2096
2097/* Create a new filter, or replace an existing one
2098 * with a new definition.
2099 */
2100void owl_function_create_filter(int argc, const char *const *argv)
2101{
2102  owl_filter *f;
2103  const owl_view *v;
2104  int inuse = 0;
2105
2106  if (argc < 2) {
2107    owl_function_error("Wrong number of arguments to filter command");
2108    return;
2109  }
2110
2111  owl_function_debugmsg("owl_function_create_filter: starting to create filter named %s", argv[1]);
2112
2113  v=owl_global_get_current_view(&g);
2114
2115  /* don't touch the all filter */
2116  if (!strcmp(argv[1], "all")) {
2117    owl_function_error("You may not change the 'all' filter.");
2118    return;
2119  }
2120
2121  /* deal with the case of trying change the filter color */
2122  if (argc==4 && !strcmp(argv[2], "-c")) {
2123    f=owl_global_get_filter(&g, argv[1]);
2124    if (!f) {
2125      owl_function_error("The filter '%s' does not exist.", argv[1]);
2126      return;
2127    }
2128    if (owl_util_string_to_color(argv[3])==OWL_COLOR_INVALID) {
2129      owl_function_error("The color '%s' is not available.", argv[3]);
2130      return;
2131    }
2132    owl_filter_set_fgcolor(f, owl_util_string_to_color(argv[3]));
2133    owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2134    return;
2135  }
2136  if (argc==4 && !strcmp(argv[2], "-b")) {
2137    f=owl_global_get_filter(&g, argv[1]);
2138    if (!f) {
2139      owl_function_error("The filter '%s' does not exist.", argv[1]);
2140      return;
2141    }
2142    if (owl_util_string_to_color(argv[3])==OWL_COLOR_INVALID) {
2143      owl_function_error("The color '%s' is not available.", argv[3]);
2144      return;
2145    }
2146    owl_filter_set_bgcolor(f, owl_util_string_to_color(argv[3]));
2147    owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2148    return;
2149  }
2150
2151  /* create the filter and check for errors */
2152  f = owl_filter_new(argv[1], argc-2, argv+2);
2153  if (f == NULL) {
2154    owl_function_error("Invalid filter");
2155    return;
2156  }
2157
2158  /* if the named filter is in use by the current view, remember it */
2159  if (!strcmp(owl_view_get_filtname(v), argv[1])) {
2160    inuse=1;
2161  }
2162
2163  /* if the named filter already exists, nuke it */
2164  if (owl_global_get_filter(&g, argv[1])) {
2165    owl_global_remove_filter(&g, argv[1]);
2166  }
2167
2168  /* add the filter */
2169  owl_global_add_filter(&g, f);
2170
2171  /* if it was in use by the current view then update */
2172  if (inuse) {
2173    owl_function_change_currentview_filter(argv[1]);
2174  }
2175  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2176}
2177
2178/* If 'filtername' does not start with 'not-' create a filter named
2179 * 'not-<filtername>' defined as "not filter <filtername>".  If the
2180 * filter 'not-<filtername>' already exists, do not overwrite it.  If
2181 * 'filtername' begins with 'not-' and a filter 'filtername' already
2182 * exists, then do nothing.  If the filter 'filtername' does not
2183 * exist, create it and define it as 'not filter <filtername>'
2184 *
2185 * Returns the name of the negated filter, which the caller must free.
2186 */
2187char *owl_function_create_negative_filter(const char *filtername)
2188{
2189  char *newname;
2190  const owl_filter *tmpfilt;
2191  const char *argv[5];
2192
2193  owl_function_debugmsg("owl_function_create_negative_filter");
2194 
2195  if (!strncmp(filtername, "not-", 4)) {
2196    newname=g_strdup(filtername+4);
2197  } else {
2198    newname=g_strdup_printf("not-%s", filtername);
2199  }
2200
2201  tmpfilt=owl_global_get_filter(&g, newname);
2202  if (!tmpfilt) {
2203    argv[0]="filter"; /* anything is fine here */
2204    argv[1]=newname;
2205    argv[2]="not";
2206    argv[3]="filter";
2207    argv[4]=filtername;
2208    owl_function_create_filter(5, argv);
2209  }
2210
2211  owl_function_debugmsg("owl_function_create_negative_filter: returning with %s", newname);
2212  return(newname);
2213}
2214
2215void owl_function_show_filters(void)
2216{
2217  const owl_filter *f;
2218  GList *fl;
2219  owl_fmtext fm;
2220
2221  owl_fmtext_init_null(&fm);
2222
2223  owl_fmtext_append_bold(&fm, "Filters:\n");
2224
2225  for (fl = g.filterlist; fl; fl = g_list_next(fl)) {
2226    f = fl->data;
2227    owl_fmtext_append_normal(&fm, "   ");
2228    if (owl_global_get_hascolors(&g)) {
2229      owl_fmtext_append_normal_color(&fm, owl_filter_get_name(f), owl_filter_get_fgcolor(f), owl_filter_get_bgcolor(f));
2230    } else {
2231      owl_fmtext_append_normal(&fm, owl_filter_get_name(f));
2232    }
2233    owl_fmtext_append_normal(&fm, "\n");
2234  }
2235  owl_function_popless_fmtext(&fm);
2236  owl_fmtext_cleanup(&fm);
2237}
2238
2239void owl_function_show_filter(const char *name)
2240{
2241  const owl_filter *f;
2242  char *buff, *tmp;
2243
2244  f=owl_global_get_filter(&g, name);
2245  if (!f) {
2246    owl_function_error("There is no filter named %s", name);
2247    return;
2248  }
2249  tmp = owl_filter_print(f);
2250  buff = g_strdup_printf("%s: %s", owl_filter_get_name(f), tmp);
2251  owl_function_popless_text(buff);
2252  g_free(buff);
2253  g_free(tmp);
2254}
2255
2256void owl_function_show_zpunts(void)
2257{
2258  const owl_filter *f;
2259  const owl_list *fl;
2260  char buff[5000];
2261  char *tmp;
2262  owl_fmtext fm;
2263  int i, j;
2264
2265  owl_fmtext_init_null(&fm);
2266
2267  fl=owl_global_get_puntlist(&g);
2268  j=owl_list_get_size(fl);
2269  owl_fmtext_append_bold(&fm, "Active zpunt filters:\n");
2270
2271  for (i=0; i<j; i++) {
2272    f=owl_list_get_element(fl, i);
2273    snprintf(buff, sizeof(buff), "[% 2d] ", i+1);
2274    owl_fmtext_append_normal(&fm, buff);
2275    tmp = owl_filter_print(f);
2276    owl_fmtext_append_normal(&fm, tmp);
2277    g_free(tmp);
2278  }
2279  owl_function_popless_fmtext(&fm);
2280  owl_fmtext_cleanup(&fm);
2281}
2282
2283/* Create a filter for a class, instance if one doesn't exist.  If
2284 * instance is NULL then catch all messgaes in the class.  Returns the
2285 * name of the filter or null.  The caller must free this name.
2286 * If 'related' is nonzero, encompass unclasses and .d classes as well.
2287 */
2288char *owl_function_classinstfilt(const char *c, const char *i, int related) 
2289{
2290  owl_filter *f;
2291  char *filtname;
2292  char *tmpclass, *tmpinstance = NULL;
2293  char *class, *instance = NULL;
2294  GString *buf;
2295
2296  if (related) {
2297    class = owl_util_baseclass(c);
2298    if (i) {
2299      instance = owl_util_baseclass(i);
2300    }
2301  } else {
2302    class = g_strdup(c);
2303    if (i) {
2304      instance = g_strdup(i);
2305    }
2306  }
2307
2308  /* name for the filter */
2309  if (!instance) {
2310    filtname = g_strdup_printf("%sclass-%s", related ? "related-" : "", class);
2311  } else {
2312    filtname = g_strdup_printf("%sclass-%s-instance-%s", related ? "related-" : "", class, instance);
2313  }
2314  /* downcase it */
2315  {
2316    char *temp = g_utf8_strdown(filtname, -1);
2317    if (temp) {
2318      g_free(filtname);
2319      filtname = temp;
2320    }
2321  }
2322 
2323  /* if it already exists then go with it.  This lets users override */
2324  if (owl_global_get_filter(&g, filtname)) {
2325    goto done;
2326  }
2327
2328  /* create the new filter */
2329  tmpclass=owl_text_quote(class, OWL_REGEX_QUOTECHARS, OWL_REGEX_QUOTEWITH);
2330  if (instance) {
2331    tmpinstance=owl_text_quote(instance, OWL_REGEX_QUOTECHARS, OWL_REGEX_QUOTEWITH);
2332  }
2333
2334  buf = g_string_new("");
2335  owl_string_appendf_quoted(buf,
2336                            related ? "class ^(un)*%q(\\.d)*$" : "class ^%q$",
2337                            tmpclass);
2338
2339  if (tmpinstance) {
2340    owl_string_appendf_quoted(buf,
2341                              related ?
2342                              " and ( instance ^(un)*%q(\\.d)*$ )" :
2343                              " and instance ^%q$",
2344                              tmpinstance);
2345  }
2346  g_free(tmpclass);
2347  g_free(tmpinstance);
2348
2349  f = owl_filter_new_fromstring(filtname, buf->str);
2350  g_string_free(buf, true);
2351  if (f == NULL) {
2352    /* Couldn't make a filter for some reason. Return NULL. */
2353    owl_function_error("Error creating filter '%s'", filtname);
2354    g_free(filtname);
2355    filtname = NULL;
2356    goto done;
2357  }
2358
2359  /* add it to the global list */
2360  owl_global_add_filter(&g, f);
2361
2362done:
2363  g_free(class);
2364  if (instance) {
2365    g_free(instance);
2366  }
2367  return(filtname);
2368}
2369
2370/* Create a filter for personal zephyrs to or from the specified
2371 * zephyr user.  Includes login/logout notifications for the user.
2372 * The name of the filter will be 'user-<shortuser>'.  If a filter already
2373 * exists with this name, no new filter will be created.  This allows
2374 * the configuration to override this function.  Returns the name of
2375 * the filter, which the caller must free.
2376 */
2377char *owl_function_zuserfilt(const char *longuser)
2378{
2379  owl_filter *f;
2380  char *argbuff, *esclonguser, *shortuser, *filtname;
2381
2382  /* name for the filter */
2383  shortuser = short_zuser(longuser);
2384  filtname = g_strdup_printf("user-%s", shortuser);
2385  g_free(shortuser);
2386
2387  /* if it already exists then go with it.  This lets users override */
2388  if (owl_global_get_filter(&g, filtname)) {
2389    return filtname;
2390  }
2391
2392  /* create the new-internal filter */
2393  esclonguser = owl_text_quote(longuser, OWL_REGEX_QUOTECHARS, OWL_REGEX_QUOTEWITH);
2394
2395  argbuff=owl_string_build_quoted("( type ^zephyr$ and filter personal and "
2396      "( ( direction ^in$ and sender ^%q$ ) or ( direction ^out$ and "
2397      "recipient ^%q$ ) ) ) or ( ( class ^login$ ) and ( sender ^%q$ ) )",
2398      esclonguser, esclonguser, esclonguser);
2399  g_free(esclonguser);
2400
2401  f = owl_filter_new_fromstring(filtname, argbuff);
2402  g_free(argbuff);
2403
2404  if (f == NULL) {
2405    /* Couldn't make a filter for some reason. Return NULL. */
2406    owl_function_error("Error creating filter '%s'", filtname);
2407    g_free(filtname);
2408    return NULL;
2409  }
2410
2411  /* add it to the global list */
2412  owl_global_add_filter(&g, f);
2413
2414  return(filtname);
2415}
2416
2417/* Create a filter for AIM IM messages to or from the specified
2418 * screenname.  The name of the filter will be 'aimuser-<user>'.  If a
2419 * filter already exists with this name, no new filter will be
2420 * created.  This allows the configuration to override this function.
2421 * Returns the name of the filter, which the caller must free.
2422 */
2423char *owl_function_aimuserfilt(const char *user)
2424{
2425  owl_filter *f;
2426  char *argbuff, *filtname;
2427  char *escuser;
2428
2429  /* name for the filter */
2430  filtname=g_strdup_printf("aimuser-%s", user);
2431
2432  /* if it already exists then go with it.  This lets users override */
2433  if (owl_global_get_filter(&g, filtname)) {
2434    return(g_strdup(filtname));
2435  }
2436
2437  /* create the new-internal filter */
2438  escuser = owl_text_quote(user, OWL_REGEX_QUOTECHARS, OWL_REGEX_QUOTEWITH);
2439
2440  argbuff = g_strdup_printf(
2441      "( type ^aim$ and ( ( sender ^%1$s$ and recipient ^%2$s$ ) or "
2442      "( sender ^%2$s$ and recipient ^%1$s$ ) ) )",
2443      escuser, owl_global_get_aim_screenname_for_filters(&g));
2444  g_free(escuser);
2445
2446  f = owl_filter_new_fromstring(filtname, argbuff);
2447  g_free(argbuff);
2448
2449  if (f == NULL) {
2450    owl_function_error("Error creating filter '%s'", filtname);
2451    g_free(filtname);
2452    return NULL;
2453  }
2454
2455  /* add it to the global list */
2456  owl_global_add_filter(&g, f);
2457
2458  return(filtname);
2459}
2460
2461char *owl_function_typefilt(const char *type)
2462{
2463  owl_filter *f;
2464  char *argbuff, *filtname, *esctype;
2465
2466  /* name for the filter */
2467  filtname=g_strdup_printf("type-%s", type);
2468
2469  /* if it already exists then go with it.  This lets users override */
2470  if (owl_global_get_filter(&g, filtname)) {
2471    return filtname;
2472  }
2473
2474  /* create the new-internal filter */
2475  esctype = owl_text_quote(type, OWL_REGEX_QUOTECHARS, OWL_REGEX_QUOTEWITH);
2476
2477  argbuff = owl_string_build_quoted("type ^%q$", esctype);
2478  g_free(esctype);
2479
2480  f = owl_filter_new_fromstring(filtname, argbuff);
2481  g_free(argbuff);
2482
2483  if (f == NULL) {
2484    owl_function_error("Error creating filter '%s'", filtname);
2485    g_free(filtname);
2486    return NULL;
2487  }
2488
2489  /* add it to the global list */
2490  owl_global_add_filter(&g, f);
2491
2492  return filtname;
2493}
2494
2495/* If flag is 1, marks for deletion.  If flag is 0,
2496 * unmarks for deletion. */
2497void owl_function_delete_curview_msgs(int flag)
2498{
2499  const owl_view *v;
2500  int i, j;
2501
2502  v=owl_global_get_current_view(&g);
2503  j=owl_view_get_size(v);
2504  for (i=0; i<j; i++) {
2505    if (flag == 1) {
2506      owl_message_mark_delete(owl_view_get_element(v, i));
2507    } else if (flag == 0) {
2508      owl_message_unmark_delete(owl_view_get_element(v, i));
2509    }
2510  }
2511
2512  owl_function_makemsg("%i messages marked for %sdeletion", j, flag?"":"un");
2513
2514  owl_mainwin_redisplay(owl_global_get_mainwin(&g)); 
2515}
2516
2517static char *owl_function_smartfilter_cc(const owl_message *m) {
2518  const char *ccs;
2519  char *ccs_quoted;
2520  char *filtname;
2521  owl_filter *f;
2522  GString *buf;
2523
2524  ccs = owl_message_get_attribute_value(m, "zephyr_ccs");
2525
2526  filtname = g_strdup_printf("conversation-%s", ccs);
2527  owl_text_tr(filtname, ' ', '-');
2528
2529  if (owl_global_get_filter(&g, filtname)) {
2530    return filtname;
2531  }
2532
2533  buf = g_string_new("type ^zephyr$ and filter personal and "
2534                     "zephyr_ccs ^");
2535  ccs_quoted = owl_text_quote(ccs, OWL_REGEX_QUOTECHARS, OWL_REGEX_QUOTEWITH);
2536  owl_string_append_quoted_arg(buf, ccs_quoted);
2537  g_string_append_c(buf, '$');
2538  g_free(ccs_quoted);
2539
2540  f = owl_filter_new_fromstring(filtname, buf->str);
2541  g_string_free(buf, true);
2542
2543  if (f == NULL) {
2544    owl_function_error("Error creating filter '%s'", filtname);
2545    g_free(filtname);
2546    return NULL;
2547  }
2548
2549  owl_global_add_filter(&g, f);
2550
2551  return filtname;
2552}
2553
2554/* Create a filter based on the current message.  Returns the name of
2555 * a filter or null.  The caller must free this name.
2556 *
2557 * if the curmsg is a personal zephyr return a filter name
2558 *    to the zephyr conversation with that user.
2559 * If the curmsg is a zephyr class message, instance foo, recip *,
2560 *    return a filter name to the class, inst.
2561 * If the curmsg is a zephyr class message and type==0 then
2562 *    return a filter name for just the class.
2563 * If the curmsg is a zephyr class message and type==1 then
2564 *    return a filter name for the class and instance.
2565 * If the curmsg is a personal AIM message returna  filter
2566 *    name to the AIM conversation with that user
2567 */
2568char *owl_function_smartfilter(int type, int invert_related)
2569{
2570  const owl_view *v;
2571  const owl_message *m;
2572  char *filtname = NULL;
2573  const char *argv[2], *zperson;
2574  int related = owl_global_is_narrow_related(&g) ^ invert_related;
2575
2576  v=owl_global_get_current_view(&g);
2577  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
2578
2579  if (!m || owl_view_get_size(v)==0) {
2580    owl_function_error("No message selected\n");
2581    return(NULL);
2582  }
2583
2584  /* very simple handling of admin messages for now */
2585  if (owl_message_is_type_admin(m)) {
2586    return(owl_function_typefilt("admin"));
2587  }
2588
2589  /* very simple handling of loopback messages for now */
2590  if (owl_message_is_type_loopback(m)) {
2591    return(owl_function_typefilt("loopback"));
2592  }
2593
2594  /* aim messages */
2595  if (owl_message_is_type_aim(m)) {
2596    if (owl_message_is_direction_in(m)) {
2597      filtname=owl_function_aimuserfilt(owl_message_get_sender(m));
2598    } else if (owl_message_is_direction_out(m)) {
2599      filtname=owl_function_aimuserfilt(owl_message_get_recipient(m));
2600    }
2601    return(filtname);
2602  }
2603
2604  /* narrow personal and login messages to the sender or recip as appropriate */
2605  if (owl_message_is_type_zephyr(m)) {
2606    if (owl_message_is_personal(m) || owl_message_is_loginout(m)) {
2607      if (owl_message_get_attribute_value(m, "zephyr_ccs") != NULL) {
2608        return owl_function_smartfilter_cc(m);
2609      }
2610
2611      if (owl_message_is_direction_in(m)) {
2612        zperson = owl_message_get_sender(m);
2613      } else {
2614        zperson = owl_message_get_recipient(m);
2615      }
2616      filtname = owl_function_zuserfilt(zperson);
2617      return filtname;
2618    }
2619
2620    /* narrow class MESSAGE, instance foo, recip * messages to class, inst */
2621    if (!strcasecmp(owl_message_get_class(m), "message")) {
2622      filtname=owl_function_classinstfilt(owl_message_get_class(m), owl_message_get_instance(m), related);
2623      return(filtname);
2624    }
2625
2626    /* otherwise narrow to the class */
2627    if (type==0) {
2628      filtname=owl_function_classinstfilt(owl_message_get_class(m), NULL, related);
2629    } else if (type==1) {
2630      filtname=owl_function_classinstfilt(owl_message_get_class(m), owl_message_get_instance(m), related);
2631    }
2632    return(filtname);
2633  }
2634
2635  /* pass it off to perl */
2636  argv[0] = type ? "1" : "0";
2637  argv[1] = related ? "1" : "0";
2638  return owl_perlconfig_message_call_method(m, "smartfilter", 2, argv);
2639}
2640
2641void owl_function_smartzpunt(int type)
2642{
2643  /* Starts a zpunt command based on the current class,instance pair.
2644   * If type=0, uses just class.  If type=1, uses instance as well. */
2645  const owl_view *v;
2646  const owl_message *m;
2647  const char *mclass, *minst;
2648  GString *buf;
2649 
2650  v=owl_global_get_current_view(&g);
2651  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
2652
2653  if (!m || owl_view_get_size(v)==0) {
2654    owl_function_error("No message selected\n");
2655    return;
2656  }
2657
2658  /* for now we skip admin messages. */
2659  if (owl_message_is_type_admin(m)
2660      || owl_message_is_loginout(m)
2661      || !owl_message_is_type_zephyr(m)) {
2662    owl_function_error("smartzpunt doesn't support this message type.");
2663    return;
2664  }
2665
2666  mclass = owl_message_get_class(m);
2667  minst = owl_message_get_instance(m);
2668  if (!mclass || !*mclass || *mclass==' '
2669      || (!strcasecmp(mclass, "message") && !strcasecmp(minst, "personal"))
2670      || (type && (!minst || !*minst|| *minst==' '))) {
2671    owl_function_error("smartzpunt can't safely do this for <%s,%s>",
2672                         mclass, minst);
2673  } else {
2674    buf = g_string_new("start-command zpunt ");
2675    owl_string_append_quoted_arg(buf, mclass);
2676    if (type) {
2677      g_string_append_c(buf, ' ');
2678      owl_string_append_quoted_arg(buf, minst);
2679    } else {
2680      g_string_append(buf, " *");
2681    }
2682    owl_function_command(buf->str);
2683    g_string_free(buf, true);
2684  }
2685}
2686
2687/* Set the color of the current view's filter to
2688 * be 'color'
2689 */
2690void owl_function_color_current_filter(const char *fgcolor, const char *bgcolor)
2691{
2692  const char *name;
2693
2694  name=owl_view_get_filtname(owl_global_get_current_view(&g));
2695  owl_function_color_filter(name, fgcolor, bgcolor);
2696}
2697
2698/* Set the color of the filter 'filter' to be 'color'.  If the color
2699 * name does not exist, return -1, if the filter does not exist or is
2700 * the "all" filter, return -2.  Return 0 on success
2701 */
2702int owl_function_color_filter(const char *filtname, const char *fgcolor, const char *bgcolor)
2703{
2704  owl_filter *f;
2705
2706  f=owl_global_get_filter(&g, filtname);
2707  if (!f) {
2708    owl_function_error("Unknown filter");
2709    return(-2);
2710  }
2711
2712  /* don't touch the all filter */
2713  if (!strcmp(filtname, "all")) {
2714    owl_function_error("You may not change the 'all' filter.");
2715    return(-2);
2716  }
2717
2718  if (owl_util_string_to_color(fgcolor)==OWL_COLOR_INVALID) {
2719    owl_function_error("No color named '%s' avilable.", fgcolor);
2720    return(-1);
2721  }
2722
2723
2724  if (bgcolor != NULL) {
2725    if (owl_util_string_to_color(bgcolor)==OWL_COLOR_INVALID) {
2726      owl_function_error("No color named '%s' avilable.", bgcolor);
2727      return(-1);
2728    }
2729    owl_filter_set_bgcolor(f, owl_util_string_to_color(bgcolor));
2730  }
2731  owl_filter_set_fgcolor(f, owl_util_string_to_color(fgcolor));
2732 
2733  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2734  return(0);
2735}
2736
2737void owl_function_show_colors(void)
2738{
2739  owl_fmtext fm;
2740  int i; 
2741 
2742  owl_fmtext_init_null(&fm);
2743  owl_fmtext_append_normal(&fm, "default: ");
2744  owl_fmtext_append_normal_color(&fm, "default\n", OWL_COLOR_DEFAULT, OWL_COLOR_DEFAULT);
2745
2746  owl_fmtext_append_normal(&fm,"red:      ");
2747  owl_fmtext_append_normal_color(&fm, "red\n", OWL_COLOR_RED, OWL_COLOR_DEFAULT);
2748
2749  owl_fmtext_append_normal(&fm,"green:    ");
2750  owl_fmtext_append_normal_color(&fm, "green\n", OWL_COLOR_GREEN, OWL_COLOR_DEFAULT);
2751
2752  owl_fmtext_append_normal(&fm,"yellow:   ");
2753  owl_fmtext_append_normal_color(&fm, "yellow\n", OWL_COLOR_YELLOW, OWL_COLOR_DEFAULT);
2754
2755  owl_fmtext_append_normal(&fm,"blue:     ");
2756  owl_fmtext_append_normal_color(&fm, "blue\n", OWL_COLOR_BLUE, OWL_COLOR_DEFAULT);
2757
2758  owl_fmtext_append_normal(&fm,"magenta:  ");
2759  owl_fmtext_append_normal_color(&fm, "magenta\n", OWL_COLOR_MAGENTA, OWL_COLOR_DEFAULT);
2760
2761  owl_fmtext_append_normal(&fm,"cyan:     ");
2762  owl_fmtext_append_normal_color(&fm, "cyan\n", OWL_COLOR_CYAN, OWL_COLOR_DEFAULT);
2763
2764  owl_fmtext_append_normal(&fm,"white:    ");
2765  owl_fmtext_append_normal_color(&fm, "white\n", OWL_COLOR_WHITE, OWL_COLOR_DEFAULT);
2766
2767  for(i = 8; i < COLORS; ++i) {
2768    char* str1 = g_strdup_printf("%4i:     ",i);
2769    char* str2 = g_strdup_printf("%i\n",i);
2770    owl_fmtext_append_normal(&fm,str1);
2771    owl_fmtext_append_normal_color(&fm, str2, i, OWL_COLOR_DEFAULT);
2772    g_free(str1);
2773     g_free(str2);
2774  }
2775 
2776  owl_function_popless_fmtext(&fm);
2777  owl_fmtext_cleanup(&fm);
2778}
2779
2780/* add the given class, inst, recip to the punt list for filtering.
2781 *   if direction==0 then punt
2782 *   if direction==1 then unpunt
2783 */
2784void owl_function_zpunt(const char *class, const char *inst, const char *recip, int direction)
2785{
2786  GString *buf;
2787  char *quoted;
2788
2789  buf = g_string_new("");
2790  if (!strcmp(class, "*")) {
2791    g_string_append(buf, "class .*");
2792  } else {
2793    quoted=owl_text_quote(class, OWL_REGEX_QUOTECHARS, OWL_REGEX_QUOTEWITH);
2794    owl_string_appendf_quoted(buf, "class ^(un)*%q(\\.d)*$", quoted);
2795    g_free(quoted);
2796  }
2797  if (!strcmp(inst, "*")) {
2798    g_string_append(buf, " and instance .*");
2799  } else {
2800    quoted=owl_text_quote(inst, OWL_REGEX_QUOTECHARS, OWL_REGEX_QUOTEWITH);
2801    owl_string_appendf_quoted(buf, " and instance ^(un)*%q(\\.d)*$", quoted);
2802    g_free(quoted);
2803  }
2804  if (!strcmp(recip, "*")) {
2805    /* g_string_append(buf, ""); */
2806  } else {
2807    if(!strcmp(recip, "%me%")) {
2808      recip = owl_zephyr_get_sender();
2809    }
2810    quoted=owl_text_quote(recip, OWL_REGEX_QUOTECHARS, OWL_REGEX_QUOTEWITH);
2811    owl_string_appendf_quoted(buf, " and recipient ^%q$", quoted);
2812    g_free(quoted);
2813  }
2814
2815  owl_function_punt(buf->str, direction);
2816  g_string_free(buf, true);
2817}
2818
2819void owl_function_punt(const char *filter, int direction)
2820{
2821  owl_filter *f;
2822  owl_list *fl;
2823  int i, j;
2824  fl=owl_global_get_puntlist(&g);
2825
2826  /* first, create the filter */
2827  owl_function_debugmsg("About to filter %s", filter);
2828  f = owl_filter_new_fromstring("punt-filter", filter);
2829  if (f == NULL) {
2830    owl_function_error("Error creating filter for zpunt");
2831    return;
2832  }
2833
2834  /* Check for an identical filter */
2835  j=owl_list_get_size(fl);
2836  for (i=0; i<j; i++) {
2837    if (owl_filter_equiv(f, owl_list_get_element(fl, i))) {
2838      owl_function_debugmsg("found an equivalent punt filter");
2839      /* if we're punting, then just silently bow out on this duplicate */
2840      if (direction==0) {
2841        owl_filter_delete(f);
2842        return;
2843      }
2844
2845      /* if we're unpunting, then remove this filter from the puntlist */
2846      if (direction==1) {
2847        owl_filter_delete(owl_list_get_element(fl, i));
2848        owl_list_remove_element(fl, i);
2849        owl_filter_delete(f);
2850        return;
2851      }
2852    }
2853  }
2854
2855  owl_function_debugmsg("punting");
2856  /* If we're punting, add the filter to the global punt list */
2857  if (direction==0) {
2858    owl_list_append_element(fl, f);
2859  }
2860}
2861
2862void owl_function_show_keymaps(void)
2863{
2864  owl_list l;
2865  owl_fmtext fm;
2866  const owl_keymap *km;
2867  const owl_keyhandler *kh;
2868  int i, numkm;
2869  const char *kmname;
2870
2871  kh = owl_global_get_keyhandler(&g);
2872  owl_fmtext_init_null(&fm);
2873  owl_fmtext_append_bold(&fm, "Keymaps:   ");
2874  owl_fmtext_append_normal(&fm, "(use 'show keymap <name>' for details)\n");
2875  owl_keyhandler_get_keymap_names(kh, &l);
2876  owl_fmtext_append_list(&fm, &l, "\n", owl_function_keymap_summary);
2877  owl_fmtext_append_normal(&fm, "\n");
2878
2879  numkm = owl_list_get_size(&l);
2880  for (i=0; i<numkm; i++) {
2881    kmname = owl_list_get_element(&l, i);
2882    km = owl_keyhandler_get_keymap(kh, kmname);
2883    owl_fmtext_append_bold(&fm, "\n\n----------------------------------------------------------------------------------------------------\n\n");
2884    owl_keymap_get_details(km, &fm, 0);
2885  }
2886  owl_fmtext_append_normal(&fm, "\n");
2887 
2888  owl_function_popless_fmtext(&fm);
2889  owl_keyhandler_keymap_namelist_cleanup(&l);
2890  owl_fmtext_cleanup(&fm);
2891}
2892
2893char *owl_function_keymap_summary(const char *name)
2894{
2895  const owl_keymap *km
2896    = owl_keyhandler_get_keymap(owl_global_get_keyhandler(&g), name);
2897  if (km) return owl_keymap_summary(km);
2898  else return(NULL);
2899}
2900
2901/* TODO: implement for real */
2902void owl_function_show_keymap(const char *name)
2903{
2904  owl_fmtext fm;
2905  const owl_keymap *km;
2906
2907  owl_fmtext_init_null(&fm);
2908  km = owl_keyhandler_get_keymap(owl_global_get_keyhandler(&g), name);
2909  if (km) {
2910    owl_keymap_get_details(km, &fm, 1);
2911  } else {
2912    owl_fmtext_append_normal(&fm, "No such keymap...\n");
2913  } 
2914  owl_function_popless_fmtext(&fm);
2915  owl_fmtext_cleanup(&fm);
2916}
2917
2918void owl_function_help_for_command(const char *cmdname)
2919{
2920  owl_fmtext fm;
2921
2922  owl_fmtext_init_null(&fm);
2923  owl_cmd_get_help(owl_global_get_cmddict(&g), cmdname, &fm);
2924  owl_function_popless_fmtext(&fm); 
2925  owl_fmtext_cleanup(&fm);
2926}
2927
2928void owl_function_set_search(const char *string)
2929{
2930  owl_regex re;
2931
2932  if (string && owl_regex_create_quoted(&re, string) == 0) {
2933    owl_global_set_search_re(&g, &re);
2934    owl_regex_cleanup(&re);
2935  } else {
2936    owl_global_set_search_re(&g, NULL);
2937  }
2938}
2939
2940void owl_function_search_helper(int consider_current, int direction)
2941{
2942  /* move to a message that contains the string.  If direction is
2943   * OWL_DIRECTION_DOWNWARDS then search fowards, if direction is
2944   * OWL_DIRECTION_UPWARDS then search backwards.
2945   *
2946   * If consider_current is true then it will stay on the
2947   * current message if it contains the string.
2948   */
2949
2950  const owl_view *v;
2951  int viewsize, i, curmsg, start;
2952  owl_message *m;
2953
2954  v=owl_global_get_current_view(&g);
2955  viewsize=owl_view_get_size(v);
2956  curmsg=owl_global_get_curmsg(&g);
2957 
2958  if (viewsize==0) {
2959    owl_function_error("No messages present");
2960    return;
2961  }
2962
2963  if (consider_current) {
2964    start=curmsg;
2965  } else if (direction==OWL_DIRECTION_DOWNWARDS) {
2966    start=curmsg+1;
2967  } else {
2968    start=curmsg-1;
2969  }
2970
2971  /* bounds check */
2972  if (start>=viewsize || start<0) {
2973    owl_function_error("No further matches found");
2974    return;
2975  }
2976
2977  for (i=start; i<viewsize && i>=0;) {
2978    m=owl_view_get_element(v, i);
2979    if (owl_message_search(m, owl_global_get_search_re(&g))) {
2980      owl_global_set_curmsg(&g, i);
2981      owl_function_calculate_topmsg(direction);
2982      owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2983      if (direction==OWL_DIRECTION_DOWNWARDS) {
2984        owl_global_set_direction_downwards(&g);
2985      } else {
2986        owl_global_set_direction_upwards(&g);
2987      }
2988      return;
2989    }
2990    if (direction==OWL_DIRECTION_DOWNWARDS) {
2991      i++;
2992    } else {
2993      i--;
2994    }
2995    owl_function_mask_sigint(NULL);
2996    if(owl_global_is_interrupted(&g)) {
2997      owl_global_unset_interrupted(&g);
2998      owl_function_unmask_sigint(NULL);
2999      owl_function_makemsg("Search interrupted!");
3000      owl_mainwin_redisplay(owl_global_get_mainwin(&g));
3001      return;
3002    }
3003    owl_function_unmask_sigint(NULL);
3004  }
3005  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
3006  owl_function_error("No matches found");
3007}
3008
3009/* strips formatting from ztext and returns the unformatted text.
3010 * caller is responsible for freeing. */
3011char *owl_function_ztext_stylestrip(const char *zt)
3012{
3013  owl_fmtext fm;
3014  char *plaintext;
3015
3016  owl_fmtext_init_null(&fm);
3017  owl_fmtext_append_ztext(&fm, zt);
3018  plaintext = owl_fmtext_print_plain(&fm);
3019  owl_fmtext_cleanup(&fm);
3020  return(plaintext);
3021}
3022
3023/* Popup a buddylisting.  If filename is NULL use the default .anyone */
3024void owl_function_buddylist(int aim, int zephyr, const char *filename)
3025{
3026  int i, j, idle;
3027  int interrupted = 0;
3028  owl_fmtext fm;
3029  const owl_buddylist *bl;
3030  const owl_buddy *b;
3031  char *timestr;
3032#ifdef HAVE_LIBZEPHYR
3033  int x;
3034  owl_list anyone;
3035  const char *user;
3036  char *tmp;
3037  ZLocations_t location[200];
3038  int numlocs, ret;
3039#endif
3040
3041  owl_fmtext_init_null(&fm);
3042
3043  /* AIM first */
3044  if (aim && owl_global_is_aimloggedin(&g)) {
3045    bl=owl_global_get_buddylist(&g);
3046
3047    owl_fmtext_append_bold(&fm, "AIM users logged in:\n");
3048    /* we're assuming AIM for now */
3049    j=owl_buddylist_get_size(bl);
3050    for (i=0; i<j; i++) {
3051      b=owl_buddylist_get_buddy_n(bl, i);
3052      idle=owl_buddy_get_idle_time(b);
3053      if (idle!=0) {
3054        timestr=owl_util_minutes_to_timestr(idle);
3055      } else {
3056        timestr=g_strdup("");
3057      }
3058      owl_fmtext_appendf_normal(&fm, "  %-20.20s %-12.12s\n", owl_buddy_get_name(b), timestr);
3059      g_free(timestr);
3060    }
3061  }
3062
3063#ifdef HAVE_LIBZEPHYR
3064  if (zephyr) {
3065    if(!owl_global_is_havezephyr(&g)) {
3066      owl_function_error("Zephyr currently not available.");
3067    } else {
3068      owl_fmtext_append_bold(&fm, "Zephyr users logged in:\n");
3069      owl_list_create(&anyone);
3070      ret=owl_zephyr_get_anyone_list(&anyone, filename);
3071      if (ret) {
3072        if (errno == ENOENT) {
3073          owl_fmtext_append_normal(&fm, " You have not added any zephyr buddies.  Use the\n");
3074          owl_fmtext_append_normal(&fm, " command ':addbuddy zephyr ");
3075          owl_fmtext_append_bold(  &fm, "<username>");
3076          owl_fmtext_append_normal(&fm, "'.\n");
3077        } else {
3078          owl_fmtext_append_normal(&fm, " Could not read zephyr buddies from the .anyone file.\n");
3079        }
3080      } else {
3081        j=owl_list_get_size(&anyone);
3082        for (i=0; i<j; i++) {
3083          user=owl_list_get_element(&anyone, i);
3084          ret=ZLocateUser(zstr(user), &numlocs, ZAUTH);
3085
3086          owl_function_mask_sigint(NULL);
3087          if(owl_global_is_interrupted(&g)) {
3088            interrupted = 1;
3089            owl_global_unset_interrupted(&g);
3090            owl_function_unmask_sigint(NULL);
3091            owl_function_makemsg("Interrupted!");
3092            break;
3093          }
3094
3095          owl_function_unmask_sigint(NULL);
3096
3097          if (ret!=ZERR_NONE) {
3098            owl_function_error("Error getting location for %s", user);
3099            continue;
3100          }
3101
3102          numlocs=200;
3103          ret=ZGetLocations(location, &numlocs);
3104          if (ret==0) {
3105            for (x=0; x<numlocs; x++) {
3106              tmp=short_zuser(user);
3107              owl_fmtext_appendf_normal(&fm, "  %-10.10s %-24.24s %-12.12s  %20.20s\n",
3108                                        tmp,
3109                                        location[x].host,
3110                                        location[x].tty,
3111                                        location[x].time);
3112              g_free(tmp);
3113            }
3114            if (numlocs>=200) {
3115              owl_fmtext_append_normal(&fm, "  Too many locations found for this user, truncating.\n");
3116            }
3117          }
3118        }
3119      }
3120      owl_list_cleanup(&anyone, g_free);
3121    }
3122  }
3123#endif
3124
3125  if (aim && zephyr) {
3126    if (owl_perlconfig_is_function("BarnOwl::Hooks::_get_blist")) {
3127      char * perlblist = owl_perlconfig_execute("BarnOwl::Hooks::_get_blist()");
3128      if (perlblist) {
3129        owl_fmtext_append_ztext(&fm, perlblist);
3130        g_free(perlblist);
3131      }
3132    }
3133  }
3134
3135  if(!interrupted) {
3136    owl_function_popless_fmtext(&fm);
3137  }
3138  owl_fmtext_cleanup(&fm);
3139}
3140
3141/* Dump messages in the current view to the file 'filename'. */
3142void owl_function_dump(const char *filename) 
3143{
3144  int i, j;
3145  owl_message *m;
3146  const owl_view *v;
3147  FILE *file;
3148  char *plaintext;
3149
3150  v=owl_global_get_current_view(&g);
3151
3152  /* in the future make it ask yes/no */
3153  /*
3154  ret=stat(filename, &sbuf);
3155  if (!ret) {
3156    ret=owl_function_askyesno("File exists, continue? [Y/n]");
3157    if (!ret) return;
3158  }
3159  */
3160
3161  file=fopen(filename, "w");
3162  if (!file) {
3163    owl_function_error("Error opening file");
3164    return;
3165  }
3166
3167  j=owl_view_get_size(v);
3168  for (i=0; i<j; i++) {
3169    m=owl_view_get_element(v, i);
3170    plaintext = owl_strip_format_chars(owl_message_get_text(m));
3171    if (plaintext) {
3172      fputs(plaintext, file);
3173      g_free(plaintext);
3174    }
3175  }
3176  fclose(file);
3177  owl_function_makemsg("Messages dumped to %s", filename);
3178}
3179
3180void owl_function_do_newmsgproc(void)
3181{
3182  if (owl_global_get_newmsgproc(&g) && strcmp(owl_global_get_newmsgproc(&g), "")) {
3183    /* if there's a process out there, we need to check on it */
3184    if (owl_global_get_newmsgproc_pid(&g)) {
3185      owl_function_debugmsg("Checking on newmsgproc pid==%i", owl_global_get_newmsgproc_pid(&g));
3186      owl_function_debugmsg("Waitpid return is %i", waitpid(owl_global_get_newmsgproc_pid(&g), NULL, WNOHANG));
3187      waitpid(owl_global_get_newmsgproc_pid(&g), NULL, WNOHANG);
3188      if (waitpid(owl_global_get_newmsgproc_pid(&g), NULL, WNOHANG)==-1) {
3189        /* it exited */
3190        owl_global_set_newmsgproc_pid(&g, 0);
3191        owl_function_debugmsg("newmsgproc exited");
3192      } else {
3193        owl_function_debugmsg("newmsgproc did not exit");
3194      }
3195    }
3196   
3197    /* if it exited, fork & exec a new one */
3198    if (owl_global_get_newmsgproc_pid(&g)==0) {
3199      pid_t i;
3200      int myargc;
3201      i=fork();
3202      if (i) {
3203        /* parent set the child's pid */
3204        owl_global_set_newmsgproc_pid(&g, i);
3205        owl_function_debugmsg("I'm the parent and I started a new newmsgproc with pid %i", i);
3206      } else {
3207        /* child exec's the program */
3208        char **parsed;
3209        parsed=owl_parseline(owl_global_get_newmsgproc(&g), &myargc);
3210        if (myargc < 0) {
3211          owl_function_debugmsg("Could not parse newmsgproc '%s': unbalanced quotes?", owl_global_get_newmsgproc(&g));
3212        }
3213        if (myargc <= 0) {
3214          _exit(127);
3215        }
3216        owl_function_debugmsg("About to exec \"%s\" with %d arguments", parsed[0], myargc);
3217       
3218        execvp(parsed[0], parsed);
3219       
3220       
3221        /* was there an error exec'ing? */
3222        owl_function_debugmsg("Cannot run newmsgproc '%s': cannot exec '%s': %s", 
3223                              owl_global_get_newmsgproc(&g), parsed[0], strerror(errno));
3224        _exit(127);
3225      }
3226    }
3227  }
3228}
3229
3230/* print the xterm escape sequence to raise the window */
3231void owl_function_xterm_raise(void)
3232{
3233  printf("\033[5t");
3234}
3235
3236/* print the xterm escape sequence to deiconify the window */
3237void owl_function_xterm_deiconify(void)
3238{
3239  printf("\033[1t");
3240}
3241
3242/* Add the specified command to the startup file.  Eventually this
3243 * should be clever, and rewriting settings that will obviosly
3244 * override earlier settings with 'set' 'bindkey' and 'alias'
3245 * commands.  For now though we just remove any line that would
3246 * duplicate this one and then append this line to the end of
3247 * startupfile.
3248 */
3249void owl_function_addstartup(const char *buff)
3250{
3251  FILE *file;
3252  const char *filename;
3253
3254  filename=owl_global_get_startupfile(&g);
3255
3256  /* delete earlier copies */
3257  owl_util_file_deleteline(filename, buff, 1);
3258
3259  file=fopen(filename, "a");
3260  if (!file) {
3261    owl_function_error("Error opening startupfile for new command");
3262    return;
3263  }
3264
3265  /* add this line */
3266  fprintf(file, "%s\n", buff);
3267
3268  fclose(file);
3269}
3270
3271/* Remove the specified command from the startup file. */
3272void owl_function_delstartup(const char *buff)
3273{
3274  const char *filename;
3275  filename=owl_global_get_startupfile(&g);
3276  owl_util_file_deleteline(filename, buff, 1);
3277}
3278
3279/* Execute owl commands from the given filename.  If the filename
3280 * is NULL, use the default owl startup commands file.
3281 */
3282void owl_function_source(const char *filename)
3283{
3284  char *path;
3285  FILE *file;
3286  char *s = NULL;
3287  int fail_silent = 0;
3288
3289  if (!filename) {
3290    fail_silent = 1;
3291    path = g_strdup(owl_global_get_startupfile(&g));
3292  } else {
3293    path = owl_util_makepath(filename);
3294  }
3295  file = fopen(path, "r");
3296  g_free(path);
3297  if (!file) {
3298    if (!fail_silent) {
3299      owl_function_error("Error opening file: %s", filename);
3300    }
3301    return;
3302  }
3303  while (owl_getline_chomp(&s, file)) {
3304    if (s[0] == '\0' || s[0] == '#')
3305      continue;
3306    owl_function_command(s);
3307  }
3308
3309  g_free(s);
3310  fclose(file);
3311}
3312
3313void owl_function_change_style(owl_view *v, const char *stylename)
3314{
3315  const owl_style *s;
3316
3317  s=owl_global_get_style_by_name(&g, stylename);
3318  if (!s) {
3319    owl_function_error("No style named %s", stylename);
3320    return;
3321  }
3322  owl_view_set_style(v, s);
3323  owl_messagelist_invalidate_formats(owl_global_get_msglist(&g));
3324  owl_function_calculate_topmsg(OWL_DIRECTION_DOWNWARDS);
3325  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
3326}
3327
3328void owl_function_toggleoneline(void)
3329{
3330  owl_view *v;
3331  const owl_style *s;
3332
3333  v=owl_global_get_current_view(&g);
3334  s=owl_view_get_style(v);
3335
3336  if (!owl_style_matches_name(s, "oneline")) {
3337    owl_function_change_style(v, "oneline");
3338  } else {
3339    owl_function_change_style(v, owl_global_get_default_style(&g));
3340  }
3341
3342  owl_messagelist_invalidate_formats(owl_global_get_msglist(&g));
3343  owl_function_calculate_topmsg(OWL_DIRECTION_DOWNWARDS);
3344  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
3345}
3346
3347void owl_function_error(const char *fmt, ...)
3348{
3349  static int in_error = 0;
3350  va_list ap;
3351  char *buff;
3352  const char *nl;
3353
3354  if (++in_error > 2) {
3355    /* More than two nested errors, bail immediately. */
3356    in_error--;
3357    return;
3358  }
3359
3360  va_start(ap, fmt);
3361  buff = g_strdup_vprintf(fmt, ap);
3362  va_end(ap);
3363
3364  owl_function_debugmsg("ERROR: %s", buff);
3365  owl_function_log_err(buff);
3366
3367  nl = strchr(buff, '\n');
3368
3369  /*
3370    Showing admin messages triggers a lot of code. If we have a
3371    recursive error call, that's the most likely candidate, so
3372    suppress the call in that case, to try to avoid infinite looping.
3373  */
3374
3375  if(nl && *(nl + 1) && in_error == 1) {
3376    /* Multiline error */
3377    owl_function_adminmsg("ERROR", buff);
3378  } else {
3379    owl_function_makemsg("[Error] %s", buff);
3380  }
3381
3382  g_free(buff);
3383
3384  in_error--;
3385}
3386
3387void owl_function_log_err(const char *string)
3388{
3389  char *date;
3390  time_t now;
3391  char *buff;
3392
3393  now=time(NULL);
3394  date=g_strdup(ctime(&now));
3395  date[strlen(date)-1]='\0';
3396
3397  buff = g_strdup_printf("%s %s", date, string);
3398
3399  owl_errqueue_append_err(owl_global_get_errqueue(&g), buff);
3400
3401  g_free(buff);
3402  g_free(date);
3403}
3404
3405void owl_function_showerrs(void)
3406{
3407  owl_fmtext fm;
3408
3409  owl_fmtext_init_null(&fm);
3410  owl_fmtext_append_normal(&fm, "Errors:\n\n");
3411  owl_errqueue_to_fmtext(owl_global_get_errqueue(&g), &fm);
3412  owl_function_popless_fmtext(&fm);
3413}
3414
3415void owl_function_makemsg(const char *fmt, ...)
3416{
3417  va_list ap;
3418  char *str;
3419
3420  va_start(ap, fmt);
3421  str = g_strdup_vprintf(fmt, ap);
3422  va_end(ap);
3423
3424  owl_function_debugmsg("makemsg: %s", str);
3425  owl_msgwin_set_text_nocopy(&g.msgwin, str);
3426}
3427
3428/* get locations for everyone in .anyone.  If 'notify' is '1' then
3429 * send a pseudo login or logout message for everyone not in sync with
3430 * the global zephyr buddy list.  The list is updated regardless of
3431 * the status of 'notify'.
3432 */
3433void owl_function_zephyr_buddy_check(int notify)
3434{
3435#ifdef HAVE_LIBZEPHYR
3436  int i, j;
3437  owl_list anyone;
3438  GList **zaldlist;
3439  GList *zaldptr;
3440  ZAsyncLocateData_t *zald;
3441  const char *user;
3442
3443  if (!owl_global_is_havezephyr(&g)) return;
3444  owl_global_set_pseudologin_notify(&g, notify);
3445  zaldlist = owl_global_get_zaldlist(&g);
3446
3447  /* Clear the existing ZALDs first. */
3448  zaldptr = g_list_first(*zaldlist);
3449  while (zaldptr) {
3450    ZFreeALD(zaldptr->data);
3451    g_free(zaldptr->data);
3452    zaldptr = g_list_next(zaldptr);
3453  }
3454  g_list_free(*zaldlist);
3455  *zaldlist = NULL;
3456
3457  owl_list_create(&anyone);
3458  owl_zephyr_get_anyone_list(&anyone, NULL);
3459  j = owl_list_get_size(&anyone);
3460  for (i = 0; i < j; i++) {
3461    user = owl_list_get_element(&anyone, i);
3462    zald = g_new(ZAsyncLocateData_t, 1);
3463    if (ZRequestLocations(zstr(user), zald, UNACKED, ZAUTH) == ZERR_NONE) {
3464      *zaldlist = g_list_append(*zaldlist, zald);
3465    } else {
3466      g_free(zald);
3467    }
3468  }
3469
3470  owl_list_cleanup(&anyone, g_free);
3471#endif
3472}
3473
3474void owl_function_aimsearch_results(const char *email, owl_list *namelist)
3475{
3476  owl_fmtext fm;
3477  int i, j;
3478
3479  owl_fmtext_init_null(&fm);
3480  owl_fmtext_append_normal(&fm, "AIM screennames associated with ");
3481  owl_fmtext_append_normal(&fm, email);
3482  owl_fmtext_append_normal(&fm, ":\n");
3483
3484  j=owl_list_get_size(namelist);
3485  for (i=0; i<j; i++) {
3486    owl_fmtext_append_normal(&fm, "  ");
3487    owl_fmtext_append_normal(&fm, owl_list_get_element(namelist, i));
3488    owl_fmtext_append_normal(&fm, "\n");
3489  }
3490
3491  owl_function_popless_fmtext(&fm);
3492  owl_fmtext_cleanup(&fm);
3493}
3494
3495int owl_function_get_color_count(void)
3496{
3497     return COLORS;
3498}
3499
3500void owl_function_mask_sigint(sigset_t *oldmask) {
3501  sigset_t intr;
3502
3503  sigemptyset(&intr);
3504  sigaddset(&intr, SIGINT);
3505  sigprocmask(SIG_BLOCK, &intr, oldmask);
3506}
3507
3508void owl_function_unmask_sigint(sigset_t *oldmask) {
3509  sigset_t intr;
3510
3511  sigemptyset(&intr);
3512  sigaddset(&intr, SIGINT);
3513  sigprocmask(SIG_UNBLOCK, &intr, oldmask);
3514}
3515
3516void _owl_function_mark_message(const owl_message *m)
3517{
3518  if (m) {
3519    owl_global_set_markedmsgid(&g, owl_message_get_id(m));
3520    owl_mainwin_redisplay(owl_global_get_mainwin(&g));
3521  }
3522}
3523
3524void owl_function_mark_message(void)
3525{
3526  const owl_message *m;
3527  const owl_view *v;
3528
3529  v=owl_global_get_current_view(&g);
3530
3531  /* bail if there's no current message */
3532  if (owl_view_get_size(v) < 1) {
3533    owl_function_error("No messages to mark");
3534    return;
3535  }
3536
3537  /* mark the message */
3538  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
3539  _owl_function_mark_message(m);
3540  owl_function_makemsg("Mark set");
3541}
3542
3543void owl_function_swap_cur_marked(void)
3544{
3545  int marked_id;
3546  const owl_message *m;
3547  const owl_view *v;
3548
3549  marked_id=owl_global_get_markedmsgid(&g);
3550  if (marked_id == -1) {
3551    owl_function_error("Mark not set.");
3552    return;
3553  }
3554
3555  v=owl_global_get_current_view(&g);
3556  /* bail if there's no current message */
3557  if (owl_view_get_size(v) < 1) {
3558    return;
3559  }
3560
3561  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
3562  _owl_function_mark_message(m);
3563  owl_global_set_curmsg(&g, owl_view_get_nearest_to_msgid(v, marked_id));
3564  owl_function_calculate_topmsg(OWL_DIRECTION_NONE);
3565  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
3566  owl_global_set_direction_downwards(&g);
3567}
Note: See TracBrowser for help on using the repository browser.