]> code.citadel.org Git - citadel.git/blob - citadel/messages.c
* Altered the display and conversion of RFC822 messages
[citadel.git] / citadel / messages.c
1 /*
2  * Citadel/UX message support routines
3  * see copyright.txt for copyright information
4  * $Id$
5  */
6
7 #include "sysdep.h"
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <sys/types.h>
11 #include <fcntl.h>
12 #include <stdio.h>
13 #include <ctype.h>
14 #include <string.h>
15 #include <time.h>
16 #include <signal.h>
17 #include <errno.h>
18 #include <limits.h>
19 #include <sys/wait.h>
20 #include <sys/stat.h>
21 #include <stdarg.h>
22 #include "citadel.h"
23 #include "messages.h"
24 #include "commands.h"
25 #include "rooms.h"
26 #include "tools.h"
27 #ifndef HAVE_SNPRINTF
28 #include "snprintf.h"
29 #endif
30
31 #define MAXWORDBUF 256
32 #define MAXMSGS 512
33
34 struct cittext {
35         struct cittext *next;
36         char text[MAXWORDBUF];
37         };
38
39 void sttybbs(int cmd);
40 int struncmp(char *lstr, char *rstr, int len);
41 int fmout(int width, FILE *fp, char pagin, int height, int starting_lp, char subst);
42 int haschar(char *st, int ch);
43 int checkpagin(int lp, int pagin, int height);
44 void getline(char *string, int lim);
45 void formout(char *name);
46 int yesno(void);
47 void newprompt(char *prompt, char *str, int len);
48 int file_checksum(char *filename);
49 void do_edit(char *desc, char *read_cmd, char *check_cmd, char *write_cmd);
50
51 char reply_to[512];
52 long msg_arr[MAXMSGS];
53 int num_msgs;
54 extern char room_name[];
55 extern unsigned room_flags;
56 extern long highest_msg_read;
57 extern struct CtdlServInfo serv_info;
58 extern char temp[];
59 extern char temp2[];
60 extern int screenwidth;
61 extern int screenheight;
62 extern long maxmsgnum;
63 extern char is_mail;
64 extern char is_aide;
65 extern char is_room_aide;
66 extern char fullname[];
67 extern char axlevel;
68 extern unsigned userflags;
69 extern char sigcaught;
70 extern char editor_path[];
71 extern char printcmd[];
72 extern int rc_allow_attachments;
73 extern int rc_display_message_numbers;
74 extern int rc_force_mail_prompts;
75
76 extern int editor_pid;
77
78 int lines_printed;
79
80 void ka_sigcatch(int signum) {
81         char buf[256];
82         alarm(S_KEEPALIVE);
83         signal(SIGALRM, ka_sigcatch);
84         serv_puts("NOOP");
85         serv_gets(buf);
86         }
87
88
89 /*
90  * server keep-alive version of wait() (needed for external editor)
91  */
92 pid_t ka_wait(int *kstatus)
93 {
94         pid_t p;
95
96         alarm(S_KEEPALIVE);
97         signal(SIGALRM, ka_sigcatch);
98         do {
99                 errno = 0;
100                 p = wait(kstatus);
101                 } while (errno==EINTR);
102         signal(SIGALRM,SIG_IGN);
103         alarm(0);
104         return(p);
105         }
106
107
108 /*
109  * version of system() that uses ka_wait()
110  */
111 int ka_system(char *shc)
112 {
113         pid_t childpid;
114         pid_t waitpid;
115         int retcode;
116
117         childpid = fork();
118         if (childpid < 0) {
119                 color(BRIGHT_RED);
120                 perror("Cannot fork");
121                 color(DIM_WHITE);
122                 return((pid_t)childpid);
123                 }
124
125         if (childpid == 0) {
126                 execlp("/bin/sh","sh","-c",shc,NULL);
127                 exit(127);
128                 }
129
130         if (childpid > 0) {
131                 do {
132                         waitpid = ka_wait(&retcode);
133                         } while (waitpid != childpid);
134                 return(retcode);
135                 }
136
137         return(-1);
138         }
139
140
141
142 /*
143  * add a newline to the buffer...
144  */
145 void add_newline(struct cittext *textlist)
146 {
147         struct cittext *ptr;
148
149         ptr=textlist;
150         while (ptr->next != NULL) ptr = ptr->next;
151
152         while (ptr->text[strlen(ptr->text)-1]==32)
153                 ptr->text[strlen(ptr->text)-1] = 0;
154         /* strcat(ptr->text,"\n"); */
155
156         ptr->next = (struct cittext *)
157                 malloc(sizeof(struct cittext));
158         ptr = ptr->next;
159         ptr->next = NULL;
160         strcpy(ptr->text,"");
161         }
162
163
164 /*
165  * add a word to the buffer...
166  */
167 void add_word(struct cittext *textlist, char *wordbuf)
168 {
169         struct cittext *ptr;
170
171
172         ptr=textlist;
173         while (ptr->next != NULL) ptr = ptr->next;
174         
175         if (3+strlen(ptr->text)+strlen(wordbuf) > screenwidth) {
176                 ptr->next = (struct cittext *)
177                         malloc(sizeof(struct cittext));
178                 ptr = ptr->next;
179                 ptr->next = NULL;
180                 strcpy(ptr->text,"");
181                 }
182         
183         strcat(ptr->text,wordbuf);
184         strcat(ptr->text," ");
185         }
186
187
188 /*
189  * begin editing of an opened file pointed to by fp, beginning at position pos.
190  */
191 void citedit(FILE *fp, long int base_pos)
192 {
193         int a,prev,finished,b,last_space;
194         int appending = 0;
195         struct cittext *textlist = NULL;
196         struct cittext *ptr;
197         char wordbuf[MAXWORDBUF];
198         char buf[256];
199         time_t last_server_msg,now;
200
201         /*
202          * we're going to keep track of the last time we talked to
203          * the server, so we can periodically send keep-alive messages
204          * out so it doesn't time out.
205          */
206         time(&last_server_msg);
207
208         /* first, load the text into the buffer */
209         fseek(fp,base_pos,0);
210         textlist = (struct cittext *)malloc(sizeof(struct cittext));
211         textlist->next = NULL;
212         strcpy(textlist->text,"");
213
214         strcpy(wordbuf,"");
215         prev = (-1);
216         while (a=getc(fp), a>=0) {
217                 appending = 1;
218                 if ((a==32)||(a==9)||(a==13)||(a==10)) {
219                         add_word(textlist,wordbuf);
220                         strcpy(wordbuf,"");
221                         if ((prev==13)||(prev==10)) {
222                                 add_word(textlist,"\n");
223                                 add_newline(textlist);
224                                 add_word(textlist,"");
225                                 }
226                         }
227                 else {
228                         wordbuf[strlen(wordbuf)+1] = 0;
229                         wordbuf[strlen(wordbuf)] = a;
230                         }
231                 if (strlen(wordbuf)+3 > screenwidth) {
232                         add_word(textlist,wordbuf);
233                         strcpy(wordbuf,"");
234                         }
235                 prev = a;
236                 }
237
238         /* get text */
239         finished = 0;
240         prev = (appending ? 13 : (-1));
241         strcpy(wordbuf,"");
242         do {
243                 a=inkey();
244                 if (a==10) a=13;
245                 if (a==9) a=32;
246                 if (a==127) a=8;
247                 if ((a==32)&&(prev==13)) {
248                         add_word(textlist,"\n");
249                         add_newline(textlist);
250                         }
251                 if (a==8) {
252                         if (strlen(wordbuf)>0) {
253                                 wordbuf[strlen(wordbuf)-1] = 0;
254                                 putc(8,stdout);
255                                 putc(32,stdout);
256                                 putc(8,stdout);
257                                 }
258                         }
259                 else if (a==13) {
260                         printf("\n");
261                         if (strlen(wordbuf)==0) finished = 1;
262                         else {
263                                 for (b=0; b<strlen(wordbuf); ++b)
264                                    if (wordbuf[b]==32) {
265                                         wordbuf[b]=0;
266                                         add_word(textlist,wordbuf);
267                                         strcpy(wordbuf,&wordbuf[b+1]);
268                                         b=0;
269                                         }
270                                 add_word(textlist,wordbuf);
271                                 strcpy(wordbuf,"");
272                                 }
273                         }
274                 else {
275                         putc(a,stdout);
276                         wordbuf[strlen(wordbuf)+1] = 0;
277                         wordbuf[strlen(wordbuf)] = a;
278                         }
279                 if ((strlen(wordbuf)+3) > screenwidth) {
280                         last_space = (-1);
281                         for (b=0; b<strlen(wordbuf); ++b)
282                                 if (wordbuf[b]==32) last_space = b;
283                         if (last_space>=0) {
284                                 for (b=0; b<strlen(wordbuf); ++b)
285                                    if (wordbuf[b]==32) {
286                                         wordbuf[b]=0;
287                                         add_word(textlist,wordbuf);
288                                         strcpy(wordbuf,&wordbuf[b+1]);
289                                         b=0;
290                                         }
291                                 for (b=0; b<strlen(wordbuf); ++b) {
292                                         putc(8,stdout);
293                                         putc(32,stdout);
294                                         putc(8,stdout);
295                                         }
296                                 printf("\n%s",wordbuf);
297                                 }
298                         else {
299                                 add_word(textlist,wordbuf);
300                                 strcpy(wordbuf,"");
301                                 printf("\n");
302                                 }
303                         }
304                 prev = a;
305
306                 /* this check implements the server keep-alive */
307                 time(&now);
308                 if ( (now - last_server_msg) > S_KEEPALIVE ) {
309                         serv_puts("NOOP");
310                         serv_gets(buf);
311                         last_server_msg = now;
312                         }
313
314                 } while (finished==0);
315
316         /* write the buffer back to disk */
317         fseek(fp,base_pos,0);
318         for (ptr=textlist; ptr!=NULL; ptr=ptr->next) {
319                 fprintf(fp,"%s",ptr->text);
320                 }
321         putc(10,fp);
322         putc(0,fp);
323
324         /* and deallocate the memory we used */
325         while (textlist!=NULL) {
326                 ptr=textlist->next;
327                 free(textlist);
328                 textlist=ptr;
329                 }
330         }
331
332
333 int read_message(long int num, char pagin) /* Read a message from the server */
334                                            /* message number */
335                 /* 0 = normal read, 1 = read with pagination, 2 = header */
336 {
337         char buf[256];
338         char m_subject[256];
339         char from[256];
340         time_t now;
341         struct tm *tm;
342         int format_type = 0;
343         int fr = 0;
344         int nhdr = 0;
345         int have_rfca = 0;
346
347         sigcaught = 0;
348         sttybbs(1);
349
350         sprintf(buf,"MSG0 %ld|%d",num,(pagin==READ_HEADER ? 1 : 0));
351         serv_puts(buf);
352         serv_gets(buf);
353         if (buf[0]!='1') {
354                 printf("*** msg #%ld: %s\n",num,buf);
355                 ++lines_printed;
356                 lines_printed = checkpagin(lines_printed,pagin,screenheight);
357                 sttybbs(0);
358                 return(0);
359                 }
360
361         strcpy(m_subject,"");
362         strcpy(reply_to, "nobody ... xxxxx");
363         printf("\n");
364         ++lines_printed;
365         lines_printed = checkpagin(lines_printed,pagin,screenheight);
366         printf(" ");
367         if (pagin == 1) color(BRIGHT_CYAN);
368
369         if (pagin==2) {
370                 while(serv_gets(buf), strcmp(buf,"000")) {
371                         if (buf[4]=='=') {
372                                 printf("%s\n",buf);
373                                 ++lines_printed;
374                                 lines_printed = 
375                                         checkpagin(lines_printed,
376                                                 pagin,screenheight);
377                                 }
378                         }
379                 sttybbs(0);
380                 return(0);
381                 }
382
383         while(serv_gets(buf), struncmp(buf,"text",4)) {
384                 if (!struncmp(buf,"nhdr=yes",8)) nhdr=1;
385                 if (!struncmp(buf,"from=",5)) {
386                         strcpy(from,&buf[5]);
387                         }
388                 if (nhdr==1) buf[0]='_';
389                 if (!struncmp(buf,"type=",5))
390                         format_type=atoi(&buf[5]);
391                 if ((!struncmp(buf,"msgn=",5))&&(rc_display_message_numbers)) {
392                         color(DIM_WHITE);
393                         printf("[");
394                         color(BRIGHT_WHITE);
395                         printf("#%s",&buf[5]);
396                         color(DIM_WHITE);
397                         printf("] ");
398                         }
399                 if (!struncmp(buf,"from=",5)) {
400                         color(DIM_WHITE);
401                         printf("from ");
402                         color(BRIGHT_CYAN);
403                         printf("%s ",&buf[5]);
404                         }
405                 if (!struncmp(buf,"subj=",5))
406                         strcpy(m_subject,&buf[5]);
407
408                 if (!struncmp(buf,"rfca=",5)) {
409                         strcpy(reply_to, &buf[5]);
410                         have_rfca = 1;
411                         color(DIM_WHITE);
412                         printf("<");
413                         color(BRIGHT_BLUE);
414                         printf("%s",&buf[5]);
415                         color(DIM_WHITE);
416                         printf("> ");
417                         }
418                 if ((!struncmp(buf,"hnod=",5)) 
419                    && (strucmp(&buf[5],serv_info.serv_humannode))
420                    && (have_rfca == 0) ) {
421                         color(DIM_WHITE);
422                         printf("(");
423                         color(BRIGHT_WHITE);
424                         printf("%s",&buf[5]);
425                         color(DIM_WHITE);
426                         printf(") ");
427                         }
428                 if ((!struncmp(buf,"room=",5))
429                    && (strucmp(&buf[5],room_name)) 
430                    && (have_rfca == 0)) {
431                         color(DIM_WHITE);
432                         printf("in ");
433                         color(BRIGHT_MAGENTA);
434                         printf("%s> ",&buf[5]);
435                         }
436
437                 if (!struncmp(buf,"node=",5)) {
438                         if ( (room_flags&QR_NETWORK)
439                            || ((strucmp(&buf[5],serv_info.serv_nodename)
440                            &&(strucmp(&buf[5],serv_info.serv_fqdn)))) ) 
441                                 {
442                                 if (have_rfca == 0) {
443                                         color(DIM_WHITE);
444                                         printf("@");
445                                         color(BRIGHT_YELLOW);
446                                         printf("%s ",&buf[5]);
447                                 }
448                         }
449                         if (have_rfca == 0) {
450                          if ((!strucmp(&buf[5],serv_info.serv_nodename))
451                            ||(!strucmp(&buf[5],serv_info.serv_fqdn)))
452                                 {
453                                 strcpy(reply_to,from);
454                                 }
455                         }
456                         else {
457                                 sprintf(reply_to,"%s @ %s",from,&buf[5]);
458                                 }
459                         }
460
461                 if (!struncmp(buf,"rcpt=",5)) {
462                         color(DIM_WHITE);
463                         printf("to ");
464                         color(BRIGHT_CYAN);
465                         printf("%s ",&buf[5]);
466                         }
467                 if (!struncmp(buf,"time=",5)) {
468                         now=atol(&buf[5]);
469                         tm=(struct tm *)localtime(&now);
470                         strcpy(buf,asctime(tm)); buf[strlen(buf)-1]=0;
471                         strcpy(&buf[16],&buf[19]);
472                         color(BRIGHT_MAGENTA);
473                         printf("%s ",&buf[4]);
474                         }
475                 }
476
477         if (nhdr==1) {
478                 if (!is_room_aide) {
479                         printf(" ****");
480                         }
481                 else {
482                         printf(" %s",from);
483                         }
484                 }
485         printf("\n");
486         if (pagin == 1) color(BRIGHT_WHITE);
487         ++lines_printed;
488         lines_printed = checkpagin(lines_printed,pagin,screenheight);
489
490         if (strlen(m_subject)>0) {
491                 printf("Subject: %s\n",m_subject);
492                 ++lines_printed;
493                 lines_printed = checkpagin(lines_printed,pagin,screenheight);
494                 }
495
496         if (format_type == 0) {
497                 fr=fmout(screenwidth,NULL,
498                         ((pagin==1) ? 1 : 0),
499                         screenheight,(-1),1);
500                 }
501         else {
502                 while(serv_gets(buf), strcmp(buf,"000")) {
503                         if (sigcaught==0) {
504                                 printf("%s\n",buf);
505                                 lines_printed = lines_printed + 1 +
506                                         (strlen(buf)/screenwidth);
507                                 lines_printed =
508                                         checkpagin(lines_printed,pagin,screenheight);
509                                 }
510                         }
511                 fr = sigcaught;
512                 }
513         printf("\n");
514         ++lines_printed;
515         lines_printed = checkpagin(lines_printed,pagin,screenheight);
516
517         if (pagin == 1) color(DIM_WHITE);
518         sttybbs(0);
519         return(fr);
520         }
521
522 /*
523  * replace string function for the built-in editor
524  */
525 void replace_string(char *filename, long int startpos)
526 {
527         char buf[512];
528         char srch_str[128];
529         char rplc_str[128];
530         FILE *fp;
531         int a;
532         long rpos,wpos;
533         char *ptr;
534         int substitutions = 0;
535         long msglen = 0L;
536
537         printf("Enter text to be replaced:\n: ");
538         getline(srch_str,128);
539         if (strlen(srch_str)==0) return;
540         
541         printf("Enter text to replace it with:\n: ");
542         getline(rplc_str,128);
543
544         fp=fopen(filename,"r+");
545         if (fp==NULL) return;
546
547         wpos=startpos;
548         fseek(fp,startpos,0);
549         strcpy(buf,"");
550         while (a=getc(fp), a>0) {
551                 ++msglen;
552                 buf[strlen(buf)+1] = 0;
553                 buf[strlen(buf)] = a;
554                 if ( strlen(buf) >= strlen(srch_str) ) {
555                         ptr=&buf[strlen(buf)-strlen(srch_str)];
556                         if (!struncmp(ptr,srch_str,strlen(srch_str))) {
557                                 strcpy(ptr,rplc_str);
558                                 ++substitutions;
559                                 }
560                         }
561                 if (strlen(buf)>384) {
562                         rpos=ftell(fp);
563                         fseek(fp,wpos,0);
564                         fwrite((char *)buf,128,1,fp);
565                         strcpy(buf,&buf[128]);
566                         wpos = ftell(fp);
567                         fseek(fp,rpos,0);
568                         }
569                 }
570         fseek(fp,wpos,0);
571         if (strlen(buf)>0) fwrite((char *)buf,strlen(buf),1,fp);
572         putc(0,fp);
573         fclose(fp);
574         printf("<R>eplace made %d substitution(s).\n\n",substitutions);
575         }
576
577
578 int make_message(char *filename,        /* temporary file name */
579                 char *recipient,        /* NULL if it's not mail */
580                 int anon_type,          /* see MES_ types in header file */
581                 int format_type,
582                 int mode)
583
584         FILE *fp;
585         int a,b,e_ex_code;
586         time_t now;
587         long beg;
588         char datestr[64];
589         int cksum = 0;
590
591         if (mode==2) if (strlen(editor_path)==0) {
592                 printf("*** No editor available, using built-in editor\n");
593                 mode=0;
594                 }
595
596         time(&now);
597         strcpy(datestr,asctime(localtime(&now)));
598         datestr[strlen(datestr)-1] = 0;
599
600         if (room_flags & QR_ANONONLY) {
601                 printf(" ****");
602                 }
603         else {
604                 printf(" %s from %s",datestr,fullname);
605                 if (strlen(recipient)>0) printf(" to %s",recipient);
606                 }
607         printf("\n");
608
609         beg = 0L;
610
611         if (mode==1) printf("(Press ctrl-d when finished)\n");
612         if (mode==0) {
613                 fp=fopen(filename,"r");
614                 if (fp!=NULL) {
615                         fmout(screenwidth,fp,0,screenheight,0,0);
616                         beg = ftell(fp);
617                         fclose(fp);
618                         }
619                 else {
620                         fp=fopen(filename,"w");
621                         fclose(fp);
622                         }
623                 }
624
625 ME1:    switch(mode) {
626
627            case 0:
628                 fp=fopen(filename,"r+");
629                 citedit(fp, beg);
630                 fclose(fp);
631                 goto MECR;
632
633            case 1:
634                 fp=fopen(filename,"w");
635                 do {
636                         a=inkey(); if (a==255) a=32;
637                         if (a==13) a=10;
638                         if (a!=4) {
639                                 putc(a,fp);
640                                 putc(a,stdout);
641                                 }
642                         if (a==10) putc(13,stdout);
643                         } while(a!=4);
644                 fclose(fp);
645                 break;
646
647            case 2:
648                 e_ex_code = 1;  /* start with a failed exit code */
649                 editor_pid=fork();
650                 cksum = file_checksum(filename);
651                 if (editor_pid==0) {
652                         chmod(filename,0600);
653                         sttybbs(SB_RESTORE);
654                         execlp(editor_path,editor_path,filename,NULL);
655                         exit(1);
656                         }
657                 if (editor_pid>0) do {
658                         e_ex_code = 0;
659                         b=ka_wait(&e_ex_code);
660                         } while((b!=editor_pid)&&(b>=0));
661                 editor_pid = (-1);
662                 sttybbs(0);
663                 break;
664                 }
665
666 MECR:   if (mode==2) {
667                 if (file_checksum(filename) == cksum) {
668                         printf("*** Aborted message.\n");
669                         e_ex_code = 1;
670                         }
671                 if (e_ex_code==0) goto MEFIN;
672                 goto MEABT2;
673                 }
674 MECR1:  printf("Entry cmd (? for options) -> ");
675 MECR2:  b=inkey();
676         if (b==NEXT_KEY) b='n';
677         if (b==STOP_KEY) b='s';
678         b=(b&127); b=tolower(b);
679         if (b=='?') {
680                 printf("Help\n");
681                 formout("saveopt");
682                 goto MECR1;
683                 }
684         if (b=='a') { printf("Abort\n");        goto MEABT;     }
685         if (b=='c') { printf("Continue\n");     goto ME1;       }
686         if (b=='s') { printf("Save message\n"); goto MEFIN;     } 
687         if (b=='p') {
688                 printf("Print formatted\n");
689                 printf(" %s from %s",datestr,fullname);
690                 if (strlen(recipient)>0) printf(" to %s",recipient);
691                 printf("\n");
692                 fp=fopen(filename,"r");
693                 if (fp!=NULL) {
694                         fmout(screenwidth,fp,
695                                 ((userflags & US_PAGINATOR) ? 1 : 0),
696                                 screenheight,0,0);
697                         beg = ftell(fp);
698                         fclose(fp);
699                         }
700                 goto MECR;
701                 }
702         if (b=='r') {
703                 printf("Replace string\n");
704                 replace_string(filename,0L);
705                 goto MECR;
706                 }
707         if (b=='h') {
708                 printf("Hold message\n");
709                 return(2);
710                 }
711         goto MECR2;
712
713 MEFIN:  return(0);
714
715 MEABT:  printf("Are you sure? ");
716         if (yesno()==0) goto ME1;
717 MEABT2: unlink(filename);
718         return(2);
719         }
720
721 /*
722  * transmit message text to the server
723  */
724 void transmit_message(FILE *fp)
725 {
726         char buf[256];
727         int ch,a;
728         long msglen;
729         time_t lasttick;
730
731         fseek(fp, 0L, SEEK_END);
732         msglen = ftell(fp);
733         rewind(fp);
734         lasttick = time(NULL);
735         strcpy(buf,"");
736         while (ch=getc(fp), (ch>=0)) {
737                 if (ch==10) {
738                         if (!strcmp(buf,"000")) strcpy(buf,">000");
739                         serv_puts(buf);
740                         strcpy(buf,"");
741                         }
742                 else {
743                         a = strlen(buf);
744                         buf[a+1] = 0;
745                         buf[a] = ch;
746                         if ((ch==32)&&(strlen(buf)>200)) {
747                                 buf[a]=0;
748                                 if (!strcmp(buf,"000")) strcpy(buf,">000");
749                                 serv_puts(buf);
750                                 strcpy(buf,"");
751                                 }
752                         if (strlen(buf)>250) {
753                                 if (!strcmp(buf,"000")) strcpy(buf,">000");
754                                 serv_puts(buf);
755                                 strcpy(buf,"");
756                                 }
757                         }
758
759                 if ( (time(NULL) - lasttick) > 2L ) {
760                         printf(" %3ld%% completed\r",
761                                 ((ftell(fp) * 100L) / msglen) );
762                         fflush(stdout);
763                         lasttick = time(NULL);
764                         }
765
766                 }
767         serv_puts(buf);
768         printf("                \r");
769         fflush(stdout);
770         }
771
772
773
774 /*
775  * entmsg()  -  edit and create a message
776  *              returns 0 if message was saved
777  */
778 int entmsg(int is_reply, int c)
779                         /* nonzero if this was a <R>eply command */
780        {                /* */
781         char buf[300];
782         char cmd[256];
783         int a,b;
784         int need_recp = 0;
785         int mode;
786         long highmsg;
787         FILE *fp;
788
789         if (c>0) mode=1;
790         else mode=0;
791
792         sprintf(cmd,"ENT0 0||0|%d",mode);
793         serv_puts(cmd);
794         serv_gets(cmd);
795
796         if ((strncmp(cmd,"570",3)) && (strncmp(cmd,"200",3))) {
797                 printf("%s\n",&cmd[4]);
798                 return(1);
799                 }
800         need_recp = 0;
801         if (!strncmp(cmd,"570",3)) need_recp = 1;
802
803         if ((userflags & US_EXPERT) == 0) formout("entermsg");
804                 
805         strcpy(buf,"");
806         if (need_recp==1) {
807                 if (axlevel>=2) {
808                         if (is_reply) {
809                                 strcpy(buf,reply_to);
810                                 }
811                         else {
812                                 printf("Enter recipient: ");
813                                 getline(buf, 60);
814                                 if (strlen(buf)==0) return(1);
815                                 }
816                         }
817                 else strcpy(buf,"sysop");
818                 }
819
820         b=0;
821         if (room_flags&QR_ANONOPT) {
822                 printf("Anonymous (Y/N)? ");
823                 if (yesno()==1) b=1;
824                 }
825
826 /* if it's mail, we've got to check the validity of the recipient... */
827         if (strlen(buf)>0) {
828                 sprintf(cmd,"ENT0 0|%s|%d|%d",buf,b,mode);
829                 serv_puts(cmd);
830                 serv_gets(cmd);
831                 if (cmd[0]!='2') {
832                         printf("%s\n",&cmd[4]);
833                         return(1);
834                         }
835                 }
836
837 /* learn the number of the newest message in in the room, so we can tell
838  * upon saving whether someone else has posted too
839  */
840         num_msgs = 0;
841         serv_puts("MSGS LAST|1");
842         serv_gets(cmd);
843         if (cmd[0]!='1') {
844                 printf("%s\n",&cmd[5]);
845                 }
846         else {
847                 while (serv_gets(cmd), strcmp(cmd,"000")) {
848                         msg_arr[num_msgs++] = atol(cmd);
849                         }
850                 }
851
852 /* now put together the message */
853         if ( make_message(temp,buf,b,0,c) != 0 ) return(2);
854
855 /* and send it to the server */
856         sprintf(cmd,"ENT0 1|%s|%d|%d||",buf,b,mode);
857         serv_puts(cmd);
858         serv_gets(cmd);
859         if (cmd[0]!='4') {
860                 printf("%s\n",&cmd[4]);
861                 return(1);
862                 }
863         fp=fopen(temp,"r");
864         if (fp!=NULL) {
865                 transmit_message(fp);
866                 fclose(fp);
867                 }
868         serv_puts("000");
869         unlink(temp);
870         
871         highmsg = msg_arr[num_msgs - 1];
872         num_msgs = 0;
873         serv_puts("MSGS NEW");
874         serv_gets(cmd);
875         if (cmd[0]!='1') {
876                 printf("%s\n",&cmd[5]);
877                 }
878         else {
879                 while (serv_gets(cmd), strcmp(cmd,"000")) {
880                         msg_arr[num_msgs++] = atol(cmd);
881                         }
882                 }
883
884         /* get new highest message number in room to set lrp for goto... */
885         maxmsgnum = msg_arr[num_msgs - 1];
886
887         /* now see if anyone else has posted in here */
888         b=(-1);
889         for (a=0; a<num_msgs; ++a) if (msg_arr[a]>highmsg) ++b;
890
891         /* in the Mail> room, this algorithm always counts one message
892          * higher than in public rooms, so we decrement it by one */
893         if (need_recp) --b;
894
895         if (b==1) printf(
896 "*** 1 additional message has been entered in this room by another user.\n");
897         if (b>1) printf(
898 "*** %d additional messages have been entered in this room by other users.\n",b);
899
900         return(0);
901         }
902
903 void process_quote(void) {      /* do editing on quoted file */
904 FILE *qfile,*tfile;
905 char buf[128];
906 int line,qstart,qend;
907
908         /* Unlink the second temp file as soon as it's opened, so it'll get
909          * deleted even if the program dies
910          */
911         qfile = fopen(temp2,"r");
912         unlink(temp2);
913
914         /* Display the quotable text with line numbers added */
915         line = 0;
916         fgets(buf,128,qfile);
917         while (fgets(buf,128,qfile)!=NULL) {
918                 printf("%2d %s",++line,buf);
919                 }
920         printf("Begin quoting at [ 1] : ");
921         getline(buf,3);
922         qstart = (buf[0]==0) ? (1) : atoi(buf);
923         printf("  End quoting at [%d] : ",line);
924         getline(buf,3);
925         qend = (buf[0]==0) ? (line) : atoi(buf);
926         rewind(qfile);
927         line=0;
928         fgets(buf,128,qfile);
929         tfile=fopen(temp,"w");
930         while(fgets(buf,128,qfile)!=NULL) {
931                 if ((++line>=qstart)&&(line<=qend)) fprintf(tfile," >%s",buf);
932                 }
933         fprintf(tfile," \n");
934         fclose(qfile);
935         fclose(tfile);
936         chmod(temp,0666);
937         }
938
939
940
941 /*
942  * List the URL's which were embedded in the previous message
943  */
944 void list_urls() {
945         int i;
946         char cmd[256];
947
948         if (num_urls == 0) {
949                 printf("There were no URL's in the previous message.\n\n");
950                 return;
951         }
952
953         for (i=0; i<num_urls; ++i) {
954                 printf("%3d %s\n", i+1, urls[i]);
955         }
956
957         if((i = num_urls) != 1)
958                 i = intprompt("Display which one", 1, 1, num_urls);
959
960         sprintf(cmd, rc_url_cmd, urls[i-1]);
961         system(cmd);
962         printf("\n");
963 }
964
965
966 void readmsgs(int c, int rdir, int q)   /* read contents of a room */
967                 /* 0=Read all  1=Read new  2=Read old 3=Read last q */
968                 /* 1=Forward (-1)=Reverse */
969                 /* Number of msgs to read (if c==3) */
970         {
971         int a,b,e,f,g,start;
972         int hold_sw = 0;
973         char arcflag = 0;
974         char quotflag = 0;
975         int hold_color = 0;
976         char prtfile[PATH_MAX];
977         char pagin;
978         char cmd[256];
979         char targ[ROOMNAMELEN];
980         char filename[256];
981
982         signal(SIGINT,SIG_IGN);
983         signal(SIGQUIT,SIG_IGN);
984
985         if (c<0) b=(MAXMSGS-1);
986         else b=0;
987
988         sprintf(prtfile, tmpnam(NULL));
989
990         num_msgs = 0;
991         strcpy(cmd,"MSGS ");
992         switch(c) {
993                 case 0: strcat(cmd,"ALL");
994                         break;
995                 case 1: strcat(cmd,"NEW");
996                         break;
997                 case 2: strcat(cmd,"OLD");
998                         break;
999                 case 3: sprintf(&cmd[strlen(cmd)], "LAST|%d", q);
1000                         break;
1001                 }
1002         serv_puts(cmd);
1003         serv_gets(cmd);
1004         if (cmd[0]!='1') {
1005                 printf("%s\n",&cmd[5]);
1006                 }
1007         else {
1008                 while (serv_gets(cmd), strcmp(cmd,"000")) {
1009                         if (num_msgs == MAXMSGS) {
1010                                 memcpy(&msg_arr[0], &msg_arr[1],
1011                                         (sizeof(long) * (MAXMSGS - 1)) );
1012                                 --num_msgs;
1013                                 }
1014                         msg_arr[num_msgs++] = atol(cmd);
1015                         }
1016                 }
1017
1018         lines_printed = 0;
1019
1020         /* this loop cycles through each message... */
1021         start = ( (rdir==1) ? 0 : (num_msgs-1) );
1022         for (a=start; ((a<num_msgs)&&(a>=0)); a=a+rdir) {
1023                 while (msg_arr[a]==0L) {
1024                         a=a+rdir; if ((a==MAXMSGS)||(a==(-1))) return;
1025                         }
1026
1027 RAGAIN:         pagin=((arcflag==0)&&(quotflag==0)&&
1028                         (userflags & US_PAGINATOR)) ? 1 : 0;
1029
1030         /* If we're doing a quote, set the screenwidth to 72 temporarily */
1031                 if (quotflag) {
1032                         hold_sw = screenwidth;
1033                         screenwidth = 72;
1034                         }
1035
1036         /* If printing or archiving, set the screenwidth to 80 temporarily */
1037                 if (arcflag) {
1038                         hold_sw = screenwidth;
1039                         screenwidth = 80;
1040                         }
1041
1042         /* now read the message... */
1043                 e=read_message(msg_arr[a],pagin);
1044
1045         /* ...and set the screenwidth back if we have to */
1046                 if ((quotflag)||(arcflag)) {
1047                         screenwidth = hold_sw;
1048                         }
1049 RMSGREAD:       fflush(stdout);
1050                 highest_msg_read = msg_arr[a];
1051                 if (quotflag) {
1052                         freopen("/dev/tty","r+",stdout);
1053                         quotflag=0;
1054                         enable_color = hold_color;
1055                         process_quote();
1056                         }
1057                 if (arcflag) {
1058                         freopen("/dev/tty","r+",stdout);
1059                         arcflag=0;
1060                         enable_color = hold_color;
1061                         f=fork();
1062                         if (f==0) {
1063                                 freopen(prtfile, "r", stdin);
1064                                 sttybbs(SB_RESTORE);
1065                                 ka_system(printcmd);
1066                                 sttybbs(SB_NO_INTR);
1067                                 unlink(prtfile);
1068                                 exit(0);
1069                                 }
1070                         if (f>0) do {
1071                                 g=wait(NULL);
1072                                 } while((g!=f)&&(g>=0));
1073                         printf("Message printed.\n");
1074                         }
1075                 if (e==3) return;
1076                 if ( ((userflags&US_NOPROMPT)||(e==2)) 
1077                    && (((room_flags&QR_MAILBOX)==0)
1078                      ||(rc_force_mail_prompts==0))  ) {
1079                         e='n';
1080                         }
1081                 else {
1082                         color(DIM_WHITE);
1083                         printf("(");
1084                         color(BRIGHT_WHITE);
1085                         printf("%d",num_msgs-a-1);
1086                         color(DIM_WHITE);
1087                         printf(") ");
1088
1089                         if (is_mail==1) keyopt("<R>eply ");
1090                         keyopt("<B>ack <A>gain <Q>uote <N>ext <S>top ");
1091                         if (rc_url_cmd[0] && num_urls) keyopt("<U>RL View ");
1092                         keyopt("<?>Help/others -> ");
1093                         
1094                         do {
1095                                 lines_printed = 2;
1096                                 e=(inkey()&127); e=tolower(e);
1097 /* return key same as <N> */    if (e==13) e='n';
1098 /* space key same as <N> */     if (e==32) e='n';
1099 /* del/move for aides only */   if ((!is_room_aide)
1100                                     &&((room_flags&QR_MAILBOX)==0)) {
1101                                         if ((e=='d')||(e=='m')||(e=='c')) e=0;
1102                                         }
1103 /* print only if available */   if ((e=='p')&&(strlen(printcmd)==0)) e=0;
1104 /* can't reply in public rms */ if ((e=='r')&&(is_mail!=1)) e=0;
1105 /* can't file if not allowed */ if ((e=='f')&&(rc_allow_attachments==0)) e=0;
1106 /* link only if browser avail*/ if ((e=='u')&&(strlen(rc_url_cmd)==0)) e=0;
1107                                 } while((e!='a')&&(e!='n')&&(e!='s')
1108                                         &&(e!='d')&&(e!='m')&&(e!='p')
1109                                         &&(e!='q')&&(e!='b')&&(e!='h')
1110                                         &&(e!='r')&&(e!='f')&&(e!='?')
1111                                         &&(e!='u')&&(e!='c'));
1112                         switch(e) {
1113                                 case 's':       printf("Stop\r");       break;
1114                                 case 'a':       printf("Again\r");      break;
1115                                 case 'd':       printf("Delete\r");     break;
1116                                 case 'm':       printf("Move\r");       break;
1117                                 case 'c':       printf("Copy\r");       break;
1118                                 case 'n':       printf("Next\r");       break;
1119                                 case 'p':       printf("Print\r");      break;
1120                                 case 'q':       printf("Quote\r");      break;
1121                                 case 'b':       printf("Back\r");       break;
1122                                 case 'h':       printf("Header\r");     break;
1123                                 case 'r':       printf("Reply\r");      break;
1124                                 case 'f':       printf("File\r");       break;
1125                                 case 'u':       printf("URL's\r");      break;
1126                                 case '?':       printf("? <help>\r");   break;
1127                                 }
1128                         if (userflags & US_DISAPPEAR)
1129                                 printf("\r%79s\r","");
1130                         else
1131                                 printf("\n");
1132                         fflush(stdout);
1133                         }
1134                 switch(e) {
1135                    case '?':    printf("Options available here:\n");
1136                                 printf(" ?  Help (prints this message)\n");
1137                                 printf(" S  Stop reading immediately\n");
1138                                 printf(" A  Again (repeats last message)\n");
1139                                 printf(" N  Next (continue with next message)\n");
1140                                 printf(" B  Back (go back to previous message)\n");
1141                                 if ((is_room_aide)
1142                                     ||(room_flags&QR_MAILBOX)) {
1143                                         printf(" D  Delete this message\n");
1144                                         printf(" M  Move message to another room\n");
1145                                         printf(" C  Copy message to another room\n");
1146                                         }
1147                                 if (strlen(printcmd)>0)
1148                                         printf(" P  Print this message\n");
1149                                 printf(" Q  Quote portions of this message for your next post\n");
1150                                 printf(" H  Headers (display message headers only)\n");
1151                                 if (is_mail)
1152                                         printf(" R  Reply to this message\n");
1153                                 if (rc_allow_attachments)
1154                                         printf(" F  (save attachments to a file)\n");
1155                                 if (strlen(rc_url_cmd)>0)
1156                                         printf(" U  (list URL's for display)\n");
1157                                 printf("\n");
1158                                 goto RMSGREAD;
1159                    case 'p':    fflush(stdout);
1160                                 freopen(prtfile,"w",stdout);
1161                                 arcflag = 1;
1162                                 hold_color = enable_color;
1163                                 enable_color = 0;
1164                                 goto RAGAIN;
1165                    case 'q':    fflush(stdout);
1166                                 freopen(temp2,"w",stdout);
1167                                 quotflag = 1;
1168                                 hold_color = enable_color;
1169                                 enable_color = 0;
1170                                 goto RAGAIN;
1171                    case 's':    return;
1172                    case 'a':    goto RAGAIN;
1173                    case 'b':    a=a-(rdir*2);
1174                                 break;
1175                    case 'm':
1176                    case 'c':
1177                                 newprompt("Enter target room: ",
1178                                         targ,ROOMNAMELEN-1);
1179                                 if (strlen(targ)>0) {
1180                                         sprintf(cmd,"MOVE %ld|%s|%d",
1181                                                 msg_arr[a],targ,
1182                                                 (e=='c' ? 1 : 0) );
1183                                         serv_puts(cmd);
1184                                         serv_gets(cmd);
1185                                         printf("%s\n",&cmd[4]);
1186                                         if (cmd[0]=='2') msg_arr[a]=0L;
1187                                         }
1188                                 else {
1189                                         goto RMSGREAD;
1190                                         }
1191                                 if (cmd[0]!='2') goto RMSGREAD;
1192                                 break;
1193                    case 'f':    newprompt("Which section? ", filename,
1194                                         ((sizeof filename) -1));
1195                                 snprintf(cmd, sizeof cmd,
1196                                         "OPNA %ld|%s", msg_arr[a], filename);
1197                                 serv_puts(cmd);
1198                                 serv_gets(cmd);
1199                                 if (cmd[0]=='2') {
1200                                         extract(filename, &cmd[4], 2);
1201                                         download_to_local_disk(filename,
1202                                                 extract_int(&cmd[4], 0));
1203                                         }
1204                                 else {
1205                                         printf("%s\n",&cmd[4]);
1206                                         }
1207                                 goto RMSGREAD;
1208                    case 'd':    printf("*** Delete this message? ");
1209                                 if (yesno()==1) {
1210                                         sprintf(cmd,"DELE %ld",msg_arr[a]);
1211                                         serv_puts(cmd);
1212                                         serv_gets(cmd);
1213                                         printf("%s\n",&cmd[4]);
1214                                         if (cmd[0]=='2') msg_arr[a]=0L;
1215                                         }
1216                                 else {
1217                                         goto RMSGREAD;
1218                                         }
1219                                 break;
1220                    case 'h':    read_message(msg_arr[a],READ_HEADER);
1221                                 goto RMSGREAD;
1222                    case 'r':    entmsg(1,(DEFAULT_ENTRY==46 ? 2 : 0));
1223                                 goto RMSGREAD;
1224                    case 'u':    list_urls();
1225                                 goto RMSGREAD;
1226                         }
1227                 } /* end for loop */
1228         } /* end read routine */
1229
1230
1231
1232
1233 /*
1234  * View and edit a system message
1235  */
1236 void edit_system_message(char *which_message)
1237 {
1238         char desc[64];
1239         char read_cmd[64];
1240         char write_cmd[64];
1241
1242         sprintf(desc, "system message '%s'", which_message);
1243         sprintf(read_cmd, "MESG %s", which_message);
1244         sprintf(write_cmd, "EMSG %s", which_message);
1245         do_edit(desc, read_cmd, "NOOP", write_cmd);
1246         }