source: functions.c @ df0d93a

barnowl_perlaimdebianowlrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since df0d93a was df0d93a, checked in by James M. Kretchmar <kretch@mit.edu>, 21 years ago
cleaned up the info funciton, put back additional data for outgoing messages
  • Property mode set to 100644
File size: 72.5 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
1228  owl_fmtext_append_normal(&fm, "Type      : ");
1229  owl_fmtext_append_bold(&fm, owl_message_type_to_string(m));
1230  owl_fmtext_append_normal(&fm, "\n");
1231
1232  if (owl_message_is_direction_in(m)) {
1233    owl_fmtext_append_normal(&fm, "Direction : in\n");
1234  } else if (owl_message_is_direction_out(m)) {
1235    owl_fmtext_append_normal(&fm, "Direction : out\n");
1236  } else if (owl_message_is_direction_none(m)) {
1237    owl_fmtext_append_normal(&fm, "Direction : none\n");
1238  } else {
1239    owl_fmtext_append_normal(&fm, "Direction : unknown\n");
1240  }
1241
1242  owl_fmtext_append_normal(&fm, "Time      : ");
1243  owl_fmtext_append_normal(&fm, owl_message_get_timestr(m));
1244  owl_fmtext_append_normal(&fm, "\n");
1245
1246  if (!owl_message_is_type_admin(m)) {
1247    owl_fmtext_append_normal(&fm, "Sender    : ");
1248    owl_fmtext_append_normal(&fm, owl_message_get_sender(m));
1249    owl_fmtext_append_normal(&fm, "\n");
1250   
1251    owl_fmtext_append_normal(&fm, "Recipient : ");
1252    owl_fmtext_append_normal(&fm, owl_message_get_recipient(m));
1253    owl_fmtext_append_normal(&fm, "\n");
1254  }
1255   
1256  if (owl_message_is_type_zephyr(m)) {
1257    owl_fmtext_append_bold(&fm, "  Zephyr Specific Info\n");
1258   
1259    owl_fmtext_append_normal(&fm, "Class     : ");
1260    owl_fmtext_append_normal(&fm, owl_message_get_class(m));
1261    owl_fmtext_append_normal(&fm, "\n");
1262    owl_fmtext_append_normal(&fm, "Instance  : ");
1263    owl_fmtext_append_normal(&fm, owl_message_get_instance(m));
1264    owl_fmtext_append_normal(&fm, "\n");
1265    owl_fmtext_append_normal(&fm, "Opcode    : ");
1266    owl_fmtext_append_normal(&fm, owl_message_get_opcode(m));
1267    owl_fmtext_append_normal(&fm, "\n");
1268   
1269    owl_fmtext_append_normal(&fm, "Time      : ");
1270    owl_fmtext_append_normal(&fm, owl_message_get_timestr(m));
1271    owl_fmtext_append_normal(&fm, "\n");
1272
1273    if (owl_message_is_direction_in(m)) {
1274      n=owl_message_get_notice(m);
1275
1276      owl_fmtext_append_normal(&fm, "Kind      : ");
1277      if (n->z_kind==UNSAFE) {
1278        owl_fmtext_append_normal(&fm, "UNSAFE\n");
1279      } else if (n->z_kind==UNACKED) {
1280        owl_fmtext_append_normal(&fm, "UNACKED\n");
1281      } else if (n->z_kind==ACKED) {
1282        owl_fmtext_append_normal(&fm, "ACKED\n");
1283      } else if (n->z_kind==HMACK) {
1284        owl_fmtext_append_normal(&fm, "HMACK\n");
1285      } else if (n->z_kind==HMCTL) {
1286        owl_fmtext_append_normal(&fm, "HMCTL\n");
1287      } else if (n->z_kind==SERVACK) {
1288        owl_fmtext_append_normal(&fm, "SERVACK\n");
1289      } else if (n->z_kind==SERVNAK) {
1290        owl_fmtext_append_normal(&fm, "SERVNACK\n");
1291      } else if (n->z_kind==CLIENTACK) {
1292        owl_fmtext_append_normal(&fm, "CLIENTACK\n");
1293      } else if (n->z_kind==STAT) {
1294        owl_fmtext_append_normal(&fm, "STAT\n");
1295      } else {
1296        owl_fmtext_append_normal(&fm, "ILLEGAL VALUE\n");
1297      }
1298      owl_fmtext_append_normal(&fm, "Host      : ");
1299      owl_fmtext_append_normal(&fm, owl_message_get_hostname(m));
1300      owl_fmtext_append_normal(&fm, "\n");
1301      sprintf(buff, "Port      : %i\n", n->z_port);
1302      owl_fmtext_append_normal(&fm, buff);
1303
1304      owl_fmtext_append_normal(&fm,    "Auth      : ");
1305      if (n->z_auth == ZAUTH_FAILED) {
1306        owl_fmtext_append_normal(&fm, "FAILED\n");
1307      } else if (n->z_auth == ZAUTH_NO) {
1308        owl_fmtext_append_normal(&fm, "NO\n");
1309      } else if (n->z_auth == ZAUTH_YES) {
1310        owl_fmtext_append_normal(&fm, "YES\n");
1311      } else {
1312        sprintf(buff, "Unknown State (%i)\n", n->z_auth);
1313        owl_fmtext_append_normal(&fm, buff);
1314      }
1315
1316      sprintf(buff, "Checkd Ath: %i\n", n->z_checked_auth);
1317      sprintf(buff, "%sMulti notc: %s\n", buff, n->z_multinotice);
1318      sprintf(buff, "%sNum other : %i\n", buff, n->z_num_other_fields);
1319      sprintf(buff, "%sMsg Len   : %i\n", buff, n->z_message_len);
1320      owl_fmtext_append_normal(&fm, buff);
1321     
1322      sprintf(buff, "Fields    : %i\n", owl_zephyr_get_num_fields(n));
1323      owl_fmtext_append_normal(&fm, buff);
1324     
1325      fields=owl_zephyr_get_num_fields(n);
1326      for (i=0; i<fields; i++) {
1327        sprintf(buff, "Field %i   : ", i+1);
1328       
1329        ptr=owl_zephyr_get_field(n, i+1, &len);
1330        if (!ptr) break;
1331        if (len<30) {
1332          strncpy(tmpbuff, ptr, len);
1333          tmpbuff[len]='\0';
1334        } else {
1335          strncpy(tmpbuff, ptr, 30);
1336          tmpbuff[30]='\0';
1337          strcat(tmpbuff, "...");
1338        }
1339       
1340        for (j=0; j<strlen(tmpbuff); j++) {
1341          if (tmpbuff[j]=='\n') tmpbuff[j]='~';
1342          if (tmpbuff[j]=='\r') tmpbuff[j]='!';
1343        }
1344       
1345        strcat(buff, tmpbuff);
1346        strcat(buff, "\n");
1347        owl_fmtext_append_normal(&fm, buff);
1348      }
1349      owl_fmtext_append_normal(&fm, "Default Fm:");
1350      owl_fmtext_append_normal(&fm, n->z_default_format);
1351    }
1352  }
1353
1354  if (owl_message_is_type_aim(m)) {
1355    owl_fmtext_append_bold(&fm, "  AIM Specific Info\n");   
1356  }
1357 
1358  owl_function_popless_fmtext(&fm);
1359}
1360
1361
1362void owl_function_curmsg_to_popwin()
1363{
1364  owl_popwin *pw;
1365  owl_view *v;
1366  owl_message *m;
1367
1368  v = owl_global_get_current_view(&g);
1369
1370  pw=owl_global_get_popwin(&g);
1371
1372  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
1373
1374  if (!m || owl_view_get_size(v)==0) {
1375    owl_function_makemsg("No current message");
1376    return;
1377  }
1378
1379  owl_function_popless_fmtext(owl_message_get_fmtext(m));
1380}
1381
1382
1383void owl_function_page_curmsg(int step)
1384{
1385  /* scroll down or up within the current message IF the message is truncated */
1386
1387  int offset, curmsg, lines;
1388  owl_view *v;
1389  owl_message *m;
1390
1391  offset=owl_global_get_curmsg_vert_offset(&g);
1392  v=owl_global_get_current_view(&g);
1393  curmsg=owl_global_get_curmsg(&g);
1394  m=owl_view_get_element(v, curmsg);
1395  if (!m || owl_view_get_size(v)==0) return;
1396  lines=owl_message_get_numlines(m);
1397
1398  if (offset==0) {
1399    /* Bail if the curmsg isn't the last one displayed */
1400    if (curmsg != owl_mainwin_get_last_msg(owl_global_get_mainwin(&g))) {
1401      owl_function_makemsg("The entire message is already displayed");
1402      return;
1403    }
1404   
1405    /* Bail if we're not truncated */
1406    if (!owl_mainwin_is_curmsg_truncated(owl_global_get_mainwin(&g))) {
1407      owl_function_makemsg("The entire message is already displayed");
1408      return;
1409    }
1410  }
1411 
1412 
1413  /* don't scroll past the last line */
1414  if (step>0) {
1415    if (offset+step > lines-1) {
1416      owl_global_set_curmsg_vert_offset(&g, lines-1);
1417    } else {
1418      owl_global_set_curmsg_vert_offset(&g, offset+step);
1419    }
1420  }
1421
1422  /* would we be before the beginning of the message? */
1423  if (step<0) {
1424    if (offset+step<0) {
1425      owl_global_set_curmsg_vert_offset(&g, 0);
1426    } else {
1427      owl_global_set_curmsg_vert_offset(&g, offset+step);
1428    }
1429  }
1430 
1431  /* redisplay */
1432  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
1433  owl_global_set_needrefresh(&g);
1434}
1435
1436void owl_function_resize_typwin(int newsize)
1437{
1438  owl_global_set_typwin_lines(&g, newsize);
1439  owl_function_resize();
1440}
1441
1442void owl_function_typwin_grow()
1443{
1444  int i;
1445
1446  i=owl_global_get_typwin_lines(&g);
1447  owl_function_resize_typwin(i+1);
1448}
1449
1450void owl_function_typwin_shrink()
1451{
1452  int i;
1453
1454  i=owl_global_get_typwin_lines(&g);
1455  if (i>2) {
1456    owl_function_resize_typwin(i-1);
1457  }
1458}
1459
1460void owl_function_mainwin_pagedown()
1461{
1462  int i;
1463
1464  i=owl_mainwin_get_last_msg(owl_global_get_mainwin(&g));
1465  if (i<0) return;
1466  if (owl_mainwin_is_last_msg_truncated(owl_global_get_mainwin(&g))
1467      && (owl_global_get_curmsg(&g) < i)
1468      && (i>0)) {
1469    i--;
1470  }
1471  owl_global_set_curmsg(&g, i);
1472  owl_function_nextmsg();
1473}
1474
1475void owl_function_mainwin_pageup()
1476{
1477  owl_global_set_curmsg(&g, owl_global_get_topmsg(&g));
1478  owl_function_prevmsg();
1479}
1480
1481void owl_function_getsubs()
1482{
1483  int ret, num, i, one;
1484  ZSubscription_t sub;
1485  char *buff, *tmpbuff;
1486
1487  one = 1;
1488
1489  ret=ZRetrieveSubscriptions(0, &num);
1490  if (ret == ZERR_TOOMANYSUBS) {
1491    owl_function_makemsg("Zephyr: too many subscriptions");
1492    return;
1493  }
1494
1495  buff=owl_malloc(num*500);
1496  tmpbuff=owl_malloc(num*500);
1497  strcpy(buff, "");
1498  for (i=0; i<num; i++) {
1499    if ((ret = ZGetSubscriptions(&sub, &one)) != ZERR_NONE) {
1500      owl_function_makemsg("Error while getting subscriptions");
1501      owl_free(buff);
1502      owl_free(tmpbuff);
1503      ZFlushSubscriptions();
1504      return;
1505    } else {
1506      sprintf(tmpbuff, "<%s,%s,%s>\n%s", sub.zsub_class, sub.zsub_classinst, sub.zsub_recipient, buff);
1507      strcpy(buff, tmpbuff);
1508    }
1509  }
1510
1511  owl_function_popless_text(buff);
1512  owl_free(buff);
1513  owl_free(tmpbuff);
1514  ZFlushSubscriptions();
1515}
1516
1517#define PABUFLEN 5000
1518void owl_function_printallvars()
1519{
1520  char buff[PABUFLEN], *pos, *name;
1521  owl_list varnames;
1522  int i, numvarnames, rem;
1523
1524  pos = buff;
1525  pos += sprintf(pos, "%-20s = %s\n", "VARIABLE", "VALUE");
1526  pos += sprintf(pos, "%-20s   %s\n",  "--------", "-----");
1527  owl_variable_dict_get_names(owl_global_get_vardict(&g), &varnames);
1528  rem = (buff+PABUFLEN)-pos-1;
1529  numvarnames = owl_list_get_size(&varnames);
1530  for (i=0; i<numvarnames; i++) {
1531    name = owl_list_get_element(&varnames, i);
1532    if (name && name[0]!='_') {
1533      rem = (buff+PABUFLEN)-pos-1;   
1534      pos += snprintf(pos, rem, "\n%-20s = ", name);
1535      rem = (buff+PABUFLEN)-pos-1;   
1536      owl_variable_get_tostring(owl_global_get_vardict(&g), name, pos, rem);
1537      pos = buff+strlen(buff);
1538    }
1539  }
1540  rem = (buff+PABUFLEN)-pos-1;   
1541  snprintf(pos, rem, "\n");
1542  owl_variable_dict_namelist_free(&varnames);
1543 
1544  owl_function_popless_text(buff);
1545}
1546
1547void owl_function_show_variables()
1548{
1549  owl_list varnames;
1550  owl_fmtext fm; 
1551  int i, numvarnames;
1552  char *varname;
1553
1554  owl_fmtext_init_null(&fm);
1555  owl_fmtext_append_bold(&fm, 
1556      "Variables: (use 'show variable <name>' for details)\n");
1557  owl_variable_dict_get_names(owl_global_get_vardict(&g), &varnames);
1558  numvarnames = owl_list_get_size(&varnames);
1559  for (i=0; i<numvarnames; i++) {
1560    varname = owl_list_get_element(&varnames, i);
1561    if (varname && varname[0]!='_') {
1562      owl_variable_describe(owl_global_get_vardict(&g), varname, &fm);
1563    }
1564  }
1565  owl_variable_dict_namelist_free(&varnames);
1566  owl_function_popless_fmtext(&fm);
1567  owl_fmtext_free(&fm);
1568}
1569
1570void owl_function_show_variable(char *name)
1571{
1572  owl_fmtext fm; 
1573
1574  owl_fmtext_init_null(&fm);
1575  owl_variable_get_help(owl_global_get_vardict(&g), name, &fm);
1576  owl_function_popless_fmtext(&fm);
1577  owl_fmtext_free(&fm); 
1578}
1579
1580/* note: this applies to global message list, not to view.
1581 * If flag is 1, deletes.  If flag is 0, undeletes. */
1582void owl_function_delete_by_id(int id, int flag)
1583{
1584  owl_messagelist *ml;
1585  owl_message *m;
1586  ml = owl_global_get_msglist(&g);
1587  m = owl_messagelist_get_by_id(ml, id);
1588  if (m) {
1589    if (flag == 1) {
1590      owl_message_mark_delete(m);
1591    } else if (flag == 0) {
1592      owl_message_unmark_delete(m);
1593    }
1594    owl_mainwin_redisplay(owl_global_get_mainwin(&g));
1595    owl_global_set_needrefresh(&g);
1596  } else {
1597    owl_function_makemsg("No message with id %d: unable to mark for (un)delete",id);
1598  }
1599}
1600
1601void owl_function_delete_automsgs()
1602{
1603  /* mark for deletion all messages in the current view that match the
1604   * 'trash' filter */
1605
1606  int i, j, count;
1607  owl_message *m;
1608  owl_view *v;
1609  owl_filter *f;
1610
1611  /* get the trash filter */
1612  f=owl_global_get_filter(&g, "trash");
1613  if (!f) {
1614    owl_function_makemsg("No trash filter defined");
1615    return;
1616  }
1617
1618  v=owl_global_get_current_view(&g);
1619
1620  count=0;
1621  j=owl_view_get_size(v);
1622  for (i=0; i<j; i++) {
1623    m=owl_view_get_element(v, i);
1624    if (owl_filter_message_match(f, m)) {
1625      count++;
1626      owl_message_mark_delete(m);
1627    }
1628  }
1629  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
1630  owl_function_makemsg("%i messages marked for deletion", count);
1631  owl_global_set_needrefresh(&g);
1632}
1633
1634
1635void owl_function_status()
1636{
1637  char buff[5000];
1638  time_t start;
1639  int up, days, hours, minutes;
1640
1641  start=owl_global_get_starttime(&g);
1642
1643  sprintf(buff, "Version: %s\n", OWL_VERSION_STRING);
1644  sprintf(buff, "%sScreen size: %i lines, %i columns\n", buff, owl_global_get_lines(&g), owl_global_get_cols(&g));
1645  sprintf(buff, "%sStartup Arugments: %s\n", buff, owl_global_get_startupargs(&g));
1646  sprintf(buff, "%sStartup Time: %s", buff, ctime(&start));
1647
1648  up=owl_global_get_runtime(&g);
1649  days=up/86400;
1650  up-=days*86400;
1651  hours=up/3600;
1652  up-=hours*3600;
1653  minutes=up/60;
1654  up-=minutes*60;
1655  sprintf(buff, "%sRun Time: %i days %2.2i:%2.2i:%2.2i\n", buff, days, hours, minutes, up);
1656
1657  if (owl_global_get_hascolors(&g)) {
1658    sprintf(buff, "%sColor: Yes, %i color pairs.\n", buff, owl_global_get_colorpairs(&g));
1659  } else {
1660    strcat(buff, "Color: No.\n");
1661  }
1662
1663  sprintf(buff, "%sMemory Malloced: %i\n", buff, owl_global_get_malloced(&g));
1664  sprintf(buff, "%sMemory Freed: %i\n", buff, owl_global_get_freed(&g));
1665  sprintf(buff, "%sMemory In Use: %i\n", buff, owl_global_get_meminuse(&g));
1666
1667  owl_function_popless_text(buff);
1668}
1669
1670void owl_function_show_term()
1671{
1672  owl_fmtext fm;
1673  char buff[LINE];
1674
1675  owl_fmtext_init_null(&fm);
1676  sprintf(buff, "Terminal Lines: %i\nTerminal Columns: %i\n",
1677          owl_global_get_lines(&g),
1678          owl_global_get_cols(&g));
1679  owl_fmtext_append_normal(&fm, buff);
1680
1681  if (owl_global_get_hascolors(&g)) {
1682    owl_fmtext_append_normal(&fm, "Color: Yes\n");
1683    sprintf(buff, "Number of color pairs: %i\n", owl_global_get_colorpairs(&g));
1684    owl_fmtext_append_normal(&fm, buff);
1685    sprintf(buff, "Can change colors: %s\n", can_change_color() ? "yes" : "no");
1686    owl_fmtext_append_normal(&fm, buff);
1687  } else {
1688    owl_fmtext_append_normal(&fm, "Color: No\n");
1689  }
1690
1691  owl_function_popless_fmtext(&fm);
1692  owl_fmtext_free(&fm);
1693}
1694
1695
1696void owl_function_reply(int type, int enter)
1697{
1698  /* if type = 0 then normal reply.
1699   * if type = 1 then it's a reply to sender
1700   * if enter = 0 then allow the command to be edited
1701   * if enter = 1 then don't wait for editing
1702   */
1703  char *buff, *oldbuff;
1704  owl_message *m;
1705  owl_filter *f;
1706 
1707  if (owl_view_get_size(owl_global_get_current_view(&g))==0) {
1708    owl_function_makemsg("No message selected");
1709  } else {
1710    char *class, *inst, *to, *cc=NULL;
1711   
1712    m=owl_view_get_element(owl_global_get_current_view(&g), owl_global_get_curmsg(&g));
1713    if (!m) {
1714      owl_function_makemsg("No message selected");
1715      return;
1716    }
1717
1718    /* first check if we catch the reply-lockout filter */
1719    f=owl_global_get_filter(&g, "reply-lockout");
1720    if (f) {
1721      if (owl_filter_message_match(f, m)) {
1722        owl_function_makemsg("Sorry, replies to this message have been disabled by the reply-lockout filter");
1723        return;
1724      }
1725    }
1726
1727    /* admin */
1728    if (owl_message_is_type_admin(m)) {
1729      owl_function_makemsg("You cannot reply to an admin message");
1730      return;
1731    }
1732
1733    /* zephyr */
1734    if (owl_message_is_type_zephyr(m)) {
1735      /* for now we disable replies to zcrypt messages, since we can't
1736         support an encrypted reply */
1737      if (!strcasecmp(owl_message_get_opcode(m), "crypt")) {
1738        owl_function_makemsg("Replies to zcrypt messages are not enabled in this release");
1739        return;
1740      }
1741
1742      if (owl_message_is_direction_out(m)) {
1743        owl_function_zwrite_setup(owl_message_get_zwriteline(m));
1744        owl_global_set_buffercommand(&g, owl_message_get_zwriteline(m));
1745        return;
1746      }
1747     
1748      if (owl_message_is_login(m)) {
1749        class="MESSAGE";
1750        inst="PERSONAL";
1751        to=owl_message_get_sender(m);
1752      } else if (type==1) {
1753        class="MESSAGE";
1754        inst="PERSONAL";
1755        to=owl_message_get_sender(m);
1756      } else {
1757        class=owl_message_get_class(m);
1758        inst=owl_message_get_instance(m);
1759        to=owl_message_get_recipient(m);
1760        cc=owl_message_get_cc(m);
1761        if (!strcmp(to, "") || !strcmp(to, "*")) {
1762          to="";
1763        } else if (to[0]=='@') {
1764          /* leave it, to get the realm */
1765        } else {
1766          to=owl_message_get_sender(m);
1767        }
1768      }
1769       
1770      /* create the command line */
1771      buff = owl_strdup("zwrite");
1772      if (strcasecmp(class, "message")) {
1773        buff = owl_sprintf("%s -c %s%s%s", oldbuff=buff, owl_getquoting(class), class, owl_getquoting(class));
1774        owl_free(oldbuff);
1775      }
1776      if (strcasecmp(inst, "personal")) {
1777        buff = owl_sprintf("%s -i %s%s%s", oldbuff=buff, owl_getquoting(inst), inst, owl_getquoting(inst));
1778        owl_free(oldbuff);
1779      }
1780      if (*to != '\0') {
1781        char *tmp, *oldtmp, *tmp2;
1782        tmp=short_zuser(to);
1783        if (cc) {
1784          tmp = owl_util_uniq(oldtmp=tmp, cc, "-");
1785          owl_free(oldtmp);
1786          buff = owl_sprintf("%s -C %s", oldbuff=buff, tmp);
1787          owl_free(oldbuff);
1788        } else {
1789          if (owl_global_is_smartstrip(&g)) {
1790            tmp2=tmp;
1791            tmp=owl_util_smartstripped_user(tmp2);
1792            owl_free(tmp2);
1793          }
1794          buff = owl_sprintf("%s %s", oldbuff=buff, tmp);
1795          owl_free(oldbuff);
1796        }
1797        owl_free(tmp);
1798      }
1799      if (cc) owl_free(cc);
1800    }
1801
1802    /* aim */
1803    if (owl_message_is_type_aim(m)) {
1804      if (owl_message_is_direction_out(m)) {
1805        buff=owl_sprintf("aimwrite %s", owl_message_get_recipient(m));
1806      } else {
1807        buff=owl_sprintf("aimwrite %s", owl_message_get_sender(m));
1808      }
1809    }
1810   
1811    if (enter) {
1812      owl_history *hist = owl_global_get_cmd_history(&g);
1813      owl_history_store(hist, buff);
1814      owl_history_reset(hist);
1815      owl_function_command_norv(buff);
1816    } else {
1817      owl_function_start_command(buff);
1818    }
1819    owl_free(buff);
1820  }
1821}
1822
1823void owl_function_zlocate(int argc, char **argv, int auth)
1824{
1825  owl_fmtext fm;
1826  char *ptr, buff[LINE];
1827  int i;
1828
1829  owl_fmtext_init_null(&fm);
1830
1831  for (i=0; i<argc; i++) {
1832    ptr=long_zuser(argv[i]);
1833    owl_zephyr_zlocate(ptr, buff, auth);
1834    owl_fmtext_append_normal(&fm, buff);
1835    owl_free(ptr);
1836  }
1837
1838  owl_function_popless_fmtext(&fm);
1839  owl_fmtext_free(&fm);
1840}
1841
1842void owl_function_start_command(char *line)
1843{
1844  int i, j;
1845  owl_editwin *tw;
1846
1847  tw=owl_global_get_typwin(&g);
1848  owl_global_set_typwin_active(&g);
1849  owl_editwin_new_style(tw, OWL_EDITWIN_STYLE_ONELINE, 
1850                        owl_global_get_cmd_history(&g));
1851
1852  owl_editwin_set_locktext(tw, "command: ");
1853  owl_global_set_needrefresh(&g);
1854
1855  j=strlen(line);
1856  for (i=0; i<j; i++) {
1857    owl_editwin_process_char(tw, line[i]);
1858  }
1859  owl_editwin_redisplay(tw, 0);
1860}
1861
1862char *owl_function_exec(int argc, char **argv, char *buff, int type)
1863{
1864  /* if type == 1 display in a popup
1865   * if type == 2 display an admin messages
1866   * if type == 0 return output
1867   * else display in a popup
1868   */
1869  char *newbuff, *redirect = " 2>&1 < /dev/null";
1870  char *out, buff2[1024];
1871  int size;
1872  FILE *p;
1873
1874  if (argc<2) {
1875    owl_function_makemsg("Wrong number of arguments to the exec command");
1876    return NULL;
1877  }
1878
1879  buff = skiptokens(buff, 1);
1880  newbuff = owl_malloc(strlen(buff)+strlen(redirect)+1);
1881  strcpy(newbuff, buff);
1882  strcat(newbuff, redirect);
1883
1884  p=popen(newbuff, "r");
1885  out=owl_malloc(1024);
1886  size=1024;
1887  strcpy(out, "");
1888  while (fgets(buff2, 1024, p)!=NULL) {
1889    size+=1024;
1890    out=owl_realloc(out, size);
1891    strcat(out, buff2);
1892  }
1893  pclose(p);
1894
1895  if (type==1) {
1896    owl_function_popless_text(out);
1897  } else if (type==0) {
1898    return out;
1899  } else if (type==2) {
1900    owl_function_adminmsg(buff, out);
1901  } else {
1902    owl_function_popless_text(out);
1903  }
1904  owl_free(out);
1905  return NULL;
1906}
1907
1908
1909char *owl_function_perl(int argc, char **argv, char *buff, int type)
1910{
1911  /* if type == 1 display in a popup
1912   * if type == 2 display an admin messages
1913   * if type == 0 return output
1914   * else display in a popup
1915   */
1916  char *perlout;
1917
1918  if (argc<2) {
1919    owl_function_makemsg("Wrong number of arguments to perl command");
1920    return NULL;
1921  }
1922
1923  /* consume first token (argv[0]) */
1924  buff = skiptokens(buff, 1);
1925
1926  perlout = owl_config_execute(buff);
1927  if (perlout) { 
1928    if (type==1) {
1929      owl_function_popless_text(perlout);
1930    } else if (type==2) {
1931      owl_function_adminmsg(buff, perlout);
1932    } else if (type==0) {
1933      return perlout;
1934    } else {
1935      owl_function_popless_text(perlout);
1936    }
1937    owl_free(perlout);
1938  }
1939  return NULL;
1940}
1941
1942
1943void owl_function_change_view(char *filtname)
1944{
1945  owl_view *v;
1946  owl_filter *f;
1947  int curid=-1, newpos, curmsg;
1948  owl_message *curm=NULL;
1949
1950  v=owl_global_get_current_view(&g);
1951  curmsg=owl_global_get_curmsg(&g);
1952  if (curmsg==-1) {
1953    owl_function_debugmsg("Hit the curmsg==-1 case in change_view");
1954  } else {
1955    curm=owl_view_get_element(v, curmsg);
1956    if (curm) {
1957      curid=owl_message_get_id(curm);
1958      owl_view_save_curmsgid(v, curid);
1959    }
1960  }
1961
1962  /* grab the filter */;
1963  f=owl_global_get_filter(&g, filtname);
1964  if (!f) {
1965    owl_function_makemsg("Unknown filter");
1966    return;
1967  }
1968
1969  /* free the existing view and create a new one based on the filter */
1970  owl_view_free(v);
1971  owl_view_create(v, f);
1972
1973  /* Figure out what to set the current message to.
1974   * - If the view we're leaving has messages in it, go to the closest message
1975   *   to the last message pointed to in that view.
1976   * - If the view we're leaving is empty, try to restore the position
1977   *   from the last time we were in the new view.  */
1978  if (curm) {
1979    newpos = owl_view_get_nearest_to_msgid(v, curid);
1980  } else {
1981    newpos = owl_view_get_nearest_to_saved(v);
1982  }
1983
1984  owl_global_set_curmsg(&g, newpos);
1985
1986  owl_function_calculate_topmsg(OWL_DIRECTION_DOWNWARDS);
1987  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
1988  owl_global_set_direction_downwards(&g);
1989}
1990
1991void owl_function_create_filter(int argc, char **argv)
1992{
1993  owl_filter *f;
1994  owl_view *v;
1995  int ret, inuse=0;
1996
1997  if (argc < 2) {
1998    owl_function_makemsg("Wrong number of arguments to filter command");
1999    return;
2000  }
2001
2002  v=owl_global_get_current_view(&g);
2003
2004  /* don't touch the all filter */
2005  if (!strcmp(argv[1], "all")) {
2006    owl_function_makemsg("You may not change the 'all' filter.");
2007    return;
2008  }
2009
2010  /* deal with the case of trying change the filter color */
2011  if (argc==4 && !strcmp(argv[2], "-c")) {
2012    f=owl_global_get_filter(&g, argv[1]);
2013    if (!f) {
2014      owl_function_makemsg("The filter '%s' does not exist.", argv[1]);
2015      return;
2016    }
2017    owl_filter_set_color(f, owl_util_string_to_color(argv[3]));
2018    owl_global_set_needrefresh(&g);
2019    owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2020    return;
2021  }
2022
2023  /* create the filter and check for errors */
2024  f=owl_malloc(sizeof(owl_filter));
2025  ret=owl_filter_init(f, argv[1], argc-2, argv+2);
2026  if (ret==-1) {
2027    owl_free(f);
2028    owl_function_makemsg("Invalid filter syntax");
2029    return;
2030  }
2031
2032  /* if the named filter is in use by the current view, remember it */
2033  if (!strcmp(owl_view_get_filtname(v), argv[1])) {
2034    inuse=1;
2035  }
2036
2037  /* if the named filter already exists, nuke it */
2038  if (owl_global_get_filter(&g, argv[1])) {
2039    owl_global_remove_filter(&g, argv[1]);
2040  }
2041
2042  /* add the filter */
2043  owl_global_add_filter(&g, f);
2044
2045  /* if it was in use by the current view then update */
2046  if (inuse) {
2047    owl_function_change_view(argv[1]);
2048  }
2049  owl_global_set_needrefresh(&g);
2050  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2051}
2052
2053void owl_function_show_filters()
2054{
2055  owl_list *l;
2056  owl_filter *f;
2057  int i, j;
2058  owl_fmtext fm;
2059
2060  owl_fmtext_init_null(&fm);
2061
2062  l=owl_global_get_filterlist(&g);
2063  j=owl_list_get_size(l);
2064
2065  owl_fmtext_append_bold(&fm, "Filters:\n");
2066
2067  for (i=0; i<j; i++) {
2068    f=owl_list_get_element(l, i);
2069    owl_fmtext_append_normal(&fm, "   ");
2070    if (owl_global_get_hascolors(&g)) {
2071      owl_fmtext_append_normal_color(&fm, owl_filter_get_name(f), owl_filter_get_color(f));
2072    } else {
2073      owl_fmtext_append_normal(&fm, owl_filter_get_name(f));
2074    }
2075    owl_fmtext_append_normal(&fm, "\n");
2076  }
2077  owl_function_popless_fmtext(&fm);
2078  owl_fmtext_free(&fm);
2079}
2080
2081void owl_function_show_filter(char *name)
2082{
2083  owl_filter *f;
2084  char buff[5000];
2085
2086  f=owl_global_get_filter(&g, name);
2087  if (!f) {
2088    owl_function_makemsg("There is no filter with that name");
2089    return;
2090  }
2091  owl_filter_print(f, buff);
2092  owl_function_popless_text(buff);
2093}
2094
2095void owl_function_show_zpunts()
2096{
2097  owl_filter *f;
2098  owl_list *fl;
2099  char buff[5000];
2100  owl_fmtext fm;
2101  int i, j;
2102
2103  owl_fmtext_init_null(&fm);
2104
2105  fl=owl_global_get_puntlist(&g);
2106  j=owl_list_get_size(fl);
2107  owl_fmtext_append_bold(&fm, "Active zpunt filters:\n");
2108
2109  for (i=0; i<j; i++) {
2110    f=owl_list_get_element(fl, i);
2111    owl_filter_print(f, buff);
2112    owl_fmtext_append_normal(&fm, buff);
2113  }
2114  owl_function_popless_fmtext(&fm);
2115  owl_fmtext_free(&fm);
2116}
2117
2118char *owl_function_fastclassinstfilt(char *class, char *instance) 
2119{
2120  /* creates a filter for a class, instance if one doesn't exist.
2121   * If instance is null then apply for all messgaes in the class.
2122   * returns the name of the filter, which the caller must free.*/
2123  owl_list *fl;
2124  owl_filter *f;
2125  char *argbuff, *filtname;
2126  char *tmpclass, *tmpinstance = NULL;
2127  int len;
2128
2129  fl=owl_global_get_filterlist(&g);
2130
2131  /* name for the filter */
2132  len=strlen(class)+30;
2133  if (instance) len+=strlen(instance);
2134  filtname=owl_malloc(len);
2135  if (!instance) {
2136    sprintf(filtname, "class-%s", class);
2137  } else {
2138    sprintf(filtname, "class-%s-instance-%s", class, instance);
2139  }
2140  /* downcase it */
2141  downstr(filtname);
2142  /* turn spaces into hyphens */
2143  owl_util_tr(filtname, ' ', '.');
2144 
2145  /* if it already exists then go with it.  This lets users override */
2146  if (owl_global_get_filter(&g, filtname)) {
2147    return(filtname);
2148  }
2149
2150  /* create the new filter */
2151  argbuff=owl_malloc(len+20);
2152  tmpclass=owl_strdup(class);
2153  owl_util_tr(tmpclass, ' ', '.');
2154  if (instance) {
2155    tmpinstance=owl_strdup(instance);
2156    owl_util_tr(tmpinstance, ' ', '.');
2157  }
2158  sprintf(argbuff, "( class ^%s$ )", tmpclass);
2159  if (tmpinstance) {
2160    sprintf(argbuff, "%s and ( instance ^%s$ )", argbuff, tmpinstance);
2161  }
2162  owl_free(tmpclass);
2163  if (tmpinstance) owl_free(tmpinstance);
2164
2165  f=owl_malloc(sizeof(owl_filter));
2166  owl_filter_init_fromstring(f, filtname, argbuff);
2167
2168  /* add it to the global list */
2169  owl_global_add_filter(&g, f);
2170
2171  owl_free(argbuff);
2172  return(filtname);
2173}
2174
2175char *owl_function_fastuserfilt(char *user)
2176{
2177  owl_filter *f;
2178  char *argbuff, *longuser, *shortuser, *filtname;
2179
2180  /* stick the local realm on if it's not there */
2181  longuser=long_zuser(user);
2182  shortuser=short_zuser(user);
2183
2184  /* name for the filter */
2185  filtname=owl_malloc(strlen(shortuser)+20);
2186  sprintf(filtname, "user-%s", shortuser);
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-internal filter */
2194  f=owl_malloc(sizeof(owl_filter));
2195
2196  argbuff=owl_malloc(strlen(longuser)+1000);
2197  sprintf(argbuff, "( type ^zephyr$ and ( class ^message$ and instance ^personal$ and ");
2198  sprintf(argbuff, "%s ( ( direction ^in$ and sender ^%s$ ) or ( direction ^out$ and recipient ^%s$ ) ) )", argbuff, longuser, longuser);
2199  sprintf(argbuff, "%s or ( ( class ^login$ ) and ( sender ^%s$ ) ) )", argbuff, longuser);
2200
2201  owl_filter_init_fromstring(f, filtname, argbuff);
2202
2203  /* add it to the global list */
2204  owl_global_add_filter(&g, f);
2205
2206  /* free stuff */
2207  owl_free(argbuff);
2208  owl_free(longuser);
2209  owl_free(shortuser);
2210
2211  return(filtname);
2212}
2213
2214char *owl_function_fasttypefilt(char *type)
2215{
2216  owl_filter *f;
2217  char *argbuff, *filtname;
2218
2219  /* name for the filter */
2220  filtname=owl_sprintf("type-%s", type);
2221
2222  /* if it already exists then go with it.  This lets users override */
2223  if (owl_global_get_filter(&g, filtname)) {
2224    return filtname;
2225  }
2226
2227  /* create the new-internal filter */
2228  f=owl_malloc(sizeof(owl_filter));
2229
2230  argbuff = owl_sprintf("type ^%s$", type);
2231
2232  owl_filter_init_fromstring(f, filtname, argbuff);
2233
2234  /* add it to the global list */
2235  owl_global_add_filter(&g, f);
2236
2237  /* free stuff */
2238  owl_free(argbuff);
2239
2240  return filtname;
2241}
2242
2243/* If flag is 1, marks for deletion.  If flag is 0,
2244 * unmarks for deletion. */
2245void owl_function_delete_curview_msgs(int flag)
2246{
2247  owl_view *v;
2248  int i, j;
2249
2250  v=owl_global_get_current_view(&g);
2251  j=owl_view_get_size(v);
2252  for (i=0; i<j; i++) {
2253    if (flag == 1) {
2254      owl_message_mark_delete(owl_view_get_element(v, i));
2255    } else if (flag == 0) {
2256      owl_message_unmark_delete(owl_view_get_element(v, i));
2257    }
2258  }
2259
2260  owl_function_makemsg("%i messages marked for %sdeletion", j, flag?"":"un");
2261
2262  owl_mainwin_redisplay(owl_global_get_mainwin(&g)); 
2263}
2264
2265char *owl_function_smartfilter(int type)
2266{
2267  /* Returns the name of a filter, or null.  The caller
2268   * must free this name.  */
2269  /* if the curmsg is a personal message return a filter name
2270   *    to the converstaion with that user.
2271   * If the curmsg is a class message, instance foo, recip *
2272   *    message, return a filter name to the class, inst.
2273   * If the curmsg is a class message and type==0 then
2274   *    return a filter name for just the class.
2275   * If the curmsg is a class message and type==1 then
2276   *    return a filter name for the class and instance.
2277   */
2278  owl_view *v;
2279  owl_message *m;
2280  char *zperson, *filtname=NULL;
2281 
2282  v=owl_global_get_current_view(&g);
2283  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
2284
2285  if (!m || owl_view_get_size(v)==0) {
2286    owl_function_makemsg("No message selected\n");
2287    return(NULL);
2288  }
2289
2290  /* very simple handling of admin messages for now */
2291  if (owl_message_is_type_admin(m)) {
2292    return(owl_function_fasttypefilt("admin"));
2293  }
2294
2295  /* narrow personal and login messages to the sender or recip as appropriate */
2296  if (owl_message_is_personal(m) || owl_message_is_login(m)) {
2297    if (owl_message_is_type_zephyr(m)) {
2298      if (owl_message_is_direction_in(m)) {
2299        zperson=short_zuser(owl_message_get_sender(m));
2300      } else {
2301        zperson=short_zuser(owl_message_get_recipient(m));
2302      }
2303      filtname=owl_function_fastuserfilt(zperson);
2304      owl_free(zperson);
2305      return(filtname);
2306    }
2307    return(NULL);
2308  }
2309
2310  /* narrow class MESSAGE, instance foo, recip * messages to class, inst */
2311  if (!strcasecmp(owl_message_get_class(m), "message") &&
2312      !owl_message_is_personal(m)) {
2313    filtname=owl_function_fastclassinstfilt(owl_message_get_class(m), owl_message_get_instance(m));
2314    return(filtname);
2315  }
2316
2317  /* otherwise narrow to the class */
2318  if (type==0) {
2319    filtname=owl_function_fastclassinstfilt(owl_message_get_class(m), NULL);
2320  } else if (type==1) {
2321    filtname=owl_function_fastclassinstfilt(owl_message_get_class(m), owl_message_get_instance(m));
2322  }
2323  return(filtname);
2324}
2325
2326void owl_function_smartzpunt(int type)
2327{
2328  /* Starts a zpunt command based on the current class,instance pair.
2329   * If type=0, uses just class.  If type=1, uses instance as well. */
2330  owl_view *v;
2331  owl_message *m;
2332  char *cmd, *cmdprefix, *mclass, *minst;
2333 
2334  v=owl_global_get_current_view(&g);
2335  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
2336
2337  if (!m || owl_view_get_size(v)==0) {
2338    owl_function_makemsg("No message selected\n");
2339    return;
2340  }
2341
2342  /* for now we skip admin messages. */
2343  if (owl_message_is_type_admin(m)
2344      || owl_message_is_login(m)
2345      || !owl_message_is_type_zephyr(m)) {
2346    owl_function_makemsg("smartzpunt doesn't support this message type.");
2347    return;
2348  }
2349
2350  mclass = owl_message_get_class(m);
2351  minst = owl_message_get_instance(m);
2352  if (!mclass || !*mclass || *mclass==' '
2353      || (!strcasecmp(mclass, "message") && !strcasecmp(minst, "personal"))
2354      || (type && (!minst || !*minst|| *minst==' '))) {
2355    owl_function_makemsg("smartzpunt can't safely do this for <%s,%s>",
2356                         mclass, minst);
2357  } else {
2358    cmdprefix = "start-command zpunt ";
2359    cmd = owl_malloc(strlen(cmdprefix)+strlen(mclass)+strlen(minst)+3);
2360    strcpy(cmd, cmdprefix);
2361    strcat(cmd, mclass);
2362    if (type) {
2363      strcat(cmd, " ");
2364      strcat(cmd, minst);
2365    } else {
2366      strcat(cmd, " *");
2367    }
2368    owl_function_command(cmd);
2369    owl_free(cmd);
2370  }
2371}
2372
2373
2374
2375void owl_function_color_current_filter(char *color)
2376{
2377  owl_filter *f;
2378  char *name;
2379
2380  name=owl_view_get_filtname(owl_global_get_current_view(&g));
2381  f=owl_global_get_filter(&g, name);
2382  if (!f) {
2383    owl_function_makemsg("Unknown filter");
2384    return;
2385  }
2386
2387  /* don't touch the all filter */
2388  if (!strcmp(name, "all")) {
2389    owl_function_makemsg("You may not change the 'all' filter.");
2390    return;
2391  }
2392
2393  /* deal with the case of trying change the filter color */
2394  owl_filter_set_color(f, owl_util_string_to_color(color));
2395  owl_global_set_needrefresh(&g);
2396  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2397}
2398
2399void owl_function_show_colors()
2400{
2401  owl_fmtext fm;
2402
2403  owl_fmtext_init_null(&fm);
2404  owl_fmtext_append_normal_color(&fm, "default\n", OWL_COLOR_DEFAULT);
2405  owl_fmtext_append_normal_color(&fm, "red\n", OWL_COLOR_RED);
2406  owl_fmtext_append_normal_color(&fm, "green\n", OWL_COLOR_GREEN);
2407  owl_fmtext_append_normal_color(&fm, "yellow\n", OWL_COLOR_YELLOW);
2408  owl_fmtext_append_normal_color(&fm, "blue\n", OWL_COLOR_BLUE);
2409  owl_fmtext_append_normal_color(&fm, "magenta\n", OWL_COLOR_MAGENTA);
2410  owl_fmtext_append_normal_color(&fm, "cyan\n", OWL_COLOR_CYAN);
2411  owl_fmtext_append_normal_color(&fm, "white\n", OWL_COLOR_WHITE);
2412
2413  owl_function_popless_fmtext(&fm);
2414  owl_fmtext_free(&fm);
2415}
2416
2417void owl_function_zpunt(char *class, char *inst, char *recip, int direction)
2418{
2419  /* add the given class, inst, recip to the punt list for filtering.
2420   *   if direction==0 then punt
2421   *   if direction==1 then unpunt */
2422  owl_filter *f;
2423  owl_list *fl;
2424  char *buff;
2425  int ret, i, j;
2426
2427  fl=owl_global_get_puntlist(&g);
2428
2429  /* first, create the filter */
2430  f=malloc(sizeof(owl_filter));
2431  buff=malloc(strlen(class)+strlen(inst)+strlen(recip)+100);
2432  if (!strcmp(recip, "*")) {
2433    sprintf(buff, "class ^%s$ and instance ^%s$", class, inst);
2434  } else {
2435    sprintf(buff, "class ^%s$ and instance ^%s$ and recipient %s", class, inst, recip);
2436  }
2437  owl_function_debugmsg("About to filter %s", buff);
2438  ret=owl_filter_init_fromstring(f, "punt-filter", buff);
2439  owl_free(buff);
2440  if (ret) {
2441    owl_function_makemsg("Error creating filter for zpunt");
2442    owl_filter_free(f);
2443    return;
2444  }
2445
2446  /* Check for an identical filter */
2447  j=owl_list_get_size(fl);
2448  for (i=0; i<j; i++) {
2449    if (owl_filter_equiv(f, owl_list_get_element(fl, i))) {
2450      /* if we're punting, then just silently bow out on this duplicate */
2451      if (direction==0) {
2452        owl_filter_free(f);
2453        return;
2454      }
2455
2456      /* if we're unpunting, then remove this filter from the puntlist */
2457      if (direction==1) {
2458        owl_filter_free(owl_list_get_element(fl, i));
2459        owl_list_remove_element(fl, i);
2460        return;
2461      }
2462    }
2463  }
2464
2465  /* If we're punting, add the filter to the global punt list */
2466  if (direction==0) {
2467    owl_list_append_element(fl, f);
2468  }
2469}
2470
2471void owl_function_activate_keymap(char *keymap)
2472{
2473  if (!owl_keyhandler_activate(owl_global_get_keyhandler(&g), keymap)) {
2474    owl_function_makemsg("Unable to activate keymap '%s'", keymap);
2475  }
2476}
2477
2478
2479void owl_function_show_keymaps()
2480{
2481  owl_list l;
2482  owl_fmtext fm;
2483  owl_keymap *km;
2484  owl_keyhandler *kh;
2485  int i, numkm;
2486  char *kmname;
2487
2488  kh = owl_global_get_keyhandler(&g);
2489  owl_fmtext_init_null(&fm);
2490  owl_fmtext_append_bold(&fm, "Keymaps:   ");
2491  owl_fmtext_append_normal(&fm, "(use 'show keymap <name>' for details)\n");
2492  owl_keyhandler_get_keymap_names(kh, &l);
2493  owl_fmtext_append_list(&fm, &l, "\n", owl_function_keymap_summary);
2494  owl_fmtext_append_normal(&fm, "\n");
2495
2496  numkm = owl_list_get_size(&l);
2497  for (i=0; i<numkm; i++) {
2498    kmname = owl_list_get_element(&l, i);
2499    km = owl_keyhandler_get_keymap(kh, kmname);
2500    owl_fmtext_append_bold(&fm, "\n\n----------------------------------------------------------------------------------------------------\n\n");
2501    owl_keymap_get_details(km, &fm);   
2502  }
2503  owl_fmtext_append_normal(&fm, "\n");
2504 
2505  owl_function_popless_fmtext(&fm);
2506  owl_keyhandler_keymap_namelist_free(&l);
2507  owl_fmtext_free(&fm);
2508}
2509
2510char *owl_function_keymap_summary(void *name)
2511{
2512  owl_keymap *km
2513    = owl_keyhandler_get_keymap(owl_global_get_keyhandler(&g), name);
2514  if (km) return owl_keymap_summary(km);
2515  else return(NULL);
2516}
2517
2518/* TODO: implement for real */
2519void owl_function_show_keymap(char *name)
2520{
2521  owl_fmtext fm;
2522  owl_keymap *km;
2523
2524  owl_fmtext_init_null(&fm);
2525  km = owl_keyhandler_get_keymap(owl_global_get_keyhandler(&g), name);
2526  if (km) {
2527    owl_keymap_get_details(km, &fm);
2528  } else {
2529    owl_fmtext_append_normal(&fm, "No such keymap...\n");
2530  } 
2531  owl_function_popless_fmtext(&fm);
2532  owl_fmtext_free(&fm);
2533}
2534
2535void owl_function_help_for_command(char *cmdname)
2536{
2537  owl_fmtext fm;
2538
2539  owl_fmtext_init_null(&fm);
2540  owl_cmd_get_help(owl_global_get_cmddict(&g), cmdname, &fm);
2541  owl_function_popless_fmtext(&fm); 
2542  owl_fmtext_free(&fm);
2543}
2544
2545void owl_function_search_start(char *string, int direction)
2546{
2547  /* direction is OWL_DIRECTION_DOWNWARDS or OWL_DIRECTION_UPWARDS */
2548  owl_global_set_search_active(&g, string);
2549  owl_function_search_helper(0, direction);
2550}
2551
2552void owl_function_search_continue(int direction)
2553{
2554  /* direction is OWL_DIRECTION_DOWNWARDS or OWL_DIRECTION_UPWARDS */
2555  owl_function_search_helper(1, direction);
2556}
2557
2558void owl_function_search_helper(int mode, int direction)
2559{
2560  /* move to a message that contains the string.  If direction is
2561   * OWL_DIRECTION_DOWNWARDS then search fowards, if direction is
2562   * OWL_DIRECTION_UPWARDS then search backwards.
2563   *
2564   * If mode==0 then it will stay on the current message if it
2565   * contains the string.
2566   */
2567
2568  owl_view *v;
2569  int viewsize, i, curmsg, start;
2570  owl_message *m;
2571
2572  v=owl_global_get_current_view(&g);
2573  viewsize=owl_view_get_size(v);
2574  curmsg=owl_global_get_curmsg(&g);
2575 
2576  if (viewsize==0) {
2577    owl_function_makemsg("No messages present");
2578    return;
2579  }
2580
2581  if (mode==0) {
2582    start=curmsg;
2583  } else if (direction==OWL_DIRECTION_DOWNWARDS) {
2584    start=curmsg+1;
2585  } else {
2586    start=curmsg-1;
2587  }
2588
2589  /* bounds check */
2590  if (start>=viewsize || start<0) {
2591    owl_function_makemsg("No further matches found");
2592    return;
2593  }
2594
2595  for (i=start; i<viewsize && i>=0;) {
2596    m=owl_view_get_element(v, i);
2597    if (owl_message_search(m, owl_global_get_search_string(&g))) {
2598      owl_global_set_curmsg(&g, i);
2599      owl_function_calculate_topmsg(direction);
2600      owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2601      if (direction==OWL_DIRECTION_DOWNWARDS) {
2602        owl_global_set_direction_downwards(&g);
2603      } else {
2604        owl_global_set_direction_upwards(&g);
2605      }
2606      return;
2607    }
2608    if (direction==OWL_DIRECTION_DOWNWARDS) {
2609      i++;
2610    } else {
2611      i--;
2612    }
2613  }
2614  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2615  owl_function_makemsg("No matches found");
2616}
2617
2618
2619/* strips formatting from ztext and returns the unformatted text.
2620 * caller is responsible for freeing. */
2621char *owl_function_ztext_stylestrip(char *zt)
2622{
2623  owl_fmtext fm;
2624  char *plaintext;
2625
2626  owl_fmtext_init_null(&fm);
2627  owl_fmtext_append_ztext(&fm, zt);
2628  plaintext = owl_fmtext_print_plain(&fm);
2629  owl_fmtext_free(&fm);
2630  return(plaintext);
2631}
2632
2633/* popup a znol listing.  If file is NULL use the default .anyone */
2634/* this doesn't obey 'elapsed' or 'timesort' yet */
2635void owl_function_zlist(char *file, int elapsed, int timesort)
2636{
2637  char *ourfile, *tmp, buff[LINE], *line;
2638  FILE *f;
2639  int numlocs, ret, i;
2640  ZLocations_t location[200];
2641  owl_fmtext fm;
2642
2643  if (file==NULL) {
2644    tmp=owl_global_get_homedir(&g);
2645    if (!tmp) {
2646      owl_function_makemsg("Could not determine home directory");
2647      return;
2648    }
2649    ourfile=owl_malloc(strlen(tmp)+50);
2650    sprintf(ourfile, "%s/.anyone", owl_global_get_homedir(&g));
2651  } else {
2652    ourfile=owl_strdup(file);
2653  }
2654
2655  f=fopen(ourfile, "r");
2656  if (!f) {
2657    owl_function_makemsg("Error opening file %s: %s",
2658                         ourfile,
2659                         strerror(errno) ? strerror(errno) : "");
2660    return;
2661  }
2662
2663  owl_fmtext_init_null(&fm);
2664   
2665  while (fgets(buff, LINE, f)!=NULL) {
2666    /* ignore comments, blank lines etc. */
2667    if (buff[0]=='#') continue;
2668    if (buff[0]=='\n') continue;
2669    if (buff[0]=='\0') continue;
2670
2671    /* strip the \n */
2672    buff[strlen(buff)-1]='\0';
2673
2674    /* ingore from # on */
2675    tmp=strchr(buff, '#');
2676    if (tmp) tmp[0]='\0';
2677
2678    /* ingore from SPC */
2679    tmp=strchr(buff, ' ');
2680    if (tmp) tmp[0]='\0';
2681
2682    /* stick on the local realm. */
2683    if (!strchr(buff, '@')) {
2684      strcat(buff, "@");
2685      strcat(buff, ZGetRealm());
2686    }
2687
2688    ret=ZLocateUser(buff, &numlocs, ZAUTH);
2689    if (ret!=ZERR_NONE) {
2690      owl_function_makemsg("Error getting location for %s", buff);
2691      continue;
2692    }
2693
2694    numlocs=200;
2695    ret=ZGetLocations(location, &numlocs);
2696    if (ret==0) {
2697      for (i=0; i<numlocs; i++) {
2698        line=malloc(strlen(location[i].host)+strlen(location[i].time)+strlen(location[i].tty)+100);
2699        tmp=short_zuser(buff);
2700        sprintf(line, "%-10.10s %-24.24s %-12.12s  %20.20s\n",
2701                tmp,
2702                location[i].host,
2703                location[i].tty,
2704                location[i].time);
2705        owl_fmtext_append_normal(&fm, line);
2706        owl_free(tmp);
2707      }
2708      if (numlocs>=200) {
2709        owl_fmtext_append_normal(&fm, "Too many locations found for this user, truncating.\n");
2710      }
2711    }
2712  }
2713  fclose(f);
2714
2715  owl_function_popless_fmtext(&fm);
2716  owl_fmtext_free(&fm);
2717
2718  owl_free(ourfile);
2719}
2720
2721void owl_function_dump(char *filename) 
2722{
2723  int i, j, count;
2724  owl_message *m;
2725  owl_view *v;
2726  FILE *file;
2727  /* struct stat sbuf; */
2728
2729  v=owl_global_get_current_view(&g);
2730
2731  /* in the future make it ask yes/no */
2732  /*
2733  ret=stat(filename, &sbuf);
2734  if (!ret) {
2735    ret=owl_function_askyesno("File exists, continue? [Y/n]");
2736    if (!ret) return;
2737  }
2738  */
2739
2740  file=fopen(filename, "w");
2741  if (!file) {
2742    owl_function_makemsg("Error opening file");
2743    return;
2744  }
2745
2746  count=0;
2747  j=owl_view_get_size(v);
2748  for (i=0; i<j; i++) {
2749    m=owl_view_get_element(v, i);
2750    fputs(owl_message_get_text(m), file);
2751  }
2752  fclose(file);
2753}
2754
2755
2756
2757void owl_function_do_newmsgproc(void)
2758{
2759  if (owl_global_get_newmsgproc(&g) && strcmp(owl_global_get_newmsgproc(&g), "")) {
2760    /* if there's a process out there, we need to check on it */
2761    if (owl_global_get_newmsgproc_pid(&g)) {
2762      owl_function_debugmsg("Checking on newmsgproc pid==%i", owl_global_get_newmsgproc_pid(&g));
2763      owl_function_debugmsg("Waitpid return is %i", waitpid(owl_global_get_newmsgproc_pid(&g), NULL, WNOHANG));
2764      waitpid(owl_global_get_newmsgproc_pid(&g), NULL, WNOHANG);
2765      if (waitpid(owl_global_get_newmsgproc_pid(&g), NULL, WNOHANG)==-1) {
2766        /* it exited */
2767        owl_global_set_newmsgproc_pid(&g, 0);
2768        owl_function_debugmsg("newmsgproc exited");
2769      } else {
2770        owl_function_debugmsg("newmsgproc did not exit");
2771      }
2772    }
2773   
2774    /* if it exited, fork & exec a new one */
2775    if (owl_global_get_newmsgproc_pid(&g)==0) {
2776      int i, myargc;
2777      i=fork();
2778      if (i) {
2779        /* parent set the child's pid */
2780        owl_global_set_newmsgproc_pid(&g, i);
2781        owl_function_debugmsg("I'm the parent and I started a new newmsgproc with pid %i", i);
2782      } else {
2783        /* child exec's the program */
2784        char **parsed;
2785        parsed=owl_parseline(owl_global_get_newmsgproc(&g), &myargc);
2786        if (myargc < 0) {
2787          owl_function_debugmsg("Could not parse newmsgproc '%s': unbalanced quotes?", owl_global_get_newmsgproc(&g));
2788        }
2789        if (myargc <= 0) {
2790          _exit(127);
2791        }
2792        parsed=realloc(parsed, sizeof(*parsed) * (myargc+1));
2793        parsed[myargc] = NULL;
2794       
2795        owl_function_debugmsg("About to exec \"%s\" with %d arguments", parsed[0], myargc);
2796       
2797        execvp(parsed[0], parsed);
2798       
2799       
2800        /* was there an error exec'ing? */
2801        owl_function_debugmsg("Cannot run newmsgproc '%s': cannot exec '%s': %s", 
2802                              owl_global_get_newmsgproc(&g), parsed[0], strerror(errno));
2803        _exit(127);
2804      }
2805    }
2806  }
2807}
2808
2809void owl_function_xterm_raise(void)
2810{
2811  printf("\033[5t");
2812}
2813
2814void owl_function_xterm_deiconify(void)
2815{
2816  printf("\033[1t");
2817}
Note: See TracBrowser for help on using the repository browser.