source: functions.c @ f4d0975

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