source: functions.c @ 486688f

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