source: functions.c @ 7b4f3be

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