source: functions.c @ 340c3e7

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