source: functions.c @ 31e48a3

barnowl_perlaimdebianowlrelease-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 31e48a3 was 31e48a3, checked in by James M. Kretchmar <kretch@mit.edu>, 18 years ago
Do ZResetAuthentication() before zlog_in and zlog_out as well
  • Property mode set to 100644
File size: 72.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_zephyr_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_makemsg(char *fmt, ...)
717{
718  va_list ap;
719  char buff[2048];
720
721  va_start(ap, fmt);
722  werase(owl_global_get_curs_msgwin(&g));
723 
724  vsnprintf(buff, 2048, fmt, ap);
725  owl_function_debugmsg("makemsg: %s", buff);
726  waddstr(owl_global_get_curs_msgwin(&g), buff); 
727  wnoutrefresh(owl_global_get_curs_msgwin(&g));
728  owl_global_set_needrefresh(&g);
729  va_end(ap);
730}
731
732void owl_function_errormsg(char *fmt, ...)
733{
734  va_list ap;
735  char buff[2048];
736
737  va_start(ap, fmt);
738  werase(owl_global_get_curs_msgwin(&g));
739 
740  vsnprintf(buff, 2048, fmt, ap);
741  owl_function_debugmsg("ERROR: %s", buff);
742  waddstr(owl_global_get_curs_msgwin(&g), buff); 
743  waddstr(owl_global_get_curs_msgwin(&g), "ERROR: "); 
744  wnoutrefresh(owl_global_get_curs_msgwin(&g));
745  owl_global_set_needrefresh(&g);
746  va_end(ap);
747}
748
749
750void owl_function_openurl()
751{
752  /* visit the first url in the current message */
753  owl_message *m;
754  owl_view *v;
755  char *ptr1, *ptr2, *text, url[LINE], tmpbuff[LINE];
756  int webbrowser;
757
758  webbrowser = owl_global_get_webbrowser(&g);
759
760  if (webbrowser < 0 || webbrowser == OWL_WEBBROWSER_NONE) {
761    owl_function_makemsg("No browser selected");
762    return;
763  }
764
765  v=owl_global_get_current_view(&g);
766 
767  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
768
769  if (!m || owl_view_get_size(v)==0) {
770    owl_function_makemsg("No current message selected");
771    return;
772  }
773
774  text=owl_message_get_text(m);
775
776  /* First look for a good URL */ 
777  if ((ptr1=strstr(text, "http://"))!=NULL) {
778    ptr2=strpbrk(ptr1, " \n\t");
779    if (ptr2) {
780      strncpy(url, ptr1, ptr2-ptr1+1);
781      url[ptr2-ptr1+1]='\0';
782    } else {
783      strcpy(url, ptr1);
784    }
785
786    /* if we had <http strip a trailing > */
787    if (ptr1>text && ptr1[-1]=='<') {
788      if (url[strlen(url)-1]=='>') {
789        url[strlen(url)-1]='\0';
790      }
791    }
792  } else if ((ptr1=strstr(text, "https://"))!=NULL) {
793    /* Look for an https URL */ 
794    ptr2=strpbrk(ptr1, " \n\t");
795    if (ptr2) {
796      strncpy(url, ptr1, ptr2-ptr1+1);
797      url[ptr2-ptr1+1]='\0';
798    } else {
799      strcpy(url, ptr1);
800    }
801   
802    /* if we had <http strip a trailing > */
803    if (ptr1>text && ptr1[-1]=='<') {
804      if (url[strlen(url)-1]=='>') {
805        url[strlen(url)-1]='\0';
806      }
807    }
808  } else if ((ptr1=strstr(text, "www."))!=NULL) {
809    /* if we can't find a real url look for www.something */
810    ptr2=strpbrk(ptr1, " \n\t");
811    if (ptr2) {
812      strncpy(url, ptr1, ptr2-ptr1+1);
813      url[ptr2-ptr1+1]='\0';
814    } else {
815      strcpy(url, ptr1);
816    }
817  } else {
818    owl_function_beep();
819    owl_function_makemsg("Could not find URL to open.");
820    return;
821  }
822
823  /* Make sure there aren't any quotes or \'s in the url */
824  for (ptr1 = url; *ptr1; ptr1++) {
825    if (*ptr1 == '"' || *ptr1 == '\\') {
826      owl_function_beep();
827      owl_function_makemsg("URL contains invalid characters.");
828      return;
829    }
830  }
831 
832  /* NOTE: There are potentially serious security issues here... */
833
834  /* open the page */
835  owl_function_makemsg("Opening %s", url);
836  if (webbrowser == OWL_WEBBROWSER_NETSCAPE) {
837    snprintf(tmpbuff, LINE, "netscape -remote \"openURL(%s)\" > /dev/null 2> /dev/null", url);
838    system(tmpbuff); 
839  } else if (webbrowser == OWL_WEBBROWSER_GALEON) {
840    snprintf(tmpbuff, LINE, "galeon \"%s\" > /dev/null 2> /dev/null &", url);
841    system(tmpbuff); 
842  } else if (webbrowser == OWL_WEBBROWSER_OPERA) {
843    snprintf(tmpbuff, LINE, "opera \"%s\" > /dev/null 2> /dev/null &", url);
844    system(tmpbuff); 
845  }
846}
847
848void owl_function_calculate_topmsg(int direction)
849{
850  int recwinlines, topmsg, curmsg;
851  owl_view *v;
852
853  v=owl_global_get_current_view(&g);
854  curmsg=owl_global_get_curmsg(&g);
855  topmsg=owl_global_get_topmsg(&g);
856  recwinlines=owl_global_get_recwin_lines(&g);
857
858  /*
859  if (owl_view_get_size(v) < 1) {
860    return;
861  }
862  */
863
864  switch (owl_global_get_scrollmode(&g)) {
865  case OWL_SCROLLMODE_TOP:
866    topmsg = owl_function_calculate_topmsg_top(direction, v, curmsg, topmsg, recwinlines);
867    break;
868  case OWL_SCROLLMODE_NEARTOP:
869    topmsg = owl_function_calculate_topmsg_neartop(direction, v, curmsg, topmsg, recwinlines);
870    break;
871  case OWL_SCROLLMODE_CENTER:
872    topmsg = owl_function_calculate_topmsg_center(direction, v, curmsg, topmsg, recwinlines);
873    break;
874  case OWL_SCROLLMODE_PAGED:
875    topmsg = owl_function_calculate_topmsg_paged(direction, v, curmsg, topmsg, recwinlines, 0);
876    break;
877  case OWL_SCROLLMODE_PAGEDCENTER:
878    topmsg = owl_function_calculate_topmsg_paged(direction, v, curmsg, topmsg, recwinlines, 1);
879    break;
880  case OWL_SCROLLMODE_NORMAL:
881  default:
882    topmsg = owl_function_calculate_topmsg_normal(direction, v, curmsg, topmsg, recwinlines);
883  }
884  owl_function_debugmsg("Calculated a topmsg of %i", topmsg);
885  owl_global_set_topmsg(&g, topmsg);
886}
887
888/* Returns what the new topmsg should be. 
889 * Passed the last direction of movement,
890 * the current view,
891 * the current message number in the view,
892 * the top message currently being displayed,
893 * and the number of lines in the recwin.
894 */
895int owl_function_calculate_topmsg_top(int direction, owl_view *v, int curmsg, int topmsg, int recwinlines)
896{
897  return(curmsg);
898}
899
900int owl_function_calculate_topmsg_neartop(int direction, owl_view *v, int curmsg, int topmsg, int recwinlines)
901{
902  if (curmsg>0 
903      && (owl_message_get_numlines(owl_view_get_element(v, curmsg-1))
904          <  recwinlines/2)) {
905    return(curmsg-1);
906  } else {
907    return(curmsg);
908  }
909}
910 
911int owl_function_calculate_topmsg_center(int direction, owl_view *v, int curmsg, int topmsg, int recwinlines)
912{
913  int i, last, lines;
914
915  last = curmsg;
916  lines = 0;
917  for (i=curmsg-1; i>=0; i--) {
918    lines += owl_message_get_numlines(owl_view_get_element(v, i));
919    if (lines > recwinlines/2) break;
920    last = i;
921  }
922  return(last);
923}
924 
925int owl_function_calculate_topmsg_paged(int direction, owl_view *v, int curmsg, int topmsg, int recwinlines, int center_on_page)
926{
927  int i, last, lines, savey;
928 
929  /* If we're off the top of the screen, scroll up such that the
930   * curmsg is near the botton of the screen. */
931  if (curmsg < topmsg) {
932    last = curmsg;
933    lines = 0;
934    for (i=curmsg; i>=0; i--) {
935      lines += owl_message_get_numlines(owl_view_get_element(v, i));
936      if (lines > recwinlines) break;
937    last = i;
938    }
939    if (center_on_page) {
940      return(owl_function_calculate_topmsg_center(direction, v, curmsg, 0, recwinlines));
941    } else {
942      return(last);
943    }
944  }
945
946  /* Find number of lines from top to bottom of curmsg (store in savey) */
947  savey=0;
948  for (i=topmsg; i<=curmsg; i++) {
949    savey+=owl_message_get_numlines(owl_view_get_element(v, i));
950  }
951
952  /* if we're off the bottom of the screen, scroll down */
953  if (savey > recwinlines) {
954    if (center_on_page) {
955      return(owl_function_calculate_topmsg_center(direction, v, curmsg, 0, recwinlines));
956    } else {
957      return(curmsg);
958    }
959  }
960
961  /* else just stay as we are... */
962  return(topmsg);
963}
964
965
966int owl_function_calculate_topmsg_normal(int direction, owl_view *v, int curmsg, int topmsg, int recwinlines)
967{
968  int savey, j, i, foo, y;
969
970  if (curmsg<0) return(topmsg);
971   
972  /* If we're off the top of the screen then center */
973  if (curmsg<topmsg) {
974    topmsg=owl_function_calculate_topmsg_center(direction, v, curmsg, 0, recwinlines);
975  }
976
977  /* Find number of lines from top to bottom of curmsg (store in savey) */
978  savey=0;
979  for (i=topmsg; i<=curmsg; i++) {
980    savey+=owl_message_get_numlines(owl_view_get_element(v, i));
981  }
982
983  /* If we're off the bottom of the screen, set the topmsg to curmsg
984   * and scroll upwards */
985  if (savey > recwinlines) {
986    topmsg=curmsg;
987    savey=owl_message_get_numlines(owl_view_get_element(v, i));
988    direction=OWL_DIRECTION_UPWARDS;
989  }
990 
991  /* If our bottom line is less than 1/4 down the screen then scroll up */
992  if (direction == OWL_DIRECTION_UPWARDS || direction == OWL_DIRECTION_NONE) {
993    if (savey < (recwinlines / 4)) {
994      y=0;
995      for (j=curmsg; j>=0; j--) {
996        foo=owl_message_get_numlines(owl_view_get_element(v, j));
997        /* will we run the curmsg off the screen? */
998        if ((foo+y) >= recwinlines) {
999          j++;
1000          if (j>curmsg) j=curmsg;
1001          break;
1002        }
1003        /* have saved 1/2 the screen space? */
1004        y+=foo;
1005        if (y > (recwinlines / 2)) break;
1006      }
1007      if (j<0) j=0;
1008      return(j);
1009    }
1010  }
1011
1012  if (direction == OWL_DIRECTION_DOWNWARDS || direction == OWL_DIRECTION_NONE) {
1013    /* If curmsg bottom line is more than 3/4 down the screen then scroll down */
1014    if (savey > ((recwinlines * 3)/4)) {
1015      y=0;
1016      /* count lines from the top until we can save 1/2 the screen size */
1017      for (j=topmsg; j<curmsg; j++) {
1018        y+=owl_message_get_numlines(owl_view_get_element(v, j));
1019        if (y > (recwinlines / 2)) break;
1020      }
1021      if (j==curmsg) {
1022        j--;
1023      }
1024      return(j+1);
1025    }
1026  }
1027
1028  return(topmsg);
1029}
1030
1031
1032void owl_function_resize()
1033{
1034  owl_global_set_resize_pending(&g);
1035}
1036
1037
1038void owl_function_run_buffercommand()
1039{
1040  char *buff;
1041
1042  buff=owl_global_get_buffercommand(&g);
1043  if (!strncmp(buff, "zwrite ", 7)) {
1044    owl_function_zwrite(buff);
1045  } else if (!strncmp(buff, "aimwrite ", 9)) {
1046    owl_function_aimwrite(buff+9);
1047  }
1048}
1049
1050void owl_function_debugmsg(char *fmt, ...)
1051{
1052  FILE *file;
1053  time_t now;
1054  char buff1[LINE], buff2[LINE];
1055  va_list ap;
1056  va_start(ap, fmt);
1057
1058  if (!owl_global_is_debug_fast(&g)) return;
1059
1060  file=fopen(owl_global_get_debug_file(&g), "a");
1061  if (!file) return;
1062
1063  now=time(NULL);
1064  strcpy(buff1, ctime(&now));
1065  buff1[strlen(buff1)-1]='\0';
1066
1067  owl_global_get_runtime_string(&g, buff2);
1068 
1069  fprintf(file, "[%i -  %s - %s]: ", (int) getpid(), buff1, buff2);
1070  vfprintf(file, fmt, ap);
1071  fprintf(file, "\n");
1072  fclose(file);
1073
1074  va_end(ap);
1075}
1076
1077
1078void owl_function_refresh()
1079{
1080  owl_function_resize();
1081}
1082
1083void owl_function_beep()
1084{
1085  if (owl_global_is_bell(&g)) {
1086    beep();
1087    owl_global_set_needrefresh(&g); /* do we really need this? */
1088  }
1089}
1090
1091
1092void owl_function_subscribe(char *class, char *inst, char *recip)
1093{
1094  int ret;
1095
1096  ret=owl_zephyr_sub(class, inst, recip);
1097  if (ret) {
1098    owl_function_makemsg("Error subscribing.");
1099  } else {
1100    owl_function_makemsg("Subscribed.");
1101  }
1102}
1103
1104
1105void owl_function_unsubscribe(char *class, char *inst, char *recip)
1106{
1107  int ret;
1108
1109  ret=owl_zephyr_unsub(class, inst, recip);
1110  if (ret) {
1111    owl_function_makemsg("Error subscribing.");
1112  } else {
1113    owl_function_makemsg("Unsubscribed.");
1114  }
1115}
1116
1117
1118void owl_function_set_cursor(WINDOW *win)
1119{
1120  wnoutrefresh(win);
1121}
1122
1123
1124void owl_function_full_redisplay()
1125{
1126  redrawwin(owl_global_get_curs_recwin(&g));
1127  redrawwin(owl_global_get_curs_sepwin(&g));
1128  redrawwin(owl_global_get_curs_typwin(&g));
1129  redrawwin(owl_global_get_curs_msgwin(&g));
1130
1131  wnoutrefresh(owl_global_get_curs_recwin(&g));
1132  wnoutrefresh(owl_global_get_curs_sepwin(&g));
1133  wnoutrefresh(owl_global_get_curs_typwin(&g));
1134  wnoutrefresh(owl_global_get_curs_msgwin(&g));
1135 
1136  sepbar("");
1137  owl_function_makemsg("");
1138
1139  owl_global_set_needrefresh(&g);
1140}
1141
1142
1143void owl_function_popless_text(char *text)
1144{
1145  owl_popwin *pw;
1146  owl_viewwin *v;
1147
1148  pw=owl_global_get_popwin(&g);
1149  v=owl_global_get_viewwin(&g);
1150
1151  owl_popwin_up(pw);
1152  owl_viewwin_init_text(v, owl_popwin_get_curswin(pw),
1153                        owl_popwin_get_lines(pw), owl_popwin_get_cols(pw),
1154                        text);
1155  owl_popwin_refresh(pw);
1156  owl_viewwin_redisplay(v, 0);
1157  owl_global_set_needrefresh(&g);
1158}
1159
1160
1161void owl_function_popless_fmtext(owl_fmtext *fm)
1162{
1163  owl_popwin *pw;
1164  owl_viewwin *v;
1165
1166  pw=owl_global_get_popwin(&g);
1167  v=owl_global_get_viewwin(&g);
1168
1169  owl_popwin_up(pw);
1170  owl_viewwin_init_fmtext(v, owl_popwin_get_curswin(pw),
1171                   owl_popwin_get_lines(pw), owl_popwin_get_cols(pw),
1172                   fm);
1173  owl_popwin_refresh(pw);
1174  owl_viewwin_redisplay(v, 0);
1175  owl_global_set_needrefresh(&g);
1176}
1177
1178void owl_function_about()
1179{
1180  char buff[5000];
1181
1182  sprintf(buff, "This is owl version %s\n", OWL_VERSION_STRING);
1183  strcat(buff, "\nOwl was written by James Kretchmar at the Massachusetts\n");
1184  strcat(buff, "Institute of Technology.  The first version, 0.5, was\n");
1185  strcat(buff, "released in March 2002.\n");
1186  strcat(buff, "\n");
1187  strcat(buff, "The name 'owl' was chosen in reference to the owls in the\n");
1188  strcat(buff, "Harry Potter novels, who are tasked with carrying messages\n");
1189  strcat(buff, "between Witches and Wizards.\n");
1190  strcat(buff, "\n");
1191  strcat(buff, "Copyright 2002 Massachusetts Institute of Technology\n");
1192  strcat(buff, "\n");
1193  strcat(buff, "Permission to use, copy, modify, and distribute this\n");
1194  strcat(buff, "software and its documentation for any purpose and without\n");
1195  strcat(buff, "fee is hereby granted, provided that the above copyright\n");
1196  strcat(buff, "notice and this permission notice appear in all copies\n");
1197  strcat(buff, "and in supporting documentation.  No representation is\n");
1198  strcat(buff, "made about the suitability of this software for any\n");
1199  strcat(buff, "purpose.  It is provided \"as is\" without express\n");
1200  strcat(buff, "or implied warranty.\n");
1201  owl_function_popless_text(buff);
1202}
1203
1204void owl_function_info()
1205{
1206  owl_message *m;
1207  owl_fmtext fm;
1208  ZNotice_t *n;
1209  char buff[10000], tmpbuff[1024];
1210  char *ptr;
1211  int i, j, fields, len;
1212  owl_view *v;
1213
1214  owl_fmtext_init_null(&fm);
1215 
1216  v=owl_global_get_current_view(&g);
1217  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
1218  if (!m || owl_view_get_size(v)==0) {
1219    owl_function_makemsg("No message selected\n");
1220    return;
1221  }
1222
1223  owl_fmtext_append_normal(&fm, "Msg Id    : ");
1224  sprintf(buff, "%i", owl_message_get_id(m));
1225  owl_fmtext_append_normal(&fm, buff);
1226  owl_fmtext_append_normal(&fm, "\n");
1227  if (owl_message_is_type_zephyr(m)) {
1228    owl_fmtext_append_normal(&fm, "Type      : zephyr\n");
1229  } else if (owl_message_is_type_admin(m)) {
1230    owl_fmtext_append_normal(&fm, "Type      : admin\n");
1231  } else if (owl_message_is_type_generic(m)) {
1232    owl_fmtext_append_normal(&fm, "Type      : generic\n");
1233  } else if (owl_message_is_type_aim(m)) {
1234    owl_fmtext_append_normal(&fm, "Type      : aim\n");
1235  } else {
1236    owl_fmtext_append_normal(&fm, "Type      : unknown\n");
1237  }
1238  if (owl_message_is_direction_in(m)) {
1239    owl_fmtext_append_normal(&fm, "Direction : in\n");
1240  } else if (owl_message_is_direction_out(m)) {
1241    owl_fmtext_append_normal(&fm, "Direction : out\n");
1242  } else if (owl_message_is_direction_none(m)) {
1243    owl_fmtext_append_normal(&fm, "Direction : none\n");
1244  } else {
1245    owl_fmtext_append_normal(&fm, "Direction : unknown\n");
1246  }
1247  owl_fmtext_append_normal(&fm, "Time      : ");
1248  owl_fmtext_append_normal(&fm, owl_message_get_timestr(m));
1249  owl_fmtext_append_normal(&fm, "\n");
1250
1251  owl_fmtext_append_normal(&fm, "Sender    : ");
1252  owl_fmtext_append_normal(&fm, owl_message_get_sender(m));
1253  owl_fmtext_append_normal(&fm, "\n");
1254
1255  owl_fmtext_append_normal(&fm, "Recipient : ");
1256  owl_fmtext_append_normal(&fm, owl_message_get_recipient(m));
1257  owl_fmtext_append_normal(&fm, "\n");
1258
1259
1260  if (owl_message_is_type_zephyr(m)) {
1261    owl_fmtext_append_bold(&fm, "  Zephyr Specific Info\n");
1262   
1263    if (owl_message_is_direction_in(m)) {
1264      n=owl_message_get_notice(m);
1265      owl_fmtext_append_normal(&fm, "Class     : ");
1266      owl_fmtext_append_normal(&fm, owl_message_get_class(m));
1267      owl_fmtext_append_normal(&fm, "\n");
1268      owl_fmtext_append_normal(&fm, "Instance  : ");
1269      owl_fmtext_append_normal(&fm, owl_message_get_instance(m));
1270      owl_fmtext_append_normal(&fm, "\n");
1271      owl_fmtext_append_normal(&fm, "Opcode    : ");
1272      owl_fmtext_append_normal(&fm, owl_message_get_opcode(m));
1273      owl_fmtext_append_normal(&fm, "\n");
1274      owl_fmtext_append_normal(&fm, "Kind      : ");
1275      if (n->z_kind==UNSAFE) {
1276        owl_fmtext_append_normal(&fm, "UNSAFE\n");
1277      } else if (n->z_kind==UNACKED) {
1278        owl_fmtext_append_normal(&fm, "UNACKED\n");
1279      } else if (n->z_kind==ACKED) {
1280        owl_fmtext_append_normal(&fm, "ACKED\n");
1281      } else if (n->z_kind==HMACK) {
1282        owl_fmtext_append_normal(&fm, "HMACK\n");
1283      } else if (n->z_kind==HMCTL) {
1284        owl_fmtext_append_normal(&fm, "HMCTL\n");
1285      } else if (n->z_kind==SERVACK) {
1286        owl_fmtext_append_normal(&fm, "SERVACK\n");
1287      } else if (n->z_kind==SERVNAK) {
1288        owl_fmtext_append_normal(&fm, "SERVNACK\n");
1289      } else if (n->z_kind==CLIENTACK) {
1290        owl_fmtext_append_normal(&fm, "CLIENTACK\n");
1291      } else if (n->z_kind==STAT) {
1292        owl_fmtext_append_normal(&fm, "STAT\n");
1293      } else {
1294        owl_fmtext_append_normal(&fm, "ILLEGAL VALUE\n");
1295      }
1296
1297      owl_fmtext_append_normal(&fm, "Time      : ");
1298      owl_fmtext_append_normal(&fm, owl_message_get_timestr(m));
1299      owl_fmtext_append_normal(&fm, "\n");
1300      owl_fmtext_append_normal(&fm, "Host      : ");
1301      owl_fmtext_append_normal(&fm, owl_message_get_hostname(m));
1302      owl_fmtext_append_normal(&fm, "\n");
1303      sprintf(buff, "Port      : %i\n", n->z_port);
1304      owl_fmtext_append_normal(&fm, buff);
1305
1306      owl_fmtext_append_normal(&fm,    "Auth      : ");
1307      if (n->z_auth == ZAUTH_FAILED) {
1308        owl_fmtext_append_normal(&fm, "FAILED\n");
1309      } else if (n->z_auth == ZAUTH_NO) {
1310        owl_fmtext_append_normal(&fm, "NO\n");
1311      } else if (n->z_auth == ZAUTH_YES) {
1312        owl_fmtext_append_normal(&fm, "YES\n");
1313      } else {
1314        sprintf(buff, "Unknown State (%i)\n", n->z_auth);
1315        owl_fmtext_append_normal(&fm, buff);
1316      }
1317
1318      sprintf(buff, "Checkd Ath: %i\n", n->z_checked_auth);
1319      sprintf(buff, "%sMulti notc: %s\n", buff, n->z_multinotice);
1320      sprintf(buff, "%sNum other : %i\n", buff, n->z_num_other_fields);
1321      sprintf(buff, "%sMsg Len   : %i\n", buff, n->z_message_len);
1322      owl_fmtext_append_normal(&fm, buff);
1323     
1324      sprintf(buff, "Fields    : %i\n", owl_zephyr_get_num_fields(n));
1325      owl_fmtext_append_normal(&fm, buff);
1326     
1327      fields=owl_zephyr_get_num_fields(n);
1328      for (i=0; i<fields; i++) {
1329        sprintf(buff, "Field %i   : ", i+1);
1330       
1331        ptr=owl_zephyr_get_field(n, i+1, &len);
1332        if (!ptr) break;
1333        if (len<30) {
1334          strncpy(tmpbuff, ptr, len);
1335          tmpbuff[len]='\0';
1336        } else {
1337          strncpy(tmpbuff, ptr, 30);
1338          tmpbuff[30]='\0';
1339          strcat(tmpbuff, "...");
1340        }
1341       
1342        for (j=0; j<strlen(tmpbuff); j++) {
1343          if (tmpbuff[j]=='\n') tmpbuff[j]='~';
1344          if (tmpbuff[j]=='\r') tmpbuff[j]='!';
1345        }
1346       
1347        strcat(buff, tmpbuff);
1348        strcat(buff, "\n");
1349        owl_fmtext_append_normal(&fm, buff);
1350      }
1351      owl_fmtext_append_normal(&fm, "Default Fm:");
1352      owl_fmtext_append_normal(&fm, n->z_default_format);
1353    }
1354  }
1355
1356  if (owl_message_is_type_aim(m)) {
1357
1358  }
1359 
1360  owl_function_popless_fmtext(&fm);
1361}
1362
1363
1364void owl_function_curmsg_to_popwin()
1365{
1366  owl_popwin *pw;
1367  owl_view *v;
1368  owl_message *m;
1369
1370  v = owl_global_get_current_view(&g);
1371
1372  pw=owl_global_get_popwin(&g);
1373
1374  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
1375
1376  if (!m || owl_view_get_size(v)==0) {
1377    owl_function_makemsg("No current message");
1378    return;
1379  }
1380
1381  owl_function_popless_fmtext(owl_message_get_fmtext(m));
1382}
1383
1384
1385void owl_function_page_curmsg(int step)
1386{
1387  /* scroll down or up within the current message IF the message is truncated */
1388
1389  int offset, curmsg, lines;
1390  owl_view *v;
1391  owl_message *m;
1392
1393  offset=owl_global_get_curmsg_vert_offset(&g);
1394  v=owl_global_get_current_view(&g);
1395  curmsg=owl_global_get_curmsg(&g);
1396  m=owl_view_get_element(v, curmsg);
1397  if (!m || owl_view_get_size(v)==0) return;
1398  lines=owl_message_get_numlines(m);
1399
1400  if (offset==0) {
1401    /* Bail if the curmsg isn't the last one displayed */
1402    if (curmsg != owl_mainwin_get_last_msg(owl_global_get_mainwin(&g))) {
1403      owl_function_makemsg("The entire message is already displayed");
1404      return;
1405    }
1406   
1407    /* Bail if we're not truncated */
1408    if (!owl_mainwin_is_curmsg_truncated(owl_global_get_mainwin(&g))) {
1409      owl_function_makemsg("The entire message is already displayed");
1410      return;
1411    }
1412  }
1413 
1414 
1415  /* don't scroll past the last line */
1416  if (step>0) {
1417    if (offset+step > lines-1) {
1418      owl_global_set_curmsg_vert_offset(&g, lines-1);
1419    } else {
1420      owl_global_set_curmsg_vert_offset(&g, offset+step);
1421    }
1422  }
1423
1424  /* would we be before the beginning of the message? */
1425  if (step<0) {
1426    if (offset+step<0) {
1427      owl_global_set_curmsg_vert_offset(&g, 0);
1428    } else {
1429      owl_global_set_curmsg_vert_offset(&g, offset+step);
1430    }
1431  }
1432 
1433  /* redisplay */
1434  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
1435  owl_global_set_needrefresh(&g);
1436}
1437
1438void owl_function_resize_typwin(int newsize)
1439{
1440  owl_global_set_typwin_lines(&g, newsize);
1441  owl_function_resize();
1442}
1443
1444void owl_function_typwin_grow()
1445{
1446  int i;
1447
1448  i=owl_global_get_typwin_lines(&g);
1449  owl_function_resize_typwin(i+1);
1450}
1451
1452void owl_function_typwin_shrink()
1453{
1454  int i;
1455
1456  i=owl_global_get_typwin_lines(&g);
1457  if (i>2) {
1458    owl_function_resize_typwin(i-1);
1459  }
1460}
1461
1462void owl_function_mainwin_pagedown()
1463{
1464  int i;
1465
1466  i=owl_mainwin_get_last_msg(owl_global_get_mainwin(&g));
1467  if (i<0) return;
1468  if (owl_mainwin_is_last_msg_truncated(owl_global_get_mainwin(&g))
1469      && (owl_global_get_curmsg(&g) < i)
1470      && (i>0)) {
1471    i--;
1472  }
1473  owl_global_set_curmsg(&g, i);
1474  owl_function_nextmsg();
1475}
1476
1477void owl_function_mainwin_pageup()
1478{
1479  owl_global_set_curmsg(&g, owl_global_get_topmsg(&g));
1480  owl_function_prevmsg();
1481}
1482
1483void owl_function_getsubs()
1484{
1485  int ret, num, i, one;
1486  ZSubscription_t sub;
1487  char *buff, *tmpbuff;
1488
1489  one = 1;
1490
1491  ret=ZRetrieveSubscriptions(0, &num);
1492  if (ret == ZERR_TOOMANYSUBS) {
1493    owl_function_makemsg("Zephyr: too many subscriptions");
1494    return;
1495  }
1496
1497  buff=owl_malloc(num*500);
1498  tmpbuff=owl_malloc(num*500);
1499  strcpy(buff, "");
1500  for (i=0; i<num; i++) {
1501    if ((ret = ZGetSubscriptions(&sub, &one)) != ZERR_NONE) {
1502      owl_function_makemsg("Error while getting subscriptions");
1503      owl_free(buff);
1504      owl_free(tmpbuff);
1505      ZFlushSubscriptions();
1506      return;
1507    } else {
1508      sprintf(tmpbuff, "<%s,%s,%s>\n%s", sub.zsub_class, sub.zsub_classinst, sub.zsub_recipient, buff);
1509      strcpy(buff, tmpbuff);
1510    }
1511  }
1512
1513  owl_function_popless_text(buff);
1514  owl_free(buff);
1515  owl_free(tmpbuff);
1516  ZFlushSubscriptions();
1517}
1518
1519#define PABUFLEN 5000
1520void owl_function_printallvars()
1521{
1522  char buff[PABUFLEN], *pos, *name;
1523  owl_list varnames;
1524  int i, numvarnames, rem;
1525
1526  pos = buff;
1527  pos += sprintf(pos, "%-20s = %s\n", "VARIABLE", "VALUE");
1528  pos += sprintf(pos, "%-20s   %s\n",  "--------", "-----");
1529  owl_variable_dict_get_names(owl_global_get_vardict(&g), &varnames);
1530  rem = (buff+PABUFLEN)-pos-1;
1531  numvarnames = owl_list_get_size(&varnames);
1532  for (i=0; i<numvarnames; i++) {
1533    name = owl_list_get_element(&varnames, i);
1534    if (name && name[0]!='_') {
1535      rem = (buff+PABUFLEN)-pos-1;   
1536      pos += snprintf(pos, rem, "\n%-20s = ", name);
1537      rem = (buff+PABUFLEN)-pos-1;   
1538      owl_variable_get_tostring(owl_global_get_vardict(&g), name, pos, rem);
1539      pos = buff+strlen(buff);
1540    }
1541  }
1542  rem = (buff+PABUFLEN)-pos-1;   
1543  snprintf(pos, rem, "\n");
1544  owl_variable_dict_namelist_free(&varnames);
1545 
1546  owl_function_popless_text(buff);
1547}
1548
1549void owl_function_show_variables()
1550{
1551  owl_list varnames;
1552  owl_fmtext fm; 
1553  int i, numvarnames;
1554  char *varname;
1555
1556  owl_fmtext_init_null(&fm);
1557  owl_fmtext_append_bold(&fm, 
1558      "Variables: (use 'show variable <name>' for details)\n");
1559  owl_variable_dict_get_names(owl_global_get_vardict(&g), &varnames);
1560  numvarnames = owl_list_get_size(&varnames);
1561  for (i=0; i<numvarnames; i++) {
1562    varname = owl_list_get_element(&varnames, i);
1563    if (varname && varname[0]!='_') {
1564      owl_variable_describe(owl_global_get_vardict(&g), varname, &fm);
1565    }
1566  }
1567  owl_variable_dict_namelist_free(&varnames);
1568  owl_function_popless_fmtext(&fm);
1569  owl_fmtext_free(&fm);
1570}
1571
1572void owl_function_show_variable(char *name)
1573{
1574  owl_fmtext fm; 
1575
1576  owl_fmtext_init_null(&fm);
1577  owl_variable_get_help(owl_global_get_vardict(&g), name, &fm);
1578  owl_function_popless_fmtext(&fm);
1579  owl_fmtext_free(&fm); 
1580}
1581
1582/* note: this applies to global message list, not to view.
1583 * If flag is 1, deletes.  If flag is 0, undeletes. */
1584void owl_function_delete_by_id(int id, int flag)
1585{
1586  owl_messagelist *ml;
1587  owl_message *m;
1588  ml = owl_global_get_msglist(&g);
1589  m = owl_messagelist_get_by_id(ml, id);
1590  if (m) {
1591    if (flag == 1) {
1592      owl_message_mark_delete(m);
1593    } else if (flag == 0) {
1594      owl_message_unmark_delete(m);
1595    }
1596    owl_mainwin_redisplay(owl_global_get_mainwin(&g));
1597    owl_global_set_needrefresh(&g);
1598  } else {
1599    owl_function_makemsg("No message with id %d: unable to mark for (un)delete",id);
1600  }
1601}
1602
1603void owl_function_delete_automsgs()
1604{
1605  /* mark for deletion all messages in the current view that match the
1606   * 'trash' filter */
1607
1608  int i, j, count;
1609  owl_message *m;
1610  owl_view *v;
1611  owl_filter *f;
1612
1613  /* get the trash filter */
1614  f=owl_global_get_filter(&g, "trash");
1615  if (!f) {
1616    owl_function_makemsg("No trash filter defined");
1617    return;
1618  }
1619
1620  v=owl_global_get_current_view(&g);
1621
1622  count=0;
1623  j=owl_view_get_size(v);
1624  for (i=0; i<j; i++) {
1625    m=owl_view_get_element(v, i);
1626    if (owl_filter_message_match(f, m)) {
1627      count++;
1628      owl_message_mark_delete(m);
1629    }
1630  }
1631  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
1632  owl_function_makemsg("%i messages marked for deletion", count);
1633  owl_global_set_needrefresh(&g);
1634}
1635
1636
1637void owl_function_status()
1638{
1639  char buff[5000];
1640  time_t start;
1641  int up, days, hours, minutes;
1642
1643  start=owl_global_get_starttime(&g);
1644
1645  sprintf(buff, "Version: %s\n", OWL_VERSION_STRING);
1646  sprintf(buff, "%sScreen size: %i lines, %i columns\n", buff, owl_global_get_lines(&g), owl_global_get_cols(&g));
1647  sprintf(buff, "%sStartup Arugments: %s\n", buff, owl_global_get_startupargs(&g));
1648  sprintf(buff, "%sStartup Time: %s", buff, ctime(&start));
1649
1650  up=owl_global_get_runtime(&g);
1651  days=up/86400;
1652  up-=days*86400;
1653  hours=up/3600;
1654  up-=hours*3600;
1655  minutes=up/60;
1656  up-=minutes*60;
1657  sprintf(buff, "%sRun Time: %i days %2.2i:%2.2i:%2.2i\n", buff, days, hours, minutes, up);
1658
1659  if (owl_global_get_hascolors(&g)) {
1660    sprintf(buff, "%sColor: Yes, %i color pairs.\n", buff, owl_global_get_colorpairs(&g));
1661  } else {
1662    strcat(buff, "Color: No.\n");
1663  }
1664
1665  sprintf(buff, "%sMemory Malloced: %i\n", buff, owl_global_get_malloced(&g));
1666  sprintf(buff, "%sMemory Freed: %i\n", buff, owl_global_get_freed(&g));
1667  sprintf(buff, "%sMemory In Use: %i\n", buff, owl_global_get_meminuse(&g));
1668
1669  owl_function_popless_text(buff);
1670}
1671
1672void owl_function_show_term()
1673{
1674  owl_fmtext fm;
1675  char buff[LINE];
1676
1677  owl_fmtext_init_null(&fm);
1678  sprintf(buff, "Terminal Lines: %i\nTerminal Columns: %i\n",
1679          owl_global_get_lines(&g),
1680          owl_global_get_cols(&g));
1681  owl_fmtext_append_normal(&fm, buff);
1682
1683  if (owl_global_get_hascolors(&g)) {
1684    owl_fmtext_append_normal(&fm, "Color: Yes\n");
1685    sprintf(buff, "Number of color pairs: %i\n", owl_global_get_colorpairs(&g));
1686    owl_fmtext_append_normal(&fm, buff);
1687    sprintf(buff, "Can change colors: %s\n", can_change_color() ? "yes" : "no");
1688    owl_fmtext_append_normal(&fm, buff);
1689  } else {
1690    owl_fmtext_append_normal(&fm, "Color: No\n");
1691  }
1692
1693  owl_function_popless_fmtext(&fm);
1694  owl_fmtext_free(&fm);
1695}
1696
1697
1698void owl_function_reply(int type, int enter)
1699{
1700  /* if type = 0 then normal reply.
1701   * if type = 1 then it's a reply to sender
1702   * if enter = 0 then allow the command to be edited
1703   * if enter = 1 then don't wait for editing
1704   */
1705  char *buff, *oldbuff;
1706  owl_message *m;
1707  owl_filter *f;
1708 
1709  if (owl_view_get_size(owl_global_get_current_view(&g))==0) {
1710    owl_function_makemsg("No message selected");
1711  } else {
1712    char *class, *inst, *to, *cc=NULL;
1713   
1714    m=owl_view_get_element(owl_global_get_current_view(&g), owl_global_get_curmsg(&g));
1715    if (!m) {
1716      owl_function_makemsg("No message selected");
1717      return;
1718    }
1719
1720    /* first check if we catch the reply-lockout filter */
1721    f=owl_global_get_filter(&g, "reply-lockout");
1722    if (f) {
1723      if (owl_filter_message_match(f, m)) {
1724        owl_function_makemsg("Sorry, replies to this message have been disabled by the reply-lockout filter");
1725        return;
1726      }
1727    }
1728
1729    /* admin */
1730    if (owl_message_is_type_admin(m)) {
1731      owl_function_makemsg("You cannot reply to an admin message");
1732      return;
1733    }
1734
1735    /* zephyr */
1736    if (owl_message_is_type_zephyr(m)) {
1737      /* for now we disable replies to zcrypt messages, since we can't
1738         support an encrypted reply */
1739      if (!strcasecmp(owl_message_get_opcode(m), "crypt")) {
1740        owl_function_makemsg("Replies to zcrypt messages are not enabled in this release");
1741        return;
1742      }
1743
1744      if (owl_message_is_direction_out(m)) {
1745        owl_function_zwrite_setup(owl_message_get_zwriteline(m));
1746        owl_global_set_buffercommand(&g, owl_message_get_zwriteline(m));
1747        return;
1748      }
1749     
1750      if (owl_message_is_login(m)) {
1751        class="MESSAGE";
1752        inst="PERSONAL";
1753        to=owl_message_get_sender(m);
1754      } else if (type==1) {
1755        class="MESSAGE";
1756        inst="PERSONAL";
1757        to=owl_message_get_sender(m);
1758      } else {
1759        class=owl_message_get_class(m);
1760        inst=owl_message_get_instance(m);
1761        to=owl_message_get_recipient(m);
1762        cc=owl_message_get_cc(m);
1763        if (!strcmp(to, "") || !strcmp(to, "*")) {
1764          to="";
1765        } else if (to[0]=='@') {
1766          /* leave it, to get the realm */
1767        } else {
1768          to=owl_message_get_sender(m);
1769        }
1770      }
1771       
1772      /* create the command line */
1773      buff = owl_strdup("zwrite");
1774      if (strcasecmp(class, "message")) {
1775        buff = owl_sprintf("%s -c %s%s%s", oldbuff=buff, owl_getquoting(class), class, owl_getquoting(class));
1776        owl_free(oldbuff);
1777      }
1778      if (strcasecmp(inst, "personal")) {
1779        buff = owl_sprintf("%s -i %s%s%s", oldbuff=buff, owl_getquoting(inst), inst, owl_getquoting(inst));
1780        owl_free(oldbuff);
1781      }
1782      if (*to != '\0') {
1783        char *tmp, *oldtmp, *tmp2;
1784        tmp=short_zuser(to);
1785        if (cc) {
1786          tmp = owl_util_uniq(oldtmp=tmp, cc, "-");
1787          owl_free(oldtmp);
1788          buff = owl_sprintf("%s -C %s", oldbuff=buff, tmp);
1789          owl_free(oldbuff);
1790        } else {
1791          if (owl_global_is_smartstrip(&g)) {
1792            tmp2=tmp;
1793            tmp=owl_util_smartstripped_user(tmp2);
1794            owl_free(tmp2);
1795          }
1796          buff = owl_sprintf("%s %s", oldbuff=buff, tmp);
1797          owl_free(oldbuff);
1798        }
1799        owl_free(tmp);
1800      }
1801      if (cc) owl_free(cc);
1802    }
1803
1804    /* aim */
1805    if (owl_message_is_type_aim(m)) {
1806      if (owl_message_is_direction_out(m)) {
1807        buff=owl_sprintf("aimwrite %s", owl_message_get_recipient(m));
1808      } else {
1809        buff=owl_sprintf("aimwrite %s", owl_message_get_sender(m));
1810      }
1811    }
1812   
1813    if (enter) {
1814      owl_history *hist = owl_global_get_cmd_history(&g);
1815      owl_history_store(hist, buff);
1816      owl_history_reset(hist);
1817      owl_function_command_norv(buff);
1818    } else {
1819      owl_function_start_command(buff);
1820    }
1821    owl_free(buff);
1822  }
1823}
1824
1825void owl_function_zlocate(int argc, char **argv, int auth)
1826{
1827  owl_fmtext fm;
1828  char *ptr, buff[LINE];
1829  int i;
1830
1831  owl_fmtext_init_null(&fm);
1832
1833  for (i=0; i<argc; i++) {
1834    ptr=long_zuser(argv[i]);
1835    owl_zephyr_zlocate(ptr, buff, auth);
1836    owl_fmtext_append_normal(&fm, buff);
1837    owl_free(ptr);
1838  }
1839
1840  owl_function_popless_fmtext(&fm);
1841  owl_fmtext_free(&fm);
1842}
1843
1844void owl_function_start_command(char *line)
1845{
1846  int i, j;
1847  owl_editwin *tw;
1848
1849  tw=owl_global_get_typwin(&g);
1850  owl_global_set_typwin_active(&g);
1851  owl_editwin_new_style(tw, OWL_EDITWIN_STYLE_ONELINE, 
1852                        owl_global_get_cmd_history(&g));
1853
1854  owl_editwin_set_locktext(tw, "command: ");
1855  owl_global_set_needrefresh(&g);
1856
1857  j=strlen(line);
1858  for (i=0; i<j; i++) {
1859    owl_editwin_process_char(tw, line[i]);
1860  }
1861  owl_editwin_redisplay(tw, 0);
1862}
1863
1864char *owl_function_exec(int argc, char **argv, char *buff, int type)
1865{
1866  /* if type == 1 display in a popup
1867   * if type == 2 display an admin messages
1868   * if type == 0 return output
1869   * else display in a popup
1870   */
1871  char *newbuff, *redirect = " 2>&1 < /dev/null";
1872  char *out, buff2[1024];
1873  int size;
1874  FILE *p;
1875
1876  if (argc<2) {
1877    owl_function_makemsg("Wrong number of arguments to the exec command");
1878    return NULL;
1879  }
1880
1881  buff = skiptokens(buff, 1);
1882  newbuff = owl_malloc(strlen(buff)+strlen(redirect)+1);
1883  strcpy(newbuff, buff);
1884  strcat(newbuff, redirect);
1885
1886  p=popen(newbuff, "r");
1887  out=owl_malloc(1024);
1888  size=1024;
1889  strcpy(out, "");
1890  while (fgets(buff2, 1024, p)!=NULL) {
1891    size+=1024;
1892    out=owl_realloc(out, size);
1893    strcat(out, buff2);
1894  }
1895  pclose(p);
1896
1897  if (type==1) {
1898    owl_function_popless_text(out);
1899  } else if (type==0) {
1900    return out;
1901  } else if (type==2) {
1902    owl_function_adminmsg(buff, out);
1903  } else {
1904    owl_function_popless_text(out);
1905  }
1906  owl_free(out);
1907  return NULL;
1908}
1909
1910
1911char *owl_function_perl(int argc, char **argv, char *buff, int type)
1912{
1913  /* if type == 1 display in a popup
1914   * if type == 2 display an admin messages
1915   * if type == 0 return output
1916   * else display in a popup
1917   */
1918  char *perlout;
1919
1920  if (argc<2) {
1921    owl_function_makemsg("Wrong number of arguments to perl command");
1922    return NULL;
1923  }
1924
1925  /* consume first token (argv[0]) */
1926  buff = skiptokens(buff, 1);
1927
1928  perlout = owl_config_execute(buff);
1929  if (perlout) { 
1930    if (type==1) {
1931      owl_function_popless_text(perlout);
1932    } else if (type==2) {
1933      owl_function_adminmsg(buff, perlout);
1934    } else if (type==0) {
1935      return perlout;
1936    } else {
1937      owl_function_popless_text(perlout);
1938    }
1939    owl_free(perlout);
1940  }
1941  return NULL;
1942}
1943
1944
1945void owl_function_change_view(char *filtname)
1946{
1947  owl_view *v;
1948  owl_filter *f;
1949  int curid=-1, newpos, curmsg;
1950  owl_message *curm=NULL;
1951
1952  v=owl_global_get_current_view(&g);
1953  curmsg=owl_global_get_curmsg(&g);
1954  if (curmsg==-1) {
1955    owl_function_debugmsg("Hit the curmsg==-1 case in change_view");
1956  } else {
1957    curm=owl_view_get_element(v, curmsg);
1958    if (curm) {
1959      curid=owl_message_get_id(curm);
1960      owl_view_save_curmsgid(v, curid);
1961    }
1962  }
1963
1964  /* grab the filter */;
1965  f=owl_global_get_filter(&g, filtname);
1966  if (!f) {
1967    owl_function_makemsg("Unknown filter");
1968    return;
1969  }
1970
1971  /* free the existing view and create a new one based on the filter */
1972  owl_view_free(v);
1973  owl_view_create(v, f);
1974
1975  /* Figure out what to set the current message to.
1976   * - If the view we're leaving has messages in it, go to the closest message
1977   *   to the last message pointed to in that view.
1978   * - If the view we're leaving is empty, try to restore the position
1979   *   from the last time we were in the new view.  */
1980  if (curm) {
1981    newpos = owl_view_get_nearest_to_msgid(v, curid);
1982  } else {
1983    newpos = owl_view_get_nearest_to_saved(v);
1984  }
1985
1986  owl_global_set_curmsg(&g, newpos);
1987
1988  owl_function_calculate_topmsg(OWL_DIRECTION_DOWNWARDS);
1989  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
1990  owl_global_set_direction_downwards(&g);
1991}
1992
1993void owl_function_create_filter(int argc, char **argv)
1994{
1995  owl_filter *f;
1996  owl_view *v;
1997  int ret, inuse=0;
1998
1999  if (argc < 2) {
2000    owl_function_makemsg("Wrong number of arguments to filter command");
2001    return;
2002  }
2003
2004  v=owl_global_get_current_view(&g);
2005
2006  /* don't touch the all filter */
2007  if (!strcmp(argv[1], "all")) {
2008    owl_function_makemsg("You may not change the 'all' filter.");
2009    return;
2010  }
2011
2012  /* deal with the case of trying change the filter color */
2013  if (argc==4 && !strcmp(argv[2], "-c")) {
2014    f=owl_global_get_filter(&g, argv[1]);
2015    if (!f) {
2016      owl_function_makemsg("The filter '%s' does not exist.", argv[1]);
2017      return;
2018    }
2019    owl_filter_set_color(f, owl_util_string_to_color(argv[3]));
2020    owl_global_set_needrefresh(&g);
2021    owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2022    return;
2023  }
2024
2025  /* create the filter and check for errors */
2026  f=owl_malloc(sizeof(owl_filter));
2027  ret=owl_filter_init(f, argv[1], argc-2, argv+2);
2028  if (ret==-1) {
2029    owl_free(f);
2030    owl_function_makemsg("Invalid filter syntax");
2031    return;
2032  }
2033
2034  /* if the named filter is in use by the current view, remember it */
2035  if (!strcmp(owl_view_get_filtname(v), argv[1])) {
2036    inuse=1;
2037  }
2038
2039  /* if the named filter already exists, nuke it */
2040  if (owl_global_get_filter(&g, argv[1])) {
2041    owl_global_remove_filter(&g, argv[1]);
2042  }
2043
2044  /* add the filter */
2045  owl_global_add_filter(&g, f);
2046
2047  /* if it was in use by the current view then update */
2048  if (inuse) {
2049    owl_function_change_view(argv[1]);
2050  }
2051  owl_global_set_needrefresh(&g);
2052  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2053}
2054
2055void owl_function_show_filters()
2056{
2057  owl_list *l;
2058  owl_filter *f;
2059  int i, j;
2060  owl_fmtext fm;
2061
2062  owl_fmtext_init_null(&fm);
2063
2064  l=owl_global_get_filterlist(&g);
2065  j=owl_list_get_size(l);
2066
2067  owl_fmtext_append_bold(&fm, "Filters:\n");
2068
2069  for (i=0; i<j; i++) {
2070    f=owl_list_get_element(l, i);
2071    owl_fmtext_append_normal(&fm, "   ");
2072    if (owl_global_get_hascolors(&g)) {
2073      owl_fmtext_append_normal_color(&fm, owl_filter_get_name(f), owl_filter_get_color(f));
2074    } else {
2075      owl_fmtext_append_normal(&fm, owl_filter_get_name(f));
2076    }
2077    owl_fmtext_append_normal(&fm, "\n");
2078  }
2079  owl_function_popless_fmtext(&fm);
2080  owl_fmtext_free(&fm);
2081}
2082
2083void owl_function_show_filter(char *name)
2084{
2085  owl_filter *f;
2086  char buff[5000];
2087
2088  f=owl_global_get_filter(&g, name);
2089  if (!f) {
2090    owl_function_makemsg("There is no filter with that name");
2091    return;
2092  }
2093  owl_filter_print(f, buff);
2094  owl_function_popless_text(buff);
2095}
2096
2097void owl_function_show_zpunts()
2098{
2099  owl_filter *f;
2100  owl_list *fl;
2101  char buff[5000];
2102  owl_fmtext fm;
2103  int i, j;
2104
2105  owl_fmtext_init_null(&fm);
2106
2107  fl=owl_global_get_puntlist(&g);
2108  j=owl_list_get_size(fl);
2109  owl_fmtext_append_bold(&fm, "Active zpunt filters:\n");
2110
2111  for (i=0; i<j; i++) {
2112    f=owl_list_get_element(fl, i);
2113    owl_filter_print(f, buff);
2114    owl_fmtext_append_normal(&fm, buff);
2115  }
2116  owl_function_popless_fmtext(&fm);
2117  owl_fmtext_free(&fm);
2118}
2119
2120char *owl_function_fastclassinstfilt(char *class, char *instance) 
2121{
2122  /* creates a filter for a class, instance if one doesn't exist.
2123   * If instance is null then apply for all messgaes in the class.
2124   * returns the name of the filter, which the caller must free.*/
2125  owl_list *fl;
2126  owl_filter *f;
2127  char *argbuff, *filtname;
2128  char *tmpclass, *tmpinstance = NULL;
2129  int len;
2130
2131  fl=owl_global_get_filterlist(&g);
2132
2133  /* name for the filter */
2134  len=strlen(class)+30;
2135  if (instance) len+=strlen(instance);
2136  filtname=owl_malloc(len);
2137  if (!instance) {
2138    sprintf(filtname, "class-%s", class);
2139  } else {
2140    sprintf(filtname, "class-%s-instance-%s", class, instance);
2141  }
2142  /* downcase it */
2143  downstr(filtname);
2144  /* turn spaces into hyphens */
2145  owl_util_tr(filtname, ' ', '.');
2146 
2147  /* if it already exists then go with it.  This lets users override */
2148  if (owl_global_get_filter(&g, filtname)) {
2149    return(filtname);
2150  }
2151
2152  /* create the new filter */
2153  argbuff=owl_malloc(len+20);
2154  tmpclass=owl_strdup(class);
2155  owl_util_tr(tmpclass, ' ', '.');
2156  if (instance) {
2157    tmpinstance=owl_strdup(instance);
2158    owl_util_tr(tmpinstance, ' ', '.');
2159  }
2160  sprintf(argbuff, "( class ^%s$ )", tmpclass);
2161  if (tmpinstance) {
2162    sprintf(argbuff, "%s and ( instance ^%s$ )", argbuff, tmpinstance);
2163  }
2164  owl_free(tmpclass);
2165  if (tmpinstance) owl_free(tmpinstance);
2166
2167  f=owl_malloc(sizeof(owl_filter));
2168  owl_filter_init_fromstring(f, filtname, argbuff);
2169
2170  /* add it to the global list */
2171  owl_global_add_filter(&g, f);
2172
2173  owl_free(argbuff);
2174  return(filtname);
2175}
2176
2177char *owl_function_fastuserfilt(char *user)
2178{
2179  owl_filter *f;
2180  char *argbuff, *longuser, *shortuser, *filtname;
2181
2182  /* stick the local realm on if it's not there */
2183  longuser=long_zuser(user);
2184  shortuser=short_zuser(user);
2185
2186  /* name for the filter */
2187  filtname=owl_malloc(strlen(shortuser)+20);
2188  sprintf(filtname, "user-%s", shortuser);
2189
2190  /* if it already exists then go with it.  This lets users override */
2191  if (owl_global_get_filter(&g, filtname)) {
2192    return(filtname);
2193  }
2194
2195  /* create the new-internal filter */
2196  f=owl_malloc(sizeof(owl_filter));
2197
2198  argbuff=owl_malloc(strlen(longuser)+1000);
2199  sprintf(argbuff, "( type ^zephyr$ and ( class ^message$ and instance ^personal$ and ");
2200  sprintf(argbuff, "%s ( ( direction ^in$ and sender ^%s$ ) or ( direction ^out$ and recipient ^%s$ ) ) )", argbuff, longuser, longuser);
2201  sprintf(argbuff, "%s or ( ( class ^login$ ) and ( sender ^%s$ ) ) )", argbuff, longuser);
2202
2203  owl_filter_init_fromstring(f, filtname, argbuff);
2204
2205  /* add it to the global list */
2206  owl_global_add_filter(&g, f);
2207
2208  /* free stuff */
2209  owl_free(argbuff);
2210  owl_free(longuser);
2211  owl_free(shortuser);
2212
2213  return(filtname);
2214}
2215
2216char *owl_function_fasttypefilt(char *type)
2217{
2218  owl_filter *f;
2219  char *argbuff, *filtname;
2220
2221  /* name for the filter */
2222  filtname=owl_sprintf("type-%s", type);
2223
2224  /* if it already exists then go with it.  This lets users override */
2225  if (owl_global_get_filter(&g, filtname)) {
2226    return filtname;
2227  }
2228
2229  /* create the new-internal filter */
2230  f=owl_malloc(sizeof(owl_filter));
2231
2232  argbuff = owl_sprintf("type ^%s$", type);
2233
2234  owl_filter_init_fromstring(f, filtname, argbuff);
2235
2236  /* add it to the global list */
2237  owl_global_add_filter(&g, f);
2238
2239  /* free stuff */
2240  owl_free(argbuff);
2241
2242  return filtname;
2243}
2244
2245/* If flag is 1, marks for deletion.  If flag is 0,
2246 * unmarks for deletion. */
2247void owl_function_delete_curview_msgs(int flag)
2248{
2249  owl_view *v;
2250  int i, j;
2251
2252  v=owl_global_get_current_view(&g);
2253  j=owl_view_get_size(v);
2254  for (i=0; i<j; i++) {
2255    if (flag == 1) {
2256      owl_message_mark_delete(owl_view_get_element(v, i));
2257    } else if (flag == 0) {
2258      owl_message_unmark_delete(owl_view_get_element(v, i));
2259    }
2260  }
2261
2262  owl_function_makemsg("%i messages marked for %sdeletion", j, flag?"":"un");
2263
2264  owl_mainwin_redisplay(owl_global_get_mainwin(&g)); 
2265}
2266
2267char *owl_function_smartfilter(int type)
2268{
2269  /* Returns the name of a filter, or null.  The caller
2270   * must free this name.  */
2271  /* if the curmsg is a personal message return a filter name
2272   *    to the converstaion with that user.
2273   * If the curmsg is a class message, instance foo, recip *
2274   *    message, return a filter name to the class, inst.
2275   * If the curmsg is a class message and type==0 then
2276   *    return a filter name for just the class.
2277   * If the curmsg is a class message and type==1 then
2278   *    return a filter name for the class and instance.
2279   */
2280  owl_view *v;
2281  owl_message *m;
2282  char *zperson, *filtname=NULL;
2283 
2284  v=owl_global_get_current_view(&g);
2285  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
2286
2287  if (!m || owl_view_get_size(v)==0) {
2288    owl_function_makemsg("No message selected\n");
2289    return(NULL);
2290  }
2291
2292  /* very simple handling of admin messages for now */
2293  if (owl_message_is_type_admin(m)) {
2294    return(owl_function_fasttypefilt("admin"));
2295  }
2296
2297  /* narrow personal and login messages to the sender or recip as appropriate */
2298  if (owl_message_is_personal(m) || owl_message_is_login(m)) {
2299    if (owl_message_is_type_zephyr(m)) {
2300      if (owl_message_is_direction_in(m)) {
2301        zperson=short_zuser(owl_message_get_sender(m));
2302      } else {
2303        zperson=short_zuser(owl_message_get_recipient(m));
2304      }
2305      filtname=owl_function_fastuserfilt(zperson);
2306      owl_free(zperson);
2307      return(filtname);
2308    }
2309    return(NULL);
2310  }
2311
2312  /* narrow class MESSAGE, instance foo, recip * messages to class, inst */
2313  if (!strcasecmp(owl_message_get_class(m), "message") &&
2314      !owl_message_is_personal(m)) {
2315    filtname=owl_function_fastclassinstfilt(owl_message_get_class(m), owl_message_get_instance(m));
2316    return(filtname);
2317  }
2318
2319  /* otherwise narrow to the class */
2320  if (type==0) {
2321    filtname=owl_function_fastclassinstfilt(owl_message_get_class(m), NULL);
2322  } else if (type==1) {
2323    filtname=owl_function_fastclassinstfilt(owl_message_get_class(m), owl_message_get_instance(m));
2324  }
2325  return(filtname);
2326}
2327
2328void owl_function_smartzpunt(int type)
2329{
2330  /* Starts a zpunt command based on the current class,instance pair.
2331   * If type=0, uses just class.  If type=1, uses instance as well. */
2332  owl_view *v;
2333  owl_message *m;
2334  char *cmd, *cmdprefix, *mclass, *minst;
2335 
2336  v=owl_global_get_current_view(&g);
2337  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
2338
2339  if (!m || owl_view_get_size(v)==0) {
2340    owl_function_makemsg("No message selected\n");
2341    return;
2342  }
2343
2344  /* for now we skip admin messages. */
2345  if (owl_message_is_type_admin(m)
2346      || owl_message_is_login(m)
2347      || !owl_message_is_type_zephyr(m)) {
2348    owl_function_makemsg("smartzpunt doesn't support this message type.");
2349    return;
2350  }
2351
2352  mclass = owl_message_get_class(m);
2353  minst = owl_message_get_instance(m);
2354  if (!mclass || !*mclass || *mclass==' '
2355      || (!strcasecmp(mclass, "message") && !strcasecmp(minst, "personal"))
2356      || (type && (!minst || !*minst|| *minst==' '))) {
2357    owl_function_makemsg("smartzpunt can't safely do this for <%s,%s>",
2358                         mclass, minst);
2359  } else {
2360    cmdprefix = "start-command zpunt ";
2361    cmd = owl_malloc(strlen(cmdprefix)+strlen(mclass)+strlen(minst)+3);
2362    strcpy(cmd, cmdprefix);
2363    strcat(cmd, mclass);
2364    if (type) {
2365      strcat(cmd, " ");
2366      strcat(cmd, minst);
2367    } else {
2368      strcat(cmd, " *");
2369    }
2370    owl_function_command(cmd);
2371    owl_free(cmd);
2372  }
2373}
2374
2375
2376
2377void owl_function_color_current_filter(char *color)
2378{
2379  owl_filter *f;
2380  char *name;
2381
2382  name=owl_view_get_filtname(owl_global_get_current_view(&g));
2383  f=owl_global_get_filter(&g, name);
2384  if (!f) {
2385    owl_function_makemsg("Unknown filter");
2386    return;
2387  }
2388
2389  /* don't touch the all filter */
2390  if (!strcmp(name, "all")) {
2391    owl_function_makemsg("You may not change the 'all' filter.");
2392    return;
2393  }
2394
2395  /* deal with the case of trying change the filter color */
2396  owl_filter_set_color(f, owl_util_string_to_color(color));
2397  owl_global_set_needrefresh(&g);
2398  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2399}
2400
2401void owl_function_show_colors()
2402{
2403  owl_fmtext fm;
2404
2405  owl_fmtext_init_null(&fm);
2406  owl_fmtext_append_normal_color(&fm, "default\n", OWL_COLOR_DEFAULT);
2407  owl_fmtext_append_normal_color(&fm, "red\n", OWL_COLOR_RED);
2408  owl_fmtext_append_normal_color(&fm, "green\n", OWL_COLOR_GREEN);
2409  owl_fmtext_append_normal_color(&fm, "yellow\n", OWL_COLOR_YELLOW);
2410  owl_fmtext_append_normal_color(&fm, "blue\n", OWL_COLOR_BLUE);
2411  owl_fmtext_append_normal_color(&fm, "magenta\n", OWL_COLOR_MAGENTA);
2412  owl_fmtext_append_normal_color(&fm, "cyan\n", OWL_COLOR_CYAN);
2413  owl_fmtext_append_normal_color(&fm, "white\n", OWL_COLOR_WHITE);
2414
2415  owl_function_popless_fmtext(&fm);
2416  owl_fmtext_free(&fm);
2417}
2418
2419void owl_function_zpunt(char *class, char *inst, char *recip, int direction)
2420{
2421  /* add the given class, inst, recip to the punt list for filtering.
2422   *   if direction==0 then punt
2423   *   if direction==1 then unpunt */
2424  owl_filter *f;
2425  owl_list *fl;
2426  char *buff;
2427  int ret, i, j;
2428
2429  fl=owl_global_get_puntlist(&g);
2430
2431  /* first, create the filter */
2432  f=malloc(sizeof(owl_filter));
2433  buff=malloc(strlen(class)+strlen(inst)+strlen(recip)+100);
2434  if (!strcmp(recip, "*")) {
2435    sprintf(buff, "class ^%s$ and instance ^%s$", class, inst);
2436  } else {
2437    sprintf(buff, "class ^%s$ and instance ^%s$ and recipient %s", class, inst, recip);
2438  }
2439  owl_function_debugmsg("About to filter %s", buff);
2440  ret=owl_filter_init_fromstring(f, "punt-filter", buff);
2441  owl_free(buff);
2442  if (ret) {
2443    owl_function_makemsg("Error creating filter for zpunt");
2444    owl_filter_free(f);
2445    return;
2446  }
2447
2448  /* Check for an identical filter */
2449  j=owl_list_get_size(fl);
2450  for (i=0; i<j; i++) {
2451    if (owl_filter_equiv(f, owl_list_get_element(fl, i))) {
2452      /* if we're punting, then just silently bow out on this duplicate */
2453      if (direction==0) {
2454        owl_filter_free(f);
2455        return;
2456      }
2457
2458      /* if we're unpunting, then remove this filter from the puntlist */
2459      if (direction==1) {
2460        owl_filter_free(owl_list_get_element(fl, i));
2461        owl_list_remove_element(fl, i);
2462        return;
2463      }
2464    }
2465  }
2466
2467  /* If we're punting, add the filter to the global punt list */
2468  if (direction==0) {
2469    owl_list_append_element(fl, f);
2470  }
2471}
2472
2473void owl_function_activate_keymap(char *keymap)
2474{
2475  if (!owl_keyhandler_activate(owl_global_get_keyhandler(&g), keymap)) {
2476    owl_function_makemsg("Unable to activate keymap '%s'", keymap);
2477  }
2478}
2479
2480
2481void owl_function_show_keymaps()
2482{
2483  owl_list l;
2484  owl_fmtext fm;
2485  owl_keymap *km;
2486  owl_keyhandler *kh;
2487  int i, numkm;
2488  char *kmname;
2489
2490  kh = owl_global_get_keyhandler(&g);
2491  owl_fmtext_init_null(&fm);
2492  owl_fmtext_append_bold(&fm, "Keymaps:   ");
2493  owl_fmtext_append_normal(&fm, "(use 'show keymap <name>' for details)\n");
2494  owl_keyhandler_get_keymap_names(kh, &l);
2495  owl_fmtext_append_list(&fm, &l, "\n", owl_function_keymap_summary);
2496  owl_fmtext_append_normal(&fm, "\n");
2497
2498  numkm = owl_list_get_size(&l);
2499  for (i=0; i<numkm; i++) {
2500    kmname = owl_list_get_element(&l, i);
2501    km = owl_keyhandler_get_keymap(kh, kmname);
2502    owl_fmtext_append_bold(&fm, "\n\n----------------------------------------------------------------------------------------------------\n\n");
2503    owl_keymap_get_details(km, &fm);   
2504  }
2505  owl_fmtext_append_normal(&fm, "\n");
2506 
2507  owl_function_popless_fmtext(&fm);
2508  owl_keyhandler_keymap_namelist_free(&l);
2509  owl_fmtext_free(&fm);
2510}
2511
2512char *owl_function_keymap_summary(void *name)
2513{
2514  owl_keymap *km
2515    = owl_keyhandler_get_keymap(owl_global_get_keyhandler(&g), name);
2516  if (km) return owl_keymap_summary(km);
2517  else return(NULL);
2518}
2519
2520/* TODO: implement for real */
2521void owl_function_show_keymap(char *name)
2522{
2523  owl_fmtext fm;
2524  owl_keymap *km;
2525
2526  owl_fmtext_init_null(&fm);
2527  km = owl_keyhandler_get_keymap(owl_global_get_keyhandler(&g), name);
2528  if (km) {
2529    owl_keymap_get_details(km, &fm);
2530  } else {
2531    owl_fmtext_append_normal(&fm, "No such keymap...\n");
2532  } 
2533  owl_function_popless_fmtext(&fm);
2534  owl_fmtext_free(&fm);
2535}
2536
2537void owl_function_help_for_command(char *cmdname)
2538{
2539  owl_fmtext fm;
2540
2541  owl_fmtext_init_null(&fm);
2542  owl_cmd_get_help(owl_global_get_cmddict(&g), cmdname, &fm);
2543  owl_function_popless_fmtext(&fm); 
2544  owl_fmtext_free(&fm);
2545}
2546
2547void owl_function_search_start(char *string, int direction)
2548{
2549  /* direction is OWL_DIRECTION_DOWNWARDS or OWL_DIRECTION_UPWARDS */
2550  owl_global_set_search_active(&g, string);
2551  owl_function_search_helper(0, direction);
2552}
2553
2554void owl_function_search_continue(int direction)
2555{
2556  /* direction is OWL_DIRECTION_DOWNWARDS or OWL_DIRECTION_UPWARDS */
2557  owl_function_search_helper(1, direction);
2558}
2559
2560void owl_function_search_helper(int mode, int direction)
2561{
2562  /* move to a message that contains the string.  If direction is
2563   * OWL_DIRECTION_DOWNWARDS then search fowards, if direction is
2564   * OWL_DIRECTION_UPWARDS then search backwards.
2565   *
2566   * If mode==0 then it will stay on the current message if it
2567   * contains the string.
2568   */
2569
2570  owl_view *v;
2571  int viewsize, i, curmsg, start;
2572  owl_message *m;
2573
2574  v=owl_global_get_current_view(&g);
2575  viewsize=owl_view_get_size(v);
2576  curmsg=owl_global_get_curmsg(&g);
2577 
2578  if (viewsize==0) {
2579    owl_function_makemsg("No messages present");
2580    return;
2581  }
2582
2583  if (mode==0) {
2584    start=curmsg;
2585  } else if (direction==OWL_DIRECTION_DOWNWARDS) {
2586    start=curmsg+1;
2587  } else {
2588    start=curmsg-1;
2589  }
2590
2591  /* bounds check */
2592  if (start>=viewsize || start<0) {
2593    owl_function_makemsg("No further matches found");
2594    return;
2595  }
2596
2597  for (i=start; i<viewsize && i>=0;) {
2598    m=owl_view_get_element(v, i);
2599    if (owl_message_search(m, owl_global_get_search_string(&g))) {
2600      owl_global_set_curmsg(&g, i);
2601      owl_function_calculate_topmsg(direction);
2602      owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2603      if (direction==OWL_DIRECTION_DOWNWARDS) {
2604        owl_global_set_direction_downwards(&g);
2605      } else {
2606        owl_global_set_direction_upwards(&g);
2607      }
2608      return;
2609    }
2610    if (direction==OWL_DIRECTION_DOWNWARDS) {
2611      i++;
2612    } else {
2613      i--;
2614    }
2615  }
2616  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2617  owl_function_makemsg("No matches found");
2618}
2619
2620
2621/* strips formatting from ztext and returns the unformatted text.
2622 * caller is responsible for freeing. */
2623char *owl_function_ztext_stylestrip(char *zt)
2624{
2625  owl_fmtext fm;
2626  char *plaintext;
2627
2628  owl_fmtext_init_null(&fm);
2629  owl_fmtext_append_ztext(&fm, zt);
2630  plaintext = owl_fmtext_print_plain(&fm);
2631  owl_fmtext_free(&fm);
2632  return(plaintext);
2633}
2634
2635/* popup a znol listing.  If file is NULL use the default .anyone */
2636/* this doesn't obey 'elapsed' or 'timesort' yet */
2637void owl_function_zlist(char *file, int elapsed, int timesort)
2638{
2639  char *ourfile, *tmp, buff[LINE], *line;
2640  FILE *f;
2641  int numlocs, ret, i;
2642  ZLocations_t location[200];
2643  owl_fmtext fm;
2644
2645  if (file==NULL) {
2646    tmp=owl_global_get_homedir(&g);
2647    if (!tmp) {
2648      owl_function_makemsg("Could not determine home directory");
2649      return;
2650    }
2651    ourfile=owl_malloc(strlen(tmp)+50);
2652    sprintf(ourfile, "%s/.anyone", owl_global_get_homedir(&g));
2653  } else {
2654    ourfile=owl_strdup(file);
2655  }
2656
2657  f=fopen(ourfile, "r");
2658  if (!f) {
2659    owl_function_makemsg("Error opening file %s: %s",
2660                         ourfile,
2661                         strerror(errno) ? strerror(errno) : "");
2662    return;
2663  }
2664
2665  owl_fmtext_init_null(&fm);
2666   
2667  while (fgets(buff, LINE, f)!=NULL) {
2668    /* ignore comments, blank lines etc. */
2669    if (buff[0]=='#') continue;
2670    if (buff[0]=='\n') continue;
2671    if (buff[0]=='\0') continue;
2672
2673    /* strip the \n */
2674    buff[strlen(buff)-1]='\0';
2675
2676    /* ingore from # on */
2677    tmp=strchr(buff, '#');
2678    if (tmp) tmp[0]='\0';
2679
2680    /* ingore from SPC */
2681    tmp=strchr(buff, ' ');
2682    if (tmp) tmp[0]='\0';
2683
2684    /* stick on the local realm. */
2685    if (!strchr(buff, '@')) {
2686      strcat(buff, "@");
2687      strcat(buff, ZGetRealm());
2688    }
2689
2690    ret=ZLocateUser(buff, &numlocs, ZAUTH);
2691    if (ret!=ZERR_NONE) {
2692      owl_function_makemsg("Error getting location for %s", buff);
2693      continue;
2694    }
2695
2696    numlocs=200;
2697    ret=ZGetLocations(location, &numlocs);
2698    if (ret==0) {
2699      for (i=0; i<numlocs; i++) {
2700        line=malloc(strlen(location[i].host)+strlen(location[i].time)+strlen(location[i].tty)+100);
2701        tmp=short_zuser(buff);
2702        sprintf(line, "%-10.10s %-24.24s %-12.12s  %20.20s\n",
2703                tmp,
2704                location[i].host,
2705                location[i].tty,
2706                location[i].time);
2707        owl_fmtext_append_normal(&fm, line);
2708        owl_free(tmp);
2709      }
2710      if (numlocs>=200) {
2711        owl_fmtext_append_normal(&fm, "Too many locations found for this user, truncating.\n");
2712      }
2713    }
2714  }
2715  fclose(f);
2716
2717  owl_function_popless_fmtext(&fm);
2718  owl_fmtext_free(&fm);
2719
2720  owl_free(ourfile);
2721}
2722
2723void owl_function_dump(char *filename) 
2724{
2725  int i, j, count;
2726  owl_message *m;
2727  owl_view *v;
2728  FILE *file;
2729  /* struct stat sbuf; */
2730
2731  v=owl_global_get_current_view(&g);
2732
2733  /* in the future make it ask yes/no */
2734  /*
2735  ret=stat(filename, &sbuf);
2736  if (!ret) {
2737    ret=owl_function_askyesno("File exists, continue? [Y/n]");
2738    if (!ret) return;
2739  }
2740  */
2741
2742  file=fopen(filename, "w");
2743  if (!file) {
2744    owl_function_makemsg("Error opening file");
2745    return;
2746  }
2747
2748  count=0;
2749  j=owl_view_get_size(v);
2750  for (i=0; i<j; i++) {
2751    m=owl_view_get_element(v, i);
2752    fputs(owl_message_get_text(m), file);
2753  }
2754  fclose(file);
2755}
2756
2757
2758
2759void owl_function_do_newmsgproc(void)
2760{
2761  if (owl_global_get_newmsgproc(&g) && strcmp(owl_global_get_newmsgproc(&g), "")) {
2762    /* if there's a process out there, we need to check on it */
2763    if (owl_global_get_newmsgproc_pid(&g)) {
2764      owl_function_debugmsg("Checking on newmsgproc pid==%i", owl_global_get_newmsgproc_pid(&g));
2765      owl_function_debugmsg("Waitpid return is %i", waitpid(owl_global_get_newmsgproc_pid(&g), NULL, WNOHANG));
2766      waitpid(owl_global_get_newmsgproc_pid(&g), NULL, WNOHANG);
2767      if (waitpid(owl_global_get_newmsgproc_pid(&g), NULL, WNOHANG)==-1) {
2768        /* it exited */
2769        owl_global_set_newmsgproc_pid(&g, 0);
2770        owl_function_debugmsg("newmsgproc exited");
2771      } else {
2772        owl_function_debugmsg("newmsgproc did not exit");
2773      }
2774    }
2775   
2776    /* if it exited, fork & exec a new one */
2777    if (owl_global_get_newmsgproc_pid(&g)==0) {
2778      int i, myargc;
2779      i=fork();
2780      if (i) {
2781        /* parent set the child's pid */
2782        owl_global_set_newmsgproc_pid(&g, i);
2783        owl_function_debugmsg("I'm the parent and I started a new newmsgproc with pid %i", i);
2784      } else {
2785        /* child exec's the program */
2786        char **parsed;
2787        parsed=owl_parseline(owl_global_get_newmsgproc(&g), &myargc);
2788        if (myargc < 0) {
2789          owl_function_debugmsg("Could not parse newmsgproc '%s': unbalanced quotes?", owl_global_get_newmsgproc(&g));
2790        }
2791        if (myargc <= 0) {
2792          _exit(127);
2793        }
2794        parsed=realloc(parsed, sizeof(*parsed) * (myargc+1));
2795        parsed[myargc] = NULL;
2796       
2797        owl_function_debugmsg("About to exec \"%s\" with %d arguments", parsed[0], myargc);
2798       
2799        execvp(parsed[0], parsed);
2800       
2801       
2802        /* was there an error exec'ing? */
2803        owl_function_debugmsg("Cannot run newmsgproc '%s': cannot exec '%s': %s", 
2804                              owl_global_get_newmsgproc(&g), parsed[0], strerror(errno));
2805        _exit(127);
2806      }
2807    }
2808  }
2809}
2810
2811void owl_function_xterm_raise(void)
2812{
2813  printf("\033[5t");
2814}
2815
2816void owl_function_xterm_deiconify(void)
2817{
2818  printf("\033[1t");
2819}
Note: See TracBrowser for help on using the repository browser.