source: functions.c @ 5bb6c21

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