source: functions.c @ 65fc0900

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