source: functions.c @ 6ddeb17

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