source: functions.c @ f166580

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