source: functions.c @ 8a54409

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