source: functions.c @ a836200

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