source: functions.c @ af1920fd

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