source: functions.c @ f98c74e

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