source: functions.c @ 5639bf2

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