source: functions.c @ e3d9c77

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