source: functions.c @ 9381782

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