source: functions.c @ 12c35df

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