source: functions.c @ ec6ff52

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