source: functions.c @ 1cdb459

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