source: functions.c @ 7abfcf2

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