source: functions.c @ 15283bb

barnowl_perlaimdebianowlrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 15283bb was f17bff98, checked in by James M. Kretchmar <kretch@mit.edu>, 20 years ago
Added the 'show startup' command Alphebetized 'show' help.
  • Property mode set to 100644
File size: 84.6 KB
Line 
1#include <stdio.h>
2#include <stdlib.h>
3#include <unistd.h>
4#include <signal.h>
5#include <string.h>
6#include <time.h>
7#include <sys/types.h>
8#include <sys/stat.h>
9#include <sys/wait.h>
10#include <errno.h>
11#include <signal.h>
12#include "owl.h"
13
14static const char fileIdent[] = "$Id$";
15
16void owl_function_noop(void)
17{
18  return;
19}
20
21char *owl_function_command(char *cmdbuff)
22{
23  owl_function_debugmsg("executing command: %s", cmdbuff);
24  return owl_cmddict_execute(owl_global_get_cmddict(&g), 
25                             owl_global_get_context(&g), cmdbuff);
26}
27
28void owl_function_command_norv(char *cmdbuff)
29{
30  char *rv;
31  rv=owl_function_command(cmdbuff);
32  if (rv) owl_free(rv);
33}
34
35void owl_function_command_alias(char *alias_from, char *alias_to)
36{
37  owl_cmddict_add_alias(owl_global_get_cmddict(&g), alias_from, alias_to);
38}
39
40owl_cmd *owl_function_get_cmd(char *name)
41{
42  return owl_cmddict_find(owl_global_get_cmddict(&g), name);
43}
44
45void owl_function_show_commands()
46{
47  owl_list l;
48  owl_fmtext fm;
49
50  owl_fmtext_init_null(&fm);
51  owl_fmtext_append_bold(&fm, "Commands:  ");
52  owl_fmtext_append_normal(&fm, "(use 'show command <name>' for details)\n");
53  owl_cmddict_get_names(owl_global_get_cmddict(&g), &l);
54  owl_fmtext_append_list(&fm, &l, "\n", owl_function_cmd_describe);
55  owl_fmtext_append_normal(&fm, "\n");
56  owl_function_popless_fmtext(&fm);
57  owl_cmddict_namelist_free(&l);
58  owl_fmtext_free(&fm);
59}
60
61void owl_function_show_view(char *viewname)
62{
63  owl_view *v;
64  owl_fmtext fm;
65
66  /* we only have the one view right now */
67  v=owl_global_get_current_view(&g);
68  if (viewname && strcmp(viewname, owl_view_get_name(v))) {
69    owl_function_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_popless_file(char *filename)
1209{
1210  owl_fmtext fm;
1211  FILE *file;
1212  char buff[1024];
1213
1214  file=fopen(filename, "r");
1215  if (!file) {
1216    owl_function_error("Could not open file: %s", filename);
1217    return;
1218  }
1219
1220  owl_fmtext_init_null(&fm);
1221  while (fgets(buff, 1024, file)) {
1222    owl_fmtext_append_normal(&fm, buff);
1223    owl_fmtext_append_normal(&fm, "\n");
1224  }
1225
1226  owl_function_popless_fmtext(&fm);
1227  owl_fmtext_free(&fm);
1228  fclose(file);
1229}
1230
1231void owl_function_about()
1232{
1233  char buff[5000];
1234
1235  sprintf(buff, "This is owl version %s\n", OWL_VERSION_STRING);
1236  strcat(buff, "\nOwl was written by James Kretchmar at the Massachusetts\n");
1237  strcat(buff, "Institute of Technology.  The first version, 0.5, was\n");
1238  strcat(buff, "released in March 2002.\n");
1239  strcat(buff, "\n");
1240  strcat(buff, "The name 'owl' was chosen in reference to the owls in the\n");
1241  strcat(buff, "Harry Potter novels, who are tasked with carrying messages\n");
1242  strcat(buff, "between Witches and Wizards.\n");
1243  strcat(buff, "\n");
1244  strcat(buff, "Copyright 2002 Massachusetts Institute of Technology\n");
1245  strcat(buff, "\n");
1246  strcat(buff, "Permission to use, copy, modify, and distribute this\n");
1247  strcat(buff, "software and its documentation for any purpose and without\n");
1248  strcat(buff, "fee is hereby granted, provided that the above copyright\n");
1249  strcat(buff, "notice and this permission notice appear in all copies\n");
1250  strcat(buff, "and in supporting documentation.  No representation is\n");
1251  strcat(buff, "made about the suitability of this software for any\n");
1252  strcat(buff, "purpose.  It is provided \"as is\" without express\n");
1253  strcat(buff, "or implied warranty.\n");
1254  owl_function_popless_text(buff);
1255}
1256
1257void owl_function_info()
1258{
1259  owl_message *m;
1260  owl_fmtext fm, attrfm;
1261  char buff[10000];
1262  owl_view *v;
1263#ifdef HAVE_LIBZEPHYR
1264  ZNotice_t *n;
1265#endif
1266
1267  owl_fmtext_init_null(&fm);
1268 
1269  v=owl_global_get_current_view(&g);
1270  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
1271  if (!m || owl_view_get_size(v)==0) {
1272    owl_function_error("No message selected\n");
1273    return;
1274  }
1275
1276  owl_fmtext_append_bold(&fm, "General Information:\n");
1277  owl_fmtext_append_normal(&fm, "  Msg Id    : ");
1278  sprintf(buff, "%i", owl_message_get_id(m));
1279  owl_fmtext_append_normal(&fm, buff);
1280  owl_fmtext_append_normal(&fm, "\n");
1281
1282  owl_fmtext_append_normal(&fm, "  Type      : ");
1283  owl_fmtext_append_bold(&fm, owl_message_type_to_string(m));
1284  owl_fmtext_append_normal(&fm, "\n");
1285
1286  if (owl_message_is_direction_in(m)) {
1287    owl_fmtext_append_normal(&fm, "  Direction : in\n");
1288  } else if (owl_message_is_direction_out(m)) {
1289    owl_fmtext_append_normal(&fm, "  Direction : out\n");
1290  } else if (owl_message_is_direction_none(m)) {
1291    owl_fmtext_append_normal(&fm, "  Direction : none\n");
1292  } else {
1293    owl_fmtext_append_normal(&fm, "  Direction : unknown\n");
1294  }
1295
1296  owl_fmtext_append_normal(&fm, "  Time      : ");
1297  owl_fmtext_append_normal(&fm, owl_message_get_timestr(m));
1298  owl_fmtext_append_normal(&fm, "\n");
1299
1300  if (!owl_message_is_type_admin(m)) {
1301    owl_fmtext_append_normal(&fm, "  Sender    : ");
1302    owl_fmtext_append_normal(&fm, owl_message_get_sender(m));
1303    owl_fmtext_append_normal(&fm, "\n");
1304   
1305    owl_fmtext_append_normal(&fm, "  Recipient : ");
1306    owl_fmtext_append_normal(&fm, owl_message_get_recipient(m));
1307    owl_fmtext_append_normal(&fm, "\n");
1308  }
1309   
1310  if (owl_message_is_type_zephyr(m)) {
1311    owl_fmtext_append_bold(&fm, "\nZephyr Specific Information:\n");
1312   
1313    owl_fmtext_append_normal(&fm, "  Class     : ");
1314    owl_fmtext_append_normal(&fm, owl_message_get_class(m));
1315    owl_fmtext_append_normal(&fm, "\n");
1316    owl_fmtext_append_normal(&fm, "  Instance  : ");
1317    owl_fmtext_append_normal(&fm, owl_message_get_instance(m));
1318    owl_fmtext_append_normal(&fm, "\n");
1319    owl_fmtext_append_normal(&fm, "  Opcode    : ");
1320    owl_fmtext_append_normal(&fm, owl_message_get_opcode(m));
1321    owl_fmtext_append_normal(&fm, "\n");
1322   
1323    owl_fmtext_append_normal(&fm, "  Time      : ");
1324    owl_fmtext_append_normal(&fm, owl_message_get_timestr(m));
1325    owl_fmtext_append_normal(&fm, "\n");
1326#ifdef HAVE_LIBZEPHYR
1327    if (owl_message_is_direction_in(m)) {
1328      char *ptr, tmpbuff[1024];
1329      int i, j, fields, len;
1330
1331      n=owl_message_get_notice(m);
1332
1333      owl_fmtext_append_normal(&fm, "  Kind      : ");
1334      if (n->z_kind==UNSAFE) {
1335        owl_fmtext_append_normal(&fm, "UNSAFE\n");
1336      } else if (n->z_kind==UNACKED) {
1337        owl_fmtext_append_normal(&fm, "UNACKED\n");
1338      } else if (n->z_kind==ACKED) {
1339        owl_fmtext_append_normal(&fm, "ACKED\n");
1340      } else if (n->z_kind==HMACK) {
1341        owl_fmtext_append_normal(&fm, "HMACK\n");
1342      } else if (n->z_kind==HMCTL) {
1343        owl_fmtext_append_normal(&fm, "HMCTL\n");
1344      } else if (n->z_kind==SERVACK) {
1345        owl_fmtext_append_normal(&fm, "SERVACK\n");
1346      } else if (n->z_kind==SERVNAK) {
1347        owl_fmtext_append_normal(&fm, "SERVNACK\n");
1348      } else if (n->z_kind==CLIENTACK) {
1349        owl_fmtext_append_normal(&fm, "CLIENTACK\n");
1350      } else if (n->z_kind==STAT) {
1351        owl_fmtext_append_normal(&fm, "STAT\n");
1352      } else {
1353        owl_fmtext_append_normal(&fm, "ILLEGAL VALUE\n");
1354      }
1355      owl_fmtext_append_normal(&fm, "  Host      : ");
1356      owl_fmtext_append_normal(&fm, owl_message_get_hostname(m));
1357      owl_fmtext_append_normal(&fm, "\n");
1358      sprintf(buff, "  Port      : %i\n", n->z_port);
1359      owl_fmtext_append_normal(&fm, buff);
1360
1361      owl_fmtext_append_normal(&fm,    "  Auth      : ");
1362      owl_fmtext_append_normal(&fm, owl_zephyr_get_authstr(n));
1363      owl_fmtext_append_normal(&fm, "\n");
1364
1365      /* fix this */
1366      sprintf(buff, "  Checkd Ath: %i\n", n->z_checked_auth);
1367      sprintf(buff, "%s  Multi notc: %s\n", buff, n->z_multinotice);
1368      sprintf(buff, "%s  Num other : %i\n", buff, n->z_num_other_fields);
1369      sprintf(buff, "%s  Msg Len   : %i\n", buff, n->z_message_len);
1370      owl_fmtext_append_normal(&fm, buff);
1371     
1372      sprintf(buff, "  Fields    : %i\n", owl_zephyr_get_num_fields(n));
1373      owl_fmtext_append_normal(&fm, buff);
1374     
1375      fields=owl_zephyr_get_num_fields(n);
1376      for (i=0; i<fields; i++) {
1377        sprintf(buff, "  Field %i   : ", i+1);
1378       
1379        ptr=owl_zephyr_get_field(n, i+1, &len);
1380        if (!ptr) break;
1381        if (len<30) {
1382          strncpy(tmpbuff, ptr, len);
1383          tmpbuff[len]='\0';
1384        } else {
1385          strncpy(tmpbuff, ptr, 30);
1386          tmpbuff[30]='\0';
1387          strcat(tmpbuff, "...");
1388        }
1389       
1390        for (j=0; j<strlen(tmpbuff); j++) {
1391          if (tmpbuff[j]=='\n') tmpbuff[j]='~';
1392          if (tmpbuff[j]=='\r') tmpbuff[j]='!';
1393        }
1394       
1395        strcat(buff, tmpbuff);
1396        strcat(buff, "\n");
1397        owl_fmtext_append_normal(&fm, buff);
1398      }
1399      owl_fmtext_append_normal(&fm, "  Default Fm:");
1400      owl_fmtext_append_normal(&fm, n->z_default_format);
1401    }
1402#endif   
1403  }
1404
1405  if (owl_message_is_type_aim(m)) {
1406    owl_fmtext_append_bold(&fm, "\nAIM Specific Information:\n");
1407  }
1408
1409  owl_fmtext_append_bold(&fm, "\nOwl Message Attributes:\n");
1410  owl_message_attributes_tofmtext(m, &attrfm);
1411  owl_fmtext_append_fmtext(&fm, &attrfm);
1412 
1413  owl_function_popless_fmtext(&fm);
1414  owl_fmtext_free(&fm);
1415  owl_fmtext_free(&attrfm);
1416}
1417
1418/* print the current message in a popup window.
1419 * Use the 'default' style regardless of whatever
1420 * style the user may be using
1421 */
1422void owl_function_curmsg_to_popwin()
1423{
1424  owl_popwin *pw;
1425  owl_view *v;
1426  owl_message *m;
1427  owl_style *s;
1428  owl_fmtext fm;
1429
1430  v=owl_global_get_current_view(&g);
1431  s=owl_global_get_style_by_name(&g, "default");
1432  pw=owl_global_get_popwin(&g);
1433
1434  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
1435
1436  if (!m || owl_view_get_size(v)==0) {
1437    owl_function_error("No current message");
1438    return;
1439  }
1440
1441  owl_fmtext_init_null(&fm);
1442  owl_style_get_formattext(s, &fm, m);
1443
1444  owl_function_popless_fmtext(&fm);
1445  owl_fmtext_free(&fm);
1446}
1447
1448void owl_function_page_curmsg(int step)
1449{
1450  /* scroll down or up within the current message IF the message is truncated */
1451
1452  int offset, curmsg, lines;
1453  owl_view *v;
1454  owl_message *m;
1455
1456  offset=owl_global_get_curmsg_vert_offset(&g);
1457  v=owl_global_get_current_view(&g);
1458  curmsg=owl_global_get_curmsg(&g);
1459  m=owl_view_get_element(v, curmsg);
1460  if (!m || owl_view_get_size(v)==0) return;
1461  lines=owl_message_get_numlines(m);
1462
1463  if (offset==0) {
1464    /* Bail if the curmsg isn't the last one displayed */
1465    if (curmsg != owl_mainwin_get_last_msg(owl_global_get_mainwin(&g))) {
1466      owl_function_makemsg("The entire message is already displayed");
1467      return;
1468    }
1469   
1470    /* Bail if we're not truncated */
1471    if (!owl_mainwin_is_curmsg_truncated(owl_global_get_mainwin(&g))) {
1472      owl_function_makemsg("The entire message is already displayed");
1473      return;
1474    }
1475  }
1476 
1477 
1478  /* don't scroll past the last line */
1479  if (step>0) {
1480    if (offset+step > lines-1) {
1481      owl_global_set_curmsg_vert_offset(&g, lines-1);
1482    } else {
1483      owl_global_set_curmsg_vert_offset(&g, offset+step);
1484    }
1485  }
1486
1487  /* would we be before the beginning of the message? */
1488  if (step<0) {
1489    if (offset+step<0) {
1490      owl_global_set_curmsg_vert_offset(&g, 0);
1491    } else {
1492      owl_global_set_curmsg_vert_offset(&g, offset+step);
1493    }
1494  }
1495 
1496  /* redisplay */
1497  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
1498  owl_global_set_needrefresh(&g);
1499}
1500
1501void owl_function_resize_typwin(int newsize)
1502{
1503  owl_global_set_typwin_lines(&g, newsize);
1504  owl_function_resize();
1505}
1506
1507void owl_function_typwin_grow()
1508{
1509  int i;
1510
1511  i=owl_global_get_typwin_lines(&g);
1512  owl_function_resize_typwin(i+1);
1513}
1514
1515void owl_function_typwin_shrink()
1516{
1517  int i;
1518
1519  i=owl_global_get_typwin_lines(&g);
1520  if (i>2) {
1521    owl_function_resize_typwin(i-1);
1522  }
1523}
1524
1525void owl_function_mainwin_pagedown()
1526{
1527  int i;
1528
1529  i=owl_mainwin_get_last_msg(owl_global_get_mainwin(&g));
1530  if (i<0) return;
1531  if (owl_mainwin_is_last_msg_truncated(owl_global_get_mainwin(&g))
1532      && (owl_global_get_curmsg(&g) < i)
1533      && (i>0)) {
1534    i--;
1535  }
1536  owl_global_set_curmsg(&g, i);
1537  owl_function_nextmsg();
1538}
1539
1540void owl_function_mainwin_pageup()
1541{
1542  owl_global_set_curmsg(&g, owl_global_get_topmsg(&g));
1543  owl_function_prevmsg();
1544}
1545
1546void owl_function_getsubs()
1547{
1548  char *buff;
1549
1550  buff=owl_zephyr_getsubs();
1551
1552  if (buff) {
1553    owl_function_popless_text(buff);
1554  } else {
1555    owl_function_popless_text("Error getting subscriptions");
1556  }
1557           
1558  owl_free(buff);
1559}
1560
1561#define PABUFLEN 5000
1562void owl_function_printallvars()
1563{
1564  char buff[PABUFLEN], *pos, *name;
1565  owl_list varnames;
1566  int i, numvarnames, rem;
1567
1568  pos = buff;
1569  pos += sprintf(pos, "%-20s = %s\n", "VARIABLE", "VALUE");
1570  pos += sprintf(pos, "%-20s   %s\n",  "--------", "-----");
1571  owl_variable_dict_get_names(owl_global_get_vardict(&g), &varnames);
1572  rem = (buff+PABUFLEN)-pos-1;
1573  numvarnames = owl_list_get_size(&varnames);
1574  for (i=0; i<numvarnames; i++) {
1575    name = owl_list_get_element(&varnames, i);
1576    if (name && name[0]!='_') {
1577      rem = (buff+PABUFLEN)-pos-1;   
1578      pos += snprintf(pos, rem, "\n%-20s = ", name);
1579      rem = (buff+PABUFLEN)-pos-1;   
1580      owl_variable_get_tostring(owl_global_get_vardict(&g), name, pos, rem);
1581      pos = buff+strlen(buff);
1582    }
1583  }
1584  rem = (buff+PABUFLEN)-pos-1;   
1585  snprintf(pos, rem, "\n");
1586  owl_variable_dict_namelist_free(&varnames);
1587 
1588  owl_function_popless_text(buff);
1589}
1590
1591void owl_function_show_variables()
1592{
1593  owl_list varnames;
1594  owl_fmtext fm; 
1595  int i, numvarnames;
1596  char *varname;
1597
1598  owl_fmtext_init_null(&fm);
1599  owl_fmtext_append_bold(&fm, 
1600      "Variables: (use 'show variable <name>' for details)\n");
1601  owl_variable_dict_get_names(owl_global_get_vardict(&g), &varnames);
1602  numvarnames = owl_list_get_size(&varnames);
1603  for (i=0; i<numvarnames; i++) {
1604    varname = owl_list_get_element(&varnames, i);
1605    if (varname && varname[0]!='_') {
1606      owl_variable_describe(owl_global_get_vardict(&g), varname, &fm);
1607    }
1608  }
1609  owl_variable_dict_namelist_free(&varnames);
1610  owl_function_popless_fmtext(&fm);
1611  owl_fmtext_free(&fm);
1612}
1613
1614void owl_function_show_variable(char *name)
1615{
1616  owl_fmtext fm; 
1617
1618  owl_fmtext_init_null(&fm);
1619  owl_variable_get_help(owl_global_get_vardict(&g), name, &fm);
1620  owl_function_popless_fmtext(&fm);
1621  owl_fmtext_free(&fm); 
1622}
1623
1624/* note: this applies to global message list, not to view.
1625 * If flag is 1, deletes.  If flag is 0, undeletes. */
1626void owl_function_delete_by_id(int id, int flag)
1627{
1628  owl_messagelist *ml;
1629  owl_message *m;
1630  ml = owl_global_get_msglist(&g);
1631  m = owl_messagelist_get_by_id(ml, id);
1632  if (m) {
1633    if (flag == 1) {
1634      owl_message_mark_delete(m);
1635    } else if (flag == 0) {
1636      owl_message_unmark_delete(m);
1637    }
1638    owl_mainwin_redisplay(owl_global_get_mainwin(&g));
1639    owl_global_set_needrefresh(&g);
1640  } else {
1641    owl_function_error("No message with id %d: unable to mark for (un)delete",id);
1642  }
1643}
1644
1645void owl_function_delete_automsgs()
1646{
1647  /* mark for deletion all messages in the current view that match the
1648   * 'trash' filter */
1649
1650  int i, j, count;
1651  owl_message *m;
1652  owl_view *v;
1653  owl_filter *f;
1654
1655  /* get the trash filter */
1656  f=owl_global_get_filter(&g, "trash");
1657  if (!f) {
1658    owl_function_error("No trash filter defined");
1659    return;
1660  }
1661
1662  v=owl_global_get_current_view(&g);
1663
1664  count=0;
1665  j=owl_view_get_size(v);
1666  for (i=0; i<j; i++) {
1667    m=owl_view_get_element(v, i);
1668    if (owl_filter_message_match(f, m)) {
1669      count++;
1670      owl_message_mark_delete(m);
1671    }
1672  }
1673  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
1674  owl_function_makemsg("%i messages marked for deletion", count);
1675  owl_global_set_needrefresh(&g);
1676}
1677
1678void owl_function_status()
1679{
1680  char buff[5000];
1681  time_t start;
1682  int up, days, hours, minutes;
1683  owl_fmtext fm;
1684
1685  owl_fmtext_init_null(&fm);
1686
1687  start=owl_global_get_starttime(&g);
1688
1689  owl_fmtext_append_normal(&fm, "Version: ");
1690  owl_fmtext_append_normal(&fm, OWL_VERSION_STRING);
1691  owl_fmtext_append_normal(&fm, "\n");
1692
1693  sprintf(buff, "Screen size: %i lines, %i columns\n", owl_global_get_lines(&g), owl_global_get_cols(&g));
1694  owl_fmtext_append_normal(&fm, buff);
1695
1696  owl_fmtext_append_normal(&fm, "Startup Arugments: ");
1697  owl_fmtext_append_normal(&fm, owl_global_get_startupargs(&g));
1698  owl_fmtext_append_normal(&fm, "\n");
1699  sprintf(buff, "Startup Time: %s", ctime(&start));
1700  owl_fmtext_append_normal(&fm, buff);
1701 
1702
1703  up=owl_global_get_runtime(&g);
1704  days=up/86400;
1705  up-=days*86400;
1706  hours=up/3600;
1707  up-=hours*3600;
1708  minutes=up/60;
1709  up-=minutes*60;
1710  sprintf(buff, "Run Time: %i days %2.2i:%2.2i:%2.2i\n", days, hours, minutes, up);
1711  owl_fmtext_append_normal(&fm, buff);
1712
1713  if (owl_global_get_hascolors(&g)) {
1714    sprintf(buff, "Color: Yes, %i color pairs.\n", owl_global_get_colorpairs(&g));
1715  } else {
1716    sprintf(buff, "Color: No.\n");
1717  }
1718  owl_fmtext_append_normal(&fm, buff);
1719
1720  /*
1721  sprintf(buff, "%sMemory Malloced: %i\n", buff, owl_global_get_malloced(&g));
1722  sprintf(buff, "%sMemory Freed: %i\n", buff, owl_global_get_freed(&g));
1723  sprintf(buff, "%sMemory In Use: %i\n", buff, owl_global_get_meminuse(&g));
1724  */
1725
1726  owl_fmtext_append_normal(&fm, "\n");
1727  if (owl_global_is_aimloggedin(&g)) {
1728    owl_fmtext_append_normal(&fm, "AIM: logged in as ");
1729    owl_fmtext_append_normal(&fm, owl_global_get_aim_screenname(&g));
1730    owl_fmtext_append_normal(&fm, "\n");
1731  } else {
1732    owl_fmtext_append_normal(&fm, "AIM: not logged in\n");
1733  }
1734  if (owl_global_is_doaimevents(&g)) {
1735    owl_fmtext_append_normal(&fm, "AIM: processing events\n ");
1736  } else {
1737    owl_fmtext_append_normal(&fm, "AIM: not processing events\n ");
1738  }
1739
1740  owl_function_popless_fmtext(&fm);
1741  owl_fmtext_free(&fm);
1742}
1743
1744void owl_function_show_term()
1745{
1746  owl_fmtext fm;
1747  char buff[LINE];
1748
1749  owl_fmtext_init_null(&fm);
1750  sprintf(buff, "Terminal Lines: %i\nTerminal Columns: %i\n",
1751          owl_global_get_lines(&g),
1752          owl_global_get_cols(&g));
1753  owl_fmtext_append_normal(&fm, buff);
1754
1755  if (owl_global_get_hascolors(&g)) {
1756    owl_fmtext_append_normal(&fm, "Color: Yes\n");
1757    sprintf(buff, "Number of color pairs: %i\n", owl_global_get_colorpairs(&g));
1758    owl_fmtext_append_normal(&fm, buff);
1759    sprintf(buff, "Can change colors: %s\n", can_change_color() ? "yes" : "no");
1760    owl_fmtext_append_normal(&fm, buff);
1761  } else {
1762    owl_fmtext_append_normal(&fm, "Color: No\n");
1763  }
1764
1765  owl_function_popless_fmtext(&fm);
1766  owl_fmtext_free(&fm);
1767}
1768
1769/* if type = 0 then normal reply.
1770 * if type = 1 then it's a reply to sender
1771 * if enter = 0 then allow the command to be edited
1772 * if enter = 1 then don't wait for editing
1773 */
1774void owl_function_reply(int type, int enter)
1775{
1776  char *buff=NULL, *oldbuff;
1777  owl_message *m;
1778  owl_filter *f;
1779 
1780  if (owl_view_get_size(owl_global_get_current_view(&g))==0) {
1781    owl_function_error("No message selected");
1782  } else {
1783    char *class, *inst, *to, *cc=NULL;
1784   
1785    m=owl_view_get_element(owl_global_get_current_view(&g), owl_global_get_curmsg(&g));
1786    if (!m) {
1787      owl_function_error("No message selected");
1788      return;
1789    }
1790
1791    /* first check if we catch the reply-lockout filter */
1792    f=owl_global_get_filter(&g, "reply-lockout");
1793    if (f) {
1794      if (owl_filter_message_match(f, m)) {
1795        owl_function_error("Sorry, replies to this message have been disabled by the reply-lockout filter");
1796        return;
1797      }
1798    }
1799
1800    /* admin */
1801    if (owl_message_is_type_admin(m)) {
1802      owl_function_error("You cannot reply to an admin message");
1803      return;
1804    }
1805
1806    /* zephyr */
1807    if (owl_message_is_type_zephyr(m)) {
1808      /* if it's a zephyr we sent, send it out the same way again */
1809      if (owl_message_is_direction_out(m)) {
1810        owl_function_zwrite_setup(owl_message_get_zwriteline(m));
1811        owl_global_set_buffercommand(&g, owl_message_get_zwriteline(m));
1812        return;
1813      }
1814
1815      /* Special case a personal reply to a webzephyr user on a class */
1816      if ((type==1) && !strcasecmp(owl_message_get_opcode(m), OWL_WEBZEPHYR_OPCODE)) {
1817        class=OWL_WEBZEPHYR_CLASS;
1818        inst=owl_message_get_sender(m);
1819        to=OWL_WEBZEPHYR_PRINCIPAL;
1820      } else if (!strcasecmp(owl_message_get_class(m), OWL_WEBZEPHYR_CLASS) && owl_message_is_loginout(m)) {
1821        /* Special case LOGIN/LOGOUT notifications on class "webzephyr" */
1822        class=OWL_WEBZEPHYR_CLASS;
1823        inst=owl_message_get_instance(m);
1824        to=OWL_WEBZEPHYR_PRINCIPAL;
1825      } else if (owl_message_is_loginout(m)) {
1826        /* Normal LOGIN/LOGOUT messages */
1827        class="MESSAGE";
1828        inst="PERSONAL";
1829        to=owl_message_get_sender(m);
1830      } else if (type==1) {
1831        /* Personal reply */
1832        class="MESSAGE";
1833        inst="PERSONAL";
1834        to=owl_message_get_sender(m);
1835      } else {
1836        /* General reply */
1837        class=owl_message_get_class(m);
1838        inst=owl_message_get_instance(m);
1839        to=owl_message_get_recipient(m);
1840        cc=owl_message_get_cc(m);
1841        if (!strcmp(to, "") || !strcmp(to, "*")) {
1842          to="";
1843        } else if (to[0]=='@') {
1844          /* leave it, to get the realm */
1845        } else {
1846          to=owl_message_get_sender(m);
1847        }
1848      }
1849       
1850      /* create the command line */
1851      if (!strcasecmp(owl_message_get_opcode(m), "CRYPT")) {
1852        buff=owl_strdup("zcrypt");
1853      } else {
1854        buff = owl_strdup("zwrite");
1855      }
1856      if (strcasecmp(class, "message")) {
1857        buff = owl_sprintf("%s -c %s%s%s", oldbuff=buff, owl_getquoting(class), class, owl_getquoting(class));
1858        owl_free(oldbuff);
1859      }
1860      if (strcasecmp(inst, "personal")) {
1861        buff = owl_sprintf("%s -i %s%s%s", oldbuff=buff, owl_getquoting(inst), inst, owl_getquoting(inst));
1862        owl_free(oldbuff);
1863      }
1864      if (*to != '\0') {
1865        char *tmp, *oldtmp, *tmp2;
1866        tmp=short_zuser(to);
1867        if (cc) {
1868          tmp = owl_util_uniq(oldtmp=tmp, cc, "-");
1869          owl_free(oldtmp);
1870          buff = owl_sprintf("%s -C %s", oldbuff=buff, tmp);
1871          owl_free(oldbuff);
1872        } else {
1873          if (owl_global_is_smartstrip(&g)) {
1874            tmp2=tmp;
1875            tmp=owl_util_smartstripped_user(tmp2);
1876            owl_free(tmp2);
1877          }
1878          buff = owl_sprintf("%s %s", oldbuff=buff, tmp);
1879          owl_free(oldbuff);
1880        }
1881        owl_free(tmp);
1882      }
1883      if (cc) owl_free(cc);
1884    }
1885
1886    /* aim */
1887    if (owl_message_is_type_aim(m)) {
1888      if (owl_message_is_direction_out(m)) {
1889        buff=owl_sprintf("aimwrite %s", owl_message_get_recipient(m));
1890      } else {
1891        buff=owl_sprintf("aimwrite %s", owl_message_get_sender(m));
1892      }
1893    }
1894   
1895    if (enter) {
1896      owl_history *hist = owl_global_get_cmd_history(&g);
1897      owl_history_store(hist, buff);
1898      owl_history_reset(hist);
1899      owl_function_command_norv(buff);
1900    } else {
1901      owl_function_start_command(buff);
1902    }
1903    owl_free(buff);
1904  }
1905}
1906
1907void owl_function_zlocate(int argc, char **argv, int auth)
1908{
1909  owl_fmtext fm;
1910  char *ptr, buff[LINE];
1911  int i;
1912
1913  owl_fmtext_init_null(&fm);
1914
1915  for (i=0; i<argc; i++) {
1916    ptr=long_zuser(argv[i]);
1917    owl_zephyr_zlocate(ptr, buff, auth);
1918    owl_fmtext_append_normal(&fm, buff);
1919    owl_free(ptr);
1920  }
1921
1922  owl_function_popless_fmtext(&fm);
1923  owl_fmtext_free(&fm);
1924}
1925
1926void owl_function_start_command(char *line)
1927{
1928  int i, j;
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, 
1934                        owl_global_get_cmd_history(&g));
1935
1936  owl_editwin_set_locktext(tw, "command: ");
1937  owl_global_set_needrefresh(&g);
1938
1939  j=strlen(line);
1940  for (i=0; i<j; i++) {
1941    owl_editwin_process_char(tw, line[i]);
1942  }
1943  owl_editwin_redisplay(tw, 0);
1944
1945  owl_context_set_editline(owl_global_get_context(&g), tw);
1946  owl_function_activate_keymap("editline");
1947   
1948}
1949
1950void owl_function_start_question(char *line)
1951{
1952  owl_editwin *tw;
1953
1954  tw=owl_global_get_typwin(&g);
1955  owl_global_set_typwin_active(&g);
1956  owl_editwin_new_style(tw, OWL_EDITWIN_STYLE_ONELINE, owl_global_get_cmd_history(&g));
1957
1958  owl_editwin_set_locktext(tw, line);
1959  owl_global_set_needrefresh(&g);
1960
1961  owl_editwin_redisplay(tw, 0);
1962
1963  owl_context_set_editresponse(owl_global_get_context(&g), tw);
1964  owl_function_activate_keymap("editresponse");
1965}
1966
1967void owl_function_start_password(char *line)
1968{
1969  owl_editwin *tw;
1970
1971  tw=owl_global_get_typwin(&g);
1972  owl_global_set_typwin_active(&g);
1973  owl_editwin_new_style(tw, OWL_EDITWIN_STYLE_ONELINE, owl_global_get_cmd_history(&g));
1974  owl_editwin_set_echochar(tw, '*');
1975
1976  owl_editwin_set_locktext(tw, line);
1977  owl_global_set_needrefresh(&g);
1978
1979  owl_editwin_redisplay(tw, 0);
1980
1981  owl_context_set_editresponse(owl_global_get_context(&g), tw);
1982  owl_function_activate_keymap("editresponse");
1983}
1984
1985char *owl_function_exec(int argc, char **argv, char *buff, int type)
1986{
1987  /* if type == 1 display in a popup
1988   * if type == 2 display an admin messages
1989   * if type == 0 return output
1990   * else display in a popup
1991   */
1992  char *newbuff, *redirect = " 2>&1 < /dev/null";
1993  char *out, buff2[1024];
1994  int size;
1995  FILE *p;
1996
1997#if OWL_STDERR_REDIR
1998  redirect = " < /dev/null";
1999#endif
2000
2001  if (argc<2) {
2002    owl_function_error("Wrong number of arguments to the exec command");
2003    return NULL;
2004  }
2005
2006  buff = skiptokens(buff, 1);
2007  newbuff = owl_malloc(strlen(buff)+strlen(redirect)+1);
2008  strcpy(newbuff, buff);
2009  strcat(newbuff, redirect);
2010
2011  p=popen(newbuff, "r");
2012  out=owl_malloc(1024);
2013  size=1024;
2014  strcpy(out, "");
2015  while (fgets(buff2, 1024, p)!=NULL) {
2016    size+=1024;
2017    out=owl_realloc(out, size);
2018    strcat(out, buff2);
2019  }
2020  pclose(p);
2021
2022  if (type==1) {
2023    owl_function_popless_text(out);
2024  } else if (type==0) {
2025    return out;
2026  } else if (type==2) {
2027    owl_function_adminmsg(buff, out);
2028  } else {
2029    owl_function_popless_text(out);
2030  }
2031  owl_free(out);
2032  return NULL;
2033}
2034
2035char *owl_function_perl(int argc, char **argv, char *buff, int type)
2036{
2037  /* if type == 1 display in a popup
2038   * if type == 2 display an admin messages
2039   * if type == 0 return output
2040   * else display in a popup
2041   */
2042  char *perlout;
2043
2044  if (argc<2) {
2045    owl_function_error("Wrong number of arguments to perl command");
2046    return NULL;
2047  }
2048
2049  /* consume first token (argv[0]) */
2050  buff = skiptokens(buff, 1);
2051
2052  perlout = owl_perlconfig_execute(buff);
2053  if (perlout) { 
2054    if (type==1) {
2055      owl_function_popless_text(perlout);
2056    } else if (type==2) {
2057      owl_function_adminmsg(buff, perlout);
2058    } else if (type==0) {
2059      return perlout;
2060    } else {
2061      owl_function_popless_text(perlout);
2062    }
2063    owl_free(perlout);
2064  }
2065  return NULL;
2066}
2067
2068#if 0
2069void owl_function_change_view_old(char *filtname)
2070{
2071  owl_view *v;
2072  owl_filter *f;
2073  int curid=-1, newpos, curmsg;
2074  owl_message *curm=NULL;
2075
2076  v=owl_global_get_current_view(&g);
2077  curmsg=owl_global_get_curmsg(&g);
2078  if (curmsg==-1) {
2079    owl_function_debugmsg("Hit the curmsg==-1 case in change_view");
2080  } else {
2081    curm=owl_view_get_element(v, curmsg);
2082    if (curm) {
2083      curid=owl_message_get_id(curm);
2084      owl_view_save_curmsgid(v, curid);
2085    }
2086  }
2087
2088  /* grab the filter */;
2089  f=owl_global_get_filter(&g, filtname);
2090  if (!f) {
2091    owl_function_error("Unknown filter %s", filtname);
2092    return;
2093  }
2094
2095  /* free the existing view and create a new one based on the filter */
2096  owl_view_free(v);
2097  owl_view_create(v, f);
2098
2099  /* Figure out what to set the current message to.
2100   * - If the view we're leaving has messages in it, go to the closest message
2101   *   to the last message pointed to in that view.
2102   * - If the view we're leaving is empty, try to restore the position
2103   *   from the last time we were in the new view.  */
2104  if (curm) {
2105    newpos = owl_view_get_nearest_to_msgid(v, curid);
2106  } else {
2107    newpos = owl_view_get_nearest_to_saved(v);
2108  }
2109
2110  owl_global_set_curmsg(&g, newpos);
2111
2112  owl_function_calculate_topmsg(OWL_DIRECTION_DOWNWARDS);
2113  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2114  owl_global_set_direction_downwards(&g);
2115}
2116#endif
2117
2118void owl_function_change_view(char *filtname)
2119{
2120  owl_view *v;
2121  owl_filter *f;
2122  int curid=-1, newpos, curmsg;
2123  owl_message *curm=NULL;
2124
2125  v=owl_global_get_current_view(&g);
2126
2127  curmsg=owl_global_get_curmsg(&g);
2128  if (curmsg==-1) {
2129    owl_function_debugmsg("Hit the curmsg==-1 case in change_view");
2130  } else {
2131    curm=owl_view_get_element(v, curmsg);
2132    if (curm) {
2133      curid=owl_message_get_id(curm);
2134      owl_view_save_curmsgid(v, curid);
2135    }
2136  }
2137
2138  f=owl_global_get_filter(&g, filtname);
2139  if (!f) {
2140    owl_function_error("Unknown filter %s", filtname);
2141    return;
2142  }
2143
2144  owl_view_new_filter(v, f);
2145
2146  /* Figure out what to set the current message to.
2147   * - If the view we're leaving has messages in it, go to the closest message
2148   *   to the last message pointed to in that view.
2149   * - If the view we're leaving is empty, try to restore the position
2150   *   from the last time we were in the new view.  */
2151  if (curm) {
2152    newpos = owl_view_get_nearest_to_msgid(v, curid);
2153  } else {
2154    newpos = owl_view_get_nearest_to_saved(v);
2155  }
2156
2157  owl_global_set_curmsg(&g, newpos);
2158  owl_function_calculate_topmsg(OWL_DIRECTION_DOWNWARDS);
2159  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2160  owl_global_set_direction_downwards(&g);
2161}
2162
2163void owl_function_create_filter(int argc, char **argv)
2164{
2165  owl_filter *f;
2166  owl_view *v;
2167  int ret, inuse=0;
2168
2169  if (argc < 2) {
2170    owl_function_error("Wrong number of arguments to filter command");
2171    return;
2172  }
2173
2174  v=owl_global_get_current_view(&g);
2175
2176  /* don't touch the all filter */
2177  if (!strcmp(argv[1], "all")) {
2178    owl_function_error("You may not change the 'all' filter.");
2179    return;
2180  }
2181
2182  /* deal with the case of trying change the filter color */
2183  if (argc==4 && !strcmp(argv[2], "-c")) {
2184    f=owl_global_get_filter(&g, argv[1]);
2185    if (!f) {
2186      owl_function_error("The filter '%s' does not exist.", argv[1]);
2187      return;
2188    }
2189    owl_filter_set_color(f, owl_util_string_to_color(argv[3]));
2190    owl_global_set_needrefresh(&g);
2191    owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2192    return;
2193  }
2194
2195  /* create the filter and check for errors */
2196  f=owl_malloc(sizeof(owl_filter));
2197  ret=owl_filter_init(f, argv[1], argc-2, argv+2);
2198  if (ret==-1) {
2199    owl_free(f);
2200    owl_function_error("Invalid filter syntax");
2201    return;
2202  }
2203
2204  /* if the named filter is in use by the current view, remember it */
2205  if (!strcmp(owl_view_get_filtname(v), argv[1])) {
2206    inuse=1;
2207  }
2208
2209  /* if the named filter already exists, nuke it */
2210  if (owl_global_get_filter(&g, argv[1])) {
2211    owl_global_remove_filter(&g, argv[1]);
2212  }
2213
2214  /* add the filter */
2215  owl_global_add_filter(&g, f);
2216
2217  /* if it was in use by the current view then update */
2218  if (inuse) {
2219    owl_function_change_view(argv[1]);
2220  }
2221  owl_global_set_needrefresh(&g);
2222  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2223}
2224
2225void owl_function_show_filters()
2226{
2227  owl_list *l;
2228  owl_filter *f;
2229  int i, j;
2230  owl_fmtext fm;
2231
2232  owl_fmtext_init_null(&fm);
2233
2234  l=owl_global_get_filterlist(&g);
2235  j=owl_list_get_size(l);
2236
2237  owl_fmtext_append_bold(&fm, "Filters:\n");
2238
2239  for (i=0; i<j; i++) {
2240    f=owl_list_get_element(l, i);
2241    owl_fmtext_append_normal(&fm, "   ");
2242    if (owl_global_get_hascolors(&g)) {
2243      owl_fmtext_append_normal_color(&fm, owl_filter_get_name(f), owl_filter_get_color(f));
2244    } else {
2245      owl_fmtext_append_normal(&fm, owl_filter_get_name(f));
2246    }
2247    owl_fmtext_append_normal(&fm, "\n");
2248  }
2249  owl_function_popless_fmtext(&fm);
2250  owl_fmtext_free(&fm);
2251}
2252
2253void owl_function_show_filter(char *name)
2254{
2255  owl_filter *f;
2256  char buff[5000];
2257
2258  f=owl_global_get_filter(&g, name);
2259  if (!f) {
2260    owl_function_error("There is no filter named %s", name);
2261    return;
2262  }
2263  owl_filter_print(f, buff);
2264  owl_function_popless_text(buff);
2265}
2266
2267void owl_function_show_zpunts()
2268{
2269  owl_filter *f;
2270  owl_list *fl;
2271  char buff[5000];
2272  owl_fmtext fm;
2273  int i, j;
2274
2275  owl_fmtext_init_null(&fm);
2276
2277  fl=owl_global_get_puntlist(&g);
2278  j=owl_list_get_size(fl);
2279  owl_fmtext_append_bold(&fm, "Active zpunt filters:\n");
2280
2281  for (i=0; i<j; i++) {
2282    f=owl_list_get_element(fl, i);
2283    owl_filter_print(f, buff);
2284    owl_fmtext_append_normal(&fm, buff);
2285  }
2286  owl_function_popless_fmtext(&fm);
2287  owl_fmtext_free(&fm);
2288}
2289
2290/* Create a filter for a class, instance if one doesn't exist.  If
2291 * instance is NULL then catch all messgaes in the class.  Returns the
2292 * name of the filter, which the caller must free.
2293 */
2294char *owl_function_classinstfilt(char *class, char *instance) 
2295{
2296  owl_list *fl;
2297  owl_filter *f;
2298  char *argbuff, *filtname;
2299  char *tmpclass, *tmpinstance = NULL;
2300  int len;
2301
2302  fl=owl_global_get_filterlist(&g);
2303
2304  /* name for the filter */
2305  len=strlen(class)+30;
2306  if (instance) len+=strlen(instance);
2307  filtname=owl_malloc(len);
2308  if (!instance) {
2309    sprintf(filtname, "class-%s", class);
2310  } else {
2311    sprintf(filtname, "class-%s-instance-%s", class, instance);
2312  }
2313  /* downcase it */
2314  downstr(filtname);
2315  /* turn spaces into hyphens */
2316  owl_util_tr(filtname, ' ', '.');
2317 
2318  /* if it already exists then go with it.  This lets users override */
2319  if (owl_global_get_filter(&g, filtname)) {
2320    return(filtname);
2321  }
2322
2323  /* create the new filter */
2324  argbuff=owl_malloc(len+20);
2325  tmpclass=owl_strdup(class);
2326  owl_util_tr(tmpclass, ' ', '.');
2327  if (instance) {
2328    tmpinstance=owl_strdup(instance);
2329    owl_util_tr(tmpinstance, ' ', '.');
2330  }
2331  sprintf(argbuff, "( class ^%s$ )", tmpclass);
2332  if (tmpinstance) {
2333    sprintf(argbuff, "%s and ( instance ^%s$ )", argbuff, tmpinstance);
2334  }
2335  owl_free(tmpclass);
2336  if (tmpinstance) owl_free(tmpinstance);
2337
2338  f=owl_malloc(sizeof(owl_filter));
2339  owl_filter_init_fromstring(f, filtname, argbuff);
2340
2341  /* add it to the global list */
2342  owl_global_add_filter(&g, f);
2343
2344  owl_free(argbuff);
2345  return(filtname);
2346}
2347
2348/* Create a filter for personal zephyrs to or from the specified
2349 * zephyr user.  Includes login/logout notifications for the user.
2350 * The name of the filter will be 'user-<user>'.  If a filter already
2351 * exists with this name, no new filter will be created.  This allows
2352 * the configuration to override this function.  Returns the name of
2353 * the filter, which the caller must free.
2354 */
2355char *owl_function_zuserfilt(char *user)
2356{
2357  owl_filter *f;
2358  char *argbuff, *longuser, *shortuser, *filtname;
2359
2360  /* stick the local realm on if it's not there */
2361  longuser=long_zuser(user);
2362  shortuser=short_zuser(user);
2363
2364  /* name for the filter */
2365  filtname=owl_malloc(strlen(shortuser)+20);
2366  sprintf(filtname, "user-%s", shortuser);
2367
2368  /* if it already exists then go with it.  This lets users override */
2369  if (owl_global_get_filter(&g, filtname)) {
2370    return(owl_strdup(filtname));
2371  }
2372
2373  /* create the new-internal filter */
2374  f=owl_malloc(sizeof(owl_filter));
2375
2376  argbuff=owl_malloc(strlen(longuser)+1000);
2377  sprintf(argbuff, "( type ^zephyr$ and ( class ^message$ and instance ^personal$ and ");
2378  sprintf(argbuff, "%s ( ( direction ^in$ and sender ^%s$ ) or ( direction ^out$ and recipient ^%s$ ) ) )", argbuff, longuser, longuser);
2379  sprintf(argbuff, "%s or ( ( class ^login$ ) and ( sender ^%s$ ) ) )", argbuff, longuser);
2380
2381  owl_filter_init_fromstring(f, filtname, argbuff);
2382
2383  /* add it to the global list */
2384  owl_global_add_filter(&g, f);
2385
2386  /* free stuff */
2387  owl_free(argbuff);
2388  owl_free(longuser);
2389  owl_free(shortuser);
2390
2391  return(filtname);
2392}
2393
2394/* Create a filter for AIM IM messages to or from the specified
2395 * screenname.  The name of the filter will be 'aimuser-<user>'.  If a
2396 * filter already exists with this name, no new filter will be
2397 * created.  This allows the configuration to override this function.
2398 * Returns the name of the filter, which the caller must free.
2399 */
2400char *owl_function_aimuserfilt(char *user)
2401{
2402  owl_filter *f;
2403  char *argbuff, *filtname;
2404
2405  /* name for the filter */
2406  filtname=owl_malloc(strlen(user)+40);
2407  sprintf(filtname, "aimuser-%s", user);
2408
2409  /* if it already exists then go with it.  This lets users override */
2410  if (owl_global_get_filter(&g, filtname)) {
2411    return(owl_strdup(filtname));
2412  }
2413
2414  /* create the new-internal filter */
2415  f=owl_malloc(sizeof(owl_filter));
2416
2417  argbuff=owl_malloc(1000);
2418  sprintf(argbuff,
2419          "( type ^aim$ and ( ( sender ^%s$ and recipient ^%s$ ) or ( sender ^%s$ and recipient ^%s$ ) ) )",
2420          user, owl_global_get_aim_screenname(&g), owl_global_get_aim_screenname(&g), user);
2421
2422  owl_filter_init_fromstring(f, filtname, argbuff);
2423
2424  /* add it to the global list */
2425  owl_global_add_filter(&g, f);
2426
2427  /* free stuff */
2428  owl_free(argbuff);
2429
2430  return(filtname);
2431}
2432
2433char *owl_function_typefilt(char *type)
2434{
2435  owl_filter *f;
2436  char *argbuff, *filtname;
2437
2438  /* name for the filter */
2439  filtname=owl_sprintf("type-%s", type);
2440
2441  /* if it already exists then go with it.  This lets users override */
2442  if (owl_global_get_filter(&g, filtname)) {
2443    return filtname;
2444  }
2445
2446  /* create the new-internal filter */
2447  f=owl_malloc(sizeof(owl_filter));
2448
2449  argbuff = owl_sprintf("type ^%s$", type);
2450
2451  owl_filter_init_fromstring(f, filtname, argbuff);
2452
2453  /* add it to the global list */
2454  owl_global_add_filter(&g, f);
2455
2456  /* free stuff */
2457  owl_free(argbuff);
2458
2459  return filtname;
2460}
2461
2462/* If flag is 1, marks for deletion.  If flag is 0,
2463 * unmarks for deletion. */
2464void owl_function_delete_curview_msgs(int flag)
2465{
2466  owl_view *v;
2467  int i, j;
2468
2469  v=owl_global_get_current_view(&g);
2470  j=owl_view_get_size(v);
2471  for (i=0; i<j; i++) {
2472    if (flag == 1) {
2473      owl_message_mark_delete(owl_view_get_element(v, i));
2474    } else if (flag == 0) {
2475      owl_message_unmark_delete(owl_view_get_element(v, i));
2476    }
2477  }
2478
2479  owl_function_makemsg("%i messages marked for %sdeletion", j, flag?"":"un");
2480
2481  owl_mainwin_redisplay(owl_global_get_mainwin(&g)); 
2482}
2483
2484/* Create a filter based on the current message.  Returns the name of
2485 * a filter or null.  The caller must free this name.
2486 *
2487 * if the curmsg is a personal zephyr return a filter name
2488 *    to the zephyr converstaion with that user.
2489 * If the curmsg is a zephyr class message, instance foo, recip *,
2490 *    return a filter name to the class, inst.
2491 * If the curmsg is a zephyr class message and type==0 then
2492 *    return a filter name for just the class.
2493 * If the curmsg is a zephyr class message and type==1 then
2494 *    return a filter name for the class and instance.
2495 * If the curmsg is a personal AIM message returna  filter
2496 *    name to the AIM conversation with that user
2497 */
2498char *owl_function_smartfilter(int type)
2499{
2500  owl_view *v;
2501  owl_message *m;
2502  char *zperson, *filtname=NULL;
2503 
2504  v=owl_global_get_current_view(&g);
2505  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
2506
2507  if (!m || owl_view_get_size(v)==0) {
2508    owl_function_error("No message selected\n");
2509    return(NULL);
2510  }
2511
2512  /* very simple handling of admin messages for now */
2513  if (owl_message_is_type_admin(m)) {
2514    return(owl_function_typefilt("admin"));
2515  }
2516
2517  /* aim messages */
2518  if (owl_message_is_type_aim(m)) {
2519    if (owl_message_is_direction_in(m)) {
2520      filtname=owl_function_aimuserfilt(owl_message_get_sender(m));
2521    } else if (owl_message_is_direction_out(m)) {
2522      filtname=owl_function_aimuserfilt(owl_message_get_recipient(m));
2523    }
2524    return(filtname);
2525  }
2526
2527  /* narrow personal and login messages to the sender or recip as appropriate */
2528  if (owl_message_is_personal(m) || owl_message_is_loginout(m)) {
2529    if (owl_message_is_type_zephyr(m)) {
2530      if (owl_message_is_direction_in(m)) {
2531        zperson=short_zuser(owl_message_get_sender(m));
2532      } else {
2533        zperson=short_zuser(owl_message_get_recipient(m));
2534      }
2535      filtname=owl_function_zuserfilt(zperson);
2536      owl_free(zperson);
2537      return(filtname);
2538    }
2539    return(NULL);
2540  }
2541
2542  /* narrow class MESSAGE, instance foo, recip * messages to class, inst */
2543  if (!strcasecmp(owl_message_get_class(m), "message") && !owl_message_is_personal(m)) {
2544    filtname=owl_function_classinstfilt(owl_message_get_class(m), owl_message_get_instance(m));
2545    return(filtname);
2546  }
2547
2548  /* otherwise narrow to the class */
2549  if (type==0) {
2550    filtname=owl_function_classinstfilt(owl_message_get_class(m), NULL);
2551  } else if (type==1) {
2552    filtname=owl_function_classinstfilt(owl_message_get_class(m), owl_message_get_instance(m));
2553  }
2554  return(filtname);
2555}
2556
2557void owl_function_smartzpunt(int type)
2558{
2559  /* Starts a zpunt command based on the current class,instance pair.
2560   * If type=0, uses just class.  If type=1, uses instance as well. */
2561  owl_view *v;
2562  owl_message *m;
2563  char *cmd, *cmdprefix, *mclass, *minst;
2564 
2565  v=owl_global_get_current_view(&g);
2566  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
2567
2568  if (!m || owl_view_get_size(v)==0) {
2569    owl_function_error("No message selected\n");
2570    return;
2571  }
2572
2573  /* for now we skip admin messages. */
2574  if (owl_message_is_type_admin(m)
2575      || owl_message_is_loginout(m)
2576      || !owl_message_is_type_zephyr(m)) {
2577    owl_function_error("smartzpunt doesn't support this message type.");
2578    return;
2579  }
2580
2581  mclass = owl_message_get_class(m);
2582  minst = owl_message_get_instance(m);
2583  if (!mclass || !*mclass || *mclass==' '
2584      || (!strcasecmp(mclass, "message") && !strcasecmp(minst, "personal"))
2585      || (type && (!minst || !*minst|| *minst==' '))) {
2586    owl_function_error("smartzpunt can't safely do this for <%s,%s>",
2587                         mclass, minst);
2588  } else {
2589    cmdprefix = "start-command zpunt ";
2590    cmd = owl_malloc(strlen(cmdprefix)+strlen(mclass)+strlen(minst)+3);
2591    strcpy(cmd, cmdprefix);
2592    strcat(cmd, mclass);
2593    if (type) {
2594      strcat(cmd, " ");
2595      strcat(cmd, minst);
2596    } else {
2597      strcat(cmd, " *");
2598    }
2599    owl_function_command(cmd);
2600    owl_free(cmd);
2601  }
2602}
2603
2604void owl_function_color_current_filter(char *color)
2605{
2606  owl_filter *f;
2607  char *name;
2608
2609  name=owl_view_get_filtname(owl_global_get_current_view(&g));
2610  f=owl_global_get_filter(&g, name);
2611  if (!f) {
2612    owl_function_error("Unknown filter");
2613    return;
2614  }
2615
2616  /* don't touch the all filter */
2617  if (!strcmp(name, "all")) {
2618    owl_function_error("You may not change the 'all' filter.");
2619    return;
2620  }
2621
2622  /* deal with the case of trying change the filter color */
2623  owl_filter_set_color(f, owl_util_string_to_color(color));
2624  owl_global_set_needrefresh(&g);
2625  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2626}
2627
2628void owl_function_show_colors()
2629{
2630  owl_fmtext fm;
2631
2632  owl_fmtext_init_null(&fm);
2633  owl_fmtext_append_normal_color(&fm, "default\n", OWL_COLOR_DEFAULT);
2634  owl_fmtext_append_normal_color(&fm, "red\n", OWL_COLOR_RED);
2635  owl_fmtext_append_normal_color(&fm, "green\n", OWL_COLOR_GREEN);
2636  owl_fmtext_append_normal_color(&fm, "yellow\n", OWL_COLOR_YELLOW);
2637  owl_fmtext_append_normal_color(&fm, "blue\n", OWL_COLOR_BLUE);
2638  owl_fmtext_append_normal_color(&fm, "magenta\n", OWL_COLOR_MAGENTA);
2639  owl_fmtext_append_normal_color(&fm, "cyan\n", OWL_COLOR_CYAN);
2640  owl_fmtext_append_normal_color(&fm, "white\n", OWL_COLOR_WHITE);
2641
2642  owl_function_popless_fmtext(&fm);
2643  owl_fmtext_free(&fm);
2644}
2645
2646/* add the given class, inst, recip to the punt list for filtering.
2647 *   if direction==0 then punt
2648 *   if direction==1 then unpunt
2649 */
2650void owl_function_zpunt(char *class, char *inst, char *recip, int direction)
2651{
2652  owl_filter *f;
2653  owl_list *fl;
2654  char *buff;
2655  int ret, i, j;
2656
2657  fl=owl_global_get_puntlist(&g);
2658
2659  /* first, create the filter */
2660  f=malloc(sizeof(owl_filter));
2661  buff=malloc(strlen(class)+strlen(inst)+strlen(recip)+100);
2662  strcpy(buff, "class");
2663  if (!strcmp(class, "*")) {
2664    strcat(buff, " .*");
2665  } else {
2666    sprintf(buff, "%s ^%s$", buff, class);
2667  }
2668  if (!strcmp(inst, "*")) {
2669    strcat(buff, " and instance .*");
2670  } else {
2671    sprintf(buff, "%s and instance ^%s$", buff, inst);
2672  }
2673  if (strcmp(recip, "*")) {
2674    sprintf(buff, "%s and recipient ^%s$", buff, recip);
2675  }
2676 
2677  owl_function_debugmsg("About to filter %s", buff);
2678  ret=owl_filter_init_fromstring(f, "punt-filter", buff);
2679  owl_free(buff);
2680  if (ret) {
2681    owl_function_error("Error creating filter for zpunt");
2682    owl_filter_free(f);
2683    return;
2684  }
2685
2686  /* Check for an identical filter */
2687  j=owl_list_get_size(fl);
2688  for (i=0; i<j; i++) {
2689    if (owl_filter_equiv(f, owl_list_get_element(fl, i))) {
2690      /* if we're punting, then just silently bow out on this duplicate */
2691      if (direction==0) {
2692        owl_filter_free(f);
2693        return;
2694      }
2695
2696      /* if we're unpunting, then remove this filter from the puntlist */
2697      if (direction==1) {
2698        owl_filter_free(owl_list_get_element(fl, i));
2699        owl_list_remove_element(fl, i);
2700        return;
2701      }
2702    }
2703  }
2704
2705  /* If we're punting, add the filter to the global punt list */
2706  if (direction==0) {
2707    owl_list_append_element(fl, f);
2708  }
2709}
2710
2711void owl_function_activate_keymap(char *keymap)
2712{
2713  if (!owl_keyhandler_activate(owl_global_get_keyhandler(&g), keymap)) {
2714    owl_function_error("Unable to activate keymap '%s'", keymap);
2715  }
2716}
2717
2718void owl_function_show_keymaps()
2719{
2720  owl_list l;
2721  owl_fmtext fm;
2722  owl_keymap *km;
2723  owl_keyhandler *kh;
2724  int i, numkm;
2725  char *kmname;
2726
2727  kh = owl_global_get_keyhandler(&g);
2728  owl_fmtext_init_null(&fm);
2729  owl_fmtext_append_bold(&fm, "Keymaps:   ");
2730  owl_fmtext_append_normal(&fm, "(use 'show keymap <name>' for details)\n");
2731  owl_keyhandler_get_keymap_names(kh, &l);
2732  owl_fmtext_append_list(&fm, &l, "\n", owl_function_keymap_summary);
2733  owl_fmtext_append_normal(&fm, "\n");
2734
2735  numkm = owl_list_get_size(&l);
2736  for (i=0; i<numkm; i++) {
2737    kmname = owl_list_get_element(&l, i);
2738    km = owl_keyhandler_get_keymap(kh, kmname);
2739    owl_fmtext_append_bold(&fm, "\n\n----------------------------------------------------------------------------------------------------\n\n");
2740    owl_keymap_get_details(km, &fm);   
2741  }
2742  owl_fmtext_append_normal(&fm, "\n");
2743 
2744  owl_function_popless_fmtext(&fm);
2745  owl_keyhandler_keymap_namelist_free(&l);
2746  owl_fmtext_free(&fm);
2747}
2748
2749char *owl_function_keymap_summary(void *name)
2750{
2751  owl_keymap *km
2752    = owl_keyhandler_get_keymap(owl_global_get_keyhandler(&g), name);
2753  if (km) return owl_keymap_summary(km);
2754  else return(NULL);
2755}
2756
2757/* TODO: implement for real */
2758void owl_function_show_keymap(char *name)
2759{
2760  owl_fmtext fm;
2761  owl_keymap *km;
2762
2763  owl_fmtext_init_null(&fm);
2764  km = owl_keyhandler_get_keymap(owl_global_get_keyhandler(&g), name);
2765  if (km) {
2766    owl_keymap_get_details(km, &fm);
2767  } else {
2768    owl_fmtext_append_normal(&fm, "No such keymap...\n");
2769  } 
2770  owl_function_popless_fmtext(&fm);
2771  owl_fmtext_free(&fm);
2772}
2773
2774void owl_function_help_for_command(char *cmdname)
2775{
2776  owl_fmtext fm;
2777
2778  owl_fmtext_init_null(&fm);
2779  owl_cmd_get_help(owl_global_get_cmddict(&g), cmdname, &fm);
2780  owl_function_popless_fmtext(&fm); 
2781  owl_fmtext_free(&fm);
2782}
2783
2784void owl_function_search_start(char *string, int direction)
2785{
2786  /* direction is OWL_DIRECTION_DOWNWARDS or OWL_DIRECTION_UPWARDS */
2787  owl_global_set_search_active(&g, string);
2788  owl_function_search_helper(0, direction);
2789}
2790
2791void owl_function_search_continue(int direction)
2792{
2793  /* direction is OWL_DIRECTION_DOWNWARDS or OWL_DIRECTION_UPWARDS */
2794  owl_function_search_helper(1, direction);
2795}
2796
2797void owl_function_search_helper(int mode, int direction)
2798{
2799  /* move to a message that contains the string.  If direction is
2800   * OWL_DIRECTION_DOWNWARDS then search fowards, if direction is
2801   * OWL_DIRECTION_UPWARDS then search backwards.
2802   *
2803   * If mode==0 then it will stay on the current message if it
2804   * contains the string.
2805   */
2806
2807  owl_view *v;
2808  int viewsize, i, curmsg, start;
2809  owl_message *m;
2810
2811  v=owl_global_get_current_view(&g);
2812  viewsize=owl_view_get_size(v);
2813  curmsg=owl_global_get_curmsg(&g);
2814 
2815  if (viewsize==0) {
2816    owl_function_error("No messages present");
2817    return;
2818  }
2819
2820  if (mode==0) {
2821    start=curmsg;
2822  } else if (direction==OWL_DIRECTION_DOWNWARDS) {
2823    start=curmsg+1;
2824  } else {
2825    start=curmsg-1;
2826  }
2827
2828  /* bounds check */
2829  if (start>=viewsize || start<0) {
2830    owl_function_error("No further matches found");
2831    return;
2832  }
2833
2834  for (i=start; i<viewsize && i>=0;) {
2835    m=owl_view_get_element(v, i);
2836    if (owl_message_search(m, owl_global_get_search_string(&g))) {
2837      owl_global_set_curmsg(&g, i);
2838      owl_function_calculate_topmsg(direction);
2839      owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2840      if (direction==OWL_DIRECTION_DOWNWARDS) {
2841        owl_global_set_direction_downwards(&g);
2842      } else {
2843        owl_global_set_direction_upwards(&g);
2844      }
2845      return;
2846    }
2847    if (direction==OWL_DIRECTION_DOWNWARDS) {
2848      i++;
2849    } else {
2850      i--;
2851    }
2852  }
2853  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2854  owl_function_error("No matches found");
2855}
2856
2857/* strips formatting from ztext and returns the unformatted text.
2858 * caller is responsible for freeing. */
2859char *owl_function_ztext_stylestrip(char *zt)
2860{
2861  owl_fmtext fm;
2862  char *plaintext;
2863
2864  owl_fmtext_init_null(&fm);
2865  owl_fmtext_append_ztext(&fm, zt);
2866  plaintext = owl_fmtext_print_plain(&fm);
2867  owl_fmtext_free(&fm);
2868  return(plaintext);
2869}
2870
2871/* Popup a buddylisting.  If file is NULL use the default .anyone */
2872void owl_function_buddylist(int aim, int zephyr, char *file)
2873{
2874  int i, j, idle;
2875  owl_fmtext fm;
2876  owl_buddylist *bl;
2877  owl_buddy *b;
2878  char *foo, *timestr;
2879#ifdef HAVE_LIBZEPHYR
2880  char *ourfile, *tmp, buff[LINE], *line;
2881  ZLocations_t location[200];
2882  FILE *f;
2883  int numlocs, ret;
2884#endif
2885
2886  owl_fmtext_init_null(&fm);
2887
2888  if (aim && owl_global_is_aimloggedin(&g)) {
2889    bl=owl_global_get_buddylist(&g);
2890
2891    owl_fmtext_append_bold(&fm, "AIM users logged in:\n");
2892    /* we're assuming AIM for now */
2893    j=owl_buddylist_get_size(bl);
2894    for (i=0; i<j; i++) {
2895      b=owl_buddylist_get_buddy_n(bl, i);
2896      idle=owl_buddy_get_idle_time(b);
2897      if (idle!=0) {
2898        timestr=owl_util_minutes_to_timestr(idle);
2899      } else {
2900        timestr=owl_strdup("");
2901      }
2902      foo=owl_sprintf("  %-20.20s %-12.12s\n",
2903                      owl_buddy_get_name(b),
2904                      timestr);
2905      owl_fmtext_append_normal(&fm, foo);
2906      owl_free(timestr);
2907      owl_free(foo);
2908    }
2909  }
2910
2911#ifdef HAVE_LIBZEPHYR
2912  if (zephyr) {
2913    if (file==NULL) {
2914      tmp=owl_global_get_homedir(&g);
2915      if (!tmp) {
2916        owl_function_error("Could not determine home directory");
2917        return;
2918      }
2919      ourfile=owl_malloc(strlen(tmp)+50);
2920      sprintf(ourfile, "%s/.anyone", owl_global_get_homedir(&g));
2921    } else {
2922      ourfile=owl_strdup(file);
2923    }
2924   
2925    f=fopen(ourfile, "r");
2926    if (!f) {
2927      owl_function_error("Error opening file %s: %s",
2928                           ourfile,
2929                           strerror(errno) ? strerror(errno) : "");
2930      return;
2931    }
2932
2933    owl_fmtext_append_bold(&fm, "Zephyr users logged in:\n");
2934   
2935    while (fgets(buff, LINE, f)!=NULL) {
2936      /* ignore comments, blank lines etc. */
2937      if (buff[0]=='#') continue;
2938      if (buff[0]=='\n') continue;
2939      if (buff[0]=='\0') continue;
2940     
2941      /* strip the \n */
2942      buff[strlen(buff)-1]='\0';
2943     
2944      /* ingore from # on */
2945      tmp=strchr(buff, '#');
2946      if (tmp) tmp[0]='\0';
2947     
2948      /* ingore from SPC */
2949      tmp=strchr(buff, ' ');
2950      if (tmp) tmp[0]='\0';
2951     
2952      /* stick on the local realm. */
2953      if (!strchr(buff, '@')) {
2954        strcat(buff, "@");
2955        strcat(buff, ZGetRealm());
2956      }
2957     
2958      ret=ZLocateUser(buff, &numlocs, ZAUTH);
2959      if (ret!=ZERR_NONE) {
2960        owl_function_error("Error getting location for %s", buff);
2961        continue;
2962      }
2963     
2964      numlocs=200;
2965      ret=ZGetLocations(location, &numlocs);
2966      if (ret==0) {
2967        for (i=0; i<numlocs; i++) {
2968          line=malloc(strlen(location[i].host)+strlen(location[i].time)+strlen(location[i].tty)+100);
2969          tmp=short_zuser(buff);
2970          sprintf(line, "  %-10.10s %-24.24s %-12.12s  %20.20s\n",
2971                  tmp,
2972                  location[i].host,
2973                  location[i].tty,
2974                  location[i].time);
2975          owl_fmtext_append_normal(&fm, line);
2976          owl_free(tmp);
2977        }
2978        if (numlocs>=200) {
2979          owl_fmtext_append_normal(&fm, "  Too many locations found for this user, truncating.\n");
2980        }
2981      }
2982    }
2983    fclose(f);
2984    owl_free(ourfile);
2985  }
2986#endif
2987 
2988  owl_function_popless_fmtext(&fm);
2989  owl_fmtext_free(&fm);
2990}
2991
2992void owl_function_dump(char *filename) 
2993{
2994  int i, j, count;
2995  owl_message *m;
2996  owl_view *v;
2997  FILE *file;
2998  /* struct stat sbuf; */
2999
3000  v=owl_global_get_current_view(&g);
3001
3002  /* in the future make it ask yes/no */
3003  /*
3004  ret=stat(filename, &sbuf);
3005  if (!ret) {
3006    ret=owl_function_askyesno("File exists, continue? [Y/n]");
3007    if (!ret) return;
3008  }
3009  */
3010
3011  file=fopen(filename, "w");
3012  if (!file) {
3013    owl_function_error("Error opening file");
3014    return;
3015  }
3016
3017  count=0;
3018  j=owl_view_get_size(v);
3019  for (i=0; i<j; i++) {
3020    m=owl_view_get_element(v, i);
3021    fputs(owl_message_get_text(m), file);
3022  }
3023  fclose(file);
3024}
3025
3026void owl_function_do_newmsgproc(void)
3027{
3028  if (owl_global_get_newmsgproc(&g) && strcmp(owl_global_get_newmsgproc(&g), "")) {
3029    /* if there's a process out there, we need to check on it */
3030    if (owl_global_get_newmsgproc_pid(&g)) {
3031      owl_function_debugmsg("Checking on newmsgproc pid==%i", owl_global_get_newmsgproc_pid(&g));
3032      owl_function_debugmsg("Waitpid return is %i", waitpid(owl_global_get_newmsgproc_pid(&g), NULL, WNOHANG));
3033      waitpid(owl_global_get_newmsgproc_pid(&g), NULL, WNOHANG);
3034      if (waitpid(owl_global_get_newmsgproc_pid(&g), NULL, WNOHANG)==-1) {
3035        /* it exited */
3036        owl_global_set_newmsgproc_pid(&g, 0);
3037        owl_function_debugmsg("newmsgproc exited");
3038      } else {
3039        owl_function_debugmsg("newmsgproc did not exit");
3040      }
3041    }
3042   
3043    /* if it exited, fork & exec a new one */
3044    if (owl_global_get_newmsgproc_pid(&g)==0) {
3045      int i, myargc;
3046      i=fork();
3047      if (i) {
3048        /* parent set the child's pid */
3049        owl_global_set_newmsgproc_pid(&g, i);
3050        owl_function_debugmsg("I'm the parent and I started a new newmsgproc with pid %i", i);
3051      } else {
3052        /* child exec's the program */
3053        char **parsed;
3054        parsed=owl_parseline(owl_global_get_newmsgproc(&g), &myargc);
3055        if (myargc < 0) {
3056          owl_function_debugmsg("Could not parse newmsgproc '%s': unbalanced quotes?", owl_global_get_newmsgproc(&g));
3057        }
3058        if (myargc <= 0) {
3059          _exit(127);
3060        }
3061        parsed=realloc(parsed, sizeof(*parsed) * (myargc+1));
3062        parsed[myargc] = NULL;
3063       
3064        owl_function_debugmsg("About to exec \"%s\" with %d arguments", parsed[0], myargc);
3065       
3066        execvp(parsed[0], parsed);
3067       
3068       
3069        /* was there an error exec'ing? */
3070        owl_function_debugmsg("Cannot run newmsgproc '%s': cannot exec '%s': %s", 
3071                              owl_global_get_newmsgproc(&g), parsed[0], strerror(errno));
3072        _exit(127);
3073      }
3074    }
3075  }
3076}
3077
3078/* print the xterm escape sequence to raise the window */
3079void owl_function_xterm_raise(void)
3080{
3081  printf("\033[5t");
3082}
3083
3084/* print the xterm escape sequence to deiconify the window */
3085void owl_function_xterm_deiconify(void)
3086{
3087  printf("\033[1t");
3088}
3089
3090/* Add the specified command to the startup file.  Eventually this
3091 * should be clever, and rewriting settings that will obviosly
3092 * override earlier settings with 'set' 'bindkey' and 'alias'
3093 * commands.  For now though we just remove any line that would
3094 * duplicate this one and then append this line to the end of
3095 * startupfile.
3096 */
3097void owl_function_addstartup(char *buff)
3098{
3099  FILE *file;
3100  char *filename;
3101
3102  filename=owl_sprintf("%s/%s", owl_global_get_homedir(&g), OWL_STARTUP_FILE);
3103  file=fopen(filename, "a");
3104  if (!file) {
3105    owl_function_error("Error opening startupfile for new command");
3106    owl_free(filename);
3107    return;
3108  }
3109
3110  /* delete earlier copies */
3111  owl_util_file_deleteline(filename, buff, 1);
3112  owl_free(filename);
3113
3114  /* add this line */
3115  fprintf(file, "%s\n", buff);
3116
3117  fclose(file);
3118}
3119
3120/* Remove the specified command from the startup file. */
3121void owl_function_delstartup(char *buff)
3122{
3123  char *filename;
3124  filename=owl_sprintf("%s/%s", owl_global_get_homedir(&g), OWL_STARTUP_FILE);
3125  owl_util_file_deleteline(filename, buff, 1);
3126  owl_free(filename);
3127}
3128
3129/* Execute owl commands from the given filename.  If the filename
3130 * is NULL, use the default owl startup commands file.
3131 */
3132void owl_function_source(char *filename)
3133{
3134  FILE *file;
3135  char buff[LINE];
3136
3137  if (!filename) {
3138    filename=owl_sprintf("%s/%s", owl_global_get_homedir(&g), OWL_STARTUP_FILE);
3139    file=fopen(filename, "r");
3140    owl_free(filename);
3141  } else {
3142    file=fopen(filename, "r");
3143  }
3144  if (!file) {
3145    /* just fail silently if it doesn't exist */
3146    return;
3147  }
3148  while (fgets(buff, LINE, file)!=NULL) {
3149    buff[strlen(buff)-1]='\0';
3150    owl_function_command(buff);
3151  }
3152  fclose(file);
3153}
3154
3155void owl_function_change_style(owl_view *v, char *stylename)
3156{
3157  owl_style *s;
3158
3159  s=owl_global_get_style_by_name(&g, stylename);
3160  if (!s) {
3161    owl_function_error("No style named %s", stylename);
3162    return;
3163  }
3164  owl_view_set_style(v, s);
3165  owl_messagelist_invalidate_formats(owl_global_get_msglist(&g));
3166  owl_function_calculate_topmsg(OWL_DIRECTION_DOWNWARDS);
3167  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
3168}
3169
3170void owl_function_toggleoneline()
3171{
3172  owl_view *v;
3173  owl_style *s;
3174
3175  v=owl_global_get_current_view(&g);
3176  s=owl_view_get_style(v);
3177
3178  if (!owl_style_matches_name(s, "oneline")) {
3179    owl_function_change_style(v, "oneline");
3180  } else {
3181    owl_function_change_style(v, owl_global_get_default_style(&g));
3182  }
3183
3184  owl_messagelist_invalidate_formats(owl_global_get_msglist(&g));
3185  owl_function_calculate_topmsg(OWL_DIRECTION_DOWNWARDS);
3186  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
3187}
3188
3189void owl_function_error(char *fmt, ...)
3190{
3191  va_list ap;
3192  char buff[2048], buff2[2048];
3193  char *date;
3194  time_t now;
3195
3196  now=time(NULL);
3197  date=owl_strdup(ctime(&now));
3198  date[strlen(date)-1]='\0';
3199
3200  va_start(ap, fmt);
3201
3202  vsnprintf(buff, 2048, fmt, ap);
3203  sprintf(buff2, "%s %s", date, buff);
3204  owl_function_debugmsg("ERROR: %s", buff);
3205  if (owl_global_get_curs_msgwin(&g)) {
3206    werase(owl_global_get_curs_msgwin(&g));
3207    waddstr(owl_global_get_curs_msgwin(&g), buff); 
3208    wnoutrefresh(owl_global_get_curs_msgwin(&g));
3209    owl_global_set_needrefresh(&g);
3210  }
3211  owl_errqueue_append_err(owl_global_get_errqueue(&g), buff2);
3212  va_end(ap);
3213  owl_free(date);
3214}
3215
3216void owl_function_showerrs()
3217{
3218  owl_fmtext fm;
3219
3220  owl_fmtext_init_null(&fm);
3221  owl_fmtext_append_normal(&fm, "Errors:\n\n");
3222  owl_errqueue_to_fmtext(owl_global_get_errqueue(&g), &fm);
3223  owl_function_popless_fmtext(&fm);
3224}
3225
3226void owl_function_makemsg(char *fmt, ...)
3227{
3228  va_list ap;
3229  char buff[2048];
3230
3231  if (!owl_global_get_curs_msgwin(&g)) return;
3232
3233  va_start(ap, fmt);
3234  werase(owl_global_get_curs_msgwin(&g));
3235 
3236  vsnprintf(buff, 2048, fmt, ap);
3237  owl_function_debugmsg("makemsg: %s", buff);
3238  waddstr(owl_global_get_curs_msgwin(&g), buff); 
3239  wnoutrefresh(owl_global_get_curs_msgwin(&g));
3240  owl_global_set_needrefresh(&g);
3241  va_end(ap);
3242}
Note: See TracBrowser for help on using the repository browser.