source: functions.c @ 7e3e00a

barnowl_perlaimdebianowlrelease-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 7e3e00a was 8f44c6b, checked in by James M. Kretchmar <kretch@mit.edu>, 19 years ago
Changed version to 2.0.1-pre-1 Moved the (broken) newmsgproc stuff it a function procedure
  • Property mode set to 100644
File size: 68.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  ZNotice_t *n;
1091  char buff[5000], tmpbuff[1024];
1092  char *ptr;
1093  int i, j, fields, len;
1094  owl_view *v;
1095
1096  v=owl_global_get_current_view(&g);
1097  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
1098  if (!m || owl_view_get_size(v)==0) {
1099    owl_function_makemsg("No message selected\n");
1100    return;
1101  }
1102
1103  sprintf(buff,     "Msg Id    : %i\n", owl_message_get_id(m));
1104  if (owl_message_is_type_zephyr(m)) {
1105    sprintf(buff, "%sType      : zephyr\n", buff);
1106  } else if (owl_message_is_type_admin(m)) {
1107    sprintf(buff, "%sType      : admin\n", buff);
1108  } else if (owl_message_is_type_generic(m)) {
1109    sprintf(buff, "%sType      : generic\n", buff);
1110  } else {
1111    sprintf(buff, "%sType      : unknown\n", buff);
1112  }
1113  if (owl_message_is_direction_in(m)) {
1114    sprintf(buff, "%sDirection : in\n", buff);
1115  } else if (owl_message_is_direction_out(m)) {
1116    sprintf(buff, "%sDirection : out\n", buff);
1117  } else if (owl_message_is_direction_none(m)) {
1118    sprintf(buff, "%sDirection : none\n", buff);
1119  } else {
1120    sprintf(buff, "%sDirection : unknown\n", buff);
1121  }
1122  sprintf(buff, "%sTime      : %s\n", buff, owl_message_get_timestr(m));
1123
1124  if (!owl_message_is_type_zephyr(m)) {
1125    owl_function_popless_text(buff);
1126    return;
1127  }
1128
1129
1130  if (owl_message_is_direction_out(m)) {
1131    sprintf(buff, "%sClass     : %s\n", buff, owl_message_get_class(m));
1132    sprintf(buff, "%sInstance  : %s\n", buff, owl_message_get_instance(m));
1133    sprintf(buff, "%sSender    : %s\n", buff, owl_message_get_sender(m));
1134    sprintf(buff, "%sRecip     : %s\n", buff, owl_message_get_recipient(m));
1135    sprintf(buff, "%sOpcode    : %s\n", buff, owl_message_get_opcode(m));
1136   
1137    owl_function_popless_text(buff);
1138    return;
1139  }
1140
1141  n=owl_message_get_notice(m);
1142
1143  sprintf(buff, "%sClass     : %s\n", buff, owl_message_get_class(m));
1144  sprintf(buff, "%sInstance  : %s\n", buff, owl_message_get_instance(m));
1145  sprintf(buff, "%sSender    : %s\n", buff, owl_message_get_sender(m));
1146  sprintf(buff, "%sRecip     : %s\n", buff, owl_message_get_recipient(m));
1147  sprintf(buff, "%sOpcode    : %s\n", buff, owl_message_get_opcode(m));
1148  strcat(buff,    "Kind      : ");
1149  if (n->z_kind==UNSAFE) {
1150    strcat(buff, "UNSAFE\n");
1151  } else if (n->z_kind==UNACKED) {
1152    strcat(buff, "UNACKED\n");
1153  } else if (n->z_kind==ACKED) {
1154    strcat(buff, "ACKED\n");
1155  } else if (n->z_kind==HMACK) {
1156    strcat(buff, "HMACK\n");
1157  } else if (n->z_kind==HMCTL) {
1158    strcat(buff, "HMCTL\n");
1159  } else if (n->z_kind==SERVACK) {
1160    strcat(buff, "SERVACK\n");
1161  } else if (n->z_kind==SERVNAK) {
1162    strcat(buff, "SERVNAK\n");
1163  } else if (n->z_kind==CLIENTACK) {
1164    strcat(buff, "CLIENTACK\n");
1165  } else if (n->z_kind==STAT) {
1166    strcat(buff, "STAT\n");
1167  } else {
1168    strcat(buff, "ILLEGAL VALUE\n");
1169  }
1170  sprintf(buff, "%sTime      : %s\n", buff, owl_message_get_timestr(m));
1171  sprintf(buff, "%sHost      : %s\n", buff, owl_message_get_hostname(m));
1172  sprintf(buff, "%sPort      : %i\n", buff, n->z_port);
1173  strcat(buff,    "Auth      : ");
1174  if (n->z_auth == ZAUTH_FAILED) {
1175    strcat(buff, "FAILED\n");
1176  } else if (n->z_auth == ZAUTH_NO) {
1177    strcat(buff, "NO\n");
1178  } else if (n->z_auth == ZAUTH_YES) {
1179    strcat(buff, "YES\n");
1180  } else {
1181    sprintf(buff, "%sUnknown State (%i)\n", buff, n->z_auth);
1182  }
1183  sprintf(buff, "%sCheckd Ath: %i\n", buff, n->z_checked_auth);
1184  sprintf(buff, "%sMulti notc: %s\n", buff, n->z_multinotice);
1185  sprintf(buff, "%sNum other : %i\n", buff, n->z_num_other_fields);
1186  sprintf(buff, "%sMsg Len   : %i\n", buff, n->z_message_len);
1187
1188  sprintf(buff, "%sFields    : %i\n", buff, owl_zephyr_get_num_fields(n));
1189
1190  fields=owl_zephyr_get_num_fields(n);
1191  for (i=0; i<fields; i++) {
1192    sprintf(buff, "%sField %i   : ", buff, i+1);
1193
1194    ptr=owl_zephyr_get_field(n, i+1, &len);
1195    if (!ptr) break;
1196    if (len<30) {
1197      strncpy(tmpbuff, ptr, len);
1198      tmpbuff[len]='\0';
1199    } else {
1200      strncpy(tmpbuff, ptr, 30);
1201      tmpbuff[30]='\0';
1202      strcat(tmpbuff, "...");
1203    }
1204
1205    /* just for testing for now */
1206    for (j=0; j<strlen(tmpbuff); j++) {
1207      if (tmpbuff[j]=='\n') tmpbuff[j]='~';
1208      if (tmpbuff[j]=='\r') tmpbuff[j]='!';
1209    }
1210
1211    strcat(buff, tmpbuff);
1212    strcat(buff, "\n");
1213  }
1214  sprintf(buff, "%sDefault Fm: %s\n", buff, n->z_default_format);
1215       
1216  owl_function_popless_text(buff);
1217}
1218
1219
1220void owl_function_curmsg_to_popwin() {
1221  owl_popwin *pw;
1222  owl_view *v;
1223  owl_message *m;
1224
1225  v = owl_global_get_current_view(&g);
1226
1227  pw=owl_global_get_popwin(&g);
1228
1229  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
1230
1231  if (!m || owl_view_get_size(v)==0) {
1232    owl_function_makemsg("No current message");
1233    return;
1234  }
1235
1236  owl_function_popless_fmtext(owl_message_get_fmtext(m));
1237}
1238
1239
1240void owl_function_page_curmsg(int step) {
1241  /* scroll down or up within the current message IF the message is truncated */
1242
1243  int offset, curmsg, lines;
1244  owl_view *v;
1245  owl_message *m;
1246
1247  offset=owl_global_get_curmsg_vert_offset(&g);
1248  v=owl_global_get_current_view(&g);
1249  curmsg=owl_global_get_curmsg(&g);
1250  m=owl_view_get_element(v, curmsg);
1251  if (!m || owl_view_get_size(v)==0) return;
1252  lines=owl_message_get_numlines(m);
1253
1254  if (offset==0) {
1255    /* Bail if the curmsg isn't the last one displayed */
1256    if (curmsg != owl_mainwin_get_last_msg(owl_global_get_mainwin(&g))) {
1257      owl_function_makemsg("The entire message is already displayed");
1258      return;
1259    }
1260   
1261    /* Bail if we're not truncated */
1262    if (!owl_mainwin_is_curmsg_truncated(owl_global_get_mainwin(&g))) {
1263      owl_function_makemsg("The entire message is already displayed");
1264      return;
1265    }
1266  }
1267 
1268 
1269  /* don't scroll past the last line */
1270  if (step>0) {
1271    if (offset+step > lines-1) {
1272      owl_global_set_curmsg_vert_offset(&g, lines-1);
1273    } else {
1274      owl_global_set_curmsg_vert_offset(&g, offset+step);
1275    }
1276  }
1277
1278  /* would we be before the beginning of the message? */
1279  if (step<0) {
1280    if (offset+step<0) {
1281      owl_global_set_curmsg_vert_offset(&g, 0);
1282    } else {
1283      owl_global_set_curmsg_vert_offset(&g, offset+step);
1284    }
1285  }
1286 
1287  /* redisplay */
1288  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
1289  owl_global_set_needrefresh(&g);
1290}
1291
1292void owl_function_resize_typwin(int newsize) {
1293  owl_global_set_typwin_lines(&g, newsize);
1294  owl_function_resize();
1295}
1296
1297void owl_function_typwin_grow() {
1298  int i;
1299
1300  i=owl_global_get_typwin_lines(&g);
1301  owl_function_resize_typwin(i+1);
1302}
1303
1304void owl_function_typwin_shrink() {
1305  int i;
1306
1307  i=owl_global_get_typwin_lines(&g);
1308  if (i>2) {
1309    owl_function_resize_typwin(i-1);
1310  }
1311}
1312
1313void owl_function_mainwin_pagedown() {
1314  int i;
1315
1316  i=owl_mainwin_get_last_msg(owl_global_get_mainwin(&g));
1317  if (i<0) return;
1318  if (owl_mainwin_is_last_msg_truncated(owl_global_get_mainwin(&g))
1319      && (owl_global_get_curmsg(&g) < i)
1320      && (i>0)) {
1321    i--;
1322  }
1323  owl_global_set_curmsg(&g, i);
1324  owl_function_nextmsg();
1325}
1326
1327void owl_function_mainwin_pageup() {
1328  owl_global_set_curmsg(&g, owl_global_get_topmsg(&g));
1329  owl_function_prevmsg();
1330}
1331
1332void owl_function_getsubs() {
1333  int ret, num, i, one;
1334  ZSubscription_t sub;
1335  char *buff, *tmpbuff;
1336
1337  one = 1;
1338
1339  ret=ZRetrieveSubscriptions(0, &num);
1340  if (ret == ZERR_TOOMANYSUBS) {
1341    owl_function_makemsg("Zephyr: too many subscriptions");
1342    return;
1343  }
1344
1345  buff=owl_malloc(num*500);
1346  tmpbuff=owl_malloc(num*500);
1347  strcpy(buff, "");
1348  for (i=0; i<num; i++) {
1349    if ((ret = ZGetSubscriptions(&sub, &one)) != ZERR_NONE) {
1350      owl_function_makemsg("Error while getting subscriptions");
1351      owl_free(buff);
1352      owl_free(tmpbuff);
1353      ZFlushSubscriptions();
1354      return;
1355    } else {
1356      sprintf(tmpbuff, "<%s,%s,%s>\n%s", sub.zsub_class, sub.zsub_classinst, sub.zsub_recipient, buff);
1357      strcpy(buff, tmpbuff);
1358    }
1359  }
1360
1361  owl_function_popless_text(buff);
1362  owl_free(buff);
1363  owl_free(tmpbuff);
1364  ZFlushSubscriptions();
1365}
1366
1367#define PABUFLEN 5000
1368void owl_function_printallvars() {
1369  char buff[PABUFLEN], *pos, *name;
1370  owl_list varnames;
1371  int i, numvarnames, rem;
1372
1373  pos = buff;
1374  pos += sprintf(pos, "%-20s = %s\n", "VARIABLE", "VALUE");
1375  pos += sprintf(pos, "%-20s   %s\n",  "--------", "-----");
1376  owl_variable_dict_get_names(owl_global_get_vardict(&g), &varnames);
1377  rem = (buff+PABUFLEN)-pos-1;
1378  numvarnames = owl_list_get_size(&varnames);
1379  for (i=0; i<numvarnames; i++) {
1380    name = owl_list_get_element(&varnames, i);
1381    if (name && name[0]!='_') {
1382      rem = (buff+PABUFLEN)-pos-1;   
1383      pos += snprintf(pos, rem, "\n%-20s = ", name);
1384      rem = (buff+PABUFLEN)-pos-1;   
1385      owl_variable_get_tostring(owl_global_get_vardict(&g), name, pos, rem);
1386      pos = buff+strlen(buff);
1387    }
1388  }
1389  rem = (buff+PABUFLEN)-pos-1;   
1390  snprintf(pos, rem, "\n");
1391  owl_variable_dict_namelist_free(&varnames);
1392 
1393  owl_function_popless_text(buff);
1394}
1395
1396void owl_function_show_variables() {
1397  owl_list varnames;
1398  owl_fmtext fm; 
1399  int i, numvarnames;
1400  char *varname;
1401
1402  owl_fmtext_init_null(&fm);
1403  owl_fmtext_append_bold(&fm, 
1404      "Variables: (use 'show variable <name>' for details)\n");
1405  owl_variable_dict_get_names(owl_global_get_vardict(&g), &varnames);
1406  numvarnames = owl_list_get_size(&varnames);
1407  for (i=0; i<numvarnames; i++) {
1408    varname = owl_list_get_element(&varnames, i);
1409    if (varname && varname[0]!='_') {
1410      owl_variable_describe(owl_global_get_vardict(&g), varname, &fm);
1411    }
1412  }
1413  owl_variable_dict_namelist_free(&varnames);
1414  owl_function_popless_fmtext(&fm);
1415  owl_fmtext_free(&fm);
1416}
1417
1418void owl_function_show_variable(char *name) {
1419  owl_fmtext fm; 
1420
1421  owl_fmtext_init_null(&fm);
1422  owl_variable_get_help(owl_global_get_vardict(&g), name, &fm);
1423  owl_function_popless_fmtext(&fm);
1424  owl_fmtext_free(&fm); 
1425}
1426
1427/* note: this applies to global message list, not to view.
1428 * If flag is 1, deletes.  If flag is 0, undeletes. */
1429void owl_function_delete_by_id(int id, int flag) {
1430  owl_messagelist *ml;
1431  owl_message *m;
1432  ml = owl_global_get_msglist(&g);
1433  m = owl_messagelist_get_by_id(ml, id);
1434  if (m) {
1435    if (flag == 1) {
1436      owl_message_mark_delete(m);
1437    } else if (flag == 0) {
1438      owl_message_unmark_delete(m);
1439    }
1440    owl_mainwin_redisplay(owl_global_get_mainwin(&g));
1441    owl_global_set_needrefresh(&g);
1442  } else {
1443    owl_function_makemsg("No message with id %d: unable to mark for (un)delete",id);
1444  }
1445}
1446
1447void owl_function_delete_automsgs() {
1448  /* mark for deletion all messages in the current view that match the
1449   * 'trash' filter */
1450
1451  int i, j, count;
1452  owl_message *m;
1453  owl_view *v;
1454  owl_filter *f;
1455
1456  /* get the trash filter */
1457  f=owl_global_get_filter(&g, "trash");
1458  if (!f) {
1459    owl_function_makemsg("No trash filter defined");
1460    return;
1461  }
1462
1463  v=owl_global_get_current_view(&g);
1464
1465  count=0;
1466  j=owl_view_get_size(v);
1467  for (i=0; i<j; i++) {
1468    m=owl_view_get_element(v, i);
1469    if (owl_filter_message_match(f, m)) {
1470      count++;
1471      owl_message_mark_delete(m);
1472    }
1473  }
1474  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
1475  owl_function_makemsg("%i messages marked for deletion", count);
1476  owl_global_set_needrefresh(&g);
1477}
1478
1479
1480void owl_function_status() {
1481  char buff[5000];
1482  time_t start;
1483  int up, days, hours, minutes;
1484
1485  start=owl_global_get_starttime(&g);
1486
1487  sprintf(buff, "Version: %s\n", OWL_VERSION_STRING);
1488  sprintf(buff, "%sScreen size: %i lines, %i columns\n", buff, owl_global_get_lines(&g), owl_global_get_cols(&g));
1489  sprintf(buff, "%sStartup Arugments: %s\n", buff, owl_global_get_startupargs(&g));
1490  sprintf(buff, "%sStartup Time: %s", buff, ctime(&start));
1491
1492  up=owl_global_get_runtime(&g);
1493  days=up/86400;
1494  up-=days*86400;
1495  hours=up/3600;
1496  up-=hours*3600;
1497  minutes=up/60;
1498  up-=minutes*60;
1499  sprintf(buff, "%sRun Time: %i days %2.2i:%2.2i:%2.2i\n", buff, days, hours, minutes, up);
1500
1501  if (owl_global_get_hascolors(&g)) {
1502    sprintf(buff, "%sColor: Yes, %i color pairs.\n", buff, owl_global_get_colorpairs(&g));
1503  } else {
1504    strcat(buff, "Color: No.\n");
1505  }
1506 
1507  owl_function_popless_text(buff);
1508}
1509
1510void owl_function_show_term() {
1511  owl_fmtext fm;
1512  char buff[LINE];
1513
1514  owl_fmtext_init_null(&fm);
1515  sprintf(buff, "Terminal Lines: %i\nTerminal Columns: %i\n",
1516          owl_global_get_lines(&g),
1517          owl_global_get_cols(&g));
1518  owl_fmtext_append_normal(&fm, buff);
1519
1520  if (owl_global_get_hascolors(&g)) {
1521    owl_fmtext_append_normal(&fm, "Color: Yes\n");
1522    sprintf(buff, "Number of color pairs: %i\n", owl_global_get_colorpairs(&g));
1523    owl_fmtext_append_normal(&fm, buff);
1524    sprintf(buff, "Can change colors: %s\n", can_change_color() ? "yes" : "no");
1525    owl_fmtext_append_normal(&fm, buff);
1526  } else {
1527    owl_fmtext_append_normal(&fm, "Color: No\n");
1528  }
1529
1530  owl_function_popless_fmtext(&fm);
1531  owl_fmtext_free(&fm);
1532}
1533
1534
1535void owl_function_reply(int type, int enter) {
1536  /* if type = 0 then normal reply.
1537   * if type = 1 then it's a reply to sender
1538   * if enter = 0 then allow the command to be edited
1539   * if enter = 1 then don't wait for editing
1540   */
1541  char *buff, *oldbuff;
1542  owl_message *m;
1543  owl_filter *f;
1544 
1545  if (owl_view_get_size(owl_global_get_current_view(&g))==0) {
1546    owl_function_makemsg("No message selected");
1547  } else {
1548    char *class, *inst, *to, *cc=NULL;
1549   
1550    m=owl_view_get_element(owl_global_get_current_view(&g), owl_global_get_curmsg(&g));
1551    if (!m) {
1552      owl_function_makemsg("No message selected");
1553      return;
1554    }
1555
1556    /* first check if we catch the reply-lockout filter */
1557    f=owl_global_get_filter(&g, "reply-lockout");
1558    if (f) {
1559      if (owl_filter_message_match(f, m)) {
1560        owl_function_makemsg("Sorry, replies to this message have been disabled by the reply-lockout filter");
1561        return;
1562      }
1563    }
1564
1565    /* for now we disable replies to zcrypt messages, since we can't
1566       support an encrypted reply */
1567    if (!strcasecmp(owl_message_get_opcode(m), "crypt")) {
1568      owl_function_makemsg("Replies to zcrypt messages are not enabled in this release");
1569      return;
1570    }
1571
1572    if (owl_message_is_direction_out(m)) {
1573      owl_function_zwrite_setup(owl_message_get_zwriteline(m));
1574      owl_global_set_buffercommand(&g, owl_message_get_zwriteline(m));
1575    } else if (owl_message_is_type_admin(m)) {
1576      owl_function_makemsg("You cannot reply to an admin message");
1577    } else {
1578      if (owl_message_is_login(m)) {
1579        class="MESSAGE";
1580        inst="PERSONAL";
1581        to=owl_message_get_sender(m);
1582      } else if (type==1) {
1583        class="MESSAGE";
1584        inst="PERSONAL";
1585        to=owl_message_get_sender(m);
1586      } else {
1587        class=owl_message_get_class(m);
1588        inst=owl_message_get_instance(m);
1589        to=owl_message_get_recipient(m);
1590        cc=owl_message_get_cc(m);
1591        if (!strcmp(to, "") || !strcmp(to, "*")) {
1592          to="";
1593        } else if (to[0]=='@') {
1594          /* leave it, to get the realm */
1595        } else {
1596          to=owl_message_get_sender(m);
1597        }
1598      }
1599     
1600      /* create the command line */
1601      buff = owl_strdup("zwrite");
1602      if (strcasecmp(class, "message")) {
1603        buff = owl_sprintf("%s -c %s%s%s", oldbuff=buff, owl_getquoting(class), class, owl_getquoting(class));
1604        owl_free(oldbuff);
1605      }
1606      if (strcasecmp(inst, "personal")) {
1607        buff = owl_sprintf("%s -i %s%s%s", oldbuff=buff, owl_getquoting(inst), inst, owl_getquoting(inst));
1608        owl_free(oldbuff);
1609      }
1610      if (*to != '\0') {
1611        char *tmp, *oldtmp, *tmp2;
1612        tmp=short_zuser(to);
1613        if (cc) {
1614          tmp = owl_util_uniq(oldtmp=tmp, cc, "-");
1615          owl_free(oldtmp);
1616          buff = owl_sprintf("%s -C %s", oldbuff=buff, tmp);
1617          owl_free(oldbuff);
1618        } else {
1619          if (owl_global_is_smartstrip(&g)) {
1620            tmp2=tmp;
1621            tmp=smartstripped_user(tmp2);
1622            owl_free(tmp2);
1623          }
1624          buff = owl_sprintf("%s %s", oldbuff=buff, tmp);
1625          owl_free(oldbuff);
1626        }
1627        owl_free(tmp);
1628      }
1629      if (cc) owl_free(cc);
1630
1631      if (enter) {
1632        owl_history *hist = owl_global_get_cmd_history(&g);
1633        owl_history_store(hist, buff);
1634        owl_history_reset(hist);
1635        owl_function_command_norv(buff);
1636      } else {
1637        owl_function_start_command(buff);
1638      }
1639      owl_free(buff);
1640    }
1641  }
1642}
1643
1644void owl_function_zlocate(int argc, char **argv, int auth) {
1645  owl_fmtext fm;
1646  char *ptr, buff[LINE];
1647  int i;
1648
1649  owl_fmtext_init_null(&fm);
1650
1651  for (i=0; i<argc; i++) {
1652    ptr=long_zuser(argv[i]);
1653    owl_zephyr_zlocate(ptr, buff, auth);
1654    owl_fmtext_append_normal(&fm, buff);
1655    owl_free(ptr);
1656  }
1657
1658  owl_function_popless_fmtext(&fm);
1659  owl_fmtext_free(&fm);
1660}
1661
1662void owl_function_start_command(char *line) {
1663  int i, j;
1664  owl_editwin *tw;
1665
1666  tw=owl_global_get_typwin(&g);
1667  owl_global_set_typwin_active(&g);
1668  owl_editwin_new_style(tw, OWL_EDITWIN_STYLE_ONELINE, 
1669                        owl_global_get_cmd_history(&g));
1670
1671  owl_editwin_set_locktext(tw, "command: ");
1672  owl_global_set_needrefresh(&g);
1673
1674  j=strlen(line);
1675  for (i=0; i<j; i++) {
1676    owl_editwin_process_char(tw, line[i]);
1677  }
1678  owl_editwin_redisplay(tw, 0);
1679}
1680
1681char *owl_function_exec(int argc, char **argv, char *buff, int type) {
1682  /* if type == 1 display in a popup
1683   * if type == 2 display an admin messages
1684   * if type == 0 return output
1685   * else display in a popup
1686   */
1687  char *newbuff, *redirect = " 2>&1 < /dev/null";
1688  char *out, buff2[1024];
1689  int size;
1690  FILE *p;
1691
1692  if (argc<2) {
1693    owl_function_makemsg("Wrong number of arguments to the exec command");
1694    return NULL;
1695  }
1696
1697  buff = skiptokens(buff, 1);
1698  newbuff = owl_malloc(strlen(buff)+strlen(redirect)+1);
1699  strcpy(newbuff, buff);
1700  strcat(newbuff, redirect);
1701
1702  p=popen(newbuff, "r");
1703  out=owl_malloc(1024);
1704  size=1024;
1705  strcpy(out, "");
1706  while (fgets(buff2, 1024, p)!=NULL) {
1707    size+=1024;
1708    out=owl_realloc(out, size);
1709    strcat(out, buff2);
1710  }
1711  pclose(p);
1712
1713  if (type==1) {
1714    owl_function_popless_text(out);
1715  } else if (type==0) {
1716    return out;
1717  } else if (type==2) {
1718    owl_function_adminmsg(buff, out);
1719  } else {
1720    owl_function_popless_text(out);
1721  }
1722  owl_free(out);
1723  return NULL;
1724}
1725
1726
1727char *owl_function_perl(int argc, char **argv, char *buff, int type) {
1728  /* if type == 1 display in a popup
1729   * if type == 2 display an admin messages
1730   * if type == 0 return output
1731   * else display in a popup
1732   */
1733  char *perlout;
1734
1735  if (argc<2) {
1736    owl_function_makemsg("Wrong number of arguments to perl command");
1737    return NULL;
1738  }
1739
1740  /* consume first token (argv[0]) */
1741  buff = skiptokens(buff, 1);
1742
1743  perlout = owl_config_execute(buff);
1744  if (perlout) { 
1745    if (type==1) {
1746      owl_function_popless_text(perlout);
1747    } else if (type==2) {
1748      owl_function_adminmsg(buff, perlout);
1749    } else if (type==0) {
1750      return perlout;
1751    } else {
1752      owl_function_popless_text(perlout);
1753    }
1754    owl_free(perlout);
1755  }
1756  return NULL;
1757}
1758
1759
1760void owl_function_change_view(char *filtname) {
1761  owl_view *v;
1762  owl_filter *f;
1763  int curid=-1, newpos, curmsg;
1764  owl_message *curm=NULL;
1765
1766  v=owl_global_get_current_view(&g);
1767  curmsg=owl_global_get_curmsg(&g);
1768  if (curmsg==-1) {
1769    owl_function_debugmsg("Hit the curmsg==-1 case in change_view");
1770  } else {
1771    curm=owl_view_get_element(v, curmsg);
1772    if (curm) {
1773      curid=owl_message_get_id(curm);
1774      owl_view_save_curmsgid(v, curid);
1775    }
1776  }
1777
1778  /* grab the filter */;
1779  f=owl_global_get_filter(&g, filtname);
1780  if (!f) {
1781    owl_function_makemsg("Unknown filter");
1782    return;
1783  }
1784
1785  /* free the existing view and create a new one based on the filter */
1786  owl_view_free(v);
1787  owl_view_create(v, f);
1788
1789  /* Figure out what to set the current message to.
1790   * - If the view we're leaving has messages in it, go to the closest message
1791   *   to the last message pointed to in that view.
1792   * - If the view we're leaving is empty, try to restore the position
1793   *   from the last time we were in the new view.  */
1794  if (curm) {
1795    newpos = owl_view_get_nearest_to_msgid(v, curid);
1796  } else {
1797    newpos = owl_view_get_nearest_to_saved(v);
1798  }
1799
1800  owl_global_set_curmsg(&g, newpos);
1801
1802  owl_function_calculate_topmsg(OWL_DIRECTION_DOWNWARDS);
1803  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
1804  owl_global_set_direction_downwards(&g);
1805}
1806
1807void owl_function_create_filter(int argc, char **argv) {
1808  owl_filter *f;
1809  owl_view *v;
1810  int ret, inuse=0;
1811
1812  if (argc < 2) {
1813    owl_function_makemsg("Wrong number of arguments to filter command");
1814    return;
1815  }
1816
1817  v=owl_global_get_current_view(&g);
1818
1819  /* don't touch the all filter */
1820  if (!strcmp(argv[1], "all")) {
1821    owl_function_makemsg("You may not change the 'all' filter.");
1822    return;
1823  }
1824
1825  /* deal with the case of trying change the filter color */
1826  if (argc==4 && !strcmp(argv[2], "-c")) {
1827    f=owl_global_get_filter(&g, argv[1]);
1828    if (!f) {
1829      owl_function_makemsg("The filter '%s' does not exist.", argv[1]);
1830      return;
1831    }
1832    owl_filter_set_color(f, owl_util_string_to_color(argv[3]));
1833    owl_global_set_needrefresh(&g);
1834    owl_mainwin_redisplay(owl_global_get_mainwin(&g));
1835    return;
1836  }
1837
1838  /* create the filter and check for errors */
1839  f=owl_malloc(sizeof(owl_filter));
1840  ret=owl_filter_init(f, argv[1], argc-2, argv+2);
1841  if (ret==-1) {
1842    owl_free(f);
1843    owl_function_makemsg("Invalid filter syntax");
1844    return;
1845  }
1846
1847  /* if the named filter is in use by the current view, remember it */
1848  if (!strcmp(owl_view_get_filtname(v), argv[1])) {
1849    inuse=1;
1850  }
1851
1852  /* if the named filter already exists, nuke it */
1853  if (owl_global_get_filter(&g, argv[1])) {
1854    owl_global_remove_filter(&g, argv[1]);
1855  }
1856
1857  /* add the filter */
1858  owl_global_add_filter(&g, f);
1859
1860  /* if it was in use by the current view then update */
1861  if (inuse) {
1862    owl_function_change_view(argv[1]);
1863  }
1864  owl_global_set_needrefresh(&g);
1865  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
1866}
1867
1868void owl_function_show_filters() {
1869  owl_list *l;
1870  owl_filter *f;
1871  int i, j;
1872  owl_fmtext fm;
1873
1874  owl_fmtext_init_null(&fm);
1875
1876  l=owl_global_get_filterlist(&g);
1877  j=owl_list_get_size(l);
1878
1879  owl_fmtext_append_bold(&fm, "Filters:\n");
1880
1881  for (i=0; i<j; i++) {
1882    f=owl_list_get_element(l, i);
1883    owl_fmtext_append_normal(&fm, "   ");
1884    if (owl_global_get_hascolors(&g)) {
1885      owl_fmtext_append_normal_color(&fm, owl_filter_get_name(f), owl_filter_get_color(f));
1886    } else {
1887      owl_fmtext_append_normal(&fm, owl_filter_get_name(f));
1888    }
1889    owl_fmtext_append_normal(&fm, "\n");
1890  }
1891  owl_function_popless_fmtext(&fm);
1892  owl_fmtext_free(&fm);
1893}
1894
1895void owl_function_show_filter(char *name) {
1896  owl_filter *f;
1897  char buff[5000];
1898
1899  f=owl_global_get_filter(&g, name);
1900  if (!f) {
1901    owl_function_makemsg("There is no filter with that name");
1902    return;
1903  }
1904  owl_filter_print(f, buff);
1905  owl_function_popless_text(buff);
1906}
1907
1908void owl_function_show_zpunts() {
1909  owl_filter *f;
1910  owl_list *fl;
1911  char buff[5000];
1912  owl_fmtext fm;
1913  int i, j;
1914
1915  owl_fmtext_init_null(&fm);
1916
1917  fl=owl_global_get_puntlist(&g);
1918  j=owl_list_get_size(fl);
1919  owl_fmtext_append_bold(&fm, "Active zpunt filters:\n");
1920
1921  for (i=0; i<j; i++) {
1922    f=owl_list_get_element(fl, i);
1923    owl_filter_print(f, buff);
1924    owl_fmtext_append_normal(&fm, buff);
1925  }
1926  owl_function_popless_fmtext(&fm);
1927  owl_fmtext_free(&fm);
1928}
1929
1930char *owl_function_fastclassinstfilt(char *class, char *instance) {
1931  /* creates a filter for a class, instance if one doesn't exist.
1932   * If instance is null then apply for all messgaes in the class.
1933   * returns the name of the filter, which the caller must free.*/
1934  owl_list *fl;
1935  owl_filter *f;
1936  char *argbuff, *filtname;
1937  int len;
1938
1939  fl=owl_global_get_filterlist(&g);
1940
1941  /* name for the filter */
1942  len=strlen(class)+30;
1943  if (instance) len+=strlen(instance);
1944  filtname=owl_malloc(len);
1945  if (!instance) {
1946    sprintf(filtname, "class-%s", class);
1947  } else {
1948    sprintf(filtname, "class-%s-instance-%s", class, instance);
1949  }
1950  downstr(filtname);
1951
1952  /* if it already exists then go with it.  This lets users override */
1953  if (owl_global_get_filter(&g, filtname)) {
1954    return filtname;
1955  }
1956
1957  /* create the new filter */
1958  argbuff=owl_malloc(len+20);
1959  sprintf(argbuff, "( class ^%s$ )", class);
1960  if (instance) {
1961    sprintf(argbuff, "%s and ( instance ^%s$ )", argbuff, instance);
1962  }
1963
1964  f=owl_malloc(sizeof(owl_filter));
1965  owl_filter_init_fromstring(f, filtname, argbuff);
1966
1967  /* add it to the global list */
1968  owl_global_add_filter(&g, f);
1969
1970  owl_free(argbuff);
1971  return filtname;
1972}
1973
1974char *owl_function_fastuserfilt(char *user) {
1975  owl_filter *f;
1976  char *argbuff, *longuser, *shortuser, *filtname;
1977
1978  /* stick the local realm on if it's not there */
1979  longuser=long_zuser(user);
1980  shortuser=short_zuser(user);
1981
1982  /* name for the filter */
1983  filtname=owl_malloc(strlen(shortuser)+20);
1984  sprintf(filtname, "user-%s", shortuser);
1985
1986  /* if it already exists then go with it.  This lets users override */
1987  if (owl_global_get_filter(&g, filtname)) {
1988    return filtname;
1989  }
1990
1991  /* create the new-internal filter */
1992  f=owl_malloc(sizeof(owl_filter));
1993
1994  argbuff=owl_malloc(strlen(longuser)+1000);
1995  sprintf(argbuff, "( type ^zephyr$ and ( class ^message$ and instance ^personal$ and ");
1996  sprintf(argbuff, "%s ( ( direction ^in$ and sender ^%s$ ) or ( direction ^out$ and recipient ^%s$ ) ) )", argbuff, longuser, longuser);
1997  sprintf(argbuff, "%s or ( ( class ^login$ ) and ( sender ^%s$ ) ) )", argbuff, longuser);
1998
1999  owl_filter_init_fromstring(f, filtname, argbuff);
2000
2001  /* add it to the global list */
2002  owl_global_add_filter(&g, f);
2003
2004  /* free stuff */
2005  owl_free(argbuff);
2006  owl_free(longuser);
2007  owl_free(shortuser);
2008
2009  return filtname;
2010}
2011
2012char *owl_function_fasttypefilt(char *type) {
2013  owl_filter *f;
2014  char *argbuff, *filtname;
2015
2016  /* name for the filter */
2017  filtname=owl_sprintf("type-%s", type);
2018
2019  /* if it already exists then go with it.  This lets users override */
2020  if (owl_global_get_filter(&g, filtname)) {
2021    return filtname;
2022  }
2023
2024  /* create the new-internal filter */
2025  f=owl_malloc(sizeof(owl_filter));
2026
2027  argbuff = owl_sprintf("type ^%s$", type);
2028
2029  owl_filter_init_fromstring(f, filtname, argbuff);
2030
2031  /* add it to the global list */
2032  owl_global_add_filter(&g, f);
2033
2034  /* free stuff */
2035  owl_free(argbuff);
2036
2037  return filtname;
2038}
2039
2040/* If flag is 1, marks for deletion.  If flag is 0,
2041 * unmarks for deletion. */
2042void owl_function_delete_curview_msgs(int flag) {
2043  owl_view *v;
2044  int i, j;
2045
2046  v=owl_global_get_current_view(&g);
2047  j=owl_view_get_size(v);
2048  for (i=0; i<j; i++) {
2049    if (flag == 1) {
2050      owl_message_mark_delete(owl_view_get_element(v, i));
2051    } else if (flag == 0) {
2052      owl_message_unmark_delete(owl_view_get_element(v, i));
2053    }
2054  }
2055
2056  owl_function_makemsg("%i messages marked for %sdeletion", j, flag?"":"un");
2057
2058  owl_mainwin_redisplay(owl_global_get_mainwin(&g)); 
2059}
2060
2061char *owl_function_smartfilter(int type) {
2062  /* Returns the name of a filter, or null.  The caller
2063   * must free this name.  */
2064  /* if the curmsg is a personal message return a filter name
2065   *    to the converstaion with that user.
2066   * If the curmsg is a class message, instance foo, recip *
2067   *    message, return a filter name to the class, inst.
2068   * If the curmsg is a class message and type==0 then
2069   *    return a filter name for just the class.
2070   * If the curmsg is a class message and type==1 then
2071   *    return a filter name for the class and instance.
2072   */
2073  owl_view *v;
2074  owl_message *m;
2075  char *zperson, *filtname=NULL;
2076 
2077  v=owl_global_get_current_view(&g);
2078  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
2079
2080  if (!m || owl_view_get_size(v)==0) {
2081    owl_function_makemsg("No message selected\n");
2082    return(NULL);
2083  }
2084
2085  /* very simple handling of admin messages for now */
2086  if (owl_message_is_type_admin(m)) {
2087    return(owl_function_fasttypefilt("admin"));
2088  }
2089
2090  /* narrow personal and login messages to the sender or recip as appropriate */
2091  if (owl_message_is_personal(m) || owl_message_is_login(m)) {
2092    if (owl_message_is_type_zephyr(m)) {
2093      if (owl_message_is_direction_in(m)) {
2094        zperson=short_zuser(owl_message_get_sender(m));
2095      } else {
2096        zperson=short_zuser(owl_message_get_recipient(m));
2097      }
2098      filtname=owl_function_fastuserfilt(zperson);
2099      owl_free(zperson);
2100      return(filtname);
2101    }
2102    return(NULL);
2103  }
2104
2105  /* narrow class MESSAGE, instance foo, recip * messages to class, inst */
2106  if (!strcasecmp(owl_message_get_class(m), "message") &&
2107      !owl_message_is_personal(m)) {
2108    filtname=owl_function_fastclassinstfilt(owl_message_get_class(m), owl_message_get_instance(m));
2109    return(filtname);
2110  }
2111
2112  /* otherwise narrow to the class */
2113  if (type==0) {
2114    filtname=owl_function_fastclassinstfilt(owl_message_get_class(m), NULL);
2115  } else if (type==1) {
2116    filtname=owl_function_fastclassinstfilt(owl_message_get_class(m), owl_message_get_instance(m));
2117  }
2118  return(filtname);
2119}
2120
2121void owl_function_smartzpunt(int type) {
2122  /* Starts a zpunt command based on the current class,instance pair.
2123   * If type=0, uses just class.  If type=1, uses instance as well. */
2124  owl_view *v;
2125  owl_message *m;
2126  char *cmd, *cmdprefix, *mclass, *minst;
2127 
2128  v=owl_global_get_current_view(&g);
2129  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
2130
2131  if (!m || owl_view_get_size(v)==0) {
2132    owl_function_makemsg("No message selected\n");
2133    return;
2134  }
2135
2136  /* for now we skip admin messages. */
2137  if (owl_message_is_type_admin(m)
2138      || owl_message_is_login(m)
2139      || !owl_message_is_type_zephyr(m)) {
2140    owl_function_makemsg("smartzpunt doesn't support this message type.");
2141    return;
2142  }
2143
2144  mclass = owl_message_get_class(m);
2145  minst = owl_message_get_instance(m);
2146  if (!mclass || !*mclass || *mclass==' '
2147      || (!strcasecmp(mclass, "message") && !strcasecmp(minst, "personal"))
2148      || (type && (!minst || !*minst|| *minst==' '))) {
2149    owl_function_makemsg("smartzpunt can't safely do this for <%s,%s>",
2150                         mclass, minst);
2151  } else {
2152    cmdprefix = "start-command zpunt ";
2153    cmd = owl_malloc(strlen(cmdprefix)+strlen(mclass)+strlen(minst)+3);
2154    strcpy(cmd, cmdprefix);
2155    strcat(cmd, mclass);
2156    if (type) {
2157      strcat(cmd, " ");
2158      strcat(cmd, minst);
2159    } else {
2160      strcat(cmd, " *");
2161    }
2162    owl_function_command(cmd);
2163    owl_free(cmd);
2164  }
2165}
2166
2167
2168
2169void owl_function_color_current_filter(char *color) {
2170  owl_filter *f;
2171  char *name;
2172
2173  name=owl_view_get_filtname(owl_global_get_current_view(&g));
2174  f=owl_global_get_filter(&g, name);
2175  if (!f) {
2176    owl_function_makemsg("Unknown filter");
2177    return;
2178  }
2179
2180  /* don't touch the all filter */
2181  if (!strcmp(name, "all")) {
2182    owl_function_makemsg("You may not change the 'all' filter.");
2183    return;
2184  }
2185
2186  /* deal with the case of trying change the filter color */
2187  owl_filter_set_color(f, owl_util_string_to_color(color));
2188  owl_global_set_needrefresh(&g);
2189  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2190}
2191
2192void owl_function_show_colors() {
2193  owl_fmtext fm;
2194
2195  owl_fmtext_init_null(&fm);
2196  owl_fmtext_append_normal_color(&fm, "default\n", OWL_COLOR_DEFAULT);
2197  owl_fmtext_append_normal_color(&fm, "red\n", OWL_COLOR_RED);
2198  owl_fmtext_append_normal_color(&fm, "green\n", OWL_COLOR_GREEN);
2199  owl_fmtext_append_normal_color(&fm, "yellow\n", OWL_COLOR_YELLOW);
2200  owl_fmtext_append_normal_color(&fm, "blue\n", OWL_COLOR_BLUE);
2201  owl_fmtext_append_normal_color(&fm, "magenta\n", OWL_COLOR_MAGENTA);
2202  owl_fmtext_append_normal_color(&fm, "cyan\n", OWL_COLOR_CYAN);
2203  owl_fmtext_append_normal_color(&fm, "white\n", OWL_COLOR_WHITE);
2204
2205  owl_function_popless_fmtext(&fm);
2206  owl_fmtext_free(&fm);
2207}
2208
2209void owl_function_zpunt(char *class, char *inst, char *recip, int direction) {
2210  /* add the given class, inst, recip to the punt list for filtering.
2211   *   if direction==0 then punt
2212   *   if direction==1 then unpunt */
2213  owl_filter *f;
2214  owl_list *fl;
2215  char *buff;
2216  int ret, i, j;
2217
2218  fl=owl_global_get_puntlist(&g);
2219
2220  /* first, create the filter */
2221  f=malloc(sizeof(owl_filter));
2222  buff=malloc(strlen(class)+strlen(inst)+strlen(recip)+100);
2223  if (!strcmp(recip, "*")) {
2224    sprintf(buff, "class ^%s$ and instance ^%s$", class, inst);
2225  } else {
2226    sprintf(buff, "class ^%s$ and instance ^%s$ and recipient %s", class, inst, recip);
2227  }
2228  owl_function_debugmsg("About to filter %s", buff);
2229  ret=owl_filter_init_fromstring(f, "punt-filter", buff);
2230  owl_free(buff);
2231  if (ret) {
2232    owl_function_makemsg("Error creating filter for zpunt");
2233    owl_filter_free(f);
2234    return;
2235  }
2236
2237  /* Check for an identical filter */
2238  j=owl_list_get_size(fl);
2239  for (i=0; i<j; i++) {
2240    if (owl_filter_equiv(f, owl_list_get_element(fl, i))) {
2241      /* if we're punting, then just silently bow out on this duplicate */
2242      if (direction==0) {
2243        owl_filter_free(f);
2244        return;
2245      }
2246
2247      /* if we're unpunting, then remove this filter from the puntlist */
2248      if (direction==1) {
2249        owl_filter_free(owl_list_get_element(fl, i));
2250        owl_list_remove_element(fl, i);
2251        return;
2252      }
2253    }
2254  }
2255
2256  /* If we're punting, add the filter to the global punt list */
2257  if (direction==0) {
2258    owl_list_append_element(fl, f);
2259  }
2260}
2261
2262void owl_function_activate_keymap(char *keymap) {
2263  if (!owl_keyhandler_activate(owl_global_get_keyhandler(&g), keymap)) {
2264    owl_function_makemsg("Unable to activate keymap '%s'", keymap);
2265  }
2266}
2267
2268
2269void owl_function_show_keymaps() {
2270  owl_list l;
2271  owl_fmtext fm;
2272  owl_keymap *km;
2273  owl_keyhandler *kh;
2274  int i, numkm;
2275  char *kmname;
2276
2277  kh = owl_global_get_keyhandler(&g);
2278  owl_fmtext_init_null(&fm);
2279  owl_fmtext_append_bold(&fm, "Keymaps:   ");
2280  owl_fmtext_append_normal(&fm, "(use 'show keymap <name>' for details)\n");
2281  owl_keyhandler_get_keymap_names(kh, &l);
2282  owl_fmtext_append_list(&fm, &l, "\n", owl_function_keymap_summary);
2283  owl_fmtext_append_normal(&fm, "\n");
2284
2285  numkm = owl_list_get_size(&l);
2286  for (i=0; i<numkm; i++) {
2287    kmname = owl_list_get_element(&l, i);
2288    km = owl_keyhandler_get_keymap(kh, kmname);
2289    owl_fmtext_append_bold(&fm, "\n\n----------------------------------------------------------------------------------------------------\n\n");
2290    owl_keymap_get_details(km, &fm);   
2291  }
2292  owl_fmtext_append_normal(&fm, "\n");
2293 
2294  owl_function_popless_fmtext(&fm);
2295  owl_keyhandler_keymap_namelist_free(&l);
2296  owl_fmtext_free(&fm);
2297}
2298
2299char *owl_function_keymap_summary(void *name) {
2300  owl_keymap *km
2301    = owl_keyhandler_get_keymap(owl_global_get_keyhandler(&g), name);
2302  if (km) return owl_keymap_summary(km);
2303  else return(NULL);
2304}
2305
2306/* TODO: implement for real */
2307void owl_function_show_keymap(char *name) {
2308  owl_fmtext fm;
2309  owl_keymap *km;
2310
2311  owl_fmtext_init_null(&fm);
2312  km = owl_keyhandler_get_keymap(owl_global_get_keyhandler(&g), name);
2313  if (km) {
2314    owl_keymap_get_details(km, &fm);
2315  } else {
2316    owl_fmtext_append_normal(&fm, "No such keymap...\n");
2317  } 
2318  owl_function_popless_fmtext(&fm);
2319  owl_fmtext_free(&fm);
2320}
2321
2322void owl_function_help_for_command(char *cmdname) {
2323  owl_fmtext fm;
2324
2325  owl_fmtext_init_null(&fm);
2326  owl_cmd_get_help(owl_global_get_cmddict(&g), cmdname, &fm);
2327  owl_function_popless_fmtext(&fm); 
2328  owl_fmtext_free(&fm);
2329}
2330
2331void owl_function_search_start(char *string, int direction) {
2332  /* direction is OWL_DIRECTION_DOWNWARDS or OWL_DIRECTION_UPWARDS */
2333  owl_global_set_search_active(&g, string);
2334  owl_function_search_helper(0, direction);
2335}
2336
2337void owl_function_search_continue(int direction) {
2338  /* direction is OWL_DIRECTION_DOWNWARDS or OWL_DIRECTION_UPWARDS */
2339  owl_function_search_helper(1, direction);
2340}
2341
2342void owl_function_search_helper(int mode, int direction) {
2343  /* move to a message that contains the string.  If direction is
2344   * OWL_DIRECTION_DOWNWARDS then search fowards, if direction is
2345   * OWL_DIRECTION_UPWARDS then search backwards.
2346   *
2347   * If mode==0 then it will stay on the current message if it
2348   * contains the string.
2349   */
2350
2351  owl_view *v;
2352  int viewsize, i, curmsg, start;
2353  owl_message *m;
2354
2355  v=owl_global_get_current_view(&g);
2356  viewsize=owl_view_get_size(v);
2357  curmsg=owl_global_get_curmsg(&g);
2358 
2359  if (viewsize==0) {
2360    owl_function_makemsg("No messages present");
2361    return;
2362  }
2363
2364  if (mode==0) {
2365    start=curmsg;
2366  } else if (direction==OWL_DIRECTION_DOWNWARDS) {
2367    start=curmsg+1;
2368  } else {
2369    start=curmsg-1;
2370  }
2371
2372  /* bounds check */
2373  if (start>=viewsize || start<0) {
2374    owl_function_makemsg("No further matches found");
2375    return;
2376  }
2377
2378  for (i=start; i<viewsize && i>=0;) {
2379    m=owl_view_get_element(v, i);
2380    if (owl_message_search(m, owl_global_get_search_string(&g))) {
2381      owl_global_set_curmsg(&g, i);
2382      owl_function_calculate_topmsg(direction);
2383      owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2384      if (direction==OWL_DIRECTION_DOWNWARDS) {
2385        owl_global_set_direction_downwards(&g);
2386      } else {
2387        owl_global_set_direction_upwards(&g);
2388      }
2389      return;
2390    }
2391    if (direction==OWL_DIRECTION_DOWNWARDS) {
2392      i++;
2393    } else {
2394      i--;
2395    }
2396  }
2397  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2398  owl_function_makemsg("No matches found");
2399}
2400
2401
2402/* strips formatting from ztext and returns the unformatted text.
2403 * caller is responsible for freeing. */
2404char *owl_function_ztext_stylestrip(char *zt) {
2405  owl_fmtext fm;
2406  char *plaintext;
2407
2408  owl_fmtext_init_null(&fm);
2409  owl_fmtext_append_ztext(&fm, zt);
2410  plaintext = owl_fmtext_print_plain(&fm);
2411  owl_fmtext_free(&fm);
2412  return(plaintext);
2413}
2414
2415/* popup a znol listing.  If file is NULL use the default .anyone */
2416/* this doesn't obey 'elapsed' or 'timesort' yet */
2417void owl_function_zlist(char *file, int elapsed, int timesort) {
2418  char *ourfile, *tmp, buff[LINE], *line;
2419  FILE *f;
2420  int numlocs, ret, i;
2421  ZLocations_t location[200];
2422  owl_fmtext fm;
2423
2424  if (file==NULL) {
2425    tmp=owl_global_get_homedir(&g);
2426    if (!tmp) {
2427      owl_function_makemsg("Could not determine home directory");
2428      return;
2429    }
2430    ourfile=owl_malloc(strlen(tmp)+50);
2431    sprintf(ourfile, "%s/.anyone", owl_global_get_homedir(&g));
2432  } else {
2433    ourfile=owl_strdup(file);
2434  }
2435
2436  f=fopen(ourfile, "r");
2437  if (!f) {
2438    owl_function_makemsg("Error opening file %s", ourfile);
2439    return;
2440  }
2441
2442  owl_fmtext_init_null(&fm);
2443   
2444  while (fgets(buff, LINE, f)!=NULL) {
2445    /* ignore comments, blank lines etc. */
2446    if (buff[0]=='#') continue;
2447    if (buff[0]=='\n') continue;
2448    if (buff[0]=='\0') continue;
2449
2450    /* strip the \n */
2451    buff[strlen(buff)-1]='\0';
2452
2453    /* ingore from # on */
2454    tmp=strchr(buff, '#');
2455    if (tmp) tmp[0]='\0';
2456
2457    /* ingore from SPC */
2458    tmp=strchr(buff, ' ');
2459    if (tmp) tmp[0]='\0';
2460
2461    /* stick on the local realm. */
2462    if (!strchr(buff, '@')) {
2463      strcat(buff, "@");
2464      strcat(buff, ZGetRealm());
2465    }
2466
2467    ret=ZLocateUser(buff, &numlocs, ZAUTH);
2468    if (ret!=ZERR_NONE) {
2469      owl_function_makemsg("Error getting location for %s", buff);
2470      continue;
2471    }
2472
2473    numlocs=200;
2474    ret=ZGetLocations(location, &numlocs);
2475    if (ret==0) {
2476      for (i=0; i<numlocs; i++) {
2477        line=malloc(strlen(location[i].host)+strlen(location[i].time)+strlen(location[i].tty)+100);
2478        tmp=short_zuser(buff);
2479        sprintf(line, "%-10.10s %-24.24s %-12.12s  %20.20s\n",
2480                tmp,
2481                location[i].host,
2482                location[i].tty,
2483                location[i].time);
2484        owl_fmtext_append_normal(&fm, line);
2485        owl_free(tmp);
2486      }
2487      if (numlocs>=200) {
2488        owl_fmtext_append_normal(&fm, "Too many locations found for this user, truncating.\n");
2489      }
2490    }
2491  }
2492  fclose(f);
2493
2494  owl_function_popless_fmtext(&fm);
2495  owl_fmtext_free(&fm);
2496
2497  owl_free(ourfile);
2498}
2499
2500void owl_function_dump(char *filename) {
2501  int i, j, count;
2502  owl_message *m;
2503  owl_view *v;
2504  FILE *file;
2505  /* struct stat sbuf; */
2506
2507  v=owl_global_get_current_view(&g);
2508
2509  /* in the future make it ask yes/no */
2510  /*
2511  ret=stat(filename, &sbuf);
2512  if (!ret) {
2513    ret=owl_function_askyesno("File exists, continue? [Y/n]");
2514    if (!ret) return;
2515  }
2516  */
2517
2518  file=fopen(filename, "w");
2519  if (!file) {
2520    owl_function_makemsg("Error opening file");
2521    return;
2522  }
2523
2524  count=0;
2525  j=owl_view_get_size(v);
2526  for (i=0; i<j; i++) {
2527    m=owl_view_get_element(v, i);
2528    fputs(owl_message_get_text(m), file);
2529  }
2530  fclose(file);
2531}
2532
2533
2534
2535void owl_function_do_newmsgproc() {
2536  if (owl_global_get_newmsgproc(&g) && strcmp(owl_global_get_newmsgproc(&g), "")) {
2537    /* if there's a process out there, we need to check on it */
2538    if (owl_global_get_newmsgproc_pid(&g)) {
2539      owl_function_debugmsg("Checking on newmsgproc pid==%i", owl_global_get_newmsgproc_pid(&g));
2540      owl_function_debugmsg("Waitpid return is %i", waitpid(owl_global_get_newmsgproc_pid(&g), NULL, WNOHANG));
2541      waitpid(owl_global_get_newmsgproc_pid(&g), NULL, WNOHANG);
2542      if (waitpid(owl_global_get_newmsgproc_pid(&g), NULL, WNOHANG)==-1) {
2543        /* it exited */
2544        owl_global_set_newmsgproc_pid(&g, 0);
2545        owl_function_debugmsg("newmsgproc exited");
2546      } else {
2547        owl_function_debugmsg("newmsgproc did not exit");
2548      }
2549    }
2550   
2551    /* if it exited, fork & exec a new one */
2552    if (owl_global_get_newmsgproc_pid(&g)==0) {
2553      int i, myargc;
2554      i=fork();
2555      if (i) {
2556        /* parent set the child's pid */
2557        owl_global_set_newmsgproc_pid(&g, i);
2558        waitpid(i, NULL, WNOHANG);
2559        owl_function_debugmsg("I'm the parent and I started a new newmsgproc with pid %i", i);
2560      } else {
2561        /* child exec's the program */
2562        char **parsed;
2563        parsed=owl_parseline(owl_global_get_newmsgproc(&g), &myargc);
2564        parsed=realloc(parsed, strlen(owl_global_get_newmsgproc(&g)+300));
2565        parsed[myargc]=(char *) NULL;
2566       
2567        owl_function_debugmsg("About to exec: %s with %i arguments", parsed[0], myargc);
2568       
2569        execvp(parsed[0], (char **) parsed);
2570       
2571       
2572        /* was there an error exec'ing? */
2573        owl_function_debugmsg("Error execing: %s", strerror(errno));
2574        _exit(127);
2575      }
2576    }
2577  }
2578}
Note: See TracBrowser for help on using the repository browser.