source: functions.c @ 2adaf1d

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