source: functions.c @ 8c92848

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