source: functions.c @ b2bfe1f

Last change on this file since b2bfe1f was b2bfe1f, 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.4 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 Owl software and any\n"
147    "     accompanying software that uses the Owl 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 Owl");
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, "\nOwl 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 char *name;
1597  char *var;
1598  GPtrArray *varnames;
1599  int i;
1600  GString *str   = g_string_new("");
1601
1602  g_string_append_printf(str, "%-20s = %s\n", "VARIABLE", "VALUE");
1603  g_string_append_printf(str, "%-20s   %s\n",  "--------", "-----");
1604  varnames = owl_variable_dict_get_names(owl_global_get_vardict(&g));
1605  for (i = 0; i < varnames->len; i++) {
1606    name = varnames->pdata[i];
1607    if (name && name[0]!='_') {
1608      g_string_append_printf(str, "\n%-20s = ", name);
1609      var = owl_variable_get_tostring(owl_global_get_vardict(&g), name);
1610      if (var) {
1611        g_string_append(str, var);
1612        g_free(var);
1613      }
1614    }
1615  }
1616  g_string_append(str, "\n");
1617  owl_ptr_array_free(varnames, g_free);
1618
1619  owl_function_popless_text(str->str);
1620  g_string_free(str, true);
1621}
1622
1623void owl_function_show_variables(void)
1624{
1625  GPtrArray *varnames;
1626  owl_fmtext fm; 
1627  int i;
1628  const char *varname;
1629
1630  owl_fmtext_init_null(&fm);
1631  owl_fmtext_append_bold(&fm, 
1632      "Variables: (use 'show variable <name>' for details)\n");
1633  varnames = owl_variable_dict_get_names(owl_global_get_vardict(&g));
1634  for (i = 0; i < varnames->len; i++) {
1635    varname = varnames->pdata[i];
1636    if (varname && varname[0]!='_') {
1637      owl_variable_describe(owl_global_get_vardict(&g), varname, &fm);
1638    }
1639  }
1640  owl_ptr_array_free(varnames, g_free);
1641  owl_function_popless_fmtext(&fm);
1642  owl_fmtext_cleanup(&fm);
1643}
1644
1645void owl_function_show_variable(const char *name)
1646{
1647  owl_fmtext fm; 
1648
1649  owl_fmtext_init_null(&fm);
1650  owl_variable_get_help(owl_global_get_vardict(&g), name, &fm);
1651  owl_function_popless_fmtext(&fm);
1652  owl_fmtext_cleanup(&fm);
1653}
1654
1655/* note: this applies to global message list, not to view.
1656 * If flag is 1, deletes.  If flag is 0, undeletes. */
1657void owl_function_delete_by_id(int id, int flag)
1658{
1659  const owl_messagelist *ml;
1660  owl_message *m;
1661  ml = owl_global_get_msglist(&g);
1662  m = owl_messagelist_get_by_id(ml, id);
1663  if (m) {
1664    if (flag == 1) {
1665      owl_message_mark_delete(m);
1666    } else if (flag == 0) {
1667      owl_message_unmark_delete(m);
1668    }
1669    owl_mainwin_redisplay(owl_global_get_mainwin(&g));
1670  } else {
1671    owl_function_error("No message with id %d: unable to mark for (un)delete",id);
1672  }
1673}
1674
1675void owl_function_delete_automsgs(void)
1676{
1677  /* mark for deletion all messages in the current view that match the
1678   * 'trash' filter */
1679
1680  int i, j, count;
1681  owl_message *m;
1682  const owl_view *v;
1683  const owl_filter *f;
1684
1685  /* get the trash filter */
1686  f=owl_global_get_filter(&g, "trash");
1687  if (!f) {
1688    owl_function_error("No trash filter defined");
1689    return;
1690  }
1691
1692  v=owl_global_get_current_view(&g);
1693
1694  count=0;
1695  j=owl_view_get_size(v);
1696  for (i=0; i<j; i++) {
1697    m=owl_view_get_element(v, i);
1698    if (owl_filter_message_match(f, m)) {
1699      count++;
1700      owl_message_mark_delete(m);
1701    }
1702  }
1703  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
1704  owl_function_makemsg("%i messages marked for deletion", count);
1705}
1706
1707void owl_function_status(void)
1708{
1709  char buff[MAXPATHLEN+1];
1710  time_t start;
1711  int up, days, hours, minutes;
1712  owl_fmtext fm;
1713
1714  owl_fmtext_init_null(&fm);
1715
1716  start=owl_global_get_starttime(&g);
1717
1718  owl_fmtext_append_normal(&fm, "General Information:\n");
1719
1720  owl_fmtext_append_normal(&fm, "  Version: ");
1721  owl_fmtext_append_normal(&fm, OWL_VERSION_STRING);
1722  owl_fmtext_append_normal(&fm, "\n");
1723
1724  owl_fmtext_append_normal(&fm, "  Startup Arguments: ");
1725  owl_fmtext_append_normal(&fm, owl_global_get_startupargs(&g));
1726  owl_fmtext_append_normal(&fm, "\n");
1727
1728  owl_fmtext_append_normal(&fm, "  Current Directory: ");
1729  if(getcwd(buff, MAXPATHLEN) == NULL) {
1730    owl_fmtext_append_normal(&fm, "<Error in getcwd>");
1731  } else {
1732    owl_fmtext_append_normal(&fm, buff);
1733  }
1734  owl_fmtext_append_normal(&fm, "\n");
1735
1736  owl_fmtext_appendf_normal(&fm, "  Startup Time: %s", ctime(&start));
1737
1738  up=owl_global_get_runtime(&g);
1739  days=up/86400;
1740  up-=days*86400;
1741  hours=up/3600;
1742  up-=hours*3600;
1743  minutes=up/60;
1744  up-=minutes*60;
1745  owl_fmtext_appendf_normal(&fm, "  Run Time: %i days %2.2i:%2.2i:%2.2i\n", days, hours, minutes, up);
1746
1747  owl_fmtext_append_normal(&fm, "\nProtocol Options:\n");
1748  owl_fmtext_append_normal(&fm, "  Zephyr included    : ");
1749  if (owl_global_is_havezephyr(&g)) {
1750    owl_fmtext_append_normal(&fm, "yes\n");
1751  } else {
1752    owl_fmtext_append_normal(&fm, "no\n");
1753  }
1754  owl_fmtext_append_normal(&fm, "  AIM included       : yes\n");
1755  owl_fmtext_append_normal(&fm, "  Loopback included  : yes\n");
1756
1757
1758  owl_fmtext_append_normal(&fm, "\nBuild Options:\n");
1759  owl_fmtext_append_normal(&fm, "  Stderr redirection : ");
1760#if OWL_STDERR_REDIR
1761  owl_fmtext_append_normal(&fm, "yes\n");
1762#else
1763  owl_fmtext_append_normal(&fm, "no\n");
1764#endif
1765 
1766
1767  owl_fmtext_append_normal(&fm, "\nAIM Status:\n");
1768  owl_fmtext_append_normal(&fm, "  Logged in: ");
1769  if (owl_global_is_aimloggedin(&g)) {
1770    owl_fmtext_append_normal(&fm, owl_global_get_aim_screenname(&g));
1771    owl_fmtext_append_normal(&fm, "\n");
1772  } else {
1773    owl_fmtext_append_normal(&fm, "(not logged in)\n");
1774  }
1775
1776  owl_fmtext_append_normal(&fm, "  Processing events: ");
1777  if (owl_global_is_doaimevents(&g)) {
1778    owl_fmtext_append_normal(&fm, "yes\n");
1779  } else {
1780    owl_fmtext_append_normal(&fm, "no\n");
1781  }
1782
1783  owl_function_popless_fmtext(&fm);
1784  owl_fmtext_cleanup(&fm);
1785}
1786
1787void owl_function_show_term(void)
1788{
1789  owl_fmtext fm;
1790
1791  owl_fmtext_init_null(&fm);
1792  owl_fmtext_appendf_normal(&fm, "Terminal Lines: %i\nTerminal Columns: %i\n",
1793          owl_global_get_lines(&g),
1794          owl_global_get_cols(&g));
1795
1796  if (has_colors()) {
1797    owl_fmtext_append_normal(&fm, "Color: Yes\n");
1798    owl_fmtext_appendf_normal(&fm, "Number of color pairs: %i\n", owl_util_get_colorpairs());
1799    owl_fmtext_appendf_normal(&fm, "Can change colors: %s\n", can_change_color() ? "yes" : "no");
1800  } else {
1801    owl_fmtext_append_normal(&fm, "Color: No\n");
1802  }
1803
1804  owl_function_popless_fmtext(&fm);
1805  owl_fmtext_cleanup(&fm);
1806}
1807
1808/* if type = 0 then normal reply.
1809 * if type = 1 then it's a reply to sender
1810 * if enter = 0 then allow the command to be edited
1811 * if enter = 1 then don't wait for editing
1812 */
1813void owl_function_reply(int type, int enter)
1814{
1815  char *buff=NULL;
1816  const owl_message *m;
1817  const owl_filter *f;
1818 
1819  if (owl_view_get_size(owl_global_get_current_view(&g))==0) {
1820    owl_function_error("No message selected");
1821  } else {
1822    char *cmd;
1823   
1824    m=owl_view_get_element(owl_global_get_current_view(&g), owl_global_get_curmsg(&g));
1825    if (!m) {
1826      owl_function_error("No message selected");
1827      return;
1828    }
1829
1830    /* first check if we catch the reply-lockout filter */
1831    f=owl_global_get_filter(&g, "reply-lockout");
1832    if (f) {
1833      if (owl_filter_message_match(f, m)) {
1834        owl_function_error("Sorry, replies to this message have been disabled by the reply-lockout filter");
1835        return;
1836      }
1837    }
1838
1839    /* then check if it's a question and just bring up the command prompt */
1840    if (owl_message_is_question(m)) {
1841      owl_function_start_command("");
1842      return;
1843    }
1844
1845    if((type == 0 &&
1846        (cmd=owl_perlconfig_message_call_method(m, "replycmd", 0, NULL))) ||
1847       (type == 1 &&
1848        (cmd=owl_perlconfig_message_call_method(m, "replysendercmd", 0, NULL)))) {
1849      buff = cmd;
1850    }
1851
1852    if(!buff) {
1853        owl_function_error("I don't know how to reply to that message.");
1854        return;
1855    }
1856
1857    if (enter) {
1858      owl_history *hist = owl_global_get_cmd_history(&g);
1859      owl_history_store(hist, buff, false);
1860      owl_function_command_norv(buff);
1861    } else {
1862      owl_function_start_command(buff);
1863    }
1864    g_free(buff);
1865  }
1866}
1867
1868void owl_function_zlocate(int argc, const char *const *argv, int auth)
1869{
1870  owl_fmtext fm;
1871  char *ptr;
1872  char *result;
1873  int i;
1874
1875  owl_fmtext_init_null(&fm);
1876
1877  for (i=0; i<argc; i++) {
1878    ptr = long_zuser(argv[i]);
1879    result = owl_zephyr_zlocate(ptr, auth);
1880    owl_fmtext_append_normal(&fm, result);
1881    g_free(result);
1882    g_free(ptr);
1883  }
1884
1885  owl_function_popless_fmtext(&fm);
1886  owl_fmtext_cleanup(&fm);
1887}
1888
1889void owl_callback_command(owl_editwin *e)
1890{
1891  char *rv;
1892  const char *line = owl_editwin_get_text(e);
1893
1894  rv = owl_function_command(line);
1895   if (rv) {
1896    owl_function_makemsg("%s", rv);
1897    g_free(rv);
1898  }
1899}
1900
1901void owl_function_start_command(const char *line)
1902{
1903  owl_editwin *tw;
1904  owl_context *ctx;
1905
1906  tw = owl_global_set_typwin_active(&g, OWL_EDITWIN_STYLE_ONELINE, owl_global_get_cmd_history(&g));
1907
1908  owl_editwin_set_locktext(tw, "command: ");
1909
1910  owl_editwin_insert_string(tw, line);
1911
1912  ctx = owl_editcontext_new(OWL_CTX_EDITLINE, tw, "editline",
1913                            owl_global_deactivate_editcontext, &g);
1914  owl_global_push_context_obj(&g, ctx);
1915  owl_editwin_set_callback(tw, owl_callback_command);
1916}
1917
1918CALLER_OWN owl_editwin *owl_function_start_question(const char *line)
1919{
1920  owl_editwin *tw;
1921  owl_context *ctx;
1922
1923  tw = owl_global_set_typwin_active(&g, OWL_EDITWIN_STYLE_ONELINE, owl_global_get_cmd_history(&g));
1924
1925  owl_editwin_set_locktext(tw, line);
1926
1927  ctx = owl_editcontext_new(OWL_CTX_EDITRESPONSE, tw, "editresponse",
1928                            owl_global_deactivate_editcontext, &g);
1929  owl_global_push_context_obj(&g, ctx);
1930  return tw;
1931}
1932
1933CALLER_OWN owl_editwin *owl_function_start_password(const char *line)
1934{
1935  owl_editwin *tw;
1936  owl_context *ctx;
1937
1938  tw = owl_global_set_typwin_active(&g, OWL_EDITWIN_STYLE_ONELINE, NULL);
1939
1940  owl_editwin_set_echochar(tw, '*');
1941
1942  owl_editwin_set_locktext(tw, line);
1943
1944  ctx = owl_editcontext_new(OWL_CTX_EDITRESPONSE, tw, "editresponse",
1945                            owl_global_deactivate_editcontext, &g);
1946  owl_global_push_context_obj(&g, ctx);
1947  return tw;
1948}
1949
1950CALLER_OWN char *owl_function_exec(int argc, const char *const *argv, const char *buff, int type)
1951{
1952  /* if type == 1 display in a popup
1953   * if type == 2 display an admin messages
1954   * if type == 0 return output
1955   * else display in a popup
1956   */
1957  const char *redirect = " 2>&1 < /dev/null";
1958  char *newbuff;
1959  char *out;
1960  FILE *p;
1961
1962#if OWL_STDERR_REDIR
1963  redirect = " < /dev/null";
1964#endif
1965
1966  if (argc<2) {
1967    owl_function_error("Wrong number of arguments to the exec command");
1968    return NULL;
1969  }
1970
1971  buff = skiptokens(buff, 1);
1972  newbuff = g_strdup_printf("exec%s; %s", redirect, buff);
1973
1974  if (type == OWL_OUTPUT_POPUP) {
1975    owl_popexec_new(newbuff);
1976  } else {
1977    p = popen(newbuff, "r");
1978    out = owl_slurp(p);
1979    pclose(p);
1980   
1981    if (type == OWL_OUTPUT_RETURN) {
1982      g_free(newbuff);
1983      return out;
1984    } else if (type == OWL_OUTPUT_ADMINMSG) {
1985      owl_function_adminmsg(buff, out);
1986    }
1987    g_free(out);
1988  }
1989  g_free(newbuff);
1990  return NULL;
1991}
1992
1993CALLER_OWN char *owl_function_perl(int argc, const char *const *argv, const char *buff, int type)
1994{
1995  /* if type == 1 display in a popup
1996   * if type == 2 display an admin messages
1997   * if type == 0 return output
1998   * else display in a popup
1999   */
2000  char *perlout;
2001
2002  if (argc<2) {
2003    owl_function_error("Wrong number of arguments to perl command");
2004    return NULL;
2005  }
2006
2007  /* consume first token (argv[0]) */
2008  buff = skiptokens(buff, 1);
2009
2010  perlout = owl_perlconfig_execute(buff);
2011  if (perlout) { 
2012    if (type == OWL_OUTPUT_POPUP) {
2013      owl_function_popless_text(perlout);
2014    } else if (type == OWL_OUTPUT_ADMINMSG) {
2015      owl_function_adminmsg(buff, perlout);
2016    } else if (type == OWL_OUTPUT_RETURN) {
2017      return perlout;
2018    }
2019    g_free(perlout);
2020  }
2021  return NULL;
2022}
2023
2024/* Change the filter associated with the current view.
2025 * This also figures out which message in the new filter
2026 * should have the pointer.
2027 */
2028void owl_function_change_currentview_filter(const char *filtname)
2029{
2030  owl_view *v;
2031  owl_filter *f;
2032  int curid=-1, newpos, curmsg;
2033  const owl_message *curm=NULL;
2034
2035  v=owl_global_get_current_view(&g);
2036
2037  curmsg=owl_global_get_curmsg(&g);
2038  if (curmsg==-1) {
2039    owl_function_debugmsg("Hit the curmsg==-1 case in change_view");
2040  } else {
2041    curm=owl_view_get_element(v, curmsg);
2042    if (curm) {
2043      curid=owl_message_get_id(curm);
2044      owl_view_save_curmsgid(v, curid);
2045    }
2046  }
2047
2048  f=owl_global_get_filter(&g, filtname);
2049  if (!f) {
2050    owl_function_error("Unknown filter %s", filtname);
2051    return;
2052  }
2053
2054  owl_view_new_filter(v, f);
2055
2056  /* Figure out what to set the current message to.
2057   * - If the view we're leaving has messages in it, go to the closest message
2058   *   to the last message pointed to in that view.
2059   * - If the view we're leaving is empty, try to restore the position
2060   *   from the last time we were in the new view.  */
2061  if (curm) {
2062    newpos = owl_view_get_nearest_to_msgid(v, curid);
2063  } else {
2064    newpos = owl_view_get_nearest_to_saved(v);
2065  }
2066
2067  owl_global_set_curmsg(&g, newpos);
2068  owl_function_calculate_topmsg(OWL_DIRECTION_DOWNWARDS);
2069  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2070  owl_global_set_direction_downwards(&g);
2071}
2072
2073/* Create a new filter, or replace an existing one
2074 * with a new definition.
2075 */
2076void owl_function_create_filter(int argc, const char *const *argv)
2077{
2078  owl_filter *f;
2079  const owl_view *v;
2080  int inuse = 0;
2081
2082  if (argc < 2) {
2083    owl_function_error("Wrong number of arguments to filter command");
2084    return;
2085  }
2086
2087  owl_function_debugmsg("owl_function_create_filter: starting to create filter named %s", argv[1]);
2088
2089  v=owl_global_get_current_view(&g);
2090
2091  /* don't touch the all filter */
2092  if (!strcmp(argv[1], "all")) {
2093    owl_function_error("You may not change the 'all' filter.");
2094    return;
2095  }
2096
2097  /* deal with the case of trying change the filter color */
2098  if (argc==4 && !strcmp(argv[2], "-c")) {
2099    f=owl_global_get_filter(&g, argv[1]);
2100    if (!f) {
2101      owl_function_error("The filter '%s' does not exist.", argv[1]);
2102      return;
2103    }
2104    if (owl_util_string_to_color(argv[3])==OWL_COLOR_INVALID) {
2105      owl_function_error("The color '%s' is not available.", argv[3]);
2106      return;
2107    }
2108    owl_filter_set_fgcolor(f, owl_util_string_to_color(argv[3]));
2109    owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2110    return;
2111  }
2112  if (argc==4 && !strcmp(argv[2], "-b")) {
2113    f=owl_global_get_filter(&g, argv[1]);
2114    if (!f) {
2115      owl_function_error("The filter '%s' does not exist.", argv[1]);
2116      return;
2117    }
2118    if (owl_util_string_to_color(argv[3])==OWL_COLOR_INVALID) {
2119      owl_function_error("The color '%s' is not available.", argv[3]);
2120      return;
2121    }
2122    owl_filter_set_bgcolor(f, owl_util_string_to_color(argv[3]));
2123    owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2124    return;
2125  }
2126
2127  /* create the filter and check for errors */
2128  f = owl_filter_new(argv[1], argc-2, argv+2);
2129  if (f == NULL) {
2130    owl_function_error("Invalid filter");
2131    return;
2132  }
2133
2134  /* if the named filter is in use by the current view, remember it */
2135  if (!strcmp(owl_view_get_filtname(v), argv[1])) {
2136    inuse=1;
2137  }
2138
2139  /* if the named filter already exists, nuke it */
2140  if (owl_global_get_filter(&g, argv[1])) {
2141    owl_global_remove_filter(&g, argv[1]);
2142  }
2143
2144  /* add the filter */
2145  owl_global_add_filter(&g, f);
2146
2147  /* if it was in use by the current view then update */
2148  if (inuse) {
2149    owl_function_change_currentview_filter(argv[1]);
2150  }
2151  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2152}
2153
2154/* If 'filtername' does not start with 'not-' create a filter named
2155 * 'not-<filtername>' defined as "not filter <filtername>".  If the
2156 * filter 'not-<filtername>' already exists, do not overwrite it.  If
2157 * 'filtername' begins with 'not-' and a filter 'filtername' already
2158 * exists, then do nothing.  If the filter 'filtername' does not
2159 * exist, create it and define it as 'not filter <filtername>'
2160 *
2161 * Returns the name of the negated filter, which the caller must free.
2162 */
2163CALLER_OWN char *owl_function_create_negative_filter(const char *filtername)
2164{
2165  char *newname;
2166  const owl_filter *tmpfilt;
2167  const char *argv[5];
2168
2169  owl_function_debugmsg("owl_function_create_negative_filter");
2170 
2171  if (!strncmp(filtername, "not-", 4)) {
2172    newname=g_strdup(filtername+4);
2173  } else {
2174    newname=g_strdup_printf("not-%s", filtername);
2175  }
2176
2177  tmpfilt=owl_global_get_filter(&g, newname);
2178  if (!tmpfilt) {
2179    argv[0]="filter"; /* anything is fine here */
2180    argv[1]=newname;
2181    argv[2]="not";
2182    argv[3]="filter";
2183    argv[4]=filtername;
2184    owl_function_create_filter(5, argv);
2185  }
2186
2187  owl_function_debugmsg("owl_function_create_negative_filter: returning with %s", newname);
2188  return(newname);
2189}
2190
2191void owl_function_show_filters(void)
2192{
2193  const owl_filter *f;
2194  GList *fl;
2195  owl_fmtext fm;
2196
2197  owl_fmtext_init_null(&fm);
2198
2199  owl_fmtext_append_bold(&fm, "Filters:\n");
2200
2201  for (fl = g.filterlist; fl; fl = g_list_next(fl)) {
2202    f = fl->data;
2203    owl_fmtext_append_normal(&fm, "   ");
2204    owl_fmtext_append_normal_color(&fm, owl_filter_get_name(f),
2205                                   owl_filter_get_fgcolor(f),
2206                                   owl_filter_get_bgcolor(f));
2207    owl_fmtext_append_normal(&fm, "\n");
2208  }
2209  owl_function_popless_fmtext(&fm);
2210  owl_fmtext_cleanup(&fm);
2211}
2212
2213void owl_function_show_filter(const char *name)
2214{
2215  const owl_filter *f;
2216  char *buff, *tmp;
2217
2218  f=owl_global_get_filter(&g, name);
2219  if (!f) {
2220    owl_function_error("There is no filter named %s", name);
2221    return;
2222  }
2223  tmp = owl_filter_print(f);
2224  buff = g_strdup_printf("%s: %s", owl_filter_get_name(f), tmp);
2225  owl_function_popless_text(buff);
2226  g_free(buff);
2227  g_free(tmp);
2228}
2229
2230void owl_function_show_zpunts(void)
2231{
2232  const owl_filter *f;
2233  const GPtrArray *fl;
2234  char *tmp;
2235  owl_fmtext fm;
2236  int i;
2237
2238  owl_fmtext_init_null(&fm);
2239
2240  fl=owl_global_get_puntlist(&g);
2241  owl_fmtext_append_bold(&fm, "Active zpunt filters:\n");
2242
2243  for (i = 0; i < fl->len; i++) {
2244    f = fl->pdata[i];
2245    owl_fmtext_appendf_normal(&fm, "[% 2d] ", i+1);
2246    tmp = owl_filter_print(f);
2247    owl_fmtext_append_normal(&fm, tmp);
2248    g_free(tmp);
2249  }
2250  owl_function_popless_fmtext(&fm);
2251  owl_fmtext_cleanup(&fm);
2252}
2253
2254/* Create a filter for a class, instance if one doesn't exist.  If
2255 * instance is NULL then catch all messgaes in the class.  Returns the
2256 * name of the filter or null.  The caller must free this name.
2257 * If 'related' is nonzero, encompass unclasses and .d classes as well.
2258 */
2259CALLER_OWN char *owl_function_classinstfilt(const char *c, const char *i, int related) 
2260{
2261  owl_filter *f;
2262  char *filtname;
2263  char *tmpclass, *tmpinstance = NULL;
2264  char *class, *instance = NULL;
2265  GString *buf;
2266
2267  if (related) {
2268    class = owl_util_baseclass(c);
2269    if (i) {
2270      instance = owl_util_baseclass(i);
2271    }
2272  } else {
2273    class = g_strdup(c);
2274    if (i) {
2275      instance = g_strdup(i);
2276    }
2277  }
2278
2279  /* name for the filter */
2280  if (!instance) {
2281    filtname = g_strdup_printf("%sclass-%s", related ? "related-" : "", class);
2282  } else {
2283    filtname = g_strdup_printf("%sclass-%s-instance-%s", related ? "related-" : "", class, instance);
2284  }
2285  /* downcase it */
2286  {
2287    char *temp = g_utf8_strdown(filtname, -1);
2288    if (temp) {
2289      g_free(filtname);
2290      filtname = temp;
2291    }
2292  }
2293 
2294  /* if it already exists then go with it.  This lets users override */
2295  if (owl_global_get_filter(&g, filtname)) {
2296    goto done;
2297  }
2298
2299  /* create the new filter */
2300  tmpclass=owl_text_quote(class, OWL_REGEX_QUOTECHARS, OWL_REGEX_QUOTEWITH);
2301  if (instance) {
2302    tmpinstance=owl_text_quote(instance, OWL_REGEX_QUOTECHARS, OWL_REGEX_QUOTEWITH);
2303  }
2304
2305  buf = g_string_new("");
2306  owl_string_appendf_quoted(buf,
2307                            related ? "class ^(un)*%q(\\.d)*$" : "class ^%q$",
2308                            tmpclass);
2309
2310  if (tmpinstance) {
2311    owl_string_appendf_quoted(buf,
2312                              related ?
2313                              " and ( instance ^(un)*%q(\\.d)*$ )" :
2314                              " and instance ^%q$",
2315                              tmpinstance);
2316  }
2317  g_free(tmpclass);
2318  g_free(tmpinstance);
2319
2320  f = owl_filter_new_fromstring(filtname, buf->str);
2321  g_string_free(buf, true);
2322  if (f == NULL) {
2323    /* Couldn't make a filter for some reason. Return NULL. */
2324    owl_function_error("Error creating filter '%s'", filtname);
2325    g_free(filtname);
2326    filtname = NULL;
2327    goto done;
2328  }
2329
2330  /* add it to the global list */
2331  owl_global_add_filter(&g, f);
2332
2333done:
2334  g_free(class);
2335  g_free(instance);
2336  return(filtname);
2337}
2338
2339/* Create a filter for personal zephyrs to or from the specified
2340 * zephyr user.  Includes login/logout notifications for the user.
2341 * The name of the filter will be 'user-<shortuser>'.  If a filter already
2342 * exists with this name, no new filter will be created.  This allows
2343 * the configuration to override this function.  Returns the name of
2344 * the filter, which the caller must free.
2345 */
2346CALLER_OWN char *owl_function_zuserfilt(const char *longuser)
2347{
2348  owl_filter *f;
2349  char *argbuff, *esclonguser, *shortuser, *filtname;
2350
2351  /* name for the filter */
2352  shortuser = short_zuser(longuser);
2353  filtname = g_strdup_printf("user-%s", shortuser);
2354  g_free(shortuser);
2355
2356  /* if it already exists then go with it.  This lets users override */
2357  if (owl_global_get_filter(&g, filtname)) {
2358    return filtname;
2359  }
2360
2361  /* create the new-internal filter */
2362  esclonguser = owl_text_quote(longuser, OWL_REGEX_QUOTECHARS, OWL_REGEX_QUOTEWITH);
2363
2364  argbuff=owl_string_build_quoted("( type ^zephyr$ and filter personal and "
2365      "( ( direction ^in$ and sender ^%q$ ) or ( direction ^out$ and "
2366      "recipient ^%q$ ) ) ) or ( ( class ^login$ ) and ( sender ^%q$ ) )",
2367      esclonguser, esclonguser, esclonguser);
2368  g_free(esclonguser);
2369
2370  f = owl_filter_new_fromstring(filtname, argbuff);
2371  g_free(argbuff);
2372
2373  if (f == NULL) {
2374    /* Couldn't make a filter for some reason. Return NULL. */
2375    owl_function_error("Error creating filter '%s'", filtname);
2376    g_free(filtname);
2377    return NULL;
2378  }
2379
2380  /* add it to the global list */
2381  owl_global_add_filter(&g, f);
2382
2383  return(filtname);
2384}
2385
2386/* Create a filter for AIM IM messages to or from the specified
2387 * screenname.  The name of the filter will be 'aimuser-<user>'.  If a
2388 * filter already exists with this name, no new filter will be
2389 * created.  This allows the configuration to override this function.
2390 * Returns the name of the filter, which the caller must free.
2391 */
2392CALLER_OWN char *owl_function_aimuserfilt(const char *user)
2393{
2394  owl_filter *f;
2395  char *argbuff, *filtname;
2396  char *escuser;
2397
2398  /* name for the filter */
2399  filtname=g_strdup_printf("aimuser-%s", user);
2400
2401  /* if it already exists then go with it.  This lets users override */
2402  if (owl_global_get_filter(&g, filtname)) {
2403    return(g_strdup(filtname));
2404  }
2405
2406  /* create the new-internal filter */
2407  escuser = owl_text_quote(user, OWL_REGEX_QUOTECHARS, OWL_REGEX_QUOTEWITH);
2408
2409  argbuff = g_strdup_printf(
2410      "( type ^aim$ and ( ( sender ^%1$s$ and recipient ^%2$s$ ) or "
2411      "( sender ^%2$s$ and recipient ^%1$s$ ) ) )",
2412      escuser, owl_global_get_aim_screenname_for_filters(&g));
2413  g_free(escuser);
2414
2415  f = owl_filter_new_fromstring(filtname, argbuff);
2416  g_free(argbuff);
2417
2418  if (f == NULL) {
2419    owl_function_error("Error creating filter '%s'", filtname);
2420    g_free(filtname);
2421    return NULL;
2422  }
2423
2424  /* add it to the global list */
2425  owl_global_add_filter(&g, f);
2426
2427  return(filtname);
2428}
2429
2430CALLER_OWN char *owl_function_typefilt(const char *type)
2431{
2432  owl_filter *f;
2433  char *argbuff, *filtname, *esctype;
2434
2435  /* name for the filter */
2436  filtname=g_strdup_printf("type-%s", type);
2437
2438  /* if it already exists then go with it.  This lets users override */
2439  if (owl_global_get_filter(&g, filtname)) {
2440    return filtname;
2441  }
2442
2443  /* create the new-internal filter */
2444  esctype = owl_text_quote(type, OWL_REGEX_QUOTECHARS, OWL_REGEX_QUOTEWITH);
2445
2446  argbuff = owl_string_build_quoted("type ^%q$", esctype);
2447  g_free(esctype);
2448
2449  f = owl_filter_new_fromstring(filtname, argbuff);
2450  g_free(argbuff);
2451
2452  if (f == NULL) {
2453    owl_function_error("Error creating filter '%s'", filtname);
2454    g_free(filtname);
2455    return NULL;
2456  }
2457
2458  /* add it to the global list */
2459  owl_global_add_filter(&g, f);
2460
2461  return filtname;
2462}
2463
2464/* If flag is 1, marks for deletion.  If flag is 0,
2465 * unmarks for deletion. */
2466void owl_function_delete_curview_msgs(int flag)
2467{
2468  const owl_view *v;
2469  int i, j;
2470
2471  v=owl_global_get_current_view(&g);
2472  j=owl_view_get_size(v);
2473  for (i=0; i<j; i++) {
2474    if (flag == 1) {
2475      owl_message_mark_delete(owl_view_get_element(v, i));
2476    } else if (flag == 0) {
2477      owl_message_unmark_delete(owl_view_get_element(v, i));
2478    }
2479  }
2480
2481  owl_function_makemsg("%i messages marked for %sdeletion", j, flag?"":"un");
2482
2483  owl_mainwin_redisplay(owl_global_get_mainwin(&g)); 
2484}
2485
2486static CALLER_OWN char *owl_function_smartfilter_cc(const owl_message *m)
2487{
2488  const char *ccs;
2489  char *ccs_quoted;
2490  char *filtname;
2491  owl_filter *f;
2492  GString *buf;
2493
2494  ccs = owl_message_get_attribute_value(m, "zephyr_ccs");
2495
2496  filtname = g_strdup_printf("conversation-%s", ccs);
2497  g_strdelimit(filtname, " ", '-');
2498
2499  if (owl_global_get_filter(&g, filtname)) {
2500    return filtname;
2501  }
2502
2503  buf = g_string_new("type ^zephyr$ and filter personal and "
2504                     "zephyr_ccs ^");
2505  ccs_quoted = owl_text_quote(ccs, OWL_REGEX_QUOTECHARS, OWL_REGEX_QUOTEWITH);
2506  owl_string_append_quoted_arg(buf, ccs_quoted);
2507  g_string_append_c(buf, '$');
2508  g_free(ccs_quoted);
2509
2510  f = owl_filter_new_fromstring(filtname, buf->str);
2511  g_string_free(buf, true);
2512
2513  if (f == NULL) {
2514    owl_function_error("Error creating filter '%s'", filtname);
2515    g_free(filtname);
2516    return NULL;
2517  }
2518
2519  owl_global_add_filter(&g, f);
2520
2521  return filtname;
2522}
2523
2524/* Create a filter based on the current message.  Returns the name of
2525 * a filter or null.  The caller must free this name.
2526 *
2527 * if the curmsg is a personal zephyr return a filter name
2528 *    to the zephyr conversation with that user.
2529 * If the curmsg is a zephyr class message, instance foo, recip *,
2530 *    return a filter name to the class, inst.
2531 * If the curmsg is a zephyr class message and type==0 then
2532 *    return a filter name for just the class.
2533 * If the curmsg is a zephyr class message and type==1 then
2534 *    return a filter name for the class and instance.
2535 * If the curmsg is a personal AIM message returna  filter
2536 *    name to the AIM conversation with that user
2537 */
2538CALLER_OWN char *owl_function_smartfilter(int type, int invert_related)
2539{
2540  const owl_view *v;
2541  const owl_message *m;
2542  char *filtname = NULL;
2543  const char *argv[2], *zperson;
2544  int related = owl_global_is_narrow_related(&g) ^ invert_related;
2545
2546  v=owl_global_get_current_view(&g);
2547  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
2548
2549  if (!m || owl_view_get_size(v)==0) {
2550    owl_function_error("No message selected\n");
2551    return(NULL);
2552  }
2553
2554  /* very simple handling of admin messages for now */
2555  if (owl_message_is_type_admin(m)) {
2556    return(owl_function_typefilt("admin"));
2557  }
2558
2559  /* very simple handling of loopback messages for now */
2560  if (owl_message_is_type_loopback(m)) {
2561    return(owl_function_typefilt("loopback"));
2562  }
2563
2564  /* aim messages */
2565  if (owl_message_is_type_aim(m)) {
2566    if (owl_message_is_direction_in(m)) {
2567      filtname=owl_function_aimuserfilt(owl_message_get_sender(m));
2568    } else if (owl_message_is_direction_out(m)) {
2569      filtname=owl_function_aimuserfilt(owl_message_get_recipient(m));
2570    }
2571    return(filtname);
2572  }
2573
2574  /* narrow personal and login messages to the sender or recip as appropriate */
2575  if (owl_message_is_type_zephyr(m)) {
2576    if (owl_message_is_personal(m) || owl_message_is_loginout(m)) {
2577      if (owl_message_get_attribute_value(m, "zephyr_ccs") != NULL) {
2578        return owl_function_smartfilter_cc(m);
2579      }
2580
2581      if (owl_message_is_direction_in(m)) {
2582        zperson = owl_message_get_sender(m);
2583      } else {
2584        zperson = owl_message_get_recipient(m);
2585      }
2586      filtname = owl_function_zuserfilt(zperson);
2587      return filtname;
2588    }
2589
2590    /* narrow class MESSAGE, instance foo, recip * messages to class, inst */
2591    if (!strcasecmp(owl_message_get_class(m), "message")) {
2592      filtname=owl_function_classinstfilt(owl_message_get_class(m), owl_message_get_instance(m), related);
2593      return(filtname);
2594    }
2595
2596    /* otherwise narrow to the class */
2597    if (type==0) {
2598      filtname=owl_function_classinstfilt(owl_message_get_class(m), NULL, related);
2599    } else if (type==1) {
2600      filtname=owl_function_classinstfilt(owl_message_get_class(m), owl_message_get_instance(m), related);
2601    }
2602    return(filtname);
2603  }
2604
2605  /* pass it off to perl */
2606  argv[0] = type ? "1" : "0";
2607  argv[1] = related ? "1" : "0";
2608  return owl_perlconfig_message_call_method(m, "smartfilter", 2, argv);
2609}
2610
2611void owl_function_smartzpunt(int type)
2612{
2613  /* Starts a zpunt command based on the current class,instance pair.
2614   * If type=0, uses just class.  If type=1, uses instance as well. */
2615  const owl_view *v;
2616  const owl_message *m;
2617  const char *mclass, *minst;
2618  GString *buf;
2619 
2620  v=owl_global_get_current_view(&g);
2621  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
2622
2623  if (!m || owl_view_get_size(v)==0) {
2624    owl_function_error("No message selected\n");
2625    return;
2626  }
2627
2628  /* for now we skip admin messages. */
2629  if (owl_message_is_type_admin(m)
2630      || owl_message_is_loginout(m)
2631      || !owl_message_is_type_zephyr(m)) {
2632    owl_function_error("smartzpunt doesn't support this message type.");
2633    return;
2634  }
2635
2636  mclass = owl_message_get_class(m);
2637  minst = owl_message_get_instance(m);
2638  if (!mclass || !*mclass || *mclass==' '
2639      || (!strcasecmp(mclass, "message") && !strcasecmp(minst, "personal"))
2640      || (type && (!minst || !*minst|| *minst==' '))) {
2641    owl_function_error("smartzpunt can't safely do this for <%s,%s>",
2642                         mclass, minst);
2643  } else {
2644    buf = g_string_new("start-command zpunt ");
2645    owl_string_append_quoted_arg(buf, mclass);
2646    if (type) {
2647      g_string_append_c(buf, ' ');
2648      owl_string_append_quoted_arg(buf, minst);
2649    } else {
2650      g_string_append(buf, " *");
2651    }
2652    owl_function_command_norv(buf->str);
2653    g_string_free(buf, true);
2654  }
2655}
2656
2657/* Set the color of the current view's filter to
2658 * be 'color'
2659 */
2660void owl_function_color_current_filter(const char *fgcolor, const char *bgcolor)
2661{
2662  const char *name;
2663
2664  name=owl_view_get_filtname(owl_global_get_current_view(&g));
2665  owl_function_color_filter(name, fgcolor, bgcolor);
2666}
2667
2668/* Set the color of the filter 'filter' to be 'color'.  If the color
2669 * name does not exist, return -1, if the filter does not exist or is
2670 * the "all" filter, return -2.  Return 0 on success
2671 */
2672int owl_function_color_filter(const char *filtname, const char *fgcolor, const char *bgcolor)
2673{
2674  owl_filter *f;
2675
2676  f=owl_global_get_filter(&g, filtname);
2677  if (!f) {
2678    owl_function_error("Unknown filter");
2679    return(-2);
2680  }
2681
2682  /* don't touch the all filter */
2683  if (!strcmp(filtname, "all")) {
2684    owl_function_error("You may not change the 'all' filter.");
2685    return(-2);
2686  }
2687
2688  if (owl_util_string_to_color(fgcolor)==OWL_COLOR_INVALID) {
2689    owl_function_error("No color named '%s' avilable.", fgcolor);
2690    return(-1);
2691  }
2692
2693
2694  if (bgcolor != NULL) {
2695    if (owl_util_string_to_color(bgcolor)==OWL_COLOR_INVALID) {
2696      owl_function_error("No color named '%s' avilable.", bgcolor);
2697      return(-1);
2698    }
2699    owl_filter_set_bgcolor(f, owl_util_string_to_color(bgcolor));
2700  }
2701  owl_filter_set_fgcolor(f, owl_util_string_to_color(fgcolor));
2702 
2703  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2704  return(0);
2705}
2706
2707void owl_function_show_colors(void)
2708{
2709  owl_fmtext fm;
2710  int i; 
2711 
2712  owl_fmtext_init_null(&fm);
2713  owl_fmtext_append_normal(&fm,"default:  ");
2714  owl_fmtext_append_normal_color(&fm, "default\n", OWL_COLOR_DEFAULT, OWL_COLOR_DEFAULT);
2715
2716  owl_fmtext_append_normal(&fm,"black:    ");
2717  owl_fmtext_append_normal_color(&fm, "black\n", OWL_COLOR_BLACK, OWL_COLOR_DEFAULT);
2718
2719  owl_fmtext_append_normal(&fm,"red:      ");
2720  owl_fmtext_append_normal_color(&fm, "red\n", OWL_COLOR_RED, OWL_COLOR_DEFAULT);
2721
2722  owl_fmtext_append_normal(&fm,"green:    ");
2723  owl_fmtext_append_normal_color(&fm, "green\n", OWL_COLOR_GREEN, OWL_COLOR_DEFAULT);
2724
2725  owl_fmtext_append_normal(&fm,"yellow:   ");
2726  owl_fmtext_append_normal_color(&fm, "yellow\n", OWL_COLOR_YELLOW, OWL_COLOR_DEFAULT);
2727
2728  owl_fmtext_append_normal(&fm,"blue:     ");
2729  owl_fmtext_append_normal_color(&fm, "blue\n", OWL_COLOR_BLUE, OWL_COLOR_DEFAULT);
2730
2731  owl_fmtext_append_normal(&fm,"magenta:  ");
2732  owl_fmtext_append_normal_color(&fm, "magenta\n", OWL_COLOR_MAGENTA, OWL_COLOR_DEFAULT);
2733
2734  owl_fmtext_append_normal(&fm,"cyan:     ");
2735  owl_fmtext_append_normal_color(&fm, "cyan\n", OWL_COLOR_CYAN, OWL_COLOR_DEFAULT);
2736
2737  owl_fmtext_append_normal(&fm,"white:    ");
2738  owl_fmtext_append_normal_color(&fm, "white\n", OWL_COLOR_WHITE, OWL_COLOR_DEFAULT);
2739
2740  for(i = 8; i < COLORS; ++i) {
2741    char* str1 = g_strdup_printf("%4i:     ",i);
2742    char* str2 = g_strdup_printf("%i\n",i);
2743    owl_fmtext_append_normal(&fm,str1);
2744    owl_fmtext_append_normal_color(&fm, str2, i, OWL_COLOR_DEFAULT);
2745    g_free(str1);
2746     g_free(str2);
2747  }
2748 
2749  owl_function_popless_fmtext(&fm);
2750  owl_fmtext_cleanup(&fm);
2751}
2752
2753/* add the given class, inst, recip to the punt list for filtering.
2754 *   if direction==0 then punt
2755 *   if direction==1 then unpunt
2756 */
2757void owl_function_zpunt(const char *class, const char *inst, const char *recip, int direction)
2758{
2759  GPtrArray *argv;
2760  char *quoted;
2761
2762  argv = g_ptr_array_new();
2763  if (!strcmp(class, "*")) {
2764    g_ptr_array_add(argv, g_strdup("class"));
2765    g_ptr_array_add(argv, g_strdup(".*"));
2766  } else {
2767    quoted=owl_text_quote(class, OWL_REGEX_QUOTECHARS, OWL_REGEX_QUOTEWITH);
2768    g_ptr_array_add(argv, g_strdup("class"));
2769    g_ptr_array_add(argv, g_strdup_printf("^(un)*%s(\\.d)*$", quoted));
2770    g_free(quoted);
2771  }
2772  if (!strcmp(inst, "*")) {
2773    g_ptr_array_add(argv, g_strdup("and"));
2774    g_ptr_array_add(argv, g_strdup("instance"));
2775    g_ptr_array_add(argv, g_strdup(".*"));
2776  } else {
2777    quoted=owl_text_quote(inst, OWL_REGEX_QUOTECHARS, OWL_REGEX_QUOTEWITH);
2778    g_ptr_array_add(argv, g_strdup("and"));
2779    g_ptr_array_add(argv, g_strdup("instance"));
2780    g_ptr_array_add(argv, g_strdup_printf("^(un)*%s(\\.d)*$", quoted));
2781    g_free(quoted);
2782  }
2783  if (!strcmp(recip, "*")) {
2784    /* nothing */
2785  } else {
2786    if(!strcmp(recip, "%me%")) {
2787      recip = owl_zephyr_get_sender();
2788    }
2789    quoted=owl_text_quote(recip, OWL_REGEX_QUOTECHARS, OWL_REGEX_QUOTEWITH);
2790    g_ptr_array_add(argv, g_strdup("and"));
2791    g_ptr_array_add(argv, g_strdup("recipient"));
2792    g_ptr_array_add(argv, g_strdup_printf("^%s$", quoted));
2793    g_free(quoted);
2794  }
2795
2796  owl_function_punt(argv->len, (const char *const*) argv->pdata, direction);
2797  owl_ptr_array_free(argv, g_free);
2798}
2799
2800void owl_function_punt(int argc, const char *const *argv, int direction)
2801{
2802  owl_filter *f;
2803  GPtrArray *fl;
2804  int i;
2805  fl=owl_global_get_puntlist(&g);
2806
2807  /* first, create the filter */
2808  f = owl_filter_new("punt-filter", argc, argv);
2809  if (f == NULL) {
2810    owl_function_error("Error creating filter for zpunt");
2811    return;
2812  }
2813
2814  /* Check for an identical filter */
2815  for (i = 0; i < fl->len; i++) {
2816    if (owl_filter_equiv(f, fl->pdata[i])) {
2817      owl_function_debugmsg("found an equivalent punt filter");
2818      /* if we're punting, then just silently bow out on this duplicate */
2819      if (direction==0) {
2820        owl_filter_delete(f);
2821        return;
2822      }
2823
2824      /* if we're unpunting, then remove this filter from the puntlist */
2825      if (direction==1) {
2826        owl_filter_delete(g_ptr_array_remove_index(fl, i));
2827        owl_filter_delete(f);
2828        return;
2829      }
2830    }
2831  }
2832
2833  if (direction == 0) {
2834    owl_function_debugmsg("punting");
2835    /* If we're punting, add the filter to the global punt list */
2836    g_ptr_array_add(fl, f);
2837  } else if (direction == 1) {
2838    owl_function_makemsg("No matching punt filter");
2839 }
2840}
2841
2842void owl_function_show_keymaps(void)
2843{
2844  GPtrArray *l;
2845  owl_fmtext fm;
2846  const owl_keymap *km;
2847  const owl_keyhandler *kh;
2848  int i;
2849  const char *kmname;
2850
2851  kh = owl_global_get_keyhandler(&g);
2852  owl_fmtext_init_null(&fm);
2853  owl_fmtext_append_bold(&fm, "Keymaps:   ");
2854  owl_fmtext_append_normal(&fm, "(use 'show keymap <name>' for details)\n");
2855  l = owl_keyhandler_get_keymap_names(kh);
2856  owl_fmtext_append_list(&fm, l, "\n", owl_function_keymap_summary);
2857  owl_fmtext_append_normal(&fm, "\n");
2858
2859  for (i = 0; i < l->len; i++) {
2860    kmname = l->pdata[i];
2861    km = owl_keyhandler_get_keymap(kh, kmname);
2862    owl_fmtext_append_bold(&fm, "\n\n----------------------------------------------------------------------------------------------------\n\n");
2863    owl_keymap_get_details(km, &fm, 0);
2864  }
2865  owl_fmtext_append_normal(&fm, "\n");
2866 
2867  owl_function_popless_fmtext(&fm);
2868  owl_ptr_array_free(l, g_free);
2869  owl_fmtext_cleanup(&fm);
2870}
2871
2872CALLER_OWN char *owl_function_keymap_summary(const char *name)
2873{
2874  const owl_keymap *km
2875    = owl_keyhandler_get_keymap(owl_global_get_keyhandler(&g), name);
2876  if (km) return owl_keymap_summary(km);
2877  else return(NULL);
2878}
2879
2880/* TODO: implement for real */
2881void owl_function_show_keymap(const char *name)
2882{
2883  owl_fmtext fm;
2884  const owl_keymap *km;
2885
2886  owl_fmtext_init_null(&fm);
2887  km = owl_keyhandler_get_keymap(owl_global_get_keyhandler(&g), name);
2888  if (km) {
2889    owl_keymap_get_details(km, &fm, 1);
2890  } else {
2891    owl_fmtext_append_normal(&fm, "No such keymap...\n");
2892  } 
2893  owl_function_popless_fmtext(&fm);
2894  owl_fmtext_cleanup(&fm);
2895}
2896
2897void owl_function_help_for_command(const char *cmdname)
2898{
2899  owl_fmtext fm;
2900
2901  owl_fmtext_init_null(&fm);
2902  owl_cmd_get_help(owl_global_get_cmddict(&g), cmdname, &fm);
2903  owl_function_popless_fmtext(&fm); 
2904  owl_fmtext_cleanup(&fm);
2905}
2906
2907void owl_function_set_search(const char *string)
2908{
2909  owl_regex re;
2910
2911  if (string && owl_regex_create_quoted(&re, string) == 0) {
2912    owl_global_set_search_re(&g, &re);
2913    owl_regex_cleanup(&re);
2914  } else {
2915    owl_global_set_search_re(&g, NULL);
2916  }
2917}
2918
2919void owl_function_search_helper(int consider_current, int direction)
2920{
2921  /* move to a message that contains the string.  If direction is
2922   * OWL_DIRECTION_DOWNWARDS then search fowards, if direction is
2923   * OWL_DIRECTION_UPWARDS then search backwards.
2924   *
2925   * If consider_current is true then it will stay on the
2926   * current message if it contains the string.
2927   */
2928
2929  const owl_view *v;
2930  int viewsize, i, curmsg, start;
2931  owl_message *m;
2932
2933  v=owl_global_get_current_view(&g);
2934  viewsize=owl_view_get_size(v);
2935  curmsg=owl_global_get_curmsg(&g);
2936 
2937  if (viewsize==0) {
2938    owl_function_makemsg("No messages present");
2939    return;
2940  }
2941
2942  if (consider_current) {
2943    start=curmsg;
2944  } else if (direction==OWL_DIRECTION_DOWNWARDS) {
2945    start=curmsg+1;
2946  } else {
2947    start=curmsg-1;
2948  }
2949
2950  /* bounds check */
2951  if (start>=viewsize || start<0) {
2952    owl_function_makemsg("No further matches found");
2953    return;
2954  }
2955
2956  for (i=start; i<viewsize && i>=0;) {
2957    m=owl_view_get_element(v, i);
2958    if (owl_message_search(m, owl_global_get_search_re(&g))) {
2959      owl_global_set_curmsg(&g, i);
2960      owl_function_calculate_topmsg(direction);
2961      owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2962      if (direction==OWL_DIRECTION_DOWNWARDS) {
2963        owl_global_set_direction_downwards(&g);
2964      } else {
2965        owl_global_set_direction_upwards(&g);
2966      }
2967      return;
2968    }
2969    if (direction==OWL_DIRECTION_DOWNWARDS) {
2970      i++;
2971    } else {
2972      i--;
2973    }
2974    if (owl_global_take_interrupt(&g)) {
2975      owl_function_makemsg("Search interrupted!");
2976      owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2977      return;
2978    }
2979  }
2980  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2981  owl_function_makemsg("No matches found");
2982}
2983
2984/* strips formatting from ztext and returns the unformatted text.
2985 * caller is responsible for freeing. */
2986CALLER_OWN char *owl_function_ztext_stylestrip(const char *zt)
2987{
2988  owl_fmtext fm;
2989  char *plaintext;
2990
2991  owl_fmtext_init_null(&fm);
2992  owl_fmtext_append_ztext(&fm, zt);
2993  plaintext = owl_fmtext_print_plain(&fm);
2994  owl_fmtext_cleanup(&fm);
2995  return(plaintext);
2996}
2997
2998/* Popup a buddylisting.  If filename is NULL use the default .anyone */
2999void owl_function_buddylist(int aim, int zephyr, const char *filename)
3000{
3001  int i, j, idle;
3002  int interrupted = 0;
3003  owl_fmtext fm;
3004  const owl_buddylist *bl;
3005  const owl_buddy *b;
3006  char *timestr;
3007#ifdef HAVE_LIBZEPHYR
3008  int x;
3009  GPtrArray *anyone;
3010  const char *user;
3011  char *tmp;
3012  ZLocations_t location[200];
3013  int numlocs, ret;
3014#endif
3015
3016  owl_fmtext_init_null(&fm);
3017
3018  /* AIM first */
3019  if (aim && owl_global_is_aimloggedin(&g)) {
3020    bl=owl_global_get_buddylist(&g);
3021
3022    owl_fmtext_append_bold(&fm, "AIM users logged in:\n");
3023    /* we're assuming AIM for now */
3024    j=owl_buddylist_get_size(bl);
3025    for (i=0; i<j; i++) {
3026      b=owl_buddylist_get_buddy_n(bl, i);
3027      idle=owl_buddy_get_idle_time(b);
3028      if (idle!=0) {
3029        timestr=owl_util_minutes_to_timestr(idle);
3030      } else {
3031        timestr=g_strdup("");
3032      }
3033      owl_fmtext_appendf_normal(&fm, "  %-20.20s %-12.12s\n", owl_buddy_get_name(b), timestr);
3034      g_free(timestr);
3035    }
3036  }
3037
3038#ifdef HAVE_LIBZEPHYR
3039  if (zephyr) {
3040    if(!owl_global_is_havezephyr(&g)) {
3041      owl_function_error("Zephyr currently not available.");
3042    } else {
3043      owl_fmtext_append_bold(&fm, "Zephyr users logged in:\n");
3044      anyone = owl_zephyr_get_anyone_list(filename);
3045      if (anyone == NULL) {
3046        if (errno == ENOENT) {
3047          owl_fmtext_append_normal(&fm, " You have not added any zephyr buddies.  Use the\n");
3048          owl_fmtext_append_normal(&fm, " command ':addbuddy zephyr ");
3049          owl_fmtext_append_bold(  &fm, "<username>");
3050          owl_fmtext_append_normal(&fm, "'.\n");
3051        } else {
3052          owl_fmtext_append_normal(&fm, " Could not read zephyr buddies from the .anyone file.\n");
3053        }
3054      } else {
3055        for (i = 0; i < anyone->len; i++) {
3056          user = anyone->pdata[i];
3057          ret=ZLocateUser(zstr(user), &numlocs, ZAUTH);
3058
3059          if (owl_global_take_interrupt(&g)) {
3060            interrupted = 1;
3061            owl_function_makemsg("Interrupted!");
3062            break;
3063          }
3064
3065          if (ret!=ZERR_NONE) {
3066            owl_function_error("Error getting location for %s", user);
3067            continue;
3068          }
3069
3070          numlocs=200;
3071          ret=ZGetLocations(location, &numlocs);
3072          if (ret==0) {
3073            for (x=0; x<numlocs; x++) {
3074              tmp=short_zuser(user);
3075              owl_fmtext_appendf_normal(&fm, "  %-10.10s %-24.24s %-12.12s  %20.20s\n",
3076                                        tmp,
3077                                        location[x].host,
3078                                        location[x].tty,
3079                                        location[x].time);
3080              g_free(tmp);
3081            }
3082            if (numlocs>=200) {
3083              owl_fmtext_append_normal(&fm, "  Too many locations found for this user, truncating.\n");
3084            }
3085          }
3086        }
3087      }
3088      owl_ptr_array_free(anyone, g_free);
3089    }
3090  }
3091#endif
3092
3093  if (aim && zephyr) {
3094    if (owl_perlconfig_is_function("BarnOwl::Hooks::_get_blist")) {
3095      char * perlblist = owl_perlconfig_execute("BarnOwl::Hooks::_get_blist()");
3096      if (perlblist) {
3097        owl_fmtext_append_ztext(&fm, perlblist);
3098        g_free(perlblist);
3099      }
3100    }
3101  }
3102
3103  if(!interrupted) {
3104    owl_function_popless_fmtext(&fm);
3105  }
3106  owl_fmtext_cleanup(&fm);
3107}
3108
3109/* Dump messages in the current view to the file 'filename'. */
3110void owl_function_dump(const char *filename) 
3111{
3112  int i, j;
3113  owl_message *m;
3114  const owl_view *v;
3115  FILE *file;
3116  char *plaintext;
3117
3118  v=owl_global_get_current_view(&g);
3119
3120  /* in the future make it ask yes/no */
3121  /*
3122  ret=stat(filename, &sbuf);
3123  if (!ret) {
3124    ret=owl_function_askyesno("File exists, continue? [Y/n]");
3125    if (!ret) return;
3126  }
3127  */
3128
3129  file=fopen(filename, "w");
3130  if (!file) {
3131    owl_function_error("Error opening file");
3132    return;
3133  }
3134
3135  j=owl_view_get_size(v);
3136  for (i=0; i<j; i++) {
3137    m=owl_view_get_element(v, i);
3138    plaintext = owl_strip_format_chars(owl_message_get_text(m));
3139    if (plaintext) {
3140      fputs(plaintext, file);
3141      g_free(plaintext);
3142    }
3143  }
3144  fclose(file);
3145  owl_function_makemsg("Messages dumped to %s", filename);
3146}
3147
3148void owl_function_do_newmsgproc(void)
3149{
3150  if (owl_global_get_newmsgproc(&g) && strcmp(owl_global_get_newmsgproc(&g), "")) {
3151    /* if there's a process out there, we need to check on it */
3152    if (owl_global_get_newmsgproc_pid(&g)) {
3153      owl_function_debugmsg("Checking on newmsgproc pid==%i", owl_global_get_newmsgproc_pid(&g));
3154      owl_function_debugmsg("Waitpid return is %i", waitpid(owl_global_get_newmsgproc_pid(&g), NULL, WNOHANG));
3155      waitpid(owl_global_get_newmsgproc_pid(&g), NULL, WNOHANG);
3156      if (waitpid(owl_global_get_newmsgproc_pid(&g), NULL, WNOHANG)==-1) {
3157        /* it exited */
3158        owl_global_set_newmsgproc_pid(&g, 0);
3159        owl_function_debugmsg("newmsgproc exited");
3160      } else {
3161        owl_function_debugmsg("newmsgproc did not exit");
3162      }
3163    }
3164   
3165    /* if it exited, spawn a new one */
3166    if (owl_global_get_newmsgproc_pid(&g)==0) {
3167      int myargc;
3168      char **argv = owl_parseline(owl_global_get_newmsgproc(&g), &myargc);
3169      if (myargc < 0) {
3170        owl_function_debugmsg("Could not parse newmsgproc '%s': unbalanced quotes?",
3171                              owl_global_get_newmsgproc(&g));
3172      } else if (myargc > 0) {
3173        /* Spawn the child. */
3174        pid_t pid;
3175        GError *error = NULL;
3176        owl_function_debugmsg("About to exec \"%s\" with %d arguments", argv[0], myargc);
3177        if (g_spawn_async(NULL, argv, NULL,
3178                          G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD,
3179                          NULL, NULL, &pid, &error)) {
3180          owl_global_set_newmsgproc_pid(&g, pid);
3181          owl_function_debugmsg("I'm the parent and I started a new newmsgproc with pid %i", pid);
3182        } else {
3183          owl_function_debugmsg("Cannot run newmsgproc '%s': %s",
3184                                owl_global_get_newmsgproc(&g), error->message);
3185          g_error_free(error);
3186        }
3187      }
3188      g_strfreev(argv);
3189    }
3190  }
3191}
3192
3193/* print the xterm escape sequence to raise the window */
3194void owl_function_xterm_raise(void)
3195{
3196  printf("\033[5t");
3197}
3198
3199/* print the xterm escape sequence to deiconify the window */
3200void owl_function_xterm_deiconify(void)
3201{
3202  printf("\033[1t");
3203}
3204
3205/* Add the specified command to the startup file.  Eventually this
3206 * should be clever, and rewriting settings that will obviosly
3207 * override earlier settings with 'set' 'bindkey' and 'alias'
3208 * commands.  For now though we just remove any line that would
3209 * duplicate this one and then append this line to the end of
3210 * startupfile.
3211 */
3212void owl_function_addstartup(const char *buff)
3213{
3214  FILE *file;
3215  const char *filename;
3216
3217  filename=owl_global_get_startupfile(&g);
3218
3219  /* delete earlier copies, if the file exists */
3220  if (g_file_test(filename, G_FILE_TEST_EXISTS))
3221    owl_util_file_deleteline(filename, buff, 1);
3222
3223  file=fopen(filename, "a");
3224  if (!file) {
3225    owl_function_error("Error opening startupfile for new command");
3226    return;
3227  }
3228
3229  /* add this line */
3230  fprintf(file, "%s\n", buff);
3231
3232  fclose(file);
3233}
3234
3235/* Remove the specified command from the startup file. */
3236void owl_function_delstartup(const char *buff)
3237{
3238  const char *filename;
3239  filename=owl_global_get_startupfile(&g);
3240  owl_util_file_deleteline(filename, buff, 1);
3241}
3242
3243/* Execute owl commands from the given filename.  If the filename
3244 * is NULL, use the default owl startup commands file.
3245 */
3246void owl_function_source(const char *filename)
3247{
3248  char *path;
3249  FILE *file;
3250  char *s = NULL;
3251  int fail_silent = 0;
3252
3253  if (!filename) {
3254    fail_silent = 1;
3255    path = g_strdup(owl_global_get_startupfile(&g));
3256  } else {
3257    path = owl_util_makepath(filename);
3258  }
3259  file = fopen(path, "r");
3260  g_free(path);
3261  if (!file) {
3262    if (!fail_silent) {
3263      owl_function_error("Error opening file: %s", filename);
3264    }
3265    return;
3266  }
3267  while (owl_getline_chomp(&s, file)) {
3268    if (s[0] == '\0' || s[0] == '#')
3269      continue;
3270    owl_function_command_norv(s);
3271  }
3272
3273  g_free(s);
3274  fclose(file);
3275}
3276
3277void owl_function_change_style(owl_view *v, const char *stylename)
3278{
3279  const owl_style *s;
3280
3281  s=owl_global_get_style_by_name(&g, stylename);
3282  if (!s) {
3283    owl_function_error("No style named %s", stylename);
3284    return;
3285  }
3286  owl_view_set_style(v, s);
3287  owl_messagelist_invalidate_formats(owl_global_get_msglist(&g));
3288  owl_function_calculate_topmsg(OWL_DIRECTION_DOWNWARDS);
3289  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
3290}
3291
3292void owl_function_toggleoneline(void)
3293{
3294  owl_view *v;
3295  const owl_style *s;
3296
3297  v=owl_global_get_current_view(&g);
3298  s=owl_view_get_style(v);
3299
3300  if (!owl_style_matches_name(s, "oneline")) {
3301    owl_function_change_style(v, "oneline");
3302  } else {
3303    owl_function_change_style(v, owl_global_get_default_style(&g));
3304  }
3305
3306  owl_messagelist_invalidate_formats(owl_global_get_msglist(&g));
3307  owl_function_calculate_topmsg(OWL_DIRECTION_DOWNWARDS);
3308  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
3309}
3310
3311void G_GNUC_PRINTF(1, 2) owl_function_error(const char *fmt, ...)
3312{
3313  static int in_error = 0;
3314  va_list ap;
3315  char *buff;
3316  const char *nl;
3317
3318  if (++in_error > 2) {
3319    /* More than two nested errors, bail immediately. */
3320    in_error--;
3321    return;
3322  }
3323
3324  va_start(ap, fmt);
3325  buff = g_strdup_vprintf(fmt, ap);
3326  va_end(ap);
3327
3328  owl_function_debugmsg("ERROR: %s", buff);
3329  owl_function_log_err(buff);
3330
3331  nl = strchr(buff, '\n');
3332
3333  /*
3334    Showing admin messages triggers a lot of code. If we have a
3335    recursive error call, that's the most likely candidate, so
3336    suppress the call in that case, to try to avoid infinite looping.
3337  */
3338
3339  if(nl && *(nl + 1) && in_error == 1) {
3340    /* Multiline error */
3341    owl_function_adminmsg("ERROR", buff);
3342  } else {
3343    owl_function_makemsg("[Error] %s", buff);
3344  }
3345
3346  g_free(buff);
3347
3348  in_error--;
3349}
3350
3351void owl_function_log_err(const char *string)
3352{
3353  char *date;
3354  time_t now;
3355  char *buff;
3356
3357  now=time(NULL);
3358  date=g_strdup(ctime(&now));
3359  date[strlen(date)-1]='\0';
3360
3361  buff = g_strdup_printf("%s %s", date, string);
3362
3363  owl_errqueue_append_err(owl_global_get_errqueue(&g), buff);
3364
3365  g_free(buff);
3366  g_free(date);
3367}
3368
3369void owl_function_showerrs(void)
3370{
3371  owl_fmtext fm;
3372
3373  owl_fmtext_init_null(&fm);
3374  owl_fmtext_append_normal(&fm, "Errors:\n\n");
3375  owl_errqueue_to_fmtext(owl_global_get_errqueue(&g), &fm);
3376  owl_function_popless_fmtext(&fm);
3377}
3378
3379void G_GNUC_PRINTF(1, 2) owl_function_makemsg(const char *fmt, ...)
3380{
3381  va_list ap;
3382  char *str;
3383
3384  va_start(ap, fmt);
3385  str = g_strdup_vprintf(fmt, ap);
3386  va_end(ap);
3387
3388  owl_function_debugmsg("makemsg: %s", str);
3389  owl_msgwin_set_text_nocopy(&g.msgwin, str);
3390}
3391
3392/* get locations for everyone in .anyone.  If 'notify' is '1' then
3393 * send a pseudo login or logout message for everyone not in sync with
3394 * the global zephyr buddy list.  The list is updated regardless of
3395 * the status of 'notify'.
3396 */
3397void owl_function_zephyr_buddy_check(int notify)
3398{
3399#ifdef HAVE_LIBZEPHYR
3400  int i;
3401  GPtrArray *anyone;
3402  GList **zaldlist;
3403  GList *zaldptr;
3404  ZAsyncLocateData_t *zald;
3405  const char *user;
3406
3407  if (!owl_global_is_havezephyr(&g)) return;
3408  owl_global_set_pseudologin_notify(&g, notify);
3409  zaldlist = owl_global_get_zaldlist(&g);
3410
3411  /* Clear the existing ZALDs first. */
3412  zaldptr = g_list_first(*zaldlist);
3413  while (zaldptr) {
3414    ZFreeALD(zaldptr->data);
3415    g_free(zaldptr->data);
3416    zaldptr = g_list_next(zaldptr);
3417  }
3418  g_list_free(*zaldlist);
3419  *zaldlist = NULL;
3420
3421  anyone = owl_zephyr_get_anyone_list(NULL);
3422  for (i = 0; i < anyone->len; i++) {
3423    user = anyone->pdata[i];
3424    zald = g_new(ZAsyncLocateData_t, 1);
3425    if (ZRequestLocations(zstr(user), zald, UNACKED, ZAUTH) == ZERR_NONE) {
3426      *zaldlist = g_list_append(*zaldlist, zald);
3427    } else {
3428      g_free(zald);
3429    }
3430  }
3431
3432  owl_ptr_array_free(anyone, g_free);
3433#endif
3434}
3435
3436void owl_function_aimsearch_results(const char *email, GPtrArray *namelist)
3437{
3438  owl_fmtext fm;
3439  int i;
3440
3441  owl_fmtext_init_null(&fm);
3442  owl_fmtext_append_normal(&fm, "AIM screennames associated with ");
3443  owl_fmtext_append_normal(&fm, email);
3444  owl_fmtext_append_normal(&fm, ":\n");
3445
3446  for (i = 0; i < namelist->len; i++) {
3447    owl_fmtext_append_normal(&fm, "  ");
3448    owl_fmtext_append_normal(&fm, namelist->pdata[i]);
3449    owl_fmtext_append_normal(&fm, "\n");
3450  }
3451
3452  owl_function_popless_fmtext(&fm);
3453  owl_fmtext_cleanup(&fm);
3454}
3455
3456int owl_function_get_color_count(void)
3457{
3458     return COLORS;
3459}
3460
3461void _owl_function_mark_message(const owl_message *m)
3462{
3463  if (m) {
3464    owl_global_set_markedmsgid(&g, owl_message_get_id(m));
3465    owl_mainwin_redisplay(owl_global_get_mainwin(&g));
3466  }
3467}
3468
3469void owl_function_mark_message(void)
3470{
3471  const owl_message *m;
3472  const owl_view *v;
3473
3474  v=owl_global_get_current_view(&g);
3475
3476  /* bail if there's no current message */
3477  if (owl_view_get_size(v) < 1) {
3478    owl_function_error("No messages to mark");
3479    return;
3480  }
3481
3482  /* mark the message */
3483  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
3484  _owl_function_mark_message(m);
3485  owl_function_makemsg("Mark set");
3486}
3487
3488void owl_function_swap_cur_marked(void)
3489{
3490  int marked_id;
3491  const owl_message *m;
3492  const owl_view *v;
3493
3494  marked_id=owl_global_get_markedmsgid(&g);
3495  if (marked_id == -1) {
3496    owl_function_error("Mark not set.");
3497    return;
3498  }
3499
3500  v=owl_global_get_current_view(&g);
3501  /* bail if there's no current message */
3502  if (owl_view_get_size(v) < 1) {
3503    return;
3504  }
3505
3506  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
3507  _owl_function_mark_message(m);
3508  owl_global_set_curmsg(&g, owl_view_get_nearest_to_msgid(v, marked_id));
3509  owl_function_calculate_topmsg(OWL_DIRECTION_NONE);
3510  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
3511  owl_global_set_direction_downwards(&g);
3512}
Note: See TracBrowser for help on using the repository browser.