source: functions.c @ c3ab155

barnowl_perlaimdebianowlrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since c3ab155 was c3ab155, checked in by James M. Kretchmar <kretch@mit.edu>, 21 years ago
Really use just one view now, named 'main' and recalculate messages when its filter is changed. This is in preperation for other design changes Added the 'default_style' variable Added the 'toggle-oneline' command the 'o' key is bound to 'toggle-oneline'
  • Property mode set to 100644
File size: 78.4 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
1377void owl_function_curmsg_to_popwin()
1378{
1379  owl_popwin *pw;
1380  owl_view *v;
1381  owl_message *m;
1382
1383  v = owl_global_get_current_view(&g);
1384
1385  pw=owl_global_get_popwin(&g);
1386
1387  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
1388
1389  if (!m || owl_view_get_size(v)==0) {
1390    owl_function_makemsg("No current message");
1391    return;
1392  }
1393
1394  owl_function_popless_fmtext(owl_message_get_fmtext(m));
1395}
1396
1397
1398void owl_function_page_curmsg(int step)
1399{
1400  /* scroll down or up within the current message IF the message is truncated */
1401
1402  int offset, curmsg, lines;
1403  owl_view *v;
1404  owl_message *m;
1405
1406  offset=owl_global_get_curmsg_vert_offset(&g);
1407  v=owl_global_get_current_view(&g);
1408  curmsg=owl_global_get_curmsg(&g);
1409  m=owl_view_get_element(v, curmsg);
1410  if (!m || owl_view_get_size(v)==0) return;
1411  lines=owl_message_get_numlines(m);
1412
1413  if (offset==0) {
1414    /* Bail if the curmsg isn't the last one displayed */
1415    if (curmsg != owl_mainwin_get_last_msg(owl_global_get_mainwin(&g))) {
1416      owl_function_makemsg("The entire message is already displayed");
1417      return;
1418    }
1419   
1420    /* Bail if we're not truncated */
1421    if (!owl_mainwin_is_curmsg_truncated(owl_global_get_mainwin(&g))) {
1422      owl_function_makemsg("The entire message is already displayed");
1423      return;
1424    }
1425  }
1426 
1427 
1428  /* don't scroll past the last line */
1429  if (step>0) {
1430    if (offset+step > lines-1) {
1431      owl_global_set_curmsg_vert_offset(&g, lines-1);
1432    } else {
1433      owl_global_set_curmsg_vert_offset(&g, offset+step);
1434    }
1435  }
1436
1437  /* would we be before the beginning of the message? */
1438  if (step<0) {
1439    if (offset+step<0) {
1440      owl_global_set_curmsg_vert_offset(&g, 0);
1441    } else {
1442      owl_global_set_curmsg_vert_offset(&g, offset+step);
1443    }
1444  }
1445 
1446  /* redisplay */
1447  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
1448  owl_global_set_needrefresh(&g);
1449}
1450
1451void owl_function_resize_typwin(int newsize)
1452{
1453  owl_global_set_typwin_lines(&g, newsize);
1454  owl_function_resize();
1455}
1456
1457void owl_function_typwin_grow()
1458{
1459  int i;
1460
1461  i=owl_global_get_typwin_lines(&g);
1462  owl_function_resize_typwin(i+1);
1463}
1464
1465void owl_function_typwin_shrink()
1466{
1467  int i;
1468
1469  i=owl_global_get_typwin_lines(&g);
1470  if (i>2) {
1471    owl_function_resize_typwin(i-1);
1472  }
1473}
1474
1475void owl_function_mainwin_pagedown()
1476{
1477  int i;
1478
1479  i=owl_mainwin_get_last_msg(owl_global_get_mainwin(&g));
1480  if (i<0) return;
1481  if (owl_mainwin_is_last_msg_truncated(owl_global_get_mainwin(&g))
1482      && (owl_global_get_curmsg(&g) < i)
1483      && (i>0)) {
1484    i--;
1485  }
1486  owl_global_set_curmsg(&g, i);
1487  owl_function_nextmsg();
1488}
1489
1490void owl_function_mainwin_pageup()
1491{
1492  owl_global_set_curmsg(&g, owl_global_get_topmsg(&g));
1493  owl_function_prevmsg();
1494}
1495
1496void owl_function_getsubs()
1497{
1498  int ret, num, i, one;
1499  ZSubscription_t sub;
1500  char *buff, *tmpbuff;
1501
1502  one = 1;
1503
1504  ret=ZRetrieveSubscriptions(0, &num);
1505  if (ret == ZERR_TOOMANYSUBS) {
1506    owl_function_makemsg("Zephyr: too many subscriptions");
1507    return;
1508  }
1509
1510  buff=owl_malloc(num*500);
1511  tmpbuff=owl_malloc(num*500);
1512  strcpy(buff, "");
1513  for (i=0; i<num; i++) {
1514    if ((ret = ZGetSubscriptions(&sub, &one)) != ZERR_NONE) {
1515      owl_function_makemsg("Error while getting subscriptions");
1516      owl_free(buff);
1517      owl_free(tmpbuff);
1518      ZFlushSubscriptions();
1519      return;
1520    } else {
1521      sprintf(tmpbuff, "<%s,%s,%s>\n%s", sub.zsub_class, sub.zsub_classinst, sub.zsub_recipient, buff);
1522      strcpy(buff, tmpbuff);
1523    }
1524  }
1525
1526  owl_function_popless_text(buff);
1527  owl_free(buff);
1528  owl_free(tmpbuff);
1529  ZFlushSubscriptions();
1530}
1531
1532#define PABUFLEN 5000
1533void owl_function_printallvars()
1534{
1535  char buff[PABUFLEN], *pos, *name;
1536  owl_list varnames;
1537  int i, numvarnames, rem;
1538
1539  pos = buff;
1540  pos += sprintf(pos, "%-20s = %s\n", "VARIABLE", "VALUE");
1541  pos += sprintf(pos, "%-20s   %s\n",  "--------", "-----");
1542  owl_variable_dict_get_names(owl_global_get_vardict(&g), &varnames);
1543  rem = (buff+PABUFLEN)-pos-1;
1544  numvarnames = owl_list_get_size(&varnames);
1545  for (i=0; i<numvarnames; i++) {
1546    name = owl_list_get_element(&varnames, i);
1547    if (name && name[0]!='_') {
1548      rem = (buff+PABUFLEN)-pos-1;   
1549      pos += snprintf(pos, rem, "\n%-20s = ", name);
1550      rem = (buff+PABUFLEN)-pos-1;   
1551      owl_variable_get_tostring(owl_global_get_vardict(&g), name, pos, rem);
1552      pos = buff+strlen(buff);
1553    }
1554  }
1555  rem = (buff+PABUFLEN)-pos-1;   
1556  snprintf(pos, rem, "\n");
1557  owl_variable_dict_namelist_free(&varnames);
1558 
1559  owl_function_popless_text(buff);
1560}
1561
1562void owl_function_show_variables()
1563{
1564  owl_list varnames;
1565  owl_fmtext fm; 
1566  int i, numvarnames;
1567  char *varname;
1568
1569  owl_fmtext_init_null(&fm);
1570  owl_fmtext_append_bold(&fm, 
1571      "Variables: (use 'show variable <name>' for details)\n");
1572  owl_variable_dict_get_names(owl_global_get_vardict(&g), &varnames);
1573  numvarnames = owl_list_get_size(&varnames);
1574  for (i=0; i<numvarnames; i++) {
1575    varname = owl_list_get_element(&varnames, i);
1576    if (varname && varname[0]!='_') {
1577      owl_variable_describe(owl_global_get_vardict(&g), varname, &fm);
1578    }
1579  }
1580  owl_variable_dict_namelist_free(&varnames);
1581  owl_function_popless_fmtext(&fm);
1582  owl_fmtext_free(&fm);
1583}
1584
1585void owl_function_show_variable(char *name)
1586{
1587  owl_fmtext fm; 
1588
1589  owl_fmtext_init_null(&fm);
1590  owl_variable_get_help(owl_global_get_vardict(&g), name, &fm);
1591  owl_function_popless_fmtext(&fm);
1592  owl_fmtext_free(&fm); 
1593}
1594
1595/* note: this applies to global message list, not to view.
1596 * If flag is 1, deletes.  If flag is 0, undeletes. */
1597void owl_function_delete_by_id(int id, int flag)
1598{
1599  owl_messagelist *ml;
1600  owl_message *m;
1601  ml = owl_global_get_msglist(&g);
1602  m = owl_messagelist_get_by_id(ml, id);
1603  if (m) {
1604    if (flag == 1) {
1605      owl_message_mark_delete(m);
1606    } else if (flag == 0) {
1607      owl_message_unmark_delete(m);
1608    }
1609    owl_mainwin_redisplay(owl_global_get_mainwin(&g));
1610    owl_global_set_needrefresh(&g);
1611  } else {
1612    owl_function_makemsg("No message with id %d: unable to mark for (un)delete",id);
1613  }
1614}
1615
1616void owl_function_delete_automsgs()
1617{
1618  /* mark for deletion all messages in the current view that match the
1619   * 'trash' filter */
1620
1621  int i, j, count;
1622  owl_message *m;
1623  owl_view *v;
1624  owl_filter *f;
1625
1626  /* get the trash filter */
1627  f=owl_global_get_filter(&g, "trash");
1628  if (!f) {
1629    owl_function_makemsg("No trash filter defined");
1630    return;
1631  }
1632
1633  v=owl_global_get_current_view(&g);
1634
1635  count=0;
1636  j=owl_view_get_size(v);
1637  for (i=0; i<j; i++) {
1638    m=owl_view_get_element(v, i);
1639    if (owl_filter_message_match(f, m)) {
1640      count++;
1641      owl_message_mark_delete(m);
1642    }
1643  }
1644  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
1645  owl_function_makemsg("%i messages marked for deletion", count);
1646  owl_global_set_needrefresh(&g);
1647}
1648
1649
1650void owl_function_status()
1651{
1652  char buff[5000];
1653  time_t start;
1654  int up, days, hours, minutes;
1655
1656  start=owl_global_get_starttime(&g);
1657
1658  sprintf(buff, "Version: %s\n", OWL_VERSION_STRING);
1659  sprintf(buff, "%sScreen size: %i lines, %i columns\n", buff, owl_global_get_lines(&g), owl_global_get_cols(&g));
1660  sprintf(buff, "%sStartup Arugments: %s\n", buff, owl_global_get_startupargs(&g));
1661  sprintf(buff, "%sStartup Time: %s", buff, ctime(&start));
1662
1663  up=owl_global_get_runtime(&g);
1664  days=up/86400;
1665  up-=days*86400;
1666  hours=up/3600;
1667  up-=hours*3600;
1668  minutes=up/60;
1669  up-=minutes*60;
1670  sprintf(buff, "%sRun Time: %i days %2.2i:%2.2i:%2.2i\n", buff, days, hours, minutes, up);
1671
1672  if (owl_global_get_hascolors(&g)) {
1673    sprintf(buff, "%sColor: Yes, %i color pairs.\n", buff, owl_global_get_colorpairs(&g));
1674  } else {
1675    strcat(buff, "Color: No.\n");
1676  }
1677
1678  sprintf(buff, "%sMemory Malloced: %i\n", buff, owl_global_get_malloced(&g));
1679  sprintf(buff, "%sMemory Freed: %i\n", buff, owl_global_get_freed(&g));
1680  sprintf(buff, "%sMemory In Use: %i\n", buff, owl_global_get_meminuse(&g));
1681
1682  owl_function_popless_text(buff);
1683}
1684
1685void owl_function_show_term()
1686{
1687  owl_fmtext fm;
1688  char buff[LINE];
1689
1690  owl_fmtext_init_null(&fm);
1691  sprintf(buff, "Terminal Lines: %i\nTerminal Columns: %i\n",
1692          owl_global_get_lines(&g),
1693          owl_global_get_cols(&g));
1694  owl_fmtext_append_normal(&fm, buff);
1695
1696  if (owl_global_get_hascolors(&g)) {
1697    owl_fmtext_append_normal(&fm, "Color: Yes\n");
1698    sprintf(buff, "Number of color pairs: %i\n", owl_global_get_colorpairs(&g));
1699    owl_fmtext_append_normal(&fm, buff);
1700    sprintf(buff, "Can change colors: %s\n", can_change_color() ? "yes" : "no");
1701    owl_fmtext_append_normal(&fm, buff);
1702  } else {
1703    owl_fmtext_append_normal(&fm, "Color: No\n");
1704  }
1705
1706  owl_function_popless_fmtext(&fm);
1707  owl_fmtext_free(&fm);
1708}
1709
1710
1711void owl_function_reply(int type, int enter)
1712{
1713  /* if type = 0 then normal reply.
1714   * if type = 1 then it's a reply to sender
1715   * if enter = 0 then allow the command to be edited
1716   * if enter = 1 then don't wait for editing
1717   */
1718  char *buff, *oldbuff;
1719  owl_message *m;
1720  owl_filter *f;
1721 
1722  if (owl_view_get_size(owl_global_get_current_view(&g))==0) {
1723    owl_function_makemsg("No message selected");
1724  } else {
1725    char *class, *inst, *to, *cc=NULL;
1726   
1727    m=owl_view_get_element(owl_global_get_current_view(&g), owl_global_get_curmsg(&g));
1728    if (!m) {
1729      owl_function_makemsg("No message selected");
1730      return;
1731    }
1732
1733    /* first check if we catch the reply-lockout filter */
1734    f=owl_global_get_filter(&g, "reply-lockout");
1735    if (f) {
1736      if (owl_filter_message_match(f, m)) {
1737        owl_function_makemsg("Sorry, replies to this message have been disabled by the reply-lockout filter");
1738        return;
1739      }
1740    }
1741
1742    /* admin */
1743    if (owl_message_is_type_admin(m)) {
1744      owl_function_makemsg("You cannot reply to an admin message");
1745      return;
1746    }
1747
1748    /* zephyr */
1749    if (owl_message_is_type_zephyr(m)) {
1750      /* for now we disable replies to zcrypt messages, since we can't
1751         support an encrypted reply */
1752      if (!strcasecmp(owl_message_get_opcode(m), "crypt")) {
1753        owl_function_makemsg("Replies to zcrypt messages are not enabled in this release");
1754        return;
1755      }
1756
1757      if (owl_message_is_direction_out(m)) {
1758        owl_function_zwrite_setup(owl_message_get_zwriteline(m));
1759        owl_global_set_buffercommand(&g, owl_message_get_zwriteline(m));
1760        return;
1761      }
1762     
1763      if (owl_message_is_loginout(m)) {
1764        class="MESSAGE";
1765        inst="PERSONAL";
1766        to=owl_message_get_sender(m);
1767      } else if (type==1) {
1768        class="MESSAGE";
1769        inst="PERSONAL";
1770        to=owl_message_get_sender(m);
1771      } else {
1772        class=owl_message_get_class(m);
1773        inst=owl_message_get_instance(m);
1774        to=owl_message_get_recipient(m);
1775        cc=owl_message_get_cc(m);
1776        if (!strcmp(to, "") || !strcmp(to, "*")) {
1777          to="";
1778        } else if (to[0]=='@') {
1779          /* leave it, to get the realm */
1780        } else {
1781          to=owl_message_get_sender(m);
1782        }
1783      }
1784       
1785      /* create the command line */
1786      buff = owl_strdup("zwrite");
1787      if (strcasecmp(class, "message")) {
1788        buff = owl_sprintf("%s -c %s%s%s", oldbuff=buff, owl_getquoting(class), class, owl_getquoting(class));
1789        owl_free(oldbuff);
1790      }
1791      if (strcasecmp(inst, "personal")) {
1792        buff = owl_sprintf("%s -i %s%s%s", oldbuff=buff, owl_getquoting(inst), inst, owl_getquoting(inst));
1793        owl_free(oldbuff);
1794      }
1795      if (*to != '\0') {
1796        char *tmp, *oldtmp, *tmp2;
1797        tmp=short_zuser(to);
1798        if (cc) {
1799          tmp = owl_util_uniq(oldtmp=tmp, cc, "-");
1800          owl_free(oldtmp);
1801          buff = owl_sprintf("%s -C %s", oldbuff=buff, tmp);
1802          owl_free(oldbuff);
1803        } else {
1804          if (owl_global_is_smartstrip(&g)) {
1805            tmp2=tmp;
1806            tmp=owl_util_smartstripped_user(tmp2);
1807            owl_free(tmp2);
1808          }
1809          buff = owl_sprintf("%s %s", oldbuff=buff, tmp);
1810          owl_free(oldbuff);
1811        }
1812        owl_free(tmp);
1813      }
1814      if (cc) owl_free(cc);
1815    }
1816
1817    /* aim */
1818    if (owl_message_is_type_aim(m)) {
1819      if (owl_message_is_direction_out(m)) {
1820        buff=owl_sprintf("aimwrite %s", owl_message_get_recipient(m));
1821      } else {
1822        buff=owl_sprintf("aimwrite %s", owl_message_get_sender(m));
1823      }
1824    }
1825   
1826    if (enter) {
1827      owl_history *hist = owl_global_get_cmd_history(&g);
1828      owl_history_store(hist, buff);
1829      owl_history_reset(hist);
1830      owl_function_command_norv(buff);
1831    } else {
1832      owl_function_start_command(buff);
1833    }
1834    owl_free(buff);
1835  }
1836}
1837
1838void owl_function_zlocate(int argc, char **argv, int auth)
1839{
1840  owl_fmtext fm;
1841  char *ptr, buff[LINE];
1842  int i;
1843
1844  owl_fmtext_init_null(&fm);
1845
1846  for (i=0; i<argc; i++) {
1847    ptr=long_zuser(argv[i]);
1848    owl_zephyr_zlocate(ptr, buff, auth);
1849    owl_fmtext_append_normal(&fm, buff);
1850    owl_free(ptr);
1851  }
1852
1853  owl_function_popless_fmtext(&fm);
1854  owl_fmtext_free(&fm);
1855}
1856
1857void owl_function_start_command(char *line)
1858{
1859  int i, j;
1860  owl_editwin *tw;
1861
1862  tw=owl_global_get_typwin(&g);
1863  owl_global_set_typwin_active(&g);
1864  owl_editwin_new_style(tw, OWL_EDITWIN_STYLE_ONELINE, 
1865                        owl_global_get_cmd_history(&g));
1866
1867  owl_editwin_set_locktext(tw, "command: ");
1868  owl_global_set_needrefresh(&g);
1869
1870  j=strlen(line);
1871  for (i=0; i<j; i++) {
1872    owl_editwin_process_char(tw, line[i]);
1873  }
1874  owl_editwin_redisplay(tw, 0);
1875}
1876
1877char *owl_function_exec(int argc, char **argv, char *buff, int type)
1878{
1879  /* if type == 1 display in a popup
1880   * if type == 2 display an admin messages
1881   * if type == 0 return output
1882   * else display in a popup
1883   */
1884  char *newbuff, *redirect = " 2>&1 < /dev/null";
1885  char *out, buff2[1024];
1886  int size;
1887  FILE *p;
1888
1889  if (argc<2) {
1890    owl_function_makemsg("Wrong number of arguments to the exec command");
1891    return NULL;
1892  }
1893
1894  buff = skiptokens(buff, 1);
1895  newbuff = owl_malloc(strlen(buff)+strlen(redirect)+1);
1896  strcpy(newbuff, buff);
1897  strcat(newbuff, redirect);
1898
1899  p=popen(newbuff, "r");
1900  out=owl_malloc(1024);
1901  size=1024;
1902  strcpy(out, "");
1903  while (fgets(buff2, 1024, p)!=NULL) {
1904    size+=1024;
1905    out=owl_realloc(out, size);
1906    strcat(out, buff2);
1907  }
1908  pclose(p);
1909
1910  if (type==1) {
1911    owl_function_popless_text(out);
1912  } else if (type==0) {
1913    return out;
1914  } else if (type==2) {
1915    owl_function_adminmsg(buff, out);
1916  } else {
1917    owl_function_popless_text(out);
1918  }
1919  owl_free(out);
1920  return NULL;
1921}
1922
1923
1924char *owl_function_perl(int argc, char **argv, char *buff, int type)
1925{
1926  /* if type == 1 display in a popup
1927   * if type == 2 display an admin messages
1928   * if type == 0 return output
1929   * else display in a popup
1930   */
1931  char *perlout;
1932
1933  if (argc<2) {
1934    owl_function_makemsg("Wrong number of arguments to perl command");
1935    return NULL;
1936  }
1937
1938  /* consume first token (argv[0]) */
1939  buff = skiptokens(buff, 1);
1940
1941  perlout = owl_config_execute(buff);
1942  if (perlout) { 
1943    if (type==1) {
1944      owl_function_popless_text(perlout);
1945    } else if (type==2) {
1946      owl_function_adminmsg(buff, perlout);
1947    } else if (type==0) {
1948      return perlout;
1949    } else {
1950      owl_function_popless_text(perlout);
1951    }
1952    owl_free(perlout);
1953  }
1954  return NULL;
1955}
1956
1957#if 0
1958void owl_function_change_view_old(char *filtname)
1959{
1960  owl_view *v;
1961  owl_filter *f;
1962  int curid=-1, newpos, curmsg;
1963  owl_message *curm=NULL;
1964
1965  v=owl_global_get_current_view(&g);
1966  curmsg=owl_global_get_curmsg(&g);
1967  if (curmsg==-1) {
1968    owl_function_debugmsg("Hit the curmsg==-1 case in change_view");
1969  } else {
1970    curm=owl_view_get_element(v, curmsg);
1971    if (curm) {
1972      curid=owl_message_get_id(curm);
1973      owl_view_save_curmsgid(v, curid);
1974    }
1975  }
1976
1977  /* grab the filter */;
1978  f=owl_global_get_filter(&g, filtname);
1979  if (!f) {
1980    owl_function_makemsg("Unknown filter");
1981    return;
1982  }
1983
1984  /* free the existing view and create a new one based on the filter */
1985  owl_view_free(v);
1986  owl_view_create(v, f);
1987
1988  /* Figure out what to set the current message to.
1989   * - If the view we're leaving has messages in it, go to the closest message
1990   *   to the last message pointed to in that view.
1991   * - If the view we're leaving is empty, try to restore the position
1992   *   from the last time we were in the new view.  */
1993  if (curm) {
1994    newpos = owl_view_get_nearest_to_msgid(v, curid);
1995  } else {
1996    newpos = owl_view_get_nearest_to_saved(v);
1997  }
1998
1999  owl_global_set_curmsg(&g, newpos);
2000
2001  owl_function_calculate_topmsg(OWL_DIRECTION_DOWNWARDS);
2002  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2003  owl_global_set_direction_downwards(&g);
2004}
2005#endif
2006
2007void owl_function_change_view(char *filtname)
2008{
2009  owl_view *v;
2010  owl_filter *f;
2011  int curid=-1, newpos, curmsg;
2012  owl_message *curm=NULL;
2013
2014  v=owl_global_get_current_view(&g);
2015
2016  curmsg=owl_global_get_curmsg(&g);
2017  if (curmsg==-1) {
2018    owl_function_debugmsg("Hit the curmsg==-1 case in change_view");
2019  } else {
2020    curm=owl_view_get_element(v, curmsg);
2021    if (curm) {
2022      curid=owl_message_get_id(curm);
2023      owl_view_save_curmsgid(v, curid);
2024    }
2025  }
2026
2027  f=owl_global_get_filter(&g, filtname);
2028  if (!f) {
2029    owl_function_makemsg("Unknown filter");
2030    return;
2031  }
2032
2033  owl_view_new_filter(v, f);
2034
2035  /* Figure out what to set the current message to.
2036   * - If the view we're leaving has messages in it, go to the closest message
2037   *   to the last message pointed to in that view.
2038   * - If the view we're leaving is empty, try to restore the position
2039   *   from the last time we were in the new view.  */
2040  if (curm) {
2041    newpos = owl_view_get_nearest_to_msgid(v, curid);
2042  } else {
2043    newpos = owl_view_get_nearest_to_saved(v);
2044  }
2045
2046  owl_global_set_curmsg(&g, newpos);
2047  owl_function_calculate_topmsg(OWL_DIRECTION_DOWNWARDS);
2048  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2049  owl_global_set_direction_downwards(&g);
2050}
2051
2052void owl_function_create_filter(int argc, char **argv)
2053{
2054  owl_filter *f;
2055  owl_view *v;
2056  int ret, inuse=0;
2057
2058  if (argc < 2) {
2059    owl_function_makemsg("Wrong number of arguments to filter command");
2060    return;
2061  }
2062
2063  v=owl_global_get_current_view(&g);
2064
2065  /* don't touch the all filter */
2066  if (!strcmp(argv[1], "all")) {
2067    owl_function_makemsg("You may not change the 'all' filter.");
2068    return;
2069  }
2070
2071  /* deal with the case of trying change the filter color */
2072  if (argc==4 && !strcmp(argv[2], "-c")) {
2073    f=owl_global_get_filter(&g, argv[1]);
2074    if (!f) {
2075      owl_function_makemsg("The filter '%s' does not exist.", argv[1]);
2076      return;
2077    }
2078    owl_filter_set_color(f, owl_util_string_to_color(argv[3]));
2079    owl_global_set_needrefresh(&g);
2080    owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2081    return;
2082  }
2083
2084  /* create the filter and check for errors */
2085  f=owl_malloc(sizeof(owl_filter));
2086  ret=owl_filter_init(f, argv[1], argc-2, argv+2);
2087  if (ret==-1) {
2088    owl_free(f);
2089    owl_function_makemsg("Invalid filter syntax");
2090    return;
2091  }
2092
2093  /* if the named filter is in use by the current view, remember it */
2094  if (!strcmp(owl_view_get_filtname(v), argv[1])) {
2095    inuse=1;
2096  }
2097
2098  /* if the named filter already exists, nuke it */
2099  if (owl_global_get_filter(&g, argv[1])) {
2100    owl_global_remove_filter(&g, argv[1]);
2101  }
2102
2103  /* add the filter */
2104  owl_global_add_filter(&g, f);
2105
2106  /* if it was in use by the current view then update */
2107  if (inuse) {
2108    owl_function_change_view(argv[1]);
2109  }
2110  owl_global_set_needrefresh(&g);
2111  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2112}
2113
2114void owl_function_show_filters()
2115{
2116  owl_list *l;
2117  owl_filter *f;
2118  int i, j;
2119  owl_fmtext fm;
2120
2121  owl_fmtext_init_null(&fm);
2122
2123  l=owl_global_get_filterlist(&g);
2124  j=owl_list_get_size(l);
2125
2126  owl_fmtext_append_bold(&fm, "Filters:\n");
2127
2128  for (i=0; i<j; i++) {
2129    f=owl_list_get_element(l, i);
2130    owl_fmtext_append_normal(&fm, "   ");
2131    if (owl_global_get_hascolors(&g)) {
2132      owl_fmtext_append_normal_color(&fm, owl_filter_get_name(f), owl_filter_get_color(f));
2133    } else {
2134      owl_fmtext_append_normal(&fm, owl_filter_get_name(f));
2135    }
2136    owl_fmtext_append_normal(&fm, "\n");
2137  }
2138  owl_function_popless_fmtext(&fm);
2139  owl_fmtext_free(&fm);
2140}
2141
2142void owl_function_show_filter(char *name)
2143{
2144  owl_filter *f;
2145  char buff[5000];
2146
2147  f=owl_global_get_filter(&g, name);
2148  if (!f) {
2149    owl_function_makemsg("There is no filter with that name");
2150    return;
2151  }
2152  owl_filter_print(f, buff);
2153  owl_function_popless_text(buff);
2154}
2155
2156void owl_function_show_zpunts()
2157{
2158  owl_filter *f;
2159  owl_list *fl;
2160  char buff[5000];
2161  owl_fmtext fm;
2162  int i, j;
2163
2164  owl_fmtext_init_null(&fm);
2165
2166  fl=owl_global_get_puntlist(&g);
2167  j=owl_list_get_size(fl);
2168  owl_fmtext_append_bold(&fm, "Active zpunt filters:\n");
2169
2170  for (i=0; i<j; i++) {
2171    f=owl_list_get_element(fl, i);
2172    owl_filter_print(f, buff);
2173    owl_fmtext_append_normal(&fm, buff);
2174  }
2175  owl_function_popless_fmtext(&fm);
2176  owl_fmtext_free(&fm);
2177}
2178
2179/* Create a filter for a class, instance if one doesn't exist.  If
2180 * instance is NULL then catch all messgaes in the class.  Returns the
2181 * name of the filter, which the caller must free.
2182 */
2183char *owl_function_classinstfilt(char *class, char *instance) 
2184{
2185  owl_list *fl;
2186  owl_filter *f;
2187  char *argbuff, *filtname;
2188  char *tmpclass, *tmpinstance = NULL;
2189  int len;
2190
2191  fl=owl_global_get_filterlist(&g);
2192
2193  /* name for the filter */
2194  len=strlen(class)+30;
2195  if (instance) len+=strlen(instance);
2196  filtname=owl_malloc(len);
2197  if (!instance) {
2198    sprintf(filtname, "class-%s", class);
2199  } else {
2200    sprintf(filtname, "class-%s-instance-%s", class, instance);
2201  }
2202  /* downcase it */
2203  downstr(filtname);
2204  /* turn spaces into hyphens */
2205  owl_util_tr(filtname, ' ', '.');
2206 
2207  /* if it already exists then go with it.  This lets users override */
2208  if (owl_global_get_filter(&g, filtname)) {
2209    return(filtname);
2210  }
2211
2212  /* create the new filter */
2213  argbuff=owl_malloc(len+20);
2214  tmpclass=owl_strdup(class);
2215  owl_util_tr(tmpclass, ' ', '.');
2216  if (instance) {
2217    tmpinstance=owl_strdup(instance);
2218    owl_util_tr(tmpinstance, ' ', '.');
2219  }
2220  sprintf(argbuff, "( class ^%s$ )", tmpclass);
2221  if (tmpinstance) {
2222    sprintf(argbuff, "%s and ( instance ^%s$ )", argbuff, tmpinstance);
2223  }
2224  owl_free(tmpclass);
2225  if (tmpinstance) owl_free(tmpinstance);
2226
2227  f=owl_malloc(sizeof(owl_filter));
2228  owl_filter_init_fromstring(f, filtname, argbuff);
2229
2230  /* add it to the global list */
2231  owl_global_add_filter(&g, f);
2232
2233  owl_free(argbuff);
2234  return(filtname);
2235}
2236
2237/* Create a filter for personal zephyrs to or from the specified
2238 * zephyr user.  Includes login/logout notifications for the user.
2239 * The name of the filter will be 'user-<user>'.  If a filter already
2240 * exists with this name, no new filter will be created.  This allows
2241 * the configuration to override this function.  Returns the name of
2242 * the filter, which the caller must free.
2243 */
2244char *owl_function_zuserfilt(char *user)
2245{
2246  owl_filter *f;
2247  char *argbuff, *longuser, *shortuser, *filtname;
2248
2249  /* stick the local realm on if it's not there */
2250  longuser=long_zuser(user);
2251  shortuser=short_zuser(user);
2252
2253  /* name for the filter */
2254  filtname=owl_malloc(strlen(shortuser)+20);
2255  sprintf(filtname, "user-%s", shortuser);
2256
2257  /* if it already exists then go with it.  This lets users override */
2258  if (owl_global_get_filter(&g, filtname)) {
2259    return(owl_strdup(filtname));
2260  }
2261
2262  /* create the new-internal filter */
2263  f=owl_malloc(sizeof(owl_filter));
2264
2265  argbuff=owl_malloc(strlen(longuser)+1000);
2266  sprintf(argbuff, "( type ^zephyr$ and ( class ^message$ and instance ^personal$ and ");
2267  sprintf(argbuff, "%s ( ( direction ^in$ and sender ^%s$ ) or ( direction ^out$ and recipient ^%s$ ) ) )", argbuff, longuser, longuser);
2268  sprintf(argbuff, "%s or ( ( class ^login$ ) and ( sender ^%s$ ) ) )", argbuff, longuser);
2269
2270  owl_filter_init_fromstring(f, filtname, argbuff);
2271
2272  /* add it to the global list */
2273  owl_global_add_filter(&g, f);
2274
2275  /* free stuff */
2276  owl_free(argbuff);
2277  owl_free(longuser);
2278  owl_free(shortuser);
2279
2280  return(filtname);
2281}
2282
2283/* Create a filter for AIM IM messages to or from the specified
2284 * screenname.  The name of the filter will be 'aimuser-<user>'.  If a
2285 * filter already exists with this name, no new filter will be
2286 * created.  This allows the configuration to override this function.
2287 * Returns the name of the filter, which the caller must free.
2288 */
2289char *owl_function_aimuserfilt(char *user)
2290{
2291  owl_filter *f;
2292  char *argbuff, *filtname;
2293
2294  /* name for the filter */
2295  filtname=owl_malloc(strlen(user)+40);
2296  sprintf(filtname, "aimuser-%s", user);
2297
2298  /* if it already exists then go with it.  This lets users override */
2299  if (owl_global_get_filter(&g, filtname)) {
2300    return(owl_strdup(filtname));
2301  }
2302
2303  /* create the new-internal filter */
2304  f=owl_malloc(sizeof(owl_filter));
2305
2306  argbuff=owl_malloc(1000);
2307  sprintf(argbuff,
2308          "( type ^aim$ and ( ( sender ^%s$ and recipient ^%s$ ) or ( sender ^%s$ and recipient ^%s$ ) ) )",
2309          user, owl_global_get_aim_screenname(&g), owl_global_get_aim_screenname(&g), user);
2310
2311  owl_filter_init_fromstring(f, filtname, argbuff);
2312
2313  /* add it to the global list */
2314  owl_global_add_filter(&g, f);
2315
2316  /* free stuff */
2317  owl_free(argbuff);
2318
2319  return(filtname);
2320}
2321
2322char *owl_function_typefilt(char *type)
2323{
2324  owl_filter *f;
2325  char *argbuff, *filtname;
2326
2327  /* name for the filter */
2328  filtname=owl_sprintf("type-%s", type);
2329
2330  /* if it already exists then go with it.  This lets users override */
2331  if (owl_global_get_filter(&g, filtname)) {
2332    return filtname;
2333  }
2334
2335  /* create the new-internal filter */
2336  f=owl_malloc(sizeof(owl_filter));
2337
2338  argbuff = owl_sprintf("type ^%s$", type);
2339
2340  owl_filter_init_fromstring(f, filtname, argbuff);
2341
2342  /* add it to the global list */
2343  owl_global_add_filter(&g, f);
2344
2345  /* free stuff */
2346  owl_free(argbuff);
2347
2348  return filtname;
2349}
2350
2351/* If flag is 1, marks for deletion.  If flag is 0,
2352 * unmarks for deletion. */
2353void owl_function_delete_curview_msgs(int flag)
2354{
2355  owl_view *v;
2356  int i, j;
2357
2358  v=owl_global_get_current_view(&g);
2359  j=owl_view_get_size(v);
2360  for (i=0; i<j; i++) {
2361    if (flag == 1) {
2362      owl_message_mark_delete(owl_view_get_element(v, i));
2363    } else if (flag == 0) {
2364      owl_message_unmark_delete(owl_view_get_element(v, i));
2365    }
2366  }
2367
2368  owl_function_makemsg("%i messages marked for %sdeletion", j, flag?"":"un");
2369
2370  owl_mainwin_redisplay(owl_global_get_mainwin(&g)); 
2371}
2372
2373/* Create a filter based on the current message.  Returns the name of
2374 * a filter or null.  The caller must free this name.
2375 *
2376 * if the curmsg is a personal zephyr return a filter name
2377 *    to the zephyr converstaion with that user.
2378 * If the curmsg is a zephyr class message, instance foo, recip *,
2379 *    return a filter name to the class, inst.
2380 * If the curmsg is a zephyr class message and type==0 then
2381 *    return a filter name for just the class.
2382 * If the curmsg is a zephyr class message and type==1 then
2383 *    return a filter name for the class and instance.
2384 * If the curmsg is a personal AIM message returna  filter
2385 *    name to the AIM conversation with that user
2386 */
2387char *owl_function_smartfilter(int type)
2388{
2389  owl_view *v;
2390  owl_message *m;
2391  char *zperson, *filtname=NULL;
2392 
2393  v=owl_global_get_current_view(&g);
2394  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
2395
2396  if (!m || owl_view_get_size(v)==0) {
2397    owl_function_makemsg("No message selected\n");
2398    return(NULL);
2399  }
2400
2401  /* very simple handling of admin messages for now */
2402  if (owl_message_is_type_admin(m)) {
2403    return(owl_function_typefilt("admin"));
2404  }
2405
2406  /* aim messages */
2407  if (owl_message_is_type_aim(m)) {
2408    if (owl_message_is_direction_in(m)) {
2409      filtname=owl_function_aimuserfilt(owl_message_get_sender(m));
2410    } else if (owl_message_is_direction_out(m)) {
2411      filtname=owl_function_aimuserfilt(owl_message_get_recipient(m));
2412    }
2413    return(filtname);
2414  }
2415
2416  /* narrow personal and login messages to the sender or recip as appropriate */
2417  if (owl_message_is_personal(m) || owl_message_is_loginout(m)) {
2418    if (owl_message_is_type_zephyr(m)) {
2419      if (owl_message_is_direction_in(m)) {
2420        zperson=short_zuser(owl_message_get_sender(m));
2421      } else {
2422        zperson=short_zuser(owl_message_get_recipient(m));
2423      }
2424      filtname=owl_function_zuserfilt(zperson);
2425      owl_free(zperson);
2426      return(filtname);
2427    }
2428    return(NULL);
2429  }
2430
2431  /* narrow class MESSAGE, instance foo, recip * messages to class, inst */
2432  if (!strcasecmp(owl_message_get_class(m), "message") && !owl_message_is_personal(m)) {
2433    filtname=owl_function_classinstfilt(owl_message_get_class(m), owl_message_get_instance(m));
2434    return(filtname);
2435  }
2436
2437  /* otherwise narrow to the class */
2438  if (type==0) {
2439    filtname=owl_function_classinstfilt(owl_message_get_class(m), NULL);
2440  } else if (type==1) {
2441    filtname=owl_function_classinstfilt(owl_message_get_class(m), owl_message_get_instance(m));
2442  }
2443  return(filtname);
2444}
2445
2446void owl_function_smartzpunt(int type)
2447{
2448  /* Starts a zpunt command based on the current class,instance pair.
2449   * If type=0, uses just class.  If type=1, uses instance as well. */
2450  owl_view *v;
2451  owl_message *m;
2452  char *cmd, *cmdprefix, *mclass, *minst;
2453 
2454  v=owl_global_get_current_view(&g);
2455  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
2456
2457  if (!m || owl_view_get_size(v)==0) {
2458    owl_function_makemsg("No message selected\n");
2459    return;
2460  }
2461
2462  /* for now we skip admin messages. */
2463  if (owl_message_is_type_admin(m)
2464      || owl_message_is_loginout(m)
2465      || !owl_message_is_type_zephyr(m)) {
2466    owl_function_makemsg("smartzpunt doesn't support this message type.");
2467    return;
2468  }
2469
2470  mclass = owl_message_get_class(m);
2471  minst = owl_message_get_instance(m);
2472  if (!mclass || !*mclass || *mclass==' '
2473      || (!strcasecmp(mclass, "message") && !strcasecmp(minst, "personal"))
2474      || (type && (!minst || !*minst|| *minst==' '))) {
2475    owl_function_makemsg("smartzpunt can't safely do this for <%s,%s>",
2476                         mclass, minst);
2477  } else {
2478    cmdprefix = "start-command zpunt ";
2479    cmd = owl_malloc(strlen(cmdprefix)+strlen(mclass)+strlen(minst)+3);
2480    strcpy(cmd, cmdprefix);
2481    strcat(cmd, mclass);
2482    if (type) {
2483      strcat(cmd, " ");
2484      strcat(cmd, minst);
2485    } else {
2486      strcat(cmd, " *");
2487    }
2488    owl_function_command(cmd);
2489    owl_free(cmd);
2490  }
2491}
2492
2493
2494
2495void owl_function_color_current_filter(char *color)
2496{
2497  owl_filter *f;
2498  char *name;
2499
2500  name=owl_view_get_filtname(owl_global_get_current_view(&g));
2501  f=owl_global_get_filter(&g, name);
2502  if (!f) {
2503    owl_function_makemsg("Unknown filter");
2504    return;
2505  }
2506
2507  /* don't touch the all filter */
2508  if (!strcmp(name, "all")) {
2509    owl_function_makemsg("You may not change the 'all' filter.");
2510    return;
2511  }
2512
2513  /* deal with the case of trying change the filter color */
2514  owl_filter_set_color(f, owl_util_string_to_color(color));
2515  owl_global_set_needrefresh(&g);
2516  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2517}
2518
2519void owl_function_show_colors()
2520{
2521  owl_fmtext fm;
2522
2523  owl_fmtext_init_null(&fm);
2524  owl_fmtext_append_normal_color(&fm, "default\n", OWL_COLOR_DEFAULT);
2525  owl_fmtext_append_normal_color(&fm, "red\n", OWL_COLOR_RED);
2526  owl_fmtext_append_normal_color(&fm, "green\n", OWL_COLOR_GREEN);
2527  owl_fmtext_append_normal_color(&fm, "yellow\n", OWL_COLOR_YELLOW);
2528  owl_fmtext_append_normal_color(&fm, "blue\n", OWL_COLOR_BLUE);
2529  owl_fmtext_append_normal_color(&fm, "magenta\n", OWL_COLOR_MAGENTA);
2530  owl_fmtext_append_normal_color(&fm, "cyan\n", OWL_COLOR_CYAN);
2531  owl_fmtext_append_normal_color(&fm, "white\n", OWL_COLOR_WHITE);
2532
2533  owl_function_popless_fmtext(&fm);
2534  owl_fmtext_free(&fm);
2535}
2536
2537void owl_function_zpunt(char *class, char *inst, char *recip, int direction)
2538{
2539  /* add the given class, inst, recip to the punt list for filtering.
2540   *   if direction==0 then punt
2541   *   if direction==1 then unpunt */
2542  owl_filter *f;
2543  owl_list *fl;
2544  char *buff;
2545  int ret, i, j;
2546
2547  fl=owl_global_get_puntlist(&g);
2548
2549  /* first, create the filter */
2550  f=malloc(sizeof(owl_filter));
2551  buff=malloc(strlen(class)+strlen(inst)+strlen(recip)+100);
2552  if (!strcmp(recip, "*")) {
2553    sprintf(buff, "class ^%s$ and instance ^%s$", class, inst);
2554  } else {
2555    sprintf(buff, "class ^%s$ and instance ^%s$ and recipient %s", class, inst, recip);
2556  }
2557  owl_function_debugmsg("About to filter %s", buff);
2558  ret=owl_filter_init_fromstring(f, "punt-filter", buff);
2559  owl_free(buff);
2560  if (ret) {
2561    owl_function_makemsg("Error creating filter for zpunt");
2562    owl_filter_free(f);
2563    return;
2564  }
2565
2566  /* Check for an identical filter */
2567  j=owl_list_get_size(fl);
2568  for (i=0; i<j; i++) {
2569    if (owl_filter_equiv(f, owl_list_get_element(fl, i))) {
2570      /* if we're punting, then just silently bow out on this duplicate */
2571      if (direction==0) {
2572        owl_filter_free(f);
2573        return;
2574      }
2575
2576      /* if we're unpunting, then remove this filter from the puntlist */
2577      if (direction==1) {
2578        owl_filter_free(owl_list_get_element(fl, i));
2579        owl_list_remove_element(fl, i);
2580        return;
2581      }
2582    }
2583  }
2584
2585  /* If we're punting, add the filter to the global punt list */
2586  if (direction==0) {
2587    owl_list_append_element(fl, f);
2588  }
2589}
2590
2591void owl_function_activate_keymap(char *keymap)
2592{
2593  if (!owl_keyhandler_activate(owl_global_get_keyhandler(&g), keymap)) {
2594    owl_function_makemsg("Unable to activate keymap '%s'", keymap);
2595  }
2596}
2597
2598
2599void owl_function_show_keymaps()
2600{
2601  owl_list l;
2602  owl_fmtext fm;
2603  owl_keymap *km;
2604  owl_keyhandler *kh;
2605  int i, numkm;
2606  char *kmname;
2607
2608  kh = owl_global_get_keyhandler(&g);
2609  owl_fmtext_init_null(&fm);
2610  owl_fmtext_append_bold(&fm, "Keymaps:   ");
2611  owl_fmtext_append_normal(&fm, "(use 'show keymap <name>' for details)\n");
2612  owl_keyhandler_get_keymap_names(kh, &l);
2613  owl_fmtext_append_list(&fm, &l, "\n", owl_function_keymap_summary);
2614  owl_fmtext_append_normal(&fm, "\n");
2615
2616  numkm = owl_list_get_size(&l);
2617  for (i=0; i<numkm; i++) {
2618    kmname = owl_list_get_element(&l, i);
2619    km = owl_keyhandler_get_keymap(kh, kmname);
2620    owl_fmtext_append_bold(&fm, "\n\n----------------------------------------------------------------------------------------------------\n\n");
2621    owl_keymap_get_details(km, &fm);   
2622  }
2623  owl_fmtext_append_normal(&fm, "\n");
2624 
2625  owl_function_popless_fmtext(&fm);
2626  owl_keyhandler_keymap_namelist_free(&l);
2627  owl_fmtext_free(&fm);
2628}
2629
2630char *owl_function_keymap_summary(void *name)
2631{
2632  owl_keymap *km
2633    = owl_keyhandler_get_keymap(owl_global_get_keyhandler(&g), name);
2634  if (km) return owl_keymap_summary(km);
2635  else return(NULL);
2636}
2637
2638/* TODO: implement for real */
2639void owl_function_show_keymap(char *name)
2640{
2641  owl_fmtext fm;
2642  owl_keymap *km;
2643
2644  owl_fmtext_init_null(&fm);
2645  km = owl_keyhandler_get_keymap(owl_global_get_keyhandler(&g), name);
2646  if (km) {
2647    owl_keymap_get_details(km, &fm);
2648  } else {
2649    owl_fmtext_append_normal(&fm, "No such keymap...\n");
2650  } 
2651  owl_function_popless_fmtext(&fm);
2652  owl_fmtext_free(&fm);
2653}
2654
2655void owl_function_help_for_command(char *cmdname)
2656{
2657  owl_fmtext fm;
2658
2659  owl_fmtext_init_null(&fm);
2660  owl_cmd_get_help(owl_global_get_cmddict(&g), cmdname, &fm);
2661  owl_function_popless_fmtext(&fm); 
2662  owl_fmtext_free(&fm);
2663}
2664
2665void owl_function_search_start(char *string, int direction)
2666{
2667  /* direction is OWL_DIRECTION_DOWNWARDS or OWL_DIRECTION_UPWARDS */
2668  owl_global_set_search_active(&g, string);
2669  owl_function_search_helper(0, direction);
2670}
2671
2672void owl_function_search_continue(int direction)
2673{
2674  /* direction is OWL_DIRECTION_DOWNWARDS or OWL_DIRECTION_UPWARDS */
2675  owl_function_search_helper(1, direction);
2676}
2677
2678void owl_function_search_helper(int mode, int direction)
2679{
2680  /* move to a message that contains the string.  If direction is
2681   * OWL_DIRECTION_DOWNWARDS then search fowards, if direction is
2682   * OWL_DIRECTION_UPWARDS then search backwards.
2683   *
2684   * If mode==0 then it will stay on the current message if it
2685   * contains the string.
2686   */
2687
2688  owl_view *v;
2689  int viewsize, i, curmsg, start;
2690  owl_message *m;
2691
2692  v=owl_global_get_current_view(&g);
2693  viewsize=owl_view_get_size(v);
2694  curmsg=owl_global_get_curmsg(&g);
2695 
2696  if (viewsize==0) {
2697    owl_function_makemsg("No messages present");
2698    return;
2699  }
2700
2701  if (mode==0) {
2702    start=curmsg;
2703  } else if (direction==OWL_DIRECTION_DOWNWARDS) {
2704    start=curmsg+1;
2705  } else {
2706    start=curmsg-1;
2707  }
2708
2709  /* bounds check */
2710  if (start>=viewsize || start<0) {
2711    owl_function_makemsg("No further matches found");
2712    return;
2713  }
2714
2715  for (i=start; i<viewsize && i>=0;) {
2716    m=owl_view_get_element(v, i);
2717    if (owl_message_search(m, owl_global_get_search_string(&g))) {
2718      owl_global_set_curmsg(&g, i);
2719      owl_function_calculate_topmsg(direction);
2720      owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2721      if (direction==OWL_DIRECTION_DOWNWARDS) {
2722        owl_global_set_direction_downwards(&g);
2723      } else {
2724        owl_global_set_direction_upwards(&g);
2725      }
2726      return;
2727    }
2728    if (direction==OWL_DIRECTION_DOWNWARDS) {
2729      i++;
2730    } else {
2731      i--;
2732    }
2733  }
2734  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2735  owl_function_makemsg("No matches found");
2736}
2737
2738
2739/* strips formatting from ztext and returns the unformatted text.
2740 * caller is responsible for freeing. */
2741char *owl_function_ztext_stylestrip(char *zt)
2742{
2743  owl_fmtext fm;
2744  char *plaintext;
2745
2746  owl_fmtext_init_null(&fm);
2747  owl_fmtext_append_ztext(&fm, zt);
2748  plaintext = owl_fmtext_print_plain(&fm);
2749  owl_fmtext_free(&fm);
2750  return(plaintext);
2751}
2752
2753/* Popup a buddylisting.  If file is NULL use the default .anyone */
2754void owl_function_buddylist(int aim, int zephyr, char *file)
2755{
2756  char *ourfile, *tmp, buff[LINE], *line;
2757  FILE *f;
2758  int numlocs, ret, i, j;
2759  ZLocations_t location[200];
2760  owl_fmtext fm;
2761  owl_buddylist *b;
2762
2763  owl_fmtext_init_null(&fm);
2764
2765  if (aim && owl_global_is_aimloggedin(&g)) {
2766    b=owl_global_get_buddylist(&g);
2767
2768    owl_fmtext_append_bold(&fm, "AIM users logged in:\n");
2769    j=owl_buddylist_get_size(b);
2770    for (i=0; i<j; i++) {
2771      owl_fmtext_append_normal(&fm, "  ");
2772      owl_fmtext_append_normal(&fm, owl_buddylist_get_buddy(b, i));
2773      owl_fmtext_append_normal(&fm, "\n");
2774    }
2775  }
2776
2777  if (zephyr) {
2778    if (file==NULL) {
2779      tmp=owl_global_get_homedir(&g);
2780      if (!tmp) {
2781        owl_function_makemsg("Could not determine home directory");
2782        return;
2783      }
2784      ourfile=owl_malloc(strlen(tmp)+50);
2785      sprintf(ourfile, "%s/.anyone", owl_global_get_homedir(&g));
2786    } else {
2787      ourfile=owl_strdup(file);
2788    }
2789   
2790    f=fopen(ourfile, "r");
2791    if (!f) {
2792      owl_function_makemsg("Error opening file %s: %s",
2793                           ourfile,
2794                           strerror(errno) ? strerror(errno) : "");
2795      return;
2796    }
2797   
2798    owl_fmtext_append_bold(&fm, "Zephyr users logged in:\n");
2799   
2800    while (fgets(buff, LINE, f)!=NULL) {
2801      /* ignore comments, blank lines etc. */
2802      if (buff[0]=='#') continue;
2803      if (buff[0]=='\n') continue;
2804      if (buff[0]=='\0') continue;
2805     
2806      /* strip the \n */
2807      buff[strlen(buff)-1]='\0';
2808     
2809      /* ingore from # on */
2810      tmp=strchr(buff, '#');
2811      if (tmp) tmp[0]='\0';
2812     
2813      /* ingore from SPC */
2814      tmp=strchr(buff, ' ');
2815      if (tmp) tmp[0]='\0';
2816     
2817      /* stick on the local realm. */
2818      if (!strchr(buff, '@')) {
2819        strcat(buff, "@");
2820        strcat(buff, ZGetRealm());
2821      }
2822     
2823      ret=ZLocateUser(buff, &numlocs, ZAUTH);
2824      if (ret!=ZERR_NONE) {
2825        owl_function_makemsg("Error getting location for %s", buff);
2826        continue;
2827      }
2828     
2829      numlocs=200;
2830      ret=ZGetLocations(location, &numlocs);
2831      if (ret==0) {
2832        for (i=0; i<numlocs; i++) {
2833          line=malloc(strlen(location[i].host)+strlen(location[i].time)+strlen(location[i].tty)+100);
2834          tmp=short_zuser(buff);
2835          sprintf(line, "  %-10.10s %-24.24s %-12.12s  %20.20s\n",
2836                  tmp,
2837                  location[i].host,
2838                  location[i].tty,
2839                  location[i].time);
2840          owl_fmtext_append_normal(&fm, line);
2841          owl_free(tmp);
2842        }
2843        if (numlocs>=200) {
2844          owl_fmtext_append_normal(&fm, "  Too many locations found for this user, truncating.\n");
2845        }
2846      }
2847    }
2848    fclose(f);
2849    owl_free(ourfile);
2850  }
2851 
2852  owl_function_popless_fmtext(&fm);
2853  owl_fmtext_free(&fm);
2854}
2855
2856void owl_function_dump(char *filename) 
2857{
2858  int i, j, count;
2859  owl_message *m;
2860  owl_view *v;
2861  FILE *file;
2862  /* struct stat sbuf; */
2863
2864  v=owl_global_get_current_view(&g);
2865
2866  /* in the future make it ask yes/no */
2867  /*
2868  ret=stat(filename, &sbuf);
2869  if (!ret) {
2870    ret=owl_function_askyesno("File exists, continue? [Y/n]");
2871    if (!ret) return;
2872  }
2873  */
2874
2875  file=fopen(filename, "w");
2876  if (!file) {
2877    owl_function_makemsg("Error opening file");
2878    return;
2879  }
2880
2881  count=0;
2882  j=owl_view_get_size(v);
2883  for (i=0; i<j; i++) {
2884    m=owl_view_get_element(v, i);
2885    fputs(owl_message_get_text(m), file);
2886  }
2887  fclose(file);
2888}
2889
2890
2891
2892void owl_function_do_newmsgproc(void)
2893{
2894  if (owl_global_get_newmsgproc(&g) && strcmp(owl_global_get_newmsgproc(&g), "")) {
2895    /* if there's a process out there, we need to check on it */
2896    if (owl_global_get_newmsgproc_pid(&g)) {
2897      owl_function_debugmsg("Checking on newmsgproc pid==%i", owl_global_get_newmsgproc_pid(&g));
2898      owl_function_debugmsg("Waitpid return is %i", waitpid(owl_global_get_newmsgproc_pid(&g), NULL, WNOHANG));
2899      waitpid(owl_global_get_newmsgproc_pid(&g), NULL, WNOHANG);
2900      if (waitpid(owl_global_get_newmsgproc_pid(&g), NULL, WNOHANG)==-1) {
2901        /* it exited */
2902        owl_global_set_newmsgproc_pid(&g, 0);
2903        owl_function_debugmsg("newmsgproc exited");
2904      } else {
2905        owl_function_debugmsg("newmsgproc did not exit");
2906      }
2907    }
2908   
2909    /* if it exited, fork & exec a new one */
2910    if (owl_global_get_newmsgproc_pid(&g)==0) {
2911      int i, myargc;
2912      i=fork();
2913      if (i) {
2914        /* parent set the child's pid */
2915        owl_global_set_newmsgproc_pid(&g, i);
2916        owl_function_debugmsg("I'm the parent and I started a new newmsgproc with pid %i", i);
2917      } else {
2918        /* child exec's the program */
2919        char **parsed;
2920        parsed=owl_parseline(owl_global_get_newmsgproc(&g), &myargc);
2921        if (myargc < 0) {
2922          owl_function_debugmsg("Could not parse newmsgproc '%s': unbalanced quotes?", owl_global_get_newmsgproc(&g));
2923        }
2924        if (myargc <= 0) {
2925          _exit(127);
2926        }
2927        parsed=realloc(parsed, sizeof(*parsed) * (myargc+1));
2928        parsed[myargc] = NULL;
2929       
2930        owl_function_debugmsg("About to exec \"%s\" with %d arguments", parsed[0], myargc);
2931       
2932        execvp(parsed[0], parsed);
2933       
2934       
2935        /* was there an error exec'ing? */
2936        owl_function_debugmsg("Cannot run newmsgproc '%s': cannot exec '%s': %s", 
2937                              owl_global_get_newmsgproc(&g), parsed[0], strerror(errno));
2938        _exit(127);
2939      }
2940    }
2941  }
2942}
2943
2944/* print the xterm escape sequence to raise the window */
2945void owl_function_xterm_raise(void)
2946{
2947  printf("\033[5t");
2948}
2949
2950/* print the xterm escape sequence to deiconify the window */
2951void owl_function_xterm_deiconify(void)
2952{
2953  printf("\033[1t");
2954}
2955
2956/* Add the specified command to the startup file.  Eventually this
2957 * should be clever, and rewriting settings that will obviosly
2958 * override earlier settings with 'set' 'bindkey' and 'alias'
2959 * commands.  For now though we just remove any line that would
2960 * duplicate this one and then append this line to the end of
2961 * startupfile.
2962 */
2963void owl_function_addstartup(char *buff)
2964{
2965  FILE *file;
2966  char *filename;
2967
2968  filename=owl_sprintf("%s/%s", owl_global_get_homedir(&g), OWL_STARTUP_FILE);
2969  file=fopen(filename, "a");
2970  if (!file) {
2971    owl_function_makemsg("Error opening startupfile for new command");
2972    owl_free(filename);
2973    return;
2974  }
2975
2976  /* delete earlier copies */
2977  owl_util_file_deleteline(filename, buff, 1);
2978  owl_free(filename);
2979
2980  /* add this line */
2981  fprintf(file, "%s\n", buff);
2982
2983  fclose(file);
2984}
2985
2986/* Remove the specified command from the startup file. */
2987void owl_function_delstartup(char *buff)
2988{
2989  char *filename;
2990  filename=owl_sprintf("%s/%s", owl_global_get_homedir(&g), OWL_STARTUP_FILE);
2991  owl_util_file_deleteline(filename, buff, 1);
2992  owl_free(filename);
2993}
2994
2995void owl_function_execstartup(void)
2996{
2997  FILE *file;
2998  char *filename;
2999  char buff[LINE];
3000
3001  filename=owl_sprintf("%s/%s", owl_global_get_homedir(&g), OWL_STARTUP_FILE);
3002  file=fopen(filename, "r");
3003  owl_free(filename);
3004  if (!file) {
3005    /* just fail silently if it doesn't exist */
3006    return;
3007  }
3008  while (fgets(buff, LINE, file)!=NULL) {
3009    buff[strlen(buff)-1]='\0';
3010    owl_function_command(buff);
3011  }
3012  fclose(file);
3013}
3014
3015void owl_function_toggleoneline()
3016{
3017  char *style;
3018
3019  style=owl_global_get_style(&g);
3020
3021  if (strcmp(style, "oneline")) {
3022    owl_global_set_style(&g, "oneline");
3023  } else {
3024    owl_global_set_style(&g, owl_global_get_default_style(&g));
3025  }
3026}
Note: See TracBrowser for help on using the repository browser.