source: functions.c @ ec6ff52

barnowl_perlaimdebianowlrelease-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since ec6ff52 was ec6ff52, checked in by James M. Kretchmar <kretch@mit.edu>, 17 years ago
Added the 'show errors' command Added the error queue Converted most makemsg's to error instead Started writing an internal VT style (far from done)
  • Property mode set to 100644
File size: 84.1 KB
RevLine 
[7d4fbcd]1#include <stdio.h>
2#include <stdlib.h>
3#include <unistd.h>
4#include <signal.h>
5#include <string.h>
6#include <time.h>
[2adaf1d]7#include <sys/types.h>
8#include <sys/stat.h>
[8f44c6b]9#include <sys/wait.h>
10#include <errno.h>
[d09e5a1]11#include <signal.h>
[7d4fbcd]12#include "owl.h"
13
[1aee7d9]14static const char fileIdent[] = "$Id$";
15
[d54838d]16void owl_function_noop(void)
17{
[7d4fbcd]18  return;
19}
20
[d54838d]21char *owl_function_command(char *cmdbuff)
22{
[7d4fbcd]23  owl_function_debugmsg("executing command: %s", cmdbuff);
24  return owl_cmddict_execute(owl_global_get_cmddict(&g), 
25                             owl_global_get_context(&g), cmdbuff);
26}
27
[d54838d]28void owl_function_command_norv(char *cmdbuff)
29{
[7d4fbcd]30  char *rv;
[4b464a4]31  rv=owl_function_command(cmdbuff);
[7d4fbcd]32  if (rv) owl_free(rv);
33}
34
[d54838d]35void owl_function_command_alias(char *alias_from, char *alias_to)
36{
[7d4fbcd]37  owl_cmddict_add_alias(owl_global_get_cmddict(&g), alias_from, alias_to);
38}
39
[d54838d]40owl_cmd *owl_function_get_cmd(char *name)
41{
[7d4fbcd]42  return owl_cmddict_find(owl_global_get_cmddict(&g), name);
43}
44
[d54838d]45void owl_function_show_commands()
46{
[7d4fbcd]47  owl_list l;
48  owl_fmtext fm;
49
50  owl_fmtext_init_null(&fm);
51  owl_fmtext_append_bold(&fm, "Commands:  ");
52  owl_fmtext_append_normal(&fm, "(use 'show command <name>' for details)\n");
53  owl_cmddict_get_names(owl_global_get_cmddict(&g), &l);
54  owl_fmtext_append_list(&fm, &l, "\n", owl_function_cmd_describe);
55  owl_fmtext_append_normal(&fm, "\n");
56  owl_function_popless_fmtext(&fm);
57  owl_cmddict_namelist_free(&l);
58  owl_fmtext_free(&fm);
59}
60
[ef56a67]61void owl_function_show_view(char *viewname)
62{
63  owl_view *v;
64  owl_fmtext fm;
65
66  /* we only have the one view right now */
67  v=owl_global_get_current_view(&g);
68  if (viewname && strcmp(viewname, owl_view_get_name(v))) {
[ec6ff52]69    owl_function_error("No view named '%s'", viewname);
[ef56a67]70    return;
71  }
72
73  owl_fmtext_init_null(&fm);
74  owl_view_to_fmtext(v, &fm);
75  owl_function_popless_fmtext(&fm);
76  owl_fmtext_free(&fm);
77}
78
[f1e629d]79void owl_function_show_styles() {
80  owl_list l;
81  owl_fmtext fm;
82
83  owl_fmtext_init_null(&fm);
84  owl_fmtext_append_bold(&fm, "Styles:\n");
85  owl_global_get_style_names(&g, &l);
86  owl_fmtext_append_list(&fm, &l, "\n", owl_function_style_describe);
87  owl_fmtext_append_normal(&fm, "\n");
88  owl_function_popless_fmtext(&fm);
89  owl_list_free_all(&l, owl_free);
90  owl_fmtext_free(&fm);
91}
92
93char *owl_function_style_describe(void *name) {
94  char *desc, *s;
95  owl_style *style;
96  style = owl_global_get_style_by_name(&g, name);
97  if (style) {
98    desc = owl_style_get_description(style);
99  } else {
100    desc = "???";
101  }
102  s = owl_sprintf("%-20s - %s%s", name, 
103                  0==owl_style_validate(style)?"":"[INVALID] ",
104                  desc);
105  return s;
106}
[ef56a67]107
[d54838d]108char *owl_function_cmd_describe(void *name)
109{
[7d4fbcd]110  owl_cmd *cmd = owl_cmddict_find(owl_global_get_cmddict(&g), name);
111  if (cmd) return owl_cmd_describe(cmd);
112  else return(NULL);
113}
114
[d54838d]115void owl_function_show_command(char *name)
116{
[7d4fbcd]117  owl_function_help_for_command(name);
118}
119
[d54838d]120void owl_function_adminmsg(char *header, char *body)
121{
[7d4fbcd]122  owl_message *m;
123  int followlast;
124
125  followlast=owl_global_should_followlast(&g);
126  m=owl_malloc(sizeof(owl_message));
127  owl_message_create_admin(m, header, body);
128  owl_messagelist_append_element(owl_global_get_msglist(&g), m);
129  owl_view_consider_message(owl_global_get_current_view(&g), m);
130
131  if (followlast) owl_function_lastmsg_noredisplay();
132
133  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
134  if (owl_popwin_is_active(owl_global_get_popwin(&g))) {
135    owl_popwin_refresh(owl_global_get_popwin(&g));
136  }
137 
138  wnoutrefresh(owl_global_get_curs_recwin(&g));
139  owl_global_set_needrefresh(&g);
140}
141
[d54838d]142void owl_function_make_outgoing_zephyr(char *body, char *zwriteline, char *zsig)
143{
[7d4fbcd]144  owl_message *m;
145  int followlast;
[4b464a4]146  owl_zwrite z;
[7d4fbcd]147 
148  followlast=owl_global_should_followlast(&g);
[4b464a4]149
150  /* create a zwrite for the purpose of filling in other message fields */
151  owl_zwrite_create_from_line(&z, zwriteline);
152
153  /* create the message */
[7d4fbcd]154  m=owl_malloc(sizeof(owl_message));
[b45293f]155  owl_message_create_from_zwriteline(m, zwriteline, body, zsig);
[4b464a4]156
[8fec514]157  /* add it to the global list and current view */
[7d4fbcd]158  owl_messagelist_append_element(owl_global_get_msglist(&g), m);
159  owl_view_consider_message(owl_global_get_current_view(&g), m);
160
161  if (followlast) owl_function_lastmsg_noredisplay();
162
163  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
164  if (owl_popwin_is_active(owl_global_get_popwin(&g))) {
165    owl_popwin_refresh(owl_global_get_popwin(&g));
166  }
167 
168  wnoutrefresh(owl_global_get_curs_recwin(&g));
169  owl_global_set_needrefresh(&g);
[56330ff]170  owl_zwrite_free(&z);
[7d4fbcd]171}
172
[aa5f725]173int owl_function_make_outgoing_aim(char *body, char *to)
[d09e5a1]174{
175  owl_message *m;
176  int followlast;
[aa5f725]177
178
179  if (!owl_global_is_aimloggedin(&g)) {
180    return(-1);
181  }
[d09e5a1]182 
183  followlast=owl_global_should_followlast(&g);
184
185  /* create the message */
186  m=owl_malloc(sizeof(owl_message));
[d559df9]187  owl_message_create_aim(m,
188                         owl_global_get_aim_screenname(&g),
189                         to,
190                         body,
191                         OWL_MESSAGE_DIRECTION_OUT,
192                         0);
[d09e5a1]193
194  /* add it to the global list and current view */
195  owl_messagelist_append_element(owl_global_get_msglist(&g), m);
196  owl_view_consider_message(owl_global_get_current_view(&g), m);
197
198  if (followlast) owl_function_lastmsg_noredisplay();
199
200  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
201  if (owl_popwin_is_active(owl_global_get_popwin(&g))) {
202    owl_popwin_refresh(owl_global_get_popwin(&g));
203  }
204 
205  wnoutrefresh(owl_global_get_curs_recwin(&g));
206  owl_global_set_needrefresh(&g);
[aa5f725]207  return(0);
[d09e5a1]208}
209
[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) {
[ec6ff52]442    owl_function_error("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) {
[ec6ff52]491    owl_function_error("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();
[ec6ff52]685    owl_function_error("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);
1206}
1207
[d54838d]1208void owl_function_about()
1209{
[7d4fbcd]1210  char buff[5000];
1211
1212  sprintf(buff, "This is owl version %s\n", OWL_VERSION_STRING);
1213  strcat(buff, "\nOwl was written by James Kretchmar at the Massachusetts\n");
1214  strcat(buff, "Institute of Technology.  The first version, 0.5, was\n");
[bde7714]1215  strcat(buff, "released in March 2002.\n");
[7d4fbcd]1216  strcat(buff, "\n");
1217  strcat(buff, "The name 'owl' was chosen in reference to the owls in the\n");
1218  strcat(buff, "Harry Potter novels, who are tasked with carrying messages\n");
1219  strcat(buff, "between Witches and Wizards.\n");
1220  strcat(buff, "\n");
1221  strcat(buff, "Copyright 2002 Massachusetts Institute of Technology\n");
1222  strcat(buff, "\n");
1223  strcat(buff, "Permission to use, copy, modify, and distribute this\n");
1224  strcat(buff, "software and its documentation for any purpose and without\n");
1225  strcat(buff, "fee is hereby granted, provided that the above copyright\n");
1226  strcat(buff, "notice and this permission notice appear in all copies\n");
1227  strcat(buff, "and in supporting documentation.  No representation is\n");
1228  strcat(buff, "made about the suitability of this software for any\n");
1229  strcat(buff, "purpose.  It is provided \"as is\" without express\n");
1230  strcat(buff, "or implied warranty.\n");
1231  owl_function_popless_text(buff);
1232}
1233
[d54838d]1234void owl_function_info()
1235{
[7d4fbcd]1236  owl_message *m;
[5789230]1237  owl_fmtext fm, attrfm;
[09489b89]1238  char buff[10000];
[7d4fbcd]1239  owl_view *v;
[09489b89]1240#ifdef HAVE_LIBZEPHYR
1241  ZNotice_t *n;
1242#endif
[7d4fbcd]1243
[d0d65df]1244  owl_fmtext_init_null(&fm);
1245 
[7d4fbcd]1246  v=owl_global_get_current_view(&g);
[5eeea3b]1247  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
1248  if (!m || owl_view_get_size(v)==0) {
[ec6ff52]1249    owl_function_error("No message selected\n");
[7d4fbcd]1250    return;
1251  }
1252
[5789230]1253  owl_fmtext_append_bold(&fm, "General Information:\n");
1254  owl_fmtext_append_normal(&fm, "  Msg Id    : ");
[d0d65df]1255  sprintf(buff, "%i", owl_message_get_id(m));
1256  owl_fmtext_append_normal(&fm, buff);
1257  owl_fmtext_append_normal(&fm, "\n");
[df0d93a]1258
[5789230]1259  owl_fmtext_append_normal(&fm, "  Type      : ");
[df0d93a]1260  owl_fmtext_append_bold(&fm, owl_message_type_to_string(m));
1261  owl_fmtext_append_normal(&fm, "\n");
1262
[4b464a4]1263  if (owl_message_is_direction_in(m)) {
[5789230]1264    owl_fmtext_append_normal(&fm, "  Direction : in\n");
[4b464a4]1265  } else if (owl_message_is_direction_out(m)) {
[5789230]1266    owl_fmtext_append_normal(&fm, "  Direction : out\n");
[4b464a4]1267  } else if (owl_message_is_direction_none(m)) {
[5789230]1268    owl_fmtext_append_normal(&fm, "  Direction : none\n");
[4b464a4]1269  } else {
[5789230]1270    owl_fmtext_append_normal(&fm, "  Direction : unknown\n");
[4b464a4]1271  }
[df0d93a]1272
[5789230]1273  owl_fmtext_append_normal(&fm, "  Time      : ");
[d0d65df]1274  owl_fmtext_append_normal(&fm, owl_message_get_timestr(m));
1275  owl_fmtext_append_normal(&fm, "\n");
[4b464a4]1276
[df0d93a]1277  if (!owl_message_is_type_admin(m)) {
[5789230]1278    owl_fmtext_append_normal(&fm, "  Sender    : ");
[df0d93a]1279    owl_fmtext_append_normal(&fm, owl_message_get_sender(m));
1280    owl_fmtext_append_normal(&fm, "\n");
1281   
[5789230]1282    owl_fmtext_append_normal(&fm, "  Recipient : ");
[df0d93a]1283    owl_fmtext_append_normal(&fm, owl_message_get_recipient(m));
1284    owl_fmtext_append_normal(&fm, "\n");
1285  }
1286   
[0ff8fb57]1287  if (owl_message_is_type_zephyr(m)) {
[5789230]1288    owl_fmtext_append_bold(&fm, "\nZephyr Specific Information:\n");
[0ff8fb57]1289   
[5789230]1290    owl_fmtext_append_normal(&fm, "  Class     : ");
[df0d93a]1291    owl_fmtext_append_normal(&fm, owl_message_get_class(m));
1292    owl_fmtext_append_normal(&fm, "\n");
[5789230]1293    owl_fmtext_append_normal(&fm, "  Instance  : ");
[df0d93a]1294    owl_fmtext_append_normal(&fm, owl_message_get_instance(m));
1295    owl_fmtext_append_normal(&fm, "\n");
[5789230]1296    owl_fmtext_append_normal(&fm, "  Opcode    : ");
[df0d93a]1297    owl_fmtext_append_normal(&fm, owl_message_get_opcode(m));
1298    owl_fmtext_append_normal(&fm, "\n");
1299   
[5789230]1300    owl_fmtext_append_normal(&fm, "  Time      : ");
[df0d93a]1301    owl_fmtext_append_normal(&fm, owl_message_get_timestr(m));
1302    owl_fmtext_append_normal(&fm, "\n");
[09489b89]1303#ifdef HAVE_LIBZEPHYR
[d0d65df]1304    if (owl_message_is_direction_in(m)) {
[09489b89]1305      char *ptr, tmpbuff[1024];
1306      int i, j, fields, len;
1307
[d0d65df]1308      n=owl_message_get_notice(m);
[df0d93a]1309
[5789230]1310      owl_fmtext_append_normal(&fm, "  Kind      : ");
[d0d65df]1311      if (n->z_kind==UNSAFE) {
1312        owl_fmtext_append_normal(&fm, "UNSAFE\n");
1313      } else if (n->z_kind==UNACKED) {
1314        owl_fmtext_append_normal(&fm, "UNACKED\n");
1315      } else if (n->z_kind==ACKED) {
1316        owl_fmtext_append_normal(&fm, "ACKED\n");
1317      } else if (n->z_kind==HMACK) {
1318        owl_fmtext_append_normal(&fm, "HMACK\n");
1319      } else if (n->z_kind==HMCTL) {
1320        owl_fmtext_append_normal(&fm, "HMCTL\n");
1321      } else if (n->z_kind==SERVACK) {
1322        owl_fmtext_append_normal(&fm, "SERVACK\n");
1323      } else if (n->z_kind==SERVNAK) {
1324        owl_fmtext_append_normal(&fm, "SERVNACK\n");
1325      } else if (n->z_kind==CLIENTACK) {
1326        owl_fmtext_append_normal(&fm, "CLIENTACK\n");
1327      } else if (n->z_kind==STAT) {
1328        owl_fmtext_append_normal(&fm, "STAT\n");
1329      } else {
1330        owl_fmtext_append_normal(&fm, "ILLEGAL VALUE\n");
1331      }
[5789230]1332      owl_fmtext_append_normal(&fm, "  Host      : ");
[d0d65df]1333      owl_fmtext_append_normal(&fm, owl_message_get_hostname(m));
1334      owl_fmtext_append_normal(&fm, "\n");
[5789230]1335      sprintf(buff, "  Port      : %i\n", n->z_port);
[d0d65df]1336      owl_fmtext_append_normal(&fm, buff);
1337
[5789230]1338      owl_fmtext_append_normal(&fm,    "  Auth      : ");
[9381782]1339      owl_fmtext_append_normal(&fm, owl_zephyr_get_authstr(n));
1340      owl_fmtext_append_normal(&fm, "\n");
[7d4fbcd]1341
[5789230]1342      /* fix this */
1343      sprintf(buff, "  Checkd Ath: %i\n", n->z_checked_auth);
1344      sprintf(buff, "%s  Multi notc: %s\n", buff, n->z_multinotice);
1345      sprintf(buff, "%s  Num other : %i\n", buff, n->z_num_other_fields);
1346      sprintf(buff, "%s  Msg Len   : %i\n", buff, n->z_message_len);
[d0d65df]1347      owl_fmtext_append_normal(&fm, buff);
1348     
[5789230]1349      sprintf(buff, "  Fields    : %i\n", owl_zephyr_get_num_fields(n));
[d0d65df]1350      owl_fmtext_append_normal(&fm, buff);
1351     
1352      fields=owl_zephyr_get_num_fields(n);
1353      for (i=0; i<fields; i++) {
[5789230]1354        sprintf(buff, "  Field %i   : ", i+1);
[d0d65df]1355       
1356        ptr=owl_zephyr_get_field(n, i+1, &len);
1357        if (!ptr) break;
1358        if (len<30) {
1359          strncpy(tmpbuff, ptr, len);
1360          tmpbuff[len]='\0';
1361        } else {
1362          strncpy(tmpbuff, ptr, 30);
1363          tmpbuff[30]='\0';
1364          strcat(tmpbuff, "...");
1365        }
1366       
1367        for (j=0; j<strlen(tmpbuff); j++) {
1368          if (tmpbuff[j]=='\n') tmpbuff[j]='~';
1369          if (tmpbuff[j]=='\r') tmpbuff[j]='!';
1370        }
1371       
1372        strcat(buff, tmpbuff);
1373        strcat(buff, "\n");
1374        owl_fmtext_append_normal(&fm, buff);
1375      }
[5789230]1376      owl_fmtext_append_normal(&fm, "  Default Fm:");
[d0d65df]1377      owl_fmtext_append_normal(&fm, n->z_default_format);
[7d4fbcd]1378    }
[09489b89]1379#endif   
[7d4fbcd]1380  }
[0ff8fb57]1381
1382  if (owl_message_is_type_aim(m)) {
[5789230]1383    owl_fmtext_append_bold(&fm, "\nAIM Specific Information:\n");
[0ff8fb57]1384  }
[5789230]1385
1386  owl_fmtext_append_bold(&fm, "\nOwl Message Attributes:\n");
1387  owl_message_attributes_tofmtext(m, &attrfm);
1388  owl_fmtext_append_fmtext(&fm, &attrfm);
[d0d65df]1389 
1390  owl_function_popless_fmtext(&fm);
[5789230]1391  owl_fmtext_free(&fm);
1392  owl_fmtext_free(&attrfm);
[7d4fbcd]1393}
1394
[5639bf2]1395/* print the current message in a popup window.
1396 * Use the 'default' style regardless of whatever
1397 * style the user may be using
1398 */
[d54838d]1399void owl_function_curmsg_to_popwin()
1400{
[7d4fbcd]1401  owl_popwin *pw;
1402  owl_view *v;
1403  owl_message *m;
[5639bf2]1404  owl_style *s;
1405  owl_fmtext fm;
[7d4fbcd]1406
[5639bf2]1407  v=owl_global_get_current_view(&g);
1408  s=owl_global_get_style_by_name(&g, "default");
[7d4fbcd]1409  pw=owl_global_get_popwin(&g);
1410
[5eeea3b]1411  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
1412
1413  if (!m || owl_view_get_size(v)==0) {
[ec6ff52]1414    owl_function_error("No current message");
[7d4fbcd]1415    return;
1416  }
1417
[5639bf2]1418  owl_fmtext_init_null(&fm);
1419  owl_style_get_formattext(s, &fm, m);
1420
1421  owl_function_popless_fmtext(&fm);
1422  owl_fmtext_free(&fm);
[7d4fbcd]1423}
1424
[d54838d]1425void owl_function_page_curmsg(int step)
1426{
[7d4fbcd]1427  /* scroll down or up within the current message IF the message is truncated */
1428
1429  int offset, curmsg, lines;
1430  owl_view *v;
1431  owl_message *m;
1432
1433  offset=owl_global_get_curmsg_vert_offset(&g);
1434  v=owl_global_get_current_view(&g);
1435  curmsg=owl_global_get_curmsg(&g);
1436  m=owl_view_get_element(v, curmsg);
[5eeea3b]1437  if (!m || owl_view_get_size(v)==0) return;
[7d4fbcd]1438  lines=owl_message_get_numlines(m);
1439
1440  if (offset==0) {
1441    /* Bail if the curmsg isn't the last one displayed */
1442    if (curmsg != owl_mainwin_get_last_msg(owl_global_get_mainwin(&g))) {
[ec6ff52]1443      owl_function_error("The entire message is already displayed");
[7d4fbcd]1444      return;
1445    }
1446   
1447    /* Bail if we're not truncated */
1448    if (!owl_mainwin_is_curmsg_truncated(owl_global_get_mainwin(&g))) {
[ec6ff52]1449      owl_function_error("The entire message is already displayed");
[7d4fbcd]1450      return;
1451    }
1452  }
1453 
1454 
1455  /* don't scroll past the last line */
1456  if (step>0) {
1457    if (offset+step > lines-1) {
1458      owl_global_set_curmsg_vert_offset(&g, lines-1);
1459    } else {
1460      owl_global_set_curmsg_vert_offset(&g, offset+step);
1461    }
1462  }
1463
1464  /* would we be before the beginning of the message? */
1465  if (step<0) {
1466    if (offset+step<0) {
1467      owl_global_set_curmsg_vert_offset(&g, 0);
1468    } else {
1469      owl_global_set_curmsg_vert_offset(&g, offset+step);
1470    }
1471  }
1472 
1473  /* redisplay */
1474  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
1475  owl_global_set_needrefresh(&g);
1476}
1477
[d54838d]1478void owl_function_resize_typwin(int newsize)
1479{
[7d4fbcd]1480  owl_global_set_typwin_lines(&g, newsize);
1481  owl_function_resize();
1482}
1483
[d54838d]1484void owl_function_typwin_grow()
1485{
[7d4fbcd]1486  int i;
1487
1488  i=owl_global_get_typwin_lines(&g);
1489  owl_function_resize_typwin(i+1);
1490}
1491
[d54838d]1492void owl_function_typwin_shrink()
1493{
[7d4fbcd]1494  int i;
1495
1496  i=owl_global_get_typwin_lines(&g);
1497  if (i>2) {
1498    owl_function_resize_typwin(i-1);
1499  }
1500}
1501
[d54838d]1502void owl_function_mainwin_pagedown()
1503{
[7d4fbcd]1504  int i;
1505
1506  i=owl_mainwin_get_last_msg(owl_global_get_mainwin(&g));
1507  if (i<0) return;
[f2e36b5]1508  if (owl_mainwin_is_last_msg_truncated(owl_global_get_mainwin(&g))
1509      && (owl_global_get_curmsg(&g) < i)
1510      && (i>0)) {
1511    i--;
1512  }
[7d4fbcd]1513  owl_global_set_curmsg(&g, i);
1514  owl_function_nextmsg();
1515}
1516
[d54838d]1517void owl_function_mainwin_pageup()
1518{
[7d4fbcd]1519  owl_global_set_curmsg(&g, owl_global_get_topmsg(&g));
1520  owl_function_prevmsg();
1521}
1522
[d54838d]1523void owl_function_getsubs()
1524{
[09489b89]1525  char *buff;
[7d4fbcd]1526
[09489b89]1527  buff=owl_zephyr_getsubs();
[7d4fbcd]1528
[09489b89]1529  if (buff) {
1530    owl_function_popless_text(buff);
1531  } else {
1532    owl_function_popless_text("Error getting subscriptions");
[7d4fbcd]1533  }
[09489b89]1534           
[1c6c4d3]1535  owl_free(buff);
[7d4fbcd]1536}
1537
1538#define PABUFLEN 5000
[d54838d]1539void owl_function_printallvars()
1540{
[7d4fbcd]1541  char buff[PABUFLEN], *pos, *name;
1542  owl_list varnames;
1543  int i, numvarnames, rem;
1544
1545  pos = buff;
1546  pos += sprintf(pos, "%-20s = %s\n", "VARIABLE", "VALUE");
1547  pos += sprintf(pos, "%-20s   %s\n",  "--------", "-----");
1548  owl_variable_dict_get_names(owl_global_get_vardict(&g), &varnames);
1549  rem = (buff+PABUFLEN)-pos-1;
1550  numvarnames = owl_list_get_size(&varnames);
1551  for (i=0; i<numvarnames; i++) {
1552    name = owl_list_get_element(&varnames, i);
1553    if (name && name[0]!='_') {
1554      rem = (buff+PABUFLEN)-pos-1;   
1555      pos += snprintf(pos, rem, "\n%-20s = ", name);
1556      rem = (buff+PABUFLEN)-pos-1;   
1557      owl_variable_get_tostring(owl_global_get_vardict(&g), name, pos, rem);
1558      pos = buff+strlen(buff);
1559    }
1560  }
1561  rem = (buff+PABUFLEN)-pos-1;   
1562  snprintf(pos, rem, "\n");
1563  owl_variable_dict_namelist_free(&varnames);
1564 
1565  owl_function_popless_text(buff);
1566}
1567
[d54838d]1568void owl_function_show_variables()
1569{
[7d4fbcd]1570  owl_list varnames;
1571  owl_fmtext fm; 
1572  int i, numvarnames;
1573  char *varname;
1574
1575  owl_fmtext_init_null(&fm);
1576  owl_fmtext_append_bold(&fm, 
1577      "Variables: (use 'show variable <name>' for details)\n");
1578  owl_variable_dict_get_names(owl_global_get_vardict(&g), &varnames);
1579  numvarnames = owl_list_get_size(&varnames);
1580  for (i=0; i<numvarnames; i++) {
1581    varname = owl_list_get_element(&varnames, i);
1582    if (varname && varname[0]!='_') {
[aa2f33b3]1583      owl_variable_describe(owl_global_get_vardict(&g), varname, &fm);
[7d4fbcd]1584    }
1585  }
1586  owl_variable_dict_namelist_free(&varnames);
1587  owl_function_popless_fmtext(&fm);
1588  owl_fmtext_free(&fm);
1589}
1590
[d54838d]1591void owl_function_show_variable(char *name)
1592{
[7d4fbcd]1593  owl_fmtext fm; 
1594
1595  owl_fmtext_init_null(&fm);
1596  owl_variable_get_help(owl_global_get_vardict(&g), name, &fm);
1597  owl_function_popless_fmtext(&fm);
1598  owl_fmtext_free(&fm); 
1599}
1600
1601/* note: this applies to global message list, not to view.
1602 * If flag is 1, deletes.  If flag is 0, undeletes. */
[d54838d]1603void owl_function_delete_by_id(int id, int flag)
1604{
[7d4fbcd]1605  owl_messagelist *ml;
1606  owl_message *m;
1607  ml = owl_global_get_msglist(&g);
1608  m = owl_messagelist_get_by_id(ml, id);
1609  if (m) {
1610    if (flag == 1) {
1611      owl_message_mark_delete(m);
1612    } else if (flag == 0) {
1613      owl_message_unmark_delete(m);
1614    }
1615    owl_mainwin_redisplay(owl_global_get_mainwin(&g));
1616    owl_global_set_needrefresh(&g);
1617  } else {
[ec6ff52]1618    owl_function_error("No message with id %d: unable to mark for (un)delete",id);
[7d4fbcd]1619  }
1620}
1621
[d54838d]1622void owl_function_delete_automsgs()
1623{
[7d4fbcd]1624  /* mark for deletion all messages in the current view that match the
1625   * 'trash' filter */
1626
1627  int i, j, count;
1628  owl_message *m;
1629  owl_view *v;
1630  owl_filter *f;
1631
1632  /* get the trash filter */
1633  f=owl_global_get_filter(&g, "trash");
1634  if (!f) {
[ec6ff52]1635    owl_function_error("No trash filter defined");
[7d4fbcd]1636    return;
1637  }
1638
1639  v=owl_global_get_current_view(&g);
1640
1641  count=0;
1642  j=owl_view_get_size(v);
1643  for (i=0; i<j; i++) {
1644    m=owl_view_get_element(v, i);
1645    if (owl_filter_message_match(f, m)) {
1646      count++;
1647      owl_message_mark_delete(m);
1648    }
1649  }
1650  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
[1c6c4d3]1651  owl_function_makemsg("%i messages marked for deletion", count);
[7d4fbcd]1652  owl_global_set_needrefresh(&g);
1653}
1654
[d54838d]1655void owl_function_status()
1656{
[7d4fbcd]1657  char buff[5000];
1658  time_t start;
1659  int up, days, hours, minutes;
[a352335c]1660  owl_fmtext fm;
1661
1662  owl_fmtext_init_null(&fm);
[7d4fbcd]1663
1664  start=owl_global_get_starttime(&g);
1665
[a352335c]1666  owl_fmtext_append_normal(&fm, "Version: ");
1667  owl_fmtext_append_normal(&fm, OWL_VERSION_STRING);
1668  owl_fmtext_append_normal(&fm, "\n");
1669
1670  sprintf(buff, "Screen size: %i lines, %i columns\n", owl_global_get_lines(&g), owl_global_get_cols(&g));
1671  owl_fmtext_append_normal(&fm, buff);
1672
1673  owl_fmtext_append_normal(&fm, "Startup Arugments: ");
1674  owl_fmtext_append_normal(&fm, owl_global_get_startupargs(&g));
1675  owl_fmtext_append_normal(&fm, "\n");
1676  sprintf(buff, "Startup Time: %s", ctime(&start));
1677  owl_fmtext_append_normal(&fm, buff);
1678 
[7d4fbcd]1679
1680  up=owl_global_get_runtime(&g);
1681  days=up/86400;
1682  up-=days*86400;
1683  hours=up/3600;
1684  up-=hours*3600;
1685  minutes=up/60;
1686  up-=minutes*60;
[a352335c]1687  sprintf(buff, "Run Time: %i days %2.2i:%2.2i:%2.2i\n", days, hours, minutes, up);
1688  owl_fmtext_append_normal(&fm, buff);
[7d4fbcd]1689
1690  if (owl_global_get_hascolors(&g)) {
[a352335c]1691    sprintf(buff, "Color: Yes, %i color pairs.\n", owl_global_get_colorpairs(&g));
[7d4fbcd]1692  } else {
[a352335c]1693    sprintf(buff, "Color: No.\n");
[7d4fbcd]1694  }
[a352335c]1695  owl_fmtext_append_normal(&fm, buff);
[8262340]1696
[a352335c]1697  /*
[8262340]1698  sprintf(buff, "%sMemory Malloced: %i\n", buff, owl_global_get_malloced(&g));
1699  sprintf(buff, "%sMemory Freed: %i\n", buff, owl_global_get_freed(&g));
1700  sprintf(buff, "%sMemory In Use: %i\n", buff, owl_global_get_meminuse(&g));
[a352335c]1701  */
[8262340]1702
[a352335c]1703  owl_fmtext_append_normal(&fm, "\n");
1704  if (owl_global_is_aimloggedin(&g)) {
1705    owl_fmtext_append_normal(&fm, "AIM: logged in as ");
1706    owl_fmtext_append_normal(&fm, owl_global_get_aim_screenname(&g));
1707    owl_fmtext_append_normal(&fm, "\n");
1708  } else {
1709    owl_fmtext_append_normal(&fm, "AIM: not logged in\n");
1710  }
1711  if (owl_global_is_doaimevents(&g)) {
1712    owl_fmtext_append_normal(&fm, "AIM: processing events\n ");
1713  } else {
1714    owl_fmtext_append_normal(&fm, "AIM: not processing events\n ");
1715  }
1716
1717  owl_function_popless_fmtext(&fm);
1718  owl_fmtext_free(&fm);
[7d4fbcd]1719}
1720
[d54838d]1721void owl_function_show_term()
1722{
[7d4fbcd]1723  owl_fmtext fm;
1724  char buff[LINE];
1725
1726  owl_fmtext_init_null(&fm);
1727  sprintf(buff, "Terminal Lines: %i\nTerminal Columns: %i\n",
1728          owl_global_get_lines(&g),
1729          owl_global_get_cols(&g));
1730  owl_fmtext_append_normal(&fm, buff);
1731
1732  if (owl_global_get_hascolors(&g)) {
1733    owl_fmtext_append_normal(&fm, "Color: Yes\n");
1734    sprintf(buff, "Number of color pairs: %i\n", owl_global_get_colorpairs(&g));
1735    owl_fmtext_append_normal(&fm, buff);
1736    sprintf(buff, "Can change colors: %s\n", can_change_color() ? "yes" : "no");
1737    owl_fmtext_append_normal(&fm, buff);
1738  } else {
1739    owl_fmtext_append_normal(&fm, "Color: No\n");
1740  }
1741
1742  owl_function_popless_fmtext(&fm);
1743  owl_fmtext_free(&fm);
1744}
1745
[e7cc1c3]1746/* if type = 0 then normal reply.
1747 * if type = 1 then it's a reply to sender
1748 * if enter = 0 then allow the command to be edited
1749 * if enter = 1 then don't wait for editing
1750 */
[d54838d]1751void owl_function_reply(int type, int enter)
1752{
[e6449bc]1753  char *buff=NULL, *oldbuff;
[7d4fbcd]1754  owl_message *m;
1755  owl_filter *f;
1756 
1757  if (owl_view_get_size(owl_global_get_current_view(&g))==0) {
[ec6ff52]1758    owl_function_error("No message selected");
[7d4fbcd]1759  } else {
[e50cd56]1760    char *class, *inst, *to, *cc=NULL;
[7d4fbcd]1761   
1762    m=owl_view_get_element(owl_global_get_current_view(&g), owl_global_get_curmsg(&g));
[5eeea3b]1763    if (!m) {
[ec6ff52]1764      owl_function_error("No message selected");
[5eeea3b]1765      return;
1766    }
1767
[7d4fbcd]1768    /* first check if we catch the reply-lockout filter */
1769    f=owl_global_get_filter(&g, "reply-lockout");
1770    if (f) {
1771      if (owl_filter_message_match(f, m)) {
[ec6ff52]1772        owl_function_error("Sorry, replies to this message have been disabled by the reply-lockout filter");
[7d4fbcd]1773        return;
1774      }
1775    }
[4b464a4]1776
[d09e5a1]1777    /* admin */
1778    if (owl_message_is_type_admin(m)) {
[ec6ff52]1779      owl_function_error("You cannot reply to an admin message");
[d309eb3]1780      return;
1781    }
1782
[d09e5a1]1783    /* zephyr */
1784    if (owl_message_is_type_zephyr(m)) {
[e7cc1c3]1785      /* if it's a zephyr we sent, send it out the same way again */
[d09e5a1]1786      if (owl_message_is_direction_out(m)) {
1787        owl_function_zwrite_setup(owl_message_get_zwriteline(m));
1788        owl_global_set_buffercommand(&g, owl_message_get_zwriteline(m));
1789        return;
1790      }
[e7cc1c3]1791
1792      /* Special case a personal reply to a webzephyr user on a class */
[1d3e925]1793      if ((type==1) && !strcasecmp(owl_message_get_opcode(m), OWL_WEBZEPHYR_OPCODE)) {
1794        class=OWL_WEBZEPHYR_CLASS;
[e7cc1c3]1795        inst=owl_message_get_sender(m);
1796        to=OWL_WEBZEPHYR_PRINCIPAL;
[1d3e925]1797      } else if (!strcasecmp(owl_message_get_class(m), OWL_WEBZEPHYR_CLASS) && owl_message_is_loginout(m)) {
[f562355]1798        /* Special case LOGIN/LOGOUT notifications on class "webzephyr" */
[1d3e925]1799        class=OWL_WEBZEPHYR_CLASS;
[e7cc1c3]1800        inst=owl_message_get_instance(m);
1801        to=OWL_WEBZEPHYR_PRINCIPAL;
[f562355]1802      } else if (owl_message_is_loginout(m)) {
1803        /* Normal LOGIN/LOGOUT messages */
[7d4fbcd]1804        class="MESSAGE";
1805        inst="PERSONAL";
1806        to=owl_message_get_sender(m);
1807      } else if (type==1) {
[f562355]1808        /* Personal reply */
[7d4fbcd]1809        class="MESSAGE";
1810        inst="PERSONAL";
1811        to=owl_message_get_sender(m);
1812      } else {
[f562355]1813        /* General reply */
[7d4fbcd]1814        class=owl_message_get_class(m);
1815        inst=owl_message_get_instance(m);
1816        to=owl_message_get_recipient(m);
[e50cd56]1817        cc=owl_message_get_cc(m);
[7d4fbcd]1818        if (!strcmp(to, "") || !strcmp(to, "*")) {
1819          to="";
1820        } else if (to[0]=='@') {
1821          /* leave it, to get the realm */
1822        } else {
1823          to=owl_message_get_sender(m);
1824        }
1825      }
[d09e5a1]1826       
[7d4fbcd]1827      /* create the command line */
[9ceee9d]1828      if (!strcasecmp(owl_message_get_opcode(m), "CRYPT")) {
1829        buff=owl_strdup("zcrypt");
1830      } else {
1831        buff = owl_strdup("zwrite");
1832      }
[7d4fbcd]1833      if (strcasecmp(class, "message")) {
[e50cd56]1834        buff = owl_sprintf("%s -c %s%s%s", oldbuff=buff, owl_getquoting(class), class, owl_getquoting(class));
1835        owl_free(oldbuff);
[7d4fbcd]1836      }
1837      if (strcasecmp(inst, "personal")) {
[e50cd56]1838        buff = owl_sprintf("%s -i %s%s%s", oldbuff=buff, owl_getquoting(inst), inst, owl_getquoting(inst));
1839        owl_free(oldbuff);
[7d4fbcd]1840      }
1841      if (*to != '\0') {
[f9c43ae]1842        char *tmp, *oldtmp, *tmp2;
[601a9e0]1843        tmp=short_zuser(to);
[e50cd56]1844        if (cc) {
1845          tmp = owl_util_uniq(oldtmp=tmp, cc, "-");
1846          owl_free(oldtmp);
1847          buff = owl_sprintf("%s -C %s", oldbuff=buff, tmp);
1848          owl_free(oldbuff);
1849        } else {
[f9c43ae]1850          if (owl_global_is_smartstrip(&g)) {
1851            tmp2=tmp;
[7c8060d0]1852            tmp=owl_util_smartstripped_user(tmp2);
[f9c43ae]1853            owl_free(tmp2);
1854          }
[e50cd56]1855          buff = owl_sprintf("%s %s", oldbuff=buff, tmp);
1856          owl_free(oldbuff);
1857        }
[7d4fbcd]1858        owl_free(tmp);
1859      }
[e50cd56]1860      if (cc) owl_free(cc);
[d09e5a1]1861    }
[7d4fbcd]1862
[d09e5a1]1863    /* aim */
1864    if (owl_message_is_type_aim(m)) {
[440ce01]1865      if (owl_message_is_direction_out(m)) {
1866        buff=owl_sprintf("aimwrite %s", owl_message_get_recipient(m));
1867      } else {
1868        buff=owl_sprintf("aimwrite %s", owl_message_get_sender(m));
1869      }
[d09e5a1]1870    }
[440ce01]1871   
[d09e5a1]1872    if (enter) {
1873      owl_history *hist = owl_global_get_cmd_history(&g);
1874      owl_history_store(hist, buff);
1875      owl_history_reset(hist);
1876      owl_function_command_norv(buff);
1877    } else {
1878      owl_function_start_command(buff);
[7d4fbcd]1879    }
[d09e5a1]1880    owl_free(buff);
[7d4fbcd]1881  }
1882}
1883
[d54838d]1884void owl_function_zlocate(int argc, char **argv, int auth)
1885{
[2527615]1886  owl_fmtext fm;
1887  char *ptr, buff[LINE];
1888  int i;
1889
1890  owl_fmtext_init_null(&fm);
[7d4fbcd]1891
[2527615]1892  for (i=0; i<argc; i++) {
1893    ptr=long_zuser(argv[i]);
1894    owl_zephyr_zlocate(ptr, buff, auth);
1895    owl_fmtext_append_normal(&fm, buff);
1896    owl_free(ptr);
[7d4fbcd]1897  }
1898
[2527615]1899  owl_function_popless_fmtext(&fm);
1900  owl_fmtext_free(&fm);
[7d4fbcd]1901}
1902
[d54838d]1903void owl_function_start_command(char *line)
1904{
[7d4fbcd]1905  int i, j;
1906  owl_editwin *tw;
1907
1908  tw=owl_global_get_typwin(&g);
1909  owl_global_set_typwin_active(&g);
[10b866d]1910  owl_editwin_new_style(tw, OWL_EDITWIN_STYLE_ONELINE, 
1911                        owl_global_get_cmd_history(&g));
1912
[7d4fbcd]1913  owl_editwin_set_locktext(tw, "command: ");
1914  owl_global_set_needrefresh(&g);
1915
1916  j=strlen(line);
1917  for (i=0; i<j; i++) {
1918    owl_editwin_process_char(tw, line[i]);
1919  }
1920  owl_editwin_redisplay(tw, 0);
[cf83b7a]1921
1922  owl_context_set_editline(owl_global_get_context(&g), tw);
1923  owl_function_activate_keymap("editline");
1924   
1925}
1926
1927void owl_function_start_question(char *line)
1928{
1929  owl_editwin *tw;
1930
1931  tw=owl_global_get_typwin(&g);
1932  owl_global_set_typwin_active(&g);
1933  owl_editwin_new_style(tw, OWL_EDITWIN_STYLE_ONELINE, owl_global_get_cmd_history(&g));
1934
1935  owl_editwin_set_locktext(tw, line);
1936  owl_global_set_needrefresh(&g);
1937
1938  owl_editwin_redisplay(tw, 0);
1939
1940  owl_context_set_editresponse(owl_global_get_context(&g), tw);
1941  owl_function_activate_keymap("editresponse");
[7d4fbcd]1942}
1943
[453bd70]1944void owl_function_start_password(char *line)
1945{
1946  owl_editwin *tw;
1947
1948  tw=owl_global_get_typwin(&g);
1949  owl_global_set_typwin_active(&g);
1950  owl_editwin_new_style(tw, OWL_EDITWIN_STYLE_ONELINE, owl_global_get_cmd_history(&g));
1951  owl_editwin_set_echochar(tw, '*');
1952
1953  owl_editwin_set_locktext(tw, line);
1954  owl_global_set_needrefresh(&g);
1955
1956  owl_editwin_redisplay(tw, 0);
1957
1958  owl_context_set_editresponse(owl_global_get_context(&g), tw);
1959  owl_function_activate_keymap("editresponse");
1960}
1961
[d54838d]1962char *owl_function_exec(int argc, char **argv, char *buff, int type)
1963{
[7d4fbcd]1964  /* if type == 1 display in a popup
1965   * if type == 2 display an admin messages
1966   * if type == 0 return output
1967   * else display in a popup
1968   */
1969  char *newbuff, *redirect = " 2>&1 < /dev/null";
1970  char *out, buff2[1024];
1971  int size;
1972  FILE *p;
1973
1974  if (argc<2) {
[ec6ff52]1975    owl_function_error("Wrong number of arguments to the exec command");
[7d4fbcd]1976    return NULL;
1977  }
1978
1979  buff = skiptokens(buff, 1);
1980  newbuff = owl_malloc(strlen(buff)+strlen(redirect)+1);
1981  strcpy(newbuff, buff);
1982  strcat(newbuff, redirect);
1983
1984  p=popen(newbuff, "r");
1985  out=owl_malloc(1024);
1986  size=1024;
1987  strcpy(out, "");
1988  while (fgets(buff2, 1024, p)!=NULL) {
1989    size+=1024;
1990    out=owl_realloc(out, size);
1991    strcat(out, buff2);
1992  }
1993  pclose(p);
1994
1995  if (type==1) {
1996    owl_function_popless_text(out);
1997  } else if (type==0) {
1998    return out;
1999  } else if (type==2) {
2000    owl_function_adminmsg(buff, out);
2001  } else {
2002    owl_function_popless_text(out);
2003  }
2004  owl_free(out);
2005  return NULL;
2006}
2007
[d54838d]2008char *owl_function_perl(int argc, char **argv, char *buff, int type)
2009{
[7d4fbcd]2010  /* if type == 1 display in a popup
2011   * if type == 2 display an admin messages
2012   * if type == 0 return output
2013   * else display in a popup
2014   */
2015  char *perlout;
2016
2017  if (argc<2) {
[ec6ff52]2018    owl_function_error("Wrong number of arguments to perl command");
[7d4fbcd]2019    return NULL;
2020  }
2021
2022  /* consume first token (argv[0]) */
2023  buff = skiptokens(buff, 1);
2024
[f1e629d]2025  perlout = owl_perlconfig_execute(buff);
[7d4fbcd]2026  if (perlout) { 
2027    if (type==1) {
2028      owl_function_popless_text(perlout);
2029    } else if (type==2) {
2030      owl_function_adminmsg(buff, perlout);
2031    } else if (type==0) {
2032      return perlout;
2033    } else {
2034      owl_function_popless_text(perlout);
2035    }
2036    owl_free(perlout);
2037  }
2038  return NULL;
2039}
2040
[c3ab155]2041#if 0
2042void owl_function_change_view_old(char *filtname)
[d54838d]2043{
[7d4fbcd]2044  owl_view *v;
2045  owl_filter *f;
[f9c43ae]2046  int curid=-1, newpos, curmsg;
2047  owl_message *curm=NULL;
[7d4fbcd]2048
2049  v=owl_global_get_current_view(&g);
[f9c43ae]2050  curmsg=owl_global_get_curmsg(&g);
2051  if (curmsg==-1) {
2052    owl_function_debugmsg("Hit the curmsg==-1 case in change_view");
2053  } else {
2054    curm=owl_view_get_element(v, curmsg);
2055    if (curm) {
2056      curid=owl_message_get_id(curm);
2057      owl_view_save_curmsgid(v, curid);
2058    }
[59cf91c]2059  }
2060
[f9c43ae]2061  /* grab the filter */;
[7d4fbcd]2062  f=owl_global_get_filter(&g, filtname);
2063  if (!f) {
[ec6ff52]2064    owl_function_error("Unknown filter %s", filtname);
[7d4fbcd]2065    return;
2066  }
2067
[f9c43ae]2068  /* free the existing view and create a new one based on the filter */
[7d4fbcd]2069  owl_view_free(v);
2070  owl_view_create(v, f);
2071
[f9c43ae]2072  /* Figure out what to set the current message to.
2073   * - If the view we're leaving has messages in it, go to the closest message
2074   *   to the last message pointed to in that view.
2075   * - If the view we're leaving is empty, try to restore the position
2076   *   from the last time we were in the new view.  */
[59cf91c]2077  if (curm) {
2078    newpos = owl_view_get_nearest_to_msgid(v, curid);
2079  } else {
2080    newpos = owl_view_get_nearest_to_saved(v);
2081  }
2082
2083  owl_global_set_curmsg(&g, newpos);
2084
[f9c43ae]2085  owl_function_calculate_topmsg(OWL_DIRECTION_DOWNWARDS);
[7d4fbcd]2086  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
[f9c43ae]2087  owl_global_set_direction_downwards(&g);
[7d4fbcd]2088}
[c3ab155]2089#endif
2090
2091void owl_function_change_view(char *filtname)
2092{
2093  owl_view *v;
2094  owl_filter *f;
2095  int curid=-1, newpos, curmsg;
2096  owl_message *curm=NULL;
2097
2098  v=owl_global_get_current_view(&g);
2099
2100  curmsg=owl_global_get_curmsg(&g);
2101  if (curmsg==-1) {
2102    owl_function_debugmsg("Hit the curmsg==-1 case in change_view");
2103  } else {
2104    curm=owl_view_get_element(v, curmsg);
2105    if (curm) {
2106      curid=owl_message_get_id(curm);
2107      owl_view_save_curmsgid(v, curid);
2108    }
2109  }
2110
2111  f=owl_global_get_filter(&g, filtname);
2112  if (!f) {
[ec6ff52]2113    owl_function_error("Unknown filter %s", filtname);
[c3ab155]2114    return;
2115  }
2116
2117  owl_view_new_filter(v, f);
2118
2119  /* Figure out what to set the current message to.
2120   * - If the view we're leaving has messages in it, go to the closest message
2121   *   to the last message pointed to in that view.
2122   * - If the view we're leaving is empty, try to restore the position
2123   *   from the last time we were in the new view.  */
2124  if (curm) {
2125    newpos = owl_view_get_nearest_to_msgid(v, curid);
2126  } else {
2127    newpos = owl_view_get_nearest_to_saved(v);
2128  }
2129
2130  owl_global_set_curmsg(&g, newpos);
2131  owl_function_calculate_topmsg(OWL_DIRECTION_DOWNWARDS);
2132  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2133  owl_global_set_direction_downwards(&g);
2134}
[7d4fbcd]2135
[d54838d]2136void owl_function_create_filter(int argc, char **argv)
2137{
[7d4fbcd]2138  owl_filter *f;
2139  owl_view *v;
2140  int ret, inuse=0;
2141
2142  if (argc < 2) {
[ec6ff52]2143    owl_function_error("Wrong number of arguments to filter command");
[7d4fbcd]2144    return;
2145  }
2146
2147  v=owl_global_get_current_view(&g);
2148
2149  /* don't touch the all filter */
2150  if (!strcmp(argv[1], "all")) {
[ec6ff52]2151    owl_function_error("You may not change the 'all' filter.");
[7d4fbcd]2152    return;
2153  }
2154
2155  /* deal with the case of trying change the filter color */
2156  if (argc==4 && !strcmp(argv[2], "-c")) {
2157    f=owl_global_get_filter(&g, argv[1]);
2158    if (!f) {
[ec6ff52]2159      owl_function_error("The filter '%s' does not exist.", argv[1]);
[7d4fbcd]2160      return;
2161    }
2162    owl_filter_set_color(f, owl_util_string_to_color(argv[3]));
2163    owl_global_set_needrefresh(&g);
2164    owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2165    return;
2166  }
2167
2168  /* create the filter and check for errors */
2169  f=owl_malloc(sizeof(owl_filter));
2170  ret=owl_filter_init(f, argv[1], argc-2, argv+2);
2171  if (ret==-1) {
2172    owl_free(f);
[ec6ff52]2173    owl_function_error("Invalid filter syntax");
[7d4fbcd]2174    return;
2175  }
2176
2177  /* if the named filter is in use by the current view, remember it */
2178  if (!strcmp(owl_view_get_filtname(v), argv[1])) {
2179    inuse=1;
2180  }
2181
2182  /* if the named filter already exists, nuke it */
2183  if (owl_global_get_filter(&g, argv[1])) {
2184    owl_global_remove_filter(&g, argv[1]);
2185  }
2186
2187  /* add the filter */
2188  owl_global_add_filter(&g, f);
2189
2190  /* if it was in use by the current view then update */
2191  if (inuse) {
2192    owl_function_change_view(argv[1]);
2193  }
2194  owl_global_set_needrefresh(&g);
2195  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2196}
2197
[d54838d]2198void owl_function_show_filters()
2199{
[7d4fbcd]2200  owl_list *l;
2201  owl_filter *f;
2202  int i, j;
2203  owl_fmtext fm;
2204
2205  owl_fmtext_init_null(&fm);
2206
2207  l=owl_global_get_filterlist(&g);
2208  j=owl_list_get_size(l);
2209
2210  owl_fmtext_append_bold(&fm, "Filters:\n");
2211
2212  for (i=0; i<j; i++) {
2213    f=owl_list_get_element(l, i);
2214    owl_fmtext_append_normal(&fm, "   ");
2215    if (owl_global_get_hascolors(&g)) {
2216      owl_fmtext_append_normal_color(&fm, owl_filter_get_name(f), owl_filter_get_color(f));
2217    } else {
2218      owl_fmtext_append_normal(&fm, owl_filter_get_name(f));
2219    }
2220    owl_fmtext_append_normal(&fm, "\n");
2221  }
2222  owl_function_popless_fmtext(&fm);
2223  owl_fmtext_free(&fm);
2224}
2225
[d54838d]2226void owl_function_show_filter(char *name)
2227{
[7d4fbcd]2228  owl_filter *f;
2229  char buff[5000];
2230
2231  f=owl_global_get_filter(&g, name);
2232  if (!f) {
[ec6ff52]2233    owl_function_error("There is no filter named %s", name);
[7d4fbcd]2234    return;
2235  }
2236  owl_filter_print(f, buff);
2237  owl_function_popless_text(buff);
2238}
2239
[d54838d]2240void owl_function_show_zpunts()
2241{
[7d4fbcd]2242  owl_filter *f;
2243  owl_list *fl;
2244  char buff[5000];
2245  owl_fmtext fm;
2246  int i, j;
2247
2248  owl_fmtext_init_null(&fm);
2249
2250  fl=owl_global_get_puntlist(&g);
2251  j=owl_list_get_size(fl);
2252  owl_fmtext_append_bold(&fm, "Active zpunt filters:\n");
2253
2254  for (i=0; i<j; i++) {
2255    f=owl_list_get_element(fl, i);
2256    owl_filter_print(f, buff);
2257    owl_fmtext_append_normal(&fm, buff);
2258  }
2259  owl_function_popless_fmtext(&fm);
2260  owl_fmtext_free(&fm);
2261}
2262
[3abf28b]2263/* Create a filter for a class, instance if one doesn't exist.  If
2264 * instance is NULL then catch all messgaes in the class.  Returns the
2265 * name of the filter, which the caller must free.
2266 */
2267char *owl_function_classinstfilt(char *class, char *instance) 
[d54838d]2268{
[7d4fbcd]2269  owl_list *fl;
2270  owl_filter *f;
2271  char *argbuff, *filtname;
[d54838d]2272  char *tmpclass, *tmpinstance = NULL;
[7d4fbcd]2273  int len;
2274
2275  fl=owl_global_get_filterlist(&g);
2276
2277  /* name for the filter */
2278  len=strlen(class)+30;
2279  if (instance) len+=strlen(instance);
2280  filtname=owl_malloc(len);
2281  if (!instance) {
2282    sprintf(filtname, "class-%s", class);
2283  } else {
2284    sprintf(filtname, "class-%s-instance-%s", class, instance);
2285  }
[ed2412d]2286  /* downcase it */
[7d4fbcd]2287  downstr(filtname);
[ed2412d]2288  /* turn spaces into hyphens */
2289  owl_util_tr(filtname, ' ', '.');
2290 
[7d4fbcd]2291  /* if it already exists then go with it.  This lets users override */
2292  if (owl_global_get_filter(&g, filtname)) {
[ed2412d]2293    return(filtname);
[7d4fbcd]2294  }
2295
2296  /* create the new filter */
2297  argbuff=owl_malloc(len+20);
[ed2412d]2298  tmpclass=owl_strdup(class);
2299  owl_util_tr(tmpclass, ' ', '.');
2300  if (instance) {
2301    tmpinstance=owl_strdup(instance);
2302    owl_util_tr(tmpinstance, ' ', '.');
2303  }
2304  sprintf(argbuff, "( class ^%s$ )", tmpclass);
[d54838d]2305  if (tmpinstance) {
[ed2412d]2306    sprintf(argbuff, "%s and ( instance ^%s$ )", argbuff, tmpinstance);
[7d4fbcd]2307  }
[ed2412d]2308  owl_free(tmpclass);
[d54838d]2309  if (tmpinstance) owl_free(tmpinstance);
[7d4fbcd]2310
2311  f=owl_malloc(sizeof(owl_filter));
2312  owl_filter_init_fromstring(f, filtname, argbuff);
2313
2314  /* add it to the global list */
2315  owl_global_add_filter(&g, f);
2316
2317  owl_free(argbuff);
[ed2412d]2318  return(filtname);
[7d4fbcd]2319}
2320
[3abf28b]2321/* Create a filter for personal zephyrs to or from the specified
2322 * zephyr user.  Includes login/logout notifications for the user.
2323 * The name of the filter will be 'user-<user>'.  If a filter already
2324 * exists with this name, no new filter will be created.  This allows
2325 * the configuration to override this function.  Returns the name of
2326 * the filter, which the caller must free.
2327 */
2328char *owl_function_zuserfilt(char *user)
[d54838d]2329{
[7d4fbcd]2330  owl_filter *f;
2331  char *argbuff, *longuser, *shortuser, *filtname;
2332
2333  /* stick the local realm on if it's not there */
[4b464a4]2334  longuser=long_zuser(user);
2335  shortuser=short_zuser(user);
[7d4fbcd]2336
2337  /* name for the filter */
2338  filtname=owl_malloc(strlen(shortuser)+20);
2339  sprintf(filtname, "user-%s", shortuser);
2340
2341  /* if it already exists then go with it.  This lets users override */
2342  if (owl_global_get_filter(&g, filtname)) {
[3abf28b]2343    return(owl_strdup(filtname));
[7d4fbcd]2344  }
2345
2346  /* create the new-internal filter */
2347  f=owl_malloc(sizeof(owl_filter));
2348
[4b464a4]2349  argbuff=owl_malloc(strlen(longuser)+1000);
2350  sprintf(argbuff, "( type ^zephyr$ and ( class ^message$ and instance ^personal$ and ");
2351  sprintf(argbuff, "%s ( ( direction ^in$ and sender ^%s$ ) or ( direction ^out$ and recipient ^%s$ ) ) )", argbuff, longuser, longuser);
2352  sprintf(argbuff, "%s or ( ( class ^login$ ) and ( sender ^%s$ ) ) )", argbuff, longuser);
[7d4fbcd]2353
2354  owl_filter_init_fromstring(f, filtname, argbuff);
2355
2356  /* add it to the global list */
2357  owl_global_add_filter(&g, f);
2358
2359  /* free stuff */
2360  owl_free(argbuff);
2361  owl_free(longuser);
2362  owl_free(shortuser);
[7360fab]2363
[ed2412d]2364  return(filtname);
[7d4fbcd]2365}
2366
[3abf28b]2367/* Create a filter for AIM IM messages to or from the specified
2368 * screenname.  The name of the filter will be 'aimuser-<user>'.  If a
2369 * filter already exists with this name, no new filter will be
2370 * created.  This allows the configuration to override this function.
2371 * Returns the name of the filter, which the caller must free.
2372 */
2373char *owl_function_aimuserfilt(char *user)
2374{
2375  owl_filter *f;
2376  char *argbuff, *filtname;
2377
2378  /* name for the filter */
2379  filtname=owl_malloc(strlen(user)+40);
2380  sprintf(filtname, "aimuser-%s", user);
2381
2382  /* if it already exists then go with it.  This lets users override */
2383  if (owl_global_get_filter(&g, filtname)) {
2384    return(owl_strdup(filtname));
2385  }
2386
2387  /* create the new-internal filter */
2388  f=owl_malloc(sizeof(owl_filter));
2389
2390  argbuff=owl_malloc(1000);
2391  sprintf(argbuff,
2392          "( type ^aim$ and ( ( sender ^%s$ and recipient ^%s$ ) or ( sender ^%s$ and recipient ^%s$ ) ) )",
2393          user, owl_global_get_aim_screenname(&g), owl_global_get_aim_screenname(&g), user);
2394
2395  owl_filter_init_fromstring(f, filtname, argbuff);
2396
2397  /* add it to the global list */
2398  owl_global_add_filter(&g, f);
2399
2400  /* free stuff */
2401  owl_free(argbuff);
2402
2403  return(filtname);
2404}
2405
2406char *owl_function_typefilt(char *type)
[d54838d]2407{
[f73e519]2408  owl_filter *f;
2409  char *argbuff, *filtname;
2410
2411  /* name for the filter */
2412  filtname=owl_sprintf("type-%s", type);
2413
2414  /* if it already exists then go with it.  This lets users override */
2415  if (owl_global_get_filter(&g, filtname)) {
2416    return filtname;
2417  }
2418
2419  /* create the new-internal filter */
2420  f=owl_malloc(sizeof(owl_filter));
2421
2422  argbuff = owl_sprintf("type ^%s$", type);
2423
2424  owl_filter_init_fromstring(f, filtname, argbuff);
2425
2426  /* add it to the global list */
2427  owl_global_add_filter(&g, f);
2428
2429  /* free stuff */
2430  owl_free(argbuff);
2431
2432  return filtname;
2433}
2434
[7d4fbcd]2435/* If flag is 1, marks for deletion.  If flag is 0,
2436 * unmarks for deletion. */
[d54838d]2437void owl_function_delete_curview_msgs(int flag)
2438{
[7d4fbcd]2439  owl_view *v;
2440  int i, j;
2441
2442  v=owl_global_get_current_view(&g);
2443  j=owl_view_get_size(v);
2444  for (i=0; i<j; i++) {
2445    if (flag == 1) {
2446      owl_message_mark_delete(owl_view_get_element(v, i));
2447    } else if (flag == 0) {
2448      owl_message_unmark_delete(owl_view_get_element(v, i));
2449    }
2450  }
2451
2452  owl_function_makemsg("%i messages marked for %sdeletion", j, flag?"":"un");
2453
2454  owl_mainwin_redisplay(owl_global_get_mainwin(&g)); 
2455}
2456
[3abf28b]2457/* Create a filter based on the current message.  Returns the name of
2458 * a filter or null.  The caller must free this name.
2459 *
2460 * if the curmsg is a personal zephyr return a filter name
2461 *    to the zephyr converstaion with that user.
2462 * If the curmsg is a zephyr class message, instance foo, recip *,
2463 *    return a filter name to the class, inst.
2464 * If the curmsg is a zephyr class message and type==0 then
2465 *    return a filter name for just the class.
2466 * If the curmsg is a zephyr class message and type==1 then
2467 *    return a filter name for the class and instance.
2468 * If the curmsg is a personal AIM message returna  filter
2469 *    name to the AIM conversation with that user
2470 */
[d54838d]2471char *owl_function_smartfilter(int type)
2472{
[7d4fbcd]2473  owl_view *v;
2474  owl_message *m;
[4b464a4]2475  char *zperson, *filtname=NULL;
[7d4fbcd]2476 
2477  v=owl_global_get_current_view(&g);
2478  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
2479
[5eeea3b]2480  if (!m || owl_view_get_size(v)==0) {
[ec6ff52]2481    owl_function_error("No message selected\n");
[4b464a4]2482    return(NULL);
[7d4fbcd]2483  }
2484
[f73e519]2485  /* very simple handling of admin messages for now */
[4b464a4]2486  if (owl_message_is_type_admin(m)) {
[3abf28b]2487    return(owl_function_typefilt("admin"));
2488  }
2489
2490  /* aim messages */
2491  if (owl_message_is_type_aim(m)) {
2492    if (owl_message_is_direction_in(m)) {
2493      filtname=owl_function_aimuserfilt(owl_message_get_sender(m));
2494    } else if (owl_message_is_direction_out(m)) {
2495      filtname=owl_function_aimuserfilt(owl_message_get_recipient(m));
2496    }
2497    return(filtname);
[7d4fbcd]2498  }
2499
[4b464a4]2500  /* narrow personal and login messages to the sender or recip as appropriate */
[5789230]2501  if (owl_message_is_personal(m) || owl_message_is_loginout(m)) {
[4b464a4]2502    if (owl_message_is_type_zephyr(m)) {
2503      if (owl_message_is_direction_in(m)) {
2504        zperson=short_zuser(owl_message_get_sender(m));
2505      } else {
2506        zperson=short_zuser(owl_message_get_recipient(m));
2507      }
[3abf28b]2508      filtname=owl_function_zuserfilt(zperson);
[4b464a4]2509      owl_free(zperson);
2510      return(filtname);
[7d4fbcd]2511    }
[4b464a4]2512    return(NULL);
[7d4fbcd]2513  }
2514
2515  /* narrow class MESSAGE, instance foo, recip * messages to class, inst */
[3abf28b]2516  if (!strcasecmp(owl_message_get_class(m), "message") && !owl_message_is_personal(m)) {
2517    filtname=owl_function_classinstfilt(owl_message_get_class(m), owl_message_get_instance(m));
[4b464a4]2518    return(filtname);
[7d4fbcd]2519  }
2520
2521  /* otherwise narrow to the class */
2522  if (type==0) {
[3abf28b]2523    filtname=owl_function_classinstfilt(owl_message_get_class(m), NULL);
[7d4fbcd]2524  } else if (type==1) {
[3abf28b]2525    filtname=owl_function_classinstfilt(owl_message_get_class(m), owl_message_get_instance(m));
[7d4fbcd]2526  }
[4b464a4]2527  return(filtname);
[7d4fbcd]2528}
2529
[d54838d]2530void owl_function_smartzpunt(int type)
2531{
[d36f2cb]2532  /* Starts a zpunt command based on the current class,instance pair.
2533   * If type=0, uses just class.  If type=1, uses instance as well. */
2534  owl_view *v;
2535  owl_message *m;
2536  char *cmd, *cmdprefix, *mclass, *minst;
2537 
2538  v=owl_global_get_current_view(&g);
2539  m=owl_view_get_element(v, owl_global_get_curmsg(&g));
2540
[5eeea3b]2541  if (!m || owl_view_get_size(v)==0) {
[ec6ff52]2542    owl_function_error("No message selected\n");
[d36f2cb]2543    return;
2544  }
2545
2546  /* for now we skip admin messages. */
[4b464a4]2547  if (owl_message_is_type_admin(m)
[5789230]2548      || owl_message_is_loginout(m)
[4b464a4]2549      || !owl_message_is_type_zephyr(m)) {
[ec6ff52]2550    owl_function_error("smartzpunt doesn't support this message type.");
[d36f2cb]2551    return;
2552  }
2553
2554  mclass = owl_message_get_class(m);
2555  minst = owl_message_get_instance(m);
2556  if (!mclass || !*mclass || *mclass==' '
2557      || (!strcasecmp(mclass, "message") && !strcasecmp(minst, "personal"))
2558      || (type && (!minst || !*minst|| *minst==' '))) {
[ec6ff52]2559    owl_function_error("smartzpunt can't safely do this for <%s,%s>",
[d36f2cb]2560                         mclass, minst);
2561  } else {
2562    cmdprefix = "start-command zpunt ";
2563    cmd = owl_malloc(strlen(cmdprefix)+strlen(mclass)+strlen(minst)+3);
2564    strcpy(cmd, cmdprefix);
2565    strcat(cmd, mclass);
2566    if (type) {
2567      strcat(cmd, " ");
2568      strcat(cmd, minst);
2569    } else {
2570      strcat(cmd, " *");
2571    }
2572    owl_function_command(cmd);
2573    owl_free(cmd);
2574  }
2575}
2576
[d54838d]2577void owl_function_color_current_filter(char *color)
2578{
[7d4fbcd]2579  owl_filter *f;
2580  char *name;
2581
2582  name=owl_view_get_filtname(owl_global_get_current_view(&g));
2583  f=owl_global_get_filter(&g, name);
2584  if (!f) {
[ec6ff52]2585    owl_function_error("Unknown filter");
[7d4fbcd]2586    return;
2587  }
2588
2589  /* don't touch the all filter */
2590  if (!strcmp(name, "all")) {
[ec6ff52]2591    owl_function_error("You may not change the 'all' filter.");
[7d4fbcd]2592    return;
2593  }
2594
2595  /* deal with the case of trying change the filter color */
2596  owl_filter_set_color(f, owl_util_string_to_color(color));
2597  owl_global_set_needrefresh(&g);
2598  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2599}
2600
[d54838d]2601void owl_function_show_colors()
2602{
[7d4fbcd]2603  owl_fmtext fm;
2604
2605  owl_fmtext_init_null(&fm);
2606  owl_fmtext_append_normal_color(&fm, "default\n", OWL_COLOR_DEFAULT);
2607  owl_fmtext_append_normal_color(&fm, "red\n", OWL_COLOR_RED);
2608  owl_fmtext_append_normal_color(&fm, "green\n", OWL_COLOR_GREEN);
2609  owl_fmtext_append_normal_color(&fm, "yellow\n", OWL_COLOR_YELLOW);
2610  owl_fmtext_append_normal_color(&fm, "blue\n", OWL_COLOR_BLUE);
2611  owl_fmtext_append_normal_color(&fm, "magenta\n", OWL_COLOR_MAGENTA);
2612  owl_fmtext_append_normal_color(&fm, "cyan\n", OWL_COLOR_CYAN);
2613  owl_fmtext_append_normal_color(&fm, "white\n", OWL_COLOR_WHITE);
2614
2615  owl_function_popless_fmtext(&fm);
2616  owl_fmtext_free(&fm);
2617}
2618
[5bb6c21]2619/* add the given class, inst, recip to the punt list for filtering.
2620 *   if direction==0 then punt
2621 *   if direction==1 then unpunt
2622 */
[d54838d]2623void owl_function_zpunt(char *class, char *inst, char *recip, int direction)
2624{
[7d4fbcd]2625  owl_filter *f;
2626  owl_list *fl;
2627  char *buff;
2628  int ret, i, j;
2629
2630  fl=owl_global_get_puntlist(&g);
2631
2632  /* first, create the filter */
2633  f=malloc(sizeof(owl_filter));
2634  buff=malloc(strlen(class)+strlen(inst)+strlen(recip)+100);
[5bb6c21]2635  strcpy(buff, "class");
2636  if (!strcmp(class, "*")) {
2637    strcat(buff, " .*");
2638  } else {
2639    sprintf(buff, "%s ^%s$", buff, class);
2640  }
2641  if (!strcmp(inst, "*")) {
2642    strcat(buff, " and instance .*");
[7d4fbcd]2643  } else {
[5bb6c21]2644    sprintf(buff, "%s and instance ^%s$", buff, inst);
[7d4fbcd]2645  }
[5bb6c21]2646  if (strcmp(recip, "*")) {
2647    sprintf(buff, "%s and recipient ^%s$", buff, recip);
2648  }
2649 
[7d4fbcd]2650  owl_function_debugmsg("About to filter %s", buff);
2651  ret=owl_filter_init_fromstring(f, "punt-filter", buff);
2652  owl_free(buff);
2653  if (ret) {
[ec6ff52]2654    owl_function_error("Error creating filter for zpunt");
[7d4fbcd]2655    owl_filter_free(f);
2656    return;
2657  }
2658
2659  /* Check for an identical filter */
2660  j=owl_list_get_size(fl);
2661  for (i=0; i<j; i++) {
2662    if (owl_filter_equiv(f, owl_list_get_element(fl, i))) {
2663      /* if we're punting, then just silently bow out on this duplicate */
2664      if (direction==0) {
2665        owl_filter_free(f);
2666        return;
2667      }
2668
2669      /* if we're unpunting, then remove this filter from the puntlist */
2670      if (direction==1) {
2671        owl_filter_free(owl_list_get_element(fl, i));
2672        owl_list_remove_element(fl, i);
2673        return;
2674      }
2675    }
2676  }
2677
2678  /* If we're punting, add the filter to the global punt list */
2679  if (direction==0) {
2680    owl_list_append_element(fl, f);
2681  }
2682}
2683
[d54838d]2684void owl_function_activate_keymap(char *keymap)
2685{
[7d4fbcd]2686  if (!owl_keyhandler_activate(owl_global_get_keyhandler(&g), keymap)) {
[ec6ff52]2687    owl_function_error("Unable to activate keymap '%s'", keymap);
[7d4fbcd]2688  }
2689}
2690
[d54838d]2691void owl_function_show_keymaps()
2692{
[7d4fbcd]2693  owl_list l;
2694  owl_fmtext fm;
[1aee7d9]2695  owl_keymap *km;
2696  owl_keyhandler *kh;
2697  int i, numkm;
2698  char *kmname;
[7d4fbcd]2699
[1aee7d9]2700  kh = owl_global_get_keyhandler(&g);
[7d4fbcd]2701  owl_fmtext_init_null(&fm);
2702  owl_fmtext_append_bold(&fm, "Keymaps:   ");
2703  owl_fmtext_append_normal(&fm, "(use 'show keymap <name>' for details)\n");
[1aee7d9]2704  owl_keyhandler_get_keymap_names(kh, &l);
[7d4fbcd]2705  owl_fmtext_append_list(&fm, &l, "\n", owl_function_keymap_summary);
2706  owl_fmtext_append_normal(&fm, "\n");
[1aee7d9]2707
2708  numkm = owl_list_get_size(&l);
2709  for (i=0; i<numkm; i++) {
2710    kmname = owl_list_get_element(&l, i);
2711    km = owl_keyhandler_get_keymap(kh, kmname);
2712    owl_fmtext_append_bold(&fm, "\n\n----------------------------------------------------------------------------------------------------\n\n");
2713    owl_keymap_get_details(km, &fm);   
2714  }
2715  owl_fmtext_append_normal(&fm, "\n");
2716 
[7d4fbcd]2717  owl_function_popless_fmtext(&fm);
2718  owl_keyhandler_keymap_namelist_free(&l);
2719  owl_fmtext_free(&fm);
2720}
2721
[d54838d]2722char *owl_function_keymap_summary(void *name)
2723{
[7d4fbcd]2724  owl_keymap *km
2725    = owl_keyhandler_get_keymap(owl_global_get_keyhandler(&g), name);
2726  if (km) return owl_keymap_summary(km);
2727  else return(NULL);
2728}
2729
2730/* TODO: implement for real */
[d54838d]2731void owl_function_show_keymap(char *name)
2732{
[1fd0b25]2733  owl_fmtext fm;
[7d4fbcd]2734  owl_keymap *km;
2735
2736  owl_fmtext_init_null(&fm);
2737  km = owl_keyhandler_get_keymap(owl_global_get_keyhandler(&g), name);
2738  if (km) {
2739    owl_keymap_get_details(km, &fm);
2740  } else {
2741    owl_fmtext_append_normal(&fm, "No such keymap...\n");
2742  } 
2743  owl_function_popless_fmtext(&fm);
2744  owl_fmtext_free(&fm);
2745}
2746
[d54838d]2747void owl_function_help_for_command(char *cmdname)
2748{
[1fd0b25]2749  owl_fmtext fm;
[7d4fbcd]2750
2751  owl_fmtext_init_null(&fm);
2752  owl_cmd_get_help(owl_global_get_cmddict(&g), cmdname, &fm);
2753  owl_function_popless_fmtext(&fm); 
2754  owl_fmtext_free(&fm);
2755}
[1fd0b25]2756
[d54838d]2757void owl_function_search_start(char *string, int direction)
2758{
[1fd0b25]2759  /* direction is OWL_DIRECTION_DOWNWARDS or OWL_DIRECTION_UPWARDS */
2760  owl_global_set_search_active(&g, string);
2761  owl_function_search_helper(0, direction);
2762}
2763
[d54838d]2764void owl_function_search_continue(int direction)
2765{
[1fd0b25]2766  /* direction is OWL_DIRECTION_DOWNWARDS or OWL_DIRECTION_UPWARDS */
2767  owl_function_search_helper(1, direction);
2768}
2769
[d54838d]2770void owl_function_search_helper(int mode, int direction)
2771{
[1fd0b25]2772  /* move to a message that contains the string.  If direction is
2773   * OWL_DIRECTION_DOWNWARDS then search fowards, if direction is
2774   * OWL_DIRECTION_UPWARDS then search backwards.
2775   *
2776   * If mode==0 then it will stay on the current message if it
2777   * contains the string.
2778   */
2779
2780  owl_view *v;
2781  int viewsize, i, curmsg, start;
2782  owl_message *m;
2783
2784  v=owl_global_get_current_view(&g);
2785  viewsize=owl_view_get_size(v);
2786  curmsg=owl_global_get_curmsg(&g);
2787 
2788  if (viewsize==0) {
[ec6ff52]2789    owl_function_error("No messages present");
[1fd0b25]2790    return;
2791  }
2792
2793  if (mode==0) {
2794    start=curmsg;
2795  } else if (direction==OWL_DIRECTION_DOWNWARDS) {
2796    start=curmsg+1;
2797  } else {
2798    start=curmsg-1;
2799  }
2800
2801  /* bounds check */
2802  if (start>=viewsize || start<0) {
[ec6ff52]2803    owl_function_error("No further matches found");
[1fd0b25]2804    return;
2805  }
2806
2807  for (i=start; i<viewsize && i>=0;) {
2808    m=owl_view_get_element(v, i);
2809    if (owl_message_search(m, owl_global_get_search_string(&g))) {
2810      owl_global_set_curmsg(&g, i);
2811      owl_function_calculate_topmsg(direction);
2812      owl_mainwin_redisplay(owl_global_get_mainwin(&g));
2813      if (direction==OWL_DIRECTION_DOWNWARDS) {
2814        owl_global_set_direction_downwards(&g);
2815      } else {
2816        owl_global_set_direction_upwards(&g);
2817      }
2818      return;
2819    }
2820    if (direction==OWL_DIRECTION_DOWNWARDS) {
2821      i++;
2822    } else {
2823      i--;
2824    }
2825  }
[37c27cf]2826  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
[ec6ff52]2827  owl_function_error("No matches found");
[1fd0b25]2828}
2829
2830/* strips formatting from ztext and returns the unformatted text.
2831 * caller is responsible for freeing. */
[d54838d]2832char *owl_function_ztext_stylestrip(char *zt)
2833{
[1fd0b25]2834  owl_fmtext fm;
2835  char *plaintext;
2836
2837  owl_fmtext_init_null(&fm);
2838  owl_fmtext_append_ztext(&fm, zt);
2839  plaintext = owl_fmtext_print_plain(&fm);
2840  owl_fmtext_free(&fm);
2841  return(plaintext);
2842}
[42abb10]2843
[aa5f725]2844/* Popup a buddylisting.  If file is NULL use the default .anyone */
2845void owl_function_buddylist(int aim, int zephyr, char *file)
[d54838d]2846{
[de03334]2847  int i, j, idle;
[42abb10]2848  owl_fmtext fm;
[f4d0975]2849  owl_buddylist *bl;
2850  owl_buddy *b;
[de03334]2851  char *foo, *timestr;
[09489b89]2852#ifdef HAVE_LIBZEPHYR
2853  char *ourfile, *tmp, buff[LINE], *line;
2854  ZLocations_t location[200];
2855  FILE *f;
2856  int numlocs, ret;
2857#endif
[42abb10]2858
[aa5f725]2859  owl_fmtext_init_null(&fm);
[42abb10]2860
[aa5f725]2861  if (aim && owl_global_is_aimloggedin(&g)) {
[f4d0975]2862    bl=owl_global_get_buddylist(&g);
[42abb10]2863
[aa5f725]2864    owl_fmtext_append_bold(&fm, "AIM users logged in:\n");
[f4d0975]2865    /* we're assuming AIM for now */
2866    j=owl_buddylist_get_size(bl);
[aa5f725]2867    for (i=0; i<j; i++) {
[f4d0975]2868      b=owl_buddylist_get_buddy_n(bl, i);
2869      idle=owl_buddy_get_idle_time(b);
[de03334]2870      if (idle!=0) {
[5b85d19]2871        timestr=owl_util_minutes_to_timestr(idle);
[de03334]2872      } else {
2873        timestr=owl_strdup("");
2874      }
[8a54409]2875      foo=owl_sprintf("  %-20.20s %-12.12s\n",
[f4d0975]2876                      owl_buddy_get_name(b),
[de03334]2877                      timestr);
2878      owl_fmtext_append_normal(&fm, foo);
2879      owl_free(timestr);
2880      owl_free(foo);
[42abb10]2881    }
[aa5f725]2882  }
[42abb10]2883
[09489b89]2884#ifdef HAVE_LIBZEPHYR
[aa5f725]2885  if (zephyr) {
2886    if (file==NULL) {
2887      tmp=owl_global_get_homedir(&g);
2888      if (!tmp) {
[ec6ff52]2889        owl_function_error("Could not determine home directory");
[aa5f725]2890        return;
2891      }
2892      ourfile=owl_malloc(strlen(tmp)+50);
2893      sprintf(ourfile, "%s/.anyone", owl_global_get_homedir(&g));
2894    } else {
2895      ourfile=owl_strdup(file);
[42abb10]2896    }
[aa5f725]2897   
2898    f=fopen(ourfile, "r");
2899    if (!f) {
[ec6ff52]2900      owl_function_error("Error opening file %s: %s",
[aa5f725]2901                           ourfile,
2902                           strerror(errno) ? strerror(errno) : "");
2903      return;
2904    }
[09489b89]2905
[aa5f725]2906    owl_fmtext_append_bold(&fm, "Zephyr users logged in:\n");
2907   
2908    while (fgets(buff, LINE, f)!=NULL) {
2909      /* ignore comments, blank lines etc. */
2910      if (buff[0]=='#') continue;
2911      if (buff[0]=='\n') continue;
2912      if (buff[0]=='\0') continue;
2913     
2914      /* strip the \n */
2915      buff[strlen(buff)-1]='\0';
2916     
2917      /* ingore from # on */
2918      tmp=strchr(buff, '#');
2919      if (tmp) tmp[0]='\0';
2920     
2921      /* ingore from SPC */
2922      tmp=strchr(buff, ' ');
2923      if (tmp) tmp[0]='\0';
2924     
2925      /* stick on the local realm. */
2926      if (!strchr(buff, '@')) {
2927        strcat(buff, "@");
2928        strcat(buff, ZGetRealm());
[42abb10]2929      }
[aa5f725]2930     
2931      ret=ZLocateUser(buff, &numlocs, ZAUTH);
2932      if (ret!=ZERR_NONE) {
[ec6ff52]2933        owl_function_error("Error getting location for %s", buff);
[aa5f725]2934        continue;
2935      }
2936     
2937      numlocs=200;
2938      ret=ZGetLocations(location, &numlocs);
2939      if (ret==0) {
2940        for (i=0; i<numlocs; i++) {
2941          line=malloc(strlen(location[i].host)+strlen(location[i].time)+strlen(location[i].tty)+100);
2942          tmp=short_zuser(buff);
2943          sprintf(line, "  %-10.10s %-24.24s %-12.12s  %20.20s\n",
2944                  tmp,
2945                  location[i].host,
2946                  location[i].tty,
2947                  location[i].time);
2948          owl_fmtext_append_normal(&fm, line);
2949          owl_free(tmp);
2950        }
2951        if (numlocs>=200) {
2952          owl_fmtext_append_normal(&fm, "  Too many locations found for this user, truncating.\n");
2953        }
[42abb10]2954      }
2955    }
[aa5f725]2956    fclose(f);
2957    owl_free(ourfile);
[42abb10]2958  }
[09489b89]2959#endif
[aa5f725]2960 
[42abb10]2961  owl_function_popless_fmtext(&fm);
2962  owl_fmtext_free(&fm);
2963}
[2adaf1d]2964
[d54838d]2965void owl_function_dump(char *filename) 
2966{
[2adaf1d]2967  int i, j, count;
2968  owl_message *m;
2969  owl_view *v;
2970  FILE *file;
2971  /* struct stat sbuf; */
2972
2973  v=owl_global_get_current_view(&g);
2974
2975  /* in the future make it ask yes/no */
2976  /*
2977  ret=stat(filename, &sbuf);
2978  if (!ret) {
2979    ret=owl_function_askyesno("File exists, continue? [Y/n]");
2980    if (!ret) return;
2981  }
2982  */
2983
2984  file=fopen(filename, "w");
2985  if (!file) {
[ec6ff52]2986    owl_function_error("Error opening file");
[2adaf1d]2987    return;
2988  }
2989
2990  count=0;
2991  j=owl_view_get_size(v);
2992  for (i=0; i<j; i++) {
2993    m=owl_view_get_element(v, i);
2994    fputs(owl_message_get_text(m), file);
2995  }
2996  fclose(file);
2997}
[8f44c6b]2998
[801c7cb]2999void owl_function_do_newmsgproc(void)
3000{
[8f44c6b]3001  if (owl_global_get_newmsgproc(&g) && strcmp(owl_global_get_newmsgproc(&g), "")) {
3002    /* if there's a process out there, we need to check on it */
3003    if (owl_global_get_newmsgproc_pid(&g)) {
3004      owl_function_debugmsg("Checking on newmsgproc pid==%i", owl_global_get_newmsgproc_pid(&g));
3005      owl_function_debugmsg("Waitpid return is %i", waitpid(owl_global_get_newmsgproc_pid(&g), NULL, WNOHANG));
3006      waitpid(owl_global_get_newmsgproc_pid(&g), NULL, WNOHANG);
3007      if (waitpid(owl_global_get_newmsgproc_pid(&g), NULL, WNOHANG)==-1) {
3008        /* it exited */
3009        owl_global_set_newmsgproc_pid(&g, 0);
3010        owl_function_debugmsg("newmsgproc exited");
3011      } else {
3012        owl_function_debugmsg("newmsgproc did not exit");
3013      }
3014    }
3015   
3016    /* if it exited, fork & exec a new one */
3017    if (owl_global_get_newmsgproc_pid(&g)==0) {
3018      int i, myargc;
3019      i=fork();
3020      if (i) {
3021        /* parent set the child's pid */
3022        owl_global_set_newmsgproc_pid(&g, i);
3023        owl_function_debugmsg("I'm the parent and I started a new newmsgproc with pid %i", i);
3024      } else {
3025        /* child exec's the program */
3026        char **parsed;
3027        parsed=owl_parseline(owl_global_get_newmsgproc(&g), &myargc);
[d54838d]3028        if (myargc < 0) {
3029          owl_function_debugmsg("Could not parse newmsgproc '%s': unbalanced quotes?", owl_global_get_newmsgproc(&g));
3030        }
3031        if (myargc <= 0) {
3032          _exit(127);
3033        }
3034        parsed=realloc(parsed, sizeof(*parsed) * (myargc+1));
3035        parsed[myargc] = NULL;
[8f44c6b]3036       
[d54838d]3037        owl_function_debugmsg("About to exec \"%s\" with %d arguments", parsed[0], myargc);
[8f44c6b]3038       
[d54838d]3039        execvp(parsed[0], parsed);
[8f44c6b]3040       
3041       
3042        /* was there an error exec'ing? */
[d54838d]3043        owl_function_debugmsg("Cannot run newmsgproc '%s': cannot exec '%s': %s", 
3044                              owl_global_get_newmsgproc(&g), parsed[0], strerror(errno));
[8f44c6b]3045        _exit(127);
3046      }
3047    }
3048  }
3049}
[ecd5dc5]3050
[2824f79]3051/* print the xterm escape sequence to raise the window */
[801c7cb]3052void owl_function_xterm_raise(void)
3053{
[e9b1f60]3054  printf("\033[5t");
[ecd5dc5]3055}
3056
[2824f79]3057/* print the xterm escape sequence to deiconify the window */
[801c7cb]3058void owl_function_xterm_deiconify(void)
3059{
[e9b1f60]3060  printf("\033[1t");
[ecd5dc5]3061}
[38cf544c]3062
3063/* Add the specified command to the startup file.  Eventually this
3064 * should be clever, and rewriting settings that will obviosly
3065 * override earlier settings with 'set' 'bindkey' and 'alias'
[2824f79]3066 * commands.  For now though we just remove any line that would
3067 * duplicate this one and then append this line to the end of
3068 * startupfile.
[38cf544c]3069 */
3070void owl_function_addstartup(char *buff)
3071{
3072  FILE *file;
3073  char *filename;
3074
3075  filename=owl_sprintf("%s/%s", owl_global_get_homedir(&g), OWL_STARTUP_FILE);
3076  file=fopen(filename, "a");
3077  if (!file) {
[ec6ff52]3078    owl_function_error("Error opening startupfile for new command");
[378fa14]3079    owl_free(filename);
[38cf544c]3080    return;
3081  }
[2824f79]3082
3083  /* delete earlier copies */
3084  owl_util_file_deleteline(filename, buff, 1);
[378fa14]3085  owl_free(filename);
[2824f79]3086
3087  /* add this line */
[38cf544c]3088  fprintf(file, "%s\n", buff);
[2824f79]3089
[38cf544c]3090  fclose(file);
3091}
3092
3093/* Remove the specified command from the startup file. */
3094void owl_function_delstartup(char *buff)
3095{
3096  char *filename;
3097  filename=owl_sprintf("%s/%s", owl_global_get_homedir(&g), OWL_STARTUP_FILE);
3098  owl_util_file_deleteline(filename, buff, 1);
3099  owl_free(filename);
3100}
3101
[2404c3a]3102/* Execute owl commands from the given filename.  If the filename
3103 * is NULL, use the default owl startup commands file.
3104 */
3105void owl_function_source(char *filename)
[38cf544c]3106{
3107  FILE *file;
3108  char buff[LINE];
3109
[2404c3a]3110  if (!filename) {
3111    filename=owl_sprintf("%s/%s", owl_global_get_homedir(&g), OWL_STARTUP_FILE);
3112    file=fopen(filename, "r");
3113    owl_free(filename);
3114  } else {
3115    file=fopen(filename, "r");
3116  }
[38cf544c]3117  if (!file) {
3118    /* just fail silently if it doesn't exist */
3119    return;
3120  }
3121  while (fgets(buff, LINE, file)!=NULL) {
3122    buff[strlen(buff)-1]='\0';
3123    owl_function_command(buff);
3124  }
3125  fclose(file);
3126}
[c3ab155]3127
[ef56a67]3128void owl_function_change_style(owl_view *v, char *stylename)
3129{
[f1cbb7a]3130  owl_style *s;
3131
3132  s=owl_global_get_style_by_name(&g, stylename);
3133  if (!s) {
[ec6ff52]3134    owl_function_error("No style named %s", stylename);
[f1cbb7a]3135    return;
3136  }
3137  owl_view_set_style(v, s);
[ef56a67]3138  owl_messagelist_invalidate_formats(owl_global_get_msglist(&g));
3139  owl_function_calculate_topmsg(OWL_DIRECTION_DOWNWARDS);
3140  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
3141}