source: functions.c @ 9620c8d

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