source: functions.c @ 5e53c4a

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