source: functions.c @ 3381399

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