source: functions.c @ 453bd70

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