source: functions.c @ 212d8b7

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