source: functions.c @ 09489b89

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