source: functions.c @ 5789230

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