source: functions.c @ f36222f

barnowl_perlaimdebianowlrelease-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since f36222f was f36222f, checked in by James M. Kretchmar <kretch@mit.edu>, 17 years ago
Do ~ expansion in 'dump' command.
  • Property mode set to 100644
File size: 88.8 KB
RevLine 
[7d4fbcd]1#include <stdio.h>
2#include <stdlib.h>
3#include <unistd.h>
4#include <signal.h>
5#include <string.h>
6#include <time.h>
[2adaf1d]7#include <sys/types.h>
8#include <sys/stat.h>
[8f44c6b]9#include <sys/wait.h>
10#include <errno.h>
[d09e5a1]11#include <signal.h>
[7d4fbcd]12#include "owl.h"
13
[1aee7d9]14static const char fileIdent[] = "$Id$";
15
[d54838d]16void owl_function_noop(void)
17{
[7d4fbcd]18  return;
19}
20
[d54838d]21char *owl_function_command(char *cmdbuff)
22{
[7d4fbcd]23  owl_function_debugmsg("executing command: %s", cmdbuff);
24  return owl_cmddict_execute(owl_global_get_cmddict(&g), 
25                             owl_global_get_context(&g), cmdbuff);
26}
27
[d54838d]28void owl_function_command_norv(char *cmdbuff)
29{
[7d4fbcd]30  char *rv;
[4b464a4]31  rv=owl_function_command(cmdbuff);
[7d4fbcd]32  if (rv) owl_free(rv);
33}
34
[d54838d]35void owl_function_command_alias(char *alias_from, char *alias_to)
36{
[7d4fbcd]37  owl_cmddict_add_alias(owl_global_get_cmddict(&g), alias_from, alias_to);
38}
39
[d54838d]40owl_cmd *owl_function_get_cmd(char *name)
41{
[7d4fbcd]42  return owl_cmddict_find(owl_global_get_cmddict(&g), name);
43}
44
[d54838d]45void owl_function_show_commands()
46{
[7d4fbcd]47  owl_list l;
48  owl_fmtext fm;
49
50  owl_fmtext_init_null(&fm);
51  owl_fmtext_append_bold(&fm, "Commands:  ");
52  owl_fmtext_append_normal(&fm, "(use 'show command <name>' for details)\n");
53  owl_cmddict_get_names(owl_global_get_cmddict(&g), &l);
54  owl_fmtext_append_list(&fm, &l, "\n", owl_function_cmd_describe);
55  owl_fmtext_append_normal(&fm, "\n");
56  owl_function_popless_fmtext(&fm);
57  owl_cmddict_namelist_free(&l);
58  owl_fmtext_free(&fm);
59}
60
[ef56a67]61void owl_function_show_view(char *viewname)
62{
63  owl_view *v;
64  owl_fmtext fm;
65
66  /* we only have the one view right now */
67  v=owl_global_get_current_view(&g);
68  if (viewname && strcmp(viewname, owl_view_get_name(v))) {
[ec6ff52]69    owl_function_error("No view named '%s'", viewname);
[ef56a67]70    return;
71  }
72
73  owl_fmtext_init_null(&fm);
74  owl_view_to_fmtext(v, &fm);
75  owl_function_popless_fmtext(&fm);
76  owl_fmtext_free(&fm);
77}
78
[f1e629d]79void owl_function_show_styles() {
80  owl_list l;
81  owl_fmtext fm;
82
83  owl_fmtext_init_null(&fm);
84  owl_fmtext_append_bold(&fm, "Styles:\n");
85  owl_global_get_style_names(&g, &l);
86  owl_fmtext_append_list(&fm, &l, "\n", owl_function_style_describe);
87  owl_fmtext_append_normal(&fm, "\n");
88  owl_function_popless_fmtext(&fm);
89  owl_list_free_all(&l, owl_free);
90  owl_fmtext_free(&fm);
91}
92
93char *owl_function_style_describe(void *name) {
94  char *desc, *s;
95  owl_style *style;
96  style = owl_global_get_style_by_name(&g, name);
97  if (style) {
98    desc = owl_style_get_description(style);
99  } else {
100    desc = "???";
101  }
102  s = owl_sprintf("%-20s - %s%s", name, 
103                  0==owl_style_validate(style)?"":"[INVALID] ",
104                  desc);
105  return s;
106}
[ef56a67]107
[d54838d]108char *owl_function_cmd_describe(void *name)
109{
[7d4fbcd]110  owl_cmd *cmd = owl_cmddict_find(owl_global_get_cmddict(&g), name);
111  if (cmd) return owl_cmd_describe(cmd);
112  else return(NULL);
113}
114
[d54838d]115void owl_function_show_command(char *name)
116{
[7d4fbcd]117  owl_function_help_for_command(name);
118}
119
[d54838d]120void owl_function_adminmsg(char *header, char *body)
121{
[7d4fbcd]122  owl_message *m;
123  int followlast;
124
125  followlast=owl_global_should_followlast(&g);
126  m=owl_malloc(sizeof(owl_message));
127  owl_message_create_admin(m, header, body);
128  owl_messagelist_append_element(owl_global_get_msglist(&g), m);
129  owl_view_consider_message(owl_global_get_current_view(&g), m);
130
131  if (followlast) owl_function_lastmsg_noredisplay();
132
133  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
134  if (owl_popwin_is_active(owl_global_get_popwin(&g))) {
135    owl_popwin_refresh(owl_global_get_popwin(&g));
136  }
137 
138  wnoutrefresh(owl_global_get_curs_recwin(&g));
139  owl_global_set_needrefresh(&g);
140}
141
[d54838d]142void owl_function_make_outgoing_zephyr(char *body, char *zwriteline, char *zsig)
143{
[7d4fbcd]144  owl_message *m;
145  int followlast;
[4b464a4]146  owl_zwrite z;
[7d4fbcd]147 
148  followlast=owl_global_should_followlast(&g);
[4b464a4]149
150  /* create a zwrite for the purpose of filling in other message fields */
151  owl_zwrite_create_from_line(&z, zwriteline);
152
153  /* create the message */
[7d4fbcd]154  m=owl_malloc(sizeof(owl_message));
[b45293f]155  owl_message_create_from_zwriteline(m, zwriteline, body, zsig);
[4b464a4]156
[8fec514]157  /* add it to the global list and current view */
[7d4fbcd]158  owl_messagelist_append_element(owl_global_get_msglist(&g), m);
159  owl_view_consider_message(owl_global_get_current_view(&g), m);
160
161  if (followlast) owl_function_lastmsg_noredisplay();
162
163  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
164  if (owl_popwin_is_active(owl_global_get_popwin(&g))) {
165    owl_popwin_refresh(owl_global_get_popwin(&g));
166  }
167 
168  wnoutrefresh(owl_global_get_curs_recwin(&g));
169  owl_global_set_needrefresh(&g);
[56330ff]170  owl_zwrite_free(&z);
[7d4fbcd]171}
172
[aa5f725]173int owl_function_make_outgoing_aim(char *body, char *to)
[d09e5a1]174{
175  owl_message *m;
176  int followlast;
[aa5f725]177
178
179  if (!owl_global_is_aimloggedin(&g)) {
180    return(-1);
181  }
[d09e5a1]182 
183  followlast=owl_global_should_followlast(&g);
184
185  /* create the message */
186  m=owl_malloc(sizeof(owl_message));
[d559df9]187  owl_message_create_aim(m,
188                         owl_global_get_aim_screenname(&g),
189                         to,
190                         body,
191                         OWL_MESSAGE_DIRECTION_OUT,
192                         0);
[d09e5a1]193
194  /* add it to the global list and current view */
195  owl_messagelist_append_element(owl_global_get_msglist(&g), m);
196  owl_view_consider_message(owl_global_get_current_view(&g), m);
197
198  if (followlast) owl_function_lastmsg_noredisplay();
199
200  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
201  if (owl_popwin_is_active(owl_global_get_popwin(&g))) {
202    owl_popwin_refresh(owl_global_get_popwin(&g));
203  }
204 
205  wnoutrefresh(owl_global_get_curs_recwin(&g));
206  owl_global_set_needrefresh(&g);
[aa5f725]207  return(0);
[d09e5a1]208}
209
[37eab7f]210int owl_function_make_outgoing_loopback(char *body)
211{
212  owl_message *m;
213  int followlast;
214
215  followlast=owl_global_should_followlast(&g);
216
217  /* create the message */
218  m=owl_malloc(sizeof(owl_message));
219  owl_message_create_loopback(m, body);
220  owl_message_set_direction_out(m);
221
222  /* add it to the global list and current view */
223  owl_messagelist_append_element(owl_global_get_msglist(&g), m);
224  owl_view_consider_message(owl_global_get_current_view(&g), m);
225
226  if (followlast) owl_function_lastmsg_noredisplay();
227
228  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
229  if (owl_popwin_is_active(owl_global_get_popwin(&g))) {
230    owl_popwin_refresh(owl_global_get_popwin(&g));
231  }
232 
233  wnoutrefresh(owl_global_get_curs_recwin(&g));
234  owl_global_set_needrefresh(&g);
235  return(0);
236}
237
[d54838d]238void owl_function_zwrite_setup(char *line)
239{
[7d4fbcd]240  owl_editwin *e;
241  char buff[1024];
242  owl_zwrite z;
243  int ret;
244
245  /* check the arguments */
246  ret=owl_zwrite_create_from_line(&z, line);
247  if (ret) {
[ec6ff52]248    owl_function_error("Error in zwrite arugments");
[7d4fbcd]249    owl_zwrite_free(&z);
250    return;
251  }
252
253  /* send a ping if necessary */
254  if (owl_global_is_txping(&g)) {
255    owl_zwrite_send_ping(&z);
256  }
257  owl_zwrite_free(&z);
258
259  /* create and setup the editwin */
260  e=owl_global_get_typwin(&g);
[56330ff]261  owl_editwin_new_style(e, OWL_EDITWIN_STYLE_MULTILINE, owl_global_get_msg_history(&g));
[7d4fbcd]262
[217a43e]263  if (!owl_global_get_lockout_ctrld(&g)) {
[7d4fbcd]264    owl_function_makemsg("Type your zephyr below.  End with ^D or a dot on a line by itself.  ^C will quit.");
265  } else {
266    owl_function_makemsg("Type your zephyr below.  End with a dot on a line by itself.  ^C will quit.");
267  }
268
269  owl_editwin_clear(e);
270  owl_editwin_set_dotsend(e);
271  strcpy(buff, "----> ");
272  strcat(buff, line);
273  strcat(buff, "\n");
274  owl_editwin_set_locktext(e, buff);
275
276  /* make it active */
277  owl_global_set_typwin_active(&g);
[453bd70]278
279  owl_global_set_buffercommand(&g, line);
[7d4fbcd]280}
281
[d09e5a1]282void owl_function_aimwrite_setup(char *line)
283{
284  owl_editwin *e;
285  char buff[1024];
286
287  /* check the arguments */
288
289  /* create and setup the editwin */
290  e=owl_global_get_typwin(&g);
291  owl_editwin_new_style(e, OWL_EDITWIN_STYLE_MULTILINE, owl_global_get_msg_history(&g));
292
293  if (!owl_global_get_lockout_ctrld(&g)) {
294    owl_function_makemsg("Type your message below.  End with ^D or a dot on a line by itself.  ^C will quit.");
295  } else {
296    owl_function_makemsg("Type your message below.  End with a dot on a line by itself.  ^C will quit.");
297  }
298
299  owl_editwin_clear(e);
300  owl_editwin_set_dotsend(e);
301  strcpy(buff, "----> ");
302  strcat(buff, line);
303  strcat(buff, "\n");
304  owl_editwin_set_locktext(e, buff);
305
306  /* make it active */
307  owl_global_set_typwin_active(&g);
[453bd70]308
309  owl_global_set_buffercommand(&g, line);
[d09e5a1]310}
311
[37eab7f]312void owl_function_loopwrite_setup()
313{
314  owl_editwin *e;
315
316  /* create and setup the editwin */
317  e=owl_global_get_typwin(&g);
318  owl_editwin_new_style(e, OWL_EDITWIN_STYLE_MULTILINE, owl_global_get_msg_history(&g));
319
320  if (!owl_global_get_lockout_ctrld(&g)) {
321    owl_function_makemsg("Type your message below.  End with ^D or a dot on a line by itself.  ^C will quit.");
322  } else {
323    owl_function_makemsg("Type your message below.  End with a dot on a line by itself.  ^C will quit.");
324  }
325
326  owl_editwin_clear(e);
327  owl_editwin_set_dotsend(e);
328  owl_editwin_set_locktext(e, "----> loopwrite\n");
329
330  /* make it active */
331  owl_global_set_typwin_active(&g);
332
333  owl_global_set_buffercommand(&g, "loopwrite");
334}
335
[9ceee9d]336/* send, log and display an outgoing zephyr.  If 'msg' is NULL
337 * the message is expected to be set from the zwrite line itself
338 */
339void owl_function_zwrite(char *line, char *msg)
[d54838d]340{
[d309eb3]341  owl_zwrite z;
[9ceee9d]342  int i, j;
343  char *mymsg;
[d309eb3]344
[9ceee9d]345  /* create the zwrite and send the message */
346  owl_zwrite_create_from_line(&z, line);
347  if (msg) {
348    owl_zwrite_set_message(&z, msg);
[d309eb3]349  }
350
[9ceee9d]351  owl_zwrite_send_message(&z);
352  owl_function_makemsg("Waiting for ack...");
[d309eb3]353
[9ceee9d]354  mymsg=owl_zwrite_get_message(&z);
[d309eb3]355
[9ceee9d]356  /* display the message as an outgoing message in the receive window */
357  if (owl_global_is_displayoutgoing(&g) && owl_zwrite_is_personal(&z)) {
358    owl_function_make_outgoing_zephyr(mymsg, line, owl_zwrite_get_zsig(&z));
[d309eb3]359  }
360
[9ceee9d]361  /* log it if we have logging turned on */
362  if (owl_global_is_logging(&g) && owl_zwrite_is_personal(&z)) {
363    j=owl_zwrite_get_numrecips(&z);
364    for (i=0; i<j; i++) {
365      owl_log_outgoing_zephyr(owl_zwrite_get_recip_n(&z, i), mymsg);
366    }
367  }
[453bd70]368
[9ceee9d]369  /* free the zwrite */
370  owl_zwrite_free(&z);
[d309eb3]371}
372
[9ceee9d]373/* send, log and display an outgoing zcrypt zephyr.  If 'msg' is NULL
[ce7db4d]374 * the message is expected to be set from the zwrite line itself
375 */
[9ceee9d]376void owl_function_zcrypt(char *line, char *msg)
[d54838d]377{
[7d4fbcd]378  owl_zwrite z;
[8a54409]379  int i, j;
[ce7db4d]380  char *mymsg;
[9ceee9d]381  char *cryptmsg;
[8a54409]382#ifdef OWL_ENABLE_ZCRYPT
383  int ret;
384#endif
[7d4fbcd]385
386  /* create the zwrite and send the message */
387  owl_zwrite_create_from_line(&z, line);
[ce7db4d]388  if (msg) {
389    owl_zwrite_set_message(&z, msg);
390  }
391
[9ceee9d]392  mymsg=owl_zwrite_get_message(&z);
393#ifdef OWL_ENABLE_ZCRYPT
394  cryptmsg=owl_malloc(strlen(mymsg)*4);
395  ret=owl_zcrypt_encrypt(cryptmsg, mymsg, owl_zwrite_get_class(&z), owl_zwrite_get_instance(&z));
396  if (ret) {
[ec6ff52]397    owl_function_error("Error in zcrypt, possibly no key found.  Message not sent.");
[9ceee9d]398    owl_function_beep();
399    owl_free(cryptmsg);
400    return;
401  }
402#else
403  cryptmsg=owl_strdup(mymsg);
404#endif
405
406  owl_zwrite_set_message(&z, cryptmsg);
407  owl_zwrite_set_opcode(&z, "crypt");
408  mymsg=cryptmsg;
409   
[ce7db4d]410  owl_zwrite_send_message(&z);
[7d4fbcd]411  owl_function_makemsg("Waiting for ack...");
412
[56330ff]413  /* display the message as an outgoing message in the receive window */
[7d4fbcd]414  if (owl_global_is_displayoutgoing(&g) && owl_zwrite_is_personal(&z)) {
[ce7db4d]415    owl_function_make_outgoing_zephyr(mymsg, line, owl_zwrite_get_zsig(&z));
[7d4fbcd]416  }
417
418  /* log it if we have logging turned on */
419  if (owl_global_is_logging(&g) && owl_zwrite_is_personal(&z)) {
420    j=owl_zwrite_get_numrecips(&z);
421    for (i=0; i<j; i++) {
[ce7db4d]422      owl_log_outgoing_zephyr(owl_zwrite_get_recip_n(&z, i), mymsg);
[7d4fbcd]423    }
424  }
425
426  /* free the zwrite */
[9ceee9d]427  owl_free(cryptmsg);
[7d4fbcd]428  owl_zwrite_free(&z);
429}
430
[d09e5a1]431void owl_function_aimwrite(char *to)
432{
[ec6ff52]433  int ret;
[f82e233]434  char *msg, *format_msg;
435
436  /* make a formatted copy of the message */
437  msg=owl_editwin_get_text(owl_global_get_typwin(&g));
438  format_msg=owl_strdup(msg);
439  owl_text_wordunwrap(format_msg);
[ec6ff52]440 
[f82e233]441  /* send the message */
442  ret=owl_aim_send_im(to, format_msg);
[ec6ff52]443  if (!ret) {
444    owl_function_makemsg("AIM message sent.");
445  } else {
446    owl_function_error("Could not send AIM message.");
447  }
[d09e5a1]448
449  /* display the message as an outgoing message in the receive window */
450  if (owl_global_is_displayoutgoing(&g)) {
[f82e233]451    owl_function_make_outgoing_aim(msg, to);
[d09e5a1]452  }
453
454  /* log it if we have logging turned on */
455  if (owl_global_is_logging(&g)) {
[f82e233]456    owl_log_outgoing_aim(to, msg);
[d09e5a1]457  }
[f82e233]458
459  owl_free(format_msg);
[d09e5a1]460}
461
[37eab7f]462void owl_function_loopwrite()
463{
464  owl_message *m;
465
466  /* create a message and put it on the message queue.  This simulates
467   * an incoming message */
468  m=owl_malloc(sizeof(owl_message));
469  owl_message_create_loopback(m, owl_editwin_get_text(owl_global_get_typwin(&g)));
[eec69e1]470  owl_message_set_direction_in(m);
[37eab7f]471  owl_global_messagequeue_addmsg(&g, m);
472
473  /* display the message as an outgoing message in the receive window */
474  if (owl_global_is_displayoutgoing(&g)) {
475    owl_function_make_outgoing_loopback(owl_editwin_get_text(owl_global_get_typwin(&g)));
476  }
477
478  /* fake a makemsg */
479  owl_function_makemsg("loopback message sent");
480
481  /* log it if we have logging turned on */
482  if (owl_global_is_logging(&g)) {
483    owl_log_outgoing_loopback(owl_editwin_get_text(owl_global_get_typwin(&g)));
484  }
485}
486
[b950088]487/* If filter is non-null, looks for the next message matching
488 * that filter.  If skip_deleted, skips any deleted messages.
489 * If last_if_none, will stop at the last message in the view
490 * if no matching messages are found.  */
[d54838d]491void owl_function_nextmsg_full(char *filter, int skip_deleted, int last_if_none)
492{
[b950088]493  int curmsg, i, viewsize, found;
[7d4fbcd]494  owl_view *v;
[b950088]495  owl_filter *f = NULL;
496  owl_message *m;
[7d4fbcd]497
498  v=owl_global_get_current_view(&g);
[b950088]499
500  if (filter) {
501    f=owl_global_get_filter(&g, filter);
502    if (!f) {
[ec6ff52]503      owl_function_error("No %s filter defined", filter);
[b950088]504      return;
505    }
[7d4fbcd]506  }
507
[b950088]508  curmsg=owl_global_get_curmsg(&g);
509  viewsize=owl_view_get_size(v);
510  found=0;
[7d4fbcd]511
[b950088]512  /* just check to make sure we're in bounds... */
513  if (curmsg>viewsize-1) curmsg=viewsize-1;
514  if (curmsg<0) curmsg=0;
[7d4fbcd]515
[b950088]516  for (i=curmsg+1; i<viewsize; i++) {
517    m=owl_view_get_element(v, i);
518    if (skip_deleted && owl_message_is_delete(m)) continue;
519    if (f && !owl_filter_message_match(f, m)) continue;
520    found = 1;
521    break;
522  }
523
524  if (i>owl_view_get_size(v)-1) i=owl_view_get_size(v)-1;
525
526  if (!found) {
[f51bc78]527    owl_function_makemsg("already at last%s message%s%s",
[b950088]528                         skip_deleted?" non-deleted":"",
529                         filter?" in ":"", filter?filter:"");
[5a6e6b9]530    /* if (!skip_deleted) owl_function_beep(); */
[7d4fbcd]531  }
532
[b950088]533  if (last_if_none || found) {
534    owl_global_set_curmsg(&g, i);
535    owl_function_calculate_topmsg(OWL_DIRECTION_DOWNWARDS);
536    owl_mainwin_redisplay(owl_global_get_mainwin(&g));
537    owl_global_set_direction_downwards(&g);
538  }
539}
[7d4fbcd]540
[d54838d]541void owl_function_prevmsg_full(char *filter, int skip_deleted, int first_if_none)
542{
[b950088]543  int curmsg, i, viewsize, found;
[7d4fbcd]544  owl_view *v;
[b950088]545  owl_filter *f = NULL;
546  owl_message *m;
[7d4fbcd]547
548  v=owl_global_get_current_view(&g);
549
[b950088]550  if (filter) {
551    f=owl_global_get_filter(&g, filter);
552    if (!f) {
[ec6ff52]553      owl_function_error("No %s filter defined", filter);
[b950088]554      return;
[7d4fbcd]555    }
[b950088]556  }
[7d4fbcd]557
[b950088]558  curmsg=owl_global_get_curmsg(&g);
559  viewsize=owl_view_get_size(v);
560  found=0;
561
562  /* just check to make sure we're in bounds... */
563  if (curmsg<0) curmsg=0;
564
565  for (i=curmsg-1; i>=0; i--) {
566    m=owl_view_get_element(v, i);
567    if (skip_deleted && owl_message_is_delete(m)) continue;
568    if (f && !owl_filter_message_match(f, m)) continue;
569    found = 1;
570    break;
571  }
572
573  if (i<0) i=0;
574
575  if (!found) {
[f51bc78]576    owl_function_makemsg("already at first%s message%s%s",
[b950088]577                         skip_deleted?" non-deleted":"",
578                         filter?" in ":"", filter?filter:"");
[5a6e6b9]579    /* if (!skip_deleted) owl_function_beep(); */
[b950088]580  }
581
582  if (first_if_none || found) {
583    owl_global_set_curmsg(&g, i);
584    owl_function_calculate_topmsg(OWL_DIRECTION_UPWARDS);
585    owl_mainwin_redisplay(owl_global_get_mainwin(&g));
586    owl_global_set_direction_upwards(&g);
[7d4fbcd]587  }
588}
589
[d54838d]590void owl_function_nextmsg()
591{
[b950088]592  owl_function_nextmsg_full(NULL, 0, 1);
593}
[7d4fbcd]594
[d54838d]595void owl_function_prevmsg()
596{
[b950088]597  owl_function_prevmsg_full(NULL, 0, 1);
598}
[7d4fbcd]599
[d54838d]600void owl_function_nextmsg_notdeleted()
601{
[b950088]602  owl_function_nextmsg_full(NULL, 1, 1);
603}
[7d4fbcd]604
[d54838d]605void owl_function_prevmsg_notdeleted()
606{
[b950088]607  owl_function_prevmsg_full(NULL, 1, 1);
[7d4fbcd]608}
609
[d54838d]610void owl_function_nextmsg_personal()
611{
[b950088]612  owl_function_nextmsg_full("personal", 0, 0);
613}
614
[d54838d]615void owl_function_prevmsg_personal()
616{
[b950088]617  owl_function_prevmsg_full("personal", 0, 0);
618}
619
620
621/* if move_after is 1, moves after the delete */
[d54838d]622void owl_function_deletecur(int move_after)
623{
[7d4fbcd]624  int curmsg;
625  owl_view *v;
626
627  v=owl_global_get_current_view(&g);
628
629  /* bail if there's no current message */
630  if (owl_view_get_size(v) < 1) {
[ec6ff52]631    owl_function_error("No current message to delete");
[7d4fbcd]632    return;
633  }
634
635  /* mark the message for deletion */
636  curmsg=owl_global_get_curmsg(&g);
637  owl_view_delete_element(v, curmsg);
638
[b950088]639  if (move_after) {
640    /* move the poiner in the appropriate direction
641     * to the next undeleted msg */
642    if (owl_global_get_direction(&g)==OWL_DIRECTION_UPWARDS) {
643      owl_function_prevmsg_notdeleted();
644    } else {
645      owl_function_nextmsg_notdeleted();
646    }
[7d4fbcd]647  }
648}
649
[d54838d]650void owl_function_undeletecur(int move_after)
651{
[7d4fbcd]652  int curmsg;
653  owl_view *v;
654
655  v=owl_global_get_current_view(&g);
656 
657  if (owl_view_get_size(v) < 1) {
[ec6ff52]658    owl_function_error("No current message to undelete");
[7d4fbcd]659    return;
660  }
661  curmsg=owl_global_get_curmsg(&g);
662
663  owl_view_undelete_element(v, curmsg);
664
[b950088]665  if (move_after) {
666    if (owl_global_get_direction(&g)==OWL_DIRECTION_UPWARDS) {
667      if (curmsg>0) {
668        owl_function_prevmsg();
669      } else {
670        owl_function_nextmsg();
671      }
[7d4fbcd]672    } else {
[b950088]673      owl_function_nextmsg();
[7d4fbcd]674    }
675  }
676
677  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
678}
679
[d54838d]680void owl_function_expunge()
681{
[7d4fbcd]682  int curmsg;
683  owl_message *m;
684  owl_messagelist *ml;
685  owl_view *v;
[486688f]686  int lastmsgid=0;
[7d4fbcd]687
688  curmsg=owl_global_get_curmsg(&g);
689  v=owl_global_get_current_view(&g);
690  ml=owl_global_get_msglist(&g);
691
692  m=owl_view_get_element(v, curmsg);
[486688f]693  if (m) lastmsgid = owl_message_get_id(m);
[7d4fbcd]694
695  /* expunge the message list */
696  owl_messagelist_expunge(ml);
697
698  /* update all views (we only have one right now) */
699  owl_view_recalculate(v);
700
[486688f]701  /* find where the new position should be
702     (as close as possible to where we last where) */
703  curmsg = owl_view_get_nearest_to_msgid(v, lastmsgid);
704  if (curmsg>owl_view_get_size(v)-1) curmsg = owl_view_get_size(v)-1;
705  if (curmsg<0) curmsg = 0;
706  owl_global_set_curmsg(&g, curmsg);
707  owl_function_calculate_topmsg(OWL_DIRECTION_NONE);
[7d4fbcd]708  /* if there are no messages set the direction to down in case we
709     delete everything upwards */
710  owl_global_set_direction_downwards(&g);
711 
712  owl_function_makemsg("Messages expunged");
713  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
714}
715
[d54838d]716void owl_function_firstmsg()
717{
[7d4fbcd]718  owl_global_set_curmsg(&g, 0);
719  owl_global_set_topmsg(&g, 0);
720  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
721  owl_global_set_direction_downwards(&g);
722}
723
[d54838d]724void owl_function_lastmsg_noredisplay()
725{
[5eeea3b]726  int oldcurmsg, curmsg;
[7d4fbcd]727  owl_view *v;
728
729  v=owl_global_get_current_view(&g);
[5eeea3b]730  oldcurmsg=owl_global_get_curmsg(&g);
731  curmsg=owl_view_get_size(v)-1; 
[7d4fbcd]732  if (curmsg<0) curmsg=0;
733  owl_global_set_curmsg(&g, curmsg);
[5eeea3b]734  if (oldcurmsg < curmsg) {
735    owl_function_calculate_topmsg(OWL_DIRECTION_DOWNWARDS);
736  } else if (curmsg<owl_view_get_size(v)) {
737    /* If already at the end, blank the screen and move curmsg
738     * past the end of the messages. */
739    owl_global_set_topmsg(&g, curmsg+1);
740    owl_global_set_curmsg(&g, curmsg+1);
741  } 
[7d4fbcd]742  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
743  owl_global_set_direction_downwards(&g);
744}
745
[d54838d]746void owl_function_lastmsg()
747{
[7d4fbcd]748  owl_function_lastmsg_noredisplay();
749  owl_mainwin_redisplay(owl_global_get_mainwin(&g)); 
750}
751
[d54838d]752void owl_function_shift_right()
753{
[7d4fbcd]754  owl_global_set_rightshift(&g, owl_global_get_rightshift(&g)+10);
755  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
756  owl_global_set_needrefresh(&g);
757}
758
[d54838d]759void owl_function_shift_left()
760{
[7d4fbcd]761  int shift;
762
763  shift=owl_global_get_rightshift(&g);
764  if (shift>=10) {
765    owl_global_set_rightshift(&g, shift-10);
766    owl_mainwin_redisplay(owl_global_get_mainwin(&g));
767    owl_global_set_needrefresh(&g);
768  } else {
769    owl_function_beep();
[f51bc78]770    owl_function_makemsg("Already full left");
[7d4fbcd]771  }
772}
773
[d54838d]774void owl_function_unsuball()
775{
[7d4fbcd]776  unsuball();
777  owl_function_makemsg("Unsubscribed from all messages.");
778}
779
[d54838d]780void owl_function_loadsubs(char *file)
781{
[7d4fbcd]782  int ret;
[ecd5dc5]783
[7d4fbcd]784  ret=owl_zephyr_loadsubs(file);
[ecd5dc5]785
786  if (!owl_context_is_interactive(owl_global_get_context(&g))) return;
[7d4fbcd]787  if (ret==0) {
788    owl_function_makemsg("Subscribed to messages from file.");
789  } else if (ret==-1) {
[ec6ff52]790    owl_function_error("Could not open file.");
[7d4fbcd]791  } else {
[ec6ff52]792    owl_function_error("Error subscribing to messages from file.");
[7d4fbcd]793  }
794}
795
[d54838d]796void owl_function_loadloginsubs(char *file)
797{
[7933748]798  int ret;
[ecd5dc5]799
[7933748]800  ret=owl_zephyr_loadloginsubs(file);
[ecd5dc5]801
802  if (!owl_context_is_interactive(owl_global_get_context(&g))) return;
[7933748]803  if (ret==0) {
804    owl_function_makemsg("Subscribed to login messages from file.");
805  } else if (ret==-1) {
[ec6ff52]806    owl_function_error("Could not open file for login subscriptions.");
[7933748]807  } else {
[ec6ff52]808    owl_function_error("Error subscribing to login messages from file.");
[7933748]809  }
810}
811
[d54838d]812void owl_function_suspend()
813{
[7d4fbcd]814  endwin();
815  printf("\n");
816  kill(getpid(), SIGSTOP);
817
818  /* resize to reinitialize all the windows when we come back */
819  owl_command_resize();
820}
821
[d54838d]822void owl_function_zaway_toggle()
823{
[7d4fbcd]824  if (!owl_global_is_zaway(&g)) {
825    owl_global_set_zaway_msg(&g, owl_global_get_zaway_msg_default(&g));
826    owl_function_zaway_on();
827  } else {
828    owl_function_zaway_off();
829  }
830}
831
[d54838d]832void owl_function_zaway_on()
833{
[7d4fbcd]834  owl_global_set_zaway_on(&g);
[8c92848]835  owl_aim_set_awaymsg(owl_global_get_zaway_msg(&g));
[7d4fbcd]836  owl_function_makemsg("zaway set (%s)", owl_global_get_zaway_msg(&g));
837}
838
[d54838d]839void owl_function_zaway_off()
840{
[7d4fbcd]841  owl_global_set_zaway_off(&g);
842  owl_function_makemsg("zaway off");
843}
844
[d54838d]845void owl_function_quit()
846{
[7d4fbcd]847  char *ret;
848 
849  /* zlog out if we need to */
850  if (owl_global_is_shutdownlogout(&g)) {
[31e48a3]851    owl_zephyr_zlog_out();
[7d4fbcd]852  }
853
854  /* execute the commands in shutdown */
[f1e629d]855  ret = owl_perlconfig_execute("owl::shutdown();");
[7d4fbcd]856  if (ret) owl_free(ret);
857
[d09e5a1]858  /* signal our child process, if any */
859  if (owl_global_get_newmsgproc_pid(&g)) {
860    kill(owl_global_get_newmsgproc_pid(&g), SIGHUP);
861  }
862
[7d4fbcd]863  /* final clean up */
[09489b89]864  owl_zephyr_shutdown();
[7d4fbcd]865  endwin();
866  owl_function_debugmsg("Quitting Owl");
867  exit(0);
868}
869
[d54838d]870void owl_function_openurl()
871{
[7d4fbcd]872  /* visit the first url in the current message */
873  owl_message *m;
874  owl_view *v;
875  char *ptr1, *ptr2, *text, url[LINE], tmpbuff[LINE];
876  int webbrowser;
877
878  webbrowser = owl_global_get_webbrowser(&g);
879
880  if (webbrowser < 0 || webbrowser == OWL_WEBBROWSER_NONE) {
[ec6ff52]881    owl_function_error("No browser selected");
[7d4fbcd]882    return;
883  }
884
885  v=owl_global_get_current_view(&g);
886 
[5eeea3b]887  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
888
889  if (!m || owl_view_get_size(v)==0) {
[ec6ff52]890    owl_function_error("No current message selected");
[7d4fbcd]891    return;
892  }
893
894  text=owl_message_get_text(m);
895
896  /* First look for a good URL */ 
897  if ((ptr1=strstr(text, "http://"))!=NULL) {
898    ptr2=strpbrk(ptr1, " \n\t");
899    if (ptr2) {
900      strncpy(url, ptr1, ptr2-ptr1+1);
901      url[ptr2-ptr1+1]='\0';
902    } else {
903      strcpy(url, ptr1);
904    }
905
906    /* if we had <http strip a trailing > */
907    if (ptr1>text && ptr1[-1]=='<') {
908      if (url[strlen(url)-1]=='>') {
909        url[strlen(url)-1]='\0';
910      }
911    }
912  } else if ((ptr1=strstr(text, "https://"))!=NULL) {
913    /* Look for an https URL */ 
914    ptr2=strpbrk(ptr1, " \n\t");
915    if (ptr2) {
916      strncpy(url, ptr1, ptr2-ptr1+1);
917      url[ptr2-ptr1+1]='\0';
918    } else {
919      strcpy(url, ptr1);
920    }
921   
922    /* if we had <http strip a trailing > */
923    if (ptr1>text && ptr1[-1]=='<') {
924      if (url[strlen(url)-1]=='>') {
925        url[strlen(url)-1]='\0';
926      }
927    }
928  } else if ((ptr1=strstr(text, "www."))!=NULL) {
929    /* if we can't find a real url look for www.something */
930    ptr2=strpbrk(ptr1, " \n\t");
931    if (ptr2) {
932      strncpy(url, ptr1, ptr2-ptr1+1);
933      url[ptr2-ptr1+1]='\0';
934    } else {
935      strcpy(url, ptr1);
936    }
937  } else {
938    owl_function_beep();
[ec6ff52]939    owl_function_error("Could not find URL to open.");
[7d4fbcd]940    return;
941  }
942
943  /* Make sure there aren't any quotes or \'s in the url */
944  for (ptr1 = url; *ptr1; ptr1++) {
945    if (*ptr1 == '"' || *ptr1 == '\\') {
946      owl_function_beep();
[ec6ff52]947      owl_function_error("URL contains invalid characters.");
[7d4fbcd]948      return;
949    }
950  }
951 
952  /* NOTE: There are potentially serious security issues here... */
953
954  /* open the page */
955  owl_function_makemsg("Opening %s", url);
956  if (webbrowser == OWL_WEBBROWSER_NETSCAPE) {
957    snprintf(tmpbuff, LINE, "netscape -remote \"openURL(%s)\" > /dev/null 2> /dev/null", url);
958    system(tmpbuff); 
959  } else if (webbrowser == OWL_WEBBROWSER_GALEON) {
960    snprintf(tmpbuff, LINE, "galeon \"%s\" > /dev/null 2> /dev/null &", url);
961    system(tmpbuff); 
[ae9e6be]962  } else if (webbrowser == OWL_WEBBROWSER_OPERA) {
963    snprintf(tmpbuff, LINE, "opera \"%s\" > /dev/null 2> /dev/null &", url);
964    system(tmpbuff); 
[7d4fbcd]965  }
966}
967
[d54838d]968void owl_function_calculate_topmsg(int direction)
969{
[aa2f33b3]970  int recwinlines, topmsg, curmsg;
[7d4fbcd]971  owl_view *v;
972
973  v=owl_global_get_current_view(&g);
[aa2f33b3]974  curmsg=owl_global_get_curmsg(&g);
975  topmsg=owl_global_get_topmsg(&g);
[7d4fbcd]976  recwinlines=owl_global_get_recwin_lines(&g);
977
[f9c43ae]978  /*
[7d4fbcd]979  if (owl_view_get_size(v) < 1) {
980    return;
981  }
[f9c43ae]982  */
[aa2f33b3]983
984  switch (owl_global_get_scrollmode(&g)) {
985  case OWL_SCROLLMODE_TOP:
[f9c43ae]986    topmsg = owl_function_calculate_topmsg_top(direction, v, curmsg, topmsg, recwinlines);
[aa2f33b3]987    break;
988  case OWL_SCROLLMODE_NEARTOP:
[f9c43ae]989    topmsg = owl_function_calculate_topmsg_neartop(direction, v, curmsg, topmsg, recwinlines);
[aa2f33b3]990    break;
991  case OWL_SCROLLMODE_CENTER:
[f9c43ae]992    topmsg = owl_function_calculate_topmsg_center(direction, v, curmsg, topmsg, recwinlines);
[aa2f33b3]993    break;
994  case OWL_SCROLLMODE_PAGED:
[f9c43ae]995    topmsg = owl_function_calculate_topmsg_paged(direction, v, curmsg, topmsg, recwinlines, 0);
[aa2f33b3]996    break;
997  case OWL_SCROLLMODE_PAGEDCENTER:
[f9c43ae]998    topmsg = owl_function_calculate_topmsg_paged(direction, v, curmsg, topmsg, recwinlines, 1);
[aa2f33b3]999    break;
1000  case OWL_SCROLLMODE_NORMAL:
1001  default:
[f9c43ae]1002    topmsg = owl_function_calculate_topmsg_normal(direction, v, curmsg, topmsg, recwinlines);
[aa2f33b3]1003  }
[3a2daac]1004  owl_function_debugmsg("Calculated a topmsg of %i", topmsg);
[aa2f33b3]1005  owl_global_set_topmsg(&g, topmsg);
1006}
1007
1008/* Returns what the new topmsg should be. 
1009 * Passed the last direction of movement,
1010 * the current view,
1011 * the current message number in the view,
1012 * the top message currently being displayed,
1013 * and the number of lines in the recwin.
1014 */
[d54838d]1015int owl_function_calculate_topmsg_top(int direction, owl_view *v, int curmsg, int topmsg, int recwinlines)
1016{
[f9c43ae]1017  return(curmsg);
[aa2f33b3]1018}
1019
[d54838d]1020int owl_function_calculate_topmsg_neartop(int direction, owl_view *v, int curmsg, int topmsg, int recwinlines)
1021{
[aa2f33b3]1022  if (curmsg>0 
1023      && (owl_message_get_numlines(owl_view_get_element(v, curmsg-1))
1024          <  recwinlines/2)) {
[f9c43ae]1025    return(curmsg-1);
[aa2f33b3]1026  } else {
[f9c43ae]1027    return(curmsg);
[aa2f33b3]1028  }
1029}
1030 
[d54838d]1031int owl_function_calculate_topmsg_center(int direction, owl_view *v, int curmsg, int topmsg, int recwinlines)
1032{
[aa2f33b3]1033  int i, last, lines;
1034
1035  last = curmsg;
1036  lines = 0;
1037  for (i=curmsg-1; i>=0; i--) {
1038    lines += owl_message_get_numlines(owl_view_get_element(v, i));
1039    if (lines > recwinlines/2) break;
1040    last = i;
1041  }
[f9c43ae]1042  return(last);
[aa2f33b3]1043}
1044 
[d54838d]1045int owl_function_calculate_topmsg_paged(int direction, owl_view *v, int curmsg, int topmsg, int recwinlines, int center_on_page)
1046{
[aa2f33b3]1047  int i, last, lines, savey;
1048 
1049  /* If we're off the top of the screen, scroll up such that the
1050   * curmsg is near the botton of the screen. */
1051  if (curmsg < topmsg) {
1052    last = curmsg;
1053    lines = 0;
1054    for (i=curmsg; i>=0; i--) {
1055      lines += owl_message_get_numlines(owl_view_get_element(v, i));
1056      if (lines > recwinlines) break;
1057    last = i;
1058    }
1059    if (center_on_page) {
[f9c43ae]1060      return(owl_function_calculate_topmsg_center(direction, v, curmsg, 0, recwinlines));
[aa2f33b3]1061    } else {
[f9c43ae]1062      return(last);
[aa2f33b3]1063    }
1064  }
1065
1066  /* Find number of lines from top to bottom of curmsg (store in savey) */
1067  savey=0;
1068  for (i=topmsg; i<=curmsg; i++) {
1069    savey+=owl_message_get_numlines(owl_view_get_element(v, i));
1070  }
1071
1072  /* if we're off the bottom of the screen, scroll down */
1073  if (savey > recwinlines) {
1074    if (center_on_page) {
[f9c43ae]1075      return(owl_function_calculate_topmsg_center(direction, v, curmsg, 0, recwinlines));
[aa2f33b3]1076    } else {
[f9c43ae]1077      return(curmsg);
[aa2f33b3]1078    }
1079  }
1080
1081  /* else just stay as we are... */
[f9c43ae]1082  return(topmsg);
[aa2f33b3]1083}
1084
[d54838d]1085int owl_function_calculate_topmsg_normal(int direction, owl_view *v, int curmsg, int topmsg, int recwinlines)
1086{
[aa2f33b3]1087  int savey, j, i, foo, y;
[f9c43ae]1088
[88736cb]1089  if (curmsg<0) return(topmsg);
1090   
[f9c43ae]1091  /* If we're off the top of the screen then center */
1092  if (curmsg<topmsg) {
1093    topmsg=owl_function_calculate_topmsg_center(direction, v, curmsg, 0, recwinlines);
1094  }
1095
[7d4fbcd]1096  /* Find number of lines from top to bottom of curmsg (store in savey) */
1097  savey=0;
1098  for (i=topmsg; i<=curmsg; i++) {
1099    savey+=owl_message_get_numlines(owl_view_get_element(v, i));
1100  }
1101
[f9c43ae]1102  /* If we're off the bottom of the screen, set the topmsg to curmsg
1103   * and scroll upwards */
1104  if (savey > recwinlines) {
1105    topmsg=curmsg;
1106    savey=owl_message_get_numlines(owl_view_get_element(v, i));
1107    direction=OWL_DIRECTION_UPWARDS;
[7d4fbcd]1108  }
[f9c43ae]1109 
[7d4fbcd]1110  /* If our bottom line is less than 1/4 down the screen then scroll up */
1111  if (direction == OWL_DIRECTION_UPWARDS || direction == OWL_DIRECTION_NONE) {
1112    if (savey < (recwinlines / 4)) {
1113      y=0;
1114      for (j=curmsg; j>=0; j--) {
1115        foo=owl_message_get_numlines(owl_view_get_element(v, j));
1116        /* will we run the curmsg off the screen? */
1117        if ((foo+y) >= recwinlines) {
1118          j++;
1119          if (j>curmsg) j=curmsg;
1120          break;
1121        }
1122        /* have saved 1/2 the screen space? */
1123        y+=foo;
1124        if (y > (recwinlines / 2)) break;
1125      }
1126      if (j<0) j=0;
[f9c43ae]1127      return(j);
[7d4fbcd]1128    }
1129  }
1130
1131  if (direction == OWL_DIRECTION_DOWNWARDS || direction == OWL_DIRECTION_NONE) {
1132    /* If curmsg bottom line is more than 3/4 down the screen then scroll down */
1133    if (savey > ((recwinlines * 3)/4)) {
1134      y=0;
1135      /* count lines from the top until we can save 1/2 the screen size */
1136      for (j=topmsg; j<curmsg; j++) {
1137        y+=owl_message_get_numlines(owl_view_get_element(v, j));
1138        if (y > (recwinlines / 2)) break;
1139      }
1140      if (j==curmsg) {
1141        j--;
1142      }
[f9c43ae]1143      return(j+1);
[7d4fbcd]1144    }
1145  }
[aa2f33b3]1146
[f9c43ae]1147  return(topmsg);
[7d4fbcd]1148}
1149
[d54838d]1150void owl_function_resize()
1151{
[7d4fbcd]1152  owl_global_set_resize_pending(&g);
1153}
1154
[d54838d]1155void owl_function_run_buffercommand()
1156{
[453bd70]1157  char *buff, *ptr;
1158
[7d4fbcd]1159  buff=owl_global_get_buffercommand(&g);
1160  if (!strncmp(buff, "zwrite ", 7)) {
[ce7db4d]1161    owl_function_zwrite(buff, owl_editwin_get_text(owl_global_get_typwin(&g)));
[9ceee9d]1162  } else if (!strncmp(buff, "zcrypt ", 7)) {
1163    owl_function_zcrypt(buff, owl_editwin_get_text(owl_global_get_typwin(&g)));
[d09e5a1]1164  } else if (!strncmp(buff, "aimwrite ", 9)) {
1165    owl_function_aimwrite(buff+9);
[37eab7f]1166  } else if (!strncmp(buff, "loopwrite", 9) || !strncmp(buff, "loopwrite ", 10)) {
1167    owl_function_loopwrite();
[453bd70]1168  } else if (!strncmp(buff, "aimlogin ", 9)) {
1169    ptr=owl_sprintf("%s %s", buff, owl_global_get_response(&g));
1170    owl_function_command(ptr);
1171    owl_free(ptr);
[37eab7f]1172  } else {
1173    owl_function_error("Internal error: invalid buffercommand %s", buff);
[7d4fbcd]1174  }
1175}
1176
[d54838d]1177void owl_function_debugmsg(char *fmt, ...)
1178{
[7d4fbcd]1179  FILE *file;
1180  time_t now;
1181  char buff1[LINE], buff2[LINE];
1182  va_list ap;
1183  va_start(ap, fmt);
1184
1185  if (!owl_global_is_debug_fast(&g)) return;
1186
1187  file=fopen(owl_global_get_debug_file(&g), "a");
1188  if (!file) return;
1189
1190  now=time(NULL);
1191  strcpy(buff1, ctime(&now));
1192  buff1[strlen(buff1)-1]='\0';
1193
1194  owl_global_get_runtime_string(&g, buff2);
1195 
1196  fprintf(file, "[%i -  %s - %s]: ", (int) getpid(), buff1, buff2);
1197  vfprintf(file, fmt, ap);
1198  fprintf(file, "\n");
1199  fclose(file);
1200
1201  va_end(ap);
1202}
1203
[d54838d]1204void owl_function_refresh()
1205{
[7d4fbcd]1206  owl_function_resize();
1207}
1208
[d54838d]1209void owl_function_beep()
1210{
[7d4fbcd]1211  if (owl_global_is_bell(&g)) {
1212    beep();
[b45293f]1213    owl_global_set_needrefresh(&g); /* do we really need this? */
[7d4fbcd]1214  }
1215}
1216
[d54838d]1217void owl_function_subscribe(char *class, char *inst, char *recip)
1218{
[7d4fbcd]1219  int ret;
1220
1221  ret=owl_zephyr_sub(class, inst, recip);
1222  if (ret) {
[ec6ff52]1223    owl_function_error("Error subscribing.");
[7d4fbcd]1224  } else {
1225    owl_function_makemsg("Subscribed.");
1226  }
1227}
1228
[d54838d]1229void owl_function_unsubscribe(char *class, char *inst, char *recip)
1230{
[7d4fbcd]1231  int ret;
1232
1233  ret=owl_zephyr_unsub(class, inst, recip);
1234  if (ret) {
[ec6ff52]1235    owl_function_error("Error subscribing.");
[7d4fbcd]1236  } else {
1237    owl_function_makemsg("Unsubscribed.");
1238  }
1239}
1240
[d54838d]1241void owl_function_set_cursor(WINDOW *win)
1242{
[7d4fbcd]1243  wnoutrefresh(win);
1244}
1245
[d54838d]1246void owl_function_full_redisplay()
1247{
[7d4fbcd]1248  redrawwin(owl_global_get_curs_recwin(&g));
1249  redrawwin(owl_global_get_curs_sepwin(&g));
1250  redrawwin(owl_global_get_curs_typwin(&g));
1251  redrawwin(owl_global_get_curs_msgwin(&g));
1252
1253  wnoutrefresh(owl_global_get_curs_recwin(&g));
1254  wnoutrefresh(owl_global_get_curs_sepwin(&g));
1255  wnoutrefresh(owl_global_get_curs_typwin(&g));
1256  wnoutrefresh(owl_global_get_curs_msgwin(&g));
1257 
1258  sepbar("");
1259  owl_function_makemsg("");
1260
1261  owl_global_set_needrefresh(&g);
1262}
1263
[d54838d]1264void owl_function_popless_text(char *text)
1265{
[7d4fbcd]1266  owl_popwin *pw;
1267  owl_viewwin *v;
1268
1269  pw=owl_global_get_popwin(&g);
1270  v=owl_global_get_viewwin(&g);
1271
1272  owl_popwin_up(pw);
1273  owl_viewwin_init_text(v, owl_popwin_get_curswin(pw),
1274                        owl_popwin_get_lines(pw), owl_popwin_get_cols(pw),
1275                        text);
1276  owl_popwin_refresh(pw);
1277  owl_viewwin_redisplay(v, 0);
1278  owl_global_set_needrefresh(&g);
1279}
1280
[d54838d]1281void owl_function_popless_fmtext(owl_fmtext *fm)
1282{
[7d4fbcd]1283  owl_popwin *pw;
1284  owl_viewwin *v;
1285
1286  pw=owl_global_get_popwin(&g);
1287  v=owl_global_get_viewwin(&g);
1288
1289  owl_popwin_up(pw);
1290  owl_viewwin_init_fmtext(v, owl_popwin_get_curswin(pw),
1291                   owl_popwin_get_lines(pw), owl_popwin_get_cols(pw),
1292                   fm);
1293  owl_popwin_refresh(pw);
1294  owl_viewwin_redisplay(v, 0);
1295  owl_global_set_needrefresh(&g);
[f17bff98]1296}
1297
1298void owl_function_popless_file(char *filename)
1299{
1300  owl_fmtext fm;
1301  FILE *file;
1302  char buff[1024];
1303
1304  file=fopen(filename, "r");
1305  if (!file) {
1306    owl_function_error("Could not open file: %s", filename);
1307    return;
1308  }
1309
1310  owl_fmtext_init_null(&fm);
1311  while (fgets(buff, 1024, file)) {
1312    owl_fmtext_append_normal(&fm, buff);
1313    owl_fmtext_append_normal(&fm, "\n");
1314  }
1315
1316  owl_function_popless_fmtext(&fm);
1317  owl_fmtext_free(&fm);
1318  fclose(file);
[7d4fbcd]1319}
1320
[d54838d]1321void owl_function_about()
1322{
[7d4fbcd]1323  char buff[5000];
1324
1325  sprintf(buff, "This is owl version %s\n", OWL_VERSION_STRING);
1326  strcat(buff, "\nOwl was written by James Kretchmar at the Massachusetts\n");
1327  strcat(buff, "Institute of Technology.  The first version, 0.5, was\n");
[bde7714]1328  strcat(buff, "released in March 2002.\n");
[7d4fbcd]1329  strcat(buff, "\n");
1330  strcat(buff, "The name 'owl' was chosen in reference to the owls in the\n");
1331  strcat(buff, "Harry Potter novels, who are tasked with carrying messages\n");
1332  strcat(buff, "between Witches and Wizards.\n");
1333  strcat(buff, "\n");
1334  strcat(buff, "Copyright 2002 Massachusetts Institute of Technology\n");
1335  strcat(buff, "\n");
1336  strcat(buff, "Permission to use, copy, modify, and distribute this\n");
1337  strcat(buff, "software and its documentation for any purpose and without\n");
1338  strcat(buff, "fee is hereby granted, provided that the above copyright\n");
1339  strcat(buff, "notice and this permission notice appear in all copies\n");
1340  strcat(buff, "and in supporting documentation.  No representation is\n");
1341  strcat(buff, "made about the suitability of this software for any\n");
1342  strcat(buff, "purpose.  It is provided \"as is\" without express\n");
1343  strcat(buff, "or implied warranty.\n");
1344  owl_function_popless_text(buff);
1345}
1346
[d54838d]1347void owl_function_info()
1348{
[7d4fbcd]1349  owl_message *m;
[5789230]1350  owl_fmtext fm, attrfm;
[09489b89]1351  char buff[10000];
[7d4fbcd]1352  owl_view *v;
[09489b89]1353#ifdef HAVE_LIBZEPHYR
1354  ZNotice_t *n;
1355#endif
[7d4fbcd]1356
[d0d65df]1357  owl_fmtext_init_null(&fm);
1358 
[7d4fbcd]1359  v=owl_global_get_current_view(&g);
[5eeea3b]1360  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
1361  if (!m || owl_view_get_size(v)==0) {
[ec6ff52]1362    owl_function_error("No message selected\n");
[7d4fbcd]1363    return;
1364  }
1365
[5789230]1366  owl_fmtext_append_bold(&fm, "General Information:\n");
1367  owl_fmtext_append_normal(&fm, "  Msg Id    : ");
[d0d65df]1368  sprintf(buff, "%i", owl_message_get_id(m));
1369  owl_fmtext_append_normal(&fm, buff);
1370  owl_fmtext_append_normal(&fm, "\n");
[df0d93a]1371
[5789230]1372  owl_fmtext_append_normal(&fm, "  Type      : ");
[37eab7f]1373  owl_fmtext_append_bold(&fm, owl_message_get_type(m));
[df0d93a]1374  owl_fmtext_append_normal(&fm, "\n");
1375
[4b464a4]1376  if (owl_message_is_direction_in(m)) {
[5789230]1377    owl_fmtext_append_normal(&fm, "  Direction : in\n");
[4b464a4]1378  } else if (owl_message_is_direction_out(m)) {
[5789230]1379    owl_fmtext_append_normal(&fm, "  Direction : out\n");
[4b464a4]1380  } else if (owl_message_is_direction_none(m)) {
[5789230]1381    owl_fmtext_append_normal(&fm, "  Direction : none\n");
[4b464a4]1382  } else {
[5789230]1383    owl_fmtext_append_normal(&fm, "  Direction : unknown\n");
[4b464a4]1384  }
[df0d93a]1385
[5789230]1386  owl_fmtext_append_normal(&fm, "  Time      : ");
[d0d65df]1387  owl_fmtext_append_normal(&fm, owl_message_get_timestr(m));
1388  owl_fmtext_append_normal(&fm, "\n");
[4b464a4]1389
[df0d93a]1390  if (!owl_message_is_type_admin(m)) {
[5789230]1391    owl_fmtext_append_normal(&fm, "  Sender    : ");
[df0d93a]1392    owl_fmtext_append_normal(&fm, owl_message_get_sender(m));
1393    owl_fmtext_append_normal(&fm, "\n");
1394   
[5789230]1395    owl_fmtext_append_normal(&fm, "  Recipient : ");
[df0d93a]1396    owl_fmtext_append_normal(&fm, owl_message_get_recipient(m));
1397    owl_fmtext_append_normal(&fm, "\n");
1398  }
1399   
[0ff8fb57]1400  if (owl_message_is_type_zephyr(m)) {
[5789230]1401    owl_fmtext_append_bold(&fm, "\nZephyr Specific Information:\n");
[0ff8fb57]1402   
[5789230]1403    owl_fmtext_append_normal(&fm, "  Class     : ");
[df0d93a]1404    owl_fmtext_append_normal(&fm, owl_message_get_class(m));
1405    owl_fmtext_append_normal(&fm, "\n");
[5789230]1406    owl_fmtext_append_normal(&fm, "  Instance  : ");
[df0d93a]1407    owl_fmtext_append_normal(&fm, owl_message_get_instance(m));
1408    owl_fmtext_append_normal(&fm, "\n");
[5789230]1409    owl_fmtext_append_normal(&fm, "  Opcode    : ");
[df0d93a]1410    owl_fmtext_append_normal(&fm, owl_message_get_opcode(m));
1411    owl_fmtext_append_normal(&fm, "\n");
1412   
[5789230]1413    owl_fmtext_append_normal(&fm, "  Time      : ");
[df0d93a]1414    owl_fmtext_append_normal(&fm, owl_message_get_timestr(m));
1415    owl_fmtext_append_normal(&fm, "\n");
[09489b89]1416#ifdef HAVE_LIBZEPHYR
[d0d65df]1417    if (owl_message_is_direction_in(m)) {
[09489b89]1418      char *ptr, tmpbuff[1024];
1419      int i, j, fields, len;
1420
[d0d65df]1421      n=owl_message_get_notice(m);
[df0d93a]1422
[5789230]1423      owl_fmtext_append_normal(&fm, "  Kind      : ");
[d0d65df]1424      if (n->z_kind==UNSAFE) {
1425        owl_fmtext_append_normal(&fm, "UNSAFE\n");
1426      } else if (n->z_kind==UNACKED) {
1427        owl_fmtext_append_normal(&fm, "UNACKED\n");
1428      } else if (n->z_kind==ACKED) {
1429        owl_fmtext_append_normal(&fm, "ACKED\n");
1430      } else if (n->z_kind==HMACK) {
1431        owl_fmtext_append_normal(&fm, "HMACK\n");
1432      } else if (n->z_kind==HMCTL) {
1433        owl_fmtext_append_normal(&fm, "HMCTL\n");
1434      } else if (n->z_kind==SERVACK) {
1435        owl_fmtext_append_normal(&fm, "SERVACK\n");
1436      } else if (n->z_kind==SERVNAK) {
1437        owl_fmtext_append_normal(&fm, "SERVNACK\n");
1438      } else if (n->z_kind==CLIENTACK) {
1439        owl_fmtext_append_normal(&fm, "CLIENTACK\n");
1440      } else if (n->z_kind==STAT) {
1441        owl_fmtext_append_normal(&fm, "STAT\n");
1442      } else {
1443        owl_fmtext_append_normal(&fm, "ILLEGAL VALUE\n");
1444      }
[5789230]1445      owl_fmtext_append_normal(&fm, "  Host      : ");
[d0d65df]1446      owl_fmtext_append_normal(&fm, owl_message_get_hostname(m));
1447      owl_fmtext_append_normal(&fm, "\n");
[5789230]1448      sprintf(buff, "  Port      : %i\n", n->z_port);
[d0d65df]1449      owl_fmtext_append_normal(&fm, buff);
1450
[5789230]1451      owl_fmtext_append_normal(&fm,    "  Auth      : ");
[9381782]1452      owl_fmtext_append_normal(&fm, owl_zephyr_get_authstr(n));
1453      owl_fmtext_append_normal(&fm, "\n");
[7d4fbcd]1454
[5789230]1455      /* fix this */
1456      sprintf(buff, "  Checkd Ath: %i\n", n->z_checked_auth);
1457      sprintf(buff, "%s  Multi notc: %s\n", buff, n->z_multinotice);
1458      sprintf(buff, "%s  Num other : %i\n", buff, n->z_num_other_fields);
1459      sprintf(buff, "%s  Msg Len   : %i\n", buff, n->z_message_len);
[d0d65df]1460      owl_fmtext_append_normal(&fm, buff);
1461     
[5789230]1462      sprintf(buff, "  Fields    : %i\n", owl_zephyr_get_num_fields(n));
[d0d65df]1463      owl_fmtext_append_normal(&fm, buff);
1464     
1465      fields=owl_zephyr_get_num_fields(n);
1466      for (i=0; i<fields; i++) {
[5789230]1467        sprintf(buff, "  Field %i   : ", i+1);
[d0d65df]1468       
1469        ptr=owl_zephyr_get_field(n, i+1, &len);
1470        if (!ptr) break;
1471        if (len<30) {
1472          strncpy(tmpbuff, ptr, len);
1473          tmpbuff[len]='\0';
1474        } else {
1475          strncpy(tmpbuff, ptr, 30);
1476          tmpbuff[30]='\0';
1477          strcat(tmpbuff, "...");
1478        }
1479       
1480        for (j=0; j<strlen(tmpbuff); j++) {
1481          if (tmpbuff[j]=='\n') tmpbuff[j]='~';
1482          if (tmpbuff[j]=='\r') tmpbuff[j]='!';
1483        }
1484       
1485        strcat(buff, tmpbuff);
1486        strcat(buff, "\n");
1487        owl_fmtext_append_normal(&fm, buff);
1488      }
[5789230]1489      owl_fmtext_append_normal(&fm, "  Default Fm:");
[d0d65df]1490      owl_fmtext_append_normal(&fm, n->z_default_format);
[7d4fbcd]1491    }
[09489b89]1492#endif   
[7d4fbcd]1493  }
[0ff8fb57]1494
1495  if (owl_message_is_type_aim(m)) {
[5789230]1496    owl_fmtext_append_bold(&fm, "\nAIM Specific Information:\n");
[0ff8fb57]1497  }
[5789230]1498
1499  owl_fmtext_append_bold(&fm, "\nOwl Message Attributes:\n");
1500  owl_message_attributes_tofmtext(m, &attrfm);
1501  owl_fmtext_append_fmtext(&fm, &attrfm);
[d0d65df]1502 
1503  owl_function_popless_fmtext(&fm);
[5789230]1504  owl_fmtext_free(&fm);
1505  owl_fmtext_free(&attrfm);
[7d4fbcd]1506}
1507
[5639bf2]1508/* print the current message in a popup window.
1509 * Use the 'default' style regardless of whatever
1510 * style the user may be using
1511 */
[d54838d]1512void owl_function_curmsg_to_popwin()
1513{
[7d4fbcd]1514  owl_popwin *pw;
1515  owl_view *v;
1516  owl_message *m;
[5639bf2]1517  owl_style *s;
1518  owl_fmtext fm;
[7d4fbcd]1519
[5639bf2]1520  v=owl_global_get_current_view(&g);
1521  s=owl_global_get_style_by_name(&g, "default");
[7d4fbcd]1522  pw=owl_global_get_popwin(&g);
1523
[5eeea3b]1524  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
1525
1526  if (!m || owl_view_get_size(v)==0) {
[ec6ff52]1527    owl_function_error("No current message");
[7d4fbcd]1528    return;
1529  }
1530
[5639bf2]1531  owl_fmtext_init_null(&fm);
1532  owl_style_get_formattext(s, &fm, m);
1533
1534  owl_function_popless_fmtext(&fm);
1535  owl_fmtext_free(&fm);
[7d4fbcd]1536}
1537
[d54838d]1538void owl_function_page_curmsg(int step)
1539{
[7d4fbcd]1540  /* scroll down or up within the current message IF the message is truncated */
1541
1542  int offset, curmsg, lines;
1543  owl_view *v;
1544  owl_message *m;
1545
1546  offset=owl_global_get_curmsg_vert_offset(&g);
1547  v=owl_global_get_current_view(&g);
1548  curmsg=owl_global_get_curmsg(&g);
1549  m=owl_view_get_element(v, curmsg);
[5eeea3b]1550  if (!m || owl_view_get_size(v)==0) return;
[7d4fbcd]1551  lines=owl_message_get_numlines(m);
1552
1553  if (offset==0) {
1554    /* Bail if the curmsg isn't the last one displayed */
1555    if (curmsg != owl_mainwin_get_last_msg(owl_global_get_mainwin(&g))) {
[f51bc78]1556      owl_function_makemsg("The entire message is already displayed");
[7d4fbcd]1557      return;
1558    }
1559   
1560    /* Bail if we're not truncated */
1561    if (!owl_mainwin_is_curmsg_truncated(owl_global_get_mainwin(&g))) {
[f51bc78]1562      owl_function_makemsg("The entire message is already displayed");
[7d4fbcd]1563      return;
1564    }
1565  }
1566 
1567 
1568  /* don't scroll past the last line */
1569  if (step>0) {
1570    if (offset+step > lines-1) {
1571      owl_global_set_curmsg_vert_offset(&g, lines-1);
1572    } else {
1573      owl_global_set_curmsg_vert_offset(&g, offset+step);
1574    }
1575  }
1576
1577  /* would we be before the beginning of the message? */
1578  if (step<0) {
1579    if (offset+step<0) {
1580      owl_global_set_curmsg_vert_offset(&g, 0);
1581    } else {
1582      owl_global_set_curmsg_vert_offset(&g, offset+step);
1583    }
1584  }
1585 
1586  /* redisplay */
1587  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
1588  owl_global_set_needrefresh(&g);
1589}
1590
[d54838d]1591void owl_function_resize_typwin(int newsize)
1592{
[7d4fbcd]1593  owl_global_set_typwin_lines(&g, newsize);
1594  owl_function_resize();
1595}
1596
[d54838d]1597void owl_function_typwin_grow()
1598{
[7d4fbcd]1599  int i;
1600
1601  i=owl_global_get_typwin_lines(&g);
1602  owl_function_resize_typwin(i+1);
1603}
1604
[d54838d]1605void owl_function_typwin_shrink()
1606{
[7d4fbcd]1607  int i;
1608
1609  i=owl_global_get_typwin_lines(&g);
1610  if (i>2) {
1611    owl_function_resize_typwin(i-1);
1612  }
1613}
1614
[d54838d]1615void owl_function_mainwin_pagedown()
1616{
[7d4fbcd]1617  int i;
1618
1619  i=owl_mainwin_get_last_msg(owl_global_get_mainwin(&g));
1620  if (i<0) return;
[f2e36b5]1621  if (owl_mainwin_is_last_msg_truncated(owl_global_get_mainwin(&g))
1622      && (owl_global_get_curmsg(&g) < i)
1623      && (i>0)) {
1624    i--;
1625  }
[7d4fbcd]1626  owl_global_set_curmsg(&g, i);
1627  owl_function_nextmsg();
1628}
1629
[d54838d]1630void owl_function_mainwin_pageup()
1631{
[7d4fbcd]1632  owl_global_set_curmsg(&g, owl_global_get_topmsg(&g));
1633  owl_function_prevmsg();
1634}
1635
[d54838d]1636void owl_function_getsubs()
1637{
[09489b89]1638  char *buff;
[7d4fbcd]1639
[09489b89]1640  buff=owl_zephyr_getsubs();
[7d4fbcd]1641
[09489b89]1642  if (buff) {
1643    owl_function_popless_text(buff);
1644  } else {
1645    owl_function_popless_text("Error getting subscriptions");
[7d4fbcd]1646  }
[09489b89]1647           
[1c6c4d3]1648  owl_free(buff);
[7d4fbcd]1649}
1650
1651#define PABUFLEN 5000
[d54838d]1652void owl_function_printallvars()
1653{
[7d4fbcd]1654  char buff[PABUFLEN], *pos, *name;
1655  owl_list varnames;
1656  int i, numvarnames, rem;
1657
1658  pos = buff;
1659  pos += sprintf(pos, "%-20s = %s\n", "VARIABLE", "VALUE");
1660  pos += sprintf(pos, "%-20s   %s\n",  "--------", "-----");
1661  owl_variable_dict_get_names(owl_global_get_vardict(&g), &varnames);
1662  rem = (buff+PABUFLEN)-pos-1;
1663  numvarnames = owl_list_get_size(&varnames);
1664  for (i=0; i<numvarnames; i++) {
1665    name = owl_list_get_element(&varnames, i);
1666    if (name && name[0]!='_') {
1667      rem = (buff+PABUFLEN)-pos-1;   
1668      pos += snprintf(pos, rem, "\n%-20s = ", name);
1669      rem = (buff+PABUFLEN)-pos-1;   
1670      owl_variable_get_tostring(owl_global_get_vardict(&g), name, pos, rem);
1671      pos = buff+strlen(buff);
1672    }
1673  }
1674  rem = (buff+PABUFLEN)-pos-1;   
1675  snprintf(pos, rem, "\n");
1676  owl_variable_dict_namelist_free(&varnames);
1677 
1678  owl_function_popless_text(buff);
1679}
1680
[d54838d]1681void owl_function_show_variables()
1682{
[7d4fbcd]1683  owl_list varnames;
1684  owl_fmtext fm; 
1685  int i, numvarnames;
1686  char *varname;
1687
1688  owl_fmtext_init_null(&fm);
1689  owl_fmtext_append_bold(&fm, 
1690      "Variables: (use 'show variable <name>' for details)\n");
1691  owl_variable_dict_get_names(owl_global_get_vardict(&g), &varnames);
1692  numvarnames = owl_list_get_size(&varnames);
1693  for (i=0; i<numvarnames; i++) {
1694    varname = owl_list_get_element(&varnames, i);
1695    if (varname && varname[0]!='_') {
[aa2f33b3]1696      owl_variable_describe(owl_global_get_vardict(&g), varname, &fm);
[7d4fbcd]1697    }
1698  }
1699  owl_variable_dict_namelist_free(&varnames);
1700  owl_function_popless_fmtext(&fm);
1701  owl_fmtext_free(&fm);
1702}
1703
[d54838d]1704void owl_function_show_variable(char *name)
1705{
[7d4fbcd]1706  owl_fmtext fm; 
1707
1708  owl_fmtext_init_null(&fm);
1709  owl_variable_get_help(owl_global_get_vardict(&g), name, &fm);
1710  owl_function_popless_fmtext(&fm);
1711  owl_fmtext_free(&fm); 
1712}
1713
1714/* note: this applies to global message list, not to view.
1715 * If flag is 1, deletes.  If flag is 0, undeletes. */
[d54838d]1716void owl_function_delete_by_id(int id, int flag)
1717{
[7d4fbcd]1718  owl_messagelist *ml;
1719  owl_message *m;
1720  ml = owl_global_get_msglist(&g);
1721  m = owl_messagelist_get_by_id(ml, id);
1722  if (m) {
1723    if (flag == 1) {
1724      owl_message_mark_delete(m);
1725    } else if (flag == 0) {
1726      owl_message_unmark_delete(m);
1727    }
1728    owl_mainwin_redisplay(owl_global_get_mainwin(&g));
1729    owl_global_set_needrefresh(&g);
1730  } else {
[ec6ff52]1731    owl_function_error("No message with id %d: unable to mark for (un)delete",id);
[7d4fbcd]1732  }
1733}
1734
[d54838d]1735void owl_function_delete_automsgs()
1736{
[7d4fbcd]1737  /* mark for deletion all messages in the current view that match the
1738   * 'trash' filter */
1739
1740  int i, j, count;
1741  owl_message *m;
1742  owl_view *v;
1743  owl_filter *f;
1744
1745  /* get the trash filter */
1746  f=owl_global_get_filter(&g, "trash");
1747  if (!f) {
[ec6ff52]1748    owl_function_error("No trash filter defined");
[7d4fbcd]1749    return;
1750  }
1751
1752  v=owl_global_get_current_view(&g);
1753
1754  count=0;
1755  j=owl_view_get_size(v);
1756  for (i=0; i<j; i++) {
1757    m=owl_view_get_element(v, i);
1758    if (owl_filter_message_match(f, m)) {
1759      count++;
1760      owl_message_mark_delete(m);
1761    }
1762  }
1763  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
[1c6c4d3]1764  owl_function_makemsg("%i messages marked for deletion", count);
[7d4fbcd]1765  owl_global_set_needrefresh(&g);
1766}
1767
[d54838d]1768void owl_function_status()
1769{
[7d4fbcd]1770  char buff[5000];
1771  time_t start;
1772  int up, days, hours, minutes;
[a352335c]1773  owl_fmtext fm;
1774
1775  owl_fmtext_init_null(&fm);
[7d4fbcd]1776
1777  start=owl_global_get_starttime(&g);
1778
[d9b0b972]1779  owl_fmtext_append_normal(&fm, "General Information:\n");
1780
1781  owl_fmtext_append_normal(&fm, "  Version: ");
[a352335c]1782  owl_fmtext_append_normal(&fm, OWL_VERSION_STRING);
1783  owl_fmtext_append_normal(&fm, "\n");
1784
[d9b0b972]1785  owl_fmtext_append_normal(&fm, "  Startup Arugments: ");
[a352335c]1786  owl_fmtext_append_normal(&fm, owl_global_get_startupargs(&g));
1787  owl_fmtext_append_normal(&fm, "\n");
[d9b0b972]1788  sprintf(buff, "  Startup Time: %s", ctime(&start));
[a352335c]1789  owl_fmtext_append_normal(&fm, buff);
[7d4fbcd]1790
1791  up=owl_global_get_runtime(&g);
1792  days=up/86400;
1793  up-=days*86400;
1794  hours=up/3600;
1795  up-=hours*3600;
1796  minutes=up/60;
1797  up-=minutes*60;
[d9b0b972]1798  sprintf(buff, "  Run Time: %i days %2.2i:%2.2i:%2.2i\n", days, hours, minutes, up);
[a352335c]1799  owl_fmtext_append_normal(&fm, buff);
[7d4fbcd]1800
[d9b0b972]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");
[7d4fbcd]1805  } else {
[d9b0b972]1806    owl_fmtext_append_normal(&fm, "no\n");
[7d4fbcd]1807  }
[d9b0b972]1808  owl_fmtext_append_normal(&fm, "  AIM included       : yes\n");
1809  owl_fmtext_append_normal(&fm, "  Loopback included  : yes\n");
1810
[8262340]1811
[d9b0b972]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, "\nMemory Usage:\n");
1822  owl_fmtext_append_normal(&fm, "  Not currently available.\n");
[a352335c]1823  /*
[8262340]1824  sprintf(buff, "%sMemory Malloced: %i\n", buff, owl_global_get_malloced(&g));
1825  sprintf(buff, "%sMemory Freed: %i\n", buff, owl_global_get_freed(&g));
1826  sprintf(buff, "%sMemory In Use: %i\n", buff, owl_global_get_meminuse(&g));
[a352335c]1827  */
[8262340]1828
[d9b0b972]1829  owl_fmtext_append_normal(&fm, "\nAIM Status:\n");
1830  owl_fmtext_append_normal(&fm, "  Logged in: ");
[a352335c]1831  if (owl_global_is_aimloggedin(&g)) {
1832    owl_fmtext_append_normal(&fm, owl_global_get_aim_screenname(&g));
1833    owl_fmtext_append_normal(&fm, "\n");
1834  } else {
[d9b0b972]1835    owl_fmtext_append_normal(&fm, "(not logged in)\n");
[a352335c]1836  }
[d9b0b972]1837
1838  owl_fmtext_append_normal(&fm, "  Processing events: ");
[a352335c]1839  if (owl_global_is_doaimevents(&g)) {
[d9b0b972]1840    owl_fmtext_append_normal(&fm, "yes\n");
[a352335c]1841  } else {
[d9b0b972]1842    owl_fmtext_append_normal(&fm, "no\n");
[a352335c]1843  }
1844
1845  owl_function_popless_fmtext(&fm);
1846  owl_fmtext_free(&fm);
[7d4fbcd]1847}
1848
[d54838d]1849void owl_function_show_term()
1850{
[7d4fbcd]1851  owl_fmtext fm;
1852  char buff[LINE];
1853
1854  owl_fmtext_init_null(&fm);
1855  sprintf(buff, "Terminal Lines: %i\nTerminal Columns: %i\n",
1856          owl_global_get_lines(&g),
1857          owl_global_get_cols(&g));
1858  owl_fmtext_append_normal(&fm, buff);
1859
1860  if (owl_global_get_hascolors(&g)) {
1861    owl_fmtext_append_normal(&fm, "Color: Yes\n");
1862    sprintf(buff, "Number of color pairs: %i\n", owl_global_get_colorpairs(&g));
1863    owl_fmtext_append_normal(&fm, buff);
1864    sprintf(buff, "Can change colors: %s\n", can_change_color() ? "yes" : "no");
1865    owl_fmtext_append_normal(&fm, buff);
1866  } else {
1867    owl_fmtext_append_normal(&fm, "Color: No\n");
1868  }
1869
1870  owl_function_popless_fmtext(&fm);
1871  owl_fmtext_free(&fm);
1872}
1873
[e7cc1c3]1874/* if type = 0 then normal reply.
1875 * if type = 1 then it's a reply to sender
1876 * if enter = 0 then allow the command to be edited
1877 * if enter = 1 then don't wait for editing
1878 */
[d54838d]1879void owl_function_reply(int type, int enter)
1880{
[e6449bc]1881  char *buff=NULL, *oldbuff;
[7d4fbcd]1882  owl_message *m;
1883  owl_filter *f;
1884 
1885  if (owl_view_get_size(owl_global_get_current_view(&g))==0) {
[ec6ff52]1886    owl_function_error("No message selected");
[7d4fbcd]1887  } else {
[e50cd56]1888    char *class, *inst, *to, *cc=NULL;
[7d4fbcd]1889   
1890    m=owl_view_get_element(owl_global_get_current_view(&g), owl_global_get_curmsg(&g));
[5eeea3b]1891    if (!m) {
[ec6ff52]1892      owl_function_error("No message selected");
[5eeea3b]1893      return;
1894    }
1895
[7d4fbcd]1896    /* first check if we catch the reply-lockout filter */
1897    f=owl_global_get_filter(&g, "reply-lockout");
1898    if (f) {
1899      if (owl_filter_message_match(f, m)) {
[ec6ff52]1900        owl_function_error("Sorry, replies to this message have been disabled by the reply-lockout filter");
[7d4fbcd]1901        return;
1902      }
1903    }
[4b464a4]1904
[d09e5a1]1905    /* admin */
1906    if (owl_message_is_type_admin(m)) {
[ec6ff52]1907      owl_function_error("You cannot reply to an admin message");
[d309eb3]1908      return;
1909    }
1910
[12c35df]1911    /* loopback */
1912    if (owl_message_is_type_loopback(m)) {
1913      owl_function_loopwrite_setup();
1914      return;
1915    }
1916
[d09e5a1]1917    /* zephyr */
1918    if (owl_message_is_type_zephyr(m)) {
[e7cc1c3]1919      /* if it's a zephyr we sent, send it out the same way again */
[d09e5a1]1920      if (owl_message_is_direction_out(m)) {
1921        owl_function_zwrite_setup(owl_message_get_zwriteline(m));
1922        owl_global_set_buffercommand(&g, owl_message_get_zwriteline(m));
1923        return;
1924      }
[e7cc1c3]1925
1926      /* Special case a personal reply to a webzephyr user on a class */
[1d3e925]1927      if ((type==1) && !strcasecmp(owl_message_get_opcode(m), OWL_WEBZEPHYR_OPCODE)) {
1928        class=OWL_WEBZEPHYR_CLASS;
[e7cc1c3]1929        inst=owl_message_get_sender(m);
1930        to=OWL_WEBZEPHYR_PRINCIPAL;
[1d3e925]1931      } else if (!strcasecmp(owl_message_get_class(m), OWL_WEBZEPHYR_CLASS) && owl_message_is_loginout(m)) {
[f562355]1932        /* Special case LOGIN/LOGOUT notifications on class "webzephyr" */
[1d3e925]1933        class=OWL_WEBZEPHYR_CLASS;
[e7cc1c3]1934        inst=owl_message_get_instance(m);
1935        to=OWL_WEBZEPHYR_PRINCIPAL;
[f562355]1936      } else if (owl_message_is_loginout(m)) {
1937        /* Normal LOGIN/LOGOUT messages */
[7d4fbcd]1938        class="MESSAGE";
1939        inst="PERSONAL";
1940        to=owl_message_get_sender(m);
1941      } else if (type==1) {
[f562355]1942        /* Personal reply */
[7d4fbcd]1943        class="MESSAGE";
1944        inst="PERSONAL";
1945        to=owl_message_get_sender(m);
1946      } else {
[f562355]1947        /* General reply */
[7d4fbcd]1948        class=owl_message_get_class(m);
1949        inst=owl_message_get_instance(m);
1950        to=owl_message_get_recipient(m);
[e50cd56]1951        cc=owl_message_get_cc(m);
[7d4fbcd]1952        if (!strcmp(to, "") || !strcmp(to, "*")) {
1953          to="";
1954        } else if (to[0]=='@') {
1955          /* leave it, to get the realm */
1956        } else {
1957          to=owl_message_get_sender(m);
1958        }
1959      }
[d09e5a1]1960       
[7d4fbcd]1961      /* create the command line */
[9ceee9d]1962      if (!strcasecmp(owl_message_get_opcode(m), "CRYPT")) {
1963        buff=owl_strdup("zcrypt");
1964      } else {
1965        buff = owl_strdup("zwrite");
1966      }
[7d4fbcd]1967      if (strcasecmp(class, "message")) {
[e50cd56]1968        buff = owl_sprintf("%s -c %s%s%s", oldbuff=buff, owl_getquoting(class), class, owl_getquoting(class));
1969        owl_free(oldbuff);
[7d4fbcd]1970      }
1971      if (strcasecmp(inst, "personal")) {
[e50cd56]1972        buff = owl_sprintf("%s -i %s%s%s", oldbuff=buff, owl_getquoting(inst), inst, owl_getquoting(inst));
1973        owl_free(oldbuff);
[7d4fbcd]1974      }
1975      if (*to != '\0') {
[f9c43ae]1976        char *tmp, *oldtmp, *tmp2;
[601a9e0]1977        tmp=short_zuser(to);
[e50cd56]1978        if (cc) {
1979          tmp = owl_util_uniq(oldtmp=tmp, cc, "-");
1980          owl_free(oldtmp);
1981          buff = owl_sprintf("%s -C %s", oldbuff=buff, tmp);
1982          owl_free(oldbuff);
1983        } else {
[f9c43ae]1984          if (owl_global_is_smartstrip(&g)) {
1985            tmp2=tmp;
[e3d9c77]1986            tmp=owl_zephyr_smartstripped_user(tmp2);
[f9c43ae]1987            owl_free(tmp2);
1988          }
[e50cd56]1989          buff = owl_sprintf("%s %s", oldbuff=buff, tmp);
1990          owl_free(oldbuff);
1991        }
[7d4fbcd]1992        owl_free(tmp);
1993      }
[e50cd56]1994      if (cc) owl_free(cc);
[d09e5a1]1995    }
[7d4fbcd]1996
[d09e5a1]1997    /* aim */
1998    if (owl_message_is_type_aim(m)) {
[440ce01]1999      if (owl_message_is_direction_out(m)) {
2000        buff=owl_sprintf("aimwrite %s", owl_message_get_recipient(m));
2001      } else {
2002        buff=owl_sprintf("aimwrite %s", owl_message_get_sender(m));
2003      }
[d09e5a1]2004    }
[440ce01]2005   
[d09e5a1]2006    if (enter) {
2007      owl_history *hist = owl_global_get_cmd_history(&g);
2008      owl_history_store(hist, buff);
2009      owl_history_reset(hist);
2010      owl_function_command_norv(buff);
2011    } else {
2012      owl_function_start_command(buff);
[7d4fbcd]2013    }
[d09e5a1]2014    owl_free(buff);
[7d4fbcd]2015  }
2016}
2017
[d54838d]2018void owl_function_zlocate(int argc, char **argv, int auth)
2019{
[2527615]2020  owl_fmtext fm;
2021  char *ptr, buff[LINE];
2022  int i;
2023
2024  owl_fmtext_init_null(&fm);
[7d4fbcd]2025
[2527615]2026  for (i=0; i<argc; i++) {
2027    ptr=long_zuser(argv[i]);
2028    owl_zephyr_zlocate(ptr, buff, auth);
2029    owl_fmtext_append_normal(&fm, buff);
2030    owl_free(ptr);
[7d4fbcd]2031  }
2032
[2527615]2033  owl_function_popless_fmtext(&fm);
2034  owl_fmtext_free(&fm);
[7d4fbcd]2035}
2036
[d54838d]2037void owl_function_start_command(char *line)
2038{
[7d4fbcd]2039  int i, j;
2040  owl_editwin *tw;
2041
2042  tw=owl_global_get_typwin(&g);
2043  owl_global_set_typwin_active(&g);
[10b866d]2044  owl_editwin_new_style(tw, OWL_EDITWIN_STYLE_ONELINE, 
2045                        owl_global_get_cmd_history(&g));
2046
[7d4fbcd]2047  owl_editwin_set_locktext(tw, "command: ");
2048  owl_global_set_needrefresh(&g);
2049
2050  j=strlen(line);
2051  for (i=0; i<j; i++) {
2052    owl_editwin_process_char(tw, line[i]);
2053  }
2054  owl_editwin_redisplay(tw, 0);
[cf83b7a]2055
2056  owl_context_set_editline(owl_global_get_context(&g), tw);
2057  owl_function_activate_keymap("editline");
2058}
2059
2060void owl_function_start_question(char *line)
2061{
2062  owl_editwin *tw;
2063
2064  tw=owl_global_get_typwin(&g);
2065  owl_global_set_typwin_active(&g);
2066  owl_editwin_new_style(tw, OWL_EDITWIN_STYLE_ONELINE, owl_global_get_cmd_history(&g));
2067
2068  owl_editwin_set_locktext(tw, line);
2069  owl_global_set_needrefresh(&g);
2070
2071  owl_editwin_redisplay(tw, 0);
2072
2073  owl_context_set_editresponse(owl_global_get_context(&g), tw);
2074  owl_function_activate_keymap("editresponse");
[7d4fbcd]2075}
2076
[453bd70]2077void owl_function_start_password(char *line)
2078{
2079  owl_editwin *tw;
2080
2081  tw=owl_global_get_typwin(&g);
2082  owl_global_set_typwin_active(&g);
2083  owl_editwin_new_style(tw, OWL_EDITWIN_STYLE_ONELINE, owl_global_get_cmd_history(&g));
2084  owl_editwin_set_echochar(tw, '*');
2085
2086  owl_editwin_set_locktext(tw, line);
2087  owl_global_set_needrefresh(&g);
2088
2089  owl_editwin_redisplay(tw, 0);
2090
2091  owl_context_set_editresponse(owl_global_get_context(&g), tw);
2092  owl_function_activate_keymap("editresponse");
2093}
2094
[d54838d]2095char *owl_function_exec(int argc, char **argv, char *buff, int type)
2096{
[7d4fbcd]2097  /* if type == 1 display in a popup
2098   * if type == 2 display an admin messages
2099   * if type == 0 return output
2100   * else display in a popup
2101   */
2102  char *newbuff, *redirect = " 2>&1 < /dev/null";
2103  char *out, buff2[1024];
2104  int size;
2105  FILE *p;
2106
[2a2bb60]2107#if OWL_STDERR_REDIR
2108  redirect = " < /dev/null";
2109#endif
2110
[7d4fbcd]2111  if (argc<2) {
[ec6ff52]2112    owl_function_error("Wrong number of arguments to the exec command");
[7d4fbcd]2113    return NULL;
2114  }
2115
2116  buff = skiptokens(buff, 1);
2117  newbuff = owl_malloc(strlen(buff)+strlen(redirect)+1);
2118  strcpy(newbuff, buff);
2119  strcat(newbuff, redirect);
2120
2121  p=popen(newbuff, "r");
2122  out=owl_malloc(1024);
2123  size=1024;
2124  strcpy(out, "");
2125  while (fgets(buff2, 1024, p)!=NULL) {
2126    size+=1024;
2127    out=owl_realloc(out, size);
2128    strcat(out, buff2);
2129  }
2130  pclose(p);
2131
2132  if (type==1) {
2133    owl_function_popless_text(out);
2134  } else if (type==0) {
2135    return out;
2136  } else if (type==2) {
2137    owl_function_adminmsg(buff, out);
2138  } else {
2139    owl_function_popless_text(out);
2140  }
2141  owl_free(out);
2142  return NULL;
2143}
2144
[d54838d]2145char *owl_function_perl(int argc, char **argv, char *buff, int type)
2146{
[7d4fbcd]2147  /* if type == 1 display in a popup
2148   * if type == 2 display an admin messages
2149   * if type == 0 return output
2150   * else display in a popup
2151   */
2152  char *perlout;
2153
2154  if (argc<2) {
[ec6ff52]2155    owl_function_error("Wrong number of arguments to perl command");
[7d4fbcd]2156    return NULL;
2157  }
2158
2159  /* consume first token (argv[0]) */
2160  buff = skiptokens(buff, 1);
2161
[f1e629d]2162  perlout = owl_perlconfig_execute(buff);
[7d4fbcd]2163  if (perlout) { 
2164    if (type==1) {
2165      owl_function_popless_text(perlout);
2166    } else if (type==2) {
2167      owl_function_adminmsg(buff, perlout);
2168    } else if (type==0) {
2169      return perlout;
2170    } else {
2171      owl_function_popless_text(perlout);
2172    }
2173    owl_free(perlout);
2174  }
2175  return NULL;
2176}
2177
[c3ab155]2178#if 0
2179void owl_function_change_view_old(char *filtname)
[d54838d]2180{
[7d4fbcd]2181  owl_view *v;
2182  owl_filter *f;
[f9c43ae]2183  int curid=-1, newpos, curmsg;
2184  owl_message *curm=NULL;
[7d4fbcd]2185
2186  v=owl_global_get_current_view(&g);
[f9c43ae]2187  curmsg=owl_global_get_curmsg(&g);
2188  if (curmsg==-1) {
2189    owl_function_debugmsg("Hit the curmsg==-1 case in change_view");
2190  } else {
2191    curm=owl_view_get_element(v, curmsg);
2192    if (curm) {
2193      curid=owl_message_get_id(curm);
2194      owl_view_save_curmsgid(v, curid);
2195    }
[59cf91c]2196  }
2197
[f9c43ae]2198  /* grab the filter */;
[7d4fbcd]2199  f=owl_global_get_filter(&g, filtname);
2200  if (!f) {
[ec6ff52]2201    owl_function_error("Unknown filter %s", filtname);
[7d4fbcd]2202    return;
2203  }
2204
[f9c43ae]2205  /* free the existing view and create a new one based on the filter */
[7d4fbcd]2206  owl_view_free(v);
2207  owl_view_create(v, f);
2208
[f9c43ae]2209  /* Figure out what to set the current message to.
2210   * - If the view we're leaving has messages in it, go to the closest message
2211   *   to the last message pointed to in that view.
2212   * - If the view we're leaving is empty, try to restore the position
2213   *   from the last time we were in the new view.  */
[59cf91c]2214  if (curm) {
2215    newpos = owl_view_get_nearest_to_msgid(v, curid);
2216  } else {
2217    newpos = owl_view_get_nearest_to_saved(v);
2218  }
2219
2220  owl_global_set_curmsg(&g, newpos);
2221
[f9c43ae]2222  owl_function_calculate_topmsg(OWL_DIRECTION_DOWNWARDS);
[7d4fbcd]2223  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
[f9c43ae]2224  owl_global_set_direction_downwards(&g);
[7d4fbcd]2225}
[c3ab155]2226#endif
2227
2228void owl_function_change_view(char *filtname)
2229{
2230  owl_view *v;
2231  owl_filter *f;
2232  int curid=-1, newpos, curmsg;
2233  owl_message *curm=NULL;
2234
2235  v=owl_global_get_current_view(&g);
2236
2237  curmsg=owl_global_get_curmsg(&g);
2238  if (curmsg==-1) {
2239    owl_function_debugmsg("Hit the curmsg==-1 case in change_view");
2240  } else {
2241    curm=owl_view_get_element(v, curmsg);
2242    if (curm) {
2243      curid=owl_message_get_id(curm);
2244      owl_view_save_curmsgid(v, curid);
2245    }
2246  }
2247
2248  f=owl_global_get_filter(&g, filtname);
2249  if (!f) {
[ec6ff52]2250    owl_function_error("Unknown filter %s", filtname);
[c3ab155]2251    return;
2252  }
2253
2254  owl_view_new_filter(v, f);
2255
2256  /* Figure out what to set the current message to.
2257   * - If the view we're leaving has messages in it, go to the closest message
2258   *   to the last message pointed to in that view.
2259   * - If the view we're leaving is empty, try to restore the position
2260   *   from the last time we were in the new view.  */
2261  if (curm) {
2262    newpos = owl_view_get_nearest_to_msgid(v, curid);
2263  } else {
2264    newpos = owl_view_get_nearest_to_saved(v);
2265  }
2266
2267  owl_global_set_curmsg(&g, newpos);
2268  owl_function_calculate_topmsg(OWL_DIRECTION_DOWNWARDS);
2269  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2270  owl_global_set_direction_downwards(&g);
2271}
[7d4fbcd]2272
[d54838d]2273void owl_function_create_filter(int argc, char **argv)
2274{
[7d4fbcd]2275  owl_filter *f;
2276  owl_view *v;
2277  int ret, inuse=0;
2278
2279  if (argc < 2) {
[ec6ff52]2280    owl_function_error("Wrong number of arguments to filter command");
[7d4fbcd]2281    return;
2282  }
2283
2284  v=owl_global_get_current_view(&g);
2285
2286  /* don't touch the all filter */
2287  if (!strcmp(argv[1], "all")) {
[ec6ff52]2288    owl_function_error("You may not change the 'all' filter.");
[7d4fbcd]2289    return;
2290  }
2291
2292  /* deal with the case of trying change the filter color */
2293  if (argc==4 && !strcmp(argv[2], "-c")) {
2294    f=owl_global_get_filter(&g, argv[1]);
2295    if (!f) {
[ec6ff52]2296      owl_function_error("The filter '%s' does not exist.", argv[1]);
[7d4fbcd]2297      return;
2298    }
[12c35df]2299    if (owl_util_string_to_color(argv[3])==-1) {
2300      owl_function_error("The color '%s' is not available.", argv[3]);
2301      return;
2302    }
[7d4fbcd]2303    owl_filter_set_color(f, owl_util_string_to_color(argv[3]));
2304    owl_global_set_needrefresh(&g);
2305    owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2306    return;
2307  }
2308
2309  /* create the filter and check for errors */
2310  f=owl_malloc(sizeof(owl_filter));
2311  ret=owl_filter_init(f, argv[1], argc-2, argv+2);
2312  if (ret==-1) {
2313    owl_free(f);
[ec6ff52]2314    owl_function_error("Invalid filter syntax");
[7d4fbcd]2315    return;
2316  }
2317
2318  /* if the named filter is in use by the current view, remember it */
2319  if (!strcmp(owl_view_get_filtname(v), argv[1])) {
2320    inuse=1;
2321  }
2322
2323  /* if the named filter already exists, nuke it */
2324  if (owl_global_get_filter(&g, argv[1])) {
2325    owl_global_remove_filter(&g, argv[1]);
2326  }
2327
2328  /* add the filter */
2329  owl_global_add_filter(&g, f);
2330
2331  /* if it was in use by the current view then update */
2332  if (inuse) {
2333    owl_function_change_view(argv[1]);
2334  }
2335  owl_global_set_needrefresh(&g);
2336  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2337}
2338
[d54838d]2339void owl_function_show_filters()
2340{
[7d4fbcd]2341  owl_list *l;
2342  owl_filter *f;
2343  int i, j;
2344  owl_fmtext fm;
2345
2346  owl_fmtext_init_null(&fm);
2347
2348  l=owl_global_get_filterlist(&g);
2349  j=owl_list_get_size(l);
2350
2351  owl_fmtext_append_bold(&fm, "Filters:\n");
2352
2353  for (i=0; i<j; i++) {
2354    f=owl_list_get_element(l, i);
2355    owl_fmtext_append_normal(&fm, "   ");
2356    if (owl_global_get_hascolors(&g)) {
2357      owl_fmtext_append_normal_color(&fm, owl_filter_get_name(f), owl_filter_get_color(f));
2358    } else {
2359      owl_fmtext_append_normal(&fm, owl_filter_get_name(f));
2360    }
2361    owl_fmtext_append_normal(&fm, "\n");
2362  }
2363  owl_function_popless_fmtext(&fm);
2364  owl_fmtext_free(&fm);
2365}
2366
[d54838d]2367void owl_function_show_filter(char *name)
2368{
[7d4fbcd]2369  owl_filter *f;
2370  char buff[5000];
2371
2372  f=owl_global_get_filter(&g, name);
2373  if (!f) {
[ec6ff52]2374    owl_function_error("There is no filter named %s", name);
[7d4fbcd]2375    return;
2376  }
2377  owl_filter_print(f, buff);
2378  owl_function_popless_text(buff);
2379}
2380
[d54838d]2381void owl_function_show_zpunts()
2382{
[7d4fbcd]2383  owl_filter *f;
2384  owl_list *fl;
2385  char buff[5000];
2386  owl_fmtext fm;
2387  int i, j;
2388
2389  owl_fmtext_init_null(&fm);
2390
2391  fl=owl_global_get_puntlist(&g);
2392  j=owl_list_get_size(fl);
2393  owl_fmtext_append_bold(&fm, "Active zpunt filters:\n");
2394
2395  for (i=0; i<j; i++) {
2396    f=owl_list_get_element(fl, i);
2397    owl_filter_print(f, buff);
2398    owl_fmtext_append_normal(&fm, buff);
2399  }
2400  owl_function_popless_fmtext(&fm);
2401  owl_fmtext_free(&fm);
2402}
2403
[3abf28b]2404/* Create a filter for a class, instance if one doesn't exist.  If
2405 * instance is NULL then catch all messgaes in the class.  Returns the
2406 * name of the filter, which the caller must free.
2407 */
2408char *owl_function_classinstfilt(char *class, char *instance) 
[d54838d]2409{
[7d4fbcd]2410  owl_list *fl;
2411  owl_filter *f;
2412  char *argbuff, *filtname;
[d54838d]2413  char *tmpclass, *tmpinstance = NULL;
[7d4fbcd]2414  int len;
2415
2416  fl=owl_global_get_filterlist(&g);
2417
2418  /* name for the filter */
2419  len=strlen(class)+30;
2420  if (instance) len+=strlen(instance);
2421  filtname=owl_malloc(len);
2422  if (!instance) {
2423    sprintf(filtname, "class-%s", class);
2424  } else {
2425    sprintf(filtname, "class-%s-instance-%s", class, instance);
2426  }
[ed2412d]2427  /* downcase it */
[7d4fbcd]2428  downstr(filtname);
[995eb4b]2429  /* turn spaces into dots */
[e3d9c77]2430  owl_text_tr(filtname, ' ', '.');
[ed2412d]2431 
[7d4fbcd]2432  /* if it already exists then go with it.  This lets users override */
2433  if (owl_global_get_filter(&g, filtname)) {
[ed2412d]2434    return(filtname);
[7d4fbcd]2435  }
2436
2437  /* create the new filter */
2438  argbuff=owl_malloc(len+20);
[995eb4b]2439  tmpclass=owl_text_quote(class, OWL_REGEX_QUOTECHARS, OWL_REGEX_QUOTEWITH);
[12c35df]2440  owl_text_tr(tmpclass, ' ', '.');
[ed2412d]2441  if (instance) {
[995eb4b]2442    tmpinstance=owl_text_quote(instance, OWL_REGEX_QUOTECHARS, OWL_REGEX_QUOTEWITH);
[12c35df]2443    owl_text_tr(tmpinstance, ' ', '.');
[ed2412d]2444  }
2445  sprintf(argbuff, "( class ^%s$ )", tmpclass);
[d54838d]2446  if (tmpinstance) {
[ed2412d]2447    sprintf(argbuff, "%s and ( instance ^%s$ )", argbuff, tmpinstance);
[7d4fbcd]2448  }
[ed2412d]2449  owl_free(tmpclass);
[d54838d]2450  if (tmpinstance) owl_free(tmpinstance);
[7d4fbcd]2451
2452  f=owl_malloc(sizeof(owl_filter));
2453  owl_filter_init_fromstring(f, filtname, argbuff);
2454
2455  /* add it to the global list */
2456  owl_global_add_filter(&g, f);
2457
2458  owl_free(argbuff);
[ed2412d]2459  return(filtname);
[7d4fbcd]2460}
2461
[3abf28b]2462/* Create a filter for personal zephyrs to or from the specified
2463 * zephyr user.  Includes login/logout notifications for the user.
2464 * The name of the filter will be 'user-<user>'.  If a filter already
2465 * exists with this name, no new filter will be created.  This allows
2466 * the configuration to override this function.  Returns the name of
2467 * the filter, which the caller must free.
2468 */
2469char *owl_function_zuserfilt(char *user)
[d54838d]2470{
[7d4fbcd]2471  owl_filter *f;
2472  char *argbuff, *longuser, *shortuser, *filtname;
2473
2474  /* stick the local realm on if it's not there */
[4b464a4]2475  longuser=long_zuser(user);
2476  shortuser=short_zuser(user);
[7d4fbcd]2477
2478  /* name for the filter */
2479  filtname=owl_malloc(strlen(shortuser)+20);
2480  sprintf(filtname, "user-%s", shortuser);
2481
2482  /* if it already exists then go with it.  This lets users override */
2483  if (owl_global_get_filter(&g, filtname)) {
[3abf28b]2484    return(owl_strdup(filtname));
[7d4fbcd]2485  }
2486
2487  /* create the new-internal filter */
2488  f=owl_malloc(sizeof(owl_filter));
2489
[4b464a4]2490  argbuff=owl_malloc(strlen(longuser)+1000);
2491  sprintf(argbuff, "( type ^zephyr$ and ( class ^message$ and instance ^personal$ and ");
2492  sprintf(argbuff, "%s ( ( direction ^in$ and sender ^%s$ ) or ( direction ^out$ and recipient ^%s$ ) ) )", argbuff, longuser, longuser);
2493  sprintf(argbuff, "%s or ( ( class ^login$ ) and ( sender ^%s$ ) ) )", argbuff, longuser);
[7d4fbcd]2494
2495  owl_filter_init_fromstring(f, filtname, argbuff);
2496
2497  /* add it to the global list */
2498  owl_global_add_filter(&g, f);
2499
2500  /* free stuff */
2501  owl_free(argbuff);
2502  owl_free(longuser);
2503  owl_free(shortuser);
[7360fab]2504
[ed2412d]2505  return(filtname);
[7d4fbcd]2506}
2507
[3abf28b]2508/* Create a filter for AIM IM messages to or from the specified
2509 * screenname.  The name of the filter will be 'aimuser-<user>'.  If a
2510 * filter already exists with this name, no new filter will be
2511 * created.  This allows the configuration to override this function.
2512 * Returns the name of the filter, which the caller must free.
2513 */
2514char *owl_function_aimuserfilt(char *user)
2515{
2516  owl_filter *f;
2517  char *argbuff, *filtname;
2518
2519  /* name for the filter */
2520  filtname=owl_malloc(strlen(user)+40);
2521  sprintf(filtname, "aimuser-%s", user);
2522
2523  /* if it already exists then go with it.  This lets users override */
2524  if (owl_global_get_filter(&g, filtname)) {
2525    return(owl_strdup(filtname));
2526  }
2527
2528  /* create the new-internal filter */
2529  f=owl_malloc(sizeof(owl_filter));
2530
2531  argbuff=owl_malloc(1000);
2532  sprintf(argbuff,
2533          "( type ^aim$ and ( ( sender ^%s$ and recipient ^%s$ ) or ( sender ^%s$ and recipient ^%s$ ) ) )",
2534          user, owl_global_get_aim_screenname(&g), owl_global_get_aim_screenname(&g), user);
2535
2536  owl_filter_init_fromstring(f, filtname, argbuff);
2537
2538  /* add it to the global list */
2539  owl_global_add_filter(&g, f);
2540
2541  /* free stuff */
2542  owl_free(argbuff);
2543
2544  return(filtname);
2545}
2546
2547char *owl_function_typefilt(char *type)
[d54838d]2548{
[f73e519]2549  owl_filter *f;
2550  char *argbuff, *filtname;
2551
2552  /* name for the filter */
2553  filtname=owl_sprintf("type-%s", type);
2554
2555  /* if it already exists then go with it.  This lets users override */
2556  if (owl_global_get_filter(&g, filtname)) {
2557    return filtname;
2558  }
2559
2560  /* create the new-internal filter */
2561  f=owl_malloc(sizeof(owl_filter));
2562
2563  argbuff = owl_sprintf("type ^%s$", type);
2564
2565  owl_filter_init_fromstring(f, filtname, argbuff);
2566
2567  /* add it to the global list */
2568  owl_global_add_filter(&g, f);
2569
2570  /* free stuff */
2571  owl_free(argbuff);
2572
2573  return filtname;
2574}
2575
[7d4fbcd]2576/* If flag is 1, marks for deletion.  If flag is 0,
2577 * unmarks for deletion. */
[d54838d]2578void owl_function_delete_curview_msgs(int flag)
2579{
[7d4fbcd]2580  owl_view *v;
2581  int i, j;
2582
2583  v=owl_global_get_current_view(&g);
2584  j=owl_view_get_size(v);
2585  for (i=0; i<j; i++) {
2586    if (flag == 1) {
2587      owl_message_mark_delete(owl_view_get_element(v, i));
2588    } else if (flag == 0) {
2589      owl_message_unmark_delete(owl_view_get_element(v, i));
2590    }
2591  }
2592
2593  owl_function_makemsg("%i messages marked for %sdeletion", j, flag?"":"un");
2594
2595  owl_mainwin_redisplay(owl_global_get_mainwin(&g)); 
2596}
2597
[3abf28b]2598/* Create a filter based on the current message.  Returns the name of
2599 * a filter or null.  The caller must free this name.
2600 *
2601 * if the curmsg is a personal zephyr return a filter name
2602 *    to the zephyr converstaion with that user.
2603 * If the curmsg is a zephyr class message, instance foo, recip *,
2604 *    return a filter name to the class, inst.
2605 * If the curmsg is a zephyr class message and type==0 then
2606 *    return a filter name for just the class.
2607 * If the curmsg is a zephyr class message and type==1 then
2608 *    return a filter name for the class and instance.
2609 * If the curmsg is a personal AIM message returna  filter
2610 *    name to the AIM conversation with that user
2611 */
[d54838d]2612char *owl_function_smartfilter(int type)
2613{
[7d4fbcd]2614  owl_view *v;
2615  owl_message *m;
[4b464a4]2616  char *zperson, *filtname=NULL;
[7d4fbcd]2617 
2618  v=owl_global_get_current_view(&g);
2619  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
2620
[5eeea3b]2621  if (!m || owl_view_get_size(v)==0) {
[ec6ff52]2622    owl_function_error("No message selected\n");
[4b464a4]2623    return(NULL);
[7d4fbcd]2624  }
2625
[f73e519]2626  /* very simple handling of admin messages for now */
[4b464a4]2627  if (owl_message_is_type_admin(m)) {
[3abf28b]2628    return(owl_function_typefilt("admin"));
2629  }
2630
[995eb4b]2631  /* very simple handling of loopback messages for now */
2632  if (owl_message_is_type_loopback(m)) {
2633    return(owl_function_typefilt("loopback"));
2634  }
2635
[3abf28b]2636  /* aim messages */
2637  if (owl_message_is_type_aim(m)) {
2638    if (owl_message_is_direction_in(m)) {
2639      filtname=owl_function_aimuserfilt(owl_message_get_sender(m));
2640    } else if (owl_message_is_direction_out(m)) {
2641      filtname=owl_function_aimuserfilt(owl_message_get_recipient(m));
2642    }
2643    return(filtname);
[7d4fbcd]2644  }
2645
[4b464a4]2646  /* narrow personal and login messages to the sender or recip as appropriate */
[5789230]2647  if (owl_message_is_personal(m) || owl_message_is_loginout(m)) {
[4b464a4]2648    if (owl_message_is_type_zephyr(m)) {
2649      if (owl_message_is_direction_in(m)) {
2650        zperson=short_zuser(owl_message_get_sender(m));
2651      } else {
2652        zperson=short_zuser(owl_message_get_recipient(m));
2653      }
[3abf28b]2654      filtname=owl_function_zuserfilt(zperson);
[4b464a4]2655      owl_free(zperson);
2656      return(filtname);
[7d4fbcd]2657    }
[4b464a4]2658    return(NULL);
[7d4fbcd]2659  }
2660
2661  /* narrow class MESSAGE, instance foo, recip * messages to class, inst */
[3abf28b]2662  if (!strcasecmp(owl_message_get_class(m), "message") && !owl_message_is_personal(m)) {
2663    filtname=owl_function_classinstfilt(owl_message_get_class(m), owl_message_get_instance(m));
[4b464a4]2664    return(filtname);
[7d4fbcd]2665  }
2666
2667  /* otherwise narrow to the class */
2668  if (type==0) {
[3abf28b]2669    filtname=owl_function_classinstfilt(owl_message_get_class(m), NULL);
[7d4fbcd]2670  } else if (type==1) {
[3abf28b]2671    filtname=owl_function_classinstfilt(owl_message_get_class(m), owl_message_get_instance(m));
[7d4fbcd]2672  }
[4b464a4]2673  return(filtname);
[7d4fbcd]2674}
2675
[d54838d]2676void owl_function_smartzpunt(int type)
2677{
[d36f2cb]2678  /* Starts a zpunt command based on the current class,instance pair.
2679   * If type=0, uses just class.  If type=1, uses instance as well. */
2680  owl_view *v;
2681  owl_message *m;
2682  char *cmd, *cmdprefix, *mclass, *minst;
2683 
2684  v=owl_global_get_current_view(&g);
2685  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
2686
[5eeea3b]2687  if (!m || owl_view_get_size(v)==0) {
[ec6ff52]2688    owl_function_error("No message selected\n");
[d36f2cb]2689    return;
2690  }
2691
2692  /* for now we skip admin messages. */
[4b464a4]2693  if (owl_message_is_type_admin(m)
[5789230]2694      || owl_message_is_loginout(m)
[4b464a4]2695      || !owl_message_is_type_zephyr(m)) {
[ec6ff52]2696    owl_function_error("smartzpunt doesn't support this message type.");
[d36f2cb]2697    return;
2698  }
2699
[cee1f25]2700  mclass = owl_message_get_class(m);
2701  minst = owl_message_get_instance(m);
[d36f2cb]2702  if (!mclass || !*mclass || *mclass==' '
2703      || (!strcasecmp(mclass, "message") && !strcasecmp(minst, "personal"))
2704      || (type && (!minst || !*minst|| *minst==' '))) {
[ec6ff52]2705    owl_function_error("smartzpunt can't safely do this for <%s,%s>",
[d36f2cb]2706                         mclass, minst);
2707  } else {
2708    cmdprefix = "start-command zpunt ";
[cee1f25]2709    cmd = owl_malloc(strlen(cmdprefix)+strlen(mclass)+strlen(minst)+10);
[d36f2cb]2710    strcpy(cmd, cmdprefix);
[cee1f25]2711    strcat(cmd, owl_getquoting(mclass));
[d36f2cb]2712    strcat(cmd, mclass);
[cee1f25]2713    strcat(cmd, owl_getquoting(mclass));
[d36f2cb]2714    if (type) {
2715      strcat(cmd, " ");
[cee1f25]2716      strcat(cmd, owl_getquoting(minst));
[d36f2cb]2717      strcat(cmd, minst);
[cee1f25]2718      strcat(cmd, owl_getquoting(minst));
[d36f2cb]2719    } else {
2720      strcat(cmd, " *");
2721    }
2722    owl_function_command(cmd);
2723    owl_free(cmd);
2724  }
2725}
2726
[d54838d]2727void owl_function_color_current_filter(char *color)
2728{
[7d4fbcd]2729  owl_filter *f;
2730  char *name;
2731
2732  name=owl_view_get_filtname(owl_global_get_current_view(&g));
2733  f=owl_global_get_filter(&g, name);
2734  if (!f) {
[ec6ff52]2735    owl_function_error("Unknown filter");
[7d4fbcd]2736    return;
2737  }
2738
2739  /* don't touch the all filter */
2740  if (!strcmp(name, "all")) {
[ec6ff52]2741    owl_function_error("You may not change the 'all' filter.");
[7d4fbcd]2742    return;
2743  }
2744
2745  /* deal with the case of trying change the filter color */
[12c35df]2746  if (owl_util_string_to_color(color)==-1) {
2747    owl_function_error("No color named '%s' avilable.");
2748    return;
2749  }
[7d4fbcd]2750  owl_filter_set_color(f, owl_util_string_to_color(color));
2751  owl_global_set_needrefresh(&g);
2752  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2753}
2754
[d54838d]2755void owl_function_show_colors()
2756{
[7d4fbcd]2757  owl_fmtext fm;
2758
2759  owl_fmtext_init_null(&fm);
2760  owl_fmtext_append_normal_color(&fm, "default\n", OWL_COLOR_DEFAULT);
2761  owl_fmtext_append_normal_color(&fm, "red\n", OWL_COLOR_RED);
2762  owl_fmtext_append_normal_color(&fm, "green\n", OWL_COLOR_GREEN);
2763  owl_fmtext_append_normal_color(&fm, "yellow\n", OWL_COLOR_YELLOW);
2764  owl_fmtext_append_normal_color(&fm, "blue\n", OWL_COLOR_BLUE);
2765  owl_fmtext_append_normal_color(&fm, "magenta\n", OWL_COLOR_MAGENTA);
2766  owl_fmtext_append_normal_color(&fm, "cyan\n", OWL_COLOR_CYAN);
2767  owl_fmtext_append_normal_color(&fm, "white\n", OWL_COLOR_WHITE);
2768
2769  owl_function_popless_fmtext(&fm);
2770  owl_fmtext_free(&fm);
2771}
2772
[5bb6c21]2773/* add the given class, inst, recip to the punt list for filtering.
2774 *   if direction==0 then punt
2775 *   if direction==1 then unpunt
2776 */
[d54838d]2777void owl_function_zpunt(char *class, char *inst, char *recip, int direction)
2778{
[7d4fbcd]2779  owl_filter *f;
2780  owl_list *fl;
2781  char *buff;
[bc08664]2782  char *quoted;
[7d4fbcd]2783  int ret, i, j;
2784
2785  fl=owl_global_get_puntlist(&g);
2786
2787  /* first, create the filter */
2788  f=malloc(sizeof(owl_filter));
2789  buff=malloc(strlen(class)+strlen(inst)+strlen(recip)+100);
[5bb6c21]2790  strcpy(buff, "class");
2791  if (!strcmp(class, "*")) {
2792    strcat(buff, " .*");
2793  } else {
[bc08664]2794    quoted=owl_text_quote(class, OWL_REGEX_QUOTECHARS, OWL_REGEX_QUOTEWITH);
[cee1f25]2795    owl_text_tr(quoted, ' ', '.');
[bc08664]2796    sprintf(buff, "%s ^%s$", buff, quoted);
2797    owl_free(quoted);
[5bb6c21]2798  }
2799  if (!strcmp(inst, "*")) {
2800    strcat(buff, " and instance .*");
[7d4fbcd]2801  } else {
[bc08664]2802    quoted=owl_text_quote(inst, OWL_REGEX_QUOTECHARS, OWL_REGEX_QUOTEWITH);
[cee1f25]2803    owl_text_tr(quoted, ' ', '.');
[bc08664]2804    sprintf(buff, "%s and instance ^%s$", buff, quoted);
2805    owl_free(quoted);
[7d4fbcd]2806  }
[5bb6c21]2807  if (strcmp(recip, "*")) {
[bc08664]2808    quoted=owl_text_quote(recip, OWL_REGEX_QUOTECHARS, OWL_REGEX_QUOTEWITH);
[cee1f25]2809    owl_text_tr(quoted, ' ', '.');
[bc08664]2810    sprintf(buff, "%s and recipient ^%s$", buff, quoted);
2811    owl_free(quoted);
[5bb6c21]2812  }
2813 
[7d4fbcd]2814  owl_function_debugmsg("About to filter %s", buff);
2815  ret=owl_filter_init_fromstring(f, "punt-filter", buff);
2816  owl_free(buff);
2817  if (ret) {
[ec6ff52]2818    owl_function_error("Error creating filter for zpunt");
[7d4fbcd]2819    owl_filter_free(f);
2820    return;
2821  }
2822
2823  /* Check for an identical filter */
2824  j=owl_list_get_size(fl);
2825  for (i=0; i<j; i++) {
2826    if (owl_filter_equiv(f, owl_list_get_element(fl, i))) {
2827      /* if we're punting, then just silently bow out on this duplicate */
2828      if (direction==0) {
2829        owl_filter_free(f);
2830        return;
2831      }
2832
2833      /* if we're unpunting, then remove this filter from the puntlist */
2834      if (direction==1) {
2835        owl_filter_free(owl_list_get_element(fl, i));
2836        owl_list_remove_element(fl, i);
2837        return;
2838      }
2839    }
2840  }
2841
2842  /* If we're punting, add the filter to the global punt list */
2843  if (direction==0) {
2844    owl_list_append_element(fl, f);
2845  }
2846}
2847
[d54838d]2848void owl_function_activate_keymap(char *keymap)
2849{
[7d4fbcd]2850  if (!owl_keyhandler_activate(owl_global_get_keyhandler(&g), keymap)) {
[ec6ff52]2851    owl_function_error("Unable to activate keymap '%s'", keymap);
[7d4fbcd]2852  }
2853}
2854
[d54838d]2855void owl_function_show_keymaps()
2856{
[7d4fbcd]2857  owl_list l;
2858  owl_fmtext fm;
[1aee7d9]2859  owl_keymap *km;
2860  owl_keyhandler *kh;
2861  int i, numkm;
2862  char *kmname;
[7d4fbcd]2863
[1aee7d9]2864  kh = owl_global_get_keyhandler(&g);
[7d4fbcd]2865  owl_fmtext_init_null(&fm);
2866  owl_fmtext_append_bold(&fm, "Keymaps:   ");
2867  owl_fmtext_append_normal(&fm, "(use 'show keymap <name>' for details)\n");
[1aee7d9]2868  owl_keyhandler_get_keymap_names(kh, &l);
[7d4fbcd]2869  owl_fmtext_append_list(&fm, &l, "\n", owl_function_keymap_summary);
2870  owl_fmtext_append_normal(&fm, "\n");
[1aee7d9]2871
2872  numkm = owl_list_get_size(&l);
2873  for (i=0; i<numkm; i++) {
2874    kmname = owl_list_get_element(&l, i);
2875    km = owl_keyhandler_get_keymap(kh, kmname);
2876    owl_fmtext_append_bold(&fm, "\n\n----------------------------------------------------------------------------------------------------\n\n");
2877    owl_keymap_get_details(km, &fm);   
2878  }
2879  owl_fmtext_append_normal(&fm, "\n");
2880 
[7d4fbcd]2881  owl_function_popless_fmtext(&fm);
2882  owl_keyhandler_keymap_namelist_free(&l);
2883  owl_fmtext_free(&fm);
2884}
2885
[d54838d]2886char *owl_function_keymap_summary(void *name)
2887{
[7d4fbcd]2888  owl_keymap *km
2889    = owl_keyhandler_get_keymap(owl_global_get_keyhandler(&g), name);
2890  if (km) return owl_keymap_summary(km);
2891  else return(NULL);
2892}
2893
2894/* TODO: implement for real */
[d54838d]2895void owl_function_show_keymap(char *name)
2896{
[1fd0b25]2897  owl_fmtext fm;
[7d4fbcd]2898  owl_keymap *km;
2899
2900  owl_fmtext_init_null(&fm);
2901  km = owl_keyhandler_get_keymap(owl_global_get_keyhandler(&g), name);
2902  if (km) {
2903    owl_keymap_get_details(km, &fm);
2904  } else {
2905    owl_fmtext_append_normal(&fm, "No such keymap...\n");
2906  } 
2907  owl_function_popless_fmtext(&fm);
2908  owl_fmtext_free(&fm);
2909}
2910
[d54838d]2911void owl_function_help_for_command(char *cmdname)
2912{
[1fd0b25]2913  owl_fmtext fm;
[7d4fbcd]2914
2915  owl_fmtext_init_null(&fm);
2916  owl_cmd_get_help(owl_global_get_cmddict(&g), cmdname, &fm);
2917  owl_function_popless_fmtext(&fm); 
2918  owl_fmtext_free(&fm);
2919}
[1fd0b25]2920
[d54838d]2921void owl_function_search_start(char *string, int direction)
2922{
[1fd0b25]2923  /* direction is OWL_DIRECTION_DOWNWARDS or OWL_DIRECTION_UPWARDS */
2924  owl_global_set_search_active(&g, string);
2925  owl_function_search_helper(0, direction);
2926}
2927
[d54838d]2928void owl_function_search_continue(int direction)
2929{
[1fd0b25]2930  /* direction is OWL_DIRECTION_DOWNWARDS or OWL_DIRECTION_UPWARDS */
2931  owl_function_search_helper(1, direction);
2932}
2933
[d54838d]2934void owl_function_search_helper(int mode, int direction)
2935{
[1fd0b25]2936  /* move to a message that contains the string.  If direction is
2937   * OWL_DIRECTION_DOWNWARDS then search fowards, if direction is
2938   * OWL_DIRECTION_UPWARDS then search backwards.
2939   *
2940   * If mode==0 then it will stay on the current message if it
2941   * contains the string.
2942   */
2943
2944  owl_view *v;
2945  int viewsize, i, curmsg, start;
2946  owl_message *m;
2947
2948  v=owl_global_get_current_view(&g);
2949  viewsize=owl_view_get_size(v);
2950  curmsg=owl_global_get_curmsg(&g);
2951 
2952  if (viewsize==0) {
[ec6ff52]2953    owl_function_error("No messages present");
[1fd0b25]2954    return;
2955  }
2956
2957  if (mode==0) {
2958    start=curmsg;
2959  } else if (direction==OWL_DIRECTION_DOWNWARDS) {
2960    start=curmsg+1;
2961  } else {
2962    start=curmsg-1;
2963  }
2964
2965  /* bounds check */
2966  if (start>=viewsize || start<0) {
[ec6ff52]2967    owl_function_error("No further matches found");
[1fd0b25]2968    return;
2969  }
2970
2971  for (i=start; i<viewsize && i>=0;) {
2972    m=owl_view_get_element(v, i);
2973    if (owl_message_search(m, owl_global_get_search_string(&g))) {
2974      owl_global_set_curmsg(&g, i);
2975      owl_function_calculate_topmsg(direction);
2976      owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2977      if (direction==OWL_DIRECTION_DOWNWARDS) {
2978        owl_global_set_direction_downwards(&g);
2979      } else {
2980        owl_global_set_direction_upwards(&g);
2981      }
2982      return;
2983    }
2984    if (direction==OWL_DIRECTION_DOWNWARDS) {
2985      i++;
2986    } else {
2987      i--;
2988    }
2989  }
[37c27cf]2990  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
[ec6ff52]2991  owl_function_error("No matches found");
[1fd0b25]2992}
2993
2994/* strips formatting from ztext and returns the unformatted text.
2995 * caller is responsible for freeing. */
[d54838d]2996char *owl_function_ztext_stylestrip(char *zt)
2997{
[1fd0b25]2998  owl_fmtext fm;
2999  char *plaintext;
3000
3001  owl_fmtext_init_null(&fm);
3002  owl_fmtext_append_ztext(&fm, zt);
3003  plaintext = owl_fmtext_print_plain(&fm);
3004  owl_fmtext_free(&fm);
3005  return(plaintext);
3006}
[42abb10]3007
[aa5f725]3008/* Popup a buddylisting.  If file is NULL use the default .anyone */
3009void owl_function_buddylist(int aim, int zephyr, char *file)
[d54838d]3010{
[de03334]3011  int i, j, idle;
[42abb10]3012  owl_fmtext fm;
[f4d0975]3013  owl_buddylist *bl;
3014  owl_buddy *b;
[de03334]3015  char *foo, *timestr;
[09489b89]3016#ifdef HAVE_LIBZEPHYR
3017  char *ourfile, *tmp, buff[LINE], *line;
3018  ZLocations_t location[200];
3019  FILE *f;
3020  int numlocs, ret;
3021#endif
[42abb10]3022
[aa5f725]3023  owl_fmtext_init_null(&fm);
[42abb10]3024
[a0a5179]3025  /* AIM first */
[aa5f725]3026  if (aim && owl_global_is_aimloggedin(&g)) {
[f4d0975]3027    bl=owl_global_get_buddylist(&g);
[42abb10]3028
[aa5f725]3029    owl_fmtext_append_bold(&fm, "AIM users logged in:\n");
[f4d0975]3030    /* we're assuming AIM for now */
3031    j=owl_buddylist_get_size(bl);
[aa5f725]3032    for (i=0; i<j; i++) {
[f4d0975]3033      b=owl_buddylist_get_buddy_n(bl, i);
3034      idle=owl_buddy_get_idle_time(b);
[de03334]3035      if (idle!=0) {
[5b85d19]3036        timestr=owl_util_minutes_to_timestr(idle);
[de03334]3037      } else {
3038        timestr=owl_strdup("");
3039      }
[a0a5179]3040      foo=owl_sprintf("  %-20.20s %-12.12s\n", owl_buddy_get_name(b), timestr);
[de03334]3041      owl_fmtext_append_normal(&fm, foo);
3042      owl_free(timestr);
3043      owl_free(foo);
[42abb10]3044    }
[aa5f725]3045  }
[42abb10]3046
[09489b89]3047#ifdef HAVE_LIBZEPHYR
[aa5f725]3048  if (zephyr) {
3049    if (file==NULL) {
3050      tmp=owl_global_get_homedir(&g);
[a0a5179]3051      ourfile=owl_sprintf("%s/.anyone", owl_global_get_homedir(&g));
[aa5f725]3052    } else {
3053      ourfile=owl_strdup(file);
[42abb10]3054    }
[09489b89]3055
[aa5f725]3056    owl_fmtext_append_bold(&fm, "Zephyr users logged in:\n");
[a0a5179]3057    f=fopen(ourfile, "r");
3058    if (!f) {
3059      owl_fmtext_append_normal(&fm, "  Error opening file ");
3060      owl_fmtext_append_normal(&fm, ourfile);
3061      owl_fmtext_append_normal(&fm, "\n");
3062      owl_function_error("Error opening file %s: %s", ourfile, strerror(errno) ? strerror(errno) : "");
3063    } else {
3064      while (fgets(buff, LINE, f)!=NULL) {
3065        /* ignore comments, blank lines etc. */
3066        if (buff[0]=='#') continue;
3067        if (buff[0]=='\n') continue;
3068        if (buff[0]=='\0') continue;
3069       
3070        /* strip the \n */
3071        buff[strlen(buff)-1]='\0';
3072       
3073        /* ingore from # on */
3074        tmp=strchr(buff, '#');
3075        if (tmp) tmp[0]='\0';
3076       
3077        /* ingore from SPC */
3078        tmp=strchr(buff, ' ');
3079        if (tmp) tmp[0]='\0';
3080       
3081        /* stick on the local realm. */
3082        if (!strchr(buff, '@')) {
3083          strcat(buff, "@");
3084          strcat(buff, ZGetRealm());
3085        }
3086       
3087        ret=ZLocateUser(buff, &numlocs, ZAUTH);
3088        if (ret!=ZERR_NONE) {
3089          owl_function_error("Error getting location for %s", buff);
3090          continue;
[aa5f725]3091        }
[a0a5179]3092       
3093        numlocs=200;
3094        ret=ZGetLocations(location, &numlocs);
3095        if (ret==0) {
3096          for (i=0; i<numlocs; i++) {
3097            line=malloc(strlen(location[i].host)+strlen(location[i].time)+strlen(location[i].tty)+100);
3098            tmp=short_zuser(buff);
3099            sprintf(line, "  %-10.10s %-24.24s %-12.12s  %20.20s\n",
3100                    tmp,
3101                    location[i].host,
3102                    location[i].tty,
3103                    location[i].time);
3104            owl_fmtext_append_normal(&fm, line);
3105            owl_free(tmp);
3106          }
3107          if (numlocs>=200) {
3108            owl_fmtext_append_normal(&fm, "  Too many locations found for this user, truncating.\n");
3109          }
[aa5f725]3110        }
[42abb10]3111      }
[a0a5179]3112      fclose(f);
[42abb10]3113    }
[aa5f725]3114    owl_free(ourfile);
[42abb10]3115  }
[09489b89]3116#endif
[aa5f725]3117 
[42abb10]3118  owl_function_popless_fmtext(&fm);
3119  owl_fmtext_free(&fm);
3120}
[2adaf1d]3121
[f36222f]3122/* Dump messages in the current view to the file 'filename'. */
[d54838d]3123void owl_function_dump(char *filename) 
3124{
[2adaf1d]3125  int i, j, count;
3126  owl_message *m