source: functions.c @ 451becb

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