source: functions.c @ 488ebf6

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