source: functions.c @ 6249a88f

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