source: functions.c @ 8402a093

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