source: functions.c @ 855ebe7

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