source: functions.c @ ad37b39

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