source: functions.c @ 8a5b5a1

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