source: functions.c @ 52f3507

barnowl_perlaimdebianowlrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 52f3507 was cee1f25, checked in by James M. Kretchmar <kretch@mit.edu>, 21 years ago
Fixed double quoting in smartzpunt
  • Property mode set to 100644
File size: 88.2 KB
Line 
1#include <stdio.h>
2#include <stdlib.h>
3#include <unistd.h>
4#include <signal.h>
5#include <string.h>
6#include <time.h>
7#include <sys/types.h>
8#include <sys/stat.h>
9#include <sys/wait.h>
10#include <errno.h>
11#include <signal.h>
12#include "owl.h"
13
14static const char fileIdent[] = "$Id$";
15
16void owl_function_noop(void)
17{
18  return;
19}
20
21char *owl_function_command(char *cmdbuff)
22{
23  owl_function_debugmsg("executing command: %s", cmdbuff);
24  return owl_cmddict_execute(owl_global_get_cmddict(&g), 
25                             owl_global_get_context(&g), cmdbuff);
26}
27
28void owl_function_command_norv(char *cmdbuff)
29{
30  char *rv;
31  rv=owl_function_command(cmdbuff);
32  if (rv) owl_free(rv);
33}
34
35void owl_function_command_alias(char *alias_from, char *alias_to)
36{
37  owl_cmddict_add_alias(owl_global_get_cmddict(&g), alias_from, alias_to);
38}
39
40owl_cmd *owl_function_get_cmd(char *name)
41{
42  return owl_cmddict_find(owl_global_get_cmddict(&g), name);
43}
44
45void owl_function_show_commands()
46{
47  owl_list l;
48  owl_fmtext fm;
49
50  owl_fmtext_init_null(&fm);
51  owl_fmtext_append_bold(&fm, "Commands:  ");
52  owl_fmtext_append_normal(&fm, "(use 'show command <name>' for details)\n");
53  owl_cmddict_get_names(owl_global_get_cmddict(&g), &l);
54  owl_fmtext_append_list(&fm, &l, "\n", owl_function_cmd_describe);
55  owl_fmtext_append_normal(&fm, "\n");
56  owl_function_popless_fmtext(&fm);
57  owl_cmddict_namelist_free(&l);
58  owl_fmtext_free(&fm);
59}
60
61void owl_function_show_view(char *viewname)
62{
63  owl_view *v;
64  owl_fmtext fm;
65
66  /* we only have the one view right now */
67  v=owl_global_get_current_view(&g);
68  if (viewname && strcmp(viewname, owl_view_get_name(v))) {
69    owl_function_error("No view named '%s'", viewname);
70    return;
71  }
72
73  owl_fmtext_init_null(&fm);
74  owl_view_to_fmtext(v, &fm);
75  owl_function_popless_fmtext(&fm);
76  owl_fmtext_free(&fm);
77}
78
79void owl_function_show_styles() {
80  owl_list l;
81  owl_fmtext fm;
82
83  owl_fmtext_init_null(&fm);
84  owl_fmtext_append_bold(&fm, "Styles:\n");
85  owl_global_get_style_names(&g, &l);
86  owl_fmtext_append_list(&fm, &l, "\n", owl_function_style_describe);
87  owl_fmtext_append_normal(&fm, "\n");
88  owl_function_popless_fmtext(&fm);
89  owl_list_free_all(&l, owl_free);
90  owl_fmtext_free(&fm);
91}
92
93char *owl_function_style_describe(void *name) {
94  char *desc, *s;
95  owl_style *style;
96  style = owl_global_get_style_by_name(&g, name);
97  if (style) {
98    desc = owl_style_get_description(style);
99  } else {
100    desc = "???";
101  }
102  s = owl_sprintf("%-20s - %s%s", name, 
103                  0==owl_style_validate(style)?"":"[INVALID] ",
104                  desc);
105  return s;
106}
107
108char *owl_function_cmd_describe(void *name)
109{
110  owl_cmd *cmd = owl_cmddict_find(owl_global_get_cmddict(&g), name);
111  if (cmd) return owl_cmd_describe(cmd);
112  else return(NULL);
113}
114
115void owl_function_show_command(char *name)
116{
117  owl_function_help_for_command(name);
118}
119
120void owl_function_adminmsg(char *header, char *body)
121{
122  owl_message *m;
123  int followlast;
124
125  followlast=owl_global_should_followlast(&g);
126  m=owl_malloc(sizeof(owl_message));
127  owl_message_create_admin(m, header, body);
128  owl_messagelist_append_element(owl_global_get_msglist(&g), m);
129  owl_view_consider_message(owl_global_get_current_view(&g), m);
130
131  if (followlast) owl_function_lastmsg_noredisplay();
132
133  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
134  if (owl_popwin_is_active(owl_global_get_popwin(&g))) {
135    owl_popwin_refresh(owl_global_get_popwin(&g));
136  }
137 
138  wnoutrefresh(owl_global_get_curs_recwin(&g));
139  owl_global_set_needrefresh(&g);
140}
141
142void owl_function_make_outgoing_zephyr(char *body, char *zwriteline, char *zsig)
143{
144  owl_message *m;
145  int followlast;
146  owl_zwrite z;
147 
148  followlast=owl_global_should_followlast(&g);
149
150  /* create a zwrite for the purpose of filling in other message fields */
151  owl_zwrite_create_from_line(&z, zwriteline);
152
153  /* create the message */
154  m=owl_malloc(sizeof(owl_message));
155  owl_message_create_from_zwriteline(m, zwriteline, body, zsig);
156
157  /* add it to the global list and current view */
158  owl_messagelist_append_element(owl_global_get_msglist(&g), m);
159  owl_view_consider_message(owl_global_get_current_view(&g), m);
160
161  if (followlast) owl_function_lastmsg_noredisplay();
162
163  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
164  if (owl_popwin_is_active(owl_global_get_popwin(&g))) {
165    owl_popwin_refresh(owl_global_get_popwin(&g));
166  }
167 
168  wnoutrefresh(owl_global_get_curs_recwin(&g));
169  owl_global_set_needrefresh(&g);
170  owl_zwrite_free(&z);
171}
172
173int owl_function_make_outgoing_aim(char *body, char *to)
174{
175  owl_message *m;
176  int followlast;
177
178
179  if (!owl_global_is_aimloggedin(&g)) {
180    return(-1);
181  }
182 
183  followlast=owl_global_should_followlast(&g);
184
185  /* create the message */
186  m=owl_malloc(sizeof(owl_message));
187  owl_message_create_aim(m,
188                         owl_global_get_aim_screenname(&g),
189                         to,
190                         body,
191                         OWL_MESSAGE_DIRECTION_OUT,
192                         0);
193
194  /* add it to the global list and current view */
195  owl_messagelist_append_element(owl_global_get_msglist(&g), m);
196  owl_view_consider_message(owl_global_get_current_view(&g), m);
197
198  if (followlast) owl_function_lastmsg_noredisplay();
199
200  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
201  if (owl_popwin_is_active(owl_global_get_popwin(&g))) {
202    owl_popwin_refresh(owl_global_get_popwin(&g));
203  }
204 
205  wnoutrefresh(owl_global_get_curs_recwin(&g));
206  owl_global_set_needrefresh(&g);
207  return(0);
208}
209
210int owl_function_make_outgoing_loopback(char *body)
211{
212  owl_message *m;
213  int followlast;
214
215  followlast=owl_global_should_followlast(&g);
216
217  /* create the message */
218  m=owl_malloc(sizeof(owl_message));
219  owl_message_create_loopback(m, body);
220  owl_message_set_direction_out(m);
221
222  /* add it to the global list and current view */
223  owl_messagelist_append_element(owl_global_get_msglist(&g), m);
224  owl_view_consider_message(owl_global_get_current_view(&g), m);
225
226  if (followlast) owl_function_lastmsg_noredisplay();
227
228  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
229  if (owl_popwin_is_active(owl_global_get_popwin(&g))) {
230    owl_popwin_refresh(owl_global_get_popwin(&g));
231  }
232 
233  wnoutrefresh(owl_global_get_curs_recwin(&g));
234  owl_global_set_needrefresh(&g);
235  return(0);
236}
237
238void owl_function_zwrite_setup(char *line)
239{
240  owl_editwin *e;
241  char buff[1024];
242  owl_zwrite z;
243  int ret;
244
245  /* check the arguments */
246  ret=owl_zwrite_create_from_line(&z, line);
247  if (ret) {
248    owl_function_error("Error in zwrite arugments");
249    owl_zwrite_free(&z);
250    return;
251  }
252
253  /* send a ping if necessary */
254  if (owl_global_is_txping(&g)) {
255    owl_zwrite_send_ping(&z);
256  }
257  owl_zwrite_free(&z);
258
259  /* create and setup the editwin */
260  e=owl_global_get_typwin(&g);
261  owl_editwin_new_style(e, OWL_EDITWIN_STYLE_MULTILINE, owl_global_get_msg_history(&g));
262
263  if (!owl_global_get_lockout_ctrld(&g)) {
264    owl_function_makemsg("Type your zephyr below.  End with ^D or a dot on a line by itself.  ^C will quit.");
265  } else {
266    owl_function_makemsg("Type your zephyr below.  End with a dot on a line by itself.  ^C will quit.");
267  }
268
269  owl_editwin_clear(e);
270  owl_editwin_set_dotsend(e);
271  strcpy(buff, "----> ");
272  strcat(buff, line);
273  strcat(buff, "\n");
274  owl_editwin_set_locktext(e, buff);
275
276  /* make it active */
277  owl_global_set_typwin_active(&g);
278
279  owl_global_set_buffercommand(&g, line);
280}
281
282void owl_function_aimwrite_setup(char *line)
283{
284  owl_editwin *e;
285  char buff[1024];
286
287  /* check the arguments */
288
289  /* create and setup the editwin */
290  e=owl_global_get_typwin(&g);
291  owl_editwin_new_style(e, OWL_EDITWIN_STYLE_MULTILINE, owl_global_get_msg_history(&g));
292
293  if (!owl_global_get_lockout_ctrld(&g)) {
294    owl_function_makemsg("Type your message below.  End with ^D or a dot on a line by itself.  ^C will quit.");
295  } else {
296    owl_function_makemsg("Type your message below.  End with a dot on a line by itself.  ^C will quit.");
297  }
298
299  owl_editwin_clear(e);
300  owl_editwin_set_dotsend(e);
301  strcpy(buff, "----> ");
302  strcat(buff, line);
303  strcat(buff, "\n");
304  owl_editwin_set_locktext(e, buff);
305
306  /* make it active */
307  owl_global_set_typwin_active(&g);
308
309  owl_global_set_buffercommand(&g, line);
310}
311
312void owl_function_loopwrite_setup()
313{
314  owl_editwin *e;
315
316  /* create and setup the editwin */
317  e=owl_global_get_typwin(&g);
318  owl_editwin_new_style(e, OWL_EDITWIN_STYLE_MULTILINE, owl_global_get_msg_history(&g));
319
320  if (!owl_global_get_lockout_ctrld(&g)) {
321    owl_function_makemsg("Type your message below.  End with ^D or a dot on a line by itself.  ^C will quit.");
322  } else {
323    owl_function_makemsg("Type your message below.  End with a dot on a line by itself.  ^C will quit.");
324  }
325
326  owl_editwin_clear(e);
327  owl_editwin_set_dotsend(e);
328  owl_editwin_set_locktext(e, "----> loopwrite\n");
329
330  /* make it active */
331  owl_global_set_typwin_active(&g);
332
333  owl_global_set_buffercommand(&g, "loopwrite");
334}
335
336/* send, log and display an outgoing zephyr.  If 'msg' is NULL
337 * the message is expected to be set from the zwrite line itself
338 */
339void owl_function_zwrite(char *line, char *msg)
340{
341  owl_zwrite z;
342  int i, j;
343  char *mymsg;
344
345  /* create the zwrite and send the message */
346  owl_zwrite_create_from_line(&z, line);
347  if (msg) {
348    owl_zwrite_set_message(&z, msg);
349  }
350
351  owl_zwrite_send_message(&z);
352  owl_function_makemsg("Waiting for ack...");
353
354  mymsg=owl_zwrite_get_message(&z);
355
356  /* display the message as an outgoing message in the receive window */
357  if (owl_global_is_displayoutgoing(&g) && owl_zwrite_is_personal(&z)) {
358    owl_function_make_outgoing_zephyr(mymsg, line, owl_zwrite_get_zsig(&z));
359  }
360
361  /* log it if we have logging turned on */
362  if (owl_global_is_logging(&g) && owl_zwrite_is_personal(&z)) {
363    j=owl_zwrite_get_numrecips(&z);
364    for (i=0; i<j; i++) {
365      owl_log_outgoing_zephyr(owl_zwrite_get_recip_n(&z, i), mymsg);
366    }
367  }
368
369  /* free the zwrite */
370  owl_zwrite_free(&z);
371}
372
373/* send, log and display an outgoing zcrypt zephyr.  If 'msg' is NULL
374 * the message is expected to be set from the zwrite line itself
375 */
376void owl_function_zcrypt(char *line, char *msg)
377{
378  owl_zwrite z;
379  int i, j;
380  char *mymsg;
381  char *cryptmsg;
382#ifdef OWL_ENABLE_ZCRYPT
383  int ret;
384#endif
385
386  /* create the zwrite and send the message */
387  owl_zwrite_create_from_line(&z, line);
388  if (msg) {
389    owl_zwrite_set_message(&z, msg);
390  }
391
392  mymsg=owl_zwrite_get_message(&z);
393#ifdef OWL_ENABLE_ZCRYPT
394  cryptmsg=owl_malloc(strlen(mymsg)*4);
395  ret=owl_zcrypt_encrypt(cryptmsg, mymsg, owl_zwrite_get_class(&z), owl_zwrite_get_instance(&z));
396  if (ret) {
397    owl_function_error("Error in zcrypt, possibly no key found.  Message not sent.");
398    owl_function_beep();
399    owl_free(cryptmsg);
400    return;
401  }
402#else
403  cryptmsg=owl_strdup(mymsg);
404#endif
405
406  owl_zwrite_set_message(&z, cryptmsg);
407  owl_zwrite_set_opcode(&z, "crypt");
408  mymsg=cryptmsg;
409   
410  owl_zwrite_send_message(&z);
411  owl_function_makemsg("Waiting for ack...");
412
413  /* display the message as an outgoing message in the receive window */
414  if (owl_global_is_displayoutgoing(&g) && owl_zwrite_is_personal(&z)) {
415    owl_function_make_outgoing_zephyr(mymsg, line, owl_zwrite_get_zsig(&z));
416  }
417
418  /* log it if we have logging turned on */
419  if (owl_global_is_logging(&g) && owl_zwrite_is_personal(&z)) {
420    j=owl_zwrite_get_numrecips(&z);
421    for (i=0; i<j; i++) {
422      owl_log_outgoing_zephyr(owl_zwrite_get_recip_n(&z, i), mymsg);
423    }
424  }
425
426  /* free the zwrite */
427  owl_free(cryptmsg);
428  owl_zwrite_free(&z);
429}
430
431void owl_function_aimwrite(char *to)
432{
433  int ret;
434 
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    /* loopback */
1889    if (owl_message_is_type_loopback(m)) {
1890      owl_function_loopwrite_setup();
1891      return;
1892    }
1893
1894    /* zephyr */
1895    if (owl_message_is_type_zephyr(m)) {
1896      /* if it's a zephyr we sent, send it out the same way again */
1897      if (owl_message_is_direction_out(m)) {
1898        owl_function_zwrite_setup(owl_message_get_zwriteline(m));
1899        owl_global_set_buffercommand(&g, owl_message_get_zwriteline(m));
1900        return;
1901      }
1902
1903      /* Special case a personal reply to a webzephyr user on a class */
1904      if ((type==1) && !strcasecmp(owl_message_get_opcode(m), OWL_WEBZEPHYR_OPCODE)) {
1905        class=OWL_WEBZEPHYR_CLASS;
1906        inst=owl_message_get_sender(m);
1907        to=OWL_WEBZEPHYR_PRINCIPAL;
1908      } else if (!strcasecmp(owl_message_get_class(m), OWL_WEBZEPHYR_CLASS) && owl_message_is_loginout(m)) {
1909        /* Special case LOGIN/LOGOUT notifications on class "webzephyr" */
1910        class=OWL_WEBZEPHYR_CLASS;
1911        inst=owl_message_get_instance(m);
1912        to=OWL_WEBZEPHYR_PRINCIPAL;
1913      } else if (owl_message_is_loginout(m)) {
1914        /* Normal LOGIN/LOGOUT messages */
1915        class="MESSAGE";
1916        inst="PERSONAL";
1917        to=owl_message_get_sender(m);
1918      } else if (type==1) {
1919        /* Personal reply */
1920        class="MESSAGE";
1921        inst="PERSONAL";
1922        to=owl_message_get_sender(m);
1923      } else {
1924        /* General reply */
1925        class=owl_message_get_class(m);
1926        inst=owl_message_get_instance(m);
1927        to=owl_message_get_recipient(m);
1928        cc=owl_message_get_cc(m);
1929        if (!strcmp(to, "") || !strcmp(to, "*")) {
1930          to="";
1931        } else if (to[0]=='@') {
1932          /* leave it, to get the realm */
1933        } else {
1934          to=owl_message_get_sender(m);
1935        }
1936      }
1937       
1938      /* create the command line */
1939      if (!strcasecmp(owl_message_get_opcode(m), "CRYPT")) {
1940        buff=owl_strdup("zcrypt");
1941      } else {
1942        buff = owl_strdup("zwrite");
1943      }
1944      if (strcasecmp(class, "message")) {
1945        buff = owl_sprintf("%s -c %s%s%s", oldbuff=buff, owl_getquoting(class), class, owl_getquoting(class));
1946        owl_free(oldbuff);
1947      }
1948      if (strcasecmp(inst, "personal")) {
1949        buff = owl_sprintf("%s -i %s%s%s", oldbuff=buff, owl_getquoting(inst), inst, owl_getquoting(inst));
1950        owl_free(oldbuff);
1951      }
1952      if (*to != '\0') {
1953        char *tmp, *oldtmp, *tmp2;
1954        tmp=short_zuser(to);
1955        if (cc) {
1956          tmp = owl_util_uniq(oldtmp=tmp, cc, "-");
1957          owl_free(oldtmp);
1958          buff = owl_sprintf("%s -C %s", oldbuff=buff, tmp);
1959          owl_free(oldbuff);
1960        } else {
1961          if (owl_global_is_smartstrip(&g)) {
1962            tmp2=tmp;
1963            tmp=owl_zephyr_smartstripped_user(tmp2);
1964            owl_free(tmp2);
1965          }
1966          buff = owl_sprintf("%s %s", oldbuff=buff, tmp);
1967          owl_free(oldbuff);
1968        }
1969        owl_free(tmp);
1970      }
1971      if (cc) owl_free(cc);
1972    }
1973
1974    /* aim */
1975    if (owl_message_is_type_aim(m)) {
1976      if (owl_message_is_direction_out(m)) {
1977        buff=owl_sprintf("aimwrite %s", owl_message_get_recipient(m));
1978      } else {
1979        buff=owl_sprintf("aimwrite %s", owl_message_get_sender(m));
1980      }
1981    }
1982   
1983    if (enter) {
1984      owl_history *hist = owl_global_get_cmd_history(&g);
1985      owl_history_store(hist, buff);
1986      owl_history_reset(hist);
1987      owl_function_command_norv(buff);
1988    } else {
1989      owl_function_start_command(buff);
1990    }
1991    owl_free(buff);
1992  }
1993}
1994
1995void owl_function_zlocate(int argc, char **argv, int auth)
1996{
1997  owl_fmtext fm;
1998  char *ptr, buff[LINE];
1999  int i;
2000
2001  owl_fmtext_init_null(&fm);
2002
2003  for (i=0; i<argc; i++) {
2004    ptr=long_zuser(argv[i]);
2005    owl_zephyr_zlocate(ptr, buff, auth);
2006    owl_fmtext_append_normal(&fm, buff);
2007    owl_free(ptr);
2008  }
2009
2010  owl_function_popless_fmtext(&fm);
2011  owl_fmtext_free(&fm);
2012}
2013
2014void owl_function_start_command(char *line)
2015{
2016  int i, j;
2017  owl_editwin *tw;
2018
2019  tw=owl_global_get_typwin(&g);
2020  owl_global_set_typwin_active(&g);
2021  owl_editwin_new_style(tw, OWL_EDITWIN_STYLE_ONELINE, 
2022                        owl_global_get_cmd_history(&g));
2023
2024  owl_editwin_set_locktext(tw, "command: ");
2025  owl_global_set_needrefresh(&g);
2026
2027  j=strlen(line);
2028  for (i=0; i<j; i++) {
2029    owl_editwin_process_char(tw, line[i]);
2030  }
2031  owl_editwin_redisplay(tw, 0);
2032
2033  owl_context_set_editline(owl_global_get_context(&g), tw);
2034  owl_function_activate_keymap("editline");
2035}
2036
2037void owl_function_start_question(char *line)
2038{
2039  owl_editwin *tw;
2040
2041  tw=owl_global_get_typwin(&g);
2042  owl_global_set_typwin_active(&g);
2043  owl_editwin_new_style(tw, OWL_EDITWIN_STYLE_ONELINE, owl_global_get_cmd_history(&g));
2044
2045  owl_editwin_set_locktext(tw, line);
2046  owl_global_set_needrefresh(&g);
2047
2048  owl_editwin_redisplay(tw, 0);
2049
2050  owl_context_set_editresponse(owl_global_get_context(&g), tw);
2051  owl_function_activate_keymap("editresponse");
2052}
2053
2054void owl_function_start_password(char *line)
2055{
2056  owl_editwin *tw;
2057
2058  tw=owl_global_get_typwin(&g);
2059  owl_global_set_typwin_active(&g);
2060  owl_editwin_new_style(tw, OWL_EDITWIN_STYLE_ONELINE, owl_global_get_cmd_history(&g));
2061  owl_editwin_set_echochar(tw, '*');
2062
2063  owl_editwin_set_locktext(tw, line);
2064  owl_global_set_needrefresh(&g);
2065
2066  owl_editwin_redisplay(tw, 0);
2067
2068  owl_context_set_editresponse(owl_global_get_context(&g), tw);
2069  owl_function_activate_keymap("editresponse");
2070}
2071
2072char *owl_function_exec(int argc, char **argv, char *buff, int type)
2073{
2074  /* if type == 1 display in a popup
2075   * if type == 2 display an admin messages
2076   * if type == 0 return output
2077   * else display in a popup
2078   */
2079  char *newbuff, *redirect = " 2>&1 < /dev/null";
2080  char *out, buff2[1024];
2081  int size;
2082  FILE *p;
2083
2084#if OWL_STDERR_REDIR
2085  redirect = " < /dev/null";
2086#endif
2087
2088  if (argc<2) {
2089    owl_function_error("Wrong number of arguments to the exec command");
2090    return NULL;
2091  }
2092
2093  buff = skiptokens(buff, 1);
2094  newbuff = owl_malloc(strlen(buff)+strlen(redirect)+1);
2095  strcpy(newbuff, buff);
2096  strcat(newbuff, redirect);
2097
2098  p=popen(newbuff, "r");
2099  out=owl_malloc(1024);
2100  size=1024;
2101  strcpy(out, "");
2102  while (fgets(buff2, 1024, p)!=NULL) {
2103    size+=1024;
2104    out=owl_realloc(out, size);
2105    strcat(out, buff2);
2106  }
2107  pclose(p);
2108
2109  if (type==1) {
2110    owl_function_popless_text(out);
2111  } else if (type==0) {
2112    return out;
2113  } else if (type==2) {
2114    owl_function_adminmsg(buff, out);
2115  } else {
2116    owl_function_popless_text(out);
2117  }
2118  owl_free(out);
2119  return NULL;
2120}
2121
2122char *owl_function_perl(int argc, char **argv, char *buff, int type)
2123{
2124  /* if type == 1 display in a popup
2125   * if type == 2 display an admin messages
2126   * if type == 0 return output
2127   * else display in a popup
2128   */
2129  char *perlout;
2130
2131  if (argc<2) {
2132    owl_function_error("Wrong number of arguments to perl command");
2133    return NULL;
2134  }
2135
2136  /* consume first token (argv[0]) */
2137  buff = skiptokens(buff, 1);
2138
2139  perlout = owl_perlconfig_execute(buff);
2140  if (perlout) { 
2141    if (type==1) {
2142      owl_function_popless_text(perlout);
2143    } else if (type==2) {
2144      owl_function_adminmsg(buff, perlout);
2145    } else if (type==0) {
2146      return perlout;
2147    } else {
2148      owl_function_popless_text(perlout);
2149    }
2150    owl_free(perlout);
2151  }
2152  return NULL;
2153}
2154
2155#if 0
2156void owl_function_change_view_old(char *filtname)
2157{
2158  owl_view *v;
2159  owl_filter *f;
2160  int curid=-1, newpos, curmsg;
2161  owl_message *curm=NULL;
2162
2163  v=owl_global_get_current_view(&g);
2164  curmsg=owl_global_get_curmsg(&g);
2165  if (curmsg==-1) {
2166    owl_function_debugmsg("Hit the curmsg==-1 case in change_view");
2167  } else {
2168    curm=owl_view_get_element(v, curmsg);
2169    if (curm) {
2170      curid=owl_message_get_id(curm);
2171      owl_view_save_curmsgid(v, curid);
2172    }
2173  }
2174
2175  /* grab the filter */;
2176  f=owl_global_get_filter(&g, filtname);
2177  if (!f) {
2178    owl_function_error("Unknown filter %s", filtname);
2179    return;
2180  }
2181
2182  /* free the existing view and create a new one based on the filter */
2183  owl_view_free(v);
2184  owl_view_create(v, f);
2185
2186  /* Figure out what to set the current message to.
2187   * - If the view we're leaving has messages in it, go to the closest message
2188   *   to the last message pointed to in that view.
2189   * - If the view we're leaving is empty, try to restore the position
2190   *   from the last time we were in the new view.  */
2191  if (curm) {
2192    newpos = owl_view_get_nearest_to_msgid(v, curid);
2193  } else {
2194    newpos = owl_view_get_nearest_to_saved(v);
2195  }
2196
2197  owl_global_set_curmsg(&g, newpos);
2198
2199  owl_function_calculate_topmsg(OWL_DIRECTION_DOWNWARDS);
2200  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2201  owl_global_set_direction_downwards(&g);
2202}
2203#endif
2204
2205void owl_function_change_view(char *filtname)
2206{
2207  owl_view *v;
2208  owl_filter *f;
2209  int curid=-1, newpos, curmsg;
2210  owl_message *curm=NULL;
2211
2212  v=owl_global_get_current_view(&g);
2213
2214  curmsg=owl_global_get_curmsg(&g);
2215  if (curmsg==-1) {
2216    owl_function_debugmsg("Hit the curmsg==-1 case in change_view");
2217  } else {
2218    curm=owl_view_get_element(v, curmsg);
2219    if (curm) {
2220      curid=owl_message_get_id(curm);
2221      owl_view_save_curmsgid(v, curid);
2222    }
2223  }
2224
2225  f=owl_global_get_filter(&g, filtname);
2226  if (!f) {
2227    owl_function_error("Unknown filter %s", filtname);
2228    return;
2229  }
2230
2231  owl_view_new_filter(v, f);
2232
2233  /* Figure out what to set the current message to.
2234   * - If the view we're leaving has messages in it, go to the closest message
2235   *   to the last message pointed to in that view.
2236   * - If the view we're leaving is empty, try to restore the position
2237   *   from the last time we were in the new view.  */
2238  if (curm) {
2239    newpos = owl_view_get_nearest_to_msgid(v, curid);
2240  } else {
2241    newpos = owl_view_get_nearest_to_saved(v);
2242  }
2243
2244  owl_global_set_curmsg(&g, newpos);
2245  owl_function_calculate_topmsg(OWL_DIRECTION_DOWNWARDS);
2246  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2247  owl_global_set_direction_downwards(&g);
2248}
2249
2250void owl_function_create_filter(int argc, char **argv)
2251{
2252  owl_filter *f;
2253  owl_view *v;
2254  int ret, inuse=0;
2255
2256  if (argc < 2) {
2257    owl_function_error("Wrong number of arguments to filter command");
2258    return;
2259  }
2260
2261  v=owl_global_get_current_view(&g);
2262
2263  /* don't touch the all filter */
2264  if (!strcmp(argv[1], "all")) {
2265    owl_function_error("You may not change the 'all' filter.");
2266    return;
2267  }
2268
2269  /* deal with the case of trying change the filter color */
2270  if (argc==4 && !strcmp(argv[2], "-c")) {
2271    f=owl_global_get_filter(&g, argv[1]);
2272    if (!f) {
2273      owl_function_error("The filter '%s' does not exist.", argv[1]);
2274      return;
2275    }
2276    if (owl_util_string_to_color(argv[3])==-1) {
2277      owl_function_error("The color '%s' is not available.", argv[3]);
2278      return;
2279    }
2280    owl_filter_set_color(f, owl_util_string_to_color(argv[3]));
2281    owl_global_set_needrefresh(&g);
2282    owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2283    return;
2284  }
2285
2286  /* create the filter and check for errors */
2287  f=owl_malloc(sizeof(owl_filter));
2288  ret=owl_filter_init(f, argv[1], argc-2, argv+2);
2289  if (ret==-1) {
2290    owl_free(f);
2291    owl_function_error("Invalid filter syntax");
2292    return;
2293  }
2294
2295  /* if the named filter is in use by the current view, remember it */
2296  if (!strcmp(owl_view_get_filtname(v), argv[1])) {
2297    inuse=1;
2298  }
2299
2300  /* if the named filter already exists, nuke it */
2301  if (owl_global_get_filter(&g, argv[1])) {
2302    owl_global_remove_filter(&g, argv[1]);
2303  }
2304
2305  /* add the filter */
2306  owl_global_add_filter(&g, f);
2307
2308  /* if it was in use by the current view then update */
2309  if (inuse) {
2310    owl_function_change_view(argv[1]);
2311  }
2312  owl_global_set_needrefresh(&g);
2313  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2314}
2315
2316void owl_function_show_filters()
2317{
2318  owl_list *l;
2319  owl_filter *f;
2320  int i, j;
2321  owl_fmtext fm;
2322
2323  owl_fmtext_init_null(&fm);
2324
2325  l=owl_global_get_filterlist(&g);
2326  j=owl_list_get_size(l);
2327
2328  owl_fmtext_append_bold(&fm, "Filters:\n");
2329
2330  for (i=0; i<j; i++) {
2331    f=owl_list_get_element(l, i);
2332    owl_fmtext_append_normal(&fm, "   ");
2333    if (owl_global_get_hascolors(&g)) {
2334      owl_fmtext_append_normal_color(&fm, owl_filter_get_name(f), owl_filter_get_color(f));
2335    } else {
2336      owl_fmtext_append_normal(&fm, owl_filter_get_name(f));
2337    }
2338    owl_fmtext_append_normal(&fm, "\n");
2339  }
2340  owl_function_popless_fmtext(&fm);
2341  owl_fmtext_free(&fm);
2342}
2343
2344void owl_function_show_filter(char *name)
2345{
2346  owl_filter *f;
2347  char buff[5000];
2348
2349  f=owl_global_get_filter(&g, name);
2350  if (!f) {
2351    owl_function_error("There is no filter named %s", name);
2352    return;
2353  }
2354  owl_filter_print(f, buff);
2355  owl_function_popless_text(buff);
2356}
2357
2358void owl_function_show_zpunts()
2359{
2360  owl_filter *f;
2361  owl_list *fl;
2362  char buff[5000];
2363  owl_fmtext fm;
2364  int i, j;
2365
2366  owl_fmtext_init_null(&fm);
2367
2368  fl=owl_global_get_puntlist(&g);
2369  j=owl_list_get_size(fl);
2370  owl_fmtext_append_bold(&fm, "Active zpunt filters:\n");
2371
2372  for (i=0; i<j; i++) {
2373    f=owl_list_get_element(fl, i);
2374    owl_filter_print(f, buff);
2375    owl_fmtext_append_normal(&fm, buff);
2376  }
2377  owl_function_popless_fmtext(&fm);
2378  owl_fmtext_free(&fm);
2379}
2380
2381/* Create a filter for a class, instance if one doesn't exist.  If
2382 * instance is NULL then catch all messgaes in the class.  Returns the
2383 * name of the filter, which the caller must free.
2384 */
2385char *owl_function_classinstfilt(char *class, char *instance) 
2386{
2387  owl_list *fl;
2388  owl_filter *f;
2389  char *argbuff, *filtname;
2390  char *tmpclass, *tmpinstance = NULL;
2391  int len;
2392
2393  fl=owl_global_get_filterlist(&g);
2394
2395  /* name for the filter */
2396  len=strlen(class)+30;
2397  if (instance) len+=strlen(instance);
2398  filtname=owl_malloc(len);
2399  if (!instance) {
2400    sprintf(filtname, "class-%s", class);
2401  } else {
2402    sprintf(filtname, "class-%s-instance-%s", class, instance);
2403  }
2404  /* downcase it */
2405  downstr(filtname);
2406  /* turn spaces into dots */
2407  owl_text_tr(filtname, ' ', '.');
2408 
2409  /* if it already exists then go with it.  This lets users override */
2410  if (owl_global_get_filter(&g, filtname)) {
2411    return(filtname);
2412  }
2413
2414  /* create the new filter */
2415  argbuff=owl_malloc(len+20);
2416  tmpclass=owl_text_quote(class, OWL_REGEX_QUOTECHARS, OWL_REGEX_QUOTEWITH);
2417  owl_text_tr(tmpclass, ' ', '.');
2418  if (instance) {
2419    tmpinstance=owl_text_quote(instance, OWL_REGEX_QUOTECHARS, OWL_REGEX_QUOTEWITH);
2420    owl_text_tr(tmpinstance, ' ', '.');
2421  }
2422  sprintf(argbuff, "( class ^%s$ )", tmpclass);
2423  if (tmpinstance) {
2424    sprintf(argbuff, "%s and ( instance ^%s$ )", argbuff, tmpinstance);
2425  }
2426  owl_free(tmpclass);
2427  if (tmpinstance) owl_free(tmpinstance);
2428
2429  f=owl_malloc(sizeof(owl_filter));
2430  owl_filter_init_fromstring(f, filtname, argbuff);
2431
2432  /* add it to the global list */
2433  owl_global_add_filter(&g, f);
2434
2435  owl_free(argbuff);
2436  return(filtname);
2437}
2438
2439/* Create a filter for personal zephyrs to or from the specified
2440 * zephyr user.  Includes login/logout notifications for the user.
2441 * The name of the filter will be 'user-<user>'.  If a filter already
2442 * exists with this name, no new filter will be created.  This allows
2443 * the configuration to override this function.  Returns the name of
2444 * the filter, which the caller must free.
2445 */
2446char *owl_function_zuserfilt(char *user)
2447{
2448  owl_filter *f;
2449  char *argbuff, *longuser, *shortuser, *filtname;
2450
2451  /* stick the local realm on if it's not there */
2452  longuser=long_zuser(user);
2453  shortuser=short_zuser(user);
2454
2455  /* name for the filter */
2456  filtname=owl_malloc(strlen(shortuser)+20);
2457  sprintf(filtname, "user-%s", shortuser);
2458
2459  /* if it already exists then go with it.  This lets users override */
2460  if (owl_global_get_filter(&g, filtname)) {
2461    return(owl_strdup(filtname));
2462  }
2463
2464  /* create the new-internal filter */
2465  f=owl_malloc(sizeof(owl_filter));
2466
2467  argbuff=owl_malloc(strlen(longuser)+1000);
2468  sprintf(argbuff, "( type ^zephyr$ and ( class ^message$ and instance ^personal$ and ");
2469  sprintf(argbuff, "%s ( ( direction ^in$ and sender ^%s$ ) or ( direction ^out$ and recipient ^%s$ ) ) )", argbuff, longuser, longuser);
2470  sprintf(argbuff, "%s or ( ( class ^login$ ) and ( sender ^%s$ ) ) )", argbuff, longuser);
2471
2472  owl_filter_init_fromstring(f, filtname, argbuff);
2473
2474  /* add it to the global list */
2475  owl_global_add_filter(&g, f);
2476
2477  /* free stuff */
2478  owl_free(argbuff);
2479  owl_free(longuser);
2480  owl_free(shortuser);
2481
2482  return(filtname);
2483}
2484
2485/* Create a filter for AIM IM messages to or from the specified
2486 * screenname.  The name of the filter will be 'aimuser-<user>'.  If a
2487 * filter already exists with this name, no new filter will be
2488 * created.  This allows the configuration to override this function.
2489 * Returns the name of the filter, which the caller must free.
2490 */
2491char *owl_function_aimuserfilt(char *user)
2492{
2493  owl_filter *f;
2494  char *argbuff, *filtname;
2495
2496  /* name for the filter */
2497  filtname=owl_malloc(strlen(user)+40);
2498  sprintf(filtname, "aimuser-%s", user);
2499
2500  /* if it already exists then go with it.  This lets users override */
2501  if (owl_global_get_filter(&g, filtname)) {
2502    return(owl_strdup(filtname));
2503  }
2504
2505  /* create the new-internal filter */
2506  f=owl_malloc(sizeof(owl_filter));
2507
2508  argbuff=owl_malloc(1000);
2509  sprintf(argbuff,
2510          "( type ^aim$ and ( ( sender ^%s$ and recipient ^%s$ ) or ( sender ^%s$ and recipient ^%s$ ) ) )",
2511          user, owl_global_get_aim_screenname(&g), owl_global_get_aim_screenname(&g), user);
2512
2513  owl_filter_init_fromstring(f, filtname, argbuff);
2514
2515  /* add it to the global list */
2516  owl_global_add_filter(&g, f);
2517
2518  /* free stuff */
2519  owl_free(argbuff);
2520
2521  return(filtname);
2522}
2523
2524char *owl_function_typefilt(char *type)
2525{
2526  owl_filter *f;
2527  char *argbuff, *filtname;
2528
2529  /* name for the filter */
2530  filtname=owl_sprintf("type-%s", type);
2531
2532  /* if it already exists then go with it.  This lets users override */
2533  if (owl_global_get_filter(&g, filtname)) {
2534    return filtname;
2535  }
2536
2537  /* create the new-internal filter */
2538  f=owl_malloc(sizeof(owl_filter));
2539
2540  argbuff = owl_sprintf("type ^%s$", type);
2541
2542  owl_filter_init_fromstring(f, filtname, argbuff);
2543
2544  /* add it to the global list */
2545  owl_global_add_filter(&g, f);
2546
2547  /* free stuff */
2548  owl_free(argbuff);
2549
2550  return filtname;
2551}
2552
2553/* If flag is 1, marks for deletion.  If flag is 0,
2554 * unmarks for deletion. */
2555void owl_function_delete_curview_msgs(int flag)
2556{
2557  owl_view *v;
2558  int i, j;
2559
2560  v=owl_global_get_current_view(&g);
2561  j=owl_view_get_size(v);
2562  for (i=0; i<j; i++) {
2563    if (flag == 1) {
2564      owl_message_mark_delete(owl_view_get_element(v, i));
2565    } else if (flag == 0) {
2566      owl_message_unmark_delete(owl_view_get_element(v, i));
2567    }
2568  }
2569
2570  owl_function_makemsg("%i messages marked for %sdeletion", j, flag?"":"un");
2571
2572  owl_mainwin_redisplay(owl_global_get_mainwin(&g)); 
2573}
2574
2575/* Create a filter based on the current message.  Returns the name of
2576 * a filter or null.  The caller must free this name.
2577 *
2578 * if the curmsg is a personal zephyr return a filter name
2579 *    to the zephyr converstaion with that user.
2580 * If the curmsg is a zephyr class message, instance foo, recip *,
2581 *    return a filter name to the class, inst.
2582 * If the curmsg is a zephyr class message and type==0 then
2583 *    return a filter name for just the class.
2584 * If the curmsg is a zephyr class message and type==1 then
2585 *    return a filter name for the class and instance.
2586 * If the curmsg is a personal AIM message returna  filter
2587 *    name to the AIM conversation with that user
2588 */
2589char *owl_function_smartfilter(int type)
2590{
2591  owl_view *v;
2592  owl_message *m;
2593  char *zperson, *filtname=NULL;
2594 
2595  v=owl_global_get_current_view(&g);
2596  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
2597
2598  if (!m || owl_view_get_size(v)==0) {
2599    owl_function_error("No message selected\n");
2600    return(NULL);
2601  }
2602
2603  /* very simple handling of admin messages for now */
2604  if (owl_message_is_type_admin(m)) {
2605    return(owl_function_typefilt("admin"));
2606  }
2607
2608  /* very simple handling of loopback messages for now */
2609  if (owl_message_is_type_loopback(m)) {
2610    return(owl_function_typefilt("loopback"));
2611  }
2612
2613  /* aim messages */
2614  if (owl_message_is_type_aim(m)) {
2615    if (owl_message_is_direction_in(m)) {
2616      filtname=owl_function_aimuserfilt(owl_message_get_sender(m));
2617    } else if (owl_message_is_direction_out(m)) {
2618      filtname=owl_function_aimuserfilt(owl_message_get_recipient(m));
2619    }
2620    return(filtname);
2621  }
2622
2623  /* narrow personal and login messages to the sender or recip as appropriate */
2624  if (owl_message_is_personal(m) || owl_message_is_loginout(m)) {
2625    if (owl_message_is_type_zephyr(m)) {
2626      if (owl_message_is_direction_in(m)) {
2627        zperson=short_zuser(owl_message_get_sender(m));
2628      } else {
2629        zperson=short_zuser(owl_message_get_recipient(m));
2630      }
2631      filtname=owl_function_zuserfilt(zperson);
2632      owl_free(zperson);
2633      return(filtname);
2634    }
2635    return(NULL);
2636  }
2637
2638  /* narrow class MESSAGE, instance foo, recip * messages to class, inst */
2639  if (!strcasecmp(owl_message_get_class(m), "message") && !owl_message_is_personal(m)) {
2640    filtname=owl_function_classinstfilt(owl_message_get_class(m), owl_message_get_instance(m));
2641    return(filtname);
2642  }
2643
2644  /* otherwise narrow to the class */
2645  if (type==0) {
2646    filtname=owl_function_classinstfilt(owl_message_get_class(m), NULL);
2647  } else if (type==1) {
2648    filtname=owl_function_classinstfilt(owl_message_get_class(m), owl_message_get_instance(m));
2649  }
2650  return(filtname);
2651}
2652
2653void owl_function_smartzpunt(int type)
2654{
2655  /* Starts a zpunt command based on the current class,instance pair.
2656   * If type=0, uses just class.  If type=1, uses instance as well. */
2657  owl_view *v;
2658  owl_message *m;
2659  char *cmd, *cmdprefix, *mclass, *minst;
2660 
2661  v=owl_global_get_current_view(&g);
2662  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
2663
2664  if (!m || owl_view_get_size(v)==0) {
2665    owl_function_error("No message selected\n");
2666    return;
2667  }
2668
2669  /* for now we skip admin messages. */
2670  if (owl_message_is_type_admin(m)
2671      || owl_message_is_loginout(m)
2672      || !owl_message_is_type_zephyr(m)) {
2673    owl_function_error("smartzpunt doesn't support this message type.");
2674    return;
2675  }
2676
2677  mclass = owl_message_get_class(m);
2678  minst = owl_message_get_instance(m);
2679  if (!mclass || !*mclass || *mclass==' '
2680      || (!strcasecmp(mclass, "message") && !strcasecmp(minst, "personal"))
2681      || (type && (!minst || !*minst|| *minst==' '))) {
2682    owl_function_error("smartzpunt can't safely do this for <%s,%s>",
2683                         mclass, minst);
2684  } else {
2685    cmdprefix = "start-command zpunt ";
2686    cmd = owl_malloc(strlen(cmdprefix)+strlen(mclass)+strlen(minst)+10);
2687    strcpy(cmd, cmdprefix);
2688    strcat(cmd, owl_getquoting(mclass));
2689    strcat(cmd, mclass);
2690    strcat(cmd, owl_getquoting(mclass));
2691    if (type) {
2692      strcat(cmd, " ");
2693      strcat(cmd, owl_getquoting(minst));
2694      strcat(cmd, minst);
2695      strcat(cmd, owl_getquoting(minst));
2696    } else {
2697      strcat(cmd, " *");
2698    }
2699    owl_function_command(cmd);
2700    owl_free(cmd);
2701  }
2702}
2703
2704void owl_function_color_current_filter(char *color)
2705{
2706  owl_filter *f;
2707  char *name;
2708
2709  name=owl_view_get_filtname(owl_global_get_current_view(&g));
2710  f=owl_global_get_filter(&g, name);
2711  if (!f) {
2712    owl_function_error("Unknown filter");
2713    return;
2714  }
2715
2716  /* don't touch the all filter */
2717  if (!strcmp(name, "all")) {
2718    owl_function_error("You may not change the 'all' filter.");
2719    return;
2720  }
2721
2722  /* deal with the case of trying change the filter color */
2723  if (owl_util_string_to_color(color)==-1) {
2724    owl_function_error("No color named '%s' avilable.");
2725    return;
2726  }
2727  owl_filter_set_color(f, owl_util_string_to_color(color));
2728  owl_global_set_needrefresh(&g);
2729  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2730}
2731
2732void owl_function_show_colors()
2733{
2734  owl_fmtext fm;
2735
2736  owl_fmtext_init_null(&fm);
2737  owl_fmtext_append_normal_color(&fm, "default\n", OWL_COLOR_DEFAULT);
2738  owl_fmtext_append_normal_color(&fm, "red\n", OWL_COLOR_RED);
2739  owl_fmtext_append_normal_color(&fm, "green\n", OWL_COLOR_GREEN);
2740  owl_fmtext_append_normal_color(&fm, "yellow\n", OWL_COLOR_YELLOW);
2741  owl_fmtext_append_normal_color(&fm, "blue\n", OWL_COLOR_BLUE);
2742  owl_fmtext_append_normal_color(&fm, "magenta\n", OWL_COLOR_MAGENTA);
2743  owl_fmtext_append_normal_color(&fm, "cyan\n", OWL_COLOR_CYAN);
2744  owl_fmtext_append_normal_color(&fm, "white\n", OWL_COLOR_WHITE);
2745
2746  owl_function_popless_fmtext(&fm);
2747  owl_fmtext_free(&fm);
2748}
2749
2750/* add the given class, inst, recip to the punt list for filtering.
2751 *   if direction==0 then punt
2752 *   if direction==1 then unpunt
2753 */
2754void owl_function_zpunt(char *class, char *inst, char *recip, int direction)
2755{
2756  owl_filter *f;
2757  owl_list *fl;
2758  char *buff;
2759  char *quoted;
2760  int ret, i, j;
2761
2762  fl=owl_global_get_puntlist(&g);
2763
2764  /* first, create the filter */
2765  f=malloc(sizeof(owl_filter));
2766  buff=malloc(strlen(class)+strlen(inst)+strlen(recip)+100);
2767  strcpy(buff, "class");
2768  if (!strcmp(class, "*")) {
2769    strcat(buff, " .*");
2770  } else {
2771    quoted=owl_text_quote(class, OWL_REGEX_QUOTECHARS, OWL_REGEX_QUOTEWITH);
2772    owl_text_tr(quoted, ' ', '.');
2773    sprintf(buff, "%s ^%s$", buff, quoted);
2774    owl_free(quoted);
2775  }
2776  if (!strcmp(inst, "*")) {
2777    strcat(buff, " and instance .*");
2778  } else {
2779    quoted=owl_text_quote(inst, OWL_REGEX_QUOTECHARS, OWL_REGEX_QUOTEWITH);
2780    owl_text_tr(quoted, ' ', '.');
2781    sprintf(buff, "%s and instance ^%s$", buff, quoted);
2782    owl_free(quoted);
2783  }
2784  if (strcmp(recip, "*")) {
2785    quoted=owl_text_quote(recip, OWL_REGEX_QUOTECHARS, OWL_REGEX_QUOTEWITH);
2786    owl_text_tr(quoted, ' ', '.');
2787    sprintf(buff, "%s and recipient ^%s$", buff, quoted);
2788    owl_free(quoted);
2789  }
2790 
2791  owl_function_debugmsg("About to filter %s", buff);
2792  ret=owl_filter_init_fromstring(f, "punt-filter", buff);
2793  owl_free(buff);
2794  if (ret) {
2795    owl_function_error("Error creating filter for zpunt");
2796    owl_filter_free(f);
2797    return;
2798  }
2799
2800  /* Check for an identical filter */
2801  j=owl_list_get_size(fl);
2802  for (i=0; i<j; i++) {
2803    if (owl_filter_equiv(f, owl_list_get_element(fl, i))) {
2804      /* if we're punting, then just silently bow out on this duplicate */
2805      if (direction==0) {
2806        owl_filter_free(f);
2807        return;
2808      }
2809
2810      /* if we're unpunting, then remove this filter from the puntlist */
2811      if (direction==1) {
2812        owl_filter_free(owl_list_get_element(fl, i));
2813        owl_list_remove_element(fl, i);
2814        return;
2815      }
2816    }
2817  }
2818
2819  /* If we're punting, add the filter to the global punt list */
2820  if (direction==0) {
2821    owl_list_append_element(fl, f);
2822  }
2823}
2824
2825void owl_function_activate_keymap(char *keymap)
2826{
2827  if (!owl_keyhandler_activate(owl_global_get_keyhandler(&g), keymap)) {
2828    owl_function_error("Unable to activate keymap '%s'", keymap);
2829  }
2830}
2831
2832void owl_function_show_keymaps()
2833{
2834  owl_list l;
2835  owl_fmtext fm;
2836  owl_keymap *km;
2837  owl_keyhandler *kh;
2838  int i, numkm;
2839  char *kmname;
2840
2841  kh = owl_global_get_keyhandler(&g);
2842  owl_fmtext_init_null(&fm);
2843  owl_fmtext_append_bold(&fm, "Keymaps:   ");
2844  owl_fmtext_append_normal(&fm, "(use 'show keymap <name>' for details)\n");
2845  owl_keyhandler_get_keymap_names(kh, &l);
2846  owl_fmtext_append_list(&fm, &l, "\n", owl_function_keymap_summary);
2847  owl_fmtext_append_normal(&fm, "\n");
2848
2849  numkm = owl_list_get_size(&l);
2850  for (i=0; i<numkm; i++) {
2851    kmname = owl_list_get_element(&l, i);
2852    km = owl_keyhandler_get_keymap(kh, kmname);
2853    owl_fmtext_append_bold(&fm, "\n\n----------------------------------------------------------------------------------------------------\n\n");
2854    owl_keymap_get_details(km, &fm);   
2855  }
2856  owl_fmtext_append_normal(&fm, "\n");
2857 
2858  owl_function_popless_fmtext(&fm);
2859  owl_keyhandler_keymap_namelist_free(&l);
2860  owl_fmtext_free(&fm);
2861}
2862
2863char *owl_function_keymap_summary(void *name)
2864{
2865  owl_keymap *km
2866    = owl_keyhandler_get_keymap(owl_global_get_keyhandler(&g), name);
2867  if (km) return owl_keymap_summary(km);
2868  else return(NULL);
2869}
2870
2871/* TODO: implement for real */
2872void owl_function_show_keymap(char *name)
2873{
2874  owl_fmtext fm;
2875  owl_keymap *km;
2876
2877  owl_fmtext_init_null(&fm);
2878  km = owl_keyhandler_get_keymap(owl_global_get_keyhandler(&g), name);
2879  if (km) {
2880    owl_keymap_get_details(km, &fm);
2881  } else {
2882    owl_fmtext_append_normal(&fm, "No such keymap...\n");
2883  } 
2884  owl_function_popless_fmtext(&fm);
2885  owl_fmtext_free(&fm);
2886}
2887
2888void owl_function_help_for_command(char *cmdname)
2889{
2890  owl_fmtext fm;
2891
2892  owl_fmtext_init_null(&fm);
2893  owl_cmd_get_help(owl_global_get_cmddict(&g), cmdname, &fm);
2894  owl_function_popless_fmtext(&fm); 
2895  owl_fmtext_free(&fm);
2896}
2897
2898void owl_function_search_start(char *string, int direction)
2899{
2900  /* direction is OWL_DIRECTION_DOWNWARDS or OWL_DIRECTION_UPWARDS */
2901  owl_global_set_search_active(&g, string);
2902  owl_function_search_helper(0, direction);
2903}
2904
2905void owl_function_search_continue(int direction)
2906{
2907  /* direction is OWL_DIRECTION_DOWNWARDS or OWL_DIRECTION_UPWARDS */
2908  owl_function_search_helper(1, direction);
2909}
2910
2911void owl_function_search_helper(int mode, int direction)
2912{
2913  /* move to a message that contains the string.  If direction is
2914   * OWL_DIRECTION_DOWNWARDS then search fowards, if direction is
2915   * OWL_DIRECTION_UPWARDS then search backwards.
2916   *
2917   * If mode==0 then it will stay on the current message if it
2918   * contains the string.
2919   */
2920
2921  owl_view *v;
2922  int viewsize, i, curmsg, start;
2923  owl_message *m;
2924
2925  v=owl_global_get_current_view(&g);
2926  viewsize=owl_view_get_size(v);
2927  curmsg=owl_global_get_curmsg(&g);
2928 
2929  if (viewsize==0) {
2930    owl_function_error("No messages present");
2931    return;
2932  }
2933
2934  if (mode==0) {
2935    start=curmsg;
2936  } else if (direction==OWL_DIRECTION_DOWNWARDS) {
2937    start=curmsg+1;
2938  } else {
2939    start=curmsg-1;
2940  }
2941
2942  /* bounds check */
2943  if (start>=viewsize || start<0) {
2944    owl_function_error("No further matches found");
2945    return;
2946  }
2947
2948  for (i=start; i<viewsize && i>=0;) {
2949    m=owl_view_get_element(v, i);
2950    if (owl_message_search(m, owl_global_get_search_string(&g))) {
2951      owl_global_set_curmsg(&g, i);
2952      owl_function_calculate_topmsg(direction);
2953      owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2954      if (direction==OWL_DIRECTION_DOWNWARDS) {
2955        owl_global_set_direction_downwards(&g);
2956      } else {
2957        owl_global_set_direction_upwards(&g);
2958      }
2959      return;
2960    }
2961    if (direction==OWL_DIRECTION_DOWNWARDS) {
2962      i++;
2963    } else {
2964      i--;
2965    }
2966  }
2967  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2968  owl_function_error("No matches found");
2969}
2970
2971/* strips formatting from ztext and returns the unformatted text.
2972 * caller is responsible for freeing. */
2973char *owl_function_ztext_stylestrip(char *zt)
2974{
2975  owl_fmtext fm;
2976  char *plaintext;
2977
2978  owl_fmtext_init_null(&fm);
2979  owl_fmtext_append_ztext(&fm, zt);
2980  plaintext = owl_fmtext_print_plain(&fm);
2981  owl_fmtext_free(&fm);
2982  return(plaintext);
2983}
2984
2985/* Popup a buddylisting.  If file is NULL use the default .anyone */
2986void owl_function_buddylist(int aim, int zephyr, char *file)
2987{
2988  int i, j, idle;
2989  owl_fmtext fm;
2990  owl_buddylist *bl;
2991  owl_buddy *b;
2992  char *foo, *timestr;
2993#ifdef HAVE_LIBZEPHYR
2994  char *ourfile, *tmp, buff[LINE], *line;
2995  ZLocations_t location[200];
2996  FILE *f;
2997  int numlocs, ret;
2998#endif
2999
3000  owl_fmtext_init_null(&fm);
3001
3002  if (aim && owl_global_is_aimloggedin(&g)) {
3003    bl=owl_global_get_buddylist(&g);
3004
3005    owl_fmtext_append_bold(&fm, "AIM users logged in:\n");
3006    /* we're assuming AIM for now */
3007    j=owl_buddylist_get_size(bl);
3008    for (i=0; i<j; i++) {
3009      b=owl_buddylist_get_buddy_n(bl, i);
3010      idle=owl_buddy_get_idle_time(b);
3011      if (idle!=0) {
3012        timestr=owl_util_minutes_to_timestr(idle);
3013      } else {
3014        timestr=owl_strdup("");
3015      }
3016      foo=owl_sprintf("  %-20.20s %-12.12s\n",
3017                      owl_buddy_get_name(b),
3018                      timestr);
3019      owl_fmtext_append_normal(&fm, foo);
3020      owl_free(timestr);
3021      owl_free(foo);
3022    }
3023  }
3024
3025#ifdef HAVE_LIBZEPHYR
3026  if (zephyr) {
3027    if (file==NULL) {
3028      tmp=owl_global_get_homedir(&g);
3029      if (!tmp) {
3030        owl_function_error("Could not determine home directory");
3031        return;
3032      }
3033      ourfile=owl_malloc(strlen(tmp)+50);
3034      sprintf(ourfile, "%s/.anyone", owl_global_get_homedir(&g));
3035    } else {
3036      ourfile=owl_strdup(file);
3037    }
3038   
3039    f=fopen(ourfile, "r");
3040    if (!f) {
3041      owl_function_error("Error opening file %s: %s",
3042                           ourfile,
3043                           strerror(errno) ? strerror(errno) : "");
3044      return;
3045    }
3046
3047    owl_fmtext_append_bold(&fm, "Zephyr users logged in:\n");
3048   
3049    while (fgets(buff, LINE, f)!=NULL) {
3050      /* ignore comments, blank lines etc. */
3051      if (buff[0]=='#') continue;
3052      if (buff[0]=='\n') continue;
3053      if (buff[0]=='\0') continue;
3054     
3055      /* strip the \n */
3056      buff[strlen(buff)-1]='\0';
3057     
3058      /* ingore from # on */
3059      tmp=strchr(buff, '#');
3060      if (tmp) tmp[0]='\0';
3061     
3062      /* ingore from SPC */
3063      tmp=strchr(buff, ' ');
3064      if (tmp) tmp[0]='\0';
3065     
3066      /* stick on the local realm. */
3067      if (!strchr(buff, '@')) {
3068        strcat(buff, "@");
3069        strcat(buff, ZGetRealm());
3070      }
3071     
3072      ret=ZLocateUser(buff, &numlocs, ZAUTH);
3073      if (ret!=ZERR_NONE) {
3074        owl_function_error("Error getting location for %s", buff);
3075        continue;
3076      }
3077     
3078      numlocs=200;
3079      ret=ZGetLocations(location, &numlocs);
3080      if (ret==0) {
3081        for (i=0; i<numlocs; i++) {
3082          line=malloc(strlen(location[i].host)+strlen(location[i].time)+strlen(location[i].tty)+100);
3083          tmp=short_zuser(buff);
3084          sprintf(line, "  %-10.10s %-24.24s %-12.12s  %20.20s\n",
3085                  tmp,
3086                  location[i].host,
3087                  location[i].tty,
3088                  location[i].time);
3089          owl_fmtext_append_normal(&fm, line);
3090          owl_free(tmp);
3091        }
3092        if (numlocs>=200) {
3093          owl_fmtext_append_normal(&fm, "  Too many locations found for this user, truncating.\n");
3094        }
3095      }
3096    }
3097    fclose(f);
3098    owl_free(ourfile);
3099  }
3100#endif
3101 
3102  owl_function_popless_fmtext(&fm);
3103  owl_fmtext_free(&fm);
3104}
3105
3106void owl_function_dump(char *filename) 
3107{
3108  int i, j, count;
3109  owl_message *m;
3110  owl_view *v;
3111  FILE *file;
3112  /* struct stat sbuf; */
3113
3114  v=owl_global_get_current_view(&g);
3115
3116  /* in the future make it ask yes/no */
3117  /*
3118  ret=stat(filename, &sbuf);
3119  if (!ret) {
3120    ret=owl_function_askyesno("File exists, continue? [Y/n]");
3121    if (!ret) return;
3122  }
3123  */
3124
3125  file=fopen(filename, "w");
3126  if (!file) {
3127    owl_function_error("Error opening file");
3128    return;
3129  }
3130
3131  count=0;
3132  j=owl_view_get_size(v);
3133  for (i=0; i<j; i++) {
3134    m=owl_view_get_element(v, i);
3135    fputs(owl_message_get_text(m), file);
3136  }
3137  fclose(file);
3138}
3139
3140void owl_function_do_newmsgproc(void)
3141{
3142  if (owl_global_get_newmsgproc(&g) && strcmp(owl_global_get_newmsgproc(&g), "")) {
3143    /* if there's a process out there, we need to check on it */
3144    if (owl_global_get_newmsgproc_pid(&g)) {
3145      owl_function_debugmsg("Checking on newmsgproc pid==%i", owl_global_get_newmsgproc_pid(&g));
3146      owl_function_debugmsg("Waitpid return is %i", waitpid(owl_global_get_newmsgproc_pid(&g), NULL, WNOHANG));
3147      waitpid(owl_global_get_newmsgproc_pid(&g), NULL, WNOHANG);
3148      if (waitpid(owl_global_get_newmsgproc_pid(&g), NULL, WNOHANG)==-1) {
3149        /* it exited */
3150        owl_global_set_newmsgproc_pid(&g, 0);
3151        owl_function_debugmsg("newmsgproc exited");
3152      } else {
3153        owl_function_debugmsg("newmsgproc did not exit");
3154      }
3155    }
3156   
3157    /* if it exited, fork & exec a new one */
3158    if (owl_global_get_newmsgproc_pid(&g)==0) {
3159      int i, myargc;
3160      i=fork();
3161      if (i) {
3162        /* parent set the child's pid */
3163        owl_global_set_newmsgproc_pid(&g, i);
3164        owl_function_debugmsg("I'm the parent and I started a new newmsgproc with pid %i", i);
3165      } else {
3166        /* child exec's the program */
3167        char **parsed;
3168        parsed=owl_parseline(owl_global_get_newmsgproc(&g), &myargc);
3169        if (myargc < 0) {
3170          owl_function_debugmsg("Could not parse newmsgproc '%s': unbalanced quotes?", owl_global_get_newmsgproc(&g));
3171        }
3172        if (myargc <= 0) {
3173          _exit(127);
3174        }
3175        parsed=realloc(parsed, sizeof(*parsed) * (myargc+1));
3176        parsed[myargc] = NULL;
3177       
3178        owl_function_debugmsg("About to exec \"%s\" with %d arguments", parsed[0], myargc);
3179       
3180        execvp(parsed[0], parsed);
3181       
3182       
3183        /* was there an error exec'ing? */
3184        owl_function_debugmsg("Cannot run newmsgproc '%s': cannot exec '%s': %s", 
3185                              owl_global_get_newmsgproc(&g), parsed[0], strerror(errno));
3186        _exit(127);
3187      }
3188    }
3189  }
3190}
3191
3192/* print the xterm escape sequence to raise the window */
3193void owl_function_xterm_raise(void)
3194{
3195  printf("\033[5t");
3196}
3197
3198/* print the xterm escape sequence to deiconify the window */
3199void owl_function_xterm_deiconify(void)
3200{
3201  printf("\033[1t");
3202}
3203
3204/* Add the specified command to the startup file.  Eventually this
3205 * should be clever, and rewriting settings that will obviosly
3206 * override earlier settings with 'set' 'bindkey' and 'alias'
3207 * commands.  For now though we just remove any line that would
3208 * duplicate this one and then append this line to the end of
3209 * startupfile.
3210 */
3211void owl_function_addstartup(char *buff)
3212{
3213  FILE *file;
3214  char *filename;
3215
3216  filename=owl_sprintf("%s/%s", owl_global_get_homedir(&g), OWL_STARTUP_FILE);
3217  file=fopen(filename, "a");
3218  if (!file) {
3219    owl_function_error("Error opening startupfile for new command");
3220    owl_free(filename);
3221    return;
3222  }
3223
3224  /* delete earlier copies */
3225  owl_util_file_deleteline(filename, buff, 1);
3226  owl_free(filename);
3227
3228  /* add this line */
3229  fprintf(file, "%s\n", buff);
3230
3231  fclose(file);
3232}
3233
3234/* Remove the specified command from the startup file. */
3235void owl_function_delstartup(char *buff)
3236{
3237  char *filename;
3238  filename=owl_sprintf("%s/%s", owl_global_get_homedir(&g), OWL_STARTUP_FILE);
3239  owl_util_file_deleteline(filename, buff, 1);
3240  owl_free(filename);
3241}
3242
3243/* Execute owl commands from the given filename.  If the filename
3244 * is NULL, use the default owl startup commands file.
3245 */
3246void owl_function_source(char *filename)
3247{
3248  FILE *file;
3249  char buff[LINE];
3250
3251  if (!filename) {
3252    filename=owl_sprintf("%s/%s", owl_global_get_homedir(&g), OWL_STARTUP_FILE);
3253    file=fopen(filename, "r");
3254    owl_free(filename);
3255  } else {
3256    file=fopen(filename, "r");
3257  }
3258  if (!file) {
3259    /* just fail silently if it doesn't exist */
3260    return;
3261  }
3262  while (fgets(buff, LINE, file)!=NULL) {
3263    buff[strlen(buff)-1]='\0';
3264    owl_function_command(buff);
3265  }
3266  fclose(file);
3267}
3268
3269void owl_function_change_style(owl_view *v, char *stylename)
3270{
3271  owl_style *s;
3272
3273  s=owl_global_get_style_by_name(&g, stylename);
3274  if (!s) {
3275    owl_function_error("No style named %s", stylename);
3276    return;
3277  }
3278  owl_view_set_style(v, s);
3279  owl_messagelist_invalidate_formats(owl_global_get_msglist(&g));
3280  owl_function_calculate_topmsg(OWL_DIRECTION_DOWNWARDS);
3281  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
3282}
3283
3284void owl_function_toggleoneline()
3285{
3286  owl_view *v;
3287  owl_style *s;
3288
3289  v=owl_global_get_current_view(&g);
3290  s=owl_view_get_style(v);
3291
3292  if (!owl_style_matches_name(s, "oneline")) {
3293    owl_function_change_style(v, "oneline");
3294  } else {
3295    owl_function_change_style(v, owl_global_get_default_style(&g));
3296  }
3297
3298  owl_messagelist_invalidate_formats(owl_global_get_msglist(&g));
3299  owl_function_calculate_topmsg(OWL_DIRECTION_DOWNWARDS);
3300  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
3301}
3302
3303void owl_function_error(char *fmt, ...)
3304{
3305  va_list ap;
3306  char buff[2048], buff2[2048];
3307  char *date;
3308  time_t now;
3309
3310  now=time(NULL);
3311  date=owl_strdup(ctime(&now));
3312  date[strlen(date)-1]='\0';
3313
3314  va_start(ap, fmt);
3315
3316  vsnprintf(buff, 2048, fmt, ap);
3317  sprintf(buff2, "%s %s", date, buff);
3318  owl_function_debugmsg("ERROR: %s", buff);
3319  if (owl_global_get_curs_msgwin(&g)) {
3320    werase(owl_global_get_curs_msgwin(&g));
3321    waddstr(owl_global_get_curs_msgwin(&g), buff); 
3322    wnoutrefresh(owl_global_get_curs_msgwin(&g));
3323    owl_global_set_needrefresh(&g);
3324  }
3325  owl_errqueue_append_err(owl_global_get_errqueue(&g), buff2);
3326  va_end(ap);
3327  owl_free(date);
3328}
3329
3330void owl_function_showerrs()
3331{
3332  owl_fmtext fm;
3333
3334  owl_fmtext_init_null(&fm);
3335  owl_fmtext_append_normal(&fm, "Errors:\n\n");
3336  owl_errqueue_to_fmtext(owl_global_get_errqueue(&g), &fm);
3337  owl_function_popless_fmtext(&fm);
3338}
3339
3340void owl_function_makemsg(char *fmt, ...)
3341{
3342  va_list ap;
3343  char buff[2048];
3344
3345  if (!owl_global_get_curs_msgwin(&g)) return;
3346
3347  va_start(ap, fmt);
3348  werase(owl_global_get_curs_msgwin(&g));
3349 
3350  vsnprintf(buff, 2048, fmt, ap);
3351  owl_function_debugmsg("makemsg: %s", buff);
3352  waddstr(owl_global_get_curs_msgwin(&g), buff); 
3353  wnoutrefresh(owl_global_get_curs_msgwin(&g));
3354  owl_global_set_needrefresh(&g);
3355  va_end(ap);
3356}
Note: See TracBrowser for help on using the repository browser.