source: functions.c @ 2c8d62e

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