source: functions.c @ dab82f29

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