source: functions.c @ d559df9

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