source: functions.c @ ccafe64

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