source: functions.c @ 2527615

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