source: functions.c @ 1583c90

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