source: functions.c @ aac889a

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