source: functions.c @ a962f5c

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