source: functions.c @ 2e6d137

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