source: functions.c @ 0c502e9

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