source: functions.c @ 11a7c6a

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