source: functions.c @ 8262340

barnowl_perlaimdebianowlrelease-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 8262340 was 8262340, checked in by James M. Kretchmar <kretch@mit.edu>, 18 years ago
Added ZResetAuthentication in a number of places to fix problems with stale tickets Added some hooks for malloc debugging
  • Property mode set to 100644
File size: 70.4 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  sprintf(buff, "%sMemory Malloced: %i\n", buff, owl_global_get_malloced(&g));
1538  sprintf(buff, "%sMemory Freed: %i\n", buff, owl_global_get_freed(&g));
1539  sprintf(buff, "%sMemory In Use: %i\n", buff, owl_global_get_meminuse(&g));
1540
1541  owl_function_popless_text(buff);
1542}
1543
1544void owl_function_show_term() {
1545  owl_fmtext fm;
1546  char buff[LINE];
1547
1548  owl_fmtext_init_null(&fm);
1549  sprintf(buff, "Terminal Lines: %i\nTerminal Columns: %i\n",
1550          owl_global_get_lines(&g),
1551          owl_global_get_cols(&g));
1552  owl_fmtext_append_normal(&fm, buff);
1553
1554  if (owl_global_get_hascolors(&g)) {
1555    owl_fmtext_append_normal(&fm, "Color: Yes\n");
1556    sprintf(buff, "Number of color pairs: %i\n", owl_global_get_colorpairs(&g));
1557    owl_fmtext_append_normal(&fm, buff);
1558    sprintf(buff, "Can change colors: %s\n", can_change_color() ? "yes" : "no");
1559    owl_fmtext_append_normal(&fm, buff);
1560  } else {
1561    owl_fmtext_append_normal(&fm, "Color: No\n");
1562  }
1563
1564  owl_function_popless_fmtext(&fm);
1565  owl_fmtext_free(&fm);
1566}
1567
1568
1569void owl_function_reply(int type, int enter) {
1570  /* if type = 0 then normal reply.
1571   * if type = 1 then it's a reply to sender
1572   * if enter = 0 then allow the command to be edited
1573   * if enter = 1 then don't wait for editing
1574   */
1575  char *buff, *oldbuff;
1576  owl_message *m;
1577  owl_filter *f;
1578 
1579  if (owl_view_get_size(owl_global_get_current_view(&g))==0) {
1580    owl_function_makemsg("No message selected");
1581  } else {
1582    char *class, *inst, *to, *cc=NULL;
1583   
1584    m=owl_view_get_element(owl_global_get_current_view(&g), owl_global_get_curmsg(&g));
1585    if (!m) {
1586      owl_function_makemsg("No message selected");
1587      return;
1588    }
1589
1590    /* first check if we catch the reply-lockout filter */
1591    f=owl_global_get_filter(&g, "reply-lockout");
1592    if (f) {
1593      if (owl_filter_message_match(f, m)) {
1594        owl_function_makemsg("Sorry, replies to this message have been disabled by the reply-lockout filter");
1595        return;
1596      }
1597    }
1598
1599    /* for now we disable replies to zcrypt messages, since we can't
1600       support an encrypted reply */
1601    if (!strcasecmp(owl_message_get_opcode(m), "crypt")) {
1602      owl_function_makemsg("Replies to zcrypt messages are not enabled in this release");
1603      return;
1604    }
1605
1606    if (owl_message_is_direction_out(m)) {
1607      owl_function_zwrite_setup(owl_message_get_zwriteline(m));
1608      owl_global_set_buffercommand(&g, owl_message_get_zwriteline(m));
1609    } else if (owl_message_is_type_admin(m)) {
1610      owl_function_makemsg("You cannot reply to an admin message");
1611    } else {
1612      if (owl_message_is_login(m)) {
1613        class="MESSAGE";
1614        inst="PERSONAL";
1615        to=owl_message_get_sender(m);
1616      } else if (type==1) {
1617        class="MESSAGE";
1618        inst="PERSONAL";
1619        to=owl_message_get_sender(m);
1620      } else {
1621        class=owl_message_get_class(m);
1622        inst=owl_message_get_instance(m);
1623        to=owl_message_get_recipient(m);
1624        cc=owl_message_get_cc(m);
1625        if (!strcmp(to, "") || !strcmp(to, "*")) {
1626          to="";
1627        } else if (to[0]=='@') {
1628          /* leave it, to get the realm */
1629        } else {
1630          to=owl_message_get_sender(m);
1631        }
1632      }
1633     
1634      /* create the command line */
1635      buff = owl_strdup("zwrite");
1636      if (strcasecmp(class, "message")) {
1637        buff = owl_sprintf("%s -c %s%s%s", oldbuff=buff, owl_getquoting(class), class, owl_getquoting(class));
1638        owl_free(oldbuff);
1639      }
1640      if (strcasecmp(inst, "personal")) {
1641        buff = owl_sprintf("%s -i %s%s%s", oldbuff=buff, owl_getquoting(inst), inst, owl_getquoting(inst));
1642        owl_free(oldbuff);
1643      }
1644      if (*to != '\0') {
1645        char *tmp, *oldtmp, *tmp2;
1646        tmp=short_zuser(to);
1647        if (cc) {
1648          tmp = owl_util_uniq(oldtmp=tmp, cc, "-");
1649          owl_free(oldtmp);
1650          buff = owl_sprintf("%s -C %s", oldbuff=buff, tmp);
1651          owl_free(oldbuff);
1652        } else {
1653          if (owl_global_is_smartstrip(&g)) {
1654            tmp2=tmp;
1655            tmp=owl_util_smartstripped_user(tmp2);
1656            owl_free(tmp2);
1657          }
1658          buff = owl_sprintf("%s %s", oldbuff=buff, tmp);
1659          owl_free(oldbuff);
1660        }
1661        owl_free(tmp);
1662      }
1663      if (cc) owl_free(cc);
1664
1665      if (enter) {
1666        owl_history *hist = owl_global_get_cmd_history(&g);
1667        owl_history_store(hist, buff);
1668        owl_history_reset(hist);
1669        owl_function_command_norv(buff);
1670      } else {
1671        owl_function_start_command(buff);
1672      }
1673      owl_free(buff);
1674    }
1675  }
1676}
1677
1678void owl_function_zlocate(int argc, char **argv, int auth) {
1679  owl_fmtext fm;
1680  char *ptr, buff[LINE];
1681  int i;
1682
1683  owl_fmtext_init_null(&fm);
1684
1685  for (i=0; i<argc; i++) {
1686    ptr=long_zuser(argv[i]);
1687    owl_zephyr_zlocate(ptr, buff, auth);
1688    owl_fmtext_append_normal(&fm, buff);
1689    owl_free(ptr);
1690  }
1691
1692  owl_function_popless_fmtext(&fm);
1693  owl_fmtext_free(&fm);
1694}
1695
1696void owl_function_start_command(char *line) {
1697  int i, j;
1698  owl_editwin *tw;
1699
1700  tw=owl_global_get_typwin(&g);
1701  owl_global_set_typwin_active(&g);
1702  owl_editwin_new_style(tw, OWL_EDITWIN_STYLE_ONELINE, 
1703                        owl_global_get_cmd_history(&g));
1704
1705  owl_editwin_set_locktext(tw, "command: ");
1706  owl_global_set_needrefresh(&g);
1707
1708  j=strlen(line);
1709  for (i=0; i<j; i++) {
1710    owl_editwin_process_char(tw, line[i]);
1711  }
1712  owl_editwin_redisplay(tw, 0);
1713}
1714
1715char *owl_function_exec(int argc, char **argv, char *buff, int type) {
1716  /* if type == 1 display in a popup
1717   * if type == 2 display an admin messages
1718   * if type == 0 return output
1719   * else display in a popup
1720   */
1721  char *newbuff, *redirect = " 2>&1 < /dev/null";
1722  char *out, buff2[1024];
1723  int size;
1724  FILE *p;
1725
1726  if (argc<2) {
1727    owl_function_makemsg("Wrong number of arguments to the exec command");
1728    return NULL;
1729  }
1730
1731  buff = skiptokens(buff, 1);
1732  newbuff = owl_malloc(strlen(buff)+strlen(redirect)+1);
1733  strcpy(newbuff, buff);
1734  strcat(newbuff, redirect);
1735
1736  p=popen(newbuff, "r");
1737  out=owl_malloc(1024);
1738  size=1024;
1739  strcpy(out, "");
1740  while (fgets(buff2, 1024, p)!=NULL) {
1741    size+=1024;
1742    out=owl_realloc(out, size);
1743    strcat(out, buff2);
1744  }
1745  pclose(p);
1746
1747  if (type==1) {
1748    owl_function_popless_text(out);
1749  } else if (type==0) {
1750    return out;
1751  } else if (type==2) {
1752    owl_function_adminmsg(buff, out);
1753  } else {
1754    owl_function_popless_text(out);
1755  }
1756  owl_free(out);
1757  return NULL;
1758}
1759
1760
1761char *owl_function_perl(int argc, char **argv, char *buff, int type) {
1762  /* if type == 1 display in a popup
1763   * if type == 2 display an admin messages
1764   * if type == 0 return output
1765   * else display in a popup
1766   */
1767  char *perlout;
1768
1769  if (argc<2) {
1770    owl_function_makemsg("Wrong number of arguments to perl command");
1771    return NULL;
1772  }
1773
1774  /* consume first token (argv[0]) */
1775  buff = skiptokens(buff, 1);
1776
1777  perlout = owl_config_execute(buff);
1778  if (perlout) { 
1779    if (type==1) {
1780      owl_function_popless_text(perlout);
1781    } else if (type==2) {
1782      owl_function_adminmsg(buff, perlout);
1783    } else if (type==0) {
1784      return perlout;
1785    } else {
1786      owl_function_popless_text(perlout);
1787    }
1788    owl_free(perlout);
1789  }
1790  return NULL;
1791}
1792
1793
1794void owl_function_change_view(char *filtname) {
1795  owl_view *v;
1796  owl_filter *f;
1797  int curid=-1, newpos, curmsg;
1798  owl_message *curm=NULL;
1799
1800  v=owl_global_get_current_view(&g);
1801  curmsg=owl_global_get_curmsg(&g);
1802  if (curmsg==-1) {
1803    owl_function_debugmsg("Hit the curmsg==-1 case in change_view");
1804  } else {
1805    curm=owl_view_get_element(v, curmsg);
1806    if (curm) {
1807      curid=owl_message_get_id(curm);
1808      owl_view_save_curmsgid(v, curid);
1809    }
1810  }
1811
1812  /* grab the filter */;
1813  f=owl_global_get_filter(&g, filtname);
1814  if (!f) {
1815    owl_function_makemsg("Unknown filter");
1816    return;
1817  }
1818
1819  /* free the existing view and create a new one based on the filter */
1820  owl_view_free(v);
1821  owl_view_create(v, f);
1822
1823  /* Figure out what to set the current message to.
1824   * - If the view we're leaving has messages in it, go to the closest message
1825   *   to the last message pointed to in that view.
1826   * - If the view we're leaving is empty, try to restore the position
1827   *   from the last time we were in the new view.  */
1828  if (curm) {
1829    newpos = owl_view_get_nearest_to_msgid(v, curid);
1830  } else {
1831    newpos = owl_view_get_nearest_to_saved(v);
1832  }
1833
1834  owl_global_set_curmsg(&g, newpos);
1835
1836  owl_function_calculate_topmsg(OWL_DIRECTION_DOWNWARDS);
1837  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
1838  owl_global_set_direction_downwards(&g);
1839}
1840
1841void owl_function_create_filter(int argc, char **argv) {
1842  owl_filter *f;
1843  owl_view *v;
1844  int ret, inuse=0;
1845
1846  if (argc < 2) {
1847    owl_function_makemsg("Wrong number of arguments to filter command");
1848    return;
1849  }
1850
1851  v=owl_global_get_current_view(&g);
1852
1853  /* don't touch the all filter */
1854  if (!strcmp(argv[1], "all")) {
1855    owl_function_makemsg("You may not change the 'all' filter.");
1856    return;
1857  }
1858
1859  /* deal with the case of trying change the filter color */
1860  if (argc==4 && !strcmp(argv[2], "-c")) {
1861    f=owl_global_get_filter(&g, argv[1]);
1862    if (!f) {
1863      owl_function_makemsg("The filter '%s' does not exist.", argv[1]);
1864      return;
1865    }
1866    owl_filter_set_color(f, owl_util_string_to_color(argv[3]));
1867    owl_global_set_needrefresh(&g);
1868    owl_mainwin_redisplay(owl_global_get_mainwin(&g));
1869    return;
1870  }
1871
1872  /* create the filter and check for errors */
1873  f=owl_malloc(sizeof(owl_filter));
1874  ret=owl_filter_init(f, argv[1], argc-2, argv+2);
1875  if (ret==-1) {
1876    owl_free(f);
1877    owl_function_makemsg("Invalid filter syntax");
1878    return;
1879  }
1880
1881  /* if the named filter is in use by the current view, remember it */
1882  if (!strcmp(owl_view_get_filtname(v), argv[1])) {
1883    inuse=1;
1884  }
1885
1886  /* if the named filter already exists, nuke it */
1887  if (owl_global_get_filter(&g, argv[1])) {
1888    owl_global_remove_filter(&g, argv[1]);
1889  }
1890
1891  /* add the filter */
1892  owl_global_add_filter(&g, f);
1893
1894  /* if it was in use by the current view then update */
1895  if (inuse) {
1896    owl_function_change_view(argv[1]);
1897  }
1898  owl_global_set_needrefresh(&g);
1899  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
1900}
1901
1902void owl_function_show_filters() {
1903  owl_list *l;
1904  owl_filter *f;
1905  int i, j;
1906  owl_fmtext fm;
1907
1908  owl_fmtext_init_null(&fm);
1909
1910  l=owl_global_get_filterlist(&g);
1911  j=owl_list_get_size(l);
1912
1913  owl_fmtext_append_bold(&fm, "Filters:\n");
1914
1915  for (i=0; i<j; i++) {
1916    f=owl_list_get_element(l, i);
1917    owl_fmtext_append_normal(&fm, "   ");
1918    if (owl_global_get_hascolors(&g)) {
1919      owl_fmtext_append_normal_color(&fm, owl_filter_get_name(f), owl_filter_get_color(f));
1920    } else {
1921      owl_fmtext_append_normal(&fm, owl_filter_get_name(f));
1922    }
1923    owl_fmtext_append_normal(&fm, "\n");
1924  }
1925  owl_function_popless_fmtext(&fm);
1926  owl_fmtext_free(&fm);
1927}
1928
1929void owl_function_show_filter(char *name) {
1930  owl_filter *f;
1931  char buff[5000];
1932
1933  f=owl_global_get_filter(&g, name);
1934  if (!f) {
1935    owl_function_makemsg("There is no filter with that name");
1936    return;
1937  }
1938  owl_filter_print(f, buff);
1939  owl_function_popless_text(buff);
1940}
1941
1942void owl_function_show_zpunts() {
1943  owl_filter *f;
1944  owl_list *fl;
1945  char buff[5000];
1946  owl_fmtext fm;
1947  int i, j;
1948
1949  owl_fmtext_init_null(&fm);
1950
1951  fl=owl_global_get_puntlist(&g);
1952  j=owl_list_get_size(fl);
1953  owl_fmtext_append_bold(&fm, "Active zpunt filters:\n");
1954
1955  for (i=0; i<j; i++) {
1956    f=owl_list_get_element(fl, i);
1957    owl_filter_print(f, buff);
1958    owl_fmtext_append_normal(&fm, buff);
1959  }
1960  owl_function_popless_fmtext(&fm);
1961  owl_fmtext_free(&fm);
1962}
1963
1964char *owl_function_fastclassinstfilt(char *class, char *instance) {
1965  /* creates a filter for a class, instance if one doesn't exist.
1966   * If instance is null then apply for all messgaes in the class.
1967   * returns the name of the filter, which the caller must free.*/
1968  owl_list *fl;
1969  owl_filter *f;
1970  char *argbuff, *filtname;
1971  char *tmpclass, *tmpinstance;
1972  int len;
1973
1974  fl=owl_global_get_filterlist(&g);
1975
1976  /* name for the filter */
1977  len=strlen(class)+30;
1978  if (instance) len+=strlen(instance);
1979  filtname=owl_malloc(len);
1980  if (!instance) {
1981    sprintf(filtname, "class-%s", class);
1982  } else {
1983    sprintf(filtname, "class-%s-instance-%s", class, instance);
1984  }
1985  /* downcase it */
1986  downstr(filtname);
1987  /* turn spaces into hyphens */
1988  owl_util_tr(filtname, ' ', '.');
1989 
1990  /* if it already exists then go with it.  This lets users override */
1991  if (owl_global_get_filter(&g, filtname)) {
1992    return(filtname);
1993  }
1994
1995  /* create the new filter */
1996  argbuff=owl_malloc(len+20);
1997  tmpclass=owl_strdup(class);
1998  owl_util_tr(tmpclass, ' ', '.');
1999  if (instance) {
2000    tmpinstance=owl_strdup(instance);
2001    owl_util_tr(tmpinstance, ' ', '.');
2002  }
2003  sprintf(argbuff, "( class ^%s$ )", tmpclass);
2004  if (instance) {
2005    sprintf(argbuff, "%s and ( instance ^%s$ )", argbuff, tmpinstance);
2006  }
2007  owl_free(tmpclass);
2008  if (instance) owl_free(tmpinstance);
2009
2010  f=owl_malloc(sizeof(owl_filter));
2011  owl_filter_init_fromstring(f, filtname, argbuff);
2012
2013  /* add it to the global list */
2014  owl_global_add_filter(&g, f);
2015
2016  owl_free(argbuff);
2017  return(filtname);
2018}
2019
2020char *owl_function_fastuserfilt(char *user) {
2021  owl_filter *f;
2022  char *argbuff, *longuser, *shortuser, *filtname;
2023
2024  /* stick the local realm on if it's not there */
2025  longuser=long_zuser(user);
2026  shortuser=short_zuser(user);
2027
2028  /* name for the filter */
2029  filtname=owl_malloc(strlen(shortuser)+20);
2030  sprintf(filtname, "user-%s", shortuser);
2031
2032  /* if it already exists then go with it.  This lets users override */
2033  if (owl_global_get_filter(&g, filtname)) {
2034    return(filtname);
2035  }
2036
2037  /* create the new-internal filter */
2038  f=owl_malloc(sizeof(owl_filter));
2039
2040  argbuff=owl_malloc(strlen(longuser)+1000);
2041  sprintf(argbuff, "( type ^zephyr$ and ( class ^message$ and instance ^personal$ and ");
2042  sprintf(argbuff, "%s ( ( direction ^in$ and sender ^%s$ ) or ( direction ^out$ and recipient ^%s$ ) ) )", argbuff, longuser, longuser);
2043  sprintf(argbuff, "%s or ( ( class ^login$ ) and ( sender ^%s$ ) ) )", argbuff, longuser);
2044
2045  owl_filter_init_fromstring(f, filtname, argbuff);
2046
2047  /* add it to the global list */
2048  owl_global_add_filter(&g, f);
2049
2050  /* free stuff */
2051  owl_free(argbuff);
2052  owl_free(longuser);
2053  owl_free(shortuser);
2054
2055  return(filtname);
2056}
2057
2058char *owl_function_fasttypefilt(char *type) {
2059  owl_filter *f;
2060  char *argbuff, *filtname;
2061
2062  /* name for the filter */
2063  filtname=owl_sprintf("type-%s", type);
2064
2065  /* if it already exists then go with it.  This lets users override */
2066  if (owl_global_get_filter(&g, filtname)) {
2067    return filtname;
2068  }
2069
2070  /* create the new-internal filter */
2071  f=owl_malloc(sizeof(owl_filter));
2072
2073  argbuff = owl_sprintf("type ^%s$", type);
2074
2075  owl_filter_init_fromstring(f, filtname, argbuff);
2076
2077  /* add it to the global list */
2078  owl_global_add_filter(&g, f);
2079
2080  /* free stuff */
2081  owl_free(argbuff);
2082
2083  return filtname;
2084}
2085
2086/* If flag is 1, marks for deletion.  If flag is 0,
2087 * unmarks for deletion. */
2088void owl_function_delete_curview_msgs(int flag) {
2089  owl_view *v;
2090  int i, j;
2091
2092  v=owl_global_get_current_view(&g);
2093  j=owl_view_get_size(v);
2094  for (i=0; i<j; i++) {
2095    if (flag == 1) {
2096      owl_message_mark_delete(owl_view_get_element(v, i));
2097    } else if (flag == 0) {
2098      owl_message_unmark_delete(owl_view_get_element(v, i));
2099    }
2100  }
2101
2102  owl_function_makemsg("%i messages marked for %sdeletion", j, flag?"":"un");
2103
2104  owl_mainwin_redisplay(owl_global_get_mainwin(&g)); 
2105}
2106
2107char *owl_function_smartfilter(int type) {
2108  /* Returns the name of a filter, or null.  The caller
2109   * must free this name.  */
2110  /* if the curmsg is a personal message return a filter name
2111   *    to the converstaion with that user.
2112   * If the curmsg is a class message, instance foo, recip *
2113   *    message, return a filter name to the class, inst.
2114   * If the curmsg is a class message and type==0 then
2115   *    return a filter name for just the class.
2116   * If the curmsg is a class message and type==1 then
2117   *    return a filter name for the class and instance.
2118   */
2119  owl_view *v;
2120  owl_message *m;
2121  char *zperson, *filtname=NULL;
2122 
2123  v=owl_global_get_current_view(&g);
2124  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
2125
2126  if (!m || owl_view_get_size(v)==0) {
2127    owl_function_makemsg("No message selected\n");
2128    return(NULL);
2129  }
2130
2131  /* very simple handling of admin messages for now */
2132  if (owl_message_is_type_admin(m)) {
2133    return(owl_function_fasttypefilt("admin"));
2134  }
2135
2136  /* narrow personal and login messages to the sender or recip as appropriate */
2137  if (owl_message_is_personal(m) || owl_message_is_login(m)) {
2138    if (owl_message_is_type_zephyr(m)) {
2139      if (owl_message_is_direction_in(m)) {
2140        zperson=short_zuser(owl_message_get_sender(m));
2141      } else {
2142        zperson=short_zuser(owl_message_get_recipient(m));
2143      }
2144      filtname=owl_function_fastuserfilt(zperson);
2145      owl_free(zperson);
2146      return(filtname);
2147    }
2148    return(NULL);
2149  }
2150
2151  /* narrow class MESSAGE, instance foo, recip * messages to class, inst */
2152  if (!strcasecmp(owl_message_get_class(m), "message") &&
2153      !owl_message_is_personal(m)) {
2154    filtname=owl_function_fastclassinstfilt(owl_message_get_class(m), owl_message_get_instance(m));
2155    return(filtname);
2156  }
2157
2158  /* otherwise narrow to the class */
2159  if (type==0) {
2160    filtname=owl_function_fastclassinstfilt(owl_message_get_class(m), NULL);
2161  } else if (type==1) {
2162    filtname=owl_function_fastclassinstfilt(owl_message_get_class(m), owl_message_get_instance(m));
2163  }
2164  return(filtname);
2165}
2166
2167void owl_function_smartzpunt(int type) {
2168  /* Starts a zpunt command based on the current class,instance pair.
2169   * If type=0, uses just class.  If type=1, uses instance as well. */
2170  owl_view *v;
2171  owl_message *m;
2172  char *cmd, *cmdprefix, *mclass, *minst;
2173 
2174  v=owl_global_get_current_view(&g);
2175  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
2176
2177  if (!m || owl_view_get_size(v)==0) {
2178    owl_function_makemsg("No message selected\n");
2179    return;
2180  }
2181
2182  /* for now we skip admin messages. */
2183  if (owl_message_is_type_admin(m)
2184      || owl_message_is_login(m)
2185      || !owl_message_is_type_zephyr(m)) {
2186    owl_function_makemsg("smartzpunt doesn't support this message type.");
2187    return;
2188  }
2189
2190  mclass = owl_message_get_class(m);
2191  minst = owl_message_get_instance(m);
2192  if (!mclass || !*mclass || *mclass==' '
2193      || (!strcasecmp(mclass, "message") && !strcasecmp(minst, "personal"))
2194      || (type && (!minst || !*minst|| *minst==' '))) {
2195    owl_function_makemsg("smartzpunt can't safely do this for <%s,%s>",
2196                         mclass, minst);
2197  } else {
2198    cmdprefix = "start-command zpunt ";
2199    cmd = owl_malloc(strlen(cmdprefix)+strlen(mclass)+strlen(minst)+3);
2200    strcpy(cmd, cmdprefix);
2201    strcat(cmd, mclass);
2202    if (type) {
2203      strcat(cmd, " ");
2204      strcat(cmd, minst);
2205    } else {
2206      strcat(cmd, " *");
2207    }
2208    owl_function_command(cmd);
2209    owl_free(cmd);
2210  }
2211}
2212
2213
2214
2215void owl_function_color_current_filter(char *color) {
2216  owl_filter *f;
2217  char *name;
2218
2219  name=owl_view_get_filtname(owl_global_get_current_view(&g));
2220  f=owl_global_get_filter(&g, name);
2221  if (!f) {
2222    owl_function_makemsg("Unknown filter");
2223    return;
2224  }
2225
2226  /* don't touch the all filter */
2227  if (!strcmp(name, "all")) {
2228    owl_function_makemsg("You may not change the 'all' filter.");
2229    return;
2230  }
2231
2232  /* deal with the case of trying change the filter color */
2233  owl_filter_set_color(f, owl_util_string_to_color(color));
2234  owl_global_set_needrefresh(&g);
2235  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2236}
2237
2238void owl_function_show_colors() {
2239  owl_fmtext fm;
2240
2241  owl_fmtext_init_null(&fm);
2242  owl_fmtext_append_normal_color(&fm, "default\n", OWL_COLOR_DEFAULT);
2243  owl_fmtext_append_normal_color(&fm, "red\n", OWL_COLOR_RED);
2244  owl_fmtext_append_normal_color(&fm, "green\n", OWL_COLOR_GREEN);
2245  owl_fmtext_append_normal_color(&fm, "yellow\n", OWL_COLOR_YELLOW);
2246  owl_fmtext_append_normal_color(&fm, "blue\n", OWL_COLOR_BLUE);
2247  owl_fmtext_append_normal_color(&fm, "magenta\n", OWL_COLOR_MAGENTA);
2248  owl_fmtext_append_normal_color(&fm, "cyan\n", OWL_COLOR_CYAN);
2249  owl_fmtext_append_normal_color(&fm, "white\n", OWL_COLOR_WHITE);
2250
2251  owl_function_popless_fmtext(&fm);
2252  owl_fmtext_free(&fm);
2253}
2254
2255void owl_function_zpunt(char *class, char *inst, char *recip, int direction) {
2256  /* add the given class, inst, recip to the punt list for filtering.
2257   *   if direction==0 then punt
2258   *   if direction==1 then unpunt */
2259  owl_filter *f;
2260  owl_list *fl;
2261  char *buff;
2262  int ret, i, j;
2263
2264  fl=owl_global_get_puntlist(&g);
2265
2266  /* first, create the filter */
2267  f=malloc(sizeof(owl_filter));
2268  buff=malloc(strlen(class)+strlen(inst)+strlen(recip)+100);
2269  if (!strcmp(recip, "*")) {
2270    sprintf(buff, "class ^%s$ and instance ^%s$", class, inst);
2271  } else {
2272    sprintf(buff, "class ^%s$ and instance ^%s$ and recipient %s", class, inst, recip);
2273  }
2274  owl_function_debugmsg("About to filter %s", buff);
2275  ret=owl_filter_init_fromstring(f, "punt-filter", buff);
2276  owl_free(buff);
2277  if (ret) {
2278    owl_function_makemsg("Error creating filter for zpunt");
2279    owl_filter_free(f);
2280    return;
2281  }
2282
2283  /* Check for an identical filter */
2284  j=owl_list_get_size(fl);
2285  for (i=0; i<j; i++) {
2286    if (owl_filter_equiv(f, owl_list_get_element(fl, i))) {
2287      /* if we're punting, then just silently bow out on this duplicate */
2288      if (direction==0) {
2289        owl_filter_free(f);
2290        return;
2291      }
2292
2293      /* if we're unpunting, then remove this filter from the puntlist */
2294      if (direction==1) {
2295        owl_filter_free(owl_list_get_element(fl, i));
2296        owl_list_remove_element(fl, i);
2297        return;
2298      }
2299    }
2300  }
2301
2302  /* If we're punting, add the filter to the global punt list */
2303  if (direction==0) {
2304    owl_list_append_element(fl, f);
2305  }
2306}
2307
2308void owl_function_activate_keymap(char *keymap) {
2309  if (!owl_keyhandler_activate(owl_global_get_keyhandler(&g), keymap)) {
2310    owl_function_makemsg("Unable to activate keymap '%s'", keymap);
2311  }
2312}
2313
2314
2315void owl_function_show_keymaps() {
2316  owl_list l;
2317  owl_fmtext fm;
2318  owl_keymap *km;
2319  owl_keyhandler *kh;
2320  int i, numkm;
2321  char *kmname;
2322
2323  kh = owl_global_get_keyhandler(&g);
2324  owl_fmtext_init_null(&fm);
2325  owl_fmtext_append_bold(&fm, "Keymaps:   ");
2326  owl_fmtext_append_normal(&fm, "(use 'show keymap <name>' for details)\n");
2327  owl_keyhandler_get_keymap_names(kh, &l);
2328  owl_fmtext_append_list(&fm, &l, "\n", owl_function_keymap_summary);
2329  owl_fmtext_append_normal(&fm, "\n");
2330
2331  numkm = owl_list_get_size(&l);
2332  for (i=0; i<numkm; i++) {
2333    kmname = owl_list_get_element(&l, i);
2334    km = owl_keyhandler_get_keymap(kh, kmname);
2335    owl_fmtext_append_bold(&fm, "\n\n----------------------------------------------------------------------------------------------------\n\n");
2336    owl_keymap_get_details(km, &fm);   
2337  }
2338  owl_fmtext_append_normal(&fm, "\n");
2339 
2340  owl_function_popless_fmtext(&fm);
2341  owl_keyhandler_keymap_namelist_free(&l);
2342  owl_fmtext_free(&fm);
2343}
2344
2345char *owl_function_keymap_summary(void *name) {
2346  owl_keymap *km
2347    = owl_keyhandler_get_keymap(owl_global_get_keyhandler(&g), name);
2348  if (km) return owl_keymap_summary(km);
2349  else return(NULL);
2350}
2351
2352/* TODO: implement for real */
2353void owl_function_show_keymap(char *name) {
2354  owl_fmtext fm;
2355  owl_keymap *km;
2356
2357  owl_fmtext_init_null(&fm);
2358  km = owl_keyhandler_get_keymap(owl_global_get_keyhandler(&g), name);
2359  if (km) {
2360    owl_keymap_get_details(km, &fm);
2361  } else {
2362    owl_fmtext_append_normal(&fm, "No such keymap...\n");
2363  } 
2364  owl_function_popless_fmtext(&fm);
2365  owl_fmtext_free(&fm);
2366}
2367
2368void owl_function_help_for_command(char *cmdname) {
2369  owl_fmtext fm;
2370
2371  owl_fmtext_init_null(&fm);
2372  owl_cmd_get_help(owl_global_get_cmddict(&g), cmdname, &fm);
2373  owl_function_popless_fmtext(&fm); 
2374  owl_fmtext_free(&fm);
2375}
2376
2377void owl_function_search_start(char *string, int direction) {
2378  /* direction is OWL_DIRECTION_DOWNWARDS or OWL_DIRECTION_UPWARDS */
2379  owl_global_set_search_active(&g, string);
2380  owl_function_search_helper(0, direction);
2381}
2382
2383void owl_function_search_continue(int direction) {
2384  /* direction is OWL_DIRECTION_DOWNWARDS or OWL_DIRECTION_UPWARDS */
2385  owl_function_search_helper(1, direction);
2386}
2387
2388void owl_function_search_helper(int mode, int direction) {
2389  /* move to a message that contains the string.  If direction is
2390   * OWL_DIRECTION_DOWNWARDS then search fowards, if direction is
2391   * OWL_DIRECTION_UPWARDS then search backwards.
2392   *
2393   * If mode==0 then it will stay on the current message if it
2394   * contains the string.
2395   */
2396
2397  owl_view *v;
2398  int viewsize, i, curmsg, start;
2399  owl_message *m;
2400
2401  v=owl_global_get_current_view(&g);
2402  viewsize=owl_view_get_size(v);
2403  curmsg=owl_global_get_curmsg(&g);
2404 
2405  if (viewsize==0) {
2406    owl_function_makemsg("No messages present");
2407    return;
2408  }
2409
2410  if (mode==0) {
2411    start=curmsg;
2412  } else if (direction==OWL_DIRECTION_DOWNWARDS) {
2413    start=curmsg+1;
2414  } else {
2415    start=curmsg-1;
2416  }
2417
2418  /* bounds check */
2419  if (start>=viewsize || start<0) {
2420    owl_function_makemsg("No further matches found");
2421    return;
2422  }
2423
2424  for (i=start; i<viewsize && i>=0;) {
2425    m=owl_view_get_element(v, i);
2426    if (owl_message_search(m, owl_global_get_search_string(&g))) {
2427      owl_global_set_curmsg(&g, i);
2428      owl_function_calculate_topmsg(direction);
2429      owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2430      if (direction==OWL_DIRECTION_DOWNWARDS) {
2431        owl_global_set_direction_downwards(&g);
2432      } else {
2433        owl_global_set_direction_upwards(&g);
2434      }
2435      return;
2436    }
2437    if (direction==OWL_DIRECTION_DOWNWARDS) {
2438      i++;
2439    } else {
2440      i--;
2441    }
2442  }
2443  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2444  owl_function_makemsg("No matches found");
2445}
2446
2447
2448/* strips formatting from ztext and returns the unformatted text.
2449 * caller is responsible for freeing. */
2450char *owl_function_ztext_stylestrip(char *zt) {
2451  owl_fmtext fm;
2452  char *plaintext;
2453
2454  owl_fmtext_init_null(&fm);
2455  owl_fmtext_append_ztext(&fm, zt);
2456  plaintext = owl_fmtext_print_plain(&fm);
2457  owl_fmtext_free(&fm);
2458  return(plaintext);
2459}
2460
2461/* popup a znol listing.  If file is NULL use the default .anyone */
2462/* this doesn't obey 'elapsed' or 'timesort' yet */
2463void owl_function_zlist(char *file, int elapsed, int timesort) {
2464  char *ourfile, *tmp, buff[LINE], *line;
2465  FILE *f;
2466  int numlocs, ret, i;
2467  ZLocations_t location[200];
2468  owl_fmtext fm;
2469
2470  if (file==NULL) {
2471    tmp=owl_global_get_homedir(&g);
2472    if (!tmp) {
2473      owl_function_makemsg("Could not determine home directory");
2474      return;
2475    }
2476    ourfile=owl_malloc(strlen(tmp)+50);
2477    sprintf(ourfile, "%s/.anyone", owl_global_get_homedir(&g));
2478  } else {
2479    ourfile=owl_strdup(file);
2480  }
2481
2482  f=fopen(ourfile, "r");
2483  if (!f) {
2484    owl_function_makemsg("Error opening file %s: %s",
2485                         ourfile,
2486                         strerror(errno) ? strerror(errno) : "");
2487    return;
2488  }
2489
2490  owl_fmtext_init_null(&fm);
2491   
2492  while (fgets(buff, LINE, f)!=NULL) {
2493    /* ignore comments, blank lines etc. */
2494    if (buff[0]=='#') continue;
2495    if (buff[0]=='\n') continue;
2496    if (buff[0]=='\0') continue;
2497
2498    /* strip the \n */
2499    buff[strlen(buff)-1]='\0';
2500
2501    /* ingore from # on */
2502    tmp=strchr(buff, '#');
2503    if (tmp) tmp[0]='\0';
2504
2505    /* ingore from SPC */
2506    tmp=strchr(buff, ' ');
2507    if (tmp) tmp[0]='\0';
2508
2509    /* stick on the local realm. */
2510    if (!strchr(buff, '@')) {
2511      strcat(buff, "@");
2512      strcat(buff, ZGetRealm());
2513    }
2514
2515    ret=ZLocateUser(buff, &numlocs, ZAUTH);
2516    if (ret!=ZERR_NONE) {
2517      owl_function_makemsg("Error getting location for %s", buff);
2518      continue;
2519    }
2520
2521    numlocs=200;
2522    ret=ZGetLocations(location, &numlocs);
2523    if (ret==0) {
2524      for (i=0; i<numlocs; i++) {
2525        line=malloc(strlen(location[i].host)+strlen(location[i].time)+strlen(location[i].tty)+100);
2526        tmp=short_zuser(buff);
2527        sprintf(line, "%-10.10s %-24.24s %-12.12s  %20.20s\n",
2528                tmp,
2529                location[i].host,
2530                location[i].tty,
2531                location[i].time);
2532        owl_fmtext_append_normal(&fm, line);
2533        owl_free(tmp);
2534      }
2535      if (numlocs>=200) {
2536        owl_fmtext_append_normal(&fm, "Too many locations found for this user, truncating.\n");
2537      }
2538    }
2539  }
2540  fclose(f);
2541
2542  owl_function_popless_fmtext(&fm);
2543  owl_fmtext_free(&fm);
2544
2545  owl_free(ourfile);
2546}
2547
2548void owl_function_dump(char *filename) {
2549  int i, j, count;
2550  owl_message *m;
2551  owl_view *v;
2552  FILE *file;
2553  /* struct stat sbuf; */
2554
2555  v=owl_global_get_current_view(&g);
2556
2557  /* in the future make it ask yes/no */
2558  /*
2559  ret=stat(filename, &sbuf);
2560  if (!ret) {
2561    ret=owl_function_askyesno("File exists, continue? [Y/n]");
2562    if (!ret) return;
2563  }
2564  */
2565
2566  file=fopen(filename, "w");
2567  if (!file) {
2568    owl_function_makemsg("Error opening file");
2569    return;
2570  }
2571
2572  count=0;
2573  j=owl_view_get_size(v);
2574  for (i=0; i<j; i++) {
2575    m=owl_view_get_element(v, i);
2576    fputs(owl_message_get_text(m), file);
2577  }
2578  fclose(file);
2579}
2580
2581
2582
2583void owl_function_do_newmsgproc() {
2584  if (owl_global_get_newmsgproc(&g) && strcmp(owl_global_get_newmsgproc(&g), "")) {
2585    /* if there's a process out there, we need to check on it */
2586    if (owl_global_get_newmsgproc_pid(&g)) {
2587      owl_function_debugmsg("Checking on newmsgproc pid==%i", owl_global_get_newmsgproc_pid(&g));
2588      owl_function_debugmsg("Waitpid return is %i", waitpid(owl_global_get_newmsgproc_pid(&g), NULL, WNOHANG));
2589      waitpid(owl_global_get_newmsgproc_pid(&g), NULL, WNOHANG);
2590      if (waitpid(owl_global_get_newmsgproc_pid(&g), NULL, WNOHANG)==-1) {
2591        /* it exited */
2592        owl_global_set_newmsgproc_pid(&g, 0);
2593        owl_function_debugmsg("newmsgproc exited");
2594      } else {
2595        owl_function_debugmsg("newmsgproc did not exit");
2596      }
2597    }
2598   
2599    /* if it exited, fork & exec a new one */
2600    if (owl_global_get_newmsgproc_pid(&g)==0) {
2601      int i, myargc;
2602      i=fork();
2603      if (i) {
2604        /* parent set the child's pid */
2605        owl_global_set_newmsgproc_pid(&g, i);
2606        waitpid(i, NULL, WNOHANG);
2607        owl_function_debugmsg("I'm the parent and I started a new newmsgproc with pid %i", i);
2608      } else {
2609        /* child exec's the program */
2610        char **parsed;
2611        parsed=owl_parseline(owl_global_get_newmsgproc(&g), &myargc);
2612        parsed=realloc(parsed, strlen(owl_global_get_newmsgproc(&g)+300));
2613        parsed[myargc]=(char *) NULL;
2614       
2615        owl_function_debugmsg("About to exec: %s with %i arguments", parsed[0], myargc);
2616       
2617        execvp(parsed[0], (char **) parsed);
2618       
2619       
2620        /* was there an error exec'ing? */
2621        owl_function_debugmsg("Error execing: %s", strerror(errno));
2622        _exit(127);
2623      }
2624    }
2625  }
2626}
Note: See TracBrowser for help on using the repository browser.