source: functions.c @ 440ce01

barnowl_perlaimdebianowlrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 440ce01 was 440ce01, checked in by James M. Kretchmar <kretch@mit.edu>, 21 years ago
added owl_prototypes.h to repository fix outgoing aim message to send the real text display sender and recipient of owl messages
  • Property mode set to 100644
File size: 73.7 KB
Line 
1#include <stdio.h>
2#include <stdlib.h>
3#include <unistd.h>
4#include <signal.h>
5#include <string.h>
6#include <com_err.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
62char *owl_function_cmd_describe(void *name)
63{
64  owl_cmd *cmd = owl_cmddict_find(owl_global_get_cmddict(&g), name);
65  if (cmd) return owl_cmd_describe(cmd);
66  else return(NULL);
67}
68
69void owl_function_show_command(char *name)
70{
71  owl_function_help_for_command(name);
72}
73
74void owl_function_adminmsg(char *header, char *body)
75{
76  owl_message *m;
77  int followlast;
78
79  followlast=owl_global_should_followlast(&g);
80  m=owl_malloc(sizeof(owl_message));
81  owl_message_create_admin(m, header, body);
82  owl_messagelist_append_element(owl_global_get_msglist(&g), m);
83  owl_view_consider_message(owl_global_get_current_view(&g), m);
84
85  if (followlast) owl_function_lastmsg_noredisplay();
86
87  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
88  if (owl_popwin_is_active(owl_global_get_popwin(&g))) {
89    owl_popwin_refresh(owl_global_get_popwin(&g));
90  }
91 
92  wnoutrefresh(owl_global_get_curs_recwin(&g));
93  owl_global_set_needrefresh(&g);
94}
95
96void owl_function_make_outgoing_zephyr(char *body, char *zwriteline, char *zsig)
97{
98  owl_message *m;
99  int followlast;
100  owl_zwrite z;
101 
102  followlast=owl_global_should_followlast(&g);
103
104  /* create a zwrite for the purpose of filling in other message fields */
105  owl_zwrite_create_from_line(&z, zwriteline);
106
107  /* create the message */
108  m=owl_malloc(sizeof(owl_message));
109  owl_message_create_from_zwriteline(m, zwriteline, body, zsig);
110
111  /* add it to the global list and current view */
112  owl_messagelist_append_element(owl_global_get_msglist(&g), m);
113  owl_view_consider_message(owl_global_get_current_view(&g), m);
114
115  if (followlast) owl_function_lastmsg_noredisplay();
116
117  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
118  if (owl_popwin_is_active(owl_global_get_popwin(&g))) {
119    owl_popwin_refresh(owl_global_get_popwin(&g));
120  }
121 
122  wnoutrefresh(owl_global_get_curs_recwin(&g));
123  owl_global_set_needrefresh(&g);
124  owl_zwrite_free(&z);
125}
126
127void owl_function_make_outgoing_aim(char *body, char *to)
128{
129  owl_message *m;
130  int followlast;
131 
132  followlast=owl_global_should_followlast(&g);
133
134  /* create the message */
135  m=owl_malloc(sizeof(owl_message));
136  owl_message_create_aim(m, owl_global_get_aim_screenname(&g), to, body);
137  owl_message_set_direction_out(m);
138
139  /* add it to the global list and current view */
140  owl_messagelist_append_element(owl_global_get_msglist(&g), m);
141  owl_view_consider_message(owl_global_get_current_view(&g), m);
142
143  if (followlast) owl_function_lastmsg_noredisplay();
144
145  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
146  if (owl_popwin_is_active(owl_global_get_popwin(&g))) {
147    owl_popwin_refresh(owl_global_get_popwin(&g));
148  }
149 
150  wnoutrefresh(owl_global_get_curs_recwin(&g));
151  owl_global_set_needrefresh(&g);
152}
153
154void owl_function_zwrite_setup(char *line)
155{
156  owl_editwin *e;
157  char buff[1024];
158  owl_zwrite z;
159  int ret;
160
161  /* check the arguments */
162  ret=owl_zwrite_create_from_line(&z, line);
163  if (ret) {
164    owl_function_makemsg("Error in zwrite arugments");
165    owl_zwrite_free(&z);
166    return;
167  }
168
169  /* send a ping if necessary */
170  if (owl_global_is_txping(&g)) {
171    owl_zwrite_send_ping(&z);
172  }
173  owl_zwrite_free(&z);
174
175  /* create and setup the editwin */
176  e=owl_global_get_typwin(&g);
177  owl_editwin_new_style(e, OWL_EDITWIN_STYLE_MULTILINE, owl_global_get_msg_history(&g));
178
179  if (!owl_global_get_lockout_ctrld(&g)) {
180    owl_function_makemsg("Type your zephyr below.  End with ^D or a dot on a line by itself.  ^C will quit.");
181  } else {
182    owl_function_makemsg("Type your zephyr below.  End with a dot on a line by itself.  ^C will quit.");
183  }
184
185  owl_editwin_clear(e);
186  owl_editwin_set_dotsend(e);
187  strcpy(buff, "----> ");
188  strcat(buff, line);
189  strcat(buff, "\n");
190  owl_editwin_set_locktext(e, buff);
191
192  /* make it active */
193  owl_global_set_typwin_active(&g);
194}
195
196void owl_function_aimwrite_setup(char *line)
197{
198  owl_editwin *e;
199  char buff[1024];
200
201  /* check the arguments */
202
203  /* create and setup the editwin */
204  e=owl_global_get_typwin(&g);
205  owl_editwin_new_style(e, OWL_EDITWIN_STYLE_MULTILINE, owl_global_get_msg_history(&g));
206
207  if (!owl_global_get_lockout_ctrld(&g)) {
208    owl_function_makemsg("Type your message below.  End with ^D or a dot on a line by itself.  ^C will quit.");
209  } else {
210    owl_function_makemsg("Type your message below.  End with a dot on a line by itself.  ^C will quit.");
211  }
212
213  owl_editwin_clear(e);
214  owl_editwin_set_dotsend(e);
215  strcpy(buff, "----> ");
216  strcat(buff, line);
217  strcat(buff, "\n");
218  owl_editwin_set_locktext(e, buff);
219
220  /* make it active */
221  owl_global_set_typwin_active(&g);
222}
223
224
225
226void owl_function_zcrypt_setup(char *line)
227{
228  owl_editwin *e;
229  char buff[1024];
230  owl_zwrite z;
231  int ret;
232
233  /* check the arguments */
234  ret=owl_zwrite_create_from_line(&z, line);
235  if (ret) {
236    owl_function_makemsg("Error in zwrite arugments");
237    owl_zwrite_free(&z);
238    return;
239  }
240
241  if (owl_zwrite_get_numrecips(&z)>0) {
242    owl_function_makemsg("You may not specifiy a recipient for a zcrypt message");
243    owl_zwrite_free(&z);
244    return;
245  }
246
247  /* send a ping if necessary */
248  if (owl_global_is_txping(&g)) {
249    owl_zwrite_send_ping(&z);
250  }
251  owl_zwrite_free(&z);
252
253  /* create and setup the editwin */
254  e=owl_global_get_typwin(&g);
255  owl_editwin_new_style(e, OWL_EDITWIN_STYLE_MULTILINE, owl_global_get_msg_history(&g));
256
257  if (!owl_global_get_lockout_ctrld(&g)) {
258    owl_function_makemsg("Type your zephyr below.  End with ^D or a dot on a line by itself.  ^C will quit.");
259  } else {
260    owl_function_makemsg("Type your zephyr below.  End with a dot on a line by itself.  ^C will quit.");
261  }
262
263  owl_editwin_clear(e);
264  owl_editwin_set_dotsend(e);
265  strcpy(buff, "----> ");
266  strcat(buff, line);
267  strcat(buff, "\n");
268  owl_editwin_set_locktext(e, buff);
269
270  /* make it active */
271  owl_global_set_typwin_active(&g);
272}
273
274void owl_function_zwrite(char *line)
275{
276  owl_zwrite z;
277  int i, j;
278
279  /* create the zwrite and send the message */
280  owl_zwrite_create_from_line(&z, line);
281  owl_zwrite_send_message(&z, owl_editwin_get_text(owl_global_get_typwin(&g)));
282  owl_function_makemsg("Waiting for ack...");
283
284  /* display the message as an outgoing message in the receive window */
285  if (owl_global_is_displayoutgoing(&g) && owl_zwrite_is_personal(&z)) {
286    owl_function_make_outgoing_zephyr(owl_editwin_get_text(owl_global_get_typwin(&g)), line, owl_zwrite_get_zsig(&z));
287  }
288
289  /* log it if we have logging turned on */
290  if (owl_global_is_logging(&g) && owl_zwrite_is_personal(&z)) {
291    j=owl_zwrite_get_numrecips(&z);
292    for (i=0; i<j; i++) {
293      owl_log_outgoing(owl_zwrite_get_recip_n(&z, i),
294                       owl_editwin_get_text(owl_global_get_typwin(&g)));
295    }
296  }
297
298  /* free the zwrite */
299  owl_zwrite_free(&z);
300}
301
302
303void owl_function_aimwrite(char *to)
304{
305  /*  send the message */
306  owl_aim_send_im(to, owl_editwin_get_text(owl_global_get_typwin(&g)));
307  owl_function_makemsg("AIM message sent.");
308
309  /* display the message as an outgoing message in the receive window */
310  if (owl_global_is_displayoutgoing(&g)) {
311    owl_function_make_outgoing_aim(owl_editwin_get_text(owl_global_get_typwin(&g)), to);
312  }
313
314  /* not yet */
315#if 0
316  /* log it if we have logging turned on */
317  if (owl_global_is_logging(&g)) {
318    owl_log_outgoing(to, owl_editwin_get_text(owl_global_get_typwin(&g)));
319  }
320#endif
321}
322
323
324
325/* If filter is non-null, looks for the next message matching
326 * that filter.  If skip_deleted, skips any deleted messages.
327 * If last_if_none, will stop at the last message in the view
328 * if no matching messages are found.  */
329void owl_function_nextmsg_full(char *filter, int skip_deleted, int last_if_none)
330{
331  int curmsg, i, viewsize, found;
332  owl_view *v;
333  owl_filter *f = NULL;
334  owl_message *m;
335
336  v=owl_global_get_current_view(&g);
337
338  if (filter) {
339    f=owl_global_get_filter(&g, filter);
340    if (!f) {
341      owl_function_makemsg("No %s filter defined", filter);
342      return;
343    }
344  }
345
346  curmsg=owl_global_get_curmsg(&g);
347  viewsize=owl_view_get_size(v);
348  found=0;
349
350  /* just check to make sure we're in bounds... */
351  if (curmsg>viewsize-1) curmsg=viewsize-1;
352  if (curmsg<0) curmsg=0;
353
354  for (i=curmsg+1; i<viewsize; i++) {
355    m=owl_view_get_element(v, i);
356    if (skip_deleted && owl_message_is_delete(m)) continue;
357    if (f && !owl_filter_message_match(f, m)) continue;
358    found = 1;
359    break;
360  }
361
362  if (i>owl_view_get_size(v)-1) i=owl_view_get_size(v)-1;
363
364  if (!found) {
365    owl_function_makemsg("already at last%s message%s%s",
366                         skip_deleted?" non-deleted":"",
367                         filter?" in ":"", filter?filter:"");
368    /* if (!skip_deleted) owl_function_beep(); */
369  }
370
371  if (last_if_none || found) {
372    owl_global_set_curmsg(&g, i);
373    owl_function_calculate_topmsg(OWL_DIRECTION_DOWNWARDS);
374    owl_mainwin_redisplay(owl_global_get_mainwin(&g));
375    owl_global_set_direction_downwards(&g);
376  }
377}
378
379void owl_function_prevmsg_full(char *filter, int skip_deleted, int first_if_none)
380{
381  int curmsg, i, viewsize, found;
382  owl_view *v;
383  owl_filter *f = NULL;
384  owl_message *m;
385
386  v=owl_global_get_current_view(&g);
387
388  if (filter) {
389    f=owl_global_get_filter(&g, filter);
390    if (!f) {
391      owl_function_makemsg("No %s filter defined", filter);
392      return;
393    }
394  }
395
396  curmsg=owl_global_get_curmsg(&g);
397  viewsize=owl_view_get_size(v);
398  found=0;
399
400  /* just check to make sure we're in bounds... */
401  if (curmsg<0) curmsg=0;
402
403  for (i=curmsg-1; i>=0; i--) {
404    m=owl_view_get_element(v, i);
405    if (skip_deleted && owl_message_is_delete(m)) continue;
406    if (f && !owl_filter_message_match(f, m)) continue;
407    found = 1;
408    break;
409  }
410
411  if (i<0) i=0;
412
413  if (!found) {
414    owl_function_makemsg("already at first%s message%s%s",
415                         skip_deleted?" non-deleted":"",
416                         filter?" in ":"", filter?filter:"");
417    /* if (!skip_deleted) owl_function_beep(); */
418  }
419
420  if (first_if_none || found) {
421    owl_global_set_curmsg(&g, i);
422    owl_function_calculate_topmsg(OWL_DIRECTION_UPWARDS);
423    owl_mainwin_redisplay(owl_global_get_mainwin(&g));
424    owl_global_set_direction_upwards(&g);
425  }
426}
427
428void owl_function_nextmsg()
429{
430  owl_function_nextmsg_full(NULL, 0, 1);
431}
432
433
434void owl_function_prevmsg()
435{
436  owl_function_prevmsg_full(NULL, 0, 1);
437}
438
439void owl_function_nextmsg_notdeleted()
440{
441  owl_function_nextmsg_full(NULL, 1, 1);
442}
443
444void owl_function_prevmsg_notdeleted()
445{
446  owl_function_prevmsg_full(NULL, 1, 1);
447}
448
449
450void owl_function_nextmsg_personal()
451{
452  owl_function_nextmsg_full("personal", 0, 0);
453}
454
455void owl_function_prevmsg_personal()
456{
457  owl_function_prevmsg_full("personal", 0, 0);
458}
459
460
461/* if move_after is 1, moves after the delete */
462void owl_function_deletecur(int move_after)
463{
464  int curmsg;
465  owl_view *v;
466
467  v=owl_global_get_current_view(&g);
468
469  /* bail if there's no current message */
470  if (owl_view_get_size(v) < 1) {
471    owl_function_makemsg("No current message to delete");
472    return;
473  }
474
475  /* mark the message for deletion */
476  curmsg=owl_global_get_curmsg(&g);
477  owl_view_delete_element(v, curmsg);
478
479  if (move_after) {
480    /* move the poiner in the appropriate direction
481     * to the next undeleted msg */
482    if (owl_global_get_direction(&g)==OWL_DIRECTION_UPWARDS) {
483      owl_function_prevmsg_notdeleted();
484    } else {
485      owl_function_nextmsg_notdeleted();
486    }
487  }
488}
489
490
491void owl_function_undeletecur(int move_after)
492{
493  int curmsg;
494  owl_view *v;
495
496  v=owl_global_get_current_view(&g);
497 
498  if (owl_view_get_size(v) < 1) {
499    owl_function_makemsg("No current message to undelete");
500    return;
501  }
502  curmsg=owl_global_get_curmsg(&g);
503
504  owl_view_undelete_element(v, curmsg);
505
506  if (move_after) {
507    if (owl_global_get_direction(&g)==OWL_DIRECTION_UPWARDS) {
508      if (curmsg>0) {
509        owl_function_prevmsg();
510      } else {
511        owl_function_nextmsg();
512      }
513    } else {
514      owl_function_nextmsg();
515    }
516  }
517
518  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
519}
520
521
522void owl_function_expunge()
523{
524  int curmsg;
525  owl_message *m;
526  owl_messagelist *ml;
527  owl_view *v;
528  int lastmsgid=0;
529
530  curmsg=owl_global_get_curmsg(&g);
531  v=owl_global_get_current_view(&g);
532  ml=owl_global_get_msglist(&g);
533
534  m=owl_view_get_element(v, curmsg);
535  if (m) lastmsgid = owl_message_get_id(m);
536
537  /* expunge the message list */
538  owl_messagelist_expunge(ml);
539
540  /* update all views (we only have one right now) */
541  owl_view_recalculate(v);
542
543  /* find where the new position should be
544     (as close as possible to where we last where) */
545  curmsg = owl_view_get_nearest_to_msgid(v, lastmsgid);
546  if (curmsg>owl_view_get_size(v)-1) curmsg = owl_view_get_size(v)-1;
547  if (curmsg<0) curmsg = 0;
548  owl_global_set_curmsg(&g, curmsg);
549  owl_function_calculate_topmsg(OWL_DIRECTION_NONE);
550  /* if there are no messages set the direction to down in case we
551     delete everything upwards */
552  owl_global_set_direction_downwards(&g);
553 
554  owl_function_makemsg("Messages expunged");
555  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
556}
557
558
559void owl_function_firstmsg()
560{
561  owl_global_set_curmsg(&g, 0);
562  owl_global_set_topmsg(&g, 0);
563  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
564  owl_global_set_direction_downwards(&g);
565}
566
567void owl_function_lastmsg_noredisplay()
568{
569  int oldcurmsg, curmsg;
570  owl_view *v;
571
572  v=owl_global_get_current_view(&g);
573  oldcurmsg=owl_global_get_curmsg(&g);
574  curmsg=owl_view_get_size(v)-1; 
575  if (curmsg<0) curmsg=0;
576  owl_global_set_curmsg(&g, curmsg);
577  if (oldcurmsg < curmsg) {
578    owl_function_calculate_topmsg(OWL_DIRECTION_DOWNWARDS);
579  } else if (curmsg<owl_view_get_size(v)) {
580    /* If already at the end, blank the screen and move curmsg
581     * past the end of the messages. */
582    owl_global_set_topmsg(&g, curmsg+1);
583    owl_global_set_curmsg(&g, curmsg+1);
584  } 
585  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
586  owl_global_set_direction_downwards(&g);
587}
588
589void owl_function_lastmsg()
590{
591  owl_function_lastmsg_noredisplay();
592  owl_mainwin_redisplay(owl_global_get_mainwin(&g)); 
593}
594
595void owl_function_shift_right()
596{
597  owl_global_set_rightshift(&g, owl_global_get_rightshift(&g)+10);
598  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
599  owl_global_set_needrefresh(&g);
600}
601
602
603void owl_function_shift_left()
604{
605  int shift;
606
607  shift=owl_global_get_rightshift(&g);
608  if (shift>=10) {
609    owl_global_set_rightshift(&g, shift-10);
610    owl_mainwin_redisplay(owl_global_get_mainwin(&g));
611    owl_global_set_needrefresh(&g);
612  } else {
613    owl_function_beep();
614    owl_function_makemsg("Already full left");
615  }
616}
617
618
619void owl_function_unsuball()
620{
621  unsuball();
622  owl_function_makemsg("Unsubscribed from all messages.");
623}
624
625void owl_function_loadsubs(char *file)
626{
627  int ret;
628
629  ret=owl_zephyr_loadsubs(file);
630
631  if (!owl_context_is_interactive(owl_global_get_context(&g))) return;
632  if (ret==0) {
633    owl_function_makemsg("Subscribed to messages from file.");
634  } else if (ret==-1) {
635    owl_function_makemsg("Could not open file.");
636  } else {
637    owl_function_makemsg("Error subscribing to messages from file.");
638  }
639}
640
641void owl_function_loadloginsubs(char *file)
642{
643  int ret;
644
645  ret=owl_zephyr_loadloginsubs(file);
646
647  if (!owl_context_is_interactive(owl_global_get_context(&g))) return;
648  if (ret==0) {
649    owl_function_makemsg("Subscribed to login messages from file.");
650  } else if (ret==-1) {
651    owl_function_makemsg("Could not open file for login subscriptions.");
652  } else {
653    owl_function_makemsg("Error subscribing to login messages from file.");
654  }
655}
656
657void owl_function_suspend()
658{
659  endwin();
660  printf("\n");
661  kill(getpid(), SIGSTOP);
662
663  /* resize to reinitialize all the windows when we come back */
664  owl_command_resize();
665}
666
667void owl_function_zaway_toggle()
668{
669  if (!owl_global_is_zaway(&g)) {
670    owl_global_set_zaway_msg(&g, owl_global_get_zaway_msg_default(&g));
671    owl_function_zaway_on();
672  } else {
673    owl_function_zaway_off();
674  }
675}
676
677void owl_function_zaway_on()
678{
679  owl_global_set_zaway_on(&g);
680  owl_function_makemsg("zaway set (%s)", owl_global_get_zaway_msg(&g));
681}
682
683void owl_function_zaway_off()
684{
685  owl_global_set_zaway_off(&g);
686  owl_function_makemsg("zaway off");
687}
688
689void owl_function_quit()
690{
691  char *ret;
692 
693  /* zlog out if we need to */
694  if (owl_global_is_shutdownlogout(&g)) {
695    owl_function_zlog_out();
696  }
697
698  /* execute the commands in shutdown */
699  ret = owl_config_execute("owl::shutdown();");
700  if (ret) owl_free(ret);
701
702  /* signal our child process, if any */
703  if (owl_global_get_newmsgproc_pid(&g)) {
704    kill(owl_global_get_newmsgproc_pid(&g), SIGHUP);
705  }
706
707  /* final clean up */
708  unsuball();
709  ZClosePort();
710  endwin();
711  owl_function_debugmsg("Quitting Owl");
712  exit(0);
713}
714
715
716void owl_function_zlog_in()
717{
718  char *exposure, *eset;
719  int ret;
720
721  eset=EXPOSE_REALMVIS;
722  exposure=ZGetVariable("exposure");
723  if (exposure==NULL) {
724    eset=EXPOSE_REALMVIS;
725  } else if (!strcasecmp(exposure,EXPOSE_NONE)) {
726    eset = EXPOSE_NONE;
727  } else if (!strcasecmp(exposure,EXPOSE_OPSTAFF)) {
728    eset = EXPOSE_OPSTAFF;
729  } else if (!strcasecmp(exposure,EXPOSE_REALMVIS)) {
730    eset = EXPOSE_REALMVIS;
731  } else if (!strcasecmp(exposure,EXPOSE_REALMANN)) {
732    eset = EXPOSE_REALMANN;
733  } else if (!strcasecmp(exposure,EXPOSE_NETVIS)) {
734    eset = EXPOSE_NETVIS;
735  } else if (!strcasecmp(exposure,EXPOSE_NETANN)) {
736    eset = EXPOSE_NETANN;
737  }
738   
739  ret=ZSetLocation(eset);
740  if (ret != ZERR_NONE) {
741    /*
742      char buff[LINE];
743      sprintf(buff, "Error setting location: %s", error_message(ret));
744      owl_function_makemsg(buff);
745    */
746  }
747}
748
749void owl_function_zlog_out()
750{
751  int ret;
752 
753  ret=ZUnsetLocation();
754  if (ret != ZERR_NONE) {
755    /*
756      char buff[LINE];
757      sprintf(buff, "Error unsetting location: %s", error_message(ret));
758      owl_function_makemsg(buff);
759    */
760  }
761}
762
763
764void owl_function_makemsg(char *fmt, ...)
765{
766  va_list ap;
767  char buff[2048];
768
769  va_start(ap, fmt);
770  werase(owl_global_get_curs_msgwin(&g));
771 
772  vsnprintf(buff, 2048, fmt, ap);
773  owl_function_debugmsg("makemsg: %s", buff);
774  waddstr(owl_global_get_curs_msgwin(&g), buff); 
775  wnoutrefresh(owl_global_get_curs_msgwin(&g));
776  owl_global_set_needrefresh(&g);
777  va_end(ap);
778}
779
780void owl_function_errormsg(char *fmt, ...)
781{
782  va_list ap;
783  char buff[2048];
784
785  va_start(ap, fmt);
786  werase(owl_global_get_curs_msgwin(&g));
787 
788  vsnprintf(buff, 2048, fmt, ap);
789  owl_function_debugmsg("ERROR: %s", buff);
790  waddstr(owl_global_get_curs_msgwin(&g), buff); 
791  waddstr(owl_global_get_curs_msgwin(&g), "ERROR: "); 
792  wnoutrefresh(owl_global_get_curs_msgwin(&g));
793  owl_global_set_needrefresh(&g);
794  va_end(ap);
795}
796
797
798void owl_function_openurl()
799{
800  /* visit the first url in the current message */
801  owl_message *m;
802  owl_view *v;
803  char *ptr1, *ptr2, *text, url[LINE], tmpbuff[LINE];
804  int webbrowser;
805
806  webbrowser = owl_global_get_webbrowser(&g);
807
808  if (webbrowser < 0 || webbrowser == OWL_WEBBROWSER_NONE) {
809    owl_function_makemsg("No browser selected");
810    return;
811  }
812
813  v=owl_global_get_current_view(&g);
814 
815  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
816
817  if (!m || owl_view_get_size(v)==0) {
818    owl_function_makemsg("No current message selected");
819    return;
820  }
821
822  text=owl_message_get_text(m);
823
824  /* First look for a good URL */ 
825  if ((ptr1=strstr(text, "http://"))!=NULL) {
826    ptr2=strpbrk(ptr1, " \n\t");
827    if (ptr2) {
828      strncpy(url, ptr1, ptr2-ptr1+1);
829      url[ptr2-ptr1+1]='\0';
830    } else {
831      strcpy(url, ptr1);
832    }
833
834    /* if we had <http strip a trailing > */
835    if (ptr1>text && ptr1[-1]=='<') {
836      if (url[strlen(url)-1]=='>') {
837        url[strlen(url)-1]='\0';
838      }
839    }
840  } else if ((ptr1=strstr(text, "https://"))!=NULL) {
841    /* Look for an https URL */ 
842    ptr2=strpbrk(ptr1, " \n\t");
843    if (ptr2) {
844      strncpy(url, ptr1, ptr2-ptr1+1);
845      url[ptr2-ptr1+1]='\0';
846    } else {
847      strcpy(url, ptr1);
848    }
849   
850    /* if we had <http strip a trailing > */
851    if (ptr1>text && ptr1[-1]=='<') {
852      if (url[strlen(url)-1]=='>') {
853        url[strlen(url)-1]='\0';
854      }
855    }
856  } else if ((ptr1=strstr(text, "www."))!=NULL) {
857    /* if we can't find a real url look for www.something */
858    ptr2=strpbrk(ptr1, " \n\t");
859    if (ptr2) {
860      strncpy(url, ptr1, ptr2-ptr1+1);
861      url[ptr2-ptr1+1]='\0';
862    } else {
863      strcpy(url, ptr1);
864    }
865  } else {
866    owl_function_beep();
867    owl_function_makemsg("Could not find URL to open.");
868    return;
869  }
870
871  /* Make sure there aren't any quotes or \'s in the url */
872  for (ptr1 = url; *ptr1; ptr1++) {
873    if (*ptr1 == '"' || *ptr1 == '\\') {
874      owl_function_beep();
875      owl_function_makemsg("URL contains invalid characters.");
876      return;
877    }
878  }
879 
880  /* NOTE: There are potentially serious security issues here... */
881
882  /* open the page */
883  owl_function_makemsg("Opening %s", url);
884  if (webbrowser == OWL_WEBBROWSER_NETSCAPE) {
885    snprintf(tmpbuff, LINE, "netscape -remote \"openURL(%s)\" > /dev/null 2> /dev/null", url);
886    system(tmpbuff); 
887  } else if (webbrowser == OWL_WEBBROWSER_GALEON) {
888    snprintf(tmpbuff, LINE, "galeon \"%s\" > /dev/null 2> /dev/null &", url);
889    system(tmpbuff); 
890  } else if (webbrowser == OWL_WEBBROWSER_OPERA) {
891    snprintf(tmpbuff, LINE, "opera \"%s\" > /dev/null 2> /dev/null &", url);
892    system(tmpbuff); 
893  }
894}
895
896void owl_function_calculate_topmsg(int direction)
897{
898  int recwinlines, topmsg, curmsg;
899  owl_view *v;
900
901  v=owl_global_get_current_view(&g);
902  curmsg=owl_global_get_curmsg(&g);
903  topmsg=owl_global_get_topmsg(&g);
904  recwinlines=owl_global_get_recwin_lines(&g);
905
906  /*
907  if (owl_view_get_size(v) < 1) {
908    return;
909  }
910  */
911
912  switch (owl_global_get_scrollmode(&g)) {
913  case OWL_SCROLLMODE_TOP:
914    topmsg = owl_function_calculate_topmsg_top(direction, v, curmsg, topmsg, recwinlines);
915    break;
916  case OWL_SCROLLMODE_NEARTOP:
917    topmsg = owl_function_calculate_topmsg_neartop(direction, v, curmsg, topmsg, recwinlines);
918    break;
919  case OWL_SCROLLMODE_CENTER:
920    topmsg = owl_function_calculate_topmsg_center(direction, v, curmsg, topmsg, recwinlines);
921    break;
922  case OWL_SCROLLMODE_PAGED:
923    topmsg = owl_function_calculate_topmsg_paged(direction, v, curmsg, topmsg, recwinlines, 0);
924    break;
925  case OWL_SCROLLMODE_PAGEDCENTER:
926    topmsg = owl_function_calculate_topmsg_paged(direction, v, curmsg, topmsg, recwinlines, 1);
927    break;
928  case OWL_SCROLLMODE_NORMAL:
929  default:
930    topmsg = owl_function_calculate_topmsg_normal(direction, v, curmsg, topmsg, recwinlines);
931  }
932  owl_function_debugmsg("Calculated a topmsg of %i", topmsg);
933  owl_global_set_topmsg(&g, topmsg);
934}
935
936/* Returns what the new topmsg should be. 
937 * Passed the last direction of movement,
938 * the current view,
939 * the current message number in the view,
940 * the top message currently being displayed,
941 * and the number of lines in the recwin.
942 */
943int owl_function_calculate_topmsg_top(int direction, owl_view *v, int curmsg, int topmsg, int recwinlines)
944{
945  return(curmsg);
946}
947
948int owl_function_calculate_topmsg_neartop(int direction, owl_view *v, int curmsg, int topmsg, int recwinlines)
949{
950  if (curmsg>0 
951      && (owl_message_get_numlines(owl_view_get_element(v, curmsg-1))
952          <  recwinlines/2)) {
953    return(curmsg-1);
954  } else {
955    return(curmsg);
956  }
957}
958 
959int owl_function_calculate_topmsg_center(int direction, owl_view *v, int curmsg, int topmsg, int recwinlines)
960{
961  int i, last, lines;
962
963  last = curmsg;
964  lines = 0;
965  for (i=curmsg-1; i>=0; i--) {
966    lines += owl_message_get_numlines(owl_view_get_element(v, i));
967    if (lines > recwinlines/2) break;
968    last = i;
969  }
970  return(last);
971}
972 
973int owl_function_calculate_topmsg_paged(int direction, owl_view *v, int curmsg, int topmsg, int recwinlines, int center_on_page)
974{
975  int i, last, lines, savey;
976 
977  /* If we're off the top of the screen, scroll up such that the
978   * curmsg is near the botton of the screen. */
979  if (curmsg < topmsg) {
980    last = curmsg;
981    lines = 0;
982    for (i=curmsg; i>=0; i--) {
983      lines += owl_message_get_numlines(owl_view_get_element(v, i));
984      if (lines > recwinlines) break;
985    last = i;
986    }
987    if (center_on_page) {
988      return(owl_function_calculate_topmsg_center(direction, v, curmsg, 0, recwinlines));
989    } else {
990      return(last);
991    }
992  }
993
994  /* Find number of lines from top to bottom of curmsg (store in savey) */
995  savey=0;
996  for (i=topmsg; i<=curmsg; i++) {
997    savey+=owl_message_get_numlines(owl_view_get_element(v, i));
998  }
999
1000  /* if we're off the bottom of the screen, scroll down */
1001  if (savey > recwinlines) {
1002    if (center_on_page) {
1003      return(owl_function_calculate_topmsg_center(direction, v, curmsg, 0, recwinlines));
1004    } else {
1005      return(curmsg);
1006    }
1007  }
1008
1009  /* else just stay as we are... */
1010  return(topmsg);
1011}
1012
1013
1014int owl_function_calculate_topmsg_normal(int direction, owl_view *v, int curmsg, int topmsg, int recwinlines)
1015{
1016  int savey, j, i, foo, y;
1017
1018  if (curmsg<0) return(topmsg);
1019   
1020  /* If we're off the top of the screen then center */
1021  if (curmsg<topmsg) {
1022    topmsg=owl_function_calculate_topmsg_center(direction, v, curmsg, 0, recwinlines);
1023  }
1024
1025  /* Find number of lines from top to bottom of curmsg (store in savey) */
1026  savey=0;
1027  for (i=topmsg; i<=curmsg; i++) {
1028    savey+=owl_message_get_numlines(owl_view_get_element(v, i));
1029  }
1030
1031  /* If we're off the bottom of the screen, set the topmsg to curmsg
1032   * and scroll upwards */
1033  if (savey > recwinlines) {
1034    topmsg=curmsg;
1035    savey=owl_message_get_numlines(owl_view_get_element(v, i));
1036    direction=OWL_DIRECTION_UPWARDS;
1037  }
1038 
1039  /* If our bottom line is less than 1/4 down the screen then scroll up */
1040  if (direction == OWL_DIRECTION_UPWARDS || direction == OWL_DIRECTION_NONE) {
1041    if (savey < (recwinlines / 4)) {
1042      y=0;
1043      for (j=curmsg; j>=0; j--) {
1044        foo=owl_message_get_numlines(owl_view_get_element(v, j));
1045        /* will we run the curmsg off the screen? */
1046        if ((foo+y) >= recwinlines) {
1047          j++;
1048          if (j>curmsg) j=curmsg;
1049          break;
1050        }
1051        /* have saved 1/2 the screen space? */
1052        y+=foo;
1053        if (y > (recwinlines / 2)) break;
1054      }
1055      if (j<0) j=0;
1056      return(j);
1057    }
1058  }
1059
1060  if (direction == OWL_DIRECTION_DOWNWARDS || direction == OWL_DIRECTION_NONE) {
1061    /* If curmsg bottom line is more than 3/4 down the screen then scroll down */
1062    if (savey > ((recwinlines * 3)/4)) {
1063      y=0;
1064      /* count lines from the top until we can save 1/2 the screen size */
1065      for (j=topmsg; j<curmsg; j++) {
1066        y+=owl_message_get_numlines(owl_view_get_element(v, j));
1067        if (y > (recwinlines / 2)) break;
1068      }
1069      if (j==curmsg) {
1070        j--;
1071      }
1072      return(j+1);
1073    }
1074  }
1075
1076  return(topmsg);
1077}
1078
1079
1080void owl_function_resize()
1081{
1082  owl_global_set_resize_pending(&g);
1083}
1084
1085
1086void owl_function_run_buffercommand()
1087{
1088  char *buff;
1089
1090  buff=owl_global_get_buffercommand(&g);
1091  if (!strncmp(buff, "zwrite ", 7)) {
1092    owl_function_zwrite(buff);
1093  } else if (!strncmp(buff, "aimwrite ", 9)) {
1094    owl_function_aimwrite(buff+9);
1095  }
1096}
1097
1098void owl_function_debugmsg(char *fmt, ...)
1099{
1100  FILE *file;
1101  time_t now;
1102  char buff1[LINE], buff2[LINE];
1103  va_list ap;
1104  va_start(ap, fmt);
1105
1106  if (!owl_global_is_debug_fast(&g)) return;
1107
1108  file=fopen(owl_global_get_debug_file(&g), "a");
1109  if (!file) return;
1110
1111  now=time(NULL);
1112  strcpy(buff1, ctime(&now));
1113  buff1[strlen(buff1)-1]='\0';
1114
1115  owl_global_get_runtime_string(&g, buff2);
1116 
1117  fprintf(file, "[%i -  %s - %s]: ", (int) getpid(), buff1, buff2);
1118  vfprintf(file, fmt, ap);
1119  fprintf(file, "\n");
1120  fclose(file);
1121
1122  va_end(ap);
1123}
1124
1125
1126void owl_function_refresh()
1127{
1128  owl_function_resize();
1129}
1130
1131void owl_function_beep()
1132{
1133  if (owl_global_is_bell(&g)) {
1134    beep();
1135    owl_global_set_needrefresh(&g); /* do we really need this? */
1136  }
1137}
1138
1139
1140void owl_function_subscribe(char *class, char *inst, char *recip)
1141{
1142  int ret;
1143
1144  ret=owl_zephyr_sub(class, inst, recip);
1145  if (ret) {
1146    owl_function_makemsg("Error subscribing.");
1147  } else {
1148    owl_function_makemsg("Subscribed.");
1149  }
1150}
1151
1152
1153void owl_function_unsubscribe(char *class, char *inst, char *recip)
1154{
1155  int ret;
1156
1157  ret=owl_zephyr_unsub(class, inst, recip);
1158  if (ret) {
1159    owl_function_makemsg("Error subscribing.");
1160  } else {
1161    owl_function_makemsg("Unsubscribed.");
1162  }
1163}
1164
1165
1166void owl_function_set_cursor(WINDOW *win)
1167{
1168  wnoutrefresh(win);
1169}
1170
1171
1172void owl_function_full_redisplay()
1173{
1174  redrawwin(owl_global_get_curs_recwin(&g));
1175  redrawwin(owl_global_get_curs_sepwin(&g));
1176  redrawwin(owl_global_get_curs_typwin(&g));
1177  redrawwin(owl_global_get_curs_msgwin(&g));
1178
1179  wnoutrefresh(owl_global_get_curs_recwin(&g));
1180  wnoutrefresh(owl_global_get_curs_sepwin(&g));
1181  wnoutrefresh(owl_global_get_curs_typwin(&g));
1182  wnoutrefresh(owl_global_get_curs_msgwin(&g));
1183 
1184  sepbar("");
1185  owl_function_makemsg("");
1186
1187  owl_global_set_needrefresh(&g);
1188}
1189
1190
1191void owl_function_popless_text(char *text)
1192{
1193  owl_popwin *pw;
1194  owl_viewwin *v;
1195
1196  pw=owl_global_get_popwin(&g);
1197  v=owl_global_get_viewwin(&g);
1198
1199  owl_popwin_up(pw);
1200  owl_viewwin_init_text(v, owl_popwin_get_curswin(pw),
1201                        owl_popwin_get_lines(pw), owl_popwin_get_cols(pw),
1202                        text);
1203  owl_popwin_refresh(pw);
1204  owl_viewwin_redisplay(v, 0);
1205  owl_global_set_needrefresh(&g);
1206}
1207
1208
1209void owl_function_popless_fmtext(owl_fmtext *fm)
1210{
1211  owl_popwin *pw;
1212  owl_viewwin *v;
1213
1214  pw=owl_global_get_popwin(&g);
1215  v=owl_global_get_viewwin(&g);
1216
1217  owl_popwin_up(pw);
1218  owl_viewwin_init_fmtext(v, owl_popwin_get_curswin(pw),
1219                   owl_popwin_get_lines(pw), owl_popwin_get_cols(pw),
1220                   fm);
1221  owl_popwin_refresh(pw);
1222  owl_viewwin_redisplay(v, 0);
1223  owl_global_set_needrefresh(&g);
1224}
1225
1226void owl_function_about()
1227{
1228  char buff[5000];
1229
1230  sprintf(buff, "This is owl version %s\n", OWL_VERSION_STRING);
1231  strcat(buff, "\nOwl was written by James Kretchmar at the Massachusetts\n");
1232  strcat(buff, "Institute of Technology.  The first version, 0.5, was\n");
1233  strcat(buff, "released in March 2002.\n");
1234  strcat(buff, "\n");
1235  strcat(buff, "The name 'owl' was chosen in reference to the owls in the\n");
1236  strcat(buff, "Harry Potter novels, who are tasked with carrying messages\n");
1237  strcat(buff, "between Witches and Wizards.\n");
1238  strcat(buff, "\n");
1239  strcat(buff, "Copyright 2002 Massachusetts Institute of Technology\n");
1240  strcat(buff, "\n");
1241  strcat(buff, "Permission to use, copy, modify, and distribute this\n");
1242  strcat(buff, "software and its documentation for any purpose and without\n");
1243  strcat(buff, "fee is hereby granted, provided that the above copyright\n");
1244  strcat(buff, "notice and this permission notice appear in all copies\n");
1245  strcat(buff, "and in supporting documentation.  No representation is\n");
1246  strcat(buff, "made about the suitability of this software for any\n");
1247  strcat(buff, "purpose.  It is provided \"as is\" without express\n");
1248  strcat(buff, "or implied warranty.\n");
1249  owl_function_popless_text(buff);
1250}
1251
1252void owl_function_info()
1253{
1254  owl_message *m;
1255  owl_fmtext fm;
1256  ZNotice_t *n;
1257  char buff[10000], tmpbuff[1024];
1258  char *ptr;
1259  int i, j, fields, len;
1260  owl_view *v;
1261
1262  owl_fmtext_init_null(&fm);
1263 
1264  v=owl_global_get_current_view(&g);
1265  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
1266  if (!m || owl_view_get_size(v)==0) {
1267    owl_function_makemsg("No message selected\n");
1268    return;
1269  }
1270
1271  owl_fmtext_append_normal(&fm, "Msg Id    : ");
1272  sprintf(buff, "%i", owl_message_get_id(m));
1273  owl_fmtext_append_normal(&fm, buff);
1274  owl_fmtext_append_normal(&fm, "\n");
1275  if (owl_message_is_type_zephyr(m)) {
1276    owl_fmtext_append_normal(&fm, "Type      : zephyr\n");
1277  } else if (owl_message_is_type_admin(m)) {
1278    owl_fmtext_append_normal(&fm, "Type      : admin\n");
1279  } else if (owl_message_is_type_generic(m)) {
1280    owl_fmtext_append_normal(&fm, "Type      : generic\n");
1281  } else if (owl_message_is_type_aim(m)) {
1282    owl_fmtext_append_normal(&fm, "Type      : aim\n");
1283  } else {
1284    owl_fmtext_append_normal(&fm, "Type      : unknown\n");
1285  }
1286  if (owl_message_is_direction_in(m)) {
1287    owl_fmtext_append_normal(&fm, "Direction : in\n");
1288  } else if (owl_message_is_direction_out(m)) {
1289    owl_fmtext_append_normal(&fm, "Direction : out\n");
1290  } else if (owl_message_is_direction_none(m)) {
1291    owl_fmtext_append_normal(&fm, "Direction : none\n");
1292  } else {
1293    owl_fmtext_append_normal(&fm, "Direction : unknown\n");
1294  }
1295  owl_fmtext_append_normal(&fm, "Time      : ");
1296  owl_fmtext_append_normal(&fm, owl_message_get_timestr(m));
1297  owl_fmtext_append_normal(&fm, "\n");
1298
1299  if (owl_message_is_type_zephyr(m)) {
1300
1301    if (owl_message_is_direction_in(m)) {
1302      n=owl_message_get_notice(m);
1303      owl_fmtext_append_normal(&fm, "Class     : ");
1304      owl_fmtext_append_normal(&fm, owl_message_get_class(m));
1305      owl_fmtext_append_normal(&fm, "\n");
1306      owl_fmtext_append_normal(&fm, "Instance  : ");
1307      owl_fmtext_append_normal(&fm, owl_message_get_instance(m));
1308      owl_fmtext_append_normal(&fm, "\n");
1309      owl_fmtext_append_normal(&fm, "Sender    : ");
1310      owl_fmtext_append_normal(&fm, owl_message_get_sender(m));
1311      owl_fmtext_append_normal(&fm, "\n");
1312      owl_fmtext_append_normal(&fm, "Recipient : ");
1313      owl_fmtext_append_normal(&fm, owl_message_get_recipient(m));
1314      owl_fmtext_append_normal(&fm, "\n");
1315      owl_fmtext_append_normal(&fm, "Opcode    : ");
1316      owl_fmtext_append_normal(&fm, owl_message_get_opcode(m));
1317      owl_fmtext_append_normal(&fm, "\n");
1318      owl_fmtext_append_normal(&fm, "Kind      : ");
1319      if (n->z_kind==UNSAFE) {
1320        owl_fmtext_append_normal(&fm, "UNSAFE\n");
1321      } else if (n->z_kind==UNACKED) {
1322        owl_fmtext_append_normal(&fm, "UNACKED\n");
1323      } else if (n->z_kind==ACKED) {
1324        owl_fmtext_append_normal(&fm, "ACKED\n");
1325      } else if (n->z_kind==HMACK) {
1326        owl_fmtext_append_normal(&fm, "HMACK\n");
1327      } else if (n->z_kind==HMCTL) {
1328        owl_fmtext_append_normal(&fm, "HMCTL\n");
1329      } else if (n->z_kind==SERVACK) {
1330        owl_fmtext_append_normal(&fm, "SERVACK\n");
1331      } else if (n->z_kind==SERVNAK) {
1332        owl_fmtext_append_normal(&fm, "SERVNACK\n");
1333      } else if (n->z_kind==CLIENTACK) {
1334        owl_fmtext_append_normal(&fm, "CLIENTACK\n");
1335      } else if (n->z_kind==STAT) {
1336        owl_fmtext_append_normal(&fm, "STAT\n");
1337      } else {
1338        owl_fmtext_append_normal(&fm, "ILLEGAL VALUE\n");
1339      }
1340
1341      owl_fmtext_append_normal(&fm, "Time      : ");
1342      owl_fmtext_append_normal(&fm, owl_message_get_timestr(m));
1343      owl_fmtext_append_normal(&fm, "\n");
1344      owl_fmtext_append_normal(&fm, "Host      : ");
1345      owl_fmtext_append_normal(&fm, owl_message_get_hostname(m));
1346      owl_fmtext_append_normal(&fm, "\n");
1347      sprintf(buff, "Port      : %i\n", n->z_port);
1348      owl_fmtext_append_normal(&fm, buff);
1349
1350      owl_fmtext_append_normal(&fm,    "Auth      : ");
1351      if (n->z_auth == ZAUTH_FAILED) {
1352        owl_fmtext_append_normal(&fm, "FAILED\n");
1353      } else if (n->z_auth == ZAUTH_NO) {
1354        owl_fmtext_append_normal(&fm, "NO\n");
1355      } else if (n->z_auth == ZAUTH_YES) {
1356        owl_fmtext_append_normal(&fm, "YES\n");
1357      } else {
1358        sprintf(buff, "Unknown State (%i)\n", n->z_auth);
1359        owl_fmtext_append_normal(&fm, buff);
1360      }
1361
1362      sprintf(buff, "Checkd Ath: %i\n", n->z_checked_auth);
1363      sprintf(buff, "%sMulti notc: %s\n", buff, n->z_multinotice);
1364      sprintf(buff, "%sNum other : %i\n", buff, n->z_num_other_fields);
1365      sprintf(buff, "%sMsg Len   : %i\n", buff, n->z_message_len);
1366      owl_fmtext_append_normal(&fm, buff);
1367     
1368      sprintf(buff, "Fields    : %i\n", owl_zephyr_get_num_fields(n));
1369      owl_fmtext_append_normal(&fm, buff);
1370     
1371      fields=owl_zephyr_get_num_fields(n);
1372      for (i=0; i<fields; i++) {
1373        sprintf(buff, "Field %i   : ", i+1);
1374       
1375        ptr=owl_zephyr_get_field(n, i+1, &len);
1376        if (!ptr) break;
1377        if (len<30) {
1378          strncpy(tmpbuff, ptr, len);
1379          tmpbuff[len]='\0';
1380        } else {
1381          strncpy(tmpbuff, ptr, 30);
1382          tmpbuff[30]='\0';
1383          strcat(tmpbuff, "...");
1384        }
1385       
1386        /* just for testing for now */
1387        for (j=0; j<strlen(tmpbuff); j++) {
1388          if (tmpbuff[j]=='\n') tmpbuff[j]='~';
1389          if (tmpbuff[j]=='\r') tmpbuff[j]='!';
1390        }
1391       
1392        strcat(buff, tmpbuff);
1393        strcat(buff, "\n");
1394        owl_fmtext_append_normal(&fm, buff);
1395      }
1396      owl_fmtext_append_normal(&fm, "Default Fm:");
1397      owl_fmtext_append_normal(&fm, n->z_default_format);
1398    }
1399  }
1400 
1401  owl_function_popless_fmtext(&fm);
1402}
1403
1404
1405void owl_function_curmsg_to_popwin()
1406{
1407  owl_popwin *pw;
1408  owl_view *v;
1409  owl_message *m;
1410
1411  v = owl_global_get_current_view(&g);
1412
1413  pw=owl_global_get_popwin(&g);
1414
1415  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
1416
1417  if (!m || owl_view_get_size(v)==0) {
1418    owl_function_makemsg("No current message");
1419    return;
1420  }
1421
1422  owl_function_popless_fmtext(owl_message_get_fmtext(m));
1423}
1424
1425
1426void owl_function_page_curmsg(int step)
1427{
1428  /* scroll down or up within the current message IF the message is truncated */
1429
1430  int offset, curmsg, lines;
1431  owl_view *v;
1432  owl_message *m;
1433
1434  offset=owl_global_get_curmsg_vert_offset(&g);
1435  v=owl_global_get_current_view(&g);
1436  curmsg=owl_global_get_curmsg(&g);
1437  m=owl_view_get_element(v, curmsg);
1438  if (!m || owl_view_get_size(v)==0) return;
1439  lines=owl_message_get_numlines(m);
1440
1441  if (offset==0) {
1442    /* Bail if the curmsg isn't the last one displayed */
1443    if (curmsg != owl_mainwin_get_last_msg(owl_global_get_mainwin(&g))) {
1444      owl_function_makemsg("The entire message is already displayed");
1445      return;
1446    }
1447   
1448    /* Bail if we're not truncated */
1449    if (!owl_mainwin_is_curmsg_truncated(owl_global_get_mainwin(&g))) {
1450      owl_function_makemsg("The entire message is already displayed");
1451      return;
1452    }
1453  }
1454 
1455 
1456  /* don't scroll past the last line */
1457  if (step>0) {
1458    if (offset+step > lines-1) {
1459      owl_global_set_curmsg_vert_offset(&g, lines-1);
1460    } else {
1461      owl_global_set_curmsg_vert_offset(&g, offset+step);
1462    }
1463  }
1464
1465  /* would we be before the beginning of the message? */
1466  if (step<0) {
1467    if (offset+step<0) {
1468      owl_global_set_curmsg_vert_offset(&g, 0);
1469    } else {
1470      owl_global_set_curmsg_vert_offset(&g, offset+step);
1471    }
1472  }
1473 
1474  /* redisplay */
1475  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
1476  owl_global_set_needrefresh(&g);
1477}
1478
1479void owl_function_resize_typwin(int newsize)
1480{
1481  owl_global_set_typwin_lines(&g, newsize);
1482  owl_function_resize();
1483}
1484
1485void owl_function_typwin_grow()
1486{
1487  int i;
1488
1489  i=owl_global_get_typwin_lines(&g);
1490  owl_function_resize_typwin(i+1);
1491}
1492
1493void owl_function_typwin_shrink()
1494{
1495  int i;
1496
1497  i=owl_global_get_typwin_lines(&g);
1498  if (i>2) {
1499    owl_function_resize_typwin(i-1);
1500  }
1501}
1502
1503void owl_function_mainwin_pagedown()
1504{
1505  int i;
1506
1507  i=owl_mainwin_get_last_msg(owl_global_get_mainwin(&g));
1508  if (i<0) return;
1509  if (owl_mainwin_is_last_msg_truncated(owl_global_get_mainwin(&g))
1510      && (owl_global_get_curmsg(&g) < i)
1511      && (i>0)) {
1512    i--;
1513  }
1514  owl_global_set_curmsg(&g, i);
1515  owl_function_nextmsg();
1516}
1517
1518void owl_function_mainwin_pageup()
1519{
1520  owl_global_set_curmsg(&g, owl_global_get_topmsg(&g));
1521  owl_function_prevmsg();
1522}
1523
1524void owl_function_getsubs()
1525{
1526  int ret, num, i, one;
1527  ZSubscription_t sub;
1528  char *buff, *tmpbuff;
1529
1530  one = 1;
1531
1532  ret=ZRetrieveSubscriptions(0, &num);
1533  if (ret == ZERR_TOOMANYSUBS) {
1534    owl_function_makemsg("Zephyr: too many subscriptions");
1535    return;
1536  }
1537
1538  buff=owl_malloc(num*500);
1539  tmpbuff=owl_malloc(num*500);
1540  strcpy(buff, "");
1541  for (i=0; i<num; i++) {
1542    if ((ret = ZGetSubscriptions(&sub, &one)) != ZERR_NONE) {
1543      owl_function_makemsg("Error while getting subscriptions");
1544      owl_free(buff);
1545      owl_free(tmpbuff);
1546      ZFlushSubscriptions();
1547      return;
1548    } else {
1549      sprintf(tmpbuff, "<%s,%s,%s>\n%s", sub.zsub_class, sub.zsub_classinst, sub.zsub_recipient, buff);
1550      strcpy(buff, tmpbuff);
1551    }
1552  }
1553
1554  owl_function_popless_text(buff);
1555  owl_free(buff);
1556  owl_free(tmpbuff);
1557  ZFlushSubscriptions();
1558}
1559
1560#define PABUFLEN 5000
1561void owl_function_printallvars()
1562{
1563  char buff[PABUFLEN], *pos, *name;
1564  owl_list varnames;
1565  int i, numvarnames, rem;
1566
1567  pos = buff;
1568  pos += sprintf(pos, "%-20s = %s\n", "VARIABLE", "VALUE");
1569  pos += sprintf(pos, "%-20s   %s\n",  "--------", "-----");
1570  owl_variable_dict_get_names(owl_global_get_vardict(&g), &varnames);
1571  rem = (buff+PABUFLEN)-pos-1;
1572  numvarnames = owl_list_get_size(&varnames);
1573  for (i=0; i<numvarnames; i++) {
1574    name = owl_list_get_element(&varnames, i);
1575    if (name && name[0]!='_') {
1576      rem = (buff+PABUFLEN)-pos-1;   
1577      pos += snprintf(pos, rem, "\n%-20s = ", name);
1578      rem = (buff+PABUFLEN)-pos-1;   
1579      owl_variable_get_tostring(owl_global_get_vardict(&g), name, pos, rem);
1580      pos = buff+strlen(buff);
1581    }
1582  }
1583  rem = (buff+PABUFLEN)-pos-1;   
1584  snprintf(pos, rem, "\n");
1585  owl_variable_dict_namelist_free(&varnames);
1586 
1587  owl_function_popless_text(buff);
1588}
1589
1590void owl_function_show_variables()
1591{
1592  owl_list varnames;
1593  owl_fmtext fm; 
1594  int i, numvarnames;
1595  char *varname;
1596
1597  owl_fmtext_init_null(&fm);
1598  owl_fmtext_append_bold(&fm, 
1599      "Variables: (use 'show variable <name>' for details)\n");
1600  owl_variable_dict_get_names(owl_global_get_vardict(&g), &varnames);
1601  numvarnames = owl_list_get_size(&varnames);
1602  for (i=0; i<numvarnames; i++) {
1603    varname = owl_list_get_element(&varnames, i);
1604    if (varname && varname[0]!='_') {
1605      owl_variable_describe(owl_global_get_vardict(&g), varname, &fm);
1606    }
1607  }
1608  owl_variable_dict_namelist_free(&varnames);
1609  owl_function_popless_fmtext(&fm);
1610  owl_fmtext_free(&fm);
1611}
1612
1613void owl_function_show_variable(char *name)
1614{
1615  owl_fmtext fm; 
1616
1617  owl_fmtext_init_null(&fm);
1618  owl_variable_get_help(owl_global_get_vardict(&g), name, &fm);
1619  owl_function_popless_fmtext(&fm);
1620  owl_fmtext_free(&fm); 
1621}
1622
1623/* note: this applies to global message list, not to view.
1624 * If flag is 1, deletes.  If flag is 0, undeletes. */
1625void owl_function_delete_by_id(int id, int flag)
1626{
1627  owl_messagelist *ml;
1628  owl_message *m;
1629  ml = owl_global_get_msglist(&g);
1630  m = owl_messagelist_get_by_id(ml, id);
1631  if (m) {
1632    if (flag == 1) {
1633      owl_message_mark_delete(m);
1634    } else if (flag == 0) {
1635      owl_message_unmark_delete(m);
1636    }
1637    owl_mainwin_redisplay(owl_global_get_mainwin(&g));
1638    owl_global_set_needrefresh(&g);
1639  } else {
1640    owl_function_makemsg("No message with id %d: unable to mark for (un)delete",id);
1641  }
1642}
1643
1644void owl_function_delete_automsgs()
1645{
1646  /* mark for deletion all messages in the current view that match the
1647   * 'trash' filter */
1648
1649  int i, j, count;
1650  owl_message *m;
1651  owl_view *v;
1652  owl_filter *f;
1653
1654  /* get the trash filter */
1655  f=owl_global_get_filter(&g, "trash");
1656  if (!f) {
1657    owl_function_makemsg("No trash filter defined");
1658    return;
1659  }
1660
1661  v=owl_global_get_current_view(&g);
1662
1663  count=0;
1664  j=owl_view_get_size(v);
1665  for (i=0; i<j; i++) {
1666    m=owl_view_get_element(v, i);
1667    if (owl_filter_message_match(f, m)) {
1668      count++;
1669      owl_message_mark_delete(m);
1670    }
1671  }
1672  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
1673  owl_function_makemsg("%i messages marked for deletion", count);
1674  owl_global_set_needrefresh(&g);
1675}
1676
1677
1678void owl_function_status()
1679{
1680  char buff[5000];
1681  time_t start;
1682  int up, days, hours, minutes;
1683
1684  start=owl_global_get_starttime(&g);
1685
1686  sprintf(buff, "Version: %s\n", OWL_VERSION_STRING);
1687  sprintf(buff, "%sScreen size: %i lines, %i columns\n", buff, owl_global_get_lines(&g), owl_global_get_cols(&g));
1688  sprintf(buff, "%sStartup Arugments: %s\n", buff, owl_global_get_startupargs(&g));
1689  sprintf(buff, "%sStartup Time: %s", buff, ctime(&start));
1690
1691  up=owl_global_get_runtime(&g);
1692  days=up/86400;
1693  up-=days*86400;
1694  hours=up/3600;
1695  up-=hours*3600;
1696  minutes=up/60;
1697  up-=minutes*60;
1698  sprintf(buff, "%sRun Time: %i days %2.2i:%2.2i:%2.2i\n", buff, days, hours, minutes, up);
1699
1700  if (owl_global_get_hascolors(&g)) {
1701    sprintf(buff, "%sColor: Yes, %i color pairs.\n", buff, owl_global_get_colorpairs(&g));
1702  } else {
1703    strcat(buff, "Color: No.\n");
1704  }
1705
1706  sprintf(buff, "%sMemory Malloced: %i\n", buff, owl_global_get_malloced(&g));
1707  sprintf(buff, "%sMemory Freed: %i\n", buff, owl_global_get_freed(&g));
1708  sprintf(buff, "%sMemory In Use: %i\n", buff, owl_global_get_meminuse(&g));
1709
1710  owl_function_popless_text(buff);
1711}
1712
1713void owl_function_show_term()
1714{
1715  owl_fmtext fm;
1716  char buff[LINE];
1717
1718  owl_fmtext_init_null(&fm);
1719  sprintf(buff, "Terminal Lines: %i\nTerminal Columns: %i\n",
1720          owl_global_get_lines(&g),
1721          owl_global_get_cols(&g));
1722  owl_fmtext_append_normal(&fm, buff);
1723
1724  if (owl_global_get_hascolors(&g)) {
1725    owl_fmtext_append_normal(&fm, "Color: Yes\n");
1726    sprintf(buff, "Number of color pairs: %i\n", owl_global_get_colorpairs(&g));
1727    owl_fmtext_append_normal(&fm, buff);
1728    sprintf(buff, "Can change colors: %s\n", can_change_color() ? "yes" : "no");
1729    owl_fmtext_append_normal(&fm, buff);
1730  } else {
1731    owl_fmtext_append_normal(&fm, "Color: No\n");
1732  }
1733
1734  owl_function_popless_fmtext(&fm);
1735  owl_fmtext_free(&fm);
1736}
1737
1738
1739void owl_function_reply(int type, int enter)
1740{
1741  /* if type = 0 then normal reply.
1742   * if type = 1 then it's a reply to sender
1743   * if enter = 0 then allow the command to be edited
1744   * if enter = 1 then don't wait for editing
1745   */
1746  char *buff, *oldbuff;
1747  owl_message *m;
1748  owl_filter *f;
1749 
1750  if (owl_view_get_size(owl_global_get_current_view(&g))==0) {
1751    owl_function_makemsg("No message selected");
1752  } else {
1753    char *class, *inst, *to, *cc=NULL;
1754   
1755    m=owl_view_get_element(owl_global_get_current_view(&g), owl_global_get_curmsg(&g));
1756    if (!m) {
1757      owl_function_makemsg("No message selected");
1758      return;
1759    }
1760
1761    /* first check if we catch the reply-lockout filter */
1762    f=owl_global_get_filter(&g, "reply-lockout");
1763    if (f) {
1764      if (owl_filter_message_match(f, m)) {
1765        owl_function_makemsg("Sorry, replies to this message have been disabled by the reply-lockout filter");
1766        return;
1767      }
1768    }
1769
1770    /* admin */
1771    if (owl_message_is_type_admin(m)) {
1772      owl_function_makemsg("You cannot reply to an admin message");
1773      return;
1774    }
1775
1776    /* zephyr */
1777    if (owl_message_is_type_zephyr(m)) {
1778      /* for now we disable replies to zcrypt messages, since we can't
1779         support an encrypted reply */
1780      if (!strcasecmp(owl_message_get_opcode(m), "crypt")) {
1781        owl_function_makemsg("Replies to zcrypt messages are not enabled in this release");
1782        return;
1783      }
1784
1785      if (owl_message_is_direction_out(m)) {
1786        owl_function_zwrite_setup(owl_message_get_zwriteline(m));
1787        owl_global_set_buffercommand(&g, owl_message_get_zwriteline(m));
1788        return;
1789      }
1790     
1791      if (owl_message_is_login(m)) {
1792        class="MESSAGE";
1793        inst="PERSONAL";
1794        to=owl_message_get_sender(m);
1795      } else if (type==1) {
1796        class="MESSAGE";
1797        inst="PERSONAL";
1798        to=owl_message_get_sender(m);
1799      } else {
1800        class=owl_message_get_class(m);
1801        inst=owl_message_get_instance(m);
1802        to=owl_message_get_recipient(m);
1803        cc=owl_message_get_cc(m);
1804        if (!strcmp(to, "") || !strcmp(to, "*")) {
1805          to="";
1806        } else if (to[0]=='@') {
1807          /* leave it, to get the realm */
1808        } else {
1809          to=owl_message_get_sender(m);
1810        }
1811      }
1812       
1813      /* create the command line */
1814      buff = owl_strdup("zwrite");
1815      if (strcasecmp(class, "message")) {
1816        buff = owl_sprintf("%s -c %s%s%s", oldbuff=buff, owl_getquoting(class), class, owl_getquoting(class));
1817        owl_free(oldbuff);
1818      }
1819      if (strcasecmp(inst, "personal")) {
1820        buff = owl_sprintf("%s -i %s%s%s", oldbuff=buff, owl_getquoting(inst), inst, owl_getquoting(inst));
1821        owl_free(oldbuff);
1822      }
1823      if (*to != '\0') {
1824        char *tmp, *oldtmp, *tmp2;
1825        tmp=short_zuser(to);
1826        if (cc) {
1827          tmp = owl_util_uniq(oldtmp=tmp, cc, "-");
1828          owl_free(oldtmp);
1829          buff = owl_sprintf("%s -C %s", oldbuff=buff, tmp);
1830          owl_free(oldbuff);
1831        } else {
1832          if (owl_global_is_smartstrip(&g)) {
1833            tmp2=tmp;
1834            tmp=owl_util_smartstripped_user(tmp2);
1835            owl_free(tmp2);
1836          }
1837          buff = owl_sprintf("%s %s", oldbuff=buff, tmp);
1838          owl_free(oldbuff);
1839        }
1840        owl_free(tmp);
1841      }
1842      if (cc) owl_free(cc);
1843    }
1844
1845    /* aim */
1846    if (owl_message_is_type_aim(m)) {
1847      if (owl_message_is_direction_out(m)) {
1848        buff=owl_sprintf("aimwrite %s", owl_message_get_recipient(m));
1849      } else {
1850        buff=owl_sprintf("aimwrite %s", owl_message_get_sender(m));
1851      }
1852    }
1853   
1854    if (enter) {
1855      owl_history *hist = owl_global_get_cmd_history(&g);
1856      owl_history_store(hist, buff);
1857      owl_history_reset(hist);
1858      owl_function_command_norv(buff);
1859    } else {
1860      owl_function_start_command(buff);
1861    }
1862    owl_free(buff);
1863  }
1864}
1865
1866void owl_function_zlocate(int argc, char **argv, int auth)
1867{
1868  owl_fmtext fm;
1869  char *ptr, buff[LINE];
1870  int i;
1871
1872  owl_fmtext_init_null(&fm);
1873
1874  for (i=0; i<argc; i++) {
1875    ptr=long_zuser(argv[i]);
1876    owl_zephyr_zlocate(ptr, buff, auth);
1877    owl_fmtext_append_normal(&fm, buff);
1878    owl_free(ptr);
1879  }
1880
1881  owl_function_popless_fmtext(&fm);
1882  owl_fmtext_free(&fm);
1883}
1884
1885void owl_function_start_command(char *line)
1886{
1887  int i, j;
1888  owl_editwin *tw;
1889
1890  tw=owl_global_get_typwin(&g);
1891  owl_global_set_typwin_active(&g);
1892  owl_editwin_new_style(tw, OWL_EDITWIN_STYLE_ONELINE, 
1893                        owl_global_get_cmd_history(&g));
1894
1895  owl_editwin_set_locktext(tw, "command: ");
1896  owl_global_set_needrefresh(&g);
1897
1898  j=strlen(line);
1899  for (i=0; i<j; i++) {
1900    owl_editwin_process_char(tw, line[i]);
1901  }
1902  owl_editwin_redisplay(tw, 0);
1903}
1904
1905char *owl_function_exec(int argc, char **argv, char *buff, int type)
1906{
1907  /* if type == 1 display in a popup
1908   * if type == 2 display an admin messages
1909   * if type == 0 return output
1910   * else display in a popup
1911   */
1912  char *newbuff, *redirect = " 2>&1 < /dev/null";
1913  char *out, buff2[1024];
1914  int size;
1915  FILE *p;
1916
1917  if (argc<2) {
1918    owl_function_makemsg("Wrong number of arguments to the exec command");
1919    return NULL;
1920  }
1921
1922  buff = skiptokens(buff, 1);
1923  newbuff = owl_malloc(strlen(buff)+strlen(redirect)+1);
1924  strcpy(newbuff, buff);
1925  strcat(newbuff, redirect);
1926
1927  p=popen(newbuff, "r");
1928  out=owl_malloc(1024);
1929  size=1024;
1930  strcpy(out, "");
1931  while (fgets(buff2, 1024, p)!=NULL) {
1932    size+=1024;
1933    out=owl_realloc(out, size);
1934    strcat(out, buff2);
1935  }
1936  pclose(p);
1937
1938  if (type==1) {
1939    owl_function_popless_text(out);
1940  } else if (type==0) {
1941    return out;
1942  } else if (type==2) {
1943    owl_function_adminmsg(buff, out);
1944  } else {
1945    owl_function_popless_text(out);
1946  }
1947  owl_free(out);
1948  return NULL;
1949}
1950
1951
1952char *owl_function_perl(int argc, char **argv, char *buff, int type)
1953{
1954  /* if type == 1 display in a popup
1955   * if type == 2 display an admin messages
1956   * if type == 0 return output
1957   * else display in a popup
1958   */
1959  char *perlout;
1960
1961  if (argc<2) {
1962    owl_function_makemsg("Wrong number of arguments to perl command");
1963    return NULL;
1964  }
1965
1966  /* consume first token (argv[0]) */
1967  buff = skiptokens(buff, 1);
1968
1969  perlout = owl_config_execute(buff);
1970  if (perlout) { 
1971    if (type==1) {
1972      owl_function_popless_text(perlout);
1973    } else if (type==2) {
1974      owl_function_adminmsg(buff, perlout);
1975    } else if (type==0) {
1976      return perlout;
1977    } else {
1978      owl_function_popless_text(perlout);
1979    }
1980    owl_free(perlout);
1981  }
1982  return NULL;
1983}
1984
1985
1986void owl_function_change_view(char *filtname)
1987{
1988  owl_view *v;
1989  owl_filter *f;
1990  int curid=-1, newpos, curmsg;
1991  owl_message *curm=NULL;
1992
1993  v=owl_global_get_current_view(&g);
1994  curmsg=owl_global_get_curmsg(&g);
1995  if (curmsg==-1) {
1996    owl_function_debugmsg("Hit the curmsg==-1 case in change_view");
1997  } else {
1998    curm=owl_view_get_element(v, curmsg);
1999    if (curm) {
2000      curid=owl_message_get_id(curm);
2001      owl_view_save_curmsgid(v, curid);
2002    }
2003  }
2004
2005  /* grab the filter */;
2006  f=owl_global_get_filter(&g, filtname);
2007  if (!f) {
2008    owl_function_makemsg("Unknown filter");
2009    return;
2010  }
2011
2012  /* free the existing view and create a new one based on the filter */
2013  owl_view_free(v);
2014  owl_view_create(v, f);
2015
2016  /* Figure out what to set the current message to.
2017   * - If the view we're leaving has messages in it, go to the closest message
2018   *   to the last message pointed to in that view.
2019   * - If the view we're leaving is empty, try to restore the position
2020   *   from the last time we were in the new view.  */
2021  if (curm) {
2022    newpos = owl_view_get_nearest_to_msgid(v, curid);
2023  } else {
2024    newpos = owl_view_get_nearest_to_saved(v);
2025  }
2026
2027  owl_global_set_curmsg(&g, newpos);
2028
2029  owl_function_calculate_topmsg(OWL_DIRECTION_DOWNWARDS);
2030  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2031  owl_global_set_direction_downwards(&g);
2032}
2033
2034void owl_function_create_filter(int argc, char **argv)
2035{
2036  owl_filter *f;
2037  owl_view *v;
2038  int ret, inuse=0;
2039
2040  if (argc < 2) {
2041    owl_function_makemsg("Wrong number of arguments to filter command");
2042    return;
2043  }
2044
2045  v=owl_global_get_current_view(&g);
2046
2047  /* don't touch the all filter */
2048  if (!strcmp(argv[1], "all")) {
2049    owl_function_makemsg("You may not change the 'all' filter.");
2050    return;
2051  }
2052
2053  /* deal with the case of trying change the filter color */
2054  if (argc==4 && !strcmp(argv[2], "-c")) {
2055    f=owl_global_get_filter(&g, argv[1]);
2056    if (!f) {
2057      owl_function_makemsg("The filter '%s' does not exist.", argv[1]);
2058      return;
2059    }
2060    owl_filter_set_color(f, owl_util_string_to_color(argv[3]));
2061    owl_global_set_needrefresh(&g);
2062    owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2063    return;
2064  }
2065
2066  /* create the filter and check for errors */
2067  f=owl_malloc(sizeof(owl_filter));
2068  ret=owl_filter_init(f, argv[1], argc-2, argv+2);
2069  if (ret==-1) {
2070    owl_free(f);
2071    owl_function_makemsg("Invalid filter syntax");
2072    return;
2073  }
2074
2075  /* if the named filter is in use by the current view, remember it */
2076  if (!strcmp(owl_view_get_filtname(v), argv[1])) {
2077    inuse=1;
2078  }
2079
2080  /* if the named filter already exists, nuke it */
2081  if (owl_global_get_filter(&g, argv[1])) {
2082    owl_global_remove_filter(&g, argv[1]);
2083  }
2084
2085  /* add the filter */
2086  owl_global_add_filter(&g, f);
2087
2088  /* if it was in use by the current view then update */
2089  if (inuse) {
2090    owl_function_change_view(argv[1]);
2091  }
2092  owl_global_set_needrefresh(&g);
2093  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2094}
2095
2096void owl_function_show_filters()
2097{
2098  owl_list *l;
2099  owl_filter *f;
2100  int i, j;
2101  owl_fmtext fm;
2102
2103  owl_fmtext_init_null(&fm);
2104
2105  l=owl_global_get_filterlist(&g);
2106  j=owl_list_get_size(l);
2107
2108  owl_fmtext_append_bold(&fm, "Filters:\n");
2109
2110  for (i=0; i<j; i++) {
2111    f=owl_list_get_element(l, i);
2112    owl_fmtext_append_normal(&fm, "   ");
2113    if (owl_global_get_hascolors(&g)) {
2114      owl_fmtext_append_normal_color(&fm, owl_filter_get_name(f), owl_filter_get_color(f));
2115    } else {
2116      owl_fmtext_append_normal(&fm, owl_filter_get_name(f));
2117    }
2118    owl_fmtext_append_normal(&fm, "\n");
2119  }
2120  owl_function_popless_fmtext(&fm);
2121  owl_fmtext_free(&fm);
2122}
2123
2124void owl_function_show_filter(char *name)
2125{
2126  owl_filter *f;
2127  char buff[5000];
2128
2129  f=owl_global_get_filter(&g, name);
2130  if (!f) {
2131    owl_function_makemsg("There is no filter with that name");
2132    return;
2133  }
2134  owl_filter_print(f, buff);
2135  owl_function_popless_text(buff);
2136}
2137
2138void owl_function_show_zpunts()
2139{
2140  owl_filter *f;
2141  owl_list *fl;
2142  char buff[5000];
2143  owl_fmtext fm;
2144  int i, j;
2145
2146  owl_fmtext_init_null(&fm);
2147
2148  fl=owl_global_get_puntlist(&g);
2149  j=owl_list_get_size(fl);
2150  owl_fmtext_append_bold(&fm, "Active zpunt filters:\n");
2151
2152  for (i=0; i<j; i++) {
2153    f=owl_list_get_element(fl, i);
2154    owl_filter_print(f, buff);
2155    owl_fmtext_append_normal(&fm, buff);
2156  }
2157  owl_function_popless_fmtext(&fm);
2158  owl_fmtext_free(&fm);
2159}
2160
2161char *owl_function_fastclassinstfilt(char *class, char *instance) 
2162{
2163  /* creates a filter for a class, instance if one doesn't exist.
2164   * If instance is null then apply for all messgaes in the class.
2165   * returns the name of the filter, which the caller must free.*/
2166  owl_list *fl;
2167  owl_filter *f;
2168  char *argbuff, *filtname;
2169  char *tmpclass, *tmpinstance = NULL;
2170  int len;
2171
2172  fl=owl_global_get_filterlist(&g);
2173
2174  /* name for the filter */
2175  len=strlen(class)+30;
2176  if (instance) len+=strlen(instance);
2177  filtname=owl_malloc(len);
2178  if (!instance) {
2179    sprintf(filtname, "class-%s", class);
2180  } else {
2181    sprintf(filtname, "class-%s-instance-%s", class, instance);
2182  }
2183  /* downcase it */
2184  downstr(filtname);
2185  /* turn spaces into hyphens */
2186  owl_util_tr(filtname, ' ', '.');
2187 
2188  /* if it already exists then go with it.  This lets users override */
2189  if (owl_global_get_filter(&g, filtname)) {
2190    return(filtname);
2191  }
2192
2193  /* create the new filter */
2194  argbuff=owl_malloc(len+20);
2195  tmpclass=owl_strdup(class);
2196  owl_util_tr(tmpclass, ' ', '.');
2197  if (instance) {
2198    tmpinstance=owl_strdup(instance);
2199    owl_util_tr(tmpinstance, ' ', '.');
2200  }
2201  sprintf(argbuff, "( class ^%s$ )", tmpclass);
2202  if (tmpinstance) {
2203    sprintf(argbuff, "%s and ( instance ^%s$ )", argbuff, tmpinstance);
2204  }
2205  owl_free(tmpclass);
2206  if (tmpinstance) owl_free(tmpinstance);
2207
2208  f=owl_malloc(sizeof(owl_filter));
2209  owl_filter_init_fromstring(f, filtname, argbuff);
2210
2211  /* add it to the global list */
2212  owl_global_add_filter(&g, f);
2213
2214  owl_free(argbuff);
2215  return(filtname);
2216}
2217
2218char *owl_function_fastuserfilt(char *user)
2219{
2220  owl_filter *f;
2221  char *argbuff, *longuser, *shortuser, *filtname;
2222
2223  /* stick the local realm on if it's not there */
2224  longuser=long_zuser(user);
2225  shortuser=short_zuser(user);
2226
2227  /* name for the filter */
2228  filtname=owl_malloc(strlen(shortuser)+20);
2229  sprintf(filtname, "user-%s", shortuser);
2230
2231  /* if it already exists then go with it.  This lets users override */
2232  if (owl_global_get_filter(&g, filtname)) {
2233    return(filtname);
2234  }
2235
2236  /* create the new-internal filter */
2237  f=owl_malloc(sizeof(owl_filter));
2238
2239  argbuff=owl_malloc(strlen(longuser)+1000);
2240  sprintf(argbuff, "( type ^zephyr$ and ( class ^message$ and instance ^personal$ and ");
2241  sprintf(argbuff, "%s ( ( direction ^in$ and sender ^%s$ ) or ( direction ^out$ and recipient ^%s$ ) ) )", argbuff, longuser, longuser);
2242  sprintf(argbuff, "%s or ( ( class ^login$ ) and ( sender ^%s$ ) ) )", argbuff, longuser);
2243
2244  owl_filter_init_fromstring(f, filtname, argbuff);
2245
2246  /* add it to the global list */
2247  owl_global_add_filter(&g, f);
2248
2249  /* free stuff */
2250  owl_free(argbuff);
2251  owl_free(longuser);
2252  owl_free(shortuser);
2253
2254  return(filtname);
2255}
2256
2257char *owl_function_fasttypefilt(char *type)
2258{
2259  owl_filter *f;
2260  char *argbuff, *filtname;
2261
2262  /* name for the filter */
2263  filtname=owl_sprintf("type-%s", type);
2264
2265  /* if it already exists then go with it.  This lets users override */
2266  if (owl_global_get_filter(&g, filtname)) {
2267    return filtname;
2268  }
2269
2270  /* create the new-internal filter */
2271  f=owl_malloc(sizeof(owl_filter));
2272
2273  argbuff = owl_sprintf("type ^%s$", type);
2274
2275  owl_filter_init_fromstring(f, filtname, argbuff);
2276
2277  /* add it to the global list */
2278  owl_global_add_filter(&g, f);
2279
2280  /* free stuff */
2281  owl_free(argbuff);
2282
2283  return filtname;
2284}
2285
2286/* If flag is 1, marks for deletion.  If flag is 0,
2287 * unmarks for deletion. */
2288void owl_function_delete_curview_msgs(int flag)
2289{
2290  owl_view *v;
2291  int i, j;
2292
2293  v=owl_global_get_current_view(&g);
2294  j=owl_view_get_size(v);
2295  for (i=0; i<j; i++) {
2296    if (flag == 1) {
2297      owl_message_mark_delete(owl_view_get_element(v, i));
2298    } else if (flag == 0) {
2299      owl_message_unmark_delete(owl_view_get_element(v, i));
2300    }
2301  }
2302
2303  owl_function_makemsg("%i messages marked for %sdeletion", j, flag?"":"un");
2304
2305  owl_mainwin_redisplay(owl_global_get_mainwin(&g)); 
2306}
2307
2308char *owl_function_smartfilter(int type)
2309{
2310  /* Returns the name of a filter, or null.  The caller
2311   * must free this name.  */
2312  /* if the curmsg is a personal message return a filter name
2313   *    to the converstaion with that user.
2314   * If the curmsg is a class message, instance foo, recip *
2315   *    message, return a filter name to the class, inst.
2316   * If the curmsg is a class message and type==0 then
2317   *    return a filter name for just the class.
2318   * If the curmsg is a class message and type==1 then
2319   *    return a filter name for the class and instance.
2320   */
2321  owl_view *v;
2322  owl_message *m;
2323  char *zperson, *filtname=NULL;
2324 
2325  v=owl_global_get_current_view(&g);
2326  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
2327
2328  if (!m || owl_view_get_size(v)==0) {
2329    owl_function_makemsg("No message selected\n");
2330    return(NULL);
2331  }
2332
2333  /* very simple handling of admin messages for now */
2334  if (owl_message_is_type_admin(m)) {
2335    return(owl_function_fasttypefilt("admin"));
2336  }
2337
2338  /* narrow personal and login messages to the sender or recip as appropriate */
2339  if (owl_message_is_personal(m) || owl_message_is_login(m)) {
2340    if (owl_message_is_type_zephyr(m)) {
2341      if (owl_message_is_direction_in(m)) {
2342        zperson=short_zuser(owl_message_get_sender(m));
2343      } else {
2344        zperson=short_zuser(owl_message_get_recipient(m));
2345      }
2346      filtname=owl_function_fastuserfilt(zperson);
2347      owl_free(zperson);
2348      return(filtname);
2349    }
2350    return(NULL);
2351  }
2352
2353  /* narrow class MESSAGE, instance foo, recip * messages to class, inst */
2354  if (!strcasecmp(owl_message_get_class(m), "message") &&
2355      !owl_message_is_personal(m)) {
2356    filtname=owl_function_fastclassinstfilt(owl_message_get_class(m), owl_message_get_instance(m));
2357    return(filtname);
2358  }
2359
2360  /* otherwise narrow to the class */
2361  if (type==0) {
2362    filtname=owl_function_fastclassinstfilt(owl_message_get_class(m), NULL);
2363  } else if (type==1) {
2364    filtname=owl_function_fastclassinstfilt(owl_message_get_class(m), owl_message_get_instance(m));
2365  }
2366  return(filtname);
2367}
2368
2369void owl_function_smartzpunt(int type)
2370{
2371  /* Starts a zpunt command based on the current class,instance pair.
2372   * If type=0, uses just class.  If type=1, uses instance as well. */
2373  owl_view *v;
2374  owl_message *m;
2375  char *cmd, *cmdprefix, *mclass, *minst;
2376 
2377  v=owl_global_get_current_view(&g);
2378  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
2379
2380  if (!m || owl_view_get_size(v)==0) {
2381    owl_function_makemsg("No message selected\n");
2382    return;
2383  }
2384
2385  /* for now we skip admin messages. */
2386  if (owl_message_is_type_admin(m)
2387      || owl_message_is_login(m)
2388      || !owl_message_is_type_zephyr(m)) {
2389    owl_function_makemsg("smartzpunt doesn't support this message type.");
2390    return;
2391  }
2392
2393  mclass = owl_message_get_class(m);
2394  minst = owl_message_get_instance(m);
2395  if (!mclass || !*mclass || *mclass==' '
2396      || (!strcasecmp(mclass, "message") && !strcasecmp(minst, "personal"))
2397      || (type && (!minst || !*minst|| *minst==' '))) {
2398    owl_function_makemsg("smartzpunt can't safely do this for <%s,%s>",
2399                         mclass, minst);
2400  } else {
2401    cmdprefix = "start-command zpunt ";
2402    cmd = owl_malloc(strlen(cmdprefix)+strlen(mclass)+strlen(minst)+3);
2403    strcpy(cmd, cmdprefix);
2404    strcat(cmd, mclass);
2405    if (type) {
2406      strcat(cmd, " ");
2407      strcat(cmd, minst);
2408    } else {
2409      strcat(cmd, " *");
2410    }
2411    owl_function_command(cmd);
2412    owl_free(cmd);
2413  }
2414}
2415
2416
2417
2418void owl_function_color_current_filter(char *color)
2419{
2420  owl_filter *f;
2421  char *name;
2422
2423  name=owl_view_get_filtname(owl_global_get_current_view(&g));
2424  f=owl_global_get_filter(&g, name);
2425  if (!f) {
2426    owl_function_makemsg("Unknown filter");
2427    return;
2428  }
2429
2430  /* don't touch the all filter */
2431  if (!strcmp(name, "all")) {
2432    owl_function_makemsg("You may not change the 'all' filter.");
2433    return;
2434  }
2435
2436  /* deal with the case of trying change the filter color */
2437  owl_filter_set_color(f, owl_util_string_to_color(color));
2438  owl_global_set_needrefresh(&g);
2439  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2440}
2441
2442void owl_function_show_colors()
2443{
2444  owl_fmtext fm;
2445
2446  owl_fmtext_init_null(&fm);
2447  owl_fmtext_append_normal_color(&fm, "default\n", OWL_COLOR_DEFAULT);
2448  owl_fmtext_append_normal_color(&fm, "red\n", OWL_COLOR_RED);
2449  owl_fmtext_append_normal_color(&fm, "green\n", OWL_COLOR_GREEN);
2450  owl_fmtext_append_normal_color(&fm, "yellow\n", OWL_COLOR_YELLOW);
2451  owl_fmtext_append_normal_color(&fm, "blue\n", OWL_COLOR_BLUE);
2452  owl_fmtext_append_normal_color(&fm, "magenta\n", OWL_COLOR_MAGENTA);
2453  owl_fmtext_append_normal_color(&fm, "cyan\n", OWL_COLOR_CYAN);
2454  owl_fmtext_append_normal_color(&fm, "white\n", OWL_COLOR_WHITE);
2455
2456  owl_function_popless_fmtext(&fm);
2457  owl_fmtext_free(&fm);
2458}
2459
2460void owl_function_zpunt(char *class, char *inst, char *recip, int direction)
2461{
2462  /* add the given class, inst, recip to the punt list for filtering.
2463   *   if direction==0 then punt
2464   *   if direction==1 then unpunt */
2465  owl_filter *f;
2466  owl_list *fl;
2467  char *buff;
2468  int ret, i, j;
2469
2470  fl=owl_global_get_puntlist(&g);
2471
2472  /* first, create the filter */
2473  f=malloc(sizeof(owl_filter));
2474  buff=malloc(strlen(class)+strlen(inst)+strlen(recip)+100);
2475  if (!strcmp(recip, "*")) {
2476    sprintf(buff, "class ^%s$ and instance ^%s$", class, inst);
2477  } else {
2478    sprintf(buff, "class ^%s$ and instance ^%s$ and recipient %s", class, inst, recip);
2479  }
2480  owl_function_debugmsg("About to filter %s", buff);
2481  ret=owl_filter_init_fromstring(f, "punt-filter", buff);
2482  owl_free(buff);
2483  if (ret) {
2484    owl_function_makemsg("Error creating filter for zpunt");
2485    owl_filter_free(f);
2486    return;
2487  }
2488
2489  /* Check for an identical filter */
2490  j=owl_list_get_size(fl);
2491  for (i=0; i<j; i++) {
2492    if (owl_filter_equiv(f, owl_list_get_element(fl, i))) {
2493      /* if we're punting, then just silently bow out on this duplicate */
2494      if (direction==0) {
2495        owl_filter_free(f);
2496        return;
2497      }
2498
2499      /* if we're unpunting, then remove this filter from the puntlist */
2500      if (direction==1) {
2501        owl_filter_free(owl_list_get_element(fl, i));
2502        owl_list_remove_element(fl, i);
2503        return;
2504      }
2505    }
2506  }
2507
2508  /* If we're punting, add the filter to the global punt list */
2509  if (direction==0) {
2510    owl_list_append_element(fl, f);
2511  }
2512}
2513
2514void owl_function_activate_keymap(char *keymap)
2515{
2516  if (!owl_keyhandler_activate(owl_global_get_keyhandler(&g), keymap)) {
2517    owl_function_makemsg("Unable to activate keymap '%s'", keymap);
2518  }
2519}
2520
2521
2522void owl_function_show_keymaps()
2523{
2524  owl_list l;
2525  owl_fmtext fm;
2526  owl_keymap *km;
2527  owl_keyhandler *kh;
2528  int i, numkm;
2529  char *kmname;
2530
2531  kh = owl_global_get_keyhandler(&g);
2532  owl_fmtext_init_null(&fm);
2533  owl_fmtext_append_bold(&fm, "Keymaps:   ");
2534  owl_fmtext_append_normal(&fm, "(use 'show keymap <name>' for details)\n");
2535  owl_keyhandler_get_keymap_names(kh, &l);
2536  owl_fmtext_append_list(&fm, &l, "\n", owl_function_keymap_summary);
2537  owl_fmtext_append_normal(&fm, "\n");
2538
2539  numkm = owl_list_get_size(&l);
2540  for (i=0; i<numkm; i++) {
2541    kmname = owl_list_get_element(&l, i);
2542    km = owl_keyhandler_get_keymap(kh, kmname);
2543    owl_fmtext_append_bold(&fm, "\n\n----------------------------------------------------------------------------------------------------\n\n");
2544    owl_keymap_get_details(km, &fm);   
2545  }
2546  owl_fmtext_append_normal(&fm, "\n");
2547 
2548  owl_function_popless_fmtext(&fm);
2549  owl_keyhandler_keymap_namelist_free(&l);
2550  owl_fmtext_free(&fm);
2551}
2552
2553char *owl_function_keymap_summary(void *name)
2554{
2555  owl_keymap *km
2556    = owl_keyhandler_get_keymap(owl_global_get_keyhandler(&g), name);
2557  if (km) return owl_keymap_summary(km);
2558  else return(NULL);
2559}
2560
2561/* TODO: implement for real */
2562void owl_function_show_keymap(char *name)
2563{
2564  owl_fmtext fm;
2565  owl_keymap *km;
2566
2567  owl_fmtext_init_null(&fm);
2568  km = owl_keyhandler_get_keymap(owl_global_get_keyhandler(&g), name);
2569  if (km) {
2570    owl_keymap_get_details(km, &fm);
2571  } else {
2572    owl_fmtext_append_normal(&fm, "No such keymap...\n");
2573  } 
2574  owl_function_popless_fmtext(&fm);
2575  owl_fmtext_free(&fm);
2576}
2577
2578void owl_function_help_for_command(char *cmdname)
2579{
2580  owl_fmtext fm;
2581
2582  owl_fmtext_init_null(&fm);
2583  owl_cmd_get_help(owl_global_get_cmddict(&g), cmdname, &fm);
2584  owl_function_popless_fmtext(&fm); 
2585  owl_fmtext_free(&fm);
2586}
2587
2588void owl_function_search_start(char *string, int direction)
2589{
2590  /* direction is OWL_DIRECTION_DOWNWARDS or OWL_DIRECTION_UPWARDS */
2591  owl_global_set_search_active(&g, string);
2592  owl_function_search_helper(0, direction);
2593}
2594
2595void owl_function_search_continue(int direction)
2596{
2597  /* direction is OWL_DIRECTION_DOWNWARDS or OWL_DIRECTION_UPWARDS */
2598  owl_function_search_helper(1, direction);
2599}
2600
2601void owl_function_search_helper(int mode, int direction)
2602{
2603  /* move to a message that contains the string.  If direction is
2604   * OWL_DIRECTION_DOWNWARDS then search fowards, if direction is
2605   * OWL_DIRECTION_UPWARDS then search backwards.
2606   *
2607   * If mode==0 then it will stay on the current message if it
2608   * contains the string.
2609   */
2610
2611  owl_view *v;
2612  int viewsize, i, curmsg, start;
2613  owl_message *m;
2614
2615  v=owl_global_get_current_view(&g);
2616  viewsize=owl_view_get_size(v);
2617  curmsg=owl_global_get_curmsg(&g);
2618 
2619  if (viewsize==0) {
2620    owl_function_makemsg("No messages present");
2621    return;
2622  }
2623
2624  if (mode==0) {
2625    start=curmsg;
2626  } else if (direction==OWL_DIRECTION_DOWNWARDS) {
2627    start=curmsg+1;
2628  } else {
2629    start=curmsg-1;
2630  }
2631
2632  /* bounds check */
2633  if (start>=viewsize || start<0) {
2634    owl_function_makemsg("No further matches found");
2635    return;
2636  }
2637
2638  for (i=start; i<viewsize && i>=0;) {
2639    m=owl_view_get_element(v, i);
2640    if (owl_message_search(m, owl_global_get_search_string(&g))) {
2641      owl_global_set_curmsg(&g, i);
2642      owl_function_calculate_topmsg(direction);
2643      owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2644      if (direction==OWL_DIRECTION_DOWNWARDS) {
2645        owl_global_set_direction_downwards(&g);
2646      } else {
2647        owl_global_set_direction_upwards(&g);
2648      }
2649      return;
2650    }
2651    if (direction==OWL_DIRECTION_DOWNWARDS) {
2652      i++;
2653    } else {
2654      i--;
2655    }
2656  }
2657  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2658  owl_function_makemsg("No matches found");
2659}
2660
2661
2662/* strips formatting from ztext and returns the unformatted text.
2663 * caller is responsible for freeing. */
2664char *owl_function_ztext_stylestrip(char *zt)
2665{
2666  owl_fmtext fm;
2667  char *plaintext;
2668
2669  owl_fmtext_init_null(&fm);
2670  owl_fmtext_append_ztext(&fm, zt);
2671  plaintext = owl_fmtext_print_plain(&fm);
2672  owl_fmtext_free(&fm);
2673  return(plaintext);
2674}
2675
2676/* popup a znol listing.  If file is NULL use the default .anyone */
2677/* this doesn't obey 'elapsed' or 'timesort' yet */
2678void owl_function_zlist(char *file, int elapsed, int timesort)
2679{
2680  char *ourfile, *tmp, buff[LINE], *line;
2681  FILE *f;
2682  int numlocs, ret, i;
2683  ZLocations_t location[200];
2684  owl_fmtext fm;
2685
2686  if (file==NULL) {
2687    tmp=owl_global_get_homedir(&g);
2688    if (!tmp) {
2689      owl_function_makemsg("Could not determine home directory");
2690      return;
2691    }
2692    ourfile=owl_malloc(strlen(tmp)+50);
2693    sprintf(ourfile, "%s/.anyone", owl_global_get_homedir(&g));
2694  } else {
2695    ourfile=owl_strdup(file);
2696  }
2697
2698  f=fopen(ourfile, "r");
2699  if (!f) {
2700    owl_function_makemsg("Error opening file %s: %s",
2701                         ourfile,
2702                         strerror(errno) ? strerror(errno) : "");
2703    return;
2704  }
2705
2706  owl_fmtext_init_null(&fm);
2707   
2708  while (fgets(buff, LINE, f)!=NULL) {
2709    /* ignore comments, blank lines etc. */
2710    if (buff[0]=='#') continue;
2711    if (buff[0]=='\n') continue;
2712    if (buff[0]=='\0') continue;
2713
2714    /* strip the \n */
2715    buff[strlen(buff)-1]='\0';
2716
2717    /* ingore from # on */
2718    tmp=strchr(buff, '#');
2719    if (tmp) tmp[0]='\0';
2720
2721    /* ingore from SPC */
2722    tmp=strchr(buff, ' ');
2723    if (tmp) tmp[0]='\0';
2724
2725    /* stick on the local realm. */
2726    if (!strchr(buff, '@')) {
2727      strcat(buff, "@");
2728      strcat(buff, ZGetRealm());
2729    }
2730
2731    ret=ZLocateUser(buff, &numlocs, ZAUTH);
2732    if (ret!=ZERR_NONE) {
2733      owl_function_makemsg("Error getting location for %s", buff);
2734      continue;
2735    }
2736
2737    numlocs=200;
2738    ret=ZGetLocations(location, &numlocs);
2739    if (ret==0) {
2740      for (i=0; i<numlocs; i++) {
2741        line=malloc(strlen(location[i].host)+strlen(location[i].time)+strlen(location[i].tty)+100);
2742        tmp=short_zuser(buff);
2743        sprintf(line, "%-10.10s %-24.24s %-12.12s  %20.20s\n",
2744                tmp,
2745                location[i].host,
2746                location[i].tty,
2747                location[i].time);
2748        owl_fmtext_append_normal(&fm, line);
2749        owl_free(tmp);
2750      }
2751      if (numlocs>=200) {
2752        owl_fmtext_append_normal(&fm, "Too many locations found for this user, truncating.\n");
2753      }
2754    }
2755  }
2756  fclose(f);
2757
2758  owl_function_popless_fmtext(&fm);
2759  owl_fmtext_free(&fm);
2760
2761  owl_free(ourfile);
2762}
2763
2764void owl_function_dump(char *filename) 
2765{
2766  int i, j, count;
2767  owl_message *m;
2768  owl_view *v;
2769  FILE *file;
2770  /* struct stat sbuf; */
2771
2772  v=owl_global_get_current_view(&g);
2773
2774  /* in the future make it ask yes/no */
2775  /*
2776  ret=stat(filename, &sbuf);
2777  if (!ret) {
2778    ret=owl_function_askyesno("File exists, continue? [Y/n]");
2779    if (!ret) return;
2780  }
2781  */
2782
2783  file=fopen(filename, "w");
2784  if (!file) {
2785    owl_function_makemsg("Error opening file");
2786    return;
2787  }
2788
2789  count=0;
2790  j=owl_view_get_size(v);
2791  for (i=0; i<j; i++) {
2792    m=owl_view_get_element(v, i);
2793    fputs(owl_message_get_text(m), file);
2794  }
2795  fclose(file);
2796}
2797
2798
2799
2800void owl_function_do_newmsgproc(void)
2801{
2802  if (owl_global_get_newmsgproc(&g) && strcmp(owl_global_get_newmsgproc(&g), "")) {
2803    /* if there's a process out there, we need to check on it */
2804    if (owl_global_get_newmsgproc_pid(&g)) {
2805      owl_function_debugmsg("Checking on newmsgproc pid==%i", owl_global_get_newmsgproc_pid(&g));
2806      owl_function_debugmsg("Waitpid return is %i", waitpid(owl_global_get_newmsgproc_pid(&g), NULL, WNOHANG));
2807      waitpid(owl_global_get_newmsgproc_pid(&g), NULL, WNOHANG);
2808      if (waitpid(owl_global_get_newmsgproc_pid(&g), NULL, WNOHANG)==-1) {
2809        /* it exited */
2810        owl_global_set_newmsgproc_pid(&g, 0);
2811        owl_function_debugmsg("newmsgproc exited");
2812      } else {
2813        owl_function_debugmsg("newmsgproc did not exit");
2814      }
2815    }
2816   
2817    /* if it exited, fork & exec a new one */
2818    if (owl_global_get_newmsgproc_pid(&g)==0) {
2819      int i, myargc;
2820      i=fork();
2821      if (i) {
2822        /* parent set the child's pid */
2823        owl_global_set_newmsgproc_pid(&g, i);
2824        owl_function_debugmsg("I'm the parent and I started a new newmsgproc with pid %i", i);
2825      } else {
2826        /* child exec's the program */
2827        char **parsed;
2828        parsed=owl_parseline(owl_global_get_newmsgproc(&g), &myargc);
2829        if (myargc < 0) {
2830          owl_function_debugmsg("Could not parse newmsgproc '%s': unbalanced quotes?", owl_global_get_newmsgproc(&g));
2831        }
2832        if (myargc <= 0) {
2833          _exit(127);
2834        }
2835        parsed=realloc(parsed, sizeof(*parsed) * (myargc+1));
2836        parsed[myargc] = NULL;
2837       
2838        owl_function_debugmsg("About to exec \"%s\" with %d arguments", parsed[0], myargc);
2839       
2840        execvp(parsed[0], parsed);
2841       
2842       
2843        /* was there an error exec'ing? */
2844        owl_function_debugmsg("Cannot run newmsgproc '%s': cannot exec '%s': %s", 
2845                              owl_global_get_newmsgproc(&g), parsed[0], strerror(errno));
2846        _exit(127);
2847      }
2848    }
2849  }
2850}
2851
2852void owl_function_xterm_raise(void)
2853{
2854  printf("\033[5t");
2855}
2856
2857void owl_function_xterm_deiconify(void)
2858{
2859  printf("\033[1t");
2860}
Note: See TracBrowser for help on using the repository browser.