source: functions.c @ cf83b7a

barnowl_perlaimdebianowlrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since cf83b7a was cf83b7a, checked in by James M. Kretchmar <kretch@mit.edu>, 21 years ago
Started adding code to do question/response stuff
  • Property mode set to 100644
File size: 81.1 KB
Line 
1#include <stdio.h>
2#include <stdlib.h>
3#include <unistd.h>
4#include <signal.h>
5#include <string.h>
6#include <com_err.h>
7#include <time.h>
8#include <sys/types.h>
9#include <sys/stat.h>
10#include <sys/wait.h>
11#include <errno.h>
12#include <signal.h>
13#include "owl.h"
14
15static const char fileIdent[] = "$Id$";
16
17void owl_function_noop(void)
18{
19  return;
20}
21
22char *owl_function_command(char *cmdbuff)
23{
24  owl_function_debugmsg("executing command: %s", cmdbuff);
25  return owl_cmddict_execute(owl_global_get_cmddict(&g), 
26                             owl_global_get_context(&g), cmdbuff);
27}
28
29void owl_function_command_norv(char *cmdbuff)
30{
31  char *rv;
32  rv=owl_function_command(cmdbuff);
33  if (rv) owl_free(rv);
34}
35
36void owl_function_command_alias(char *alias_from, char *alias_to)
37{
38  owl_cmddict_add_alias(owl_global_get_cmddict(&g), alias_from, alias_to);
39}
40
41owl_cmd *owl_function_get_cmd(char *name)
42{
43  return owl_cmddict_find(owl_global_get_cmddict(&g), name);
44}
45
46void owl_function_show_commands()
47{
48  owl_list l;
49  owl_fmtext fm;
50
51  owl_fmtext_init_null(&fm);
52  owl_fmtext_append_bold(&fm, "Commands:  ");
53  owl_fmtext_append_normal(&fm, "(use 'show command <name>' for details)\n");
54  owl_cmddict_get_names(owl_global_get_cmddict(&g), &l);
55  owl_fmtext_append_list(&fm, &l, "\n", owl_function_cmd_describe);
56  owl_fmtext_append_normal(&fm, "\n");
57  owl_function_popless_fmtext(&fm);
58  owl_cmddict_namelist_free(&l);
59  owl_fmtext_free(&fm);
60}
61
62
63void owl_function_show_view(char *viewname)
64{
65  owl_view *v;
66  owl_fmtext fm;
67
68  /* we only have the one view right now */
69  v=owl_global_get_current_view(&g);
70  if (viewname && strcmp(viewname, owl_view_get_name(v))) {
71    owl_function_makemsg("No view named '%s'", viewname);
72    return;
73  }
74
75  owl_fmtext_init_null(&fm);
76  owl_view_to_fmtext(v, &fm);
77  owl_function_popless_fmtext(&fm);
78  owl_fmtext_free(&fm);
79}
80
81
82char *owl_function_cmd_describe(void *name)
83{
84  owl_cmd *cmd = owl_cmddict_find(owl_global_get_cmddict(&g), name);
85  if (cmd) return owl_cmd_describe(cmd);
86  else return(NULL);
87}
88
89void owl_function_show_command(char *name)
90{
91  owl_function_help_for_command(name);
92}
93
94void owl_function_adminmsg(char *header, char *body)
95{
96  owl_message *m;
97  int followlast;
98
99  followlast=owl_global_should_followlast(&g);
100  m=owl_malloc(sizeof(owl_message));
101  owl_message_create_admin(m, header, body);
102  owl_messagelist_append_element(owl_global_get_msglist(&g), m);
103  owl_view_consider_message(owl_global_get_current_view(&g), m);
104
105  if (followlast) owl_function_lastmsg_noredisplay();
106
107  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
108  if (owl_popwin_is_active(owl_global_get_popwin(&g))) {
109    owl_popwin_refresh(owl_global_get_popwin(&g));
110  }
111 
112  wnoutrefresh(owl_global_get_curs_recwin(&g));
113  owl_global_set_needrefresh(&g);
114}
115
116void owl_function_make_outgoing_zephyr(char *body, char *zwriteline, char *zsig)
117{
118  owl_message *m;
119  int followlast;
120  owl_zwrite z;
121 
122  followlast=owl_global_should_followlast(&g);
123
124  /* create a zwrite for the purpose of filling in other message fields */
125  owl_zwrite_create_from_line(&z, zwriteline);
126
127  /* create the message */
128  m=owl_malloc(sizeof(owl_message));
129  owl_message_create_from_zwriteline(m, zwriteline, body, zsig);
130
131  /* add it to the global list and current view */
132  owl_messagelist_append_element(owl_global_get_msglist(&g), m);
133  owl_view_consider_message(owl_global_get_current_view(&g), m);
134
135  if (followlast) owl_function_lastmsg_noredisplay();
136
137  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
138  if (owl_popwin_is_active(owl_global_get_popwin(&g))) {
139    owl_popwin_refresh(owl_global_get_popwin(&g));
140  }
141 
142  wnoutrefresh(owl_global_get_curs_recwin(&g));
143  owl_global_set_needrefresh(&g);
144  owl_zwrite_free(&z);
145}
146
147int owl_function_make_outgoing_aim(char *body, char *to)
148{
149  owl_message *m;
150  int followlast;
151
152
153  if (!owl_global_is_aimloggedin(&g)) {
154    return(-1);
155  }
156 
157  followlast=owl_global_should_followlast(&g);
158
159  /* create the message */
160  m=owl_malloc(sizeof(owl_message));
161  owl_message_create_aim(m,
162                         owl_global_get_aim_screenname(&g),
163                         to,
164                         body,
165                         OWL_MESSAGE_DIRECTION_OUT,
166                         0);
167
168  /* add it to the global list and current view */
169  owl_messagelist_append_element(owl_global_get_msglist(&g), m);
170  owl_view_consider_message(owl_global_get_current_view(&g), m);
171
172  if (followlast) owl_function_lastmsg_noredisplay();
173
174  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
175  if (owl_popwin_is_active(owl_global_get_popwin(&g))) {
176    owl_popwin_refresh(owl_global_get_popwin(&g));
177  }
178 
179  wnoutrefresh(owl_global_get_curs_recwin(&g));
180  owl_global_set_needrefresh(&g);
181  return(0);
182}
183
184void owl_function_zwrite_setup(char *line)
185{
186  owl_editwin *e;
187  char buff[1024];
188  owl_zwrite z;
189  int ret;
190
191  /* check the arguments */
192  ret=owl_zwrite_create_from_line(&z, line);
193  if (ret) {
194    owl_function_makemsg("Error in zwrite arugments");
195    owl_zwrite_free(&z);
196    return;
197  }
198
199  /* send a ping if necessary */
200  if (owl_global_is_txping(&g)) {
201    owl_zwrite_send_ping(&z);
202  }
203  owl_zwrite_free(&z);
204
205  /* create and setup the editwin */
206  e=owl_global_get_typwin(&g);
207  owl_editwin_new_style(e, OWL_EDITWIN_STYLE_MULTILINE, owl_global_get_msg_history(&g));
208
209  if (!owl_global_get_lockout_ctrld(&g)) {
210    owl_function_makemsg("Type your zephyr below.  End with ^D or a dot on a line by itself.  ^C will quit.");
211  } else {
212    owl_function_makemsg("Type your zephyr below.  End with a dot on a line by itself.  ^C will quit.");
213  }
214
215  owl_editwin_clear(e);
216  owl_editwin_set_dotsend(e);
217  strcpy(buff, "----> ");
218  strcat(buff, line);
219  strcat(buff, "\n");
220  owl_editwin_set_locktext(e, buff);
221
222  /* make it active */
223  owl_global_set_typwin_active(&g);
224}
225
226void owl_function_aimwrite_setup(char *line)
227{
228  owl_editwin *e;
229  char buff[1024];
230
231  /* check the arguments */
232
233  /* create and setup the editwin */
234  e=owl_global_get_typwin(&g);
235  owl_editwin_new_style(e, OWL_EDITWIN_STYLE_MULTILINE, owl_global_get_msg_history(&g));
236
237  if (!owl_global_get_lockout_ctrld(&g)) {
238    owl_function_makemsg("Type your message below.  End with ^D or a dot on a line by itself.  ^C will quit.");
239  } else {
240    owl_function_makemsg("Type your message below.  End with a dot on a line by itself.  ^C will quit.");
241  }
242
243  owl_editwin_clear(e);
244  owl_editwin_set_dotsend(e);
245  strcpy(buff, "----> ");
246  strcat(buff, line);
247  strcat(buff, "\n");
248  owl_editwin_set_locktext(e, buff);
249
250  /* make it active */
251  owl_global_set_typwin_active(&g);
252}
253
254
255
256void owl_function_zcrypt_setup(char *line)
257{
258  owl_editwin *e;
259  char buff[1024];
260  owl_zwrite z;
261  int ret;
262
263  /* check the arguments */
264  ret=owl_zwrite_create_from_line(&z, line);
265  if (ret) {
266    owl_function_makemsg("Error in zwrite arugments");
267    owl_zwrite_free(&z);
268    return;
269  }
270
271  if (owl_zwrite_get_numrecips(&z)>0) {
272    owl_function_makemsg("You may not specifiy a recipient for a zcrypt message");
273    owl_zwrite_free(&z);
274    return;
275  }
276
277  /* send a ping if necessary */
278  if (owl_global_is_txping(&g)) {
279    owl_zwrite_send_ping(&z);
280  }
281  owl_zwrite_free(&z);
282
283  /* create and setup the editwin */
284  e=owl_global_get_typwin(&g);
285  owl_editwin_new_style(e, OWL_EDITWIN_STYLE_MULTILINE, owl_global_get_msg_history(&g));
286
287  if (!owl_global_get_lockout_ctrld(&g)) {
288    owl_function_makemsg("Type your zephyr below.  End with ^D or a dot on a line by itself.  ^C will quit.");
289  } else {
290    owl_function_makemsg("Type your zephyr below.  End with a dot on a line by itself.  ^C will quit.");
291  }
292
293  owl_editwin_clear(e);
294  owl_editwin_set_dotsend(e);
295  strcpy(buff, "----> ");
296  strcat(buff, line);
297  strcat(buff, "\n");
298  owl_editwin_set_locktext(e, buff);
299
300  /* make it active */
301  owl_global_set_typwin_active(&g);
302}
303
304/* send, log and display an outgoing zephyr.  If 'msg' is NULL
305 * the message is expected to be set from the zwrite line itself
306 */
307void owl_function_zwrite(char *line, char *msg)
308{
309  owl_zwrite z;
310  int i, j;
311  char *mymsg;
312
313  /* create the zwrite and send the message */
314  owl_zwrite_create_from_line(&z, line);
315  if (msg) {
316    owl_zwrite_set_message(&z, msg);
317  }
318
319  owl_zwrite_send_message(&z);
320  owl_function_makemsg("Waiting for ack...");
321
322  mymsg=owl_zwrite_get_message(&z);
323
324  /* display the message as an outgoing message in the receive window */
325  if (owl_global_is_displayoutgoing(&g) && owl_zwrite_is_personal(&z)) {
326    owl_function_make_outgoing_zephyr(mymsg, line, owl_zwrite_get_zsig(&z));
327  }
328
329  /* log it if we have logging turned on */
330  if (owl_global_is_logging(&g) && owl_zwrite_is_personal(&z)) {
331    j=owl_zwrite_get_numrecips(&z);
332    for (i=0; i<j; i++) {
333      owl_log_outgoing_zephyr(owl_zwrite_get_recip_n(&z, i), mymsg);
334    }
335  }
336
337  /* free the zwrite */
338  owl_zwrite_free(&z);
339}
340
341
342void owl_function_aimwrite(char *to)
343{
344  /*  send the message */
345  owl_aim_send_im(to, owl_editwin_get_text(owl_global_get_typwin(&g)));
346  owl_function_makemsg("AIM message sent.");
347
348  /* display the message as an outgoing message in the receive window */
349  if (owl_global_is_displayoutgoing(&g)) {
350    owl_function_make_outgoing_aim(owl_editwin_get_text(owl_global_get_typwin(&g)), to);
351  }
352
353  /* log it if we have logging turned on */
354  if (owl_global_is_logging(&g)) {
355    owl_log_outgoing_aim(to, owl_editwin_get_text(owl_global_get_typwin(&g)));
356  }
357}
358
359
360
361/* If filter is non-null, looks for the next message matching
362 * that filter.  If skip_deleted, skips any deleted messages.
363 * If last_if_none, will stop at the last message in the view
364 * if no matching messages are found.  */
365void owl_function_nextmsg_full(char *filter, int skip_deleted, int last_if_none)
366{
367  int curmsg, i, viewsize, found;
368  owl_view *v;
369  owl_filter *f = NULL;
370  owl_message *m;
371
372  v=owl_global_get_current_view(&g);
373
374  if (filter) {
375    f=owl_global_get_filter(&g, filter);
376    if (!f) {
377      owl_function_makemsg("No %s filter defined", filter);
378      return;
379    }
380  }
381
382  curmsg=owl_global_get_curmsg(&g);
383  viewsize=owl_view_get_size(v);
384  found=0;
385
386  /* just check to make sure we're in bounds... */
387  if (curmsg>viewsize-1) curmsg=viewsize-1;
388  if (curmsg<0) curmsg=0;
389
390  for (i=curmsg+1; i<viewsize; i++) {
391    m=owl_view_get_element(v, i);
392    if (skip_deleted && owl_message_is_delete(m)) continue;
393    if (f && !owl_filter_message_match(f, m)) continue;
394    found = 1;
395    break;
396  }
397
398  if (i>owl_view_get_size(v)-1) i=owl_view_get_size(v)-1;
399
400  if (!found) {
401    owl_function_makemsg("already at last%s message%s%s",
402                         skip_deleted?" non-deleted":"",
403                         filter?" in ":"", filter?filter:"");
404    /* if (!skip_deleted) owl_function_beep(); */
405  }
406
407  if (last_if_none || found) {
408    owl_global_set_curmsg(&g, i);
409    owl_function_calculate_topmsg(OWL_DIRECTION_DOWNWARDS);
410    owl_mainwin_redisplay(owl_global_get_mainwin(&g));
411    owl_global_set_direction_downwards(&g);
412  }
413}
414
415void owl_function_prevmsg_full(char *filter, int skip_deleted, int first_if_none)
416{
417  int curmsg, i, viewsize, found;
418  owl_view *v;
419  owl_filter *f = NULL;
420  owl_message *m;
421
422  v=owl_global_get_current_view(&g);
423
424  if (filter) {
425    f=owl_global_get_filter(&g, filter);
426    if (!f) {
427      owl_function_makemsg("No %s filter defined", filter);
428      return;
429    }
430  }
431
432  curmsg=owl_global_get_curmsg(&g);
433  viewsize=owl_view_get_size(v);
434  found=0;
435
436  /* just check to make sure we're in bounds... */
437  if (curmsg<0) curmsg=0;
438
439  for (i=curmsg-1; i>=0; i--) {
440    m=owl_view_get_element(v, i);
441    if (skip_deleted && owl_message_is_delete(m)) continue;
442    if (f && !owl_filter_message_match(f, m)) continue;
443    found = 1;
444    break;
445  }
446
447  if (i<0) i=0;
448
449  if (!found) {
450    owl_function_makemsg("already at first%s message%s%s",
451                         skip_deleted?" non-deleted":"",
452                         filter?" in ":"", filter?filter:"");
453    /* if (!skip_deleted) owl_function_beep(); */
454  }
455
456  if (first_if_none || found) {
457    owl_global_set_curmsg(&g, i);
458    owl_function_calculate_topmsg(OWL_DIRECTION_UPWARDS);
459    owl_mainwin_redisplay(owl_global_get_mainwin(&g));
460    owl_global_set_direction_upwards(&g);
461  }
462}
463
464void owl_function_nextmsg()
465{
466  owl_function_nextmsg_full(NULL, 0, 1);
467}
468
469
470void owl_function_prevmsg()
471{
472  owl_function_prevmsg_full(NULL, 0, 1);
473}
474
475void owl_function_nextmsg_notdeleted()
476{
477  owl_function_nextmsg_full(NULL, 1, 1);
478}
479
480void owl_function_prevmsg_notdeleted()
481{
482  owl_function_prevmsg_full(NULL, 1, 1);
483}
484
485
486void owl_function_nextmsg_personal()
487{
488  owl_function_nextmsg_full("personal", 0, 0);
489}
490
491void owl_function_prevmsg_personal()
492{
493  owl_function_prevmsg_full("personal", 0, 0);
494}
495
496
497/* if move_after is 1, moves after the delete */
498void owl_function_deletecur(int move_after)
499{
500  int curmsg;
501  owl_view *v;
502
503  v=owl_global_get_current_view(&g);
504
505  /* bail if there's no current message */
506  if (owl_view_get_size(v) < 1) {
507    owl_function_makemsg("No current message to delete");
508    return;
509  }
510
511  /* mark the message for deletion */
512  curmsg=owl_global_get_curmsg(&g);
513  owl_view_delete_element(v, curmsg);
514
515  if (move_after) {
516    /* move the poiner in the appropriate direction
517     * to the next undeleted msg */
518    if (owl_global_get_direction(&g)==OWL_DIRECTION_UPWARDS) {
519      owl_function_prevmsg_notdeleted();
520    } else {
521      owl_function_nextmsg_notdeleted();
522    }
523  }
524}
525
526
527void owl_function_undeletecur(int move_after)
528{
529  int curmsg;
530  owl_view *v;
531
532  v=owl_global_get_current_view(&g);
533 
534  if (owl_view_get_size(v) < 1) {
535    owl_function_makemsg("No current message to undelete");
536    return;
537  }
538  curmsg=owl_global_get_curmsg(&g);
539
540  owl_view_undelete_element(v, curmsg);
541
542  if (move_after) {
543    if (owl_global_get_direction(&g)==OWL_DIRECTION_UPWARDS) {
544      if (curmsg>0) {
545        owl_function_prevmsg();
546      } else {
547        owl_function_nextmsg();
548      }
549    } else {
550      owl_function_nextmsg();
551    }
552  }
553
554  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
555}
556
557
558void owl_function_expunge()
559{
560  int curmsg;
561  owl_message *m;
562  owl_messagelist *ml;
563  owl_view *v;
564  int lastmsgid=0;
565
566  curmsg=owl_global_get_curmsg(&g);
567  v=owl_global_get_current_view(&g);
568  ml=owl_global_get_msglist(&g);
569
570  m=owl_view_get_element(v, curmsg);
571  if (m) lastmsgid = owl_message_get_id(m);
572
573  /* expunge the message list */
574  owl_messagelist_expunge(ml);
575
576  /* update all views (we only have one right now) */
577  owl_view_recalculate(v);
578
579  /* find where the new position should be
580     (as close as possible to where we last where) */
581  curmsg = owl_view_get_nearest_to_msgid(v, lastmsgid);
582  if (curmsg>owl_view_get_size(v)-1) curmsg = owl_view_get_size(v)-1;
583  if (curmsg<0) curmsg = 0;
584  owl_global_set_curmsg(&g, curmsg);
585  owl_function_calculate_topmsg(OWL_DIRECTION_NONE);
586  /* if there are no messages set the direction to down in case we
587     delete everything upwards */
588  owl_global_set_direction_downwards(&g);
589 
590  owl_function_makemsg("Messages expunged");
591  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
592}
593
594
595void owl_function_firstmsg()
596{
597  owl_global_set_curmsg(&g, 0);
598  owl_global_set_topmsg(&g, 0);
599  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
600  owl_global_set_direction_downwards(&g);
601}
602
603void owl_function_lastmsg_noredisplay()
604{
605  int oldcurmsg, curmsg;
606  owl_view *v;
607
608  v=owl_global_get_current_view(&g);
609  oldcurmsg=owl_global_get_curmsg(&g);
610  curmsg=owl_view_get_size(v)-1; 
611  if (curmsg<0) curmsg=0;
612  owl_global_set_curmsg(&g, curmsg);
613  if (oldcurmsg < curmsg) {
614    owl_function_calculate_topmsg(OWL_DIRECTION_DOWNWARDS);
615  } else if (curmsg<owl_view_get_size(v)) {
616    /* If already at the end, blank the screen and move curmsg
617     * past the end of the messages. */
618    owl_global_set_topmsg(&g, curmsg+1);
619    owl_global_set_curmsg(&g, curmsg+1);
620  } 
621  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
622  owl_global_set_direction_downwards(&g);
623}
624
625void owl_function_lastmsg()
626{
627  owl_function_lastmsg_noredisplay();
628  owl_mainwin_redisplay(owl_global_get_mainwin(&g)); 
629}
630
631void owl_function_shift_right()
632{
633  owl_global_set_rightshift(&g, owl_global_get_rightshift(&g)+10);
634  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
635  owl_global_set_needrefresh(&g);
636}
637
638
639void owl_function_shift_left()
640{
641  int shift;
642
643  shift=owl_global_get_rightshift(&g);
644  if (shift>=10) {
645    owl_global_set_rightshift(&g, shift-10);
646    owl_mainwin_redisplay(owl_global_get_mainwin(&g));
647    owl_global_set_needrefresh(&g);
648  } else {
649    owl_function_beep();
650    owl_function_makemsg("Already full left");
651  }
652}
653
654
655void owl_function_unsuball()
656{
657  unsuball();
658  owl_function_makemsg("Unsubscribed from all messages.");
659}
660
661void owl_function_loadsubs(char *file)
662{
663  int ret;
664
665  ret=owl_zephyr_loadsubs(file);
666
667  if (!owl_context_is_interactive(owl_global_get_context(&g))) return;
668  if (ret==0) {
669    owl_function_makemsg("Subscribed to messages from file.");
670  } else if (ret==-1) {
671    owl_function_makemsg("Could not open file.");
672  } else {
673    owl_function_makemsg("Error subscribing to messages from file.");
674  }
675}
676
677void owl_function_loadloginsubs(char *file)
678{
679  int ret;
680
681  ret=owl_zephyr_loadloginsubs(file);
682
683  if (!owl_context_is_interactive(owl_global_get_context(&g))) return;
684  if (ret==0) {
685    owl_function_makemsg("Subscribed to login messages from file.");
686  } else if (ret==-1) {
687    owl_function_makemsg("Could not open file for login subscriptions.");
688  } else {
689    owl_function_makemsg("Error subscribing to login messages from file.");
690  }
691}
692
693void owl_function_suspend()
694{
695  endwin();
696  printf("\n");
697  kill(getpid(), SIGSTOP);
698
699  /* resize to reinitialize all the windows when we come back */
700  owl_command_resize();
701}
702
703void owl_function_zaway_toggle()
704{
705  if (!owl_global_is_zaway(&g)) {
706    owl_global_set_zaway_msg(&g, owl_global_get_zaway_msg_default(&g));
707    owl_function_zaway_on();
708  } else {
709    owl_function_zaway_off();
710  }
711}
712
713void owl_function_zaway_on()
714{
715  owl_global_set_zaway_on(&g);
716  owl_function_makemsg("zaway set (%s)", owl_global_get_zaway_msg(&g));
717}
718
719void owl_function_zaway_off()
720{
721  owl_global_set_zaway_off(&g);
722  owl_function_makemsg("zaway off");
723}
724
725void owl_function_quit()
726{
727  char *ret;
728 
729  /* zlog out if we need to */
730  if (owl_global_is_shutdownlogout(&g)) {
731    owl_zephyr_zlog_out();
732  }
733
734  /* execute the commands in shutdown */
735  ret = owl_config_execute("owl::shutdown();");
736  if (ret) owl_free(ret);
737
738  /* signal our child process, if any */
739  if (owl_global_get_newmsgproc_pid(&g)) {
740    kill(owl_global_get_newmsgproc_pid(&g), SIGHUP);
741  }
742
743  /* final clean up */
744  unsuball();
745  ZClosePort();
746  endwin();
747  owl_function_debugmsg("Quitting Owl");
748  exit(0);
749}
750
751
752void owl_function_makemsg(char *fmt, ...)
753{
754  va_list ap;
755  char buff[2048];
756
757  if (!owl_global_get_curs_msgwin(&g)) return;
758
759  va_start(ap, fmt);
760  werase(owl_global_get_curs_msgwin(&g));
761 
762  vsnprintf(buff, 2048, fmt, ap);
763  owl_function_debugmsg("makemsg: %s", buff);
764  waddstr(owl_global_get_curs_msgwin(&g), buff); 
765  wnoutrefresh(owl_global_get_curs_msgwin(&g));
766  owl_global_set_needrefresh(&g);
767  va_end(ap);
768}
769
770void owl_function_errormsg(char *fmt, ...)
771{
772  va_list ap;
773  char buff[2048];
774
775  if (!owl_global_get_curs_msgwin(&g)) return;
776
777  va_start(ap, fmt);
778  werase(owl_global_get_curs_msgwin(&g));
779 
780  vsnprintf(buff, 2048, fmt, ap);
781  owl_function_debugmsg("ERROR: %s", buff);
782  waddstr(owl_global_get_curs_msgwin(&g), buff); 
783  waddstr(owl_global_get_curs_msgwin(&g), "ERROR: "); 
784  wnoutrefresh(owl_global_get_curs_msgwin(&g));
785  owl_global_set_needrefresh(&g);
786  va_end(ap);
787}
788
789
790void owl_function_openurl()
791{
792  /* visit the first url in the current message */
793  owl_message *m;
794  owl_view *v;
795  char *ptr1, *ptr2, *text, url[LINE], tmpbuff[LINE];
796  int webbrowser;
797
798  webbrowser = owl_global_get_webbrowser(&g);
799
800  if (webbrowser < 0 || webbrowser == OWL_WEBBROWSER_NONE) {
801    owl_function_makemsg("No browser selected");
802    return;
803  }
804
805  v=owl_global_get_current_view(&g);
806 
807  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
808
809  if (!m || owl_view_get_size(v)==0) {
810    owl_function_makemsg("No current message selected");
811    return;
812  }
813
814  text=owl_message_get_text(m);
815
816  /* First look for a good URL */ 
817  if ((ptr1=strstr(text, "http://"))!=NULL) {
818    ptr2=strpbrk(ptr1, " \n\t");
819    if (ptr2) {
820      strncpy(url, ptr1, ptr2-ptr1+1);
821      url[ptr2-ptr1+1]='\0';
822    } else {
823      strcpy(url, ptr1);
824    }
825
826    /* if we had <http strip a trailing > */
827    if (ptr1>text && ptr1[-1]=='<') {
828      if (url[strlen(url)-1]=='>') {
829        url[strlen(url)-1]='\0';
830      }
831    }
832  } else if ((ptr1=strstr(text, "https://"))!=NULL) {
833    /* Look for an https URL */ 
834    ptr2=strpbrk(ptr1, " \n\t");
835    if (ptr2) {
836      strncpy(url, ptr1, ptr2-ptr1+1);
837      url[ptr2-ptr1+1]='\0';
838    } else {
839      strcpy(url, ptr1);
840    }
841   
842    /* if we had <http strip a trailing > */
843    if (ptr1>text && ptr1[-1]=='<') {
844      if (url[strlen(url)-1]=='>') {
845        url[strlen(url)-1]='\0';
846      }
847    }
848  } else if ((ptr1=strstr(text, "www."))!=NULL) {
849    /* if we can't find a real url look for www.something */
850    ptr2=strpbrk(ptr1, " \n\t");
851    if (ptr2) {
852      strncpy(url, ptr1, ptr2-ptr1+1);
853      url[ptr2-ptr1+1]='\0';
854    } else {
855      strcpy(url, ptr1);
856    }
857  } else {
858    owl_function_beep();
859    owl_function_makemsg("Could not find URL to open.");
860    return;
861  }
862
863  /* Make sure there aren't any quotes or \'s in the url */
864  for (ptr1 = url; *ptr1; ptr1++) {
865    if (*ptr1 == '"' || *ptr1 == '\\') {
866      owl_function_beep();
867      owl_function_makemsg("URL contains invalid characters.");
868      return;
869    }
870  }
871 
872  /* NOTE: There are potentially serious security issues here... */
873
874  /* open the page */
875  owl_function_makemsg("Opening %s", url);
876  if (webbrowser == OWL_WEBBROWSER_NETSCAPE) {
877    snprintf(tmpbuff, LINE, "netscape -remote \"openURL(%s)\" > /dev/null 2> /dev/null", url);
878    system(tmpbuff); 
879  } else if (webbrowser == OWL_WEBBROWSER_GALEON) {
880    snprintf(tmpbuff, LINE, "galeon \"%s\" > /dev/null 2> /dev/null &", url);
881    system(tmpbuff); 
882  } else if (webbrowser == OWL_WEBBROWSER_OPERA) {
883    snprintf(tmpbuff, LINE, "opera \"%s\" > /dev/null 2> /dev/null &", url);
884    system(tmpbuff); 
885  }
886}
887
888void owl_function_calculate_topmsg(int direction)
889{
890  int recwinlines, topmsg, curmsg;
891  owl_view *v;
892
893  v=owl_global_get_current_view(&g);
894  curmsg=owl_global_get_curmsg(&g);
895  topmsg=owl_global_get_topmsg(&g);
896  recwinlines=owl_global_get_recwin_lines(&g);
897
898  /*
899  if (owl_view_get_size(v) < 1) {
900    return;
901  }
902  */
903
904  switch (owl_global_get_scrollmode(&g)) {
905  case OWL_SCROLLMODE_TOP:
906    topmsg = owl_function_calculate_topmsg_top(direction, v, curmsg, topmsg, recwinlines);
907    break;
908  case OWL_SCROLLMODE_NEARTOP:
909    topmsg = owl_function_calculate_topmsg_neartop(direction, v, curmsg, topmsg, recwinlines);
910    break;
911  case OWL_SCROLLMODE_CENTER:
912    topmsg = owl_function_calculate_topmsg_center(direction, v, curmsg, topmsg, recwinlines);
913    break;
914  case OWL_SCROLLMODE_PAGED:
915    topmsg = owl_function_calculate_topmsg_paged(direction, v, curmsg, topmsg, recwinlines, 0);
916    break;
917  case OWL_SCROLLMODE_PAGEDCENTER:
918    topmsg = owl_function_calculate_topmsg_paged(direction, v, curmsg, topmsg, recwinlines, 1);
919    break;
920  case OWL_SCROLLMODE_NORMAL:
921  default:
922    topmsg = owl_function_calculate_topmsg_normal(direction, v, curmsg, topmsg, recwinlines);
923  }
924  owl_function_debugmsg("Calculated a topmsg of %i", topmsg);
925  owl_global_set_topmsg(&g, topmsg);
926}
927
928/* Returns what the new topmsg should be. 
929 * Passed the last direction of movement,
930 * the current view,
931 * the current message number in the view,
932 * the top message currently being displayed,
933 * and the number of lines in the recwin.
934 */
935int owl_function_calculate_topmsg_top(int direction, owl_view *v, int curmsg, int topmsg, int recwinlines)
936{
937  return(curmsg);
938}
939
940int owl_function_calculate_topmsg_neartop(int direction, owl_view *v, int curmsg, int topmsg, int recwinlines)
941{
942  if (curmsg>0 
943      && (owl_message_get_numlines(owl_view_get_element(v, curmsg-1))
944          <  recwinlines/2)) {
945    return(curmsg-1);
946  } else {
947    return(curmsg);
948  }
949}
950 
951int owl_function_calculate_topmsg_center(int direction, owl_view *v, int curmsg, int topmsg, int recwinlines)
952{
953  int i, last, lines;
954
955  last = curmsg;
956  lines = 0;
957  for (i=curmsg-1; i>=0; i--) {
958    lines += owl_message_get_numlines(owl_view_get_element(v, i));
959    if (lines > recwinlines/2) break;
960    last = i;
961  }
962  return(last);
963}
964 
965int owl_function_calculate_topmsg_paged(int direction, owl_view *v, int curmsg, int topmsg, int recwinlines, int center_on_page)
966{
967  int i, last, lines, savey;
968 
969  /* If we're off the top of the screen, scroll up such that the
970   * curmsg is near the botton of the screen. */
971  if (curmsg < topmsg) {
972    last = curmsg;
973    lines = 0;
974    for (i=curmsg; i>=0; i--) {
975      lines += owl_message_get_numlines(owl_view_get_element(v, i));
976      if (lines > recwinlines) break;
977    last = i;
978    }
979    if (center_on_page) {
980      return(owl_function_calculate_topmsg_center(direction, v, curmsg, 0, recwinlines));
981    } else {
982      return(last);
983    }
984  }
985
986  /* Find number of lines from top to bottom of curmsg (store in savey) */
987  savey=0;
988  for (i=topmsg; i<=curmsg; i++) {
989    savey+=owl_message_get_numlines(owl_view_get_element(v, i));
990  }
991
992  /* if we're off the bottom of the screen, scroll down */
993  if (savey > recwinlines) {
994    if (center_on_page) {
995      return(owl_function_calculate_topmsg_center(direction, v, curmsg, 0, recwinlines));
996    } else {
997      return(curmsg);
998    }
999  }
1000
1001  /* else just stay as we are... */
1002  return(topmsg);
1003}
1004
1005
1006int owl_function_calculate_topmsg_normal(int direction, owl_view *v, int curmsg, int topmsg, int recwinlines)
1007{
1008  int savey, j, i, foo, y;
1009
1010  if (curmsg<0) return(topmsg);
1011   
1012  /* If we're off the top of the screen then center */
1013  if (curmsg<topmsg) {
1014    topmsg=owl_function_calculate_topmsg_center(direction, v, curmsg, 0, recwinlines);
1015  }
1016
1017  /* Find number of lines from top to bottom of curmsg (store in savey) */
1018  savey=0;
1019  for (i=topmsg; i<=curmsg; i++) {
1020    savey+=owl_message_get_numlines(owl_view_get_element(v, i));
1021  }
1022
1023  /* If we're off the bottom of the screen, set the topmsg to curmsg
1024   * and scroll upwards */
1025  if (savey > recwinlines) {
1026    topmsg=curmsg;
1027    savey=owl_message_get_numlines(owl_view_get_element(v, i));
1028    direction=OWL_DIRECTION_UPWARDS;
1029  }
1030 
1031  /* If our bottom line is less than 1/4 down the screen then scroll up */
1032  if (direction == OWL_DIRECTION_UPWARDS || direction == OWL_DIRECTION_NONE) {
1033    if (savey < (recwinlines / 4)) {
1034      y=0;
1035      for (j=curmsg; j>=0; j--) {
1036        foo=owl_message_get_numlines(owl_view_get_element(v, j));
1037        /* will we run the curmsg off the screen? */
1038        if ((foo+y) >= recwinlines) {
1039          j++;
1040          if (j>curmsg) j=curmsg;
1041          break;
1042        }
1043        /* have saved 1/2 the screen space? */
1044        y+=foo;
1045        if (y > (recwinlines / 2)) break;
1046      }
1047      if (j<0) j=0;
1048      return(j);
1049    }
1050  }
1051
1052  if (direction == OWL_DIRECTION_DOWNWARDS || direction == OWL_DIRECTION_NONE) {
1053    /* If curmsg bottom line is more than 3/4 down the screen then scroll down */
1054    if (savey > ((recwinlines * 3)/4)) {
1055      y=0;
1056      /* count lines from the top until we can save 1/2 the screen size */
1057      for (j=topmsg; j<curmsg; j++) {
1058        y+=owl_message_get_numlines(owl_view_get_element(v, j));
1059        if (y > (recwinlines / 2)) break;
1060      }
1061      if (j==curmsg) {
1062        j--;
1063      }
1064      return(j+1);
1065    }
1066  }
1067
1068  return(topmsg);
1069}
1070
1071
1072void owl_function_resize()
1073{
1074  owl_global_set_resize_pending(&g);
1075}
1076
1077
1078void owl_function_run_buffercommand()
1079{
1080  char *buff;
1081
1082  buff=owl_global_get_buffercommand(&g);
1083  if (!strncmp(buff, "zwrite ", 7)) {
1084    owl_function_zwrite(buff, owl_editwin_get_text(owl_global_get_typwin(&g)));
1085  } else if (!strncmp(buff, "aimwrite ", 9)) {
1086    owl_function_aimwrite(buff+9);
1087  }
1088}
1089
1090void owl_function_debugmsg(char *fmt, ...)
1091{
1092  FILE *file;
1093  time_t now;
1094  char buff1[LINE], buff2[LINE];
1095  va_list ap;
1096  va_start(ap, fmt);
1097
1098  if (!owl_global_is_debug_fast(&g)) return;
1099
1100  file=fopen(owl_global_get_debug_file(&g), "a");
1101  if (!file) return;
1102
1103  now=time(NULL);
1104  strcpy(buff1, ctime(&now));
1105  buff1[strlen(buff1)-1]='\0';
1106
1107  owl_global_get_runtime_string(&g, buff2);
1108 
1109  fprintf(file, "[%i -  %s - %s]: ", (int) getpid(), buff1, buff2);
1110  vfprintf(file, fmt, ap);
1111  fprintf(file, "\n");
1112  fclose(file);
1113
1114  va_end(ap);
1115}
1116
1117
1118void owl_function_refresh()
1119{
1120  owl_function_resize();
1121}
1122
1123void owl_function_beep()
1124{
1125  if (owl_global_is_bell(&g)) {
1126    beep();
1127    owl_global_set_needrefresh(&g); /* do we really need this? */
1128  }
1129}
1130
1131
1132void owl_function_subscribe(char *class, char *inst, char *recip)
1133{
1134  int ret;
1135
1136  ret=owl_zephyr_sub(class, inst, recip);
1137  if (ret) {
1138    owl_function_makemsg("Error subscribing.");
1139  } else {
1140    owl_function_makemsg("Subscribed.");
1141  }
1142}
1143
1144
1145void owl_function_unsubscribe(char *class, char *inst, char *recip)
1146{
1147  int ret;
1148
1149  ret=owl_zephyr_unsub(class, inst, recip);
1150  if (ret) {
1151    owl_function_makemsg("Error subscribing.");
1152  } else {
1153    owl_function_makemsg("Unsubscribed.");
1154  }
1155}
1156
1157
1158void owl_function_set_cursor(WINDOW *win)
1159{
1160  wnoutrefresh(win);
1161}
1162
1163
1164void owl_function_full_redisplay()
1165{
1166  redrawwin(owl_global_get_curs_recwin(&g));
1167  redrawwin(owl_global_get_curs_sepwin(&g));
1168  redrawwin(owl_global_get_curs_typwin(&g));
1169  redrawwin(owl_global_get_curs_msgwin(&g));
1170
1171  wnoutrefresh(owl_global_get_curs_recwin(&g));
1172  wnoutrefresh(owl_global_get_curs_sepwin(&g));
1173  wnoutrefresh(owl_global_get_curs_typwin(&g));
1174  wnoutrefresh(owl_global_get_curs_msgwin(&g));
1175 
1176  sepbar("");
1177  owl_function_makemsg("");
1178
1179  owl_global_set_needrefresh(&g);
1180}
1181
1182
1183void owl_function_popless_text(char *text)
1184{
1185  owl_popwin *pw;
1186  owl_viewwin *v;
1187
1188  pw=owl_global_get_popwin(&g);
1189  v=owl_global_get_viewwin(&g);
1190
1191  owl_popwin_up(pw);
1192  owl_viewwin_init_text(v, owl_popwin_get_curswin(pw),
1193                        owl_popwin_get_lines(pw), owl_popwin_get_cols(pw),
1194                        text);
1195  owl_popwin_refresh(pw);
1196  owl_viewwin_redisplay(v, 0);
1197  owl_global_set_needrefresh(&g);
1198}
1199
1200
1201void owl_function_popless_fmtext(owl_fmtext *fm)
1202{
1203  owl_popwin *pw;
1204  owl_viewwin *v;
1205
1206  pw=owl_global_get_popwin(&g);
1207  v=owl_global_get_viewwin(&g);
1208
1209  owl_popwin_up(pw);
1210  owl_viewwin_init_fmtext(v, owl_popwin_get_curswin(pw),
1211                   owl_popwin_get_lines(pw), owl_popwin_get_cols(pw),
1212                   fm);
1213  owl_popwin_refresh(pw);
1214  owl_viewwin_redisplay(v, 0);
1215  owl_global_set_needrefresh(&g);
1216}
1217
1218void owl_function_about()
1219{
1220  char buff[5000];
1221
1222  sprintf(buff, "This is owl version %s\n", OWL_VERSION_STRING);
1223  strcat(buff, "\nOwl was written by James Kretchmar at the Massachusetts\n");
1224  strcat(buff, "Institute of Technology.  The first version, 0.5, was\n");
1225  strcat(buff, "released in March 2002.\n");
1226  strcat(buff, "\n");
1227  strcat(buff, "The name 'owl' was chosen in reference to the owls in the\n");
1228  strcat(buff, "Harry Potter novels, who are tasked with carrying messages\n");
1229  strcat(buff, "between Witches and Wizards.\n");
1230  strcat(buff, "\n");
1231  strcat(buff, "Copyright 2002 Massachusetts Institute of Technology\n");
1232  strcat(buff, "\n");
1233  strcat(buff, "Permission to use, copy, modify, and distribute this\n");
1234  strcat(buff, "software and its documentation for any purpose and without\n");
1235  strcat(buff, "fee is hereby granted, provided that the above copyright\n");
1236  strcat(buff, "notice and this permission notice appear in all copies\n");
1237  strcat(buff, "and in supporting documentation.  No representation is\n");
1238  strcat(buff, "made about the suitability of this software for any\n");
1239  strcat(buff, "purpose.  It is provided \"as is\" without express\n");
1240  strcat(buff, "or implied warranty.\n");
1241  owl_function_popless_text(buff);
1242}
1243
1244void owl_function_info()
1245{
1246  owl_message *m;
1247  owl_fmtext fm, attrfm;
1248  ZNotice_t *n;
1249  char buff[10000], tmpbuff[1024];
1250  char *ptr;
1251  int i, j, fields, len;
1252  owl_view *v;
1253
1254  owl_fmtext_init_null(&fm);
1255 
1256  v=owl_global_get_current_view(&g);
1257  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
1258  if (!m || owl_view_get_size(v)==0) {
1259    owl_function_makemsg("No message selected\n");
1260    return;
1261  }
1262
1263  owl_fmtext_append_bold(&fm, "General Information:\n");
1264  owl_fmtext_append_normal(&fm, "  Msg Id    : ");
1265  sprintf(buff, "%i", owl_message_get_id(m));
1266  owl_fmtext_append_normal(&fm, buff);
1267  owl_fmtext_append_normal(&fm, "\n");
1268
1269  owl_fmtext_append_normal(&fm, "  Type      : ");
1270  owl_fmtext_append_bold(&fm, owl_message_type_to_string(m));
1271  owl_fmtext_append_normal(&fm, "\n");
1272
1273  if (owl_message_is_direction_in(m)) {
1274    owl_fmtext_append_normal(&fm, "  Direction : in\n");
1275  } else if (owl_message_is_direction_out(m)) {
1276    owl_fmtext_append_normal(&fm, "  Direction : out\n");
1277  } else if (owl_message_is_direction_none(m)) {
1278    owl_fmtext_append_normal(&fm, "  Direction : none\n");
1279  } else {
1280    owl_fmtext_append_normal(&fm, "  Direction : unknown\n");
1281  }
1282
1283  owl_fmtext_append_normal(&fm, "  Time      : ");
1284  owl_fmtext_append_normal(&fm, owl_message_get_timestr(m));
1285  owl_fmtext_append_normal(&fm, "\n");
1286
1287  if (!owl_message_is_type_admin(m)) {
1288    owl_fmtext_append_normal(&fm, "  Sender    : ");
1289    owl_fmtext_append_normal(&fm, owl_message_get_sender(m));
1290    owl_fmtext_append_normal(&fm, "\n");
1291   
1292    owl_fmtext_append_normal(&fm, "  Recipient : ");
1293    owl_fmtext_append_normal(&fm, owl_message_get_recipient(m));
1294    owl_fmtext_append_normal(&fm, "\n");
1295  }
1296   
1297  if (owl_message_is_type_zephyr(m)) {
1298    owl_fmtext_append_bold(&fm, "\nZephyr Specific Information:\n");
1299   
1300    owl_fmtext_append_normal(&fm, "  Class     : ");
1301    owl_fmtext_append_normal(&fm, owl_message_get_class(m));
1302    owl_fmtext_append_normal(&fm, "\n");
1303    owl_fmtext_append_normal(&fm, "  Instance  : ");
1304    owl_fmtext_append_normal(&fm, owl_message_get_instance(m));
1305    owl_fmtext_append_normal(&fm, "\n");
1306    owl_fmtext_append_normal(&fm, "  Opcode    : ");
1307    owl_fmtext_append_normal(&fm, owl_message_get_opcode(m));
1308    owl_fmtext_append_normal(&fm, "\n");
1309   
1310    owl_fmtext_append_normal(&fm, "  Time      : ");
1311    owl_fmtext_append_normal(&fm, owl_message_get_timestr(m));
1312    owl_fmtext_append_normal(&fm, "\n");
1313
1314    if (owl_message_is_direction_in(m)) {
1315      n=owl_message_get_notice(m);
1316
1317      owl_fmtext_append_normal(&fm, "  Kind      : ");
1318      if (n->z_kind==UNSAFE) {
1319        owl_fmtext_append_normal(&fm, "UNSAFE\n");
1320      } else if (n->z_kind==UNACKED) {
1321        owl_fmtext_append_normal(&fm, "UNACKED\n");
1322      } else if (n->z_kind==ACKED) {
1323        owl_fmtext_append_normal(&fm, "ACKED\n");
1324      } else if (n->z_kind==HMACK) {
1325        owl_fmtext_append_normal(&fm, "HMACK\n");
1326      } else if (n->z_kind==HMCTL) {
1327        owl_fmtext_append_normal(&fm, "HMCTL\n");
1328      } else if (n->z_kind==SERVACK) {
1329        owl_fmtext_append_normal(&fm, "SERVACK\n");
1330      } else if (n->z_kind==SERVNAK) {
1331        owl_fmtext_append_normal(&fm, "SERVNACK\n");
1332      } else if (n->z_kind==CLIENTACK) {
1333        owl_fmtext_append_normal(&fm, "CLIENTACK\n");
1334      } else if (n->z_kind==STAT) {
1335        owl_fmtext_append_normal(&fm, "STAT\n");
1336      } else {
1337        owl_fmtext_append_normal(&fm, "ILLEGAL VALUE\n");
1338      }
1339      owl_fmtext_append_normal(&fm, "  Host      : ");
1340      owl_fmtext_append_normal(&fm, owl_message_get_hostname(m));
1341      owl_fmtext_append_normal(&fm, "\n");
1342      sprintf(buff, "  Port      : %i\n", n->z_port);
1343      owl_fmtext_append_normal(&fm, buff);
1344
1345      owl_fmtext_append_normal(&fm,    "  Auth      : ");
1346      owl_fmtext_append_normal(&fm, owl_zephyr_get_authstr(n));
1347      owl_fmtext_append_normal(&fm, "\n");
1348
1349      /* fix this */
1350      sprintf(buff, "  Checkd Ath: %i\n", n->z_checked_auth);
1351      sprintf(buff, "%s  Multi notc: %s\n", buff, n->z_multinotice);
1352      sprintf(buff, "%s  Num other : %i\n", buff, n->z_num_other_fields);
1353      sprintf(buff, "%s  Msg Len   : %i\n", buff, n->z_message_len);
1354      owl_fmtext_append_normal(&fm, buff);
1355     
1356      sprintf(buff, "  Fields    : %i\n", owl_zephyr_get_num_fields(n));
1357      owl_fmtext_append_normal(&fm, buff);
1358     
1359      fields=owl_zephyr_get_num_fields(n);
1360      for (i=0; i<fields; i++) {
1361        sprintf(buff, "  Field %i   : ", i+1);
1362       
1363        ptr=owl_zephyr_get_field(n, i+1, &len);
1364        if (!ptr) break;
1365        if (len<30) {
1366          strncpy(tmpbuff, ptr, len);
1367          tmpbuff[len]='\0';
1368        } else {
1369          strncpy(tmpbuff, ptr, 30);
1370          tmpbuff[30]='\0';
1371          strcat(tmpbuff, "...");
1372        }
1373       
1374        for (j=0; j<strlen(tmpbuff); j++) {
1375          if (tmpbuff[j]=='\n') tmpbuff[j]='~';
1376          if (tmpbuff[j]=='\r') tmpbuff[j]='!';
1377        }
1378       
1379        strcat(buff, tmpbuff);
1380        strcat(buff, "\n");
1381        owl_fmtext_append_normal(&fm, buff);
1382      }
1383      owl_fmtext_append_normal(&fm, "  Default Fm:");
1384      owl_fmtext_append_normal(&fm, n->z_default_format);
1385    }
1386  }
1387
1388  if (owl_message_is_type_aim(m)) {
1389    owl_fmtext_append_bold(&fm, "\nAIM Specific Information:\n");
1390  }
1391
1392  owl_fmtext_append_bold(&fm, "\nOwl Message Attributes:\n");
1393  owl_message_attributes_tofmtext(m, &attrfm);
1394  owl_fmtext_append_fmtext(&fm, &attrfm);
1395 
1396  owl_function_popless_fmtext(&fm);
1397  owl_fmtext_free(&fm);
1398  owl_fmtext_free(&attrfm);
1399}
1400
1401
1402/* print the current message in a popup window.
1403 * Use the 'default' style regardless of whatever
1404 * style the user may be using
1405 */
1406void owl_function_curmsg_to_popwin()
1407{
1408  owl_popwin *pw;
1409  owl_view *v;
1410  owl_message *m;
1411  owl_style *s;
1412  owl_fmtext fm;
1413
1414  v=owl_global_get_current_view(&g);
1415  s=owl_global_get_style_by_name(&g, "default");
1416  pw=owl_global_get_popwin(&g);
1417
1418  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
1419
1420  if (!m || owl_view_get_size(v)==0) {
1421    owl_function_makemsg("No current message");
1422    return;
1423  }
1424
1425  owl_fmtext_init_null(&fm);
1426  owl_style_get_formattext(s, &fm, m);
1427
1428  owl_function_popless_fmtext(&fm);
1429  owl_fmtext_free(&fm);
1430}
1431
1432
1433void owl_function_page_curmsg(int step)
1434{
1435  /* scroll down or up within the current message IF the message is truncated */
1436
1437  int offset, curmsg, lines;
1438  owl_view *v;
1439  owl_message *m;
1440
1441  offset=owl_global_get_curmsg_vert_offset(&g);
1442  v=owl_global_get_current_view(&g);
1443  curmsg=owl_global_get_curmsg(&g);
1444  m=owl_view_get_element(v, curmsg);
1445  if (!m || owl_view_get_size(v)==0) return;
1446  lines=owl_message_get_numlines(m);
1447
1448  if (offset==0) {
1449    /* Bail if the curmsg isn't the last one displayed */
1450    if (curmsg != owl_mainwin_get_last_msg(owl_global_get_mainwin(&g))) {
1451      owl_function_makemsg("The entire message is already displayed");
1452      return;
1453    }
1454   
1455    /* Bail if we're not truncated */
1456    if (!owl_mainwin_is_curmsg_truncated(owl_global_get_mainwin(&g))) {
1457      owl_function_makemsg("The entire message is already displayed");
1458      return;
1459    }
1460  }
1461 
1462 
1463  /* don't scroll past the last line */
1464  if (step>0) {
1465    if (offset+step > lines-1) {
1466      owl_global_set_curmsg_vert_offset(&g, lines-1);
1467    } else {
1468      owl_global_set_curmsg_vert_offset(&g, offset+step);
1469    }
1470  }
1471
1472  /* would we be before the beginning of the message? */
1473  if (step<0) {
1474    if (offset+step<0) {
1475      owl_global_set_curmsg_vert_offset(&g, 0);
1476    } else {
1477      owl_global_set_curmsg_vert_offset(&g, offset+step);
1478    }
1479  }
1480 
1481  /* redisplay */
1482  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
1483  owl_global_set_needrefresh(&g);
1484}
1485
1486void owl_function_resize_typwin(int newsize)
1487{
1488  owl_global_set_typwin_lines(&g, newsize);
1489  owl_function_resize();
1490}
1491
1492void owl_function_typwin_grow()
1493{
1494  int i;
1495
1496  i=owl_global_get_typwin_lines(&g);
1497  owl_function_resize_typwin(i+1);
1498}
1499
1500void owl_function_typwin_shrink()
1501{
1502  int i;
1503
1504  i=owl_global_get_typwin_lines(&g);
1505  if (i>2) {
1506    owl_function_resize_typwin(i-1);
1507  }
1508}
1509
1510void owl_function_mainwin_pagedown()
1511{
1512  int i;
1513
1514  i=owl_mainwin_get_last_msg(owl_global_get_mainwin(&g));
1515  if (i<0) return;
1516  if (owl_mainwin_is_last_msg_truncated(owl_global_get_mainwin(&g))
1517      && (owl_global_get_curmsg(&g) < i)
1518      && (i>0)) {
1519    i--;
1520  }
1521  owl_global_set_curmsg(&g, i);
1522  owl_function_nextmsg();
1523}
1524
1525void owl_function_mainwin_pageup()
1526{
1527  owl_global_set_curmsg(&g, owl_global_get_topmsg(&g));
1528  owl_function_prevmsg();
1529}
1530
1531void owl_function_getsubs()
1532{
1533  int ret, num, i, one;
1534  ZSubscription_t sub;
1535  char *buff, *tmpbuff;
1536
1537  one = 1;
1538
1539  ret=ZRetrieveSubscriptions(0, &num);
1540  if (ret == ZERR_TOOMANYSUBS) {
1541    owl_function_makemsg("Zephyr: too many subscriptions");
1542    return;
1543  }
1544
1545  buff=owl_malloc(num*500);
1546  tmpbuff=owl_malloc(num*500);
1547  strcpy(buff, "");
1548  for (i=0; i<num; i++) {
1549    if ((ret = ZGetSubscriptions(&sub, &one)) != ZERR_NONE) {
1550      owl_function_makemsg("Error while getting subscriptions");
1551      owl_free(buff);
1552      owl_free(tmpbuff);
1553      ZFlushSubscriptions();
1554      return;
1555    } else {
1556      sprintf(tmpbuff, "<%s,%s,%s>\n%s", sub.zsub_class, sub.zsub_classinst, sub.zsub_recipient, buff);
1557      strcpy(buff, tmpbuff);
1558    }
1559  }
1560
1561  owl_function_popless_text(buff);
1562  owl_free(buff);
1563  owl_free(tmpbuff);
1564  ZFlushSubscriptions();
1565}
1566
1567#define PABUFLEN 5000
1568void owl_function_printallvars()
1569{
1570  char buff[PABUFLEN], *pos, *name;
1571  owl_list varnames;
1572  int i, numvarnames, rem;
1573
1574  pos = buff;
1575  pos += sprintf(pos, "%-20s = %s\n", "VARIABLE", "VALUE");
1576  pos += sprintf(pos, "%-20s   %s\n",  "--------", "-----");
1577  owl_variable_dict_get_names(owl_global_get_vardict(&g), &varnames);
1578  rem = (buff+PABUFLEN)-pos-1;
1579  numvarnames = owl_list_get_size(&varnames);
1580  for (i=0; i<numvarnames; i++) {
1581    name = owl_list_get_element(&varnames, i);
1582    if (name && name[0]!='_') {
1583      rem = (buff+PABUFLEN)-pos-1;   
1584      pos += snprintf(pos, rem, "\n%-20s = ", name);
1585      rem = (buff+PABUFLEN)-pos-1;   
1586      owl_variable_get_tostring(owl_global_get_vardict(&g), name, pos, rem);
1587      pos = buff+strlen(buff);
1588    }
1589  }
1590  rem = (buff+PABUFLEN)-pos-1;   
1591  snprintf(pos, rem, "\n");
1592  owl_variable_dict_namelist_free(&varnames);
1593 
1594  owl_function_popless_text(buff);
1595}
1596
1597void owl_function_show_variables()
1598{
1599  owl_list varnames;
1600  owl_fmtext fm; 
1601  int i, numvarnames;
1602  char *varname;
1603
1604  owl_fmtext_init_null(&fm);
1605  owl_fmtext_append_bold(&fm, 
1606      "Variables: (use 'show variable <name>' for details)\n");
1607  owl_variable_dict_get_names(owl_global_get_vardict(&g), &varnames);
1608  numvarnames = owl_list_get_size(&varnames);
1609  for (i=0; i<numvarnames; i++) {
1610    varname = owl_list_get_element(&varnames, i);
1611    if (varname && varname[0]!='_') {
1612      owl_variable_describe(owl_global_get_vardict(&g), varname, &fm);
1613    }
1614  }
1615  owl_variable_dict_namelist_free(&varnames);
1616  owl_function_popless_fmtext(&fm);
1617  owl_fmtext_free(&fm);
1618}
1619
1620void owl_function_show_variable(char *name)
1621{
1622  owl_fmtext fm; 
1623
1624  owl_fmtext_init_null(&fm);
1625  owl_variable_get_help(owl_global_get_vardict(&g), name, &fm);
1626  owl_function_popless_fmtext(&fm);
1627  owl_fmtext_free(&fm); 
1628}
1629
1630/* note: this applies to global message list, not to view.
1631 * If flag is 1, deletes.  If flag is 0, undeletes. */
1632void owl_function_delete_by_id(int id, int flag)
1633{
1634  owl_messagelist *ml;
1635  owl_message *m;
1636  ml = owl_global_get_msglist(&g);
1637  m = owl_messagelist_get_by_id(ml, id);
1638  if (m) {
1639    if (flag == 1) {
1640      owl_message_mark_delete(m);
1641    } else if (flag == 0) {
1642      owl_message_unmark_delete(m);
1643    }
1644    owl_mainwin_redisplay(owl_global_get_mainwin(&g));
1645    owl_global_set_needrefresh(&g);
1646  } else {
1647    owl_function_makemsg("No message with id %d: unable to mark for (un)delete",id);
1648  }
1649}
1650
1651void owl_function_delete_automsgs()
1652{
1653  /* mark for deletion all messages in the current view that match the
1654   * 'trash' filter */
1655
1656  int i, j, count;
1657  owl_message *m;
1658  owl_view *v;
1659  owl_filter *f;
1660
1661  /* get the trash filter */
1662  f=owl_global_get_filter(&g, "trash");
1663  if (!f) {
1664    owl_function_makemsg("No trash filter defined");
1665    return;
1666  }
1667
1668  v=owl_global_get_current_view(&g);
1669
1670  count=0;
1671  j=owl_view_get_size(v);
1672  for (i=0; i<j; i++) {
1673    m=owl_view_get_element(v, i);
1674    if (owl_filter_message_match(f, m)) {
1675      count++;
1676      owl_message_mark_delete(m);
1677    }
1678  }
1679  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
1680  owl_function_makemsg("%i messages marked for deletion", count);
1681  owl_global_set_needrefresh(&g);
1682}
1683
1684
1685void owl_function_status()
1686{
1687  char buff[5000];
1688  time_t start;
1689  int up, days, hours, minutes;
1690
1691  start=owl_global_get_starttime(&g);
1692
1693  sprintf(buff, "Version: %s\n", OWL_VERSION_STRING);
1694  sprintf(buff, "%sScreen size: %i lines, %i columns\n", buff, owl_global_get_lines(&g), owl_global_get_cols(&g));
1695  sprintf(buff, "%sStartup Arugments: %s\n", buff, owl_global_get_startupargs(&g));
1696  sprintf(buff, "%sStartup Time: %s", buff, ctime(&start));
1697
1698  up=owl_global_get_runtime(&g);
1699  days=up/86400;
1700  up-=days*86400;
1701  hours=up/3600;
1702  up-=hours*3600;
1703  minutes=up/60;
1704  up-=minutes*60;
1705  sprintf(buff, "%sRun Time: %i days %2.2i:%2.2i:%2.2i\n", buff, days, hours, minutes, up);
1706
1707  if (owl_global_get_hascolors(&g)) {
1708    sprintf(buff, "%sColor: Yes, %i color pairs.\n", buff, owl_global_get_colorpairs(&g));
1709  } else {
1710    strcat(buff, "Color: No.\n");
1711  }
1712
1713  sprintf(buff, "%sMemory Malloced: %i\n", buff, owl_global_get_malloced(&g));
1714  sprintf(buff, "%sMemory Freed: %i\n", buff, owl_global_get_freed(&g));
1715  sprintf(buff, "%sMemory In Use: %i\n", buff, owl_global_get_meminuse(&g));
1716
1717  owl_function_popless_text(buff);
1718}
1719
1720void owl_function_show_term()
1721{
1722  owl_fmtext fm;
1723  char buff[LINE];
1724
1725  owl_fmtext_init_null(&fm);
1726  sprintf(buff, "Terminal Lines: %i\nTerminal Columns: %i\n",
1727          owl_global_get_lines(&g),
1728          owl_global_get_cols(&g));
1729  owl_fmtext_append_normal(&fm, buff);
1730
1731  if (owl_global_get_hascolors(&g)) {
1732    owl_fmtext_append_normal(&fm, "Color: Yes\n");
1733    sprintf(buff, "Number of color pairs: %i\n", owl_global_get_colorpairs(&g));
1734    owl_fmtext_append_normal(&fm, buff);
1735    sprintf(buff, "Can change colors: %s\n", can_change_color() ? "yes" : "no");
1736    owl_fmtext_append_normal(&fm, buff);
1737  } else {
1738    owl_fmtext_append_normal(&fm, "Color: No\n");
1739  }
1740
1741  owl_function_popless_fmtext(&fm);
1742  owl_fmtext_free(&fm);
1743}
1744
1745
1746/* if type = 0 then normal reply.
1747 * if type = 1 then it's a reply to sender
1748 * if enter = 0 then allow the command to be edited
1749 * if enter = 1 then don't wait for editing
1750 */
1751void owl_function_reply(int type, int enter)
1752{
1753  char *buff, *oldbuff;
1754  owl_message *m;
1755  owl_filter *f;
1756 
1757  if (owl_view_get_size(owl_global_get_current_view(&g))==0) {
1758    owl_function_makemsg("No message selected");
1759  } else {
1760    char *class, *inst, *to, *cc=NULL;
1761   
1762    m=owl_view_get_element(owl_global_get_current_view(&g), owl_global_get_curmsg(&g));
1763    if (!m) {
1764      owl_function_makemsg("No message selected");
1765      return;
1766    }
1767
1768    /* first check if we catch the reply-lockout filter */
1769    f=owl_global_get_filter(&g, "reply-lockout");
1770    if (f) {
1771      if (owl_filter_message_match(f, m)) {
1772        owl_function_makemsg("Sorry, replies to this message have been disabled by the reply-lockout filter");
1773        return;
1774      }
1775    }
1776
1777    /* admin */
1778    if (owl_message_is_type_admin(m)) {
1779      owl_function_makemsg("You cannot reply to an admin message");
1780      return;
1781    }
1782
1783    /* zephyr */
1784    if (owl_message_is_type_zephyr(m)) {
1785      /* for now we disable replies to zcrypt messages, since we can't
1786         support an encrypted reply */
1787      if (!strcasecmp(owl_message_get_opcode(m), "crypt")) {
1788        owl_function_makemsg("Replies to zcrypt messages are not enabled in this release");
1789        return;
1790      }
1791
1792      /* if it's a zephyr we sent, send it out the same way again */
1793      if (owl_message_is_direction_out(m)) {
1794        owl_function_zwrite_setup(owl_message_get_zwriteline(m));
1795        owl_global_set_buffercommand(&g, owl_message_get_zwriteline(m));
1796        return;
1797      }
1798
1799      /* Special case a personal reply to a webzephyr user on a class */
1800      if ((type==1) && !strcasecmp(owl_message_get_opcode(m), OWL_WEBZEPHYR_OPCODE)) {
1801        class=OWL_WEBZEPHYR_CLASS;
1802        inst=owl_message_get_sender(m);
1803        to=OWL_WEBZEPHYR_PRINCIPAL;
1804      } else if (!strcasecmp(owl_message_get_class(m), OWL_WEBZEPHYR_CLASS) && owl_message_is_loginout(m)) {
1805        /* Special case LOGIN/LOGOUT notifications on class "webzephyr" */
1806        class=OWL_WEBZEPHYR_CLASS;
1807        inst=owl_message_get_instance(m);
1808        to=OWL_WEBZEPHYR_PRINCIPAL;
1809      } else if (owl_message_is_loginout(m)) {
1810        /* Normal LOGIN/LOGOUT messages */
1811        class="MESSAGE";
1812        inst="PERSONAL";
1813        to=owl_message_get_sender(m);
1814      } else if (type==1) {
1815        /* Personal reply */
1816        class="MESSAGE";
1817        inst="PERSONAL";
1818        to=owl_message_get_sender(m);
1819      } else {
1820        /* General reply */
1821        class=owl_message_get_class(m);
1822        inst=owl_message_get_instance(m);
1823        to=owl_message_get_recipient(m);
1824        cc=owl_message_get_cc(m);
1825        if (!strcmp(to, "") || !strcmp(to, "*")) {
1826          to="";
1827        } else if (to[0]=='@') {
1828          /* leave it, to get the realm */
1829        } else {
1830          to=owl_message_get_sender(m);
1831        }
1832      }
1833       
1834      /* create the command line */
1835      buff = owl_strdup("zwrite");
1836      if (strcasecmp(class, "message")) {
1837        buff = owl_sprintf("%s -c %s%s%s", oldbuff=buff, owl_getquoting(class), class, owl_getquoting(class));
1838        owl_free(oldbuff);
1839      }
1840      if (strcasecmp(inst, "personal")) {
1841        buff = owl_sprintf("%s -i %s%s%s", oldbuff=buff, owl_getquoting(inst), inst, owl_getquoting(inst));
1842        owl_free(oldbuff);
1843      }
1844      if (*to != '\0') {
1845        char *tmp, *oldtmp, *tmp2;
1846        tmp=short_zuser(to);
1847        if (cc) {
1848          tmp = owl_util_uniq(oldtmp=tmp, cc, "-");
1849          owl_free(oldtmp);
1850          buff = owl_sprintf("%s -C %s", oldbuff=buff, tmp);
1851          owl_free(oldbuff);
1852        } else {
1853          if (owl_global_is_smartstrip(&g)) {
1854            tmp2=tmp;
1855            tmp=owl_util_smartstripped_user(tmp2);
1856            owl_free(tmp2);
1857          }
1858          buff = owl_sprintf("%s %s", oldbuff=buff, tmp);
1859          owl_free(oldbuff);
1860        }
1861        owl_free(tmp);
1862      }
1863      if (cc) owl_free(cc);
1864    }
1865
1866    /* aim */
1867    if (owl_message_is_type_aim(m)) {
1868      if (owl_message_is_direction_out(m)) {
1869        buff=owl_sprintf("aimwrite %s", owl_message_get_recipient(m));
1870      } else {
1871        buff=owl_sprintf("aimwrite %s", owl_message_get_sender(m));
1872      }
1873    }
1874   
1875    if (enter) {
1876      owl_history *hist = owl_global_get_cmd_history(&g);
1877      owl_history_store(hist, buff);
1878      owl_history_reset(hist);
1879      owl_function_command_norv(buff);
1880    } else {
1881      owl_function_start_command(buff);
1882    }
1883    owl_free(buff);
1884  }
1885}
1886
1887void owl_function_zlocate(int argc, char **argv, int auth)
1888{
1889  owl_fmtext fm;
1890  char *ptr, buff[LINE];
1891  int i;
1892
1893  owl_fmtext_init_null(&fm);
1894
1895  for (i=0; i<argc; i++) {
1896    ptr=long_zuser(argv[i]);
1897    owl_zephyr_zlocate(ptr, buff, auth);
1898    owl_fmtext_append_normal(&fm, buff);
1899    owl_free(ptr);
1900  }
1901
1902  owl_function_popless_fmtext(&fm);
1903  owl_fmtext_free(&fm);
1904}
1905
1906void owl_function_start_command(char *line)
1907{
1908  int i, j;
1909  owl_editwin *tw;
1910
1911  tw=owl_global_get_typwin(&g);
1912  owl_global_set_typwin_active(&g);
1913  owl_editwin_new_style(tw, OWL_EDITWIN_STYLE_ONELINE, 
1914                        owl_global_get_cmd_history(&g));
1915
1916  owl_editwin_set_locktext(tw, "command: ");
1917  owl_global_set_needrefresh(&g);
1918
1919  j=strlen(line);
1920  for (i=0; i<j; i++) {
1921    owl_editwin_process_char(tw, line[i]);
1922  }
1923  owl_editwin_redisplay(tw, 0);
1924
1925  owl_context_set_editline(owl_global_get_context(&g), tw);
1926  owl_function_activate_keymap("editline");
1927   
1928}
1929
1930void owl_function_start_question(char *line)
1931{
1932  owl_editwin *tw;
1933
1934  tw=owl_global_get_typwin(&g);
1935  owl_global_set_typwin_active(&g);
1936  owl_editwin_new_style(tw, OWL_EDITWIN_STYLE_ONELINE, owl_global_get_cmd_history(&g));
1937
1938  owl_editwin_set_locktext(tw, line);
1939  owl_global_set_needrefresh(&g);
1940
1941  owl_editwin_redisplay(tw, 0);
1942
1943  owl_context_set_editresponse(owl_global_get_context(&g), tw);
1944  owl_function_activate_keymap("editresponse");
1945}
1946
1947char *owl_function_exec(int argc, char **argv, char *buff, int type)
1948{
1949  /* if type == 1 display in a popup
1950   * if type == 2 display an admin messages
1951   * if type == 0 return output
1952   * else display in a popup
1953   */
1954  char *newbuff, *redirect = " 2>&1 < /dev/null";
1955  char *out, buff2[1024];
1956  int size;
1957  FILE *p;
1958
1959  if (argc<2) {
1960    owl_function_makemsg("Wrong number of arguments to the exec command");
1961    return NULL;
1962  }
1963
1964  buff = skiptokens(buff, 1);
1965  newbuff = owl_malloc(strlen(buff)+strlen(redirect)+1);
1966  strcpy(newbuff, buff);
1967  strcat(newbuff, redirect);
1968
1969  p=popen(newbuff, "r");
1970  out=owl_malloc(1024);
1971  size=1024;
1972  strcpy(out, "");
1973  while (fgets(buff2, 1024, p)!=NULL) {
1974    size+=1024;
1975    out=owl_realloc(out, size);
1976    strcat(out, buff2);
1977  }
1978  pclose(p);
1979
1980  if (type==1) {
1981    owl_function_popless_text(out);
1982  } else if (type==0) {
1983    return out;
1984  } else if (type==2) {
1985    owl_function_adminmsg(buff, out);
1986  } else {
1987    owl_function_popless_text(out);
1988  }
1989  owl_free(out);
1990  return NULL;
1991}
1992
1993
1994char *owl_function_perl(int argc, char **argv, char *buff, int type)
1995{
1996  /* if type == 1 display in a popup
1997   * if type == 2 display an admin messages
1998   * if type == 0 return output
1999   * else display in a popup
2000   */
2001  char *perlout;
2002
2003  if (argc<2) {
2004    owl_function_makemsg("Wrong number of arguments to perl command");
2005    return NULL;
2006  }
2007
2008  /* consume first token (argv[0]) */
2009  buff = skiptokens(buff, 1);
2010
2011  perlout = owl_config_execute(buff);
2012  if (perlout) { 
2013    if (type==1) {
2014      owl_function_popless_text(perlout);
2015    } else if (type==2) {
2016      owl_function_adminmsg(buff, perlout);
2017    } else if (type==0) {
2018      return perlout;
2019    } else {
2020      owl_function_popless_text(perlout);
2021    }
2022    owl_free(perlout);
2023  }
2024  return NULL;
2025}
2026
2027#if 0
2028void owl_function_change_view_old(char *filtname)
2029{
2030  owl_view *v;
2031  owl_filter *f;
2032  int curid=-1, newpos, curmsg;
2033  owl_message *curm=NULL;
2034
2035  v=owl_global_get_current_view(&g);
2036  curmsg=owl_global_get_curmsg(&g);
2037  if (curmsg==-1) {
2038    owl_function_debugmsg("Hit the curmsg==-1 case in change_view");
2039  } else {
2040    curm=owl_view_get_element(v, curmsg);
2041    if (curm) {
2042      curid=owl_message_get_id(curm);
2043      owl_view_save_curmsgid(v, curid);
2044    }
2045  }
2046
2047  /* grab the filter */;
2048  f=owl_global_get_filter(&g, filtname);
2049  if (!f) {
2050    owl_function_makemsg("Unknown filter");
2051    return;
2052  }
2053
2054  /* free the existing view and create a new one based on the filter */
2055  owl_view_free(v);
2056  owl_view_create(v, f);
2057
2058  /* Figure out what to set the current message to.
2059   * - If the view we're leaving has messages in it, go to the closest message
2060   *   to the last message pointed to in that view.
2061   * - If the view we're leaving is empty, try to restore the position
2062   *   from the last time we were in the new view.  */
2063  if (curm) {
2064    newpos = owl_view_get_nearest_to_msgid(v, curid);
2065  } else {
2066    newpos = owl_view_get_nearest_to_saved(v);
2067  }
2068
2069  owl_global_set_curmsg(&g, newpos);
2070
2071  owl_function_calculate_topmsg(OWL_DIRECTION_DOWNWARDS);
2072  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2073  owl_global_set_direction_downwards(&g);
2074}
2075#endif
2076
2077void owl_function_change_view(char *filtname)
2078{
2079  owl_view *v;
2080  owl_filter *f;
2081  int curid=-1, newpos, curmsg;
2082  owl_message *curm=NULL;
2083
2084  v=owl_global_get_current_view(&g);
2085
2086  curmsg=owl_global_get_curmsg(&g);
2087  if (curmsg==-1) {
2088    owl_function_debugmsg("Hit the curmsg==-1 case in change_view");
2089  } else {
2090    curm=owl_view_get_element(v, curmsg);
2091    if (curm) {
2092      curid=owl_message_get_id(curm);
2093      owl_view_save_curmsgid(v, curid);
2094    }
2095  }
2096
2097  f=owl_global_get_filter(&g, filtname);
2098  if (!f) {
2099    owl_function_makemsg("Unknown filter");
2100    return;
2101  }
2102
2103  owl_view_new_filter(v, f);
2104
2105  /* Figure out what to set the current message to.
2106   * - If the view we're leaving has messages in it, go to the closest message
2107   *   to the last message pointed to in that view.
2108   * - If the view we're leaving is empty, try to restore the position
2109   *   from the last time we were in the new view.  */
2110  if (curm) {
2111    newpos = owl_view_get_nearest_to_msgid(v, curid);
2112  } else {
2113    newpos = owl_view_get_nearest_to_saved(v);
2114  }
2115
2116  owl_global_set_curmsg(&g, newpos);
2117  owl_function_calculate_topmsg(OWL_DIRECTION_DOWNWARDS);
2118  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2119  owl_global_set_direction_downwards(&g);
2120}
2121
2122void owl_function_create_filter(int argc, char **argv)
2123{
2124  owl_filter *f;
2125  owl_view *v;
2126  int ret, inuse=0;
2127
2128  if (argc < 2) {
2129    owl_function_makemsg("Wrong number of arguments to filter command");
2130    return;
2131  }
2132
2133  v=owl_global_get_current_view(&g);
2134
2135  /* don't touch the all filter */
2136  if (!strcmp(argv[1], "all")) {
2137    owl_function_makemsg("You may not change the 'all' filter.");
2138    return;
2139  }
2140
2141  /* deal with the case of trying change the filter color */
2142  if (argc==4 && !strcmp(argv[2], "-c")) {
2143    f=owl_global_get_filter(&g, argv[1]);
2144    if (!f) {
2145      owl_function_makemsg("The filter '%s' does not exist.", argv[1]);
2146      return;
2147    }
2148    owl_filter_set_color(f, owl_util_string_to_color(argv[3]));
2149    owl_global_set_needrefresh(&g);
2150    owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2151    return;
2152  }
2153
2154  /* create the filter and check for errors */
2155  f=owl_malloc(sizeof(owl_filter));
2156  ret=owl_filter_init(f, argv[1], argc-2, argv+2);
2157  if (ret==-1) {
2158    owl_free(f);
2159    owl_function_makemsg("Invalid filter syntax");
2160    return;
2161  }
2162
2163  /* if the named filter is in use by the current view, remember it */
2164  if (!strcmp(owl_view_get_filtname(v), argv[1])) {
2165    inuse=1;
2166  }
2167
2168  /* if the named filter already exists, nuke it */
2169  if (owl_global_get_filter(&g, argv[1])) {
2170    owl_global_remove_filter(&g, argv[1]);
2171  }
2172
2173  /* add the filter */
2174  owl_global_add_filter(&g, f);
2175
2176  /* if it was in use by the current view then update */
2177  if (inuse) {
2178    owl_function_change_view(argv[1]);
2179  }
2180  owl_global_set_needrefresh(&g);
2181  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2182}
2183
2184void owl_function_show_filters()
2185{
2186  owl_list *l;
2187  owl_filter *f;
2188  int i, j;
2189  owl_fmtext fm;
2190
2191  owl_fmtext_init_null(&fm);
2192
2193  l=owl_global_get_filterlist(&g);
2194  j=owl_list_get_size(l);
2195
2196  owl_fmtext_append_bold(&fm, "Filters:\n");
2197
2198  for (i=0; i<j; i++) {
2199    f=owl_list_get_element(l, i);
2200    owl_fmtext_append_normal(&fm, "   ");
2201    if (owl_global_get_hascolors(&g)) {
2202      owl_fmtext_append_normal_color(&fm, owl_filter_get_name(f), owl_filter_get_color(f));
2203    } else {
2204      owl_fmtext_append_normal(&fm, owl_filter_get_name(f));
2205    }
2206    owl_fmtext_append_normal(&fm, "\n");
2207  }
2208  owl_function_popless_fmtext(&fm);
2209  owl_fmtext_free(&fm);
2210}
2211
2212void owl_function_show_filter(char *name)
2213{
2214  owl_filter *f;
2215  char buff[5000];
2216
2217  f=owl_global_get_filter(&g, name);
2218  if (!f) {
2219    owl_function_makemsg("There is no filter with that name");
2220    return;
2221  }
2222  owl_filter_print(f, buff);
2223  owl_function_popless_text(buff);
2224}
2225
2226void owl_function_show_zpunts()
2227{
2228  owl_filter *f;
2229  owl_list *fl;
2230  char buff[5000];
2231  owl_fmtext fm;
2232  int i, j;
2233
2234  owl_fmtext_init_null(&fm);
2235
2236  fl=owl_global_get_puntlist(&g);
2237  j=owl_list_get_size(fl);
2238  owl_fmtext_append_bold(&fm, "Active zpunt filters:\n");
2239
2240  for (i=0; i<j; i++) {
2241    f=owl_list_get_element(fl, i);
2242    owl_filter_print(f, buff);
2243    owl_fmtext_append_normal(&fm, buff);
2244  }
2245  owl_function_popless_fmtext(&fm);
2246  owl_fmtext_free(&fm);
2247}
2248
2249/* Create a filter for a class, instance if one doesn't exist.  If
2250 * instance is NULL then catch all messgaes in the class.  Returns the
2251 * name of the filter, which the caller must free.
2252 */
2253char *owl_function_classinstfilt(char *class, char *instance) 
2254{
2255  owl_list *fl;
2256  owl_filter *f;
2257  char *argbuff, *filtname;
2258  char *tmpclass, *tmpinstance = NULL;
2259  int len;
2260
2261  fl=owl_global_get_filterlist(&g);
2262
2263  /* name for the filter */
2264  len=strlen(class)+30;
2265  if (instance) len+=strlen(instance);
2266  filtname=owl_malloc(len);
2267  if (!instance) {
2268    sprintf(filtname, "class-%s", class);
2269  } else {
2270    sprintf(filtname, "class-%s-instance-%s", class, instance);
2271  }
2272  /* downcase it */
2273  downstr(filtname);
2274  /* turn spaces into hyphens */
2275  owl_util_tr(filtname, ' ', '.');
2276 
2277  /* if it already exists then go with it.  This lets users override */
2278  if (owl_global_get_filter(&g, filtname)) {
2279    return(filtname);
2280  }
2281
2282  /* create the new filter */
2283  argbuff=owl_malloc(len+20);
2284  tmpclass=owl_strdup(class);
2285  owl_util_tr(tmpclass, ' ', '.');
2286  if (instance) {
2287    tmpinstance=owl_strdup(instance);
2288    owl_util_tr(tmpinstance, ' ', '.');
2289  }
2290  sprintf(argbuff, "( class ^%s$ )", tmpclass);
2291  if (tmpinstance) {
2292    sprintf(argbuff, "%s and ( instance ^%s$ )", argbuff, tmpinstance);
2293  }
2294  owl_free(tmpclass);
2295  if (tmpinstance) owl_free(tmpinstance);
2296
2297  f=owl_malloc(sizeof(owl_filter));
2298  owl_filter_init_fromstring(f, filtname, argbuff);
2299
2300  /* add it to the global list */
2301  owl_global_add_filter(&g, f);
2302
2303  owl_free(argbuff);
2304  return(filtname);
2305}
2306
2307/* Create a filter for personal zephyrs to or from the specified
2308 * zephyr user.  Includes login/logout notifications for the user.
2309 * The name of the filter will be 'user-<user>'.  If a filter already
2310 * exists with this name, no new filter will be created.  This allows
2311 * the configuration to override this function.  Returns the name of
2312 * the filter, which the caller must free.
2313 */
2314char *owl_function_zuserfilt(char *user)
2315{
2316  owl_filter *f;
2317  char *argbuff, *longuser, *shortuser, *filtname;
2318
2319  /* stick the local realm on if it's not there */
2320  longuser=long_zuser(user);
2321  shortuser=short_zuser(user);
2322
2323  /* name for the filter */
2324  filtname=owl_malloc(strlen(shortuser)+20);
2325  sprintf(filtname, "user-%s", shortuser);
2326
2327  /* if it already exists then go with it.  This lets users override */
2328  if (owl_global_get_filter(&g, filtname)) {
2329    return(owl_strdup(filtname));
2330  }
2331
2332  /* create the new-internal filter */
2333  f=owl_malloc(sizeof(owl_filter));
2334
2335  argbuff=owl_malloc(strlen(longuser)+1000);
2336  sprintf(argbuff, "( type ^zephyr$ and ( class ^message$ and instance ^personal$ and ");
2337  sprintf(argbuff, "%s ( ( direction ^in$ and sender ^%s$ ) or ( direction ^out$ and recipient ^%s$ ) ) )", argbuff, longuser, longuser);
2338  sprintf(argbuff, "%s or ( ( class ^login$ ) and ( sender ^%s$ ) ) )", argbuff, longuser);
2339
2340  owl_filter_init_fromstring(f, filtname, argbuff);
2341
2342  /* add it to the global list */
2343  owl_global_add_filter(&g, f);
2344
2345  /* free stuff */
2346  owl_free(argbuff);
2347  owl_free(longuser);
2348  owl_free(shortuser);
2349
2350  return(filtname);
2351}
2352
2353/* Create a filter for AIM IM messages to or from the specified
2354 * screenname.  The name of the filter will be 'aimuser-<user>'.  If a
2355 * filter already exists with this name, no new filter will be
2356 * created.  This allows the configuration to override this function.
2357 * Returns the name of the filter, which the caller must free.
2358 */
2359char *owl_function_aimuserfilt(char *user)
2360{
2361  owl_filter *f;
2362  char *argbuff, *filtname;
2363
2364  /* name for the filter */
2365  filtname=owl_malloc(strlen(user)+40);
2366  sprintf(filtname, "aimuser-%s", user);
2367
2368  /* if it already exists then go with it.  This lets users override */
2369  if (owl_global_get_filter(&g, filtname)) {
2370    return(owl_strdup(filtname));
2371  }
2372
2373  /* create the new-internal filter */
2374  f=owl_malloc(sizeof(owl_filter));
2375
2376  argbuff=owl_malloc(1000);
2377  sprintf(argbuff,
2378          "( type ^aim$ and ( ( sender ^%s$ and recipient ^%s$ ) or ( sender ^%s$ and recipient ^%s$ ) ) )",
2379          user, owl_global_get_aim_screenname(&g), owl_global_get_aim_screenname(&g), user);
2380
2381  owl_filter_init_fromstring(f, filtname, argbuff);
2382
2383  /* add it to the global list */
2384  owl_global_add_filter(&g, f);
2385
2386  /* free stuff */
2387  owl_free(argbuff);
2388
2389  return(filtname);
2390}
2391
2392char *owl_function_typefilt(char *type)
2393{
2394  owl_filter *f;
2395  char *argbuff, *filtname;
2396
2397  /* name for the filter */
2398  filtname=owl_sprintf("type-%s", type);
2399
2400  /* if it already exists then go with it.  This lets users override */
2401  if (owl_global_get_filter(&g, filtname)) {
2402    return filtname;
2403  }
2404
2405  /* create the new-internal filter */
2406  f=owl_malloc(sizeof(owl_filter));
2407
2408  argbuff = owl_sprintf("type ^%s$", type);
2409
2410  owl_filter_init_fromstring(f, filtname, argbuff);
2411
2412  /* add it to the global list */
2413  owl_global_add_filter(&g, f);
2414
2415  /* free stuff */
2416  owl_free(argbuff);
2417
2418  return filtname;
2419}
2420
2421/* If flag is 1, marks for deletion.  If flag is 0,
2422 * unmarks for deletion. */
2423void owl_function_delete_curview_msgs(int flag)
2424{
2425  owl_view *v;
2426  int i, j;
2427
2428  v=owl_global_get_current_view(&g);
2429  j=owl_view_get_size(v);
2430  for (i=0; i<j; i++) {
2431    if (flag == 1) {
2432      owl_message_mark_delete(owl_view_get_element(v, i));
2433    } else if (flag == 0) {
2434      owl_message_unmark_delete(owl_view_get_element(v, i));
2435    }
2436  }
2437
2438  owl_function_makemsg("%i messages marked for %sdeletion", j, flag?"":"un");
2439
2440  owl_mainwin_redisplay(owl_global_get_mainwin(&g)); 
2441}
2442
2443/* Create a filter based on the current message.  Returns the name of
2444 * a filter or null.  The caller must free this name.
2445 *
2446 * if the curmsg is a personal zephyr return a filter name
2447 *    to the zephyr converstaion with that user.
2448 * If the curmsg is a zephyr class message, instance foo, recip *,
2449 *    return a filter name to the class, inst.
2450 * If the curmsg is a zephyr class message and type==0 then
2451 *    return a filter name for just the class.
2452 * If the curmsg is a zephyr class message and type==1 then
2453 *    return a filter name for the class and instance.
2454 * If the curmsg is a personal AIM message returna  filter
2455 *    name to the AIM conversation with that user
2456 */
2457char *owl_function_smartfilter(int type)
2458{
2459  owl_view *v;
2460  owl_message *m;
2461  char *zperson, *filtname=NULL;
2462 
2463  v=owl_global_get_current_view(&g);
2464  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
2465
2466  if (!m || owl_view_get_size(v)==0) {
2467    owl_function_makemsg("No message selected\n");
2468    return(NULL);
2469  }
2470
2471  /* very simple handling of admin messages for now */
2472  if (owl_message_is_type_admin(m)) {
2473    return(owl_function_typefilt("admin"));
2474  }
2475
2476  /* aim messages */
2477  if (owl_message_is_type_aim(m)) {
2478    if (owl_message_is_direction_in(m)) {
2479      filtname=owl_function_aimuserfilt(owl_message_get_sender(m));
2480    } else if (owl_message_is_direction_out(m)) {
2481      filtname=owl_function_aimuserfilt(owl_message_get_recipient(m));
2482    }
2483    return(filtname);
2484  }
2485
2486  /* narrow personal and login messages to the sender or recip as appropriate */
2487  if (owl_message_is_personal(m) || owl_message_is_loginout(m)) {
2488    if (owl_message_is_type_zephyr(m)) {
2489      if (owl_message_is_direction_in(m)) {
2490        zperson=short_zuser(owl_message_get_sender(m));
2491      } else {
2492        zperson=short_zuser(owl_message_get_recipient(m));
2493      }
2494      filtname=owl_function_zuserfilt(zperson);
2495      owl_free(zperson);
2496      return(filtname);
2497    }
2498    return(NULL);
2499  }
2500
2501  /* narrow class MESSAGE, instance foo, recip * messages to class, inst */
2502  if (!strcasecmp(owl_message_get_class(m), "message") && !owl_message_is_personal(m)) {
2503    filtname=owl_function_classinstfilt(owl_message_get_class(m), owl_message_get_instance(m));
2504    return(filtname);
2505  }
2506
2507  /* otherwise narrow to the class */
2508  if (type==0) {
2509    filtname=owl_function_classinstfilt(owl_message_get_class(m), NULL);
2510  } else if (type==1) {
2511    filtname=owl_function_classinstfilt(owl_message_get_class(m), owl_message_get_instance(m));
2512  }
2513  return(filtname);
2514}
2515
2516void owl_function_smartzpunt(int type)
2517{
2518  /* Starts a zpunt command based on the current class,instance pair.
2519   * If type=0, uses just class.  If type=1, uses instance as well. */
2520  owl_view *v;
2521  owl_message *m;
2522  char *cmd, *cmdprefix, *mclass, *minst;
2523 
2524  v=owl_global_get_current_view(&g);
2525  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
2526
2527  if (!m || owl_view_get_size(v)==0) {
2528    owl_function_makemsg("No message selected\n");
2529    return;
2530  }
2531
2532  /* for now we skip admin messages. */
2533  if (owl_message_is_type_admin(m)
2534      || owl_message_is_loginout(m)
2535      || !owl_message_is_type_zephyr(m)) {
2536    owl_function_makemsg("smartzpunt doesn't support this message type.");
2537    return;
2538  }
2539
2540  mclass = owl_message_get_class(m);
2541  minst = owl_message_get_instance(m);
2542  if (!mclass || !*mclass || *mclass==' '
2543      || (!strcasecmp(mclass, "message") && !strcasecmp(minst, "personal"))
2544      || (type && (!minst || !*minst|| *minst==' '))) {
2545    owl_function_makemsg("smartzpunt can't safely do this for <%s,%s>",
2546                         mclass, minst);
2547  } else {
2548    cmdprefix = "start-command zpunt ";
2549    cmd = owl_malloc(strlen(cmdprefix)+strlen(mclass)+strlen(minst)+3);
2550    strcpy(cmd, cmdprefix);
2551    strcat(cmd, mclass);
2552    if (type) {
2553      strcat(cmd, " ");
2554      strcat(cmd, minst);
2555    } else {
2556      strcat(cmd, " *");
2557    }
2558    owl_function_command(cmd);
2559    owl_free(cmd);
2560  }
2561}
2562
2563
2564
2565void owl_function_color_current_filter(char *color)
2566{
2567  owl_filter *f;
2568  char *name;
2569
2570  name=owl_view_get_filtname(owl_global_get_current_view(&g));
2571  f=owl_global_get_filter(&g, name);
2572  if (!f) {
2573    owl_function_makemsg("Unknown filter");
2574    return;
2575  }
2576
2577  /* don't touch the all filter */
2578  if (!strcmp(name, "all")) {
2579    owl_function_makemsg("You may not change the 'all' filter.");
2580    return;
2581  }
2582
2583  /* deal with the case of trying change the filter color */
2584  owl_filter_set_color(f, owl_util_string_to_color(color));
2585  owl_global_set_needrefresh(&g);
2586  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2587}
2588
2589void owl_function_show_colors()
2590{
2591  owl_fmtext fm;
2592
2593  owl_fmtext_init_null(&fm);
2594  owl_fmtext_append_normal_color(&fm, "default\n", OWL_COLOR_DEFAULT);
2595  owl_fmtext_append_normal_color(&fm, "red\n", OWL_COLOR_RED);
2596  owl_fmtext_append_normal_color(&fm, "green\n", OWL_COLOR_GREEN);
2597  owl_fmtext_append_normal_color(&fm, "yellow\n", OWL_COLOR_YELLOW);
2598  owl_fmtext_append_normal_color(&fm, "blue\n", OWL_COLOR_BLUE);
2599  owl_fmtext_append_normal_color(&fm, "magenta\n", OWL_COLOR_MAGENTA);
2600  owl_fmtext_append_normal_color(&fm, "cyan\n", OWL_COLOR_CYAN);
2601  owl_fmtext_append_normal_color(&fm, "white\n", OWL_COLOR_WHITE);
2602
2603  owl_function_popless_fmtext(&fm);
2604  owl_fmtext_free(&fm);
2605}
2606
2607/* add the given class, inst, recip to the punt list for filtering.
2608 *   if direction==0 then punt
2609 *   if direction==1 then unpunt
2610 */
2611void owl_function_zpunt(char *class, char *inst, char *recip, int direction)
2612{
2613  owl_filter *f;
2614  owl_list *fl;
2615  char *buff;
2616  int ret, i, j;
2617
2618  fl=owl_global_get_puntlist(&g);
2619
2620  /* first, create the filter */
2621  f=malloc(sizeof(owl_filter));
2622  buff=malloc(strlen(class)+strlen(inst)+strlen(recip)+100);
2623  strcpy(buff, "class");
2624  if (!strcmp(class, "*")) {
2625    strcat(buff, " .*");
2626  } else {
2627    sprintf(buff, "%s ^%s$", buff, class);
2628  }
2629  if (!strcmp(inst, "*")) {
2630    strcat(buff, " and instance .*");
2631  } else {
2632    sprintf(buff, "%s and instance ^%s$", buff, inst);
2633  }
2634  if (strcmp(recip, "*")) {
2635    sprintf(buff, "%s and recipient ^%s$", buff, recip);
2636  }
2637 
2638  owl_function_debugmsg("About to filter %s", buff);
2639  ret=owl_filter_init_fromstring(f, "punt-filter", buff);
2640  owl_free(buff);
2641  if (ret) {
2642    owl_function_makemsg("Error creating filter for zpunt");
2643    owl_filter_free(f);
2644    return;
2645  }
2646
2647  /* Check for an identical filter */
2648  j=owl_list_get_size(fl);
2649  for (i=0; i<j; i++) {
2650    if (owl_filter_equiv(f, owl_list_get_element(fl, i))) {
2651      /* if we're punting, then just silently bow out on this duplicate */
2652      if (direction==0) {
2653        owl_filter_free(f);
2654        return;
2655      }
2656
2657      /* if we're unpunting, then remove this filter from the puntlist */
2658      if (direction==1) {
2659        owl_filter_free(owl_list_get_element(fl, i));
2660        owl_list_remove_element(fl, i);
2661        return;
2662      }
2663    }
2664  }
2665
2666  /* If we're punting, add the filter to the global punt list */
2667  if (direction==0) {
2668    owl_list_append_element(fl, f);
2669  }
2670}
2671
2672void owl_function_activate_keymap(char *keymap)
2673{
2674  if (!owl_keyhandler_activate(owl_global_get_keyhandler(&g), keymap)) {
2675    owl_function_makemsg("Unable to activate keymap '%s'", keymap);
2676  }
2677}
2678
2679
2680void owl_function_show_keymaps()
2681{
2682  owl_list l;
2683  owl_fmtext fm;
2684  owl_keymap *km;
2685  owl_keyhandler *kh;
2686  int i, numkm;
2687  char *kmname;
2688
2689  kh = owl_global_get_keyhandler(&g);
2690  owl_fmtext_init_null(&fm);
2691  owl_fmtext_append_bold(&fm, "Keymaps:   ");
2692  owl_fmtext_append_normal(&fm, "(use 'show keymap <name>' for details)\n");
2693  owl_keyhandler_get_keymap_names(kh, &l);
2694  owl_fmtext_append_list(&fm, &l, "\n", owl_function_keymap_summary);
2695  owl_fmtext_append_normal(&fm, "\n");
2696
2697  numkm = owl_list_get_size(&l);
2698  for (i=0; i<numkm; i++) {
2699    kmname = owl_list_get_element(&l, i);
2700    km = owl_keyhandler_get_keymap(kh, kmname);
2701    owl_fmtext_append_bold(&fm, "\n\n----------------------------------------------------------------------------------------------------\n\n");
2702    owl_keymap_get_details(km, &fm);   
2703  }
2704  owl_fmtext_append_normal(&fm, "\n");
2705 
2706  owl_function_popless_fmtext(&fm);
2707  owl_keyhandler_keymap_namelist_free(&l);
2708  owl_fmtext_free(&fm);
2709}
2710
2711char *owl_function_keymap_summary(void *name)
2712{
2713  owl_keymap *km
2714    = owl_keyhandler_get_keymap(owl_global_get_keyhandler(&g), name);
2715  if (km) return owl_keymap_summary(km);
2716  else return(NULL);
2717}
2718
2719/* TODO: implement for real */
2720void owl_function_show_keymap(char *name)
2721{
2722  owl_fmtext fm;
2723  owl_keymap *km;
2724
2725  owl_fmtext_init_null(&fm);
2726  km = owl_keyhandler_get_keymap(owl_global_get_keyhandler(&g), name);
2727  if (km) {
2728    owl_keymap_get_details(km, &fm);
2729  } else {
2730    owl_fmtext_append_normal(&fm, "No such keymap...\n");
2731  } 
2732  owl_function_popless_fmtext(&fm);
2733  owl_fmtext_free(&fm);
2734}
2735
2736void owl_function_help_for_command(char *cmdname)
2737{
2738  owl_fmtext fm;
2739
2740  owl_fmtext_init_null(&fm);
2741  owl_cmd_get_help(owl_global_get_cmddict(&g), cmdname, &fm);
2742  owl_function_popless_fmtext(&fm); 
2743  owl_fmtext_free(&fm);
2744}
2745
2746void owl_function_search_start(char *string, int direction)
2747{
2748  /* direction is OWL_DIRECTION_DOWNWARDS or OWL_DIRECTION_UPWARDS */
2749  owl_global_set_search_active(&g, string);
2750  owl_function_search_helper(0, direction);
2751}
2752
2753void owl_function_search_continue(int direction)
2754{
2755  /* direction is OWL_DIRECTION_DOWNWARDS or OWL_DIRECTION_UPWARDS */
2756  owl_function_search_helper(1, direction);
2757}
2758
2759void owl_function_search_helper(int mode, int direction)
2760{
2761  /* move to a message that contains the string.  If direction is
2762   * OWL_DIRECTION_DOWNWARDS then search fowards, if direction is
2763   * OWL_DIRECTION_UPWARDS then search backwards.
2764   *
2765   * If mode==0 then it will stay on the current message if it
2766   * contains the string.
2767   */
2768
2769  owl_view *v;
2770  int viewsize, i, curmsg, start;
2771  owl_message *m;
2772
2773  v=owl_global_get_current_view(&g);
2774  viewsize=owl_view_get_size(v);
2775  curmsg=owl_global_get_curmsg(&g);
2776 
2777  if (viewsize==0) {
2778    owl_function_makemsg("No messages present");
2779    return;
2780  }
2781
2782  if (mode==0) {
2783    start=curmsg;
2784  } else if (direction==OWL_DIRECTION_DOWNWARDS) {
2785    start=curmsg+1;
2786  } else {
2787    start=curmsg-1;
2788  }
2789
2790  /* bounds check */
2791  if (start>=viewsize || start<0) {
2792    owl_function_makemsg("No further matches found");
2793    return;
2794  }
2795
2796  for (i=start; i<viewsize && i>=0;) {
2797    m=owl_view_get_element(v, i);
2798    if (owl_message_search(m, owl_global_get_search_string(&g))) {
2799      owl_global_set_curmsg(&g, i);
2800      owl_function_calculate_topmsg(direction);
2801      owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2802      if (direction==OWL_DIRECTION_DOWNWARDS) {
2803        owl_global_set_direction_downwards(&g);
2804      } else {
2805        owl_global_set_direction_upwards(&g);
2806      }
2807      return;
2808    }
2809    if (direction==OWL_DIRECTION_DOWNWARDS) {
2810      i++;
2811    } else {
2812      i--;
2813    }
2814  }
2815  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2816  owl_function_makemsg("No matches found");
2817}
2818
2819
2820/* strips formatting from ztext and returns the unformatted text.
2821 * caller is responsible for freeing. */
2822char *owl_function_ztext_stylestrip(char *zt)
2823{
2824  owl_fmtext fm;
2825  char *plaintext;
2826
2827  owl_fmtext_init_null(&fm);
2828  owl_fmtext_append_ztext(&fm, zt);
2829  plaintext = owl_fmtext_print_plain(&fm);
2830  owl_fmtext_free(&fm);
2831  return(plaintext);
2832}
2833
2834/* Popup a buddylisting.  If file is NULL use the default .anyone */
2835void owl_function_buddylist(int aim, int zephyr, char *file)
2836{
2837  char *ourfile, *tmp, buff[LINE], *line;
2838  FILE *f;
2839  int numlocs, ret, i, j;
2840  ZLocations_t location[200];
2841  owl_fmtext fm;
2842  owl_buddylist *b;
2843
2844  owl_fmtext_init_null(&fm);
2845
2846  if (aim && owl_global_is_aimloggedin(&g)) {
2847    b=owl_global_get_buddylist(&g);
2848
2849    owl_fmtext_append_bold(&fm, "AIM users logged in:\n");
2850    j=owl_buddylist_get_size(b);
2851    for (i=0; i<j; i++) {
2852      owl_fmtext_append_normal(&fm, "  ");
2853      owl_fmtext_append_normal(&fm, owl_buddylist_get_buddy(b, i));
2854      owl_fmtext_append_normal(&fm, "\n");
2855    }
2856  }
2857
2858  if (zephyr) {
2859    if (file==NULL) {
2860      tmp=owl_global_get_homedir(&g);
2861      if (!tmp) {
2862        owl_function_makemsg("Could not determine home directory");
2863        return;
2864      }
2865      ourfile=owl_malloc(strlen(tmp)+50);
2866      sprintf(ourfile, "%s/.anyone", owl_global_get_homedir(&g));
2867    } else {
2868      ourfile=owl_strdup(file);
2869    }
2870   
2871    f=fopen(ourfile, "r");
2872    if (!f) {
2873      owl_function_makemsg("Error opening file %s: %s",
2874                           ourfile,
2875                           strerror(errno) ? strerror(errno) : "");
2876      return;
2877    }
2878   
2879    owl_fmtext_append_bold(&fm, "Zephyr users logged in:\n");
2880   
2881    while (fgets(buff, LINE, f)!=NULL) {
2882      /* ignore comments, blank lines etc. */
2883      if (buff[0]=='#') continue;
2884      if (buff[0]=='\n') continue;
2885      if (buff[0]=='\0') continue;
2886     
2887      /* strip the \n */
2888      buff[strlen(buff)-1]='\0';
2889     
2890      /* ingore from # on */
2891      tmp=strchr(buff, '#');
2892      if (tmp) tmp[0]='\0';
2893     
2894      /* ingore from SPC */
2895      tmp=strchr(buff, ' ');
2896      if (tmp) tmp[0]='\0';
2897     
2898      /* stick on the local realm. */
2899      if (!strchr(buff, '@')) {
2900        strcat(buff, "@");
2901        strcat(buff, ZGetRealm());
2902      }
2903     
2904      ret=ZLocateUser(buff, &numlocs, ZAUTH);
2905      if (ret!=ZERR_NONE) {
2906        owl_function_makemsg("Error getting location for %s", buff);
2907        continue;
2908      }
2909     
2910      numlocs=200;
2911      ret=ZGetLocations(location, &numlocs);
2912      if (ret==0) {
2913        for (i=0; i<numlocs; i++) {
2914          line=malloc(strlen(location[i].host)+strlen(location[i].time)+strlen(location[i].tty)+100);
2915          tmp=short_zuser(buff);
2916          sprintf(line, "  %-10.10s %-24.24s %-12.12s  %20.20s\n",
2917                  tmp,
2918                  location[i].host,
2919                  location[i].tty,
2920                  location[i].time);
2921          owl_fmtext_append_normal(&fm, line);
2922          owl_free(tmp);
2923        }
2924        if (numlocs>=200) {
2925          owl_fmtext_append_normal(&fm, "  Too many locations found for this user, truncating.\n");
2926        }
2927      }
2928    }
2929    fclose(f);
2930    owl_free(ourfile);
2931  }
2932 
2933  owl_function_popless_fmtext(&fm);
2934  owl_fmtext_free(&fm);
2935}
2936
2937void owl_function_dump(char *filename) 
2938{
2939  int i, j, count;
2940  owl_message *m;
2941  owl_view *v;
2942  FILE *file;
2943  /* struct stat sbuf; */
2944
2945  v=owl_global_get_current_view(&g);
2946
2947  /* in the future make it ask yes/no */
2948  /*
2949  ret=stat(filename, &sbuf);
2950  if (!ret) {
2951    ret=owl_function_askyesno("File exists, continue? [Y/n]");
2952    if (!ret) return;
2953  }
2954  */
2955
2956  file=fopen(filename, "w");
2957  if (!file) {
2958    owl_function_makemsg("Error opening file");
2959    return;
2960  }
2961
2962  count=0;
2963  j=owl_view_get_size(v);
2964  for (i=0; i<j; i++) {
2965    m=owl_view_get_element(v, i);
2966    fputs(owl_message_get_text(m), file);
2967  }
2968  fclose(file);
2969}
2970
2971
2972
2973void owl_function_do_newmsgproc(void)
2974{
2975  if (owl_global_get_newmsgproc(&g) && strcmp(owl_global_get_newmsgproc(&g), "")) {
2976    /* if there's a process out there, we need to check on it */
2977    if (owl_global_get_newmsgproc_pid(&g)) {
2978      owl_function_debugmsg("Checking on newmsgproc pid==%i", owl_global_get_newmsgproc_pid(&g));
2979      owl_function_debugmsg("Waitpid return is %i", waitpid(owl_global_get_newmsgproc_pid(&g), NULL, WNOHANG));
2980      waitpid(owl_global_get_newmsgproc_pid(&g), NULL, WNOHANG);
2981      if (waitpid(owl_global_get_newmsgproc_pid(&g), NULL, WNOHANG)==-1) {
2982        /* it exited */
2983        owl_global_set_newmsgproc_pid(&g, 0);
2984        owl_function_debugmsg("newmsgproc exited");
2985      } else {
2986        owl_function_debugmsg("newmsgproc did not exit");
2987      }
2988    }
2989   
2990    /* if it exited, fork & exec a new one */
2991    if (owl_global_get_newmsgproc_pid(&g)==0) {
2992      int i, myargc;
2993      i=fork();
2994      if (i) {
2995        /* parent set the child's pid */
2996        owl_global_set_newmsgproc_pid(&g, i);
2997        owl_function_debugmsg("I'm the parent and I started a new newmsgproc with pid %i", i);
2998      } else {
2999        /* child exec's the program */
3000        char **parsed;
3001        parsed=owl_parseline(owl_global_get_newmsgproc(&g), &myargc);
3002        if (myargc < 0) {
3003          owl_function_debugmsg("Could not parse newmsgproc '%s': unbalanced quotes?", owl_global_get_newmsgproc(&g));
3004        }
3005        if (myargc <= 0) {
3006          _exit(127);
3007        }
3008        parsed=realloc(parsed, sizeof(*parsed) * (myargc+1));
3009        parsed[myargc] = NULL;
3010       
3011        owl_function_debugmsg("About to exec \"%s\" with %d arguments", parsed[0], myargc);
3012       
3013        execvp(parsed[0], parsed);
3014       
3015       
3016        /* was there an error exec'ing? */
3017        owl_function_debugmsg("Cannot run newmsgproc '%s': cannot exec '%s': %s", 
3018                              owl_global_get_newmsgproc(&g), parsed[0], strerror(errno));
3019        _exit(127);
3020      }
3021    }
3022  }
3023}
3024
3025/* print the xterm escape sequence to raise the window */
3026void owl_function_xterm_raise(void)
3027{
3028  printf("\033[5t");
3029}
3030
3031/* print the xterm escape sequence to deiconify the window */
3032void owl_function_xterm_deiconify(void)
3033{
3034  printf("\033[1t");
3035}
3036
3037/* Add the specified command to the startup file.  Eventually this
3038 * should be clever, and rewriting settings that will obviosly
3039 * override earlier settings with 'set' 'bindkey' and 'alias'
3040 * commands.  For now though we just remove any line that would
3041 * duplicate this one and then append this line to the end of
3042 * startupfile.
3043 */
3044void owl_function_addstartup(char *buff)
3045{
3046  FILE *file;
3047  char *filename;
3048
3049  filename=owl_sprintf("%s/%s", owl_global_get_homedir(&g), OWL_STARTUP_FILE);
3050  file=fopen(filename, "a");
3051  if (!file) {
3052    owl_function_makemsg("Error opening startupfile for new command");
3053    owl_free(filename);
3054    return;
3055  }
3056
3057  /* delete earlier copies */
3058  owl_util_file_deleteline(filename, buff, 1);
3059  owl_free(filename);
3060
3061  /* add this line */
3062  fprintf(file, "%s\n", buff);
3063
3064  fclose(file);
3065}
3066
3067/* Remove the specified command from the startup file. */
3068void owl_function_delstartup(char *buff)
3069{
3070  char *filename;
3071  filename=owl_sprintf("%s/%s", owl_global_get_homedir(&g), OWL_STARTUP_FILE);
3072  owl_util_file_deleteline(filename, buff, 1);
3073  owl_free(filename);
3074}
3075
3076void owl_function_execstartup(void)
3077{
3078  FILE *file;
3079  char *filename;
3080  char buff[LINE];
3081
3082  filename=owl_sprintf("%s/%s", owl_global_get_homedir(&g), OWL_STARTUP_FILE);
3083  file=fopen(filename, "r");
3084  owl_free(filename);
3085  if (!file) {
3086    /* just fail silently if it doesn't exist */
3087    return;
3088  }
3089  while (fgets(buff, LINE, file)!=NULL) {
3090    buff[strlen(buff)-1]='\0';
3091    owl_function_command(buff);
3092  }
3093  fclose(file);
3094}
3095
3096
3097void owl_function_change_style(owl_view *v, char *stylename)
3098{
3099  owl_style *s;
3100
3101  s=owl_global_get_style_by_name(&g, stylename);
3102  if (!s) {
3103    owl_function_makemsg("No style named %s", stylename);
3104    return;
3105  }
3106  owl_view_set_style(v, s);
3107  owl_messagelist_invalidate_formats(owl_global_get_msglist(&g));
3108  owl_function_calculate_topmsg(OWL_DIRECTION_DOWNWARDS);
3109  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
3110 
3111}
3112
3113void owl_function_toggleoneline()
3114{
3115  owl_view *v;
3116  owl_style *s;
3117
3118  v=owl_global_get_current_view(&g);
3119  s=owl_view_get_style(v);
3120
3121  if (!owl_style_matches_name(s, "oneline")) {
3122    owl_function_change_style(v, "oneline");
3123  } else {
3124    owl_function_change_style(v, owl_global_get_default_style(&g));
3125  }
3126
3127  owl_messagelist_invalidate_formats(owl_global_get_msglist(&g));
3128  owl_function_calculate_topmsg(OWL_DIRECTION_DOWNWARDS);
3129  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
3130}
Note: See TracBrowser for help on using the repository browser.