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