source: functions.c @ 0982901

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