source: functions.c @ 7c9c847

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