source: functions.c @ 7b1d048

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