source: functions.c @ 3eeb6ed

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