source: functions.c @ ee6b30f

Last change on this file since ee6b30f was ee6b30f, checked in by Anders Kaseorg <andersk@mit.edu>, 7 years ago
Avoid thread-unsafe functions ctime, localtime, strtok BarnOwl seems to be growing threads at an alarming rate, so let’s be sure these don’t turn into race conditions. Signed-off-by: Anders Kaseorg <andersk@mit.edu>
  • Property mode set to 100644
File size: 97.7 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  char *text = g_strdup_printf(
118    "BarnOwl version %s\n"
119    "Copyright (c) 2006-2011 The BarnOwl Developers. All rights reserved.\n"
120    "Copyright (c) 2004 James Kretchmar. All rights reserved.\n"
121    "\n"
122    "Redistribution and use in source and binary forms, with or without\n"
123    "modification, are permitted provided that the following conditions are\n"
124    "met:\n"
125    "\n"
126    "   * Redistributions of source code must retain the above copyright\n"
127    "     notice, this list of conditions and the following disclaimer.\n"
128    "\n"
129    "   * Redistributions in binary form must reproduce the above copyright\n"
130    "     notice, this list of conditions and the following disclaimer in\n"
131    "     the documentation and/or other materials provided with the\n"
132    "     distribution.\n"
133    "\n"
134    "   * Redistributions in any form must be accompanied by information on\n"
135    "     how to obtain complete source code for the BarnOwl software and any\n"
136    "     accompanying software that uses the BarnOwl software. The source code\n"
137    "     must either be included in the distribution or be available for no\n"
138    "     more than the cost of distribution plus a nominal fee, and must be\n"
139    "     freely redistributable under reasonable conditions. For an\n"
140    "     executable file, complete source code means the source code for\n"
141    "     all modules it contains. It does not include source code for\n"
142    "     modules or files that typically accompany the major components of\n"
143    "     the operating system on which the executable file runs.\n"
144    "\n"
145    "THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR\n"
146    "IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\n"
147    "WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR\n"
148    "NON-INFRINGEMENT, ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE\n"
149    "LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n"
150    "CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n"
151    "SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR\n"
152    "BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,\n"
153    "WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n"
154    "OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN\n"
155    "IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n",
156    version);
157  owl_function_popless_text(text);
158  g_free(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_slice_new(owl_message);
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_slice_new(owl_message);
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_slice_new(owl_message);
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_slice_new(owl_message);
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_slice_new(owl_message);
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_slice_new(owl_message);
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  if (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
995bool owl_function_is_away(void)
996{
997  return owl_global_is_zaway(&g) ||
998         owl_global_is_aaway(&g) ||
999         owl_perlconfig_perl_call_bool("BarnOwl::Hooks::_get_is_away", 0, NULL);
1000}
1001
1002void owl_function_quit(void)
1003{
1004  char *ret;
1005 
1006  /* zlog out if we need to */
1007  if (owl_global_is_havezephyr(&g) &&
1008      owl_global_is_shutdownlogout(&g)) {
1009    owl_zephyr_zlog_out();
1010  }
1011
1012  /* execute the commands in shutdown */
1013  ret = owl_perlconfig_execute("BarnOwl::Hooks::_shutdown();");
1014  g_free(ret);
1015
1016  /* signal our child process, if any */
1017  if (owl_global_get_newmsgproc_pid(&g)) {
1018    kill(owl_global_get_newmsgproc_pid(&g), SIGHUP);
1019  }
1020 
1021  /* Quit AIM */
1022  if (owl_global_is_aimloggedin(&g)) {
1023    owl_aim_logout();
1024  }
1025
1026  owl_function_debugmsg("Quitting BarnOwl");
1027  owl_select_quit_loop();
1028}
1029
1030void owl_function_calculate_topmsg(int direction)
1031{
1032  int recwinlines, topmsg, curmsg;
1033  const owl_view *v;
1034
1035  v=owl_global_get_current_view(&g);
1036  curmsg=owl_global_get_curmsg(&g);
1037  topmsg=owl_global_get_topmsg(&g);
1038  recwinlines=owl_global_get_recwin_lines(&g);
1039
1040  /*
1041  if (owl_view_get_size(v) < 1) {
1042    return;
1043  }
1044  */
1045
1046  switch (owl_global_get_scrollmode(&g)) {
1047  case OWL_SCROLLMODE_TOP:
1048    topmsg = owl_function_calculate_topmsg_top(direction, v, curmsg, topmsg, recwinlines);
1049    break;
1050  case OWL_SCROLLMODE_NEARTOP:
1051    topmsg = owl_function_calculate_topmsg_neartop(direction, v, curmsg, topmsg, recwinlines);
1052    break;
1053  case OWL_SCROLLMODE_CENTER:
1054    topmsg = owl_function_calculate_topmsg_center(direction, v, curmsg, topmsg, recwinlines);
1055    break;
1056  case OWL_SCROLLMODE_PAGED:
1057    topmsg = owl_function_calculate_topmsg_paged(direction, v, curmsg, topmsg, recwinlines, 0);
1058    break;
1059  case OWL_SCROLLMODE_PAGEDCENTER:
1060    topmsg = owl_function_calculate_topmsg_paged(direction, v, curmsg, topmsg, recwinlines, 1);
1061    break;
1062  case OWL_SCROLLMODE_NORMAL:
1063  default:
1064    topmsg = owl_function_calculate_topmsg_normal(direction, v, curmsg, topmsg, recwinlines);
1065  }
1066  owl_function_debugmsg("Calculated a topmsg of %i", topmsg);
1067  owl_global_set_topmsg(&g, topmsg);
1068}
1069
1070/* Returns what the new topmsg should be. 
1071 * Passed the last direction of movement,
1072 * the current view,
1073 * the current message number in the view,
1074 * the top message currently being displayed,
1075 * and the number of lines in the recwin.
1076 */
1077int owl_function_calculate_topmsg_top(int direction, const owl_view *v, int curmsg, int topmsg, int recwinlines)
1078{
1079  return(curmsg);
1080}
1081
1082int owl_function_calculate_topmsg_neartop(int direction, const owl_view *v, int curmsg, int topmsg, int recwinlines)
1083{
1084  if (curmsg>0 
1085      && (owl_message_get_numlines(owl_view_get_element(v, curmsg-1))
1086          <  recwinlines/2)) {
1087    return(curmsg-1);
1088  } else {
1089    return(curmsg);
1090  }
1091}
1092 
1093int owl_function_calculate_topmsg_center(int direction, const owl_view *v, int curmsg, int topmsg, int recwinlines)
1094{
1095  int i, last, lines;
1096
1097  last = curmsg;
1098  lines = 0;
1099  for (i=curmsg-1; i>=0; i--) {
1100    lines += owl_message_get_numlines(owl_view_get_element(v, i));
1101    if (lines > recwinlines/2) break;
1102    last = i;
1103  }
1104  return(last);
1105}
1106 
1107int owl_function_calculate_topmsg_paged(int direction, const owl_view *v, int curmsg, int topmsg, int recwinlines, int center_on_page)
1108{
1109  int i, last, lines, savey;
1110 
1111  /* If we're off the top of the screen, scroll up such that the
1112   * curmsg is near the botton of the screen. */
1113  if (curmsg < topmsg) {
1114    last = curmsg;
1115    lines = 0;
1116    for (i=curmsg; i>=0; i--) {
1117      lines += owl_message_get_numlines(owl_view_get_element(v, i));
1118      if (lines > recwinlines) break;
1119    last = i;
1120    }
1121    if (center_on_page) {
1122      return(owl_function_calculate_topmsg_center(direction, v, curmsg, 0, recwinlines));
1123    } else {
1124      return(last);
1125    }
1126  }
1127
1128  /* Find number of lines from top to bottom of curmsg (store in savey) */
1129  savey=0;
1130  for (i=topmsg; i<=curmsg; i++) {
1131    savey+=owl_message_get_numlines(owl_view_get_element(v, i));
1132  }
1133
1134  /* if we're off the bottom of the screen, scroll down */
1135  if (savey > recwinlines) {
1136    if (center_on_page) {
1137      return(owl_function_calculate_topmsg_center(direction, v, curmsg, 0, recwinlines));
1138    } else {
1139      return(curmsg);
1140    }
1141  }
1142
1143  /* else just stay as we are... */
1144  return(topmsg);
1145}
1146
1147int owl_function_calculate_topmsg_normal(int direction, const owl_view *v, int curmsg, int topmsg, int recwinlines)
1148{
1149  int savey, i, foo, y;
1150
1151  if (curmsg<0) return(topmsg);
1152   
1153  /* If we're off the top of the screen then center */
1154  if (curmsg<topmsg) {
1155    topmsg=owl_function_calculate_topmsg_center(direction, v, curmsg, 0, recwinlines);
1156  }
1157
1158  /* If curmsg is so far past topmsg that there are more messages than
1159     lines, skip the line counting that follows because we're
1160     certainly off screen.  */
1161  savey=curmsg-topmsg;
1162  if (savey <= recwinlines) {
1163    /* Find number of lines from top to bottom of curmsg (store in savey) */
1164    savey = 0;
1165    for (i=topmsg; i<=curmsg; i++) {
1166      savey+=owl_message_get_numlines(owl_view_get_element(v, i));
1167    }
1168  }
1169
1170  /* If we're off the bottom of the screen, set the topmsg to curmsg
1171   * and scroll upwards */
1172  if (savey > recwinlines) {
1173    topmsg=curmsg;
1174    savey=owl_message_get_numlines(owl_view_get_element(v, curmsg));
1175    direction=OWL_DIRECTION_UPWARDS;
1176  }
1177 
1178  /* If our bottom line is less than 1/4 down the screen then scroll up */
1179  if (direction == OWL_DIRECTION_UPWARDS || direction == OWL_DIRECTION_NONE) {
1180    if (savey < (recwinlines / 4)) {
1181      y=0;
1182      for (i=curmsg; i>=0; i--) {
1183        foo=owl_message_get_numlines(owl_view_get_element(v, i));
1184        /* will we run the curmsg off the screen? */
1185        if ((foo+y) >= recwinlines) {
1186          i++;
1187          if (i>curmsg) i=curmsg;
1188          break;
1189        }
1190        /* have saved 1/2 the screen space? */
1191        y+=foo;
1192        if (y > (recwinlines / 2)) break;
1193      }
1194      if (i<0) i=0;
1195      return(i);
1196    }
1197  }
1198
1199  if (direction == OWL_DIRECTION_DOWNWARDS || direction == OWL_DIRECTION_NONE) {
1200    /* If curmsg bottom line is more than 3/4 down the screen then scroll down */
1201    if (savey > ((recwinlines * 3)/4)) {
1202      y=0;
1203      /* count lines from the top until we can save 1/2 the screen size */
1204      for (i=topmsg; i<curmsg; i++) {
1205        y+=owl_message_get_numlines(owl_view_get_element(v, i));
1206        if (y > (recwinlines / 2)) break;
1207      }
1208      if (i==curmsg) {
1209        i--;
1210      }
1211      return(i+1);
1212    }
1213  }
1214
1215  return(topmsg);
1216}
1217
1218void owl_function_resize(void)
1219{
1220  owl_global_set_resize_pending(&g);
1221}
1222
1223void G_GNUC_PRINTF(1, 2) owl_function_debugmsg(const char *fmt, ...)
1224{
1225  char *tmpbuff;
1226  FILE *file;
1227  time_t now;
1228  struct tm tm;
1229  va_list ap;
1230
1231  if (!owl_global_is_debug_fast(&g))
1232    return;
1233
1234  file = owl_global_get_debug_file_handle(&g);
1235  if (!file) /* XXX should report this */
1236    return;
1237
1238  now = time(NULL);
1239
1240  tmpbuff = owl_util_format_time(localtime_r(&now, &tm));
1241  fprintf(file, "[%d -  %s - %lds]: ",
1242          (int) getpid(), tmpbuff, now - owl_global_get_starttime(&g));
1243  g_free(tmpbuff);
1244
1245  va_start(ap, fmt);
1246  vfprintf(file, fmt, ap);
1247  va_end(ap);
1248
1249  putc('\n', file);
1250  fflush(file);
1251}
1252
1253void owl_function_beep(void)
1254{
1255  if (owl_global_is_bell(&g)) {
1256    beep();
1257  }
1258}
1259
1260int owl_function_subscribe(const char *class, const char *inst, const char *recip)
1261{
1262  int ret;
1263
1264  ret=owl_zephyr_sub(class, inst, recip);
1265  if (ret) {
1266    owl_function_error("Error subscribing.");
1267  } else {
1268    owl_function_makemsg("Subscribed.");
1269  }
1270  return(ret);
1271}
1272
1273void owl_function_unsubscribe(const char *class, const char *inst, const char *recip)
1274{
1275  int ret;
1276
1277  ret=owl_zephyr_unsub(class, inst, recip);
1278  if (ret) {
1279    owl_function_error("Error subscribing.");
1280  } else {
1281    owl_function_makemsg("Unsubscribed.");
1282  }
1283}
1284
1285static void _dirty_everything(gpointer data, gpointer user_data) {
1286  owl_window *w = data;
1287  if (!owl_window_is_realized(w))
1288    return;
1289  owl_window_dirty(w);
1290  owl_window_children_foreach(w, _dirty_everything, NULL);
1291}
1292
1293void owl_function_full_redisplay(void)
1294{
1295  /* Ask every widget to redraw itself. */
1296  _dirty_everything(owl_window_get_screen(), NULL);
1297  /* Force ncurses to redisplay everything. */
1298  clearok(stdscr, TRUE);
1299}
1300
1301void owl_function_popless_text(const char *text)
1302{
1303  owl_popwin *pw;
1304  owl_viewwin *v;
1305
1306  if (owl_global_get_popwin(&g) || owl_global_get_viewwin(&g)) {
1307    owl_function_error("Popwin already in use.");
1308    return;
1309  }
1310  pw = owl_popwin_new();
1311  owl_global_set_popwin(&g, pw);
1312  owl_popwin_up(pw);
1313
1314  v = owl_viewwin_new_text(owl_popwin_get_content(pw), text);
1315  owl_global_set_viewwin(&g, v);
1316
1317  owl_global_push_context(&g, OWL_CTX_POPLESS, v, "popless", NULL);
1318}
1319
1320void owl_function_popless_fmtext(const owl_fmtext *fm)
1321{
1322  owl_popwin *pw;
1323  owl_viewwin *v;
1324
1325  if (owl_global_get_popwin(&g) || owl_global_get_viewwin(&g)) {
1326    owl_function_error("Popwin already in use.");
1327    return;
1328  }
1329  pw = owl_popwin_new();
1330  owl_global_set_popwin(&g, pw);
1331  owl_popwin_up(pw);
1332
1333  v = owl_viewwin_new_fmtext(owl_popwin_get_content(pw), fm);
1334  owl_global_set_viewwin(&g, v);
1335
1336  owl_global_push_context(&g, OWL_CTX_POPLESS, v, "popless", NULL);
1337}
1338
1339void owl_function_popless_file(const char *filename)
1340{
1341  owl_fmtext fm;
1342  FILE *file;
1343  char *s = NULL;
1344
1345  file=fopen(filename, "r");
1346  if (!file) {
1347    owl_function_error("Could not open file: %s", filename);
1348    return;
1349  }
1350
1351  owl_fmtext_init_null(&fm);
1352  while (owl_getline(&s, file))
1353    owl_fmtext_append_normal(&fm, s);
1354  g_free(s);
1355
1356  owl_function_popless_fmtext(&fm);
1357  owl_fmtext_cleanup(&fm);
1358  fclose(file);
1359}
1360
1361void owl_function_about(void)
1362{
1363  char *text = g_strdup_printf(
1364    "This is BarnOwl version %s.\n\n"
1365    "BarnOwl is a fork of the Owl zephyr client, written and\n"
1366    "maintained by Alejandro Sedeno and Nelson Elhage at the\n"
1367    "Massachusetts Institute of Technology. \n"
1368    "\n"
1369    "Owl was written by James Kretchmar. The first version, 0.5, was\n"
1370    "released in March 2002.\n"
1371    "\n"
1372    "The name 'owl' was chosen in reference to the owls in the\n"
1373    "Harry Potter novels, who are tasked with carrying messages\n"
1374    "between Witches and Wizards. The name 'BarnOwl' was chosen\n"
1375    "because we feel our owls should live closer to our ponies.\n"
1376    "\n"
1377    "Copyright (c) 2006-2011 The BarnOwl Developers. All rights reserved.\n"
1378    "Copyright (c) 2004 James Kretchmar. All rights reserved.\n"
1379    "Copyright 2002 Massachusetts Institute of Technology\n"
1380    "\n"
1381    "This program is free software. You can redistribute it and/or\n"
1382    "modify under the terms of the Sleepycat License. Use the \n"
1383      "':show license' command to display the full license\n",
1384      version);
1385  owl_function_popless_text(text);
1386  g_free(text);
1387}
1388
1389void owl_function_info(void)
1390{
1391  const owl_message *m;
1392  owl_fmtext fm, attrfm;
1393  const owl_view *v;
1394  char *time;
1395#ifdef HAVE_LIBZEPHYR
1396  const ZNotice_t *n;
1397#endif
1398
1399  owl_fmtext_init_null(&fm);
1400 
1401  v=owl_global_get_current_view(&g);
1402  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
1403  if (!m || owl_view_get_size(v)==0) {
1404    owl_function_error("No message selected\n");
1405    return;
1406  }
1407
1408  owl_fmtext_append_bold(&fm, "General Information:\n");
1409  owl_fmtext_appendf_normal(&fm, "  Msg Id    : %i\n", owl_message_get_id(m));
1410
1411  owl_fmtext_append_normal(&fm, "  Type      : ");
1412  owl_fmtext_append_bold(&fm, owl_message_get_type(m));
1413  owl_fmtext_append_normal(&fm, "\n");
1414
1415  if (owl_message_is_direction_in(m)) {
1416    owl_fmtext_append_normal(&fm, "  Direction : in\n");
1417  } else if (owl_message_is_direction_out(m)) {
1418    owl_fmtext_append_normal(&fm, "  Direction : out\n");
1419  } else if (owl_message_is_direction_none(m)) {
1420    owl_fmtext_append_normal(&fm, "  Direction : none\n");
1421  } else {
1422    owl_fmtext_append_normal(&fm, "  Direction : unknown\n");
1423  }
1424
1425  time = owl_message_format_time(m);
1426  owl_fmtext_appendf_normal(&fm, "  Time      : %s\n", time);
1427  g_free(time);
1428
1429  if (!owl_message_is_type_admin(m)) {
1430    owl_fmtext_appendf_normal(&fm, "  Sender    : %s\n", owl_message_get_sender(m));
1431    owl_fmtext_appendf_normal(&fm, "  Recipient : %s\n", owl_message_get_recipient(m));
1432  }
1433
1434  if (owl_message_is_type_zephyr(m)) {
1435    owl_fmtext_append_bold(&fm, "\nZephyr Specific Information:\n");
1436   
1437    owl_fmtext_appendf_normal(&fm, "  Class     : %s\n", owl_message_get_class(m));
1438    owl_fmtext_appendf_normal(&fm, "  Instance  : %s\n", owl_message_get_instance(m));
1439    owl_fmtext_appendf_normal(&fm, "  Opcode    : %s\n", owl_message_get_opcode(m));
1440#ifdef HAVE_LIBZEPHYR
1441    n = owl_message_get_notice(m);
1442    if (n != NULL) {
1443      char *tmpbuff, *tmpbuff2;
1444      int i, fields;
1445      const char *f;
1446
1447      if (!owl_message_is_pseudo(m)) {
1448        owl_fmtext_append_normal(&fm, "  Kind      : ");
1449        if (n->z_kind==UNSAFE) {
1450          owl_fmtext_append_normal(&fm, "UNSAFE\n");
1451        } else if (n->z_kind==UNACKED) {
1452          owl_fmtext_append_normal(&fm, "UNACKED\n");
1453        } else if (n->z_kind==ACKED) {
1454          owl_fmtext_append_normal(&fm, "ACKED\n");
1455        } else if (n->z_kind==HMACK) {
1456          owl_fmtext_append_normal(&fm, "HMACK\n");
1457        } else if (n->z_kind==HMCTL) {
1458          owl_fmtext_append_normal(&fm, "HMCTL\n");
1459        } else if (n->z_kind==SERVACK) {
1460          owl_fmtext_append_normal(&fm, "SERVACK\n");
1461        } else if (n->z_kind==SERVNAK) {
1462          owl_fmtext_append_normal(&fm, "SERVNACK\n");
1463        } else if (n->z_kind==CLIENTACK) {
1464          owl_fmtext_append_normal(&fm, "CLIENTACK\n");
1465        } else if (n->z_kind==STAT) {
1466          owl_fmtext_append_normal(&fm, "STAT\n");
1467        } else {
1468          owl_fmtext_append_normal(&fm, "ILLEGAL VALUE\n");
1469        }
1470      }
1471      owl_fmtext_appendf_normal(&fm, "  Host      : %s\n", owl_message_get_hostname(m));
1472
1473      if (!owl_message_is_pseudo(m)) {
1474        owl_fmtext_append_normal(&fm, "\n");
1475        owl_fmtext_appendf_normal(&fm, "  Port      : %i\n", ntohs(n->z_port));
1476        owl_fmtext_appendf_normal(&fm, "  Charset   : %s\n", owl_zephyr_get_charsetstr(n));
1477        owl_fmtext_appendf_normal(&fm, "  Auth      : %s\n", owl_zephyr_get_authstr(n));
1478
1479        /* FIXME make these more descriptive */
1480        owl_fmtext_appendf_normal(&fm, "  Checkd Ath: %i\n", n->z_checked_auth);
1481        owl_fmtext_appendf_normal(&fm, "  Multi notc: %s\n", n->z_multinotice);
1482        owl_fmtext_appendf_normal(&fm, "  Num other : %i\n", n->z_num_other_fields);
1483        owl_fmtext_appendf_normal(&fm, "  Msg Len   : %i\n", n->z_message_len);
1484
1485        fields=owl_zephyr_get_num_fields(n);
1486        owl_fmtext_appendf_normal(&fm, "  Fields    : %i\n", fields);
1487
1488        for (i = 0, f = owl_zephyr_first_raw_field(n); f != NULL;
1489             i++, f = owl_zephyr_next_raw_field(n, f)) {
1490          tmpbuff = owl_zephyr_field_as_utf8(n, f);
1491          tmpbuff2 = owl_text_indent(tmpbuff, 14, false);
1492          owl_fmtext_appendf_normal(&fm, "  Field %i   : %s\n", i + 1, tmpbuff2);
1493          g_free(tmpbuff2);
1494          g_free(tmpbuff);
1495        }
1496        tmpbuff = owl_text_indent(n->z_default_format, 14, false);
1497        owl_fmtext_appendf_normal(&fm, "  Default Fm: %s\n", tmpbuff);
1498        g_free(tmpbuff);
1499      }
1500
1501    }
1502#endif
1503  }
1504
1505  owl_fmtext_append_bold(&fm, "\nBarnOwl Message Attributes:\n");
1506  owl_message_attributes_tofmtext(m, &attrfm);
1507  owl_fmtext_append_fmtext(&fm, &attrfm);
1508 
1509  owl_function_popless_fmtext(&fm);
1510  owl_fmtext_cleanup(&fm);
1511  owl_fmtext_cleanup(&attrfm);
1512}
1513
1514/* print the current message in a popup window.
1515 * Use the 'default' style regardless of whatever
1516 * style the user may be using
1517 */
1518void owl_function_curmsg_to_popwin(void)
1519{
1520  const owl_view *v;
1521  const owl_message *m;
1522  const owl_style *s;
1523  owl_fmtext fm;
1524
1525  v=owl_global_get_current_view(&g);
1526  s=owl_global_get_style_by_name(&g, "default");
1527
1528  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
1529
1530  if (!m || owl_view_get_size(v)==0) {
1531    owl_function_error("No current message");
1532    return;
1533  }
1534
1535  owl_fmtext_init_null(&fm);
1536  owl_style_get_formattext(s, &fm, m);
1537
1538  owl_function_popless_fmtext(&fm);
1539  owl_fmtext_cleanup(&fm);
1540}
1541
1542void owl_function_page_curmsg(int step)
1543{
1544  /* scroll down or up within the current message IF the message is truncated */
1545
1546  int offset, curmsg, lines;
1547  const owl_view *v;
1548  owl_message *m;
1549
1550  offset=owl_global_get_curmsg_vert_offset(&g);
1551  v=owl_global_get_current_view(&g);
1552  curmsg=owl_global_get_curmsg(&g);
1553  m=owl_view_get_element(v, curmsg);
1554  if (!m || owl_view_get_size(v)==0) return;
1555  lines=owl_message_get_numlines(m);
1556
1557  if (offset==0) {
1558    /* Bail if the curmsg isn't the last one displayed */
1559    if (curmsg != owl_mainwin_get_last_msg(owl_global_get_mainwin(&g))) {
1560      owl_function_makemsg("The entire message is already displayed");
1561      return;
1562    }
1563   
1564    /* Bail if we're not truncated */
1565    if (!owl_mainwin_is_curmsg_truncated(owl_global_get_mainwin(&g))) {
1566      owl_function_makemsg("The entire message is already displayed");
1567      return;
1568    }
1569  }
1570 
1571 
1572  /* don't scroll past the last line */
1573  if (step>0) {
1574    if (offset+step > lines-1) {
1575      owl_global_set_curmsg_vert_offset(&g, lines-1);
1576    } else {
1577      owl_global_set_curmsg_vert_offset(&g, offset+step);
1578    }
1579  }
1580
1581  /* would we be before the beginning of the message? */
1582  if (step<0) {
1583    if (offset+step<0) {
1584      owl_global_set_curmsg_vert_offset(&g, 0);
1585    } else {
1586      owl_global_set_curmsg_vert_offset(&g, offset+step);
1587    }
1588  }
1589 
1590  /* redisplay */
1591  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
1592}
1593
1594void owl_function_resize_typwin(int newsize)
1595{
1596  owl_global_set_typwin_lines(&g, newsize);
1597  owl_mainpanel_layout_contents(&g.mainpanel);
1598}
1599
1600void owl_function_mainwin_pagedown(void)
1601{
1602  int i;
1603
1604  i=owl_mainwin_get_last_msg(owl_global_get_mainwin(&g));
1605  if (i<0) return;
1606  if (owl_mainwin_is_last_msg_truncated(owl_global_get_mainwin(&g))
1607      && (owl_global_get_curmsg(&g) < i)
1608      && (i>0)) {
1609    i--;
1610  }
1611  owl_global_set_curmsg(&g, i);
1612  owl_function_nextmsg();
1613}
1614
1615void owl_function_mainwin_pageup(void)
1616{
1617  owl_global_set_curmsg(&g, owl_global_get_topmsg(&g));
1618  owl_function_prevmsg();
1619}
1620
1621void owl_function_getsubs(void)
1622{
1623  char *buff;
1624
1625  buff=owl_zephyr_getsubs();
1626
1627  if (buff) {
1628    owl_function_popless_text(buff);
1629  } else {
1630    owl_function_popless_text("Error getting subscriptions");
1631  }
1632           
1633  g_free(buff);
1634}
1635
1636void owl_function_printallvars(void)
1637{
1638  const owl_variable *v;
1639  const char *name;
1640  char *var;
1641  GPtrArray *varnames;
1642  int i;
1643  GString *str   = g_string_new("");
1644
1645  g_string_append_printf(str, "%-20s = %s\n", "VARIABLE", "VALUE");
1646  g_string_append_printf(str, "%-20s   %s\n",  "--------", "-----");
1647  varnames = owl_variable_dict_get_names(owl_global_get_vardict(&g));
1648  for (i = 0; i < varnames->len; i++) {
1649    name = varnames->pdata[i];
1650    if (name && name[0]!='_') {
1651      g_string_append_printf(str, "\n%-20s = ", name);
1652      v = owl_variable_get_var(owl_global_get_vardict(&g), name);
1653      var = owl_variable_get_tostring(v);
1654      if (var) {
1655        g_string_append(str, var);
1656        g_free(var);
1657      } else {
1658        g_string_append(str, "<null>");
1659      }
1660    }
1661  }
1662  g_string_append(str, "\n");
1663  owl_ptr_array_free(varnames, g_free);
1664
1665  owl_function_popless_text(str->str);
1666  g_string_free(str, true);
1667}
1668
1669void owl_function_show_variables(void)
1670{
1671  const owl_variable *v;
1672  GPtrArray *varnames;
1673  owl_fmtext fm; 
1674  int i;
1675  const char *varname;
1676
1677  owl_fmtext_init_null(&fm);
1678  owl_fmtext_append_bold(&fm, 
1679      "Variables: (use 'show variable <name>' for details)\n");
1680  varnames = owl_variable_dict_get_names(owl_global_get_vardict(&g));
1681  for (i = 0; i < varnames->len; i++) {
1682    varname = varnames->pdata[i];
1683    if (varname && varname[0]!='_') {
1684      v = owl_variable_get_var(owl_global_get_vardict(&g), varname);
1685      owl_variable_describe(v, &fm);
1686    }
1687  }
1688  owl_ptr_array_free(varnames, g_free);
1689  owl_function_popless_fmtext(&fm);
1690  owl_fmtext_cleanup(&fm);
1691}
1692
1693void owl_function_show_variable(const char *name)
1694{
1695  const owl_variable *v;
1696  owl_fmtext fm; 
1697
1698  owl_fmtext_init_null(&fm);
1699  v = owl_variable_get_var(owl_global_get_vardict(&g), name);
1700  if (v)
1701    owl_variable_get_help(v, &fm);
1702  else
1703    owl_fmtext_append_normal(&fm, "No such variable...\n");
1704  owl_function_popless_fmtext(&fm);
1705  owl_fmtext_cleanup(&fm);
1706}
1707
1708void owl_function_delete_and_expunge_by_id(int id, bool exclaim_success)
1709{
1710  const owl_messagelist *ml = owl_global_get_msglist(&g);
1711  int msg = owl_messagelist_get_index_by_id(ml, id);
1712  if (msg < 0) {
1713    owl_function_error("No message with id %d: unable to delete", id);
1714  } else {
1715    owl_function_delete_and_expunge_message(msg);
1716    if (exclaim_success)
1717      owl_function_makemsg("Message deleted and expunged");
1718  }
1719}
1720
1721/* note: this applies to global message list, not to view.
1722 * If flag is 1, deletes.  If flag is 0, undeletes. */
1723void owl_function_delete_by_id(int id, int flag)
1724{
1725  const owl_messagelist *ml;
1726  owl_message *m;
1727  ml = owl_global_get_msglist(&g);
1728  m = owl_messagelist_get_by_id(ml, id);
1729  if (m) {
1730    if (flag == 1) {
1731      owl_message_mark_delete(m);
1732    } else if (flag == 0) {
1733      owl_message_unmark_delete(m);
1734    }
1735    owl_mainwin_redisplay(owl_global_get_mainwin(&g));
1736  } else {
1737    owl_function_error("No message with id %d: unable to mark for (un)delete",id);
1738  }
1739}
1740
1741void owl_function_delete_automsgs(void)
1742{
1743  /* mark for deletion all messages in the current view that match the
1744   * 'trash' filter */
1745
1746  int i, j, count;
1747  owl_message *m;
1748  const owl_view *v;
1749  const owl_filter *f;
1750
1751  /* get the trash filter */
1752  f=owl_global_get_filter(&g, "trash");
1753  if (!f) {
1754    owl_function_error("No trash filter defined");
1755    return;
1756  }
1757
1758  v=owl_global_get_current_view(&g);
1759
1760  count=0;
1761  j=owl_view_get_size(v);
1762  for (i=0; i<j; i++) {
1763    m=owl_view_get_element(v, i);
1764    if (owl_filter_message_match(f, m)) {
1765      count++;
1766      owl_message_mark_delete(m);
1767    }
1768  }
1769  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
1770  owl_function_makemsg("%i messages marked for deletion", count);
1771}
1772
1773void owl_function_status(void)
1774{
1775  char *tmpbuff;
1776  char buff[MAXPATHLEN+1];
1777  time_t start;
1778  struct tm tm;
1779  int up, days, hours, minutes;
1780  owl_fmtext fm;
1781
1782  owl_fmtext_init_null(&fm);
1783
1784  start=owl_global_get_starttime(&g);
1785
1786  owl_fmtext_append_normal(&fm, "General Information:\n");
1787
1788  owl_fmtext_append_normal(&fm, "  Version: ");
1789  owl_fmtext_append_normal(&fm, version);
1790  owl_fmtext_append_normal(&fm, "\n");
1791
1792  owl_fmtext_append_normal(&fm, "  Startup Arguments: ");
1793  owl_fmtext_append_normal(&fm, owl_global_get_startupargs(&g));
1794  owl_fmtext_append_normal(&fm, "\n");
1795
1796  owl_fmtext_append_normal(&fm, "  Current Directory: ");
1797  if(getcwd(buff, MAXPATHLEN) == NULL) {
1798    owl_fmtext_append_normal(&fm, "<Error in getcwd>");
1799  } else {
1800    owl_fmtext_append_normal(&fm, buff);
1801  }
1802  owl_fmtext_append_normal(&fm, "\n");
1803
1804  tmpbuff = owl_util_format_time(localtime_r(&start, &tm));
1805  owl_fmtext_appendf_normal(&fm, "  Startup Time: %s\n", tmpbuff);
1806  g_free(tmpbuff);
1807
1808  up=owl_global_get_runtime(&g);
1809  days=up/86400;
1810  up-=days*86400;
1811  hours=up/3600;
1812  up-=hours*3600;
1813  minutes=up/60;
1814  up-=minutes*60;
1815  owl_fmtext_appendf_normal(&fm, "  Run Time: %i days %2.2i:%2.2i:%2.2i\n", days, hours, minutes, up);
1816
1817  owl_fmtext_append_normal(&fm, "\nProtocol Options:\n");
1818  owl_fmtext_append_normal(&fm, "  Zephyr included    : ");
1819  if (owl_global_is_havezephyr(&g)) {
1820    owl_fmtext_append_normal(&fm, "yes\n");
1821  } else {
1822    owl_fmtext_append_normal(&fm, "no\n");
1823  }
1824  owl_fmtext_append_normal(&fm, "  AIM included       : yes\n");
1825  owl_fmtext_append_normal(&fm, "  Loopback included  : yes\n");
1826
1827
1828  owl_fmtext_append_normal(&fm, "\nBuild Options:\n");
1829  owl_fmtext_append_normal(&fm, "  Stderr redirection : ");
1830#if OWL_STDERR_REDIR
1831  owl_fmtext_append_normal(&fm, "yes\n");
1832#else
1833  owl_fmtext_append_normal(&fm, "no\n");
1834#endif
1835 
1836
1837  owl_fmtext_append_normal(&fm, "\nAIM Status:\n");
1838  owl_fmtext_append_normal(&fm, "  Logged in: ");
1839  if (owl_global_is_aimloggedin(&g)) {
1840    owl_fmtext_append_normal(&fm, owl_global_get_aim_screenname(&g));
1841    owl_fmtext_append_normal(&fm, "\n");
1842  } else {
1843    owl_fmtext_append_normal(&fm, "(not logged in)\n");
1844  }
1845
1846  owl_fmtext_append_normal(&fm, "  Processing events: ");
1847  if (owl_global_is_doaimevents(&g)) {
1848    owl_fmtext_append_normal(&fm, "yes\n");
1849  } else {
1850    owl_fmtext_append_normal(&fm, "no\n");
1851  }
1852
1853  owl_function_popless_fmtext(&fm);
1854  owl_fmtext_cleanup(&fm);
1855}
1856
1857void owl_function_show_term(void)
1858{
1859  owl_fmtext fm;
1860
1861  owl_fmtext_init_null(&fm);
1862  owl_fmtext_appendf_normal(&fm, "Terminal Lines: %i\nTerminal Columns: %i\n",
1863          owl_global_get_lines(&g),
1864          owl_global_get_cols(&g));
1865
1866  if (has_colors()) {
1867    owl_fmtext_append_normal(&fm, "Color: Yes\n");
1868    owl_fmtext_appendf_normal(&fm, "Number of color pairs: %i\n", owl_util_get_colorpairs());
1869    owl_fmtext_appendf_normal(&fm, "Can change colors: %s\n", can_change_color() ? "yes" : "no");
1870  } else {
1871    owl_fmtext_append_normal(&fm, "Color: No\n");
1872  }
1873
1874  owl_function_popless_fmtext(&fm);
1875  owl_fmtext_cleanup(&fm);
1876}
1877
1878/* if type = 0 then normal reply.
1879 * if type = 1 then it's a reply to sender
1880 * if enter = 0 then allow the command to be edited
1881 * if enter = 1 then don't wait for editing
1882 */
1883void owl_function_reply(int type, int enter)
1884{
1885  char *buff=NULL;
1886  const owl_message *m;
1887  const owl_filter *f;
1888 
1889  if (owl_view_get_size(owl_global_get_current_view(&g))==0) {
1890    owl_function_error("No message selected");
1891  } else {
1892    char *cmd;
1893   
1894    m=owl_view_get_element(owl_global_get_current_view(&g), owl_global_get_curmsg(&g));
1895    if (!m) {
1896      owl_function_error("No message selected");
1897      return;
1898    }
1899
1900    /* first check if we catch the reply-lockout filter */
1901    f=owl_global_get_filter(&g, "reply-lockout");
1902    if (f) {
1903      if (owl_filter_message_match(f, m)) {
1904        owl_function_error("Sorry, replies to this message have been disabled by the reply-lockout filter");
1905        return;
1906      }
1907    }
1908
1909    /* then check if it's a question and just bring up the command prompt */
1910    if (owl_message_is_question(m)) {
1911      owl_function_start_command("");
1912      return;
1913    }
1914
1915    if((type == 0 &&
1916        (cmd=owl_perlconfig_message_call_method(m, "replycmd", 0, NULL))) ||
1917       (type == 1 &&
1918        (cmd=owl_perlconfig_message_call_method(m, "replysendercmd", 0, NULL)))) {
1919      buff = cmd;
1920    }
1921
1922    if(!buff) {
1923        owl_function_error("I don't know how to reply to that message.");
1924        return;
1925    }
1926
1927    if (enter) {
1928      owl_history *hist = owl_global_get_cmd_history(&g);
1929      owl_history_store(hist, buff, false);
1930      owl_function_command_norv(buff);
1931    } else {
1932      owl_function_start_command(buff);
1933    }
1934    g_free(buff);
1935  }
1936}
1937
1938void owl_function_zlocate(int argc, const char *const *argv, int auth)
1939{
1940  owl_fmtext fm;
1941  char *ptr;
1942  char *result;
1943  int i;
1944
1945  owl_fmtext_init_null(&fm);
1946
1947  for (i=0; i<argc; i++) {
1948    ptr = long_zuser(argv[i]);
1949    result = owl_zephyr_zlocate(ptr, auth);
1950    owl_fmtext_append_normal(&fm, result);
1951    g_free(result);
1952    g_free(ptr);
1953  }
1954
1955  owl_function_popless_fmtext(&fm);
1956  owl_fmtext_cleanup(&fm);
1957}
1958
1959void owl_callback_command(owl_editwin *e, bool success)
1960{
1961  if (!success) return;
1962  char *rv;
1963  const char *line = owl_editwin_get_text(e);
1964
1965  rv = owl_function_command(line);
1966   if (rv) {
1967    owl_function_makemsg("%s", rv);
1968    g_free(rv);
1969  }
1970}
1971
1972owl_editwin *owl_function_start_command(const char *line)
1973{
1974  owl_editwin *tw;
1975  owl_context *ctx;
1976
1977  tw = owl_global_set_typwin_active(&g, OWL_EDITWIN_STYLE_ONELINE, owl_global_get_cmd_history(&g));
1978
1979  owl_editwin_set_locktext(tw, "command: ");
1980
1981  owl_editwin_insert_string(tw, line);
1982
1983  ctx = owl_editcontext_new(OWL_CTX_EDITLINE, tw, "editline",
1984                            owl_global_deactivate_editcontext, &g);
1985  owl_global_push_context_obj(&g, ctx);
1986  owl_editwin_set_callback(tw, owl_callback_command);
1987  return tw;
1988}
1989
1990owl_editwin *owl_function_start_question(const char *line)
1991{
1992  owl_editwin *tw;
1993  owl_context *ctx;
1994
1995  tw = owl_global_set_typwin_active(&g, OWL_EDITWIN_STYLE_ONELINE, owl_global_get_cmd_history(&g));
1996
1997  owl_editwin_set_locktext(tw, line);
1998
1999  ctx = owl_editcontext_new(OWL_CTX_EDITRESPONSE, tw, "editresponse",
2000                            owl_global_deactivate_editcontext, &g);
2001  owl_global_push_context_obj(&g, ctx);
2002  return tw;
2003}
2004
2005owl_editwin *owl_function_start_password(const char *line)
2006{
2007  owl_editwin *tw;
2008  owl_context *ctx;
2009
2010  tw = owl_global_set_typwin_active(&g, OWL_EDITWIN_STYLE_ONELINE, NULL);
2011
2012  owl_editwin_set_echochar(tw, '*');
2013
2014  owl_editwin_set_locktext(tw, line);
2015
2016  ctx = owl_editcontext_new(OWL_CTX_EDITRESPONSE, tw, "editresponse",
2017                            owl_global_deactivate_editcontext, &g);
2018  owl_global_push_context_obj(&g, ctx);
2019  return tw;
2020}
2021
2022CALLER_OWN char *owl_function_exec(int argc, const char *const *argv, const char *buff, int type)
2023{
2024  /* if type == 1 display in a popup
2025   * if type == 2 display an admin messages
2026   * if type == 0 return output
2027   * else display in a popup
2028   */
2029  const char *redirect = " 2>&1 < /dev/null";
2030  char *newbuff;
2031  char *out;
2032  FILE *p;
2033
2034#if OWL_STDERR_REDIR
2035  redirect = " < /dev/null";
2036#endif
2037
2038  if (argc<2) {
2039    owl_function_error("Wrong number of arguments to the exec command");
2040    return NULL;
2041  }
2042
2043  buff = skiptokens(buff, 1);
2044  newbuff = g_strdup_printf("exec%s; %s", redirect, buff);
2045
2046  if (type == OWL_OUTPUT_POPUP) {
2047    owl_popexec_new(newbuff);
2048  } else {
2049    p = popen(newbuff, "r");
2050    out = owl_slurp(p);
2051    pclose(p);
2052   
2053    if (type == OWL_OUTPUT_RETURN) {
2054      g_free(newbuff);
2055      return out;
2056    } else if (type == OWL_OUTPUT_ADMINMSG) {
2057      owl_function_adminmsg(buff, out);
2058    }
2059    g_free(out);
2060  }
2061  g_free(newbuff);
2062  return NULL;
2063}
2064
2065CALLER_OWN char *owl_function_perl(int argc, const char *const *argv, const char *buff, int type)
2066{
2067  /* if type == 1 display in a popup
2068   * if type == 2 display an admin messages
2069   * if type == 0 return output
2070   * else display in a popup
2071   */
2072  char *perlout;
2073
2074  if (argc<2) {
2075    owl_function_error("Wrong number of arguments to perl command");
2076    return NULL;
2077  }
2078
2079  /* consume first token (argv[0]) */
2080  buff = skiptokens(buff, 1);
2081
2082  perlout = owl_perlconfig_execute(buff);
2083  if (perlout) { 
2084    if (type == OWL_OUTPUT_POPUP) {
2085      owl_function_popless_text(perlout);
2086    } else if (type == OWL_OUTPUT_ADMINMSG) {
2087      owl_function_adminmsg(buff, perlout);
2088    } else if (type == OWL_OUTPUT_RETURN) {
2089      return perlout;
2090    }
2091    g_free(perlout);
2092  }
2093  return NULL;
2094}
2095
2096/* Change the filter associated with the current view.
2097 * This also figures out which message in the new filter
2098 * should have the pointer.
2099 */
2100void owl_function_change_currentview_filter(const char *filtname)
2101{
2102  owl_view *v;
2103  owl_filter *f;
2104  int curid=-1, newpos, curmsg;
2105  const owl_message *curm=NULL;
2106
2107  v=owl_global_get_current_view(&g);
2108
2109  curmsg=owl_global_get_curmsg(&g);
2110  if (curmsg==-1) {
2111    owl_function_debugmsg("Hit the curmsg==-1 case in change_view");
2112  } else {
2113    curm=owl_view_get_element(v, curmsg);
2114    if (curm) {
2115      curid=owl_message_get_id(curm);
2116      owl_view_save_curmsgid(v, curid);
2117    }
2118  }
2119
2120  f=owl_global_get_filter(&g, filtname);
2121  if (!f) {
2122    owl_function_error("Unknown filter %s", filtname);
2123    return;
2124  }
2125
2126  owl_view_new_filter(v, f);
2127
2128  /* Figure out what to set the current message to.
2129   * - If the view we're leaving has messages in it, go to the closest message
2130   *   to the last message pointed to in that view.
2131   * - If the view we're leaving is empty, try to restore the position
2132   *   from the last time we were in the new view.  */
2133  if (curm) {
2134    newpos = owl_view_get_nearest_to_msgid(v, curid);
2135  } else {
2136    newpos = owl_view_get_nearest_to_saved(v);
2137  }
2138
2139  owl_global_set_curmsg(&g, newpos);
2140  owl_function_calculate_topmsg(OWL_DIRECTION_DOWNWARDS);
2141  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2142  owl_global_set_direction_downwards(&g);
2143}
2144
2145/* Create a new filter, or replace an existing one
2146 * with a new definition. Returns true on success.
2147 */
2148bool owl_function_create_filter(int argc, const char *const *argv)
2149{
2150  owl_filter *f;
2151  const owl_view *v;
2152  int inuse = 0;
2153
2154  if (argc < 2) {
2155    owl_function_error("Wrong number of arguments to filter command");
2156    return false;
2157  }
2158
2159  owl_function_debugmsg("owl_function_create_filter: starting to create filter named %s", argv[1]);
2160
2161  v=owl_global_get_current_view(&g);
2162
2163  /* don't touch the all filter */
2164  if (!strcmp(argv[1], "all")) {
2165    owl_function_error("You may not change the 'all' filter.");
2166    return false;
2167  }
2168
2169  /* deal with the case of trying change the filter color */
2170  if (argc==4 && !strcmp(argv[2], "-c")) {
2171    f=owl_global_get_filter(&g, argv[1]);
2172    if (!f) {
2173      owl_function_error("The filter '%s' does not exist.", argv[1]);
2174      return false;
2175    }
2176    if (owl_util_string_to_color(argv[3])==OWL_COLOR_INVALID) {
2177      owl_function_error("The color '%s' is not available.", argv[3]);
2178      return false;
2179    }
2180    owl_filter_set_fgcolor(f, owl_util_string_to_color(argv[3]));
2181    owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2182    return false;
2183  }
2184  if (argc==4 && !strcmp(argv[2], "-b")) {
2185    f=owl_global_get_filter(&g, argv[1]);
2186    if (!f) {
2187      owl_function_error("The filter '%s' does not exist.", argv[1]);
2188      return false;
2189    }
2190    if (owl_util_string_to_color(argv[3])==OWL_COLOR_INVALID) {
2191      owl_function_error("The color '%s' is not available.", argv[3]);
2192      return false;
2193    }
2194    owl_filter_set_bgcolor(f, owl_util_string_to_color(argv[3]));
2195    owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2196    return true;
2197  }
2198
2199  /* create the filter and check for errors */
2200  f = owl_filter_new(argv[1], argc-2, argv+2);
2201  if (f == NULL) {
2202    owl_function_error("Invalid filter: %s", argv[1]);
2203    return false;
2204  }
2205
2206  /* if the named filter is in use by the current view, remember it */
2207  if (!strcmp(owl_view_get_filtname(v), argv[1])) {
2208    inuse=1;
2209  }
2210
2211  /* if the named filter already exists, nuke it */
2212  if (owl_global_get_filter(&g, argv[1])) {
2213    owl_global_remove_filter(&g, argv[1]);
2214  }
2215
2216  /* add the filter */
2217  owl_global_add_filter(&g, f);
2218
2219  /* if it was in use by the current view then update */
2220  if (inuse) {
2221    owl_function_change_currentview_filter(argv[1]);
2222  }
2223  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2224  return true;
2225}
2226
2227/* If 'filtername' does not start with 'not-' create a filter named
2228 * 'not-<filtername>' defined as "not filter <filtername>".  If the
2229 * filter 'not-<filtername>' already exists, do not overwrite it.  If
2230 * 'filtername' begins with 'not-' and a filter 'filtername' already
2231 * exists, then do nothing.  If the filter 'filtername' does not
2232 * exist, create it and define it as 'not filter <filtername>'
2233 *
2234 * Returns the name of the negated filter, which the caller must free.
2235 */
2236CALLER_OWN char *owl_function_create_negative_filter(const char *filtername)
2237{
2238  char *newname;
2239  const owl_filter *tmpfilt;
2240  const char *argv[5];
2241
2242  owl_function_debugmsg("owl_function_create_negative_filter");
2243 
2244  if (!strncmp(filtername, "not-", 4)) {
2245    newname=g_strdup(filtername+4);
2246  } else {
2247    newname=g_strdup_printf("not-%s", filtername);
2248  }
2249
2250  tmpfilt=owl_global_get_filter(&g, newname);
2251  if (!tmpfilt) {
2252    argv[0]="filter"; /* anything is fine here */
2253    argv[1]=newname;
2254    argv[2]="not";
2255    argv[3]="filter";
2256    argv[4]=filtername;
2257    owl_function_create_filter(5, argv);
2258  }
2259
2260  owl_function_debugmsg("owl_function_create_negative_filter: returning with %s", newname);
2261  return(newname);
2262}
2263
2264void owl_function_show_filters(void)
2265{
2266  const owl_filter *f;
2267  GList *fl;
2268  owl_fmtext fm;
2269
2270  owl_fmtext_init_null(&fm);
2271
2272  owl_fmtext_append_bold(&fm, "Filters:\n");
2273
2274  for (fl = g.filterlist; fl; fl = g_list_next(fl)) {
2275    f = fl->data;
2276    owl_fmtext_append_normal(&fm, "   ");
2277    owl_fmtext_append_normal_color(&fm, owl_filter_get_name(f),
2278                                   owl_filter_get_fgcolor(f),
2279                                   owl_filter_get_bgcolor(f));
2280    owl_fmtext_append_normal(&fm, "\n");
2281  }
2282  owl_function_popless_fmtext(&fm);
2283  owl_fmtext_cleanup(&fm);
2284}
2285
2286void owl_function_show_filter(const char *name)
2287{
2288  const owl_filter *f;
2289  char *buff, *tmp;
2290
2291  f=owl_global_get_filter(&g, name);
2292  if (!f) {
2293    owl_function_error("There is no filter named %s", name);
2294    return;
2295  }
2296  tmp = owl_filter_print(f);
2297  buff = g_strdup_printf("%s: %s", owl_filter_get_name(f), tmp);
2298  owl_function_popless_text(buff);
2299  g_free(buff);
2300  g_free(tmp);
2301}
2302
2303void owl_function_show_zpunts(void)
2304{
2305  const owl_filter *f;
2306  const GPtrArray *fl;
2307  char *tmp;
2308  owl_fmtext fm;
2309  int i;
2310
2311  owl_fmtext_init_null(&fm);
2312
2313  fl=owl_global_get_puntlist(&g);
2314  owl_fmtext_append_bold(&fm, "Active zpunt filters:\n");
2315
2316  for (i = 0; i < fl->len; i++) {
2317    f = fl->pdata[i];
2318    owl_fmtext_appendf_normal(&fm, "[% 2d] ", i+1);
2319    tmp = owl_filter_print(f);
2320    owl_fmtext_append_normal(&fm, tmp);
2321    g_free(tmp);
2322  }
2323  owl_function_popless_fmtext(&fm);
2324  owl_fmtext_cleanup(&fm);
2325}
2326
2327/* Create a filter for a class, instance if one doesn't exist.  If
2328 * instance is NULL then catch all messgaes in the class.  Returns the
2329 * name of the filter or null.  The caller must free this name.
2330 * If 'related' is nonzero, encompass unclasses and .d classes as well.
2331 */
2332CALLER_OWN char *owl_function_classinstfilt(const char *c, const char *i, int related) 
2333{
2334  owl_filter *f;
2335  char *filtname;
2336  char *tmpclass, *tmpinstance = NULL;
2337  char *class, *instance = NULL;
2338  GString *buf;
2339
2340  if (related) {
2341    class = owl_util_baseclass(c);
2342    if (i) {
2343      instance = owl_util_baseclass(i);
2344    }
2345  } else {
2346    class = g_strdup(c);
2347    if (i) {
2348      instance = g_strdup(i);
2349    }
2350  }
2351
2352  /* name for the filter */
2353  if (!instance) {
2354    filtname = g_strdup_printf("%sclass-%s", related ? "related-" : "", class);
2355  } else {
2356    filtname = g_strdup_printf("%sclass-%s-instance-%s", related ? "related-" : "", class, instance);
2357  }
2358  /* downcase it */
2359  {
2360    char *temp = g_utf8_strdown(filtname, -1);
2361    if (temp) {
2362      g_free(filtname);
2363      filtname = temp;
2364    }
2365  }
2366 
2367  /* if it already exists then go with it.  This lets users override */
2368  if (owl_global_get_filter(&g, filtname)) {
2369    goto done;
2370  }
2371
2372  /* create the new filter */
2373  tmpclass=owl_text_quote(class, OWL_REGEX_QUOTECHARS, OWL_REGEX_QUOTEWITH);
2374  if (instance) {
2375    tmpinstance=owl_text_quote(instance, OWL_REGEX_QUOTECHARS, OWL_REGEX_QUOTEWITH);
2376  }
2377
2378  buf = g_string_new("");
2379  owl_string_appendf_quoted(buf,
2380                            related ? "class ^(un)*%q(\\.d)*$" : "class ^%q$",
2381                            tmpclass);
2382
2383  if (tmpinstance) {
2384    owl_string_appendf_quoted(buf,
2385                              related ?
2386                              " and ( instance ^(un)*%q(\\.d)*$ )" :
2387                              " and instance ^%q$",
2388                              tmpinstance);
2389  }
2390  g_free(tmpclass);
2391  g_free(tmpinstance);
2392
2393  f = owl_filter_new_fromstring(filtname, buf->str);
2394  g_string_free(buf, true);
2395  if (f == NULL) {
2396    /* Couldn't make a filter for some reason. Return NULL. */
2397    owl_function_error("Error creating filter '%s'", filtname);
2398    g_free(filtname);
2399    filtname = NULL;
2400    goto done;
2401  }
2402
2403  /* add it to the global list */
2404  owl_global_add_filter(&g, f);
2405
2406done:
2407  g_free(class);
2408  g_free(instance);
2409  return(filtname);
2410}
2411
2412/* Create a filter for personal zephyrs to or from the specified
2413 * zephyr user.  Includes login/logout notifications for the user.
2414 * The name of the filter will be 'user-<shortuser>'.  If a filter already
2415 * exists with this name, no new filter will be created.  This allows
2416 * the configuration to override this function.  Returns the name of
2417 * the filter, which the caller must free.
2418 */
2419CALLER_OWN char *owl_function_zuserfilt(const char *longuser)
2420{
2421  owl_filter *f;
2422  char *argbuff, *esclonguser, *shortuser, *filtname;
2423
2424  /* name for the filter */
2425  shortuser = short_zuser(longuser);
2426  filtname = g_strdup_printf("user-%s", shortuser);
2427  g_free(shortuser);
2428
2429  /* if it already exists then go with it.  This lets users override */
2430  if (owl_global_get_filter(&g, filtname)) {
2431    return filtname;
2432  }
2433
2434  /* create the new-internal filter */
2435  esclonguser = owl_text_quote(longuser, OWL_REGEX_QUOTECHARS, OWL_REGEX_QUOTEWITH);
2436
2437  argbuff=owl_string_build_quoted("( type ^zephyr$ and filter personal and "
2438      "( ( direction ^in$ and sender ^%q$ ) or ( direction ^out$ and "
2439      "recipient ^%q$ ) ) ) or ( ( class ^login$ ) and ( sender ^%q$ ) )",
2440      esclonguser, esclonguser, esclonguser);
2441  g_free(esclonguser);
2442
2443  f = owl_filter_new_fromstring(filtname, argbuff);
2444  g_free(argbuff);
2445
2446  if (f == NULL) {
2447    /* Couldn't make a filter for some reason. Return NULL. */
2448    owl_function_error("Error creating filter '%s'", filtname);
2449    g_free(filtname);
2450    return NULL;
2451  }
2452
2453  /* add it to the global list */
2454  owl_global_add_filter(&g, f);
2455
2456  return(filtname);
2457}
2458
2459/* Create a filter for AIM IM messages to or from the specified
2460 * screenname.  The name of the filter will be 'aimuser-<user>'.  If a
2461 * filter already exists with this name, no new filter will be
2462 * created.  This allows the configuration to override this function.
2463 * Returns the name of the filter, which the caller must free.
2464 */
2465CALLER_OWN char *owl_function_aimuserfilt(const char *user)
2466{
2467  owl_filter *f;
2468  char *argbuff, *filtname;
2469  char *escuser;
2470
2471  /* name for the filter */
2472  filtname=g_strdup_printf("aimuser-%s", user);
2473
2474  /* if it already exists then go with it.  This lets users override */
2475  if (owl_global_get_filter(&g, filtname)) {
2476    return filtname;
2477  }
2478
2479  /* create the new-internal filter */
2480  escuser = owl_text_quote(user, OWL_REGEX_QUOTECHARS, OWL_REGEX_QUOTEWITH);
2481
2482  argbuff = g_strdup_printf(
2483      "( type ^aim$ and ( ( sender ^%1$s$ and recipient ^%2$s$ ) or "
2484      "( sender ^%2$s$ and recipient ^%1$s$ ) ) )",
2485      escuser, owl_global_get_aim_screenname_for_filters(&g));
2486  g_free(escuser);
2487
2488  f = owl_filter_new_fromstring(filtname, argbuff);
2489  g_free(argbuff);
2490
2491  if (f == NULL) {
2492    owl_function_error("Error creating filter '%s'", filtname);
2493    g_free(filtname);
2494    return NULL;
2495  }
2496
2497  /* add it to the global list */
2498  owl_global_add_filter(&g, f);
2499
2500  return(filtname);
2501}
2502
2503CALLER_OWN char *owl_function_typefilt(const char *type)
2504{
2505  owl_filter *f;
2506  char *argbuff, *filtname, *esctype;
2507
2508  /* name for the filter */
2509  filtname=g_strdup_printf("type-%s", type);
2510
2511  /* if it already exists then go with it.  This lets users override */
2512  if (owl_global_get_filter(&g, filtname)) {
2513    return filtname;
2514  }
2515
2516  /* create the new-internal filter */
2517  esctype = owl_text_quote(type, OWL_REGEX_QUOTECHARS, OWL_REGEX_QUOTEWITH);
2518
2519  argbuff = owl_string_build_quoted("type ^%q$", esctype);
2520  g_free(esctype);
2521
2522  f = owl_filter_new_fromstring(filtname, argbuff);
2523  g_free(argbuff);
2524
2525  if (f == NULL) {
2526    owl_function_error("Error creating filter '%s'", filtname);
2527    g_free(filtname);
2528    return NULL;
2529  }
2530
2531  /* add it to the global list */
2532  owl_global_add_filter(&g, f);
2533
2534  return filtname;
2535}
2536
2537/* If flag is 1, marks for deletion.  If flag is 0,
2538 * unmarks for deletion. */
2539void owl_function_delete_curview_msgs(int flag)
2540{
2541  const owl_view *v;
2542  int i, j;
2543
2544  v=owl_global_get_current_view(&g);
2545  j=owl_view_get_size(v);
2546  for (i=0; i<j; i++) {
2547    if (flag == 1) {
2548      owl_message_mark_delete(owl_view_get_element(v, i));
2549    } else if (flag == 0) {
2550      owl_message_unmark_delete(owl_view_get_element(v, i));
2551    }
2552  }
2553
2554  owl_function_makemsg("%i messages marked for %sdeletion", j, flag?"":"un");
2555
2556  owl_mainwin_redisplay(owl_global_get_mainwin(&g)); 
2557}
2558
2559static CALLER_OWN char *owl_function_smartfilter_cc(const owl_message *m)
2560{
2561  const char *ccs;
2562  char *ccs_quoted;
2563  char *filtname;
2564  owl_filter *f;
2565  GString *buf;
2566
2567  ccs = owl_message_get_attribute_value(m, "zephyr_ccs");
2568
2569  filtname = g_strdup_printf("conversation-%s", ccs);
2570  g_strdelimit(filtname, " ", '-');
2571
2572  if (owl_global_get_filter(&g, filtname)) {
2573    return filtname;
2574  }
2575
2576  buf = g_string_new("type ^zephyr$ and filter personal and "
2577                     "zephyr_ccs ^");
2578  ccs_quoted = owl_text_quote(ccs, OWL_REGEX_QUOTECHARS, OWL_REGEX_QUOTEWITH);
2579  owl_string_append_quoted_arg(buf, ccs_quoted);
2580  g_string_append_c(buf, '$');
2581  g_free(ccs_quoted);
2582
2583  f = owl_filter_new_fromstring(filtname, buf->str);
2584  g_string_free(buf, true);
2585
2586  if (f == NULL) {
2587    owl_function_error("Error creating filter '%s'", filtname);
2588    g_free(filtname);
2589    return NULL;
2590  }
2591
2592  owl_global_add_filter(&g, f);
2593
2594  return filtname;
2595}
2596
2597/* Create a filter based on the current message.  Returns the name of
2598 * a filter or null.  The caller must free this name.
2599 *
2600 * if the curmsg is a personal zephyr return a filter name
2601 *    to the zephyr conversation with that user.
2602 * If the curmsg is a zephyr class message, instance foo, recip *,
2603 *    return a filter name to the class, inst.
2604 * If the curmsg is a zephyr class message and type==0 then
2605 *    return a filter name for just the class.
2606 * If the curmsg is a zephyr class message and type==1 then
2607 *    return a filter name for the class and instance.
2608 * If the curmsg is a personal AIM message returna  filter
2609 *    name to the AIM conversation with that user
2610 */
2611CALLER_OWN char *owl_function_smartfilter(int type, int invert_related)
2612{
2613  const owl_view *v;
2614  const owl_message *m;
2615  char *filtname = NULL;
2616  const char *argv[2], *zperson;
2617  int related = owl_global_is_narrow_related(&g) ^ invert_related;
2618
2619  v=owl_global_get_current_view(&g);
2620  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
2621
2622  if (!m || owl_view_get_size(v)==0) {
2623    owl_function_error("No message selected\n");
2624    return(NULL);
2625  }
2626
2627  /* very simple handling of admin messages for now */
2628  if (owl_message_is_type_admin(m)) {
2629    return(owl_function_typefilt("admin"));
2630  }
2631
2632  /* very simple handling of loopback messages for now */
2633  if (owl_message_is_type_loopback(m)) {
2634    return(owl_function_typefilt("loopback"));
2635  }
2636
2637  /* aim messages */
2638  if (owl_message_is_type_aim(m)) {
2639    if (owl_message_is_direction_in(m)) {
2640      filtname=owl_function_aimuserfilt(owl_message_get_sender(m));
2641    } else if (owl_message_is_direction_out(m)) {
2642      filtname=owl_function_aimuserfilt(owl_message_get_recipient(m));
2643    }
2644    return(filtname);
2645  }
2646
2647  /* narrow personal and login messages to the sender or recip as appropriate */
2648  if (owl_message_is_type_zephyr(m)) {
2649    if (owl_message_is_personal(m) || owl_message_is_loginout(m)) {
2650      if (owl_message_get_attribute_value(m, "zephyr_ccs") != NULL) {
2651        return owl_function_smartfilter_cc(m);
2652      }
2653
2654      if (owl_message_is_direction_in(m)) {
2655        zperson = owl_message_get_sender(m);
2656      } else {
2657        zperson = owl_message_get_recipient(m);
2658      }
2659      filtname = owl_function_zuserfilt(zperson);
2660      return filtname;
2661    }
2662
2663    /* narrow class MESSAGE, instance foo, recip * messages to class, inst */
2664    if (!strcasecmp(owl_message_get_class(m), "message")) {
2665      filtname=owl_function_classinstfilt(owl_message_get_class(m), owl_message_get_instance(m), related);
2666      return(filtname);
2667    }
2668
2669    /* otherwise narrow to the class */
2670    if (type==0) {
2671      filtname=owl_function_classinstfilt(owl_message_get_class(m), NULL, related);
2672    } else if (type==1) {
2673      filtname=owl_function_classinstfilt(owl_message_get_class(m), owl_message_get_instance(m), related);
2674    }
2675    return(filtname);
2676  }
2677
2678  /* pass it off to perl */
2679  argv[0] = type ? "1" : "0";
2680  argv[1] = related ? "1" : "0";
2681  return owl_perlconfig_message_call_method(m, "smartfilter", 2, argv);
2682}
2683
2684void owl_function_smartzpunt(int type)
2685{
2686  /* Starts a zpunt command based on the current class,instance pair.
2687   * If type=0, uses just class.  If type=1, uses instance as well. */
2688  const owl_view *v;
2689  const owl_message *m;
2690  const char *mclass, *minst;
2691  GString *buf;
2692 
2693  v=owl_global_get_current_view(&g);
2694  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
2695
2696  if (!m || owl_view_get_size(v)==0) {
2697    owl_function_error("No message selected\n");
2698    return;
2699  }
2700
2701  /* for now we skip admin messages. */
2702  if (owl_message_is_type_admin(m)
2703      || owl_message_is_loginout(m)
2704      || !owl_message_is_type_zephyr(m)) {
2705    owl_function_error("smartzpunt doesn't support this message type.");
2706    return;
2707  }
2708
2709  mclass = owl_message_get_class(m);
2710  minst = owl_message_get_instance(m);
2711  if (!mclass || !*mclass || *mclass==' '
2712      || (!strcasecmp(mclass, "message") && !strcasecmp(minst, "personal"))
2713      || (type && (!minst || !*minst|| *minst==' '))) {
2714    owl_function_error("smartzpunt can't safely do this for <%s,%s>",
2715                         mclass, minst);
2716  } else {
2717    buf = g_string_new("start-command zpunt ");
2718    owl_string_append_quoted_arg(buf, mclass);
2719    if (type) {
2720      g_string_append_c(buf, ' ');
2721      owl_string_append_quoted_arg(buf, minst);
2722    } else {
2723      g_string_append(buf, " *");
2724    }
2725    owl_function_command_norv(buf->str);
2726    g_string_free(buf, true);
2727  }
2728}
2729
2730/* Set the color of the current view's filter to
2731 * be 'color'
2732 */
2733void owl_function_color_current_filter(const char *fgcolor, const char *bgcolor)
2734{
2735  const char *name;
2736
2737  name=owl_view_get_filtname(owl_global_get_current_view(&g));
2738  owl_function_color_filter(name, fgcolor, bgcolor);
2739}
2740
2741/* Set the color of the filter 'filter' to be 'color'.  If the color
2742 * name does not exist, return -1, if the filter does not exist or is
2743 * the "all" filter, return -2.  Return 0 on success
2744 */
2745int owl_function_color_filter(const char *filtname, const char *fgcolor, const char *bgcolor)
2746{
2747  owl_filter *f;
2748
2749  f=owl_global_get_filter(&g, filtname);
2750  if (!f) {
2751    owl_function_error("Unknown filter");
2752    return(-2);
2753  }
2754
2755  /* don't touch the all filter */
2756  if (!strcmp(filtname, "all")) {
2757    owl_function_error("You may not change the 'all' filter.");
2758    return(-2);
2759  }
2760
2761  if (owl_util_string_to_color(fgcolor)==OWL_COLOR_INVALID) {
2762    owl_function_error("No color named '%s' avilable.", fgcolor);
2763    return(-1);
2764  }
2765
2766
2767  if (bgcolor != NULL) {
2768    if (owl_util_string_to_color(bgcolor)==OWL_COLOR_INVALID) {
2769      owl_function_error("No color named '%s' avilable.", bgcolor);
2770      return(-1);
2771    }
2772    owl_filter_set_bgcolor(f, owl_util_string_to_color(bgcolor));
2773  }
2774  owl_filter_set_fgcolor(f, owl_util_string_to_color(fgcolor));
2775 
2776  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2777  return(0);
2778}
2779
2780void owl_function_show_colors(void)
2781{
2782  owl_fmtext fm;
2783  int i; 
2784 
2785  owl_fmtext_init_null(&fm);
2786  owl_fmtext_append_normal(&fm,"default:  ");
2787  owl_fmtext_append_normal_color(&fm, "default\n", OWL_COLOR_DEFAULT, OWL_COLOR_DEFAULT);
2788
2789  owl_fmtext_append_normal(&fm,"black:    ");
2790  owl_fmtext_append_normal_color(&fm, "black\n", OWL_COLOR_BLACK, OWL_COLOR_DEFAULT);
2791
2792  owl_fmtext_append_normal(&fm,"red:      ");
2793  owl_fmtext_append_normal_color(&fm, "red\n", OWL_COLOR_RED, OWL_COLOR_DEFAULT);
2794
2795  owl_fmtext_append_normal(&fm,"green:    ");
2796  owl_fmtext_append_normal_color(&fm, "green\n", OWL_COLOR_GREEN, OWL_COLOR_DEFAULT);
2797
2798  owl_fmtext_append_normal(&fm,"yellow:   ");
2799  owl_fmtext_append_normal_color(&fm, "yellow\n", OWL_COLOR_YELLOW, OWL_COLOR_DEFAULT);
2800
2801  owl_fmtext_append_normal(&fm,"blue:     ");
2802  owl_fmtext_append_normal_color(&fm, "blue\n", OWL_COLOR_BLUE, OWL_COLOR_DEFAULT);
2803
2804  owl_fmtext_append_normal(&fm,"magenta:  ");
2805  owl_fmtext_append_normal_color(&fm, "magenta\n", OWL_COLOR_MAGENTA, OWL_COLOR_DEFAULT);
2806
2807  owl_fmtext_append_normal(&fm,"cyan:     ");
2808  owl_fmtext_append_normal_color(&fm, "cyan\n", OWL_COLOR_CYAN, OWL_COLOR_DEFAULT);
2809
2810  owl_fmtext_append_normal(&fm,"white:    ");
2811  owl_fmtext_append_normal_color(&fm, "white\n", OWL_COLOR_WHITE, OWL_COLOR_DEFAULT);
2812
2813  for(i = 8; i < COLORS; ++i) {
2814    char* str1 = g_strdup_printf("%4i:     ",i);
2815    char* str2 = g_strdup_printf("%i\n",i);
2816    owl_fmtext_append_normal(&fm,str1);
2817    owl_fmtext_append_normal_color(&fm, str2, i, OWL_COLOR_DEFAULT);
2818    g_free(str1);
2819     g_free(str2);
2820  }
2821 
2822  owl_function_popless_fmtext(&fm);
2823  owl_fmtext_cleanup(&fm);
2824}
2825
2826/* add the given class, inst, recip to the punt list for filtering.
2827 *   if direction==0 then punt
2828 *   if direction==1 then unpunt
2829 */
2830void owl_function_zpunt(const char *class, const char *inst, const char *recip, int direction)
2831{
2832  GPtrArray *argv;
2833  char *quoted;
2834
2835  argv = g_ptr_array_new();
2836  if (!strcmp(class, "*")) {
2837    g_ptr_array_add(argv, g_strdup("class"));
2838    g_ptr_array_add(argv, g_strdup(".*"));
2839  } else {
2840    quoted=owl_text_quote(class, OWL_REGEX_QUOTECHARS, OWL_REGEX_QUOTEWITH);
2841    g_ptr_array_add(argv, g_strdup("class"));
2842    g_ptr_array_add(argv, g_strdup_printf("^(un)*%s(\\.d)*$", quoted));
2843    g_free(quoted);
2844  }
2845  if (!strcmp(inst, "*")) {
2846    g_ptr_array_add(argv, g_strdup("and"));
2847    g_ptr_array_add(argv, g_strdup("instance"));
2848    g_ptr_array_add(argv, g_strdup(".*"));
2849  } else {
2850    quoted=owl_text_quote(inst, OWL_REGEX_QUOTECHARS, OWL_REGEX_QUOTEWITH);
2851    g_ptr_array_add(argv, g_strdup("and"));
2852    g_ptr_array_add(argv, g_strdup("instance"));
2853    g_ptr_array_add(argv, g_strdup_printf("^(un)*%s(\\.d)*$", quoted));
2854    g_free(quoted);
2855  }
2856  if (!strcmp(recip, "*")) {
2857    /* nothing */
2858  } else {
2859    if(!strcmp(recip, "%me%")) {
2860      recip = owl_zephyr_get_sender();
2861    }
2862    quoted=owl_text_quote(recip, OWL_REGEX_QUOTECHARS, OWL_REGEX_QUOTEWITH);
2863    g_ptr_array_add(argv, g_strdup("and"));
2864    g_ptr_array_add(argv, g_strdup("recipient"));
2865    g_ptr_array_add(argv, g_strdup_printf("^%s$", quoted));
2866    g_free(quoted);
2867  }
2868
2869  owl_function_punt(argv->len, (const char *const*) argv->pdata, direction);
2870  owl_ptr_array_free(argv, g_free);
2871}
2872
2873void owl_function_punt(int argc, const char *const *argv, int direction)
2874{
2875  owl_filter *f;
2876  GPtrArray *fl;
2877  int i;
2878  fl=owl_global_get_puntlist(&g);
2879
2880  /* first, create the filter */
2881  f = owl_filter_new("punt-filter", argc, argv);
2882  if (f == NULL) {
2883    owl_function_error("Error creating filter for zpunt");
2884    return;
2885  }
2886
2887  /* Check for an identical filter */
2888  for (i = 0; i < fl->len; i++) {
2889    if (owl_filter_equiv(f, fl->pdata[i])) {
2890      owl_function_debugmsg("found an equivalent punt filter");
2891      /* if we're punting, then just silently bow out on this duplicate */
2892      if (direction==0) {
2893        owl_filter_delete(f);
2894        return;
2895      }
2896
2897      /* if we're unpunting, then remove this filter from the puntlist */
2898      if (direction==1) {
2899        owl_filter_delete(g_ptr_array_remove_index(fl, i));
2900        owl_filter_delete(f);
2901        return;
2902      }
2903    }
2904  }
2905
2906  if (direction == 0) {
2907    owl_function_debugmsg("punting");
2908    /* If we're punting, add the filter to the global punt list */
2909    g_ptr_array_add(fl, f);
2910  } else if (direction == 1) {
2911    owl_function_makemsg("No matching punt filter");
2912 }
2913}
2914
2915void owl_function_show_keymaps(void)
2916{
2917  GPtrArray *l;
2918  owl_fmtext fm;
2919  const owl_keymap *km;
2920  const owl_keyhandler *kh;
2921  int i;
2922  const char *kmname;
2923
2924  kh = owl_global_get_keyhandler(&g);
2925  owl_fmtext_init_null(&fm);
2926  owl_fmtext_append_bold(&fm, "Keymaps:   ");
2927  owl_fmtext_append_normal(&fm, "(use 'show keymap <name>' for details)\n");
2928  l = owl_keyhandler_get_keymap_names(kh);
2929  owl_fmtext_append_list(&fm, l, "\n", owl_function_keymap_summary);
2930  owl_fmtext_append_normal(&fm, "\n");
2931
2932  for (i = 0; i < l->len; i++) {
2933    kmname = l->pdata[i];
2934    km = owl_keyhandler_get_keymap(kh, kmname);
2935    owl_fmtext_append_bold(&fm, "\n\n----------------------------------------------------------------------------------------------------\n\n");
2936    owl_keymap_get_details(km, &fm, 0);
2937  }
2938  owl_fmtext_append_normal(&fm, "\n");
2939 
2940  owl_function_popless_fmtext(&fm);
2941  owl_ptr_array_free(l, g_free);
2942  owl_fmtext_cleanup(&fm);
2943}
2944
2945CALLER_OWN char *owl_function_keymap_summary(const char *name)
2946{
2947  const owl_keymap *km
2948    = owl_keyhandler_get_keymap(owl_global_get_keyhandler(&g), name);
2949  if (km) return owl_keymap_summary(km);
2950  else return(NULL);
2951}
2952
2953/* TODO: implement for real */
2954void owl_function_show_keymap(const char *name)
2955{
2956  owl_fmtext fm;
2957  const owl_keymap *km;
2958
2959  owl_fmtext_init_null(&fm);
2960  km = owl_keyhandler_get_keymap(owl_global_get_keyhandler(&g), name);
2961  if (km) {
2962    owl_keymap_get_details(km, &fm, 1);
2963  } else {
2964    owl_fmtext_append_normal(&fm, "No such keymap...\n");
2965  } 
2966  owl_function_popless_fmtext(&fm);
2967  owl_fmtext_cleanup(&fm);
2968}
2969
2970void owl_function_help_for_command(const char *cmdname)
2971{
2972  owl_fmtext fm;
2973
2974  owl_fmtext_init_null(&fm);
2975  owl_cmd_get_help(owl_global_get_cmddict(&g), cmdname, &fm);
2976  owl_function_popless_fmtext(&fm); 
2977  owl_fmtext_cleanup(&fm);
2978}
2979
2980void owl_function_set_search(const char *string)
2981{
2982  owl_regex re;
2983
2984  if (string && owl_regex_create_quoted(&re, string) == 0) {
2985    owl_global_set_search_re(&g, &re);
2986    owl_regex_cleanup(&re);
2987  } else {
2988    owl_global_set_search_re(&g, NULL);
2989  }
2990}
2991
2992void owl_function_search_helper(int consider_current, int direction)
2993{
2994  /* move to a message that contains the string.  If direction is
2995   * OWL_DIRECTION_DOWNWARDS then search fowards, if direction is
2996   * OWL_DIRECTION_UPWARDS then search backwards.
2997   *
2998   * If consider_current is true then it will stay on the
2999   * current message if it contains the string.
3000   */
3001
3002  const owl_view *v;
3003  int viewsize, i, curmsg, start;
3004  owl_message *m;
3005
3006  v=owl_global_get_current_view(&g);
3007  viewsize=owl_view_get_size(v);
3008  curmsg=owl_global_get_curmsg(&g);
3009 
3010  if (viewsize==0) {
3011    owl_function_makemsg("No messages present");
3012    return;
3013  }
3014
3015  if (consider_current) {
3016    start=curmsg;
3017  } else if (direction==OWL_DIRECTION_DOWNWARDS) {
3018    start=curmsg+1;
3019  } else {
3020    start=curmsg-1;
3021  }
3022
3023  /* bounds check */
3024  if (start>=viewsize || start<0) {
3025    owl_function_makemsg("No further matches found");
3026    return;
3027  }
3028
3029  for (i=start; i<viewsize && i>=0;) {
3030    m=owl_view_get_element(v, i);
3031    if (owl_message_search(m, owl_global_get_search_re(&g))) {
3032      owl_global_set_curmsg(&g, i);
3033      owl_function_calculate_topmsg(direction);
3034      owl_mainwin_redisplay(owl_global_get_mainwin(&g));
3035      if (direction==OWL_DIRECTION_DOWNWARDS) {
3036        owl_global_set_direction_downwards(&g);
3037      } else {
3038        owl_global_set_direction_upwards(&g);
3039      }
3040      return;
3041    }
3042    if (direction==OWL_DIRECTION_DOWNWARDS) {
3043      i++;
3044    } else {
3045      i--;
3046    }
3047    if (owl_global_take_interrupt(&g)) {
3048      owl_function_makemsg("Search interrupted!");
3049      owl_mainwin_redisplay(owl_global_get_mainwin(&g));
3050      return;
3051    }
3052  }
3053  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
3054  owl_function_makemsg("No matches found");
3055}
3056
3057/* strips formatting from ztext and returns the unformatted text.
3058 * caller is responsible for freeing. */
3059CALLER_OWN char *owl_function_ztext_stylestrip(const char *zt)
3060{
3061  owl_fmtext fm;
3062  char *plaintext;
3063
3064  owl_fmtext_init_null(&fm);
3065  owl_fmtext_append_ztext(&fm, zt);
3066  plaintext = owl_fmtext_print_plain(&fm);
3067  owl_fmtext_cleanup(&fm);
3068  return(plaintext);
3069}
3070
3071/* Popup a buddylisting.  If filename is NULL use the default .anyone */
3072void owl_function_buddylist(int aim, int zephyr, const char *filename)
3073{
3074  int i, j, idle;
3075  int interrupted = 0;
3076  owl_fmtext fm;
3077  const owl_buddylist *bl;
3078  const owl_buddy *b;
3079  char *timestr;
3080#ifdef HAVE_LIBZEPHYR
3081  int x;
3082  GPtrArray *anyone;
3083  const char *user;
3084  char *tmp;
3085  ZLocations_t location[200];
3086  int numlocs, ret;
3087#endif
3088
3089  owl_fmtext_init_null(&fm);
3090
3091  /* AIM first */
3092  if (aim && owl_global_is_aimloggedin(&g)) {
3093    bl=owl_global_get_buddylist(&g);
3094
3095    owl_fmtext_append_bold(&fm, "AIM users logged in:\n");
3096    /* we're assuming AIM for now */
3097    j=owl_buddylist_get_size(bl);
3098    for (i=0; i<j; i++) {
3099      b=owl_buddylist_get_buddy_n(bl, i);
3100      idle=owl_buddy_get_idle_time(b);
3101      if (idle!=0) {
3102        timestr=owl_util_format_minutes(idle);
3103      } else {
3104        timestr=g_strdup("");
3105      }
3106      owl_fmtext_appendf_normal(&fm, "  %-20.20s %-12.12s\n", owl_buddy_get_name(b), timestr);
3107      g_free(timestr);
3108    }
3109  }
3110
3111#ifdef HAVE_LIBZEPHYR
3112  if (zephyr) {
3113    if(!owl_global_is_havezephyr(&g)) {
3114      owl_function_error("Zephyr currently not available.");
3115    } else {
3116      owl_fmtext_append_bold(&fm, "Zephyr users logged in:\n");
3117      anyone = owl_zephyr_get_anyone_list(filename);
3118      if (anyone == NULL) {
3119        if (errno == ENOENT) {
3120          owl_fmtext_append_normal(&fm, " You have not added any zephyr buddies.  Use the\n");
3121          owl_fmtext_append_normal(&fm, " command ':addbuddy zephyr ");
3122          owl_fmtext_append_bold(  &fm, "<username>");
3123          owl_fmtext_append_normal(&fm, "'.\n");
3124        } else {
3125          owl_fmtext_append_normal(&fm, " Could not read zephyr buddies from the .anyone file.\n");
3126        }
3127      } else {
3128        for (i = 0; i < anyone->len; i++) {
3129          user = anyone->pdata[i];
3130          ret=ZLocateUser(zstr(user), &numlocs, ZAUTH);
3131
3132          if (owl_global_take_interrupt(&g)) {
3133            interrupted = 1;
3134            owl_function_makemsg("Interrupted!");
3135            break;
3136          }
3137
3138          if (ret!=ZERR_NONE) {
3139            owl_function_error("Error getting location for %s", user);
3140            continue;
3141          }
3142
3143          numlocs=200;
3144          ret=ZGetLocations(location, &numlocs);
3145          if (ret==0) {
3146            for (x=0; x<numlocs; x++) {
3147              tmp=short_zuser(user);
3148              owl_fmtext_appendf_normal(&fm, "  %-10.10s %-24.24s %-12.12s  %20.20s\n",
3149                                        tmp,
3150                                        location[x].host,
3151                                        location[x].tty,
3152                                        location[x].time);
3153              g_free(tmp);
3154            }
3155            if (numlocs>=200) {
3156              owl_fmtext_append_normal(&fm, "  Too many locations found for this user, truncating.\n");
3157            }
3158          }
3159        }
3160      }
3161      owl_ptr_array_free(anyone, g_free);
3162    }
3163  }
3164#endif
3165
3166  if (aim && zephyr) {
3167    if (owl_perlconfig_is_function("BarnOwl::Hooks::_get_blist")) {
3168      char * perlblist = owl_perlconfig_execute("BarnOwl::Hooks::_get_blist()");
3169      if (perlblist) {
3170        owl_fmtext_append_ztext(&fm, perlblist);
3171        g_free(perlblist);
3172      }
3173    }
3174  }
3175
3176  if(!interrupted) {
3177    owl_function_popless_fmtext(&fm);
3178  }
3179  owl_fmtext_cleanup(&fm);
3180}
3181
3182/* Dump messages in the current view to the file 'filename'. */
3183void owl_function_dump(const char *filename) 
3184{
3185  int i, j;
3186  owl_message *m;
3187  const owl_view *v;
3188  FILE *file;
3189  char *plaintext;
3190
3191  v=owl_global_get_current_view(&g);
3192
3193  /* in the future make it ask yes/no */
3194  /*
3195  ret=stat(filename, &sbuf);
3196  if (!ret) {
3197    ret=owl_function_askyesno("File exists, continue? [Y/n]");
3198    if (!ret) return;
3199  }
3200  */
3201
3202  file=fopen(filename, "w");
3203  if (!file) {
3204    owl_function_error("Error opening file");
3205    return;
3206  }
3207
3208  j=owl_view_get_size(v);
3209  for (i=0; i<j; i++) {
3210    m=owl_view_get_element(v, i);
3211    plaintext = owl_strip_format_chars(owl_message_get_text(m));
3212    if (plaintext) {
3213      fputs(plaintext, file);
3214      g_free(plaintext);
3215    }
3216  }
3217  fclose(file);
3218  owl_function_makemsg("Messages dumped to %s", filename);
3219}
3220
3221void owl_function_do_newmsgproc(void)
3222{
3223  if (owl_global_get_newmsgproc(&g) && strcmp(owl_global_get_newmsgproc(&g), "")) {
3224    /* if there's a process out there, we need to check on it */
3225    if (owl_global_get_newmsgproc_pid(&g)) {
3226      owl_function_debugmsg("Checking on newmsgproc pid==%i", owl_global_get_newmsgproc_pid(&g));
3227      owl_function_debugmsg("Waitpid return is %i", waitpid(owl_global_get_newmsgproc_pid(&g), NULL, WNOHANG));
3228      waitpid(owl_global_get_newmsgproc_pid(&g), NULL, WNOHANG);
3229      if (waitpid(owl_global_get_newmsgproc_pid(&g), NULL, WNOHANG)==-1) {
3230        /* it exited */
3231        owl_global_set_newmsgproc_pid(&g, 0);
3232        owl_function_debugmsg("newmsgproc exited");
3233      } else {
3234        owl_function_debugmsg("newmsgproc did not exit");
3235      }
3236    }
3237   
3238    /* if it exited, spawn a new one */
3239    if (owl_global_get_newmsgproc_pid(&g)==0) {
3240      int myargc;
3241      char **argv = owl_parseline(owl_global_get_newmsgproc(&g), &myargc);
3242      if (myargc < 0) {
3243        owl_function_debugmsg("Could not parse newmsgproc '%s': unbalanced quotes?",
3244                              owl_global_get_newmsgproc(&g));
3245      } else if (myargc > 0) {
3246        /* Spawn the child. */
3247        GPid pid;
3248        GError *error = NULL;
3249        owl_function_debugmsg("About to exec \"%s\" with %d arguments", argv[0], myargc);
3250        if (g_spawn_async(NULL, argv, NULL,
3251                          G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD,
3252                          NULL, NULL, &pid, &error)) {
3253          owl_global_set_newmsgproc_pid(&g, pid);
3254          owl_function_debugmsg("I'm the parent and I started a new newmsgproc with pid %i", pid);
3255        } else {
3256          owl_function_debugmsg("Cannot run newmsgproc '%s': %s",
3257                                owl_global_get_newmsgproc(&g), error->message);
3258          g_error_free(error);
3259        }
3260      }
3261      g_strfreev(argv);
3262    }
3263  }
3264}
3265
3266/* print the xterm escape sequence to raise the window */
3267void owl_function_xterm_raise(void)
3268{
3269  printf("\033[5t");
3270}
3271
3272/* print the xterm escape sequence to deiconify the window */
3273void owl_function_xterm_deiconify(void)
3274{
3275  printf("\033[1t");
3276}
3277
3278/* Add the specified command to the startup file.  Eventually this
3279 * should be clever, and rewriting settings that will obviosly
3280 * override earlier settings with 'set' 'bindkey' and 'alias'
3281 * commands.  For now though we just remove any line that would
3282 * duplicate this one and then append this line to the end of
3283 * startupfile.
3284 */
3285void owl_function_addstartup(const char *buff)
3286{
3287  FILE *file;
3288  const char *filename;
3289
3290  filename=owl_global_get_startupfile(&g);
3291
3292  /* delete earlier copies, if the file exists */
3293  if (g_file_test(filename, G_FILE_TEST_EXISTS))
3294    owl_util_file_deleteline(filename, buff, 1);
3295
3296  file=fopen(filename, "a");
3297  if (!file) {
3298    owl_function_error("Error opening startupfile for new command");
3299    return;
3300  }
3301
3302  /* add this line */
3303  fprintf(file, "%s\n", buff);
3304
3305  fclose(file);
3306}
3307
3308/* Remove the specified command from the startup file. */
3309void owl_function_delstartup(const char *buff)
3310{
3311  const char *filename;
3312  filename=owl_global_get_startupfile(&g);
3313  owl_util_file_deleteline(filename, buff, 1);
3314}
3315
3316/* Execute owl commands from the given filename.  If the filename
3317 * is NULL, use the default owl startup commands file.
3318 */
3319void owl_function_source(const char *filename)
3320{
3321  char *path;
3322  FILE *file;
3323  char *s = NULL;
3324  int fail_silent = 0;
3325
3326  if (!filename) {
3327    fail_silent = 1;
3328    path = g_strdup(owl_global_get_startupfile(&g));
3329  } else {
3330    path = owl_util_makepath(filename);
3331  }
3332  file = fopen(path, "r");
3333  g_free(path);
3334  if (!file) {
3335    if (!fail_silent) {
3336      owl_function_error("Error opening file: %s", filename);
3337    }
3338    return;
3339  }
3340  while (owl_getline_chomp(&s, file)) {
3341    if (s[0] == '\0' || s[0] == '#')
3342      continue;
3343    owl_function_command_norv(s);
3344  }
3345
3346  g_free(s);
3347  fclose(file);
3348}
3349
3350void owl_function_change_style(owl_view *v, const char *stylename)
3351{
3352  const owl_style *s;
3353
3354  s=owl_global_get_style_by_name(&g, stylename);
3355  if (!s) {
3356    owl_function_error("No style named %s", stylename);
3357    return;
3358  }
3359  owl_view_set_style(v, s);
3360  owl_messagelist_invalidate_formats(owl_global_get_msglist(&g));
3361  owl_function_calculate_topmsg(OWL_DIRECTION_DOWNWARDS);
3362  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
3363}
3364
3365void owl_function_toggleoneline(void)
3366{
3367  owl_view *v;
3368  const owl_style *s;
3369
3370  v=owl_global_get_current_view(&g);
3371  s=owl_view_get_style(v);
3372
3373  if (!owl_style_matches_name(s, "oneline")) {
3374    owl_function_change_style(v, "oneline");
3375  } else {
3376    owl_function_change_style(v, owl_global_get_default_style(&g));
3377  }
3378
3379  owl_messagelist_invalidate_formats(owl_global_get_msglist(&g));
3380  owl_function_calculate_topmsg(OWL_DIRECTION_DOWNWARDS);
3381  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
3382}
3383
3384void G_GNUC_PRINTF(1, 2) owl_function_error(const char *fmt, ...)
3385{
3386  static int in_error = 0;
3387  va_list ap;
3388  char *buff;
3389  const char *nl;
3390
3391  if (++in_error > 2) {
3392    /* More than two nested errors, bail immediately. */
3393    in_error--;
3394    return;
3395  }
3396
3397  va_start(ap, fmt);
3398  buff = g_strdup_vprintf(fmt, ap);
3399  va_end(ap);
3400
3401  owl_function_debugmsg("ERROR: %s", buff);
3402  owl_function_log_err(buff);
3403
3404  nl = strchr(buff, '\n');
3405
3406  /*
3407    Showing admin messages triggers a lot of code. If we have a
3408    recursive error call, that's the most likely candidate, so
3409    suppress the call in that case, to try to avoid infinite looping.
3410  */
3411
3412  if(nl && *(nl + 1) && in_error == 1) {
3413    /* Multiline error */
3414    owl_function_adminmsg("ERROR", buff);
3415  } else {
3416    owl_function_makemsg("[Error] %s", buff);
3417  }
3418
3419  g_free(buff);
3420
3421  in_error--;
3422}
3423
3424void owl_function_log_err(const char *string)
3425{
3426  char *date;
3427  time_t now;
3428  struct tm tm;
3429  char *buff;
3430
3431  now = time(NULL);
3432  date = owl_util_format_time(localtime_r(&now, &tm));
3433
3434  buff = g_strdup_printf("%s %s", date, string);
3435
3436  owl_errqueue_append_err(owl_global_get_errqueue(&g), buff);
3437
3438  g_free(buff);
3439  g_free(date);
3440}
3441
3442void owl_function_showerrs(void)
3443{
3444  owl_fmtext fm;
3445
3446  owl_fmtext_init_null(&fm);
3447  owl_fmtext_append_normal(&fm, "Errors:\n\n");
3448  owl_errqueue_to_fmtext(owl_global_get_errqueue(&g), &fm);
3449  owl_function_popless_fmtext(&fm);
3450}
3451
3452void G_GNUC_PRINTF(1, 2) owl_function_makemsg(const char *fmt, ...)
3453{
3454  va_list ap;
3455  char *str;
3456
3457  va_start(ap, fmt);
3458  str = g_strdup_vprintf(fmt, ap);
3459  va_end(ap);
3460
3461  owl_function_debugmsg("makemsg: %s", str);
3462  owl_msgwin_set_text_nocopy(&g.msgwin, str);
3463}
3464
3465/* get locations for everyone in .anyone.  If 'notify' is '1' then
3466 * send a pseudo login or logout message for everyone not in sync with
3467 * the global zephyr buddy list.  The list is updated regardless of
3468 * the status of 'notify'.
3469 */
3470void owl_function_zephyr_buddy_check(int notify)
3471{
3472#ifdef HAVE_LIBZEPHYR
3473  int i;
3474  GPtrArray *anyone;
3475  GList **zaldlist;
3476  GList *zaldptr;
3477  ZAsyncLocateData_t *zald;
3478  const char *user;
3479
3480  if (!owl_global_is_havezephyr(&g)) return;
3481  owl_global_set_pseudologin_notify(&g, notify);
3482  zaldlist = owl_global_get_zaldlist(&g);
3483
3484  /* Clear the existing ZALDs first. */
3485  zaldptr = g_list_first(*zaldlist);
3486  while (zaldptr) {
3487    ZFreeALD(zaldptr->data);
3488    g_slice_free(ZAsyncLocateData_t, zaldptr->data);
3489    zaldptr = g_list_next(zaldptr);
3490  }
3491  g_list_free(*zaldlist);
3492  *zaldlist = NULL;
3493
3494  anyone = owl_zephyr_get_anyone_list(NULL);
3495  if (anyone != NULL) {
3496    for (i = 0; i < anyone->len; i++) {
3497      user = anyone->pdata[i];
3498      zald = g_slice_new(ZAsyncLocateData_t);
3499      if (ZRequestLocations(zstr(user), zald, UNACKED, ZAUTH) == ZERR_NONE) {
3500        *zaldlist = g_list_append(*zaldlist, zald);
3501      } else {
3502        g_slice_free(ZAsyncLocateData_t, zald);
3503      }
3504    }
3505    owl_ptr_array_free(anyone, g_free);
3506  }
3507#endif
3508}
3509
3510void owl_function_aimsearch_results(const char *email, GPtrArray *namelist)
3511{
3512  owl_fmtext fm;
3513  int i;
3514
3515  owl_fmtext_init_null(&fm);
3516  owl_fmtext_append_normal(&fm, "AIM screennames associated with ");
3517  owl_fmtext_append_normal(&fm, email);
3518  owl_fmtext_append_normal(&fm, ":\n");
3519
3520  for (i = 0; i < namelist->len; i++) {
3521    owl_fmtext_append_normal(&fm, "  ");
3522    owl_fmtext_append_normal(&fm, namelist->pdata[i]);
3523    owl_fmtext_append_normal(&fm, "\n");
3524  }
3525
3526  owl_function_popless_fmtext(&fm);
3527  owl_fmtext_cleanup(&fm);
3528}
3529
3530int owl_function_get_color_count(void)
3531{
3532     return COLORS;
3533}
3534
3535void _owl_function_mark_message(const owl_message *m)
3536{
3537  if (m) {
3538    owl_global_set_markedmsgid(&g, owl_message_get_id(m));
3539    owl_mainwin_redisplay(owl_global_get_mainwin(&g));
3540  }
3541}
3542
3543void owl_function_mark_message(void)
3544{
3545  const owl_message *m;
3546  const owl_view *v;
3547
3548  v=owl_global_get_current_view(&g);
3549
3550  /* bail if there's no current message */
3551  if (owl_view_get_size(v) < 1) {
3552    owl_function_error("No messages to mark");
3553    return;
3554  }
3555
3556  /* mark the message */
3557  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
3558  _owl_function_mark_message(m);
3559  owl_function_makemsg("Mark set");
3560}
3561
3562void owl_function_swap_cur_marked(void)
3563{
3564  int marked_id;
3565  const owl_message *m;
3566  const owl_view *v;
3567
3568  marked_id=owl_global_get_markedmsgid(&g);
3569  if (marked_id == -1) {
3570    owl_function_error("Mark not set.");
3571    return;
3572  }
3573
3574  v=owl_global_get_current_view(&g);
3575  /* bail if there's no current message */
3576  if (owl_view_get_size(v) < 1) {
3577    return;
3578  }
3579
3580  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
3581  _owl_function_mark_message(m);
3582  owl_global_set_curmsg(&g, owl_view_get_nearest_to_msgid(v, marked_id));
3583  owl_function_calculate_topmsg(OWL_DIRECTION_NONE);
3584  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
3585  owl_global_set_direction_downwards(&g);
3586}
Note: See TracBrowser for help on using the repository browser.