source: functions.c @ 0ff8fb57

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