source: functions.c @ 75be7c0

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