source: functions.c @ 39a531d

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