source: functions.c @ f98c74e

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