source: functions.c @ 04166e9

barnowl_perlaimdebianrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 04166e9 was ef9438a, checked in by Nelson Elhage <nelhage@mit.edu>, 17 years ago
When we're narrowing to an instance, properly include un-instances. This fixes narrowing to any instance that starts with ``un-''
  • Property mode set to 100644
File size: 99.4 KB
Line 
1#include <stdio.h>
2#include <stdlib.h>
3#include <unistd.h>
4#include <signal.h>
5#include <netinet/in.h>
6#include <string.h>
7#include <time.h>
8#include <sys/types.h>
9#include <sys/stat.h>
10#include <sys/wait.h>
11#include <errno.h>
12#include <signal.h>
13#include "owl.h"
14
15static const char fileIdent[] = "$Id$";
16
17void owl_function_noop(void)
18{
19  return;
20}
21
22char *owl_function_command(char *cmdbuff)
23{
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
29void owl_function_command_norv(char *cmdbuff)
30{
31  char *rv;
32  rv=owl_function_command(cmdbuff);
33  if (rv) owl_free(rv);
34}
35
36void owl_function_command_alias(char *alias_from, char *alias_to)
37{
38  owl_cmddict_add_alias(owl_global_get_cmddict(&g), alias_from, alias_to);
39}
40
41owl_cmd *owl_function_get_cmd(char *name)
42{
43  return owl_cmddict_find(owl_global_get_cmddict(&g), name);
44}
45
46void owl_function_show_commands()
47{
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
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))) {
70    owl_function_error("No view named '%s'", viewname);
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
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}
108
109char *owl_function_cmd_describe(void *name)
110{
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
116void owl_function_show_command(char *name)
117{
118  owl_function_help_for_command(name);
119}
120
121void owl_function_show_license()
122{
123  char *text;
124
125  text=""
126    "barnowl version " OWL_VERSION_STRING "\n"
127    "Copyright (c) 2004 James Kretchmar. All rights reserved.\n"
128    "\n"
129    "Redistribution and use in source and binary forms, with or without\n"
130    "modification, are permitted provided that the following conditions are\n"
131    "met:\n"
132    "\n"
133    "   * Redistributions of source code must retain the above copyright\n"
134    "     notice, this list of conditions and the following disclaimer.\n"
135    "\n"
136    "   * Redistributions in binary form must reproduce the above copyright\n"
137    "     notice, this list of conditions and the following disclaimer in\n"
138    "     the documentation and/or other materials provided with the\n"
139    "     distribution.\n"
140    "\n"
141    "   * Redistributions in any form must be accompanied by information on\n"
142    "     how to obtain complete source code for the Owl software and any\n"
143    "     accompanying software that uses the Owl software. The source code\n"
144    "     must either be included in the distribution or be available for no\n"
145    "     more than the cost of distribution plus a nominal fee, and must be\n"
146    "     freely redistributable under reasonable conditions. For an\n"
147    "     executable file, complete source code means the source code for\n"
148    "     all modules it contains. It does not include source code for\n"
149    "     modules or files that typically accompany the major components of\n"
150    "     the operating system on which the executable file runs.\n"
151    "\n"
152    "THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR\n"
153    "IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\n"
154    "WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR\n"
155    "NON-INFRINGEMENT, ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE\n"
156    "LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n"
157    "CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n"
158    "SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR\n"
159    "BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,\n"
160    "WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n"
161    "OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN\n"
162    "IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n";
163  owl_function_popless_text(text);
164}
165
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)
170{
171  owl_message *m;
172
173  m=owl_malloc(sizeof(owl_message));
174  owl_message_create_admin(m, header, body);
175 
176  /* add it to the global list and current view */
177  owl_messagelist_append_element(owl_global_get_msglist(&g), m);
178  owl_view_consider_message(owl_global_get_current_view(&g), m);
179
180  /* do followlast if necessary */
181  if (owl_global_should_followlast(&g)) owl_function_lastmsg_noredisplay();
182
183  /* redisplay etc. */
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
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)
197{
198  owl_message *m;
199  owl_zwrite z;
200 
201  /* create a zwrite for the purpose of filling in other message fields */
202  owl_zwrite_create_from_line(&z, zwriteline);
203
204  /* create the message */
205  m=owl_malloc(sizeof(owl_message));
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));
225  owl_message_create_aim(m,
226                         owl_global_get_aim_screenname(&g),
227                         to,
228                         body,
229                         OWL_MESSAGE_DIRECTION_OUT,
230                         0);
231  return(m);
232}
233
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)
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
247  return(m);
248}
249
250void owl_function_zwrite_setup(char *line)
251{
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) {
260    owl_function_error("Error in zwrite arugments");
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);
273  owl_editwin_new_style(e, OWL_EDITWIN_STYLE_MULTILINE, owl_global_get_msg_history(&g));
274
275  if (!owl_global_get_lockout_ctrld(&g)) {
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);
290
291  owl_global_set_buffercommand(&g, line);
292  owl_global_set_buffercallback(&g, &owl_callback_zwrite);
293}
294
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);
321
322  owl_global_set_buffercommand(&g, line);
323  owl_global_set_buffercallback(&g, &owl_callback_aimwrite);
324}
325
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");
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));
354}
355
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)
360{
361  owl_zwrite z;
362  char *mymsg;
363  owl_message *m;
364
365  if(!strncmp(line, "zcrypt", strlen("zcrypt"))) {
366    owl_function_zcrypt(line, msg);
367    return;
368  }
369
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);
374  }
375  owl_zwrite_send_message(&z);
376  owl_function_makemsg("Waiting for ack...");
377
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));
383
384    if (m) {
385      owl_global_messagequeue_addmsg(&g, m);
386    } else {
387      owl_function_error("Could not create outgoing zephyr message");
388    }
389  }
390
391  /* free the zwrite */
392  owl_zwrite_free(&z);
393}
394
395/* send, log and display an outgoing zcrypt zephyr.  If 'msg' is NULL
396 * the message is expected to be set from the zwrite line itself
397 */
398void owl_function_zcrypt(char *line, char *msg)
399{
400  owl_zwrite z;
401  char *mymsg;
402  char *cryptmsg;
403  owl_message *m;
404#ifdef OWL_ENABLE_ZCRYPT
405  int ret;
406#endif
407
408  /* create the zwrite and send the message */
409  owl_zwrite_create_from_line(&z, line);
410  if (msg) {
411    owl_zwrite_set_message(&z, msg);
412  }
413
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) {
419    owl_function_error("Error in zcrypt, possibly no key found.  Message not sent.");
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   
432  owl_zwrite_send_message(&z);
433  owl_function_makemsg("Waiting for ack...");
434
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));
440    if (m) {
441      owl_global_messagequeue_addmsg(&g, m);
442    } else {
443      owl_function_error("Could not create outgoing zephyr message");
444    }
445  }
446
447  /* free the zwrite */
448  owl_free(cryptmsg);
449  owl_zwrite_free(&z);
450}
451
452void owl_callback_aimwrite(owl_editwin *e) {
453  owl_function_aimwrite(owl_editwin_get_command(e),
454                        owl_editwin_get_text(e));
455}
456
457void owl_function_aimwrite(char *line, char *msg)
458{
459  int ret;
460  char *to, *format_msg;
461  owl_message *m;
462
463  to = line + 9;
464
465  /* make a formatted copy of the message */
466  format_msg=owl_strdup(msg);
467  owl_text_wordunwrap(format_msg);
468 
469  /* send the message */
470  ret=owl_aim_send_im(to, format_msg);
471  if (!ret) {
472    owl_function_makemsg("AIM message sent.");
473  } else {
474    owl_function_error("Could not send AIM message.");
475  }
476
477  /* create the outgoing message */
478  m=owl_function_make_outgoing_aim(msg, to);
479
480  if (m) {
481    owl_global_messagequeue_addmsg(&g, m);
482  } else {
483    owl_function_error("Could not create outgoing AIM message");
484  }
485
486  owl_free(format_msg);
487}
488
489void owl_function_send_aimawymsg(char *to, char *msg)
490{
491  int ret;
492  char *format_msg;
493  owl_message *m;
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
507  /* create the message */
508  m=owl_function_make_outgoing_aim(msg, to);
509  if (m) {
510    owl_global_messagequeue_addmsg(&g, m);
511  } else {
512    owl_function_error("Could not create AIM message");
513  }
514  owl_free(format_msg);
515}
516
517void owl_callback_loopwrite(owl_editwin *e) {
518  owl_function_loopwrite(owl_editwin_get_text(e));
519}
520
521void owl_function_loopwrite(char *msg)
522{
523  owl_message *min, *mout;
524
525  /* create a message and put it on the message queue.  This simulates
526   * an incoming message */
527  min=owl_malloc(sizeof(owl_message));
528  mout=owl_function_make_outgoing_loopback(msg);
529
530  if (owl_global_is_displayoutgoing(&g)) {
531    owl_global_messagequeue_addmsg(&g, mout);
532  } else {
533    owl_message_free(mout);
534  }
535
536  owl_message_create_loopback(min, msg);
537  owl_message_set_direction_in(min);
538  owl_global_messagequeue_addmsg(&g, min);
539
540  /* fake a makemsg */
541  owl_function_makemsg("loopback message sent");
542}
543
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.  */
548void owl_function_nextmsg_full(char *filter, int skip_deleted, int last_if_none)
549{
550  int curmsg, i, viewsize, found;
551  owl_view *v;
552  owl_filter *f = NULL;
553  owl_message *m;
554
555  v=owl_global_get_current_view(&g);
556
557  if (filter) {
558    f=owl_global_get_filter(&g, filter);
559    if (!f) {
560      owl_function_error("No %s filter defined", filter);
561      return;
562    }
563  }
564
565  curmsg=owl_global_get_curmsg(&g);
566  viewsize=owl_view_get_size(v);
567  found=0;
568
569  /* just check to make sure we're in bounds... */
570  if (curmsg>viewsize-1) curmsg=viewsize-1;
571  if (curmsg<0) curmsg=0;
572
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;
582  if (i<0) i=0;
583
584  if (!found) {
585    owl_function_makemsg("already at last%s message%s%s",
586                         skip_deleted?" non-deleted":"",
587                         filter?" in ":"", filter?filter:"");
588    /* if (!skip_deleted) owl_function_beep(); */
589  }
590
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}
598
599void owl_function_prevmsg_full(char *filter, int skip_deleted, int first_if_none)
600{
601  int curmsg, i, viewsize, found;
602  owl_view *v;
603  owl_filter *f = NULL;
604  owl_message *m;
605
606  v=owl_global_get_current_view(&g);
607
608  if (filter) {
609    f=owl_global_get_filter(&g, filter);
610    if (!f) {
611      owl_function_error("No %s filter defined", filter);
612      return;
613    }
614  }
615
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) {
634    owl_function_makemsg("already at first%s message%s%s",
635                         skip_deleted?" non-deleted":"",
636                         filter?" in ":"", filter?filter:"");
637    /* if (!skip_deleted) owl_function_beep(); */
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);
645  }
646}
647
648void owl_function_nextmsg()
649{
650  owl_function_nextmsg_full(NULL, 0, 1);
651}
652
653void owl_function_prevmsg()
654{
655  owl_function_prevmsg_full(NULL, 0, 1);
656}
657
658void owl_function_nextmsg_notdeleted()
659{
660  owl_function_nextmsg_full(NULL, 1, 1);
661}
662
663void owl_function_prevmsg_notdeleted()
664{
665  owl_function_prevmsg_full(NULL, 1, 1);
666}
667
668void owl_function_nextmsg_personal()
669{
670  owl_function_nextmsg_full("personal", 0, 0);
671}
672
673void owl_function_prevmsg_personal()
674{
675  owl_function_prevmsg_full("personal", 0, 0);
676}
677
678
679/* if move_after is 1, moves after the delete */
680void owl_function_deletecur(int move_after)
681{
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) {
689    owl_function_error("No current message to delete");
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
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    }
705  }
706}
707
708void owl_function_undeletecur(int move_after)
709{
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) {
716    owl_function_error("No current message to undelete");
717    return;
718  }
719  curmsg=owl_global_get_curmsg(&g);
720
721  owl_view_undelete_element(v, curmsg);
722
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      }
730    } else {
731      owl_function_nextmsg();
732    }
733  }
734
735  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
736}
737
738void owl_function_expunge()
739{
740  int curmsg;
741  owl_message *m;
742  owl_messagelist *ml;
743  owl_view *v;
744  int lastmsgid=0;
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);
751  if (m) lastmsgid = owl_message_get_id(m);
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
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);
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
774void owl_function_firstmsg()
775{
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
782void owl_function_lastmsg_noredisplay()
783{
784  int oldcurmsg, curmsg;
785  owl_view *v;
786
787  v=owl_global_get_current_view(&g);
788  oldcurmsg=owl_global_get_curmsg(&g);
789  curmsg=owl_view_get_size(v)-1; 
790  if (curmsg<0) curmsg=0;
791  owl_global_set_curmsg(&g, curmsg);
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  } 
800  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
801  owl_global_set_direction_downwards(&g);
802}
803
804void owl_function_lastmsg()
805{
806  owl_function_lastmsg_noredisplay();
807  owl_mainwin_redisplay(owl_global_get_mainwin(&g)); 
808}
809
810void owl_function_shift_right()
811{
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
817void owl_function_shift_left()
818{
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();
828    owl_function_makemsg("Already full left");
829  }
830}
831
832void owl_function_unsuball()
833{
834  unsuball();
835  owl_function_makemsg("Unsubscribed from all messages.");
836}
837
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 */
848void owl_function_loadsubs(char *file)
849{
850  int ret, ret2;
851  char *foo;
852
853  if (file==NULL) {
854    ret=owl_zephyr_loadsubs(NULL, 0);
855  } else {
856    ret=owl_zephyr_loadsubs(file, 1);
857  }
858
859  /* for backwards compatibility for now */
860  ret2=owl_zephyr_loaddefaultsubs();
861
862  if (!owl_context_is_interactive(owl_global_get_context(&g))) return;
863
864  foo=file?file:"file";
865  if (ret==0 && ret2==0) {
866    if (!file) {
867      owl_function_makemsg("Subscribed to messages.");
868    } else {
869      owl_function_makemsg("Subscribed to messages from %s", file);
870    }
871  } else if (ret==-1) {
872    owl_function_error("Could not read %s", foo);
873  } else {
874    owl_function_error("Error subscribing to messages");
875  }
876}
877
878void owl_function_loadloginsubs(char *file)
879{
880  int ret;
881
882  ret=owl_zephyr_loadloginsubs(file);
883
884  if (!owl_context_is_interactive(owl_global_get_context(&g))) return;
885  if (ret==0) {
886    owl_function_makemsg("Subscribed to login messages from file.");
887  } else if (ret==-1) {
888    owl_function_error("Could not open file for login subscriptions.");
889  } else {
890    owl_function_error("Error subscribing to login messages from file.");
891  }
892}
893
894void owl_callback_aimlogin(owl_editwin *e) {
895  owl_function_aimlogin(owl_editwin_get_command(e),
896                        owl_editwin_get_text(e));
897}
898
899void owl_function_aimlogin(char *user, char *passwd) {
900  int ret;
901
902  /* clear the buddylist */
903  owl_buddylist_clear(owl_global_get_buddylist(&g));
904
905  /* try to login */
906  ret=owl_aim_login(user, passwd);
907  if (ret) owl_function_makemsg("Warning: login for %s failed.\n", user);
908}
909
910void owl_function_suspend()
911{
912  endwin();
913  printf("\n");
914  kill(getpid(), SIGSTOP);
915
916  /* resize to reinitialize all the windows when we come back */
917  owl_command_resize();
918}
919
920void owl_function_zaway_toggle()
921{
922  if (!owl_global_is_zaway(&g)) {
923    owl_global_set_zaway_msg(&g, owl_global_get_zaway_msg_default(&g));
924    owl_function_zaway_on();
925  } else {
926    owl_function_zaway_off();
927  }
928}
929
930void owl_function_zaway_on()
931{
932  owl_global_set_zaway_on(&g);
933  owl_function_makemsg("zaway set (%s)", owl_global_get_zaway_msg(&g));
934}
935
936void owl_function_zaway_off()
937{
938  owl_global_set_zaway_off(&g);
939  owl_function_makemsg("zaway off");
940}
941
942void owl_function_aaway_toggle()
943{
944  if (!owl_global_is_aaway(&g)) {
945    owl_global_set_aaway_msg(&g, owl_global_get_aaway_msg_default(&g));
946    owl_function_aaway_on();
947  } else {
948    owl_function_aaway_off();
949  }
950}
951
952void owl_function_aaway_on()
953{
954  owl_global_set_aaway_on(&g);
955  /* owl_aim_set_awaymsg(owl_global_get_zaway_msg(&g)); */
956  owl_function_makemsg("AIM away set (%s)", owl_global_get_aaway_msg(&g));
957}
958
959void owl_function_aaway_off()
960{
961  owl_global_set_aaway_off(&g);
962  /* owl_aim_set_awaymsg(""); */
963  owl_function_makemsg("AIM away off");
964}
965
966void owl_function_quit()
967{
968  char *ret;
969 
970  /* zlog out if we need to */
971  if (owl_global_is_shutdownlogout(&g)) {
972    owl_zephyr_zlog_out();
973  }
974
975  /* execute the commands in shutdown */
976  ret = owl_perlconfig_execute("BarnOwl::Hooks::_shutdown();");
977  if (ret) owl_free(ret);
978
979  /* signal our child process, if any */
980  if (owl_global_get_newmsgproc_pid(&g)) {
981    kill(owl_global_get_newmsgproc_pid(&g), SIGHUP);
982  }
983
984  /* Quit zephyr */
985  owl_zephyr_shutdown();
986 
987  /* Quit AIM */
988  if (owl_global_is_aimloggedin(&g)) {
989    owl_aim_logout();
990  }
991
992  /* done with curses */
993  endwin();
994
995  /* restore terminal settings */
996  tcsetattr(0, TCSAFLUSH, owl_global_get_startup_tio(&g));
997
998  owl_function_debugmsg("Quitting Owl");
999  exit(0);
1000}
1001
1002void owl_function_openurl()
1003{
1004  /* visit the first url in the current message */
1005  owl_message *m;
1006  owl_view *v;
1007  char *ptr1, *ptr2, *text, url[LINE], tmpbuff[LINE];
1008  int webbrowser;
1009
1010  webbrowser = owl_global_get_webbrowser(&g);
1011
1012  if (webbrowser < 0 || webbrowser == OWL_WEBBROWSER_NONE) {
1013    owl_function_error("No browser selected");
1014    return;
1015  }
1016
1017  v=owl_global_get_current_view(&g);
1018 
1019  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
1020
1021  if (!m || owl_view_get_size(v)==0) {
1022    owl_function_error("No current message selected");
1023    return;
1024  }
1025
1026  text=owl_message_get_text(m);
1027
1028  /* First look for a good URL */ 
1029  if ((ptr1=strstr(text, "http://"))!=NULL) {
1030    ptr2=strpbrk(ptr1, " \n\t");
1031    if (ptr2) {
1032      strncpy(url, ptr1, ptr2-ptr1+1);
1033      url[ptr2-ptr1+1]='\0';
1034    } else {
1035      strcpy(url, ptr1);
1036    }
1037
1038    /* if we had <http strip a trailing > */
1039    if (ptr1>text && ptr1[-1]=='<') {
1040      if (url[strlen(url)-1]=='>') {
1041        url[strlen(url)-1]='\0';
1042      }
1043    }
1044  } else if ((ptr1=strstr(text, "https://"))!=NULL) {
1045    /* Look for an https URL */ 
1046    ptr2=strpbrk(ptr1, " \n\t");
1047    if (ptr2) {
1048      strncpy(url, ptr1, ptr2-ptr1+1);
1049      url[ptr2-ptr1+1]='\0';
1050    } else {
1051      strcpy(url, ptr1);
1052    }
1053   
1054    /* if we had <http strip a trailing > */
1055    if (ptr1>text && ptr1[-1]=='<') {
1056      if (url[strlen(url)-1]=='>') {
1057        url[strlen(url)-1]='\0';
1058      }
1059    }
1060  } else if ((ptr1=strstr(text, "www."))!=NULL) {
1061    /* if we can't find a real url look for www.something */
1062    ptr2=strpbrk(ptr1, " \n\t");
1063    if (ptr2) {
1064      strncpy(url, ptr1, ptr2-ptr1+1);
1065      url[ptr2-ptr1+1]='\0';
1066    } else {
1067      strcpy(url, ptr1);
1068    }
1069  } else {
1070    owl_function_beep();
1071    owl_function_error("Could not find URL to open.");
1072    return;
1073  }
1074
1075  /* Make sure there aren't any quotes or \'s in the url */
1076  for (ptr1 = url; *ptr1; ptr1++) {
1077    if (*ptr1 == '"' || *ptr1 == '\\') {
1078      owl_function_beep();
1079      owl_function_error("URL contains invalid characters.");
1080      return;
1081    }
1082  }
1083 
1084  /* NOTE: There are potentially serious security issues here... */
1085
1086  /* open the page */
1087  owl_function_makemsg("Opening %s", url);
1088  if (webbrowser == OWL_WEBBROWSER_NETSCAPE) {
1089    snprintf(tmpbuff, LINE, "netscape -remote \"openURL(%s)\" > /dev/null 2> /dev/null", url);
1090    system(tmpbuff); 
1091  } else if (webbrowser == OWL_WEBBROWSER_GALEON) {
1092    snprintf(tmpbuff, LINE, "galeon \"%s\" > /dev/null 2> /dev/null &", url);
1093    system(tmpbuff); 
1094  } else if (webbrowser == OWL_WEBBROWSER_OPERA) {
1095    snprintf(tmpbuff, LINE, "opera \"%s\" > /dev/null 2> /dev/null &", url);
1096    system(tmpbuff); 
1097  }
1098}
1099
1100void owl_function_calculate_topmsg(int direction)
1101{
1102  int recwinlines, topmsg, curmsg;
1103  owl_view *v;
1104
1105  v=owl_global_get_current_view(&g);
1106  curmsg=owl_global_get_curmsg(&g);
1107  topmsg=owl_global_get_topmsg(&g);
1108  recwinlines=owl_global_get_recwin_lines(&g);
1109
1110  /*
1111  if (owl_view_get_size(v) < 1) {
1112    return;
1113  }
1114  */
1115
1116  switch (owl_global_get_scrollmode(&g)) {
1117  case OWL_SCROLLMODE_TOP:
1118    topmsg = owl_function_calculate_topmsg_top(direction, v, curmsg, topmsg, recwinlines);
1119    break;
1120  case OWL_SCROLLMODE_NEARTOP:
1121    topmsg = owl_function_calculate_topmsg_neartop(direction, v, curmsg, topmsg, recwinlines);
1122    break;
1123  case OWL_SCROLLMODE_CENTER:
1124    topmsg = owl_function_calculate_topmsg_center(direction, v, curmsg, topmsg, recwinlines);
1125    break;
1126  case OWL_SCROLLMODE_PAGED:
1127    topmsg = owl_function_calculate_topmsg_paged(direction, v, curmsg, topmsg, recwinlines, 0);
1128    break;
1129  case OWL_SCROLLMODE_PAGEDCENTER:
1130    topmsg = owl_function_calculate_topmsg_paged(direction, v, curmsg, topmsg, recwinlines, 1);
1131    break;
1132  case OWL_SCROLLMODE_NORMAL:
1133  default:
1134    topmsg = owl_function_calculate_topmsg_normal(direction, v, curmsg, topmsg, recwinlines);
1135  }
1136  owl_function_debugmsg("Calculated a topmsg of %i", topmsg);
1137  owl_global_set_topmsg(&g, topmsg);
1138}
1139
1140/* Returns what the new topmsg should be. 
1141 * Passed the last direction of movement,
1142 * the current view,
1143 * the current message number in the view,
1144 * the top message currently being displayed,
1145 * and the number of lines in the recwin.
1146 */
1147int owl_function_calculate_topmsg_top(int direction, owl_view *v, int curmsg, int topmsg, int recwinlines)
1148{
1149  return(curmsg);
1150}
1151
1152int owl_function_calculate_topmsg_neartop(int direction, owl_view *v, int curmsg, int topmsg, int recwinlines)
1153{
1154  if (curmsg>0 
1155      && (owl_message_get_numlines(owl_view_get_element(v, curmsg-1))
1156          <  recwinlines/2)) {
1157    return(curmsg-1);
1158  } else {
1159    return(curmsg);
1160  }
1161}
1162 
1163int owl_function_calculate_topmsg_center(int direction, owl_view *v, int curmsg, int topmsg, int recwinlines)
1164{
1165  int i, last, lines;
1166
1167  last = curmsg;
1168  lines = 0;
1169  for (i=curmsg-1; i>=0; i--) {
1170    lines += owl_message_get_numlines(owl_view_get_element(v, i));
1171    if (lines > recwinlines/2) break;
1172    last = i;
1173  }
1174  return(last);
1175}
1176 
1177int owl_function_calculate_topmsg_paged(int direction, owl_view *v, int curmsg, int topmsg, int recwinlines, int center_on_page)
1178{
1179  int i, last, lines, savey;
1180 
1181  /* If we're off the top of the screen, scroll up such that the
1182   * curmsg is near the botton of the screen. */
1183  if (curmsg < topmsg) {
1184    last = curmsg;
1185    lines = 0;
1186    for (i=curmsg; i>=0; i--) {
1187      lines += owl_message_get_numlines(owl_view_get_element(v, i));
1188      if (lines > recwinlines) break;
1189    last = i;
1190    }
1191    if (center_on_page) {
1192      return(owl_function_calculate_topmsg_center(direction, v, curmsg, 0, recwinlines));
1193    } else {
1194      return(last);
1195    }
1196  }
1197
1198  /* Find number of lines from top to bottom of curmsg (store in savey) */
1199  savey=0;
1200  for (i=topmsg; i<=curmsg; i++) {
1201    savey+=owl_message_get_numlines(owl_view_get_element(v, i));
1202  }
1203
1204  /* if we're off the bottom of the screen, scroll down */
1205  if (savey > recwinlines) {
1206    if (center_on_page) {
1207      return(owl_function_calculate_topmsg_center(direction, v, curmsg, 0, recwinlines));
1208    } else {
1209      return(curmsg);
1210    }
1211  }
1212
1213  /* else just stay as we are... */
1214  return(topmsg);
1215}
1216
1217int owl_function_calculate_topmsg_normal(int direction, owl_view *v, int curmsg, int topmsg, int recwinlines)
1218{
1219  int savey, i, foo, y;
1220
1221  if (curmsg<0) return(topmsg);
1222   
1223  /* If we're off the top of the screen then center */
1224  if (curmsg<topmsg) {
1225    topmsg=owl_function_calculate_topmsg_center(direction, v, curmsg, 0, recwinlines);
1226  }
1227
1228  /* If curmsg is so far past topmsg that there are more messages than
1229     lines, skip the line counting that follows because we're
1230     certainly off screen.  */
1231  savey=curmsg-topmsg;
1232  if (savey <= recwinlines) {
1233    /* Find number of lines from top to bottom of curmsg (store in savey) */
1234    savey = 0;
1235    for (i=topmsg; i<=curmsg; i++) {
1236      savey+=owl_message_get_numlines(owl_view_get_element(v, i));
1237    }
1238  }
1239
1240  /* If we're off the bottom of the screen, set the topmsg to curmsg
1241   * and scroll upwards */
1242  if (savey > recwinlines) {
1243    topmsg=curmsg;
1244    savey=owl_message_get_numlines(owl_view_get_element(v, curmsg));
1245    direction=OWL_DIRECTION_UPWARDS;
1246  }
1247 
1248  /* If our bottom line is less than 1/4 down the screen then scroll up */
1249  if (direction == OWL_DIRECTION_UPWARDS || direction == OWL_DIRECTION_NONE) {
1250    if (savey < (recwinlines / 4)) {
1251      y=0;
1252      for (i=curmsg; i>=0; i--) {
1253        foo=owl_message_get_numlines(owl_view_get_element(v, i));
1254        /* will we run the curmsg off the screen? */
1255        if ((foo+y) >= recwinlines) {
1256          i++;
1257          if (i>curmsg) i=curmsg;
1258          break;
1259        }
1260        /* have saved 1/2 the screen space? */
1261        y+=foo;
1262        if (y > (recwinlines / 2)) break;
1263      }
1264      if (i<0) i=0;
1265      return(i);
1266    }
1267  }
1268
1269  if (direction == OWL_DIRECTION_DOWNWARDS || direction == OWL_DIRECTION_NONE) {
1270    /* If curmsg bottom line is more than 3/4 down the screen then scroll down */
1271    if (savey > ((recwinlines * 3)/4)) {
1272      y=0;
1273      /* count lines from the top until we can save 1/2 the screen size */
1274      for (i=topmsg; i<curmsg; i++) {
1275        y+=owl_message_get_numlines(owl_view_get_element(v, i));
1276        if (y > (recwinlines / 2)) break;
1277      }
1278      if (i==curmsg) {
1279        i--;
1280      }
1281      return(i+1);
1282    }
1283  }
1284
1285  return(topmsg);
1286}
1287
1288void owl_function_resize()
1289{
1290  owl_global_set_resize_pending(&g);
1291}
1292
1293void owl_function_run_buffercommand()
1294{
1295  owl_editwin_do_callback(owl_global_get_typwin(&g));
1296}
1297
1298void owl_function_debugmsg(char *fmt, ...)
1299{
1300  FILE *file;
1301  time_t now;
1302  char buff1[LINE], buff2[LINE];
1303  va_list ap;
1304  va_start(ap, fmt);
1305
1306  if (!owl_global_is_debug_fast(&g)) return;
1307
1308  file=fopen(owl_global_get_debug_file(&g), "a");
1309  if (!file) return;
1310
1311  now=time(NULL);
1312  strcpy(buff1, ctime(&now));
1313  buff1[strlen(buff1)-1]='\0';
1314
1315  owl_global_get_runtime_string(&g, buff2);
1316 
1317  fprintf(file, "[%i -  %s - %s]: ", (int) getpid(), buff1, buff2);
1318  vfprintf(file, fmt, ap);
1319  fprintf(file, "\n");
1320  fclose(file);
1321
1322  va_end(ap);
1323}
1324
1325void owl_function_refresh()
1326{
1327  owl_function_resize();
1328}
1329
1330void owl_function_beep()
1331{
1332  if (owl_global_is_bell(&g)) {
1333    beep();
1334    owl_global_set_needrefresh(&g); /* do we really need this? */
1335  }
1336}
1337
1338void owl_function_subscribe(char *class, char *inst, char *recip)
1339{
1340  int ret;
1341
1342  ret=owl_zephyr_sub(class, inst, recip);
1343  if (ret) {
1344    owl_function_error("Error subscribing.");
1345  } else {
1346    owl_function_makemsg("Subscribed.");
1347  }
1348}
1349
1350void owl_function_unsubscribe(char *class, char *inst, char *recip)
1351{
1352  int ret;
1353
1354  ret=owl_zephyr_unsub(class, inst, recip);
1355  if (ret) {
1356    owl_function_error("Error subscribing.");
1357  } else {
1358    owl_function_makemsg("Unsubscribed.");
1359  }
1360}
1361
1362void owl_function_set_cursor(WINDOW *win)
1363{
1364  wnoutrefresh(win);
1365}
1366
1367void owl_function_full_redisplay()
1368{
1369  redrawwin(owl_global_get_curs_recwin(&g));
1370  redrawwin(owl_global_get_curs_sepwin(&g));
1371  // Work around curses segfualts with windows off the screen
1372  if (g.lines >= owl_global_get_typwin_lines(&g)+2)
1373      redrawwin(owl_global_get_curs_typwin(&g));
1374  if (g.lines >= 2)
1375      redrawwin(owl_global_get_curs_msgwin(&g));
1376
1377  wnoutrefresh(owl_global_get_curs_recwin(&g));
1378  wnoutrefresh(owl_global_get_curs_sepwin(&g));
1379  wnoutrefresh(owl_global_get_curs_typwin(&g));
1380  wnoutrefresh(owl_global_get_curs_msgwin(&g));
1381
1382  if (owl_popwin_is_active(owl_global_get_popwin(&g))) {
1383    owl_popwin_refresh(owl_global_get_popwin(&g));
1384  }
1385 
1386  sepbar("");
1387  owl_function_makemsg("");
1388
1389  owl_global_set_needrefresh(&g);
1390}
1391
1392void owl_function_popless_text(char *text)
1393{
1394  owl_popwin *pw;
1395  owl_viewwin *v;
1396
1397  pw=owl_global_get_popwin(&g);
1398  v=owl_global_get_viewwin(&g);
1399
1400  owl_popwin_up(pw);
1401  owl_viewwin_init_text(v, owl_popwin_get_curswin(pw),
1402                        owl_popwin_get_lines(pw), owl_popwin_get_cols(pw),
1403                        text);
1404  owl_popwin_refresh(pw);
1405  owl_viewwin_redisplay(v, 0);
1406  owl_global_set_needrefresh(&g);
1407}
1408
1409void owl_function_popless_fmtext(owl_fmtext *fm)
1410{
1411  owl_popwin *pw;
1412  owl_viewwin *v;
1413
1414  pw=owl_global_get_popwin(&g);
1415  v=owl_global_get_viewwin(&g);
1416
1417  owl_popwin_up(pw);
1418  owl_viewwin_init_fmtext(v, owl_popwin_get_curswin(pw),
1419                   owl_popwin_get_lines(pw), owl_popwin_get_cols(pw),
1420                   fm);
1421  owl_popwin_refresh(pw);
1422  owl_viewwin_redisplay(v, 0);
1423  owl_global_set_needrefresh(&g);
1424}
1425
1426void owl_function_popless_file(char *filename)
1427{
1428  owl_fmtext fm;
1429  FILE *file;
1430  char buff[1024];
1431
1432  file=fopen(filename, "r");
1433  if (!file) {
1434    owl_function_error("Could not open file: %s", filename);
1435    return;
1436  }
1437
1438  owl_fmtext_init_null(&fm);
1439  while (fgets(buff, 1024, file)) {
1440    owl_fmtext_append_normal(&fm, buff);
1441    /*    owl_fmtext_append_normal(&fm, "\n"); */
1442  }
1443
1444  owl_function_popless_fmtext(&fm);
1445  owl_fmtext_free(&fm);
1446  fclose(file);
1447}
1448
1449void owl_function_about()
1450{
1451  char buff[5000];
1452
1453  sprintf(buff, "This is barnowl version %s\n\n", OWL_VERSION_STRING);
1454  strcat(buff, "barnowl is a fork of the Owl zephyr client, written and\n");
1455  strcat(buff, "maintained by Alejandro Sedeno and Nelson Elhage at the\n");
1456  strcat(buff, "Massachusetts Institute of Technology. \n");
1457  strcat(buff, "\n");
1458  strcat(buff, "Owl was written by James Kretchmar. The first version, 0.5, was\n");
1459  strcat(buff, "released in March 2002.\n");
1460  strcat(buff, "\n");
1461  strcat(buff, "The name 'owl' was chosen in reference to the owls in the\n");
1462  strcat(buff, "Harry Potter novels, who are tasked with carrying messages\n");
1463  strcat(buff, "between Witches and Wizards. The name 'barnowl' was chosen\n");
1464  strcat(buff, "because we feel our owls should live closer to our ponies.\n");
1465  strcat(buff, "\n");
1466  strcat(buff, "Copyright 2002 Massachusetts Institute of Technology\n");
1467  strcat(buff, "\n");
1468  strcat(buff, "Permission to use, copy, modify, and distribute this\n");
1469  strcat(buff, "software and its documentation for any purpose and without\n");
1470  strcat(buff, "fee is hereby granted, provided that the above copyright\n");
1471  strcat(buff, "notice and this permission notice appear in all copies\n");
1472  strcat(buff, "and in supporting documentation.  No representation is\n");
1473  strcat(buff, "made about the suitability of this software for any\n");
1474  strcat(buff, "purpose.  It is provided \"as is\" without express\n");
1475  strcat(buff, "or implied warranty.\n");
1476  owl_function_popless_text(buff);
1477}
1478
1479void owl_function_info()
1480{
1481  owl_message *m;
1482  owl_fmtext fm, attrfm;
1483  char buff[10000];
1484  owl_view *v;
1485#ifdef HAVE_LIBZEPHYR
1486  ZNotice_t *n;
1487#endif
1488
1489  owl_fmtext_init_null(&fm);
1490 
1491  v=owl_global_get_current_view(&g);
1492  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
1493  if (!m || owl_view_get_size(v)==0) {
1494    owl_function_error("No message selected\n");
1495    return;
1496  }
1497
1498  owl_fmtext_append_bold(&fm, "General Information:\n");
1499  owl_fmtext_append_normal(&fm, "  Msg Id    : ");
1500  sprintf(buff, "%i", owl_message_get_id(m));
1501  owl_fmtext_append_normal(&fm, buff);
1502  owl_fmtext_append_normal(&fm, "\n");
1503
1504  owl_fmtext_append_normal(&fm, "  Type      : ");
1505  owl_fmtext_append_bold(&fm, owl_message_get_type(m));
1506  owl_fmtext_append_normal(&fm, "\n");
1507
1508  if (owl_message_is_direction_in(m)) {
1509    owl_fmtext_append_normal(&fm, "  Direction : in\n");
1510  } else if (owl_message_is_direction_out(m)) {
1511    owl_fmtext_append_normal(&fm, "  Direction : out\n");
1512  } else if (owl_message_is_direction_none(m)) {
1513    owl_fmtext_append_normal(&fm, "  Direction : none\n");
1514  } else {
1515    owl_fmtext_append_normal(&fm, "  Direction : unknown\n");
1516  }
1517
1518  owl_fmtext_append_normal(&fm, "  Time      : ");
1519  owl_fmtext_append_normal(&fm, owl_message_get_timestr(m));
1520  owl_fmtext_append_normal(&fm, "\n");
1521
1522  if (!owl_message_is_type_admin(m)) {
1523    owl_fmtext_append_normal(&fm, "  Sender    : ");
1524    owl_fmtext_append_normal(&fm, owl_message_get_sender(m));
1525    owl_fmtext_append_normal(&fm, "\n");
1526   
1527    owl_fmtext_append_normal(&fm, "  Recipient : ");
1528    owl_fmtext_append_normal(&fm, owl_message_get_recipient(m));
1529    owl_fmtext_append_normal(&fm, "\n");
1530  }
1531   
1532  if (owl_message_is_type_zephyr(m)) {
1533    owl_fmtext_append_bold(&fm, "\nZephyr Specific Information:\n");
1534   
1535    owl_fmtext_append_normal(&fm, "  Class     : ");
1536    owl_fmtext_append_normal(&fm, owl_message_get_class(m));
1537    owl_fmtext_append_normal(&fm, "\n");
1538    owl_fmtext_append_normal(&fm, "  Instance  : ");
1539    owl_fmtext_append_normal(&fm, owl_message_get_instance(m));
1540    owl_fmtext_append_normal(&fm, "\n");
1541    owl_fmtext_append_normal(&fm, "  Opcode    : ");
1542    owl_fmtext_append_normal(&fm, owl_message_get_opcode(m));
1543    owl_fmtext_append_normal(&fm, "\n");
1544   
1545    owl_fmtext_append_normal(&fm, "  Time      : ");
1546    owl_fmtext_append_normal(&fm, owl_message_get_timestr(m));
1547    owl_fmtext_append_normal(&fm, "\n");
1548#ifdef HAVE_LIBZEPHYR
1549    if (owl_message_is_direction_in(m)) {
1550      char *ptr, tmpbuff[1024];
1551      int i, j, fields, len;
1552
1553      n=owl_message_get_notice(m);
1554
1555      if (!owl_message_is_pseudo(m)) {
1556        owl_fmtext_append_normal(&fm, "  Kind      : ");
1557        if (n->z_kind==UNSAFE) {
1558          owl_fmtext_append_normal(&fm, "UNSAFE\n");
1559        } else if (n->z_kind==UNACKED) {
1560          owl_fmtext_append_normal(&fm, "UNACKED\n");
1561        } else if (n->z_kind==ACKED) {
1562          owl_fmtext_append_normal(&fm, "ACKED\n");
1563        } else if (n->z_kind==HMACK) {
1564          owl_fmtext_append_normal(&fm, "HMACK\n");
1565        } else if (n->z_kind==HMCTL) {
1566          owl_fmtext_append_normal(&fm, "HMCTL\n");
1567        } else if (n->z_kind==SERVACK) {
1568          owl_fmtext_append_normal(&fm, "SERVACK\n");
1569        } else if (n->z_kind==SERVNAK) {
1570          owl_fmtext_append_normal(&fm, "SERVNACK\n");
1571        } else if (n->z_kind==CLIENTACK) {
1572          owl_fmtext_append_normal(&fm, "CLIENTACK\n");
1573        } else if (n->z_kind==STAT) {
1574          owl_fmtext_append_normal(&fm, "STAT\n");
1575        } else {
1576          owl_fmtext_append_normal(&fm, "ILLEGAL VALUE\n");
1577        }
1578      }
1579      owl_fmtext_append_normal(&fm, "  Host      : ");
1580      owl_fmtext_append_normal(&fm, owl_message_get_hostname(m));
1581
1582      if (!owl_message_is_pseudo(m)) {
1583        owl_fmtext_append_normal(&fm, "\n");
1584        sprintf(buff, "  Port      : %i\n", ntohs(n->z_port));
1585        owl_fmtext_append_normal(&fm, buff);
1586
1587        owl_fmtext_append_normal(&fm,    "  Auth      : ");
1588        owl_fmtext_append_normal(&fm, owl_zephyr_get_authstr(n));
1589        owl_fmtext_append_normal(&fm, "\n");
1590       
1591        /* fix this */
1592        sprintf(buff, "  Checkd Ath: %i\n", n->z_checked_auth);
1593        sprintf(buff, "%s  Multi notc: %s\n", buff, n->z_multinotice);
1594        sprintf(buff, "%s  Num other : %i\n", buff, n->z_num_other_fields);
1595        sprintf(buff, "%s  Msg Len   : %i\n", buff, n->z_message_len);
1596        owl_fmtext_append_normal(&fm, buff);
1597       
1598        sprintf(buff, "  Fields    : %i\n", owl_zephyr_get_num_fields(n));
1599        owl_fmtext_append_normal(&fm, buff);
1600       
1601        fields=owl_zephyr_get_num_fields(n);
1602        for (i=0; i<fields; i++) {
1603          sprintf(buff, "  Field %i   : ", i+1);
1604         
1605          ptr=owl_zephyr_get_field(n, i+1);
1606          len=strlen(ptr);
1607          if (len<30) {
1608            strncpy(tmpbuff, ptr, len);
1609            tmpbuff[len]='\0';
1610          } else {
1611            strncpy(tmpbuff, ptr, 30);
1612            tmpbuff[30]='\0';
1613            strcat(tmpbuff, "...");
1614          }
1615          owl_free(ptr);
1616         
1617          for (j=0; j<strlen(tmpbuff); j++) {
1618            if (tmpbuff[j]=='\n') tmpbuff[j]='~';
1619            if (tmpbuff[j]=='\r') tmpbuff[j]='!';
1620          }
1621         
1622          strcat(buff, tmpbuff);
1623          strcat(buff, "\n");
1624          owl_fmtext_append_normal(&fm, buff);
1625        }
1626        owl_fmtext_append_normal(&fm, "  Default Fm:");
1627        owl_fmtext_append_normal(&fm, n->z_default_format);
1628      }
1629     
1630    }
1631#endif   
1632  }
1633
1634  if (owl_message_is_type_aim(m)) {
1635    owl_fmtext_append_bold(&fm, "\nAIM Specific Information:\n");
1636  }
1637
1638  owl_fmtext_append_bold(&fm, "\nOwl Message Attributes:\n");
1639  owl_message_attributes_tofmtext(m, &attrfm);
1640  owl_fmtext_append_fmtext(&fm, &attrfm);
1641 
1642  owl_function_popless_fmtext(&fm);
1643  owl_fmtext_free(&fm);
1644  owl_fmtext_free(&attrfm);
1645}
1646
1647/* print the current message in a popup window.
1648 * Use the 'default' style regardless of whatever
1649 * style the user may be using
1650 */
1651void owl_function_curmsg_to_popwin()
1652{
1653  owl_popwin *pw;
1654  owl_view *v;
1655  owl_message *m;
1656  owl_style *s;
1657  owl_fmtext fm;
1658
1659  v=owl_global_get_current_view(&g);
1660  s=owl_global_get_style_by_name(&g, "default");
1661  pw=owl_global_get_popwin(&g);
1662
1663  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
1664
1665  if (!m || owl_view_get_size(v)==0) {
1666    owl_function_error("No current message");
1667    return;
1668  }
1669
1670  owl_fmtext_init_null(&fm);
1671  owl_style_get_formattext(s, &fm, m);
1672
1673  owl_function_popless_fmtext(&fm);
1674  owl_fmtext_free(&fm);
1675}
1676
1677void owl_function_page_curmsg(int step)
1678{
1679  /* scroll down or up within the current message IF the message is truncated */
1680
1681  int offset, curmsg, lines;
1682  owl_view *v;
1683  owl_message *m;
1684
1685  offset=owl_global_get_curmsg_vert_offset(&g);
1686  v=owl_global_get_current_view(&g);
1687  curmsg=owl_global_get_curmsg(&g);
1688  m=owl_view_get_element(v, curmsg);
1689  if (!m || owl_view_get_size(v)==0) return;
1690  lines=owl_message_get_numlines(m);
1691
1692  if (offset==0) {
1693    /* Bail if the curmsg isn't the last one displayed */
1694    if (curmsg != owl_mainwin_get_last_msg(owl_global_get_mainwin(&g))) {
1695      owl_function_makemsg("The entire message is already displayed");
1696      return;
1697    }
1698   
1699    /* Bail if we're not truncated */
1700    if (!owl_mainwin_is_curmsg_truncated(owl_global_get_mainwin(&g))) {
1701      owl_function_makemsg("The entire message is already displayed");
1702      return;
1703    }
1704  }
1705 
1706 
1707  /* don't scroll past the last line */
1708  if (step>0) {
1709    if (offset+step > lines-1) {
1710      owl_global_set_curmsg_vert_offset(&g, lines-1);
1711    } else {
1712      owl_global_set_curmsg_vert_offset(&g, offset+step);
1713    }
1714  }
1715
1716  /* would we be before the beginning of the message? */
1717  if (step<0) {
1718    if (offset+step<0) {
1719      owl_global_set_curmsg_vert_offset(&g, 0);
1720    } else {
1721      owl_global_set_curmsg_vert_offset(&g, offset+step);
1722    }
1723  }
1724 
1725  /* redisplay */
1726  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
1727  owl_global_set_needrefresh(&g);
1728}
1729
1730void owl_function_resize_typwin(int newsize)
1731{
1732  owl_global_set_typwin_lines(&g, newsize);
1733  owl_function_resize();
1734}
1735
1736void owl_function_typwin_grow()
1737{
1738  int i;
1739
1740  i=owl_global_get_typwin_lines(&g);
1741  owl_function_resize_typwin(i+1);
1742}
1743
1744void owl_function_typwin_shrink()
1745{
1746  int i;
1747
1748  i=owl_global_get_typwin_lines(&g);
1749  if (i>2) {
1750    owl_function_resize_typwin(i-1);
1751  }
1752}
1753
1754void owl_function_mainwin_pagedown()
1755{
1756  int i;
1757
1758  i=owl_mainwin_get_last_msg(owl_global_get_mainwin(&g));
1759  if (i<0) return;
1760  if (owl_mainwin_is_last_msg_truncated(owl_global_get_mainwin(&g))
1761      && (owl_global_get_curmsg(&g) < i)
1762      && (i>0)) {
1763    i--;
1764  }
1765  owl_global_set_curmsg(&g, i);
1766  owl_function_nextmsg();
1767}
1768
1769void owl_function_mainwin_pageup()
1770{
1771  owl_global_set_curmsg(&g, owl_global_get_topmsg(&g));
1772  owl_function_prevmsg();
1773}
1774
1775void owl_function_getsubs()
1776{
1777  char *buff;
1778
1779  buff=owl_zephyr_getsubs();
1780
1781  if (buff) {
1782    owl_function_popless_text(buff);
1783  } else {
1784    owl_function_popless_text("Error getting subscriptions");
1785  }
1786           
1787  owl_free(buff);
1788}
1789
1790#define PABUFLEN 5000
1791void owl_function_printallvars()
1792{
1793  char buff[PABUFLEN], *pos, *name;
1794  owl_list varnames;
1795  int i, numvarnames, rem;
1796
1797  pos = buff;
1798  pos += sprintf(pos, "%-20s = %s\n", "VARIABLE", "VALUE");
1799  pos += sprintf(pos, "%-20s   %s\n",  "--------", "-----");
1800  owl_variable_dict_get_names(owl_global_get_vardict(&g), &varnames);
1801  rem = (buff+PABUFLEN)-pos-1;
1802  numvarnames = owl_list_get_size(&varnames);
1803  for (i=0; i<numvarnames; i++) {
1804    name = owl_list_get_element(&varnames, i);
1805    if (name && name[0]!='_') {
1806      rem = (buff+PABUFLEN)-pos-1;   
1807      pos += snprintf(pos, rem, "\n%-20s = ", name);
1808      rem = (buff+PABUFLEN)-pos-1;   
1809      owl_variable_get_tostring(owl_global_get_vardict(&g), name, pos, rem);
1810      pos = buff+strlen(buff);
1811    }
1812  }
1813  rem = (buff+PABUFLEN)-pos-1;   
1814  snprintf(pos, rem, "\n");
1815  owl_variable_dict_namelist_free(&varnames);
1816 
1817  owl_function_popless_text(buff);
1818}
1819
1820void owl_function_show_variables()
1821{
1822  owl_list varnames;
1823  owl_fmtext fm; 
1824  int i, numvarnames;
1825  char *varname;
1826
1827  owl_fmtext_init_null(&fm);
1828  owl_fmtext_append_bold(&fm, 
1829      "Variables: (use 'show variable <name>' for details)\n");
1830  owl_variable_dict_get_names(owl_global_get_vardict(&g), &varnames);
1831  numvarnames = owl_list_get_size(&varnames);
1832  for (i=0; i<numvarnames; i++) {
1833    varname = owl_list_get_element(&varnames, i);
1834    if (varname && varname[0]!='_') {
1835      owl_variable_describe(owl_global_get_vardict(&g), varname, &fm);
1836    }
1837  }
1838  owl_variable_dict_namelist_free(&varnames);
1839  owl_function_popless_fmtext(&fm);
1840  owl_fmtext_free(&fm);
1841}
1842
1843void owl_function_show_variable(char *name)
1844{
1845  owl_fmtext fm; 
1846
1847  owl_fmtext_init_null(&fm);
1848  owl_variable_get_help(owl_global_get_vardict(&g), name, &fm);
1849  owl_function_popless_fmtext(&fm);
1850  owl_fmtext_free(&fm); 
1851}
1852
1853/* note: this applies to global message list, not to view.
1854 * If flag is 1, deletes.  If flag is 0, undeletes. */
1855void owl_function_delete_by_id(int id, int flag)
1856{
1857  owl_messagelist *ml;
1858  owl_message *m;
1859  ml = owl_global_get_msglist(&g);
1860  m = owl_messagelist_get_by_id(ml, id);
1861  if (m) {
1862    if (flag == 1) {
1863      owl_message_mark_delete(m);
1864    } else if (flag == 0) {
1865      owl_message_unmark_delete(m);
1866    }
1867    owl_mainwin_redisplay(owl_global_get_mainwin(&g));
1868    owl_global_set_needrefresh(&g);
1869  } else {
1870    owl_function_error("No message with id %d: unable to mark for (un)delete",id);
1871  }
1872}
1873
1874void owl_function_delete_automsgs()
1875{
1876  /* mark for deletion all messages in the current view that match the
1877   * 'trash' filter */
1878
1879  int i, j, count;
1880  owl_message *m;
1881  owl_view *v;
1882  owl_filter *f;
1883
1884  /* get the trash filter */
1885  f=owl_global_get_filter(&g, "trash");
1886  if (!f) {
1887    owl_function_error("No trash filter defined");
1888    return;
1889  }
1890
1891  v=owl_global_get_current_view(&g);
1892
1893  count=0;
1894  j=owl_view_get_size(v);
1895  for (i=0; i<j; i++) {
1896    m=owl_view_get_element(v, i);
1897    if (owl_filter_message_match(f, m)) {
1898      count++;
1899      owl_message_mark_delete(m);
1900    }
1901  }
1902  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
1903  owl_function_makemsg("%i messages marked for deletion", count);
1904  owl_global_set_needrefresh(&g);
1905}
1906
1907void owl_function_status()
1908{
1909  char buff[5000];
1910  time_t start;
1911  int up, days, hours, minutes;
1912  owl_fmtext fm;
1913
1914  owl_fmtext_init_null(&fm);
1915
1916  start=owl_global_get_starttime(&g);
1917
1918  owl_fmtext_append_normal(&fm, "General Information:\n");
1919
1920  owl_fmtext_append_normal(&fm, "  Version: ");
1921  owl_fmtext_append_normal(&fm, OWL_VERSION_STRING);
1922  owl_fmtext_append_normal(&fm, "\n");
1923
1924  owl_fmtext_append_normal(&fm, "  Startup Arugments: ");
1925  owl_fmtext_append_normal(&fm, owl_global_get_startupargs(&g));
1926  owl_fmtext_append_normal(&fm, "\n");
1927
1928  owl_fmtext_append_normal(&fm, "  Current Directory: ");
1929  (void) getcwd(buff, MAXPATHLEN);
1930  owl_fmtext_append_normal(&fm, buff);
1931  owl_fmtext_append_normal(&fm, "\n");
1932
1933  sprintf(buff, "  Startup Time: %s", ctime(&start));
1934  owl_fmtext_append_normal(&fm, buff);
1935
1936  up=owl_global_get_runtime(&g);
1937  days=up/86400;
1938  up-=days*86400;
1939  hours=up/3600;
1940  up-=hours*3600;
1941  minutes=up/60;
1942  up-=minutes*60;
1943  sprintf(buff, "  Run Time: %i days %2.2i:%2.2i:%2.2i\n", days, hours, minutes, up);
1944  owl_fmtext_append_normal(&fm, buff);
1945
1946  owl_fmtext_append_normal(&fm, "\nProtocol Options:\n");
1947  owl_fmtext_append_normal(&fm, "  Zephyr included    : ");
1948  if (owl_global_is_havezephyr(&g)) {
1949    owl_fmtext_append_normal(&fm, "yes\n");
1950  } else {
1951    owl_fmtext_append_normal(&fm, "no\n");
1952  }
1953  owl_fmtext_append_normal(&fm, "  AIM included       : yes\n");
1954  owl_fmtext_append_normal(&fm, "  Loopback included  : yes\n");
1955
1956
1957  owl_fmtext_append_normal(&fm, "\nBuild Options:\n");
1958  owl_fmtext_append_normal(&fm, "  Stderr redirection : ");
1959#if OWL_STDERR_REDIR
1960  owl_fmtext_append_normal(&fm, "yes\n");
1961#else
1962  owl_fmtext_append_normal(&fm, "no\n");
1963#endif
1964 
1965
1966  owl_fmtext_append_normal(&fm, "\nMemory Usage:\n");
1967  owl_fmtext_append_normal(&fm, "  Not currently available.\n");
1968  /*
1969  sprintf(buff, "%sMemory Malloced: %i\n", buff, owl_global_get_malloced(&g));
1970  sprintf(buff, "%sMemory Freed: %i\n", buff, owl_global_get_freed(&g));
1971  sprintf(buff, "%sMemory In Use: %i\n", buff, owl_global_get_meminuse(&g));
1972  */
1973
1974  owl_fmtext_append_normal(&fm, "\nAIM Status:\n");
1975  owl_fmtext_append_normal(&fm, "  Logged in: ");
1976  if (owl_global_is_aimloggedin(&g)) {
1977    owl_fmtext_append_normal(&fm, owl_global_get_aim_screenname(&g));
1978    owl_fmtext_append_normal(&fm, "\n");
1979  } else {
1980    owl_fmtext_append_normal(&fm, "(not logged in)\n");
1981  }
1982
1983  owl_fmtext_append_normal(&fm, "  Processing events: ");
1984  if (owl_global_is_doaimevents(&g)) {
1985    owl_fmtext_append_normal(&fm, "yes\n");
1986  } else {
1987    owl_fmtext_append_normal(&fm, "no\n");
1988  }
1989
1990  owl_function_popless_fmtext(&fm);
1991  owl_fmtext_free(&fm);
1992}
1993
1994void owl_function_show_term()
1995{
1996  owl_fmtext fm;
1997  char buff[LINE];
1998
1999  owl_fmtext_init_null(&fm);
2000  sprintf(buff, "Terminal Lines: %i\nTerminal Columns: %i\n",
2001          owl_global_get_lines(&g),
2002          owl_global_get_cols(&g));
2003  owl_fmtext_append_normal(&fm, buff);
2004
2005  if (owl_global_get_hascolors(&g)) {
2006    owl_fmtext_append_normal(&fm, "Color: Yes\n");
2007    sprintf(buff, "Number of color pairs: %i\n", owl_global_get_colorpairs(&g));
2008    owl_fmtext_append_normal(&fm, buff);
2009    sprintf(buff, "Can change colors: %s\n", can_change_color() ? "yes" : "no");
2010    owl_fmtext_append_normal(&fm, buff);
2011  } else {
2012    owl_fmtext_append_normal(&fm, "Color: No\n");
2013  }
2014
2015  owl_function_popless_fmtext(&fm);
2016  owl_fmtext_free(&fm);
2017}
2018
2019/* if type = 0 then normal reply.
2020 * if type = 1 then it's a reply to sender
2021 * if enter = 0 then allow the command to be edited
2022 * if enter = 1 then don't wait for editing
2023 */
2024void owl_function_reply(int type, int enter)
2025{
2026  char *buff=NULL, *oldbuff;
2027  owl_message *m;
2028  owl_filter *f;
2029 
2030  if (owl_view_get_size(owl_global_get_current_view(&g))==0) {
2031    owl_function_error("No message selected");
2032  } else {
2033    char *class, *inst, *to, *cc=NULL;
2034   
2035    m=owl_view_get_element(owl_global_get_current_view(&g), owl_global_get_curmsg(&g));
2036    if (!m) {
2037      owl_function_error("No message selected");
2038      return;
2039    }
2040
2041    /* first check if we catch the reply-lockout filter */
2042    f=owl_global_get_filter(&g, "reply-lockout");
2043    if (f) {
2044      if (owl_filter_message_match(f, m)) {
2045        owl_function_error("Sorry, replies to this message have been disabled by the reply-lockout filter");
2046        return;
2047      }
2048    }
2049
2050    /* loopback */
2051    if (owl_message_is_type_loopback(m)) {
2052      owl_function_loopwrite_setup();
2053      return;
2054    }
2055
2056    /* zephyr */
2057    if (owl_message_is_type_zephyr(m)) {
2058      /* if it's a zephyr we sent, send it out the same way again */
2059      if (owl_message_is_direction_out(m)) {
2060        owl_function_zwrite_setup(owl_message_get_zwriteline(m));
2061        owl_global_set_buffercommand(&g, owl_message_get_zwriteline(m));
2062        return;
2063      }
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);
2134    } else if (owl_message_is_type_aim(m)) {
2135      /* aim */
2136      if (owl_message_is_direction_out(m)) {
2137        buff=owl_sprintf("aimwrite %s", owl_message_get_recipient(m));
2138      } else {
2139        buff=owl_sprintf("aimwrite %s", owl_message_get_sender(m));
2140      }
2141    } else {
2142      char *cmd;
2143      if((type==0 && (cmd=owl_message_get_attribute_value(m, "replycmd")))
2144         || (type==1 && (cmd=owl_message_get_attribute_value(m, "replysendercmd")))) {
2145        buff = owl_strdup(cmd);
2146      }
2147    }
2148
2149    if(!buff) {
2150        owl_function_error("I don't know how to reply to that message.");
2151        return;
2152    }
2153   
2154    if (enter) {
2155      owl_history *hist = owl_global_get_cmd_history(&g);
2156      owl_history_store(hist, buff);
2157      owl_history_reset(hist);
2158      owl_function_command_norv(buff);
2159    } else {
2160      owl_function_start_command(buff);
2161    }
2162    owl_free(buff);
2163  }
2164}
2165
2166void owl_function_zlocate(int argc, char **argv, int auth)
2167{
2168  owl_fmtext fm;
2169  char *ptr, buff[LINE];
2170  int i;
2171
2172  owl_fmtext_init_null(&fm);
2173
2174  for (i=0; i<argc; i++) {
2175    ptr=long_zuser(argv[i]);
2176    owl_zephyr_zlocate(ptr, buff, auth);
2177    owl_fmtext_append_normal(&fm, buff);
2178    owl_free(ptr);
2179  }
2180
2181  owl_function_popless_fmtext(&fm);
2182  owl_fmtext_free(&fm);
2183}
2184
2185void owl_function_start_command(char *line)
2186{
2187  int i, j;
2188  owl_editwin *tw;
2189
2190  tw=owl_global_get_typwin(&g);
2191  owl_global_set_typwin_active(&g);
2192  owl_editwin_new_style(tw, OWL_EDITWIN_STYLE_ONELINE, 
2193                        owl_global_get_cmd_history(&g));
2194
2195  owl_editwin_set_locktext(tw, "command: ");
2196  owl_global_set_needrefresh(&g);
2197
2198  j=strlen(line);
2199  for (i=0; i<j; i++) {
2200    owl_editwin_process_char(tw, line[i]);
2201  }
2202  owl_editwin_redisplay(tw, 0);
2203
2204  owl_context_set_editline(owl_global_get_context(&g), tw);
2205  owl_function_activate_keymap("editline");
2206}
2207
2208void owl_function_start_question(char *line)
2209{
2210  owl_editwin *tw;
2211
2212  tw=owl_global_get_typwin(&g);
2213  owl_global_set_typwin_active(&g);
2214  owl_editwin_new_style(tw, OWL_EDITWIN_STYLE_ONELINE, owl_global_get_cmd_history(&g));
2215
2216  owl_editwin_set_locktext(tw, line);
2217  owl_global_set_needrefresh(&g);
2218
2219  owl_editwin_redisplay(tw, 0);
2220
2221  owl_context_set_editresponse(owl_global_get_context(&g), tw);
2222  owl_function_activate_keymap("editresponse");
2223}
2224
2225void owl_function_start_password(char *line)
2226{
2227  owl_editwin *tw;
2228
2229  tw=owl_global_get_typwin(&g);
2230  owl_global_set_typwin_active(&g);
2231  owl_editwin_new_style(tw, OWL_EDITWIN_STYLE_ONELINE, owl_global_get_cmd_history(&g));
2232  owl_editwin_set_echochar(tw, '*');
2233
2234  owl_editwin_set_locktext(tw, line);
2235  owl_global_set_needrefresh(&g);
2236
2237  owl_editwin_redisplay(tw, 0);
2238
2239  owl_context_set_editresponse(owl_global_get_context(&g), tw);
2240  owl_function_activate_keymap("editresponse");
2241}
2242
2243char *owl_function_exec(int argc, char **argv, char *buff, int type)
2244{
2245  /* if type == 1 display in a popup
2246   * if type == 2 display an admin messages
2247   * if type == 0 return output
2248   * else display in a popup
2249   */
2250  char *newbuff, *redirect = " 2>&1 < /dev/null";
2251  char *out, buff2[1024];
2252  int size;
2253  FILE *p;
2254
2255#if OWL_STDERR_REDIR
2256  redirect = " < /dev/null";
2257#endif
2258
2259  if (argc<2) {
2260    owl_function_error("Wrong number of arguments to the exec command");
2261    return NULL;
2262  }
2263
2264  buff = skiptokens(buff, 1);
2265  newbuff = owl_malloc(strlen(buff)+strlen(redirect)+1);
2266  strcpy(newbuff, buff);
2267  strcat(newbuff, redirect);
2268
2269  if (type == 1) {
2270    owl_popexec_new(newbuff);
2271  } else {
2272    p=popen(newbuff, "r");
2273    out=owl_malloc(1024);
2274    size=1024;
2275    strcpy(out, "");
2276    while (fgets(buff2, 1024, p)!=NULL) {
2277      size+=1024;
2278      out=owl_realloc(out, size);
2279      strcat(out, buff2);
2280    }
2281    pclose(p);
2282   
2283    if (type==1) {
2284      owl_function_popless_text(out);
2285    } else if (type==0) {
2286      return out;
2287    } else if (type==2) {
2288      owl_function_adminmsg(buff, out);
2289    } else {
2290      owl_function_popless_text(out);
2291    }
2292    owl_free(out);
2293  }
2294  return NULL;
2295}
2296
2297char *owl_function_perl(int argc, char **argv, char *buff, int type)
2298{
2299  /* if type == 1 display in a popup
2300   * if type == 2 display an admin messages
2301   * if type == 0 return output
2302   * else display in a popup
2303   */
2304  char *perlout;
2305
2306  if (argc<2) {
2307    owl_function_error("Wrong number of arguments to perl command");
2308    return NULL;
2309  }
2310
2311  /* consume first token (argv[0]) */
2312  buff = skiptokens(buff, 1);
2313
2314  perlout = owl_perlconfig_execute(buff);
2315  if (perlout) { 
2316    if (type==1) {
2317      owl_function_popless_text(perlout);
2318    } else if (type==2) {
2319      owl_function_adminmsg(buff, perlout);
2320    } else if (type==0) {
2321      return perlout;
2322    } else {
2323      owl_function_popless_text(perlout);
2324    }
2325    owl_free(perlout);
2326  }
2327  return NULL;
2328}
2329
2330/* Change the filter associated with the current view.
2331 * This also figures out which message in the new filter
2332 * should have the pointer.
2333 */
2334void owl_function_change_currentview_filter(char *filtname)
2335{
2336  owl_view *v;
2337  owl_filter *f;
2338  int curid=-1, newpos, curmsg;
2339  owl_message *curm=NULL;
2340
2341  v=owl_global_get_current_view(&g);
2342
2343  curmsg=owl_global_get_curmsg(&g);
2344  if (curmsg==-1) {
2345    owl_function_debugmsg("Hit the curmsg==-1 case in change_view");
2346  } else {
2347    curm=owl_view_get_element(v, curmsg);
2348    if (curm) {
2349      curid=owl_message_get_id(curm);
2350      owl_view_save_curmsgid(v, curid);
2351    }
2352  }
2353
2354  f=owl_global_get_filter(&g, filtname);
2355  if (!f) {
2356    owl_function_error("Unknown filter %s", filtname);
2357    return;
2358  }
2359
2360  owl_view_new_filter(v, f);
2361
2362  /* Figure out what to set the current message to.
2363   * - If the view we're leaving has messages in it, go to the closest message
2364   *   to the last message pointed to in that view.
2365   * - If the view we're leaving is empty, try to restore the position
2366   *   from the last time we were in the new view.  */
2367  if (curm) {
2368    newpos = owl_view_get_nearest_to_msgid(v, curid);
2369  } else {
2370    newpos = owl_view_get_nearest_to_saved(v);
2371  }
2372
2373  owl_global_set_curmsg(&g, newpos);
2374  owl_function_calculate_topmsg(OWL_DIRECTION_DOWNWARDS);
2375  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2376  owl_global_set_direction_downwards(&g);
2377}
2378
2379/* Create a new filter, or replace an existing one
2380 * with a new definition.
2381 */
2382void owl_function_create_filter(int argc, char **argv)
2383{
2384  owl_filter *f;
2385  owl_view *v;
2386  int ret, inuse=0;
2387
2388  if (argc < 2) {
2389    owl_function_error("Wrong number of arguments to filter command");
2390    return;
2391  }
2392
2393  owl_function_debugmsg("owl_function_create_filter: starting to create filter named %s", argv[1]);
2394
2395  v=owl_global_get_current_view(&g);
2396
2397  /* don't touch the all filter */
2398  if (!strcmp(argv[1], "all")) {
2399    owl_function_error("You may not change the 'all' filter.");
2400    return;
2401  }
2402
2403  /* deal with the case of trying change the filter color */
2404  if (argc==4 && !strcmp(argv[2], "-c")) {
2405    f=owl_global_get_filter(&g, argv[1]);
2406    if (!f) {
2407      owl_function_error("The filter '%s' does not exist.", argv[1]);
2408      return;
2409    }
2410    if (owl_util_string_to_color(argv[3])==-1) {
2411      owl_function_error("The color '%s' is not available.", argv[3]);
2412      return;
2413    }
2414    owl_filter_set_fgcolor(f, owl_util_string_to_color(argv[3]));
2415    owl_global_set_needrefresh(&g);
2416    owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2417    return;
2418  }
2419  if (argc==4 && !strcmp(argv[2], "-b")) {
2420    f=owl_global_get_filter(&g, argv[1]);
2421    if (!f) {
2422      owl_function_error("The filter '%s' does not exist.", argv[1]);
2423      return;
2424    }
2425    if (owl_util_string_to_color(argv[3])==-1) {
2426      owl_function_error("The color '%s' is not available.", argv[3]);
2427      return;
2428    }
2429    owl_filter_set_bgcolor(f, owl_util_string_to_color(argv[3]));
2430    owl_global_set_needrefresh(&g);
2431    owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2432    return;
2433  }
2434
2435  /* create the filter and check for errors */
2436  f=owl_malloc(sizeof(owl_filter));
2437  ret=owl_filter_init(f, argv[1], argc-2, argv+2);
2438  if (ret==-1) {
2439    owl_free(f);
2440    owl_function_error("Invalid filter");
2441    return;
2442  }
2443
2444  /* if the named filter is in use by the current view, remember it */
2445  if (!strcmp(owl_view_get_filtname(v), argv[1])) {
2446    inuse=1;
2447  }
2448
2449  /* if the named filter already exists, nuke it */
2450  if (owl_global_get_filter(&g, argv[1])) {
2451    owl_global_remove_filter(&g, argv[1]);
2452  }
2453
2454  /* add the filter */
2455  owl_global_add_filter(&g, f);
2456
2457  /* if it was in use by the current view then update */
2458  if (inuse) {
2459    owl_function_change_currentview_filter(argv[1]);
2460  }
2461  owl_global_set_needrefresh(&g);
2462  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2463}
2464
2465/* If 'filtername' does not start with 'not-' create a filter named
2466 * 'not-<filtername>' defined as "not filter <filtername>".  If the
2467 * filter 'not-<filtername>' already exists, do not overwrite it.  If
2468 * 'filtername' begins with 'not-' and a filter 'filtername' already
2469 * exists, then do nothing.  If the filter 'filtername' does not
2470 * exist, create it and define it as 'not filter <filtername>'
2471 *
2472 * Returns the name of the negated filter, which the caller must free.
2473 */
2474char *owl_function_create_negative_filter(char *filtername)
2475{
2476  char *newname;
2477  owl_filter *tmpfilt;
2478  char *argv[5];
2479
2480  owl_function_debugmsg("owl_function_create_negative_filter");
2481 
2482  if (!strncmp(filtername, "not-", 4)) {
2483    newname=owl_strdup(filtername+4);
2484  } else {
2485    newname=owl_sprintf("not-%s", filtername);
2486  }
2487
2488  tmpfilt=owl_global_get_filter(&g, newname);
2489  if (!tmpfilt) {
2490    argv[0]="filter"; /* anything is fine here */
2491    argv[1]=newname;
2492    argv[2]="not";
2493    argv[3]="filter";
2494    argv[4]=filtername;
2495    owl_function_create_filter(5, argv);
2496  }
2497
2498  owl_function_debugmsg("owl_function_create_negative_filter: returning with %s", newname);
2499  return(newname);
2500}
2501
2502void owl_function_show_filters()
2503{
2504  owl_list *l;
2505  owl_filter *f;
2506  int i, j;
2507  owl_fmtext fm;
2508
2509  owl_fmtext_init_null(&fm);
2510
2511  l=owl_global_get_filterlist(&g);
2512  j=owl_list_get_size(l);
2513
2514  owl_fmtext_append_bold(&fm, "Filters:\n");
2515
2516  for (i=0; i<j; i++) {
2517    f=owl_list_get_element(l, i);
2518    owl_fmtext_append_normal(&fm, "   ");
2519    if (owl_global_get_hascolors(&g)) {
2520      owl_fmtext_append_normal_color(&fm, owl_filter_get_name(f), owl_filter_get_fgcolor(f), owl_filter_get_bgcolor(f));
2521    } else {
2522      owl_fmtext_append_normal(&fm, owl_filter_get_name(f));
2523    }
2524    owl_fmtext_append_normal(&fm, "\n");
2525  }
2526  owl_function_popless_fmtext(&fm);
2527  owl_fmtext_free(&fm);
2528}
2529
2530void owl_function_show_filter(char *name)
2531{
2532  owl_filter *f;
2533  char buff[5000];
2534
2535  f=owl_global_get_filter(&g, name);
2536  if (!f) {
2537    owl_function_error("There is no filter named %s", name);
2538    return;
2539  }
2540  owl_filter_print(f, buff);
2541  owl_function_popless_text(buff);
2542}
2543
2544void owl_function_show_zpunts()
2545{
2546  owl_filter *f;
2547  owl_list *fl;
2548  char buff[5000];
2549  owl_fmtext fm;
2550  int i, j;
2551
2552  owl_fmtext_init_null(&fm);
2553
2554  fl=owl_global_get_puntlist(&g);
2555  j=owl_list_get_size(fl);
2556  owl_fmtext_append_bold(&fm, "Active zpunt filters:\n");
2557
2558  for (i=0; i<j; i++) {
2559    f=owl_list_get_element(fl, i);
2560    snprintf(buff, sizeof(buff), "[% 2d] ", i+1);
2561    owl_fmtext_append_normal(&fm, buff);
2562    owl_filter_print(f, buff);
2563    owl_fmtext_append_normal(&fm, buff);
2564  }
2565  owl_function_popless_fmtext(&fm);
2566  owl_fmtext_free(&fm);
2567}
2568
2569/* Create a filter for a class, instance if one doesn't exist.  If
2570 * instance is NULL then catch all messgaes in the class.  Returns the
2571 * name of the filter, which the caller must free.
2572 */
2573char *owl_function_classinstfilt(char *c, char *i) 
2574{
2575  owl_list *fl;
2576  owl_filter *f;
2577  char *argbuff, *filtname;
2578  char *tmpclass, *tmpinstance = NULL;
2579  char *class, *instance = NULL;
2580  int len;
2581
2582  class = owl_util_baseclass(c);
2583  if(i) {
2584    instance = owl_util_baseclass(i);
2585  }
2586
2587  fl=owl_global_get_filterlist(&g);
2588
2589  /* name for the filter */
2590  len=strlen(class)+30;
2591  if (instance) len+=strlen(instance);
2592  filtname=owl_malloc(len);
2593  if (!instance) {
2594    sprintf(filtname, "class-%s", class);
2595  } else {
2596    sprintf(filtname, "class-%s-instance-%s", class, instance);
2597  }
2598  /* downcase it */
2599  downstr(filtname);
2600  /* turn spaces, single quotes, and double quotes into dots */
2601  owl_text_tr(filtname, ' ', '.');
2602  owl_text_tr(filtname, '\'', '.');
2603  owl_text_tr(filtname, '"', '.');
2604 
2605  /* if it already exists then go with it.  This lets users override */
2606  if (owl_global_get_filter(&g, filtname)) {
2607    return(filtname);
2608  }
2609
2610  /* create the new filter */
2611  tmpclass=owl_text_quote(class, OWL_REGEX_QUOTECHARS, OWL_REGEX_QUOTEWITH);
2612  owl_text_tr(tmpclass, ' ', '.');
2613  owl_text_tr(tmpclass, '\'', '.');
2614  owl_text_tr(tmpclass, '"', '.');
2615  if (instance) {
2616    tmpinstance=owl_text_quote(instance, OWL_REGEX_QUOTECHARS, OWL_REGEX_QUOTEWITH);
2617    owl_text_tr(tmpinstance, ' ', '.');
2618    owl_text_tr(tmpinstance, '\'', '.');
2619    owl_text_tr(tmpinstance, '"', '.');
2620  }
2621  len = strlen(tmpclass);
2622  if(tmpinstance) len += strlen(tmpinstance);
2623  len += 60;
2624  argbuff = owl_malloc(len);
2625  sprintf(argbuff, "class ^(un)*%s(\\.d)*$", tmpclass);
2626  if (tmpinstance) {
2627    sprintf(argbuff, "%s and ( instance ^(un)*%s(\\.d)*$ )", argbuff, tmpinstance);
2628  }
2629  owl_free(tmpclass);
2630  if (tmpinstance) owl_free(tmpinstance);
2631
2632  f=owl_malloc(sizeof(owl_filter));
2633  owl_filter_init_fromstring(f, filtname, argbuff);
2634
2635  /* add it to the global list */
2636  owl_global_add_filter(&g, f);
2637
2638  owl_free(argbuff);
2639  owl_free(class);
2640  if (instance) {
2641    owl_free(instance);
2642  }
2643  return(filtname);
2644}
2645
2646/* Create a filter for personal zephyrs to or from the specified
2647 * zephyr user.  Includes login/logout notifications for the user.
2648 * The name of the filter will be 'user-<user>'.  If a filter already
2649 * exists with this name, no new filter will be created.  This allows
2650 * the configuration to override this function.  Returns the name of
2651 * the filter, which the caller must free.
2652 */
2653char *owl_function_zuserfilt(char *user)
2654{
2655  owl_filter *f;
2656  char *argbuff, *longuser, *shortuser, *filtname;
2657
2658  /* stick the local realm on if it's not there */
2659  longuser=long_zuser(user);
2660  shortuser=short_zuser(user);
2661
2662  /* name for the filter */
2663  filtname=owl_malloc(strlen(shortuser)+20);
2664  sprintf(filtname, "user-%s", shortuser);
2665
2666  /* if it already exists then go with it.  This lets users override */
2667  if (owl_global_get_filter(&g, filtname)) {
2668    return(owl_strdup(filtname));
2669  }
2670
2671  /* create the new-internal filter */
2672  f=owl_malloc(sizeof(owl_filter));
2673
2674  argbuff=owl_malloc(strlen(longuser)+1000);
2675  sprintf(argbuff, "( type ^zephyr$ and filter personal and ");
2676  sprintf(argbuff, "%s ( ( direction ^in$ and sender ^%s$ ) or ( direction ^out$ and recipient ^%s$ ) ) )", argbuff, longuser, longuser);
2677  sprintf(argbuff, "%s or ( ( class ^login$ ) and ( sender ^%s$ ) )", argbuff, longuser);
2678
2679  owl_filter_init_fromstring(f, filtname, argbuff);
2680
2681  /* add it to the global list */
2682  owl_global_add_filter(&g, f);
2683
2684  /* free stuff */
2685  owl_free(argbuff);
2686  owl_free(longuser);
2687  owl_free(shortuser);
2688
2689  return(filtname);
2690}
2691
2692/* Create a filter for AIM IM messages to or from the specified
2693 * screenname.  The name of the filter will be 'aimuser-<user>'.  If a
2694 * filter already exists with this name, no new filter will be
2695 * created.  This allows the configuration to override this function.
2696 * Returns the name of the filter, which the caller must free.
2697 */
2698char *owl_function_aimuserfilt(char *user)
2699{
2700  owl_filter *f;
2701  char *argbuff, *filtname;
2702  char *escuser;
2703
2704  /* name for the filter */
2705  filtname=owl_malloc(strlen(user)+40);
2706  sprintf(filtname, "aimuser-%s", user);
2707
2708  /* if it already exists then go with it.  This lets users override */
2709  if (owl_global_get_filter(&g, filtname)) {
2710    return(owl_strdup(filtname));
2711  }
2712
2713  /* create the new-internal filter */
2714  f=owl_malloc(sizeof(owl_filter));
2715
2716  escuser = owl_text_quote(user, OWL_REGEX_QUOTECHARS, OWL_REGEX_QUOTEWITH);
2717
2718  argbuff=owl_malloc(1000);
2719  sprintf(argbuff,
2720          "( type ^aim$ and ( ( sender ^%s$ and recipient ^%s$ ) or ( sender ^%s$ and recipient ^%s$ ) ) )",
2721          escuser, owl_global_get_aim_screenname(&g), owl_global_get_aim_screenname(&g), escuser);
2722
2723  owl_filter_init_fromstring(f, filtname, argbuff);
2724
2725  /* add it to the global list */
2726  owl_global_add_filter(&g, f);
2727
2728  /* free stuff */
2729  owl_free(argbuff);
2730  owl_free(escuser);
2731
2732  return(filtname);
2733}
2734
2735char *owl_function_typefilt(char *type)
2736{
2737  owl_filter *f;
2738  char *argbuff, *filtname;
2739
2740  /* name for the filter */
2741  filtname=owl_sprintf("type-%s", type);
2742
2743  /* if it already exists then go with it.  This lets users override */
2744  if (owl_global_get_filter(&g, filtname)) {
2745    return filtname;
2746  }
2747
2748  /* create the new-internal filter */
2749  f=owl_malloc(sizeof(owl_filter));
2750
2751  argbuff = owl_sprintf("type ^%s$", type);
2752
2753  owl_filter_init_fromstring(f, filtname, argbuff);
2754
2755  /* add it to the global list */
2756  owl_global_add_filter(&g, f);
2757
2758  /* free stuff */
2759  owl_free(argbuff);
2760
2761  return filtname;
2762}
2763
2764/* If flag is 1, marks for deletion.  If flag is 0,
2765 * unmarks for deletion. */
2766void owl_function_delete_curview_msgs(int flag)
2767{
2768  owl_view *v;
2769  int i, j;
2770
2771  v=owl_global_get_current_view(&g);
2772  j=owl_view_get_size(v);
2773  for (i=0; i<j; i++) {
2774    if (flag == 1) {
2775      owl_message_mark_delete(owl_view_get_element(v, i));
2776    } else if (flag == 0) {
2777      owl_message_unmark_delete(owl_view_get_element(v, i));
2778    }
2779  }
2780
2781  owl_function_makemsg("%i messages marked for %sdeletion", j, flag?"":"un");
2782
2783  owl_mainwin_redisplay(owl_global_get_mainwin(&g)); 
2784}
2785
2786/* Create a filter based on the current message.  Returns the name of
2787 * a filter or null.  The caller must free this name.
2788 *
2789 * if the curmsg is a personal zephyr return a filter name
2790 *    to the zephyr converstaion with that user.
2791 * If the curmsg is a zephyr class message, instance foo, recip *,
2792 *    return a filter name to the class, inst.
2793 * If the curmsg is a zephyr class message and type==0 then
2794 *    return a filter name for just the class.
2795 * If the curmsg is a zephyr class message and type==1 then
2796 *    return a filter name for the class and instance.
2797 * If the curmsg is a personal AIM message returna  filter
2798 *    name to the AIM conversation with that user
2799 */
2800char *owl_function_smartfilter(int type)
2801{
2802  owl_view *v;
2803  owl_message *m;
2804  char *zperson, *filtname=NULL;
2805  char *argv[1];
2806 
2807  v=owl_global_get_current_view(&g);
2808  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
2809
2810  if (!m || owl_view_get_size(v)==0) {
2811    owl_function_error("No message selected\n");
2812    return(NULL);
2813  }
2814
2815  /* very simple handling of admin messages for now */
2816  if (owl_message_is_type_admin(m)) {
2817    return(owl_function_typefilt("admin"));
2818  }
2819
2820  /* very simple handling of loopback messages for now */
2821  if (owl_message_is_type_loopback(m)) {
2822    return(owl_function_typefilt("loopback"));
2823  }
2824
2825  /* aim messages */
2826  if (owl_message_is_type_aim(m)) {
2827    if (owl_message_is_direction_in(m)) {
2828      filtname=owl_function_aimuserfilt(owl_message_get_sender(m));
2829    } else if (owl_message_is_direction_out(m)) {
2830      filtname=owl_function_aimuserfilt(owl_message_get_recipient(m));
2831    }
2832    return(filtname);
2833  }
2834
2835  /* narrow personal and login messages to the sender or recip as appropriate */
2836  if (owl_message_is_type_zephyr(m)) {
2837    if (owl_message_is_personal(m) || owl_message_is_loginout(m)) {
2838      if (owl_message_is_direction_in(m)) {
2839        zperson=short_zuser(owl_message_get_sender(m));
2840      } else {
2841        zperson=short_zuser(owl_message_get_recipient(m));
2842      }
2843      filtname=owl_function_zuserfilt(zperson);
2844      owl_free(zperson);
2845      return(filtname);
2846    }
2847
2848    /* narrow class MESSAGE, instance foo, recip * messages to class, inst */
2849    if (!strcasecmp(owl_message_get_class(m), "message")) {
2850      filtname=owl_function_classinstfilt(owl_message_get_class(m), owl_message_get_instance(m));
2851      return(filtname);
2852    }
2853
2854    /* otherwise narrow to the class */
2855    if (type==0) {
2856      filtname=owl_function_classinstfilt(owl_message_get_class(m), NULL);
2857    } else if (type==1) {
2858      filtname=owl_function_classinstfilt(owl_message_get_class(m), owl_message_get_instance(m));
2859    }
2860    return(filtname);
2861  }
2862
2863  /* pass it off to perl */
2864  if(type) {
2865    argv[0] = "-i";
2866  };
2867  return owl_perlconfig_message_call_method(m, "smartfilter", type ? 1 : 0, argv);
2868}
2869
2870void owl_function_smartzpunt(int type)
2871{
2872  /* Starts a zpunt command based on the current class,instance pair.
2873   * If type=0, uses just class.  If type=1, uses instance as well. */
2874  owl_view *v;
2875  owl_message *m;
2876  char *cmd, *cmdprefix, *mclass, *minst;
2877 
2878  v=owl_global_get_current_view(&g);
2879  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
2880
2881  if (!m || owl_view_get_size(v)==0) {
2882    owl_function_error("No message selected\n");
2883    return;
2884  }
2885
2886  /* for now we skip admin messages. */
2887  if (owl_message_is_type_admin(m)
2888      || owl_message_is_loginout(m)
2889      || !owl_message_is_type_zephyr(m)) {
2890    owl_function_error("smartzpunt doesn't support this message type.");
2891    return;
2892  }
2893
2894  mclass = owl_message_get_class(m);
2895  minst = owl_message_get_instance(m);
2896  if (!mclass || !*mclass || *mclass==' '
2897      || (!strcasecmp(mclass, "message") && !strcasecmp(minst, "personal"))
2898      || (type && (!minst || !*minst|| *minst==' '))) {
2899    owl_function_error("smartzpunt can't safely do this for <%s,%s>",
2900                         mclass, minst);
2901  } else {
2902    cmdprefix = "start-command zpunt ";
2903    cmd = owl_malloc(strlen(cmdprefix)+strlen(mclass)+strlen(minst)+10);
2904    strcpy(cmd, cmdprefix);
2905    strcat(cmd, owl_getquoting(mclass));
2906    strcat(cmd, mclass);
2907    strcat(cmd, owl_getquoting(mclass));
2908    if (type) {
2909      strcat(cmd, " ");
2910      strcat(cmd, owl_getquoting(minst));
2911      strcat(cmd, minst);
2912      strcat(cmd, owl_getquoting(minst));
2913    } else {
2914      strcat(cmd, " *");
2915    }
2916    owl_function_command(cmd);
2917    owl_free(cmd);
2918  }
2919}
2920
2921/* Set the color of the current view's filter to
2922 * be 'color'
2923 */
2924void owl_function_color_current_filter(char *fgcolor, char *bgcolor)
2925{
2926  char *name;
2927
2928  name=owl_view_get_filtname(owl_global_get_current_view(&g));
2929  owl_function_color_filter(name, fgcolor, bgcolor);
2930}
2931
2932/* Set the color of the filter 'filter' to be 'color'.  If the color
2933 * name does not exist, return -1, if the filter does not exist or is
2934 * the "all" filter, return -2.  Return 0 on success
2935 */
2936int owl_function_color_filter(char *filtname, char *fgcolor, char *bgcolor)
2937{
2938  owl_filter *f;
2939
2940  f=owl_global_get_filter(&g, filtname);
2941  if (!f) {
2942    owl_function_error("Unknown filter");
2943    return(-2);
2944  }
2945
2946  /* don't touch the all filter */
2947  if (!strcmp(filtname, "all")) {
2948    owl_function_error("You may not change the 'all' filter.");
2949    return(-2);
2950  }
2951
2952  if (owl_util_string_to_color(fgcolor)==-1) {
2953    owl_function_error("No color named '%s' avilable.", fgcolor);
2954    return(-1);
2955  }
2956
2957
2958  if (bgcolor != NULL) {
2959    if (owl_util_string_to_color(bgcolor)==-1) {
2960      owl_function_error("No color named '%s' avilable.", bgcolor);
2961      return(-1);
2962    }
2963    owl_filter_set_bgcolor(f, owl_util_string_to_color(bgcolor));
2964  }
2965  owl_filter_set_fgcolor(f, owl_util_string_to_color(fgcolor));
2966 
2967  owl_global_set_needrefresh(&g);
2968  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2969  return(0);
2970}
2971
2972void owl_function_show_colors()
2973{
2974  owl_fmtext fm;
2975  int i; 
2976 
2977  owl_fmtext_init_null(&fm);
2978  owl_fmtext_append_normal(&fm, "default: ");
2979  owl_fmtext_append_normal_color(&fm, "default\n", OWL_COLOR_DEFAULT, OWL_COLOR_DEFAULT);
2980
2981  owl_fmtext_append_normal(&fm,"red:      ");
2982  owl_fmtext_append_normal_color(&fm, "red\n", OWL_COLOR_RED, OWL_COLOR_DEFAULT);
2983
2984  owl_fmtext_append_normal(&fm,"green:    ");
2985  owl_fmtext_append_normal_color(&fm, "green\n", OWL_COLOR_GREEN, OWL_COLOR_DEFAULT);
2986
2987  owl_fmtext_append_normal(&fm,"yellow:   ");
2988  owl_fmtext_append_normal_color(&fm, "yellow\n", OWL_COLOR_YELLOW, OWL_COLOR_DEFAULT);
2989
2990  owl_fmtext_append_normal(&fm,"blue:     ");
2991  owl_fmtext_append_normal_color(&fm, "blue\n", OWL_COLOR_BLUE, OWL_COLOR_DEFAULT);
2992
2993  owl_fmtext_append_normal(&fm,"magenta:  ");
2994  owl_fmtext_append_normal_color(&fm, "magenta\n", OWL_COLOR_MAGENTA, OWL_COLOR_DEFAULT);
2995
2996  owl_fmtext_append_normal(&fm,"cyan:     ");
2997  owl_fmtext_append_normal_color(&fm, "cyan\n", OWL_COLOR_CYAN, OWL_COLOR_DEFAULT);
2998
2999  owl_fmtext_append_normal(&fm,"white:    ");
3000  owl_fmtext_append_normal_color(&fm, "white\n", OWL_COLOR_WHITE, OWL_COLOR_DEFAULT);
3001
3002  for(i = 8; i < COLORS; ++i) {
3003    char* str1 = owl_sprintf("%4i:     ",i);
3004    char* str2 = owl_sprintf("%i\n",i);
3005    owl_fmtext_append_normal(&fm,str1);
3006    owl_fmtext_append_normal_color(&fm, str2, i, OWL_COLOR_DEFAULT);
3007    owl_free(str1);
3008     owl_free(str2);
3009  }
3010 
3011  owl_function_popless_fmtext(&fm);
3012  owl_fmtext_free(&fm);
3013}
3014
3015/* add the given class, inst, recip to the punt list for filtering.
3016 *   if direction==0 then punt
3017 *   if direction==1 then unpunt
3018 */
3019void owl_function_zpunt(char *class, char *inst, char *recip, int direction)
3020{
3021  char *buff;
3022  char *quoted;
3023
3024  buff=malloc(strlen(class)+strlen(inst)+strlen(recip)+100);
3025  strcpy(buff, "class");
3026  if (!strcmp(class, "*")) {
3027    strcat(buff, " .*");
3028  } else {
3029    quoted=owl_text_quote(class, OWL_REGEX_QUOTECHARS, OWL_REGEX_QUOTEWITH);
3030    owl_text_tr(quoted, ' ', '.');
3031    owl_text_tr(quoted, '\'', '.');
3032    owl_text_tr(quoted, '"', '.');
3033    sprintf(buff, "%s ^(un)*%s(\\.d)*$", buff, quoted);
3034    owl_free(quoted);
3035  }
3036  if (!strcmp(inst, "*")) {
3037    strcat(buff, " and instance .*");
3038  } else {
3039    quoted=owl_text_quote(inst, OWL_REGEX_QUOTECHARS, OWL_REGEX_QUOTEWITH);
3040    owl_text_tr(quoted, ' ', '.');
3041    owl_text_tr(quoted, '\'', '.');
3042    owl_text_tr(quoted, '"', '.');
3043    sprintf(buff, "%s and instance ^(un)*%s(\\.d)*$", buff, quoted);
3044    owl_free(quoted);
3045  }
3046  if (strcmp(recip, "*")) {
3047    quoted=owl_text_quote(recip, OWL_REGEX_QUOTECHARS, OWL_REGEX_QUOTEWITH);
3048    owl_text_tr(quoted, ' ', '.');
3049    owl_text_tr(quoted, '\'', '.');
3050    owl_text_tr(quoted, '"', '.');
3051    sprintf(buff, "%s and recipient ^%s$", buff, quoted);
3052    owl_free(quoted);
3053  }
3054
3055  owl_function_punt(buff, direction);
3056  owl_free(buff);
3057}
3058
3059void owl_function_punt(char *filter, int direction)
3060{
3061  owl_filter *f;
3062  owl_list *fl;
3063  int ret, i, j;
3064  fl=owl_global_get_puntlist(&g);
3065
3066  /* first, create the filter */
3067  f=malloc(sizeof(owl_filter));
3068
3069  owl_function_debugmsg("About to filter %s", filter);
3070  ret=owl_filter_init_fromstring(f, "punt-filter", filter);
3071  if (ret) {
3072    owl_function_error("Error creating filter for zpunt");
3073    owl_filter_free(f);
3074    return;
3075  }
3076
3077  /* Check for an identical filter */
3078  j=owl_list_get_size(fl);
3079  for (i=0; i<j; i++) {
3080    if (owl_filter_equiv(f, owl_list_get_element(fl, i))) {
3081      owl_function_debugmsg("found an equivalent punt filter");
3082      /* if we're punting, then just silently bow out on this duplicate */
3083      if (direction==0) {
3084        owl_filter_free(f);
3085        return;
3086      }
3087
3088      /* if we're unpunting, then remove this filter from the puntlist */
3089      if (direction==1) {
3090        owl_filter_free(owl_list_get_element(fl, i));
3091        owl_list_remove_element(fl, i);
3092        owl_filter_free(f);
3093        return;
3094      }
3095    }
3096  }
3097
3098  owl_function_debugmsg("punting");
3099  /* If we're punting, add the filter to the global punt list */
3100  if (direction==0) {
3101    owl_list_append_element(fl, f);
3102  }
3103}
3104
3105void owl_function_activate_keymap(char *keymap)
3106{
3107  if (!owl_keyhandler_activate(owl_global_get_keyhandler(&g), keymap)) {
3108    owl_function_error("Unable to activate keymap '%s'", keymap);
3109  }
3110}
3111
3112void owl_function_show_keymaps()
3113{
3114  owl_list l;
3115  owl_fmtext fm;
3116  owl_keymap *km;
3117  owl_keyhandler *kh;
3118  int i, numkm;
3119  char *kmname;
3120
3121  kh = owl_global_get_keyhandler(&g);
3122  owl_fmtext_init_null(&fm);
3123  owl_fmtext_append_bold(&fm, "Keymaps:   ");
3124  owl_fmtext_append_normal(&fm, "(use 'show keymap <name>' for details)\n");
3125  owl_keyhandler_get_keymap_names(kh, &l);
3126  owl_fmtext_append_list(&fm, &l, "\n", owl_function_keymap_summary);
3127  owl_fmtext_append_normal(&fm, "\n");
3128
3129  numkm = owl_list_get_size(&l);
3130  for (i=0; i<numkm; i++) {
3131    kmname = owl_list_get_element(&l, i);
3132    km = owl_keyhandler_get_keymap(kh, kmname);
3133    owl_fmtext_append_bold(&fm, "\n\n----------------------------------------------------------------------------------------------------\n\n");
3134    owl_keymap_get_details(km, &fm);   
3135  }
3136  owl_fmtext_append_normal(&fm, "\n");
3137 
3138  owl_function_popless_fmtext(&fm);
3139  owl_keyhandler_keymap_namelist_free(&l);
3140  owl_fmtext_free(&fm);
3141}
3142
3143char *owl_function_keymap_summary(void *name)
3144{
3145  owl_keymap *km
3146    = owl_keyhandler_get_keymap(owl_global_get_keyhandler(&g), name);
3147  if (km) return owl_keymap_summary(km);
3148  else return(NULL);
3149}
3150
3151/* TODO: implement for real */
3152void owl_function_show_keymap(char *name)
3153{
3154  owl_fmtext fm;
3155  owl_keymap *km;
3156
3157  owl_fmtext_init_null(&fm);
3158  km = owl_keyhandler_get_keymap(owl_global_get_keyhandler(&g), name);
3159  if (km) {
3160    owl_keymap_get_details(km, &fm);
3161  } else {
3162    owl_fmtext_append_normal(&fm, "No such keymap...\n");
3163  } 
3164  owl_function_popless_fmtext(&fm);
3165  owl_fmtext_free(&fm);
3166}
3167
3168void owl_function_help_for_command(char *cmdname)
3169{
3170  owl_fmtext fm;
3171
3172  owl_fmtext_init_null(&fm);
3173  owl_cmd_get_help(owl_global_get_cmddict(&g), cmdname, &fm);
3174  owl_function_popless_fmtext(&fm); 
3175  owl_fmtext_free(&fm);
3176}
3177
3178void owl_function_search_start(char *string, int direction)
3179{
3180  /* direction is OWL_DIRECTION_DOWNWARDS or OWL_DIRECTION_UPWARDS */
3181  owl_global_set_search_active(&g, string);
3182  owl_function_search_helper(0, direction);
3183}
3184
3185void owl_function_search_continue(int direction)
3186{
3187  /* direction is OWL_DIRECTION_DOWNWARDS or OWL_DIRECTION_UPWARDS */
3188  owl_function_search_helper(1, direction);
3189}
3190
3191void owl_function_search_helper(int mode, int direction)
3192{
3193  /* move to a message that contains the string.  If direction is
3194   * OWL_DIRECTION_DOWNWARDS then search fowards, if direction is
3195   * OWL_DIRECTION_UPWARDS then search backwards.
3196   *
3197   * If mode==0 then it will stay on the current message if it
3198   * contains the string.
3199   */
3200
3201  owl_view *v;
3202  int viewsize, i, curmsg, start;
3203  owl_message *m;
3204
3205  v=owl_global_get_current_view(&g);
3206  viewsize=owl_view_get_size(v);
3207  curmsg=owl_global_get_curmsg(&g);
3208 
3209  if (viewsize==0) {
3210    owl_function_error("No messages present");
3211    return;
3212  }
3213
3214  if (mode==0) {
3215    start=curmsg;
3216  } else if (direction==OWL_DIRECTION_DOWNWARDS) {
3217    start=curmsg+1;
3218  } else {
3219    start=curmsg-1;
3220  }
3221
3222  /* bounds check */
3223  if (start>=viewsize || start<0) {
3224    owl_function_error("No further matches found");
3225    return;
3226  }
3227
3228  for (i=start; i<viewsize && i>=0;) {
3229    m=owl_view_get_element(v, i);
3230    if (owl_message_search(m, owl_global_get_search_string(&g))) {
3231      owl_global_set_curmsg(&g, i);
3232      owl_function_calculate_topmsg(direction);
3233      owl_mainwin_redisplay(owl_global_get_mainwin(&g));
3234      if (direction==OWL_DIRECTION_DOWNWARDS) {
3235        owl_global_set_direction_downwards(&g);
3236      } else {
3237        owl_global_set_direction_upwards(&g);
3238      }
3239      return;
3240    }
3241    if (direction==OWL_DIRECTION_DOWNWARDS) {
3242      i++;
3243    } else {
3244      i--;
3245    }
3246  }
3247  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
3248  owl_function_error("No matches found");
3249}
3250
3251/* strips formatting from ztext and returns the unformatted text.
3252 * caller is responsible for freeing. */
3253char *owl_function_ztext_stylestrip(char *zt)
3254{
3255  owl_fmtext fm;
3256  char *plaintext;
3257
3258  owl_fmtext_init_null(&fm);
3259  owl_fmtext_append_ztext(&fm, zt);
3260  plaintext = owl_fmtext_print_plain(&fm);
3261  owl_fmtext_free(&fm);
3262  return(plaintext);
3263}
3264
3265/* Popup a buddylisting.  If filename is NULL use the default .anyone */
3266void owl_function_buddylist(int aim, int zephyr, char *filename)
3267{
3268  int i, j, x, idle;
3269  owl_fmtext fm;
3270  owl_buddylist *bl;
3271  owl_buddy *b;
3272  owl_list anyone;
3273  char *foo, *timestr;
3274#ifdef HAVE_LIBZEPHYR
3275  char *tmp, *user, *line;
3276  ZLocations_t location[200];
3277  int numlocs, ret;
3278#endif
3279
3280  owl_fmtext_init_null(&fm);
3281
3282  /* AIM first */
3283  if (aim && owl_global_is_aimloggedin(&g)) {
3284    bl=owl_global_get_buddylist(&g);
3285
3286    owl_fmtext_append_bold(&fm, "AIM users logged in:\n");
3287    /* we're assuming AIM for now */
3288    j=owl_buddylist_get_size(bl);
3289    for (i=0; i<j; i++) {
3290      b=owl_buddylist_get_buddy_n(bl, i);
3291      idle=owl_buddy_get_idle_time(b);
3292      if (idle!=0) {
3293        timestr=owl_util_minutes_to_timestr(idle);
3294      } else {
3295        timestr=owl_strdup("");
3296      }
3297      foo=owl_sprintf("  %-20.20s %-12.12s\n", owl_buddy_get_name(b), timestr);
3298      owl_fmtext_append_normal(&fm, foo);
3299      owl_free(timestr);
3300      owl_free(foo);
3301    }
3302  }
3303
3304#ifdef HAVE_LIBZEPHYR
3305  if (zephyr) {
3306    if(!owl_global_is_havezephyr(&g)) {
3307      owl_function_error("Zephyr currently not available.");
3308    } else {
3309      owl_fmtext_append_bold(&fm, "Zephyr users logged in:\n");
3310      owl_list_create(&anyone);
3311      ret=owl_zephyr_get_anyone_list(&anyone, filename);
3312      if (ret) {
3313        owl_fmtext_append_normal(&fm, "  Error opening file for zephyr buddies.\n");
3314      } else {
3315        j=owl_list_get_size(&anyone);
3316        for (i=0; i<j; i++) {
3317          user=owl_list_get_element(&anyone, i);
3318          ret=ZLocateUser(user, &numlocs, ZAUTH);
3319          if (ret!=ZERR_NONE) {
3320            owl_function_error("Error getting location for %s", user);
3321            continue;
3322          }
3323
3324          numlocs=200;
3325          ret=ZGetLocations(location, &numlocs);
3326          if (ret==0) {
3327            for (x=0; x<numlocs; x++) {
3328              line=malloc(strlen(location[x].host)+strlen(location[x].time)+strlen(location[x].tty)+100);
3329              tmp=short_zuser(user);
3330              sprintf(line, "  %-10.10s %-24.24s %-12.12s  %20.20s\n",
3331                      tmp,
3332                      location[x].host,
3333                      location[x].tty,
3334                      location[x].time);
3335              owl_fmtext_append_normal(&fm, line);
3336              owl_free(tmp);
3337              owl_free(line);
3338            }
3339            if (numlocs>=200) {
3340              owl_fmtext_append_normal(&fm, "  Too many locations found for this user, truncating.\n");
3341            }
3342          }
3343        }
3344      }
3345      owl_list_free_all(&anyone, owl_free);
3346    } 
3347  }
3348#endif
3349
3350  if(aim && zephyr) {
3351      if(owl_perlconfig_is_function("BarnOwl::Hooks::_get_blist")) {
3352          char * perlblist = owl_perlconfig_execute("BarnOwl::Hooks::_get_blist()");
3353          if(perlblist) {
3354              owl_fmtext_append_ztext(&fm, perlblist);
3355              owl_free(perlblist);
3356          }
3357      }
3358  }
3359 
3360  owl_function_popless_fmtext(&fm);
3361  owl_fmtext_free(&fm);
3362}
3363
3364/* Dump messages in the current view to the file 'filename'. */
3365void owl_function_dump(char *filename) 
3366{
3367  int i, j, count;
3368  owl_message *m;
3369  owl_view *v;
3370  FILE *file;
3371
3372  v=owl_global_get_current_view(&g);
3373
3374  /* in the future make it ask yes/no */
3375  /*
3376  ret=stat(filename, &sbuf);
3377  if (!ret) {
3378    ret=owl_function_askyesno("File exists, continue? [Y/n]");
3379    if (!ret) return;
3380  }
3381  */
3382
3383  file=fopen(filename, "w");
3384  if (!file) {
3385    owl_function_error("Error opening file");
3386    return;
3387  }
3388
3389  count=0;
3390  j=owl_view_get_size(v);
3391  for (i=0; i<j; i++) {
3392    m=owl_view_get_element(v, i);
3393    fputs(owl_message_get_text(m), file);
3394  }
3395  fclose(file);
3396  owl_function_makemsg("Messages dumped to %s", filename);
3397}
3398
3399void owl_function_do_newmsgproc(void)
3400{
3401  if (owl_global_get_newmsgproc(&g) && strcmp(owl_global_get_newmsgproc(&g), "")) {
3402    /* if there's a process out there, we need to check on it */
3403    if (owl_global_get_newmsgproc_pid(&g)) {
3404      owl_function_debugmsg("Checking on newmsgproc pid==%i", owl_global_get_newmsgproc_pid(&g));
3405      owl_function_debugmsg("Waitpid return is %i", waitpid(owl_global_get_newmsgproc_pid(&g), NULL, WNOHANG));
3406      waitpid(owl_global_get_newmsgproc_pid(&g), NULL, WNOHANG);
3407      if (waitpid(owl_global_get_newmsgproc_pid(&g), NULL, WNOHANG)==-1) {
3408        /* it exited */
3409        owl_global_set_newmsgproc_pid(&g, 0);
3410        owl_function_debugmsg("newmsgproc exited");
3411      } else {
3412        owl_function_debugmsg("newmsgproc did not exit");
3413      }
3414    }
3415   
3416    /* if it exited, fork & exec a new one */
3417    if (owl_global_get_newmsgproc_pid(&g)==0) {
3418      int i, myargc;
3419      i=fork();
3420      if (i) {
3421        /* parent set the child's pid */
3422        owl_global_set_newmsgproc_pid(&g, i);
3423        owl_function_debugmsg("I'm the parent and I started a new newmsgproc with pid %i", i);
3424      } else {
3425        /* child exec's the program */
3426        char **parsed;
3427        parsed=owl_parseline(owl_global_get_newmsgproc(&g), &myargc);
3428        if (myargc < 0) {
3429          owl_function_debugmsg("Could not parse newmsgproc '%s': unbalanced quotes?", owl_global_get_newmsgproc(&g));
3430        }
3431        if (myargc <= 0) {
3432          _exit(127);
3433        }
3434        parsed=realloc(parsed, sizeof(*parsed) * (myargc+1));
3435        parsed[myargc] = NULL;
3436       
3437        owl_function_debugmsg("About to exec \"%s\" with %d arguments", parsed[0], myargc);
3438       
3439        execvp(parsed[0], parsed);
3440       
3441       
3442        /* was there an error exec'ing? */
3443        owl_function_debugmsg("Cannot run newmsgproc '%s': cannot exec '%s': %s", 
3444                              owl_global_get_newmsgproc(&g), parsed[0], strerror(errno));
3445        _exit(127);
3446      }
3447    }
3448  }
3449}
3450
3451/* print the xterm escape sequence to raise the window */
3452void owl_function_xterm_raise(void)
3453{
3454  printf("\033[5t");
3455}
3456
3457/* print the xterm escape sequence to deiconify the window */
3458void owl_function_xterm_deiconify(void)
3459{
3460  printf("\033[1t");
3461}
3462
3463/* Add the specified command to the startup file.  Eventually this
3464 * should be clever, and rewriting settings that will obviosly
3465 * override earlier settings with 'set' 'bindkey' and 'alias'
3466 * commands.  For now though we just remove any line that would
3467 * duplicate this one and then append this line to the end of
3468 * startupfile.
3469 */
3470void owl_function_addstartup(char *buff)
3471{
3472  FILE *file;
3473  char *filename;
3474
3475  filename=owl_global_get_startupfile(&g);
3476  file=fopen(filename, "a");
3477  if (!file) {
3478    owl_function_error("Error opening startupfile for new command");
3479    return;
3480  }
3481
3482  /* delete earlier copies */
3483  owl_util_file_deleteline(filename, buff, 1);
3484
3485  /* add this line */
3486  fprintf(file, "%s\n", buff);
3487
3488  fclose(file);
3489}
3490
3491/* Remove the specified command from the startup file. */
3492void owl_function_delstartup(char *buff)
3493{
3494  char *filename;
3495  filename=owl_global_get_startupfile(&g);
3496  owl_util_file_deleteline(filename, buff, 1);
3497}
3498
3499/* Execute owl commands from the given filename.  If the filename
3500 * is NULL, use the default owl startup commands file.
3501 */
3502void owl_function_source(char *filename)
3503{
3504  FILE *file;
3505  char buff[LINE];
3506
3507  if (!filename) {
3508    filename=owl_global_get_startupfile(&g);
3509    file=fopen(filename, "r");
3510  } else {
3511    file=fopen(filename, "r");
3512  }
3513  if (!file) {
3514    /* just fail silently if it doesn't exist */
3515    return;
3516  }
3517  while (fgets(buff, LINE, file)!=NULL) {
3518    if (buff[0] == '#') continue;
3519    buff[strlen(buff)-1]='\0';
3520    owl_function_command(buff);
3521  }
3522  fclose(file);
3523}
3524
3525void owl_function_change_style(owl_view *v, char *stylename)
3526{
3527  owl_style *s;
3528
3529  s=owl_global_get_style_by_name(&g, stylename);
3530  if (!s) {
3531    owl_function_error("No style named %s", stylename);
3532    return;
3533  }
3534  owl_view_set_style(v, s);
3535  owl_messagelist_invalidate_formats(owl_global_get_msglist(&g));
3536  owl_function_calculate_topmsg(OWL_DIRECTION_DOWNWARDS);
3537  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
3538}
3539
3540void owl_function_toggleoneline()
3541{
3542  owl_view *v;
3543  owl_style *s;
3544
3545  v=owl_global_get_current_view(&g);
3546  s=owl_view_get_style(v);
3547
3548  if (!owl_style_matches_name(s, "oneline")) {
3549    owl_function_change_style(v, "oneline");
3550  } else {
3551    owl_function_change_style(v, owl_global_get_default_style(&g));
3552  }
3553
3554  owl_messagelist_invalidate_formats(owl_global_get_msglist(&g));
3555  owl_function_calculate_topmsg(OWL_DIRECTION_DOWNWARDS);
3556  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
3557}
3558
3559void owl_function_error(char *fmt, ...)
3560{
3561  va_list ap;
3562  char buff[2048], buff2[2048];
3563  char *date;
3564  time_t now;
3565
3566  now=time(NULL);
3567  date=owl_strdup(ctime(&now));
3568  date[strlen(date)-1]='\0';
3569
3570  va_start(ap, fmt);
3571
3572  vsnprintf(buff, 2048, fmt, ap);
3573  sprintf(buff2, "%s %s", date, buff);
3574  owl_function_debugmsg("ERROR: %s", buff);
3575  if (owl_global_get_curs_msgwin(&g)) {
3576    werase(owl_global_get_curs_msgwin(&g));
3577    waddstr(owl_global_get_curs_msgwin(&g), buff); 
3578    wnoutrefresh(owl_global_get_curs_msgwin(&g));
3579    owl_global_set_needrefresh(&g);
3580  }
3581  owl_errqueue_append_err(owl_global_get_errqueue(&g), buff2);
3582  va_end(ap);
3583  owl_free(date);
3584}
3585
3586void owl_function_showerrs()
3587{
3588  owl_fmtext fm;
3589
3590  owl_fmtext_init_null(&fm);
3591  owl_fmtext_append_normal(&fm, "Errors:\n\n");
3592  owl_errqueue_to_fmtext(owl_global_get_errqueue(&g), &fm);
3593  owl_function_popless_fmtext(&fm);
3594}
3595
3596void owl_function_makemsg(char *fmt, ...)
3597{
3598  va_list ap;
3599  char buff[2048];
3600
3601  if (!owl_global_get_curs_msgwin(&g)) return;
3602
3603  va_start(ap, fmt);
3604  werase(owl_global_get_curs_msgwin(&g));
3605 
3606  vsnprintf(buff, 2048, fmt, ap);
3607  owl_function_debugmsg("makemsg: %s", buff);
3608  waddstr(owl_global_get_curs_msgwin(&g), buff); 
3609  wnoutrefresh(owl_global_get_curs_msgwin(&g));
3610  owl_global_set_needrefresh(&g);
3611  va_end(ap);
3612}
3613
3614/* get locations for everyone in .anyone.  If 'notify' is '1' then
3615 * send a pseudo login or logout message for everyone not in sync with
3616 * the global zephyr buddy list.  The list is updated regardless of
3617 * the status of 'notify'.
3618 */
3619void owl_function_zephyr_buddy_check(int notify)
3620{
3621#ifdef HAVE_LIBZEPHYR
3622  int i, j;
3623  owl_list anyone;
3624  owl_message *m;
3625  owl_zbuddylist *zbl;
3626  char *user;
3627  ZLocations_t location[200];
3628  int numlocs, ret;
3629
3630  zbl=owl_global_get_zephyr_buddylist(&g);
3631
3632  owl_list_create(&anyone);
3633  ret=owl_zephyr_get_anyone_list(&anyone, NULL);
3634
3635  j=owl_list_get_size(&anyone);
3636  for (i=0; i<j; i++) {
3637    user=owl_list_get_element(&anyone, i);
3638    ret=ZLocateUser(user, &numlocs, ZAUTH);
3639    if (ret!=ZERR_NONE) {
3640      owl_function_error("Error getting location for %s", user);
3641      continue;
3642    }
3643    numlocs=200;
3644    ret=ZGetLocations(location, &numlocs);
3645    if (ret==0) {
3646      if ((numlocs>0) && !owl_zbuddylist_contains_user(zbl, user)) {
3647        /* Send a PSEUDO LOGIN! */
3648        if (notify) {
3649          m=owl_malloc(sizeof(owl_message));
3650          owl_message_create_pseudo_zlogin(m, 0, user, location[0].host, location[0].time, location[0].tty);
3651          owl_global_messagequeue_addmsg(&g, m);
3652        }
3653        owl_zbuddylist_adduser(zbl, user);
3654        owl_function_debugmsg("owl_function_zephyr_buddy_check: login for %s ", user);
3655      } else if ((numlocs==0) && owl_zbuddylist_contains_user(zbl, user)) {
3656        /* I don't think this ever happens (if there are 0 locations we should get an error from
3657         * ZGetLocations)
3658         */
3659        owl_function_error("owl_function_zephyr_buddy_check: exceptional case logout for %s ",user);
3660      }
3661    } else if ((ret==ZERR_NOLOCATIONS) && owl_zbuddylist_contains_user(zbl, user)) {
3662      /* Send a PSEUDO LOGOUT! */
3663      if (notify) {
3664        m=owl_malloc(sizeof(owl_message));
3665        owl_message_create_pseudo_zlogin(m, 1, user, "", "", "");
3666        owl_global_messagequeue_addmsg(&g, m);
3667      }
3668      owl_zbuddylist_deluser(zbl, user);
3669      owl_function_debugmsg("owl_function_zephyr_buddy_check: logout for %s ",user);
3670    }
3671  }
3672
3673  owl_list_free_all(&anyone, owl_free);
3674#endif
3675}
3676
3677void owl_function_aimsearch_results(char *email, owl_list *namelist)
3678{
3679  owl_fmtext fm;
3680  int i, j;
3681
3682  owl_fmtext_init_null(&fm);
3683  owl_fmtext_append_normal(&fm, "AIM screennames associated with ");
3684  owl_fmtext_append_normal(&fm, email);
3685  owl_fmtext_append_normal(&fm, ":\n");
3686
3687  j=owl_list_get_size(namelist);
3688  for (i=0; i<j; i++) {
3689    owl_fmtext_append_normal(&fm, "  ");
3690    owl_fmtext_append_normal(&fm, owl_list_get_element(namelist, i));
3691    owl_fmtext_append_normal(&fm, "\n");
3692  }
3693
3694  owl_function_popless_fmtext(&fm);
3695  owl_fmtext_free(&fm);
3696}
3697
3698int owl_function_get_color_count()
3699{
3700     return COLORS;
3701}
Note: See TracBrowser for help on using the repository browser.