source: functions.c @ de22c3d

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