source: functions.c @ 8c92848

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