source: functions.c @ ecd5dc5

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