source: functions.c @ e7cc1c3

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