source: functions.c @ b343c2c

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