source: functions.c @ e22f27c

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