22d2c49530eec481a104af9b3e5003433f6f32f6
[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 CtdlServInfo 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,"subj=",5))
383                         strcpy(m_subject,&buf[5]);
384                 if ((!struncmp(buf,"hnod=",5)) 
385                    && (strucmp(&buf[5],serv_info.serv_humannode)))
386                         printf("(%s) ",&buf[5]);
387                 if ((!struncmp(buf,"room=",5))
388                    && (strucmp(&buf[5],room_name)))
389                         printf("in %s> ",&buf[5]);
390
391                 if (!struncmp(buf,"node=",5)) {
392                         if ( (room_flags&QR_NETWORK)
393                            || ((strucmp(&buf[5],serv_info.serv_nodename)
394                            &&(strucmp(&buf[5],serv_info.serv_fqdn)))))
395                                 {
396                                 printf("@%s ",&buf[5]);
397                                 }
398                         if ((!strucmp(&buf[5],serv_info.serv_nodename))
399                            ||(!strucmp(&buf[5],serv_info.serv_fqdn)))
400                                 {
401                                 strcpy(reply_to,from);
402                                 }
403                         else {
404                                 sprintf(reply_to,"%s @ %s",from,&buf[5]);
405                                 }
406                         }
407
408                 if (!struncmp(buf,"rcpt=",5))
409                         printf("to %s ",&buf[5]);
410                 if (!struncmp(buf,"time=",5)) {
411                         now=atol(&buf[5]);
412                         tm=(struct tm *)localtime(&now);
413                         strcpy(buf,asctime(tm)); buf[strlen(buf)-1]=0;
414                         strcpy(&buf[16],&buf[19]);
415                         printf("%s ",&buf[4]);
416                         }
417                 }
418
419         if (nhdr==1) {
420                 if (!is_room_aide) {
421                         printf(" ****");
422                         }
423                 else {
424                         printf(" %s",from);
425                         }
426                 }
427         printf("\n");
428         if (pagin == 1 ) color(2);
429         ++lines_printed;
430         lines_printed = checkpagin(lines_printed,pagin,screenheight);
431
432         if (strlen(m_subject)>0) {
433                 printf("Subject: %s\n",m_subject);
434                 ++lines_printed;
435                 lines_printed = checkpagin(lines_printed,pagin,screenheight);
436                 }
437
438         if (format_type == 0) {
439                 fr=fmout(screenwidth,NULL,
440                         ((pagin==1) ? 1 : 0),
441                         screenheight,(-1),1);
442                 }
443         else {
444                 while(serv_gets(buf), strcmp(buf,"000")) {
445                         if (sigcaught==0) {
446                                 printf("%s\n",buf);
447                                 lines_printed = lines_printed + 1 +
448                                         (strlen(buf)/screenwidth);
449                                 lines_printed =
450                                         checkpagin(lines_printed,pagin,screenheight);
451                                 }
452                         }
453                 fr = sigcaught;
454                 }
455         printf("\n");
456         ++lines_printed;
457         lines_printed = checkpagin(lines_printed,pagin,screenheight);
458
459         if (pagin == 1) color(7);
460         sttybbs(0);
461         return(fr);
462         }
463
464 /*
465  * replace string function for the built-in editor
466  */
467 void replace_string(char *filename, long int startpos)
468 {
469         char buf[512];
470         char srch_str[128];
471         char rplc_str[128];
472         FILE *fp;
473         int a;
474         long rpos,wpos;
475         char *ptr;
476         int substitutions = 0;
477         long msglen = 0L;
478
479         printf("Enter text to be replaced:\n: ");
480         getline(srch_str,128);
481         if (strlen(srch_str)==0) return;
482         
483         printf("Enter text to replace it with:\n: ");
484         getline(rplc_str,128);
485
486         fp=fopen(filename,"r+");
487         if (fp==NULL) return;
488
489         wpos=startpos;
490         fseek(fp,startpos,0);
491         strcpy(buf,"");
492         while (a=getc(fp), a>0) {
493                 ++msglen;
494                 buf[strlen(buf)+1] = 0;
495                 buf[strlen(buf)] = a;
496                 if ( strlen(buf) >= strlen(srch_str) ) {
497                         ptr=&buf[strlen(buf)-strlen(srch_str)];
498                         if (!struncmp(ptr,srch_str,strlen(srch_str))) {
499                                 strcpy(ptr,rplc_str);
500                                 ++substitutions;
501                                 }
502                         }
503                 if (strlen(buf)>384) {
504                         rpos=ftell(fp);
505                         fseek(fp,wpos,0);
506                         fwrite((char *)buf,128,1,fp);
507                         strcpy(buf,&buf[128]);
508                         wpos = ftell(fp);
509                         fseek(fp,rpos,0);
510                         }
511                 }
512         fseek(fp,wpos,0);
513         if (strlen(buf)>0) fwrite((char *)buf,strlen(buf),1,fp);
514         putc(0,fp);
515         fclose(fp);
516         printf("<R>eplace made %d substitution(s).\n\n",substitutions);
517         }
518
519
520 int make_message(char *filename, char *recipient, int anon_type, int format_type, int mode)
521                         /* temporary file name */
522                         /* NULL if it's not mail */
523                         /* see MES_ types in header file */
524                 
525          
526
527         FILE *fp;
528         int a,b,e_ex_code;
529         long now,beg;
530         char datestr[64];
531         int cksum = 0;
532
533         if (mode==2) if (strlen(editor_path)==0) {
534                 printf("*** No editor available, using built-in editor\n");
535                 mode=0;
536                 }
537
538         time(&now);
539         strcpy(datestr,asctime(localtime(&now)));
540         datestr[strlen(datestr)-1] = 0;
541
542         if (room_flags & QR_ANONONLY) {
543                 printf(" ****");
544                 }
545         else {
546                 printf(" %s from %s",datestr,fullname);
547                 if (strlen(recipient)>0) printf(" to %s",recipient);
548                 }
549         printf("\n");
550
551         beg = 0L;
552
553         if (mode==1) printf("(Press ctrl-d when finished)\n");
554         if (mode==0) {
555                 fp=fopen(filename,"r");
556                 if (fp!=NULL) {
557                         fmout(screenwidth,fp,0,screenheight,0,0);
558                         beg = ftell(fp);
559                         fclose(fp);
560                         }
561                 else {
562                         fp=fopen(filename,"w");
563                         fclose(fp);
564                         }
565                 }
566
567 ME1:    switch(mode) {
568
569            case 0:
570                 fp=fopen(filename,"r+");
571                 citedit(fp,beg);
572                 fclose(fp);
573                 goto MECR;
574                 break;
575
576            case 1:
577                 fp=fopen(filename,"w");
578                 do {
579                         a=inkey(); if (a==255) a=32;
580                         if (a==13) a=10;
581                         if (a!=4) {
582                                 putc(a,fp);
583                                 putc(a,stdout);
584                                 }
585                         if (a==10) putc(13,stdout);
586                         } while(a!=4);
587                 fclose(fp);
588                 break;
589
590            case 2:
591                 e_ex_code = 1;  /* start with a failed exit code */
592                 editor_pid=fork();
593                 cksum = file_checksum(filename);
594                 if (editor_pid==0) {
595                         chmod(filename,0600);
596                         sttybbs(SB_RESTORE);
597                         execlp(editor_path,editor_path,filename,NULL);
598                         exit(1);
599                         }
600                 if (editor_pid>0) do {
601                         e_ex_code = 0;
602                         b=ka_wait(&e_ex_code);
603                         } while((b!=editor_pid)&&(b>=0));
604                 editor_pid = (-1);
605                 sttybbs(0);
606                 break;
607                 }
608
609 MECR:   if (mode==2) {
610                 if (file_checksum(filename) == cksum) {
611                         printf("*** Aborted message.\n");
612                         e_ex_code = 1;
613                         }
614                 if (e_ex_code==0) goto MEFIN;
615                 goto MEABT2;
616                 }
617 MECR1:  printf("Entry cmd (? for options) -> ");
618 MECR2:  b=inkey();
619         if (b==NEXT_KEY) b='n';
620         if (b==STOP_KEY) b='s';
621         b=(b&127); b=tolower(b);
622         if (b=='?') {
623                 printf("Help\n");
624                 formout("saveopt");
625                 goto MECR1;
626                 }
627         if (b=='a') { printf("Abort\n");        goto MEABT;     }
628         if (b=='c') { printf("Continue\n");     goto ME1;       }
629         if (b=='s') { printf("Save message\n"); goto MEFIN;     } 
630         if (b=='p') {
631                 printf("Print formatted\n");
632                 printf(" %s from %s",datestr,fullname);
633                 if (strlen(recipient)>0) printf(" to %s",recipient);
634                 printf("\n");
635                 fp=fopen(filename,"r");
636                 if (fp!=NULL) {
637                         fmout(screenwidth,fp,
638                                 ((userflags & US_PAGINATOR) ? 1 : 0),
639                                 screenheight,0,0);
640                         beg = ftell(fp);
641                         fclose(fp);
642                         }
643                 goto MECR;
644                 }
645         if (b=='r') {
646                 printf("Replace string\n");
647                 replace_string(filename,0L);
648                 goto MECR;
649                 }
650         if (b=='h') {
651                 printf("Hold message\n");
652                 return(2);
653                 }
654         goto MECR2;
655
656 MEFIN:  return(0);
657
658 MEABT:  printf("Are you sure? ");
659         if (yesno()==0) goto ME1;
660 MEABT2: unlink(filename);
661         return(2);
662         }
663
664 /*
665  * transmit message text to the server
666  */
667 void transmit_message(FILE *fp)
668 {
669         char buf[256];
670         int ch,a;
671         
672         strcpy(buf,"");
673         while (ch=getc(fp), (ch>=0)) {
674                 if (ch==10) {
675                         if (!strcmp(buf,"000")) strcpy(buf,">000");
676                         serv_puts(buf);
677                         strcpy(buf,"");
678                         }
679                 else {
680                         a = strlen(buf);
681                         buf[a+1] = 0;
682                         buf[a] = ch;
683                         if ((ch==32)&&(strlen(buf)>200)) {
684                                 buf[a]=0;
685                                 if (!strcmp(buf,"000")) strcpy(buf,">000");
686                                 serv_puts(buf);
687                                 strcpy(buf,"");
688                                 }
689                         if (strlen(buf)>250) {
690                                 if (!strcmp(buf,"000")) strcpy(buf,">000");
691                                 serv_puts(buf);
692                                 strcpy(buf,"");
693                                 }
694                         }
695                 }
696         serv_puts(buf);
697         }
698
699
700
701 /*
702  * entmsg()  -  edit and create a message
703  *              returns 0 if message was saved
704  */
705 int entmsg(int is_reply, int c)
706                         /* nonzero if this was a <R>eply command */
707        {                /* */
708         char buf[300];
709         char cmd[256];
710         int a,b;
711         int need_recp = 0;
712         int mode;
713         long highmsg;
714         FILE *fp;
715
716         if (c>0) mode=1;
717         else mode=0;
718
719         sprintf(cmd,"ENT0 0||0|%d",mode);
720         serv_puts(cmd);
721         serv_gets(cmd);
722
723         if ((strncmp(cmd,"570",3)) && (strncmp(cmd,"200",3))) {
724                 printf("%s\n",&cmd[4]);
725                 return(1);
726                 }
727         need_recp = 0;
728         if (!strncmp(cmd,"570",3)) need_recp = 1;
729
730         if ((userflags & US_EXPERT) == 0) formout("entermsg");
731                 
732         strcpy(buf,"");
733         if (need_recp==1) {
734                 if (axlevel>=2) {
735                         if (is_reply) {
736                                 strcpy(buf,reply_to);
737                                 }
738                         else {
739                                 printf("Enter recipient: ");
740                                 getline(buf,299);
741                                 if (strlen(buf)==0) return(1);
742                                 }
743                         }
744                 else strcpy(buf,"sysop");
745                 }
746
747         b=0;
748         if (room_flags&QR_ANON2) {
749                 printf("Anonymous (Y/N)? ");
750                 if (yesno()==1) b=1;
751                 }
752
753 /* if it's mail, we've got to check the validity of the recipient... */
754         if (strlen(buf)>0) {
755                 sprintf(cmd,"ENT0 0|%s|%d|%d",buf,b,mode);
756                 serv_puts(cmd);
757                 serv_gets(cmd);
758                 if (cmd[0]!='2') {
759                         printf("%s\n",&cmd[4]);
760                         return(1);
761                         }
762                 }
763
764 /* learn the number of the newest message in in the room, so we can tell
765  * upon saving whether someone else has posted too
766  */
767         num_msgs = 0;
768         serv_puts("MSGS LAST|1");
769         serv_gets(cmd);
770         if (cmd[0]!='1') {
771                 printf("%s\n",&cmd[5]);
772                 }
773         else {
774                 while (serv_gets(cmd), strcmp(cmd,"000")) {
775                         msg_arr[num_msgs++] = atol(cmd);
776                         }
777                 }
778
779 /* now put together the message */
780         a=make_message(temp,buf,b,0,c);
781         if (a!=0)
782         {
783            return(2);
784         }
785
786 /* and send it to the server */
787         sprintf(cmd,"ENT0 1|%s|%d|%d",buf,b,mode);
788         serv_puts(cmd);
789         serv_gets(cmd);
790         if (cmd[0]!='4') {
791                 printf("%s\n",&cmd[4]);
792                 return(1);
793                 }
794         fp=fopen(temp,"r");
795         if (fp!=NULL) {
796                 transmit_message(fp);
797                 fclose(fp);
798                 }
799         serv_puts("000");
800         unlink(temp);
801         
802         highmsg = msg_arr[num_msgs - 1];
803         num_msgs = 0;
804         serv_puts("MSGS NEW");
805         serv_gets(cmd);
806         if (cmd[0]!='1') {
807                 printf("%s\n",&cmd[5]);
808                 }
809         else {
810                 while (serv_gets(cmd), strcmp(cmd,"000")) {
811                         msg_arr[num_msgs++] = atol(cmd);
812                         }
813                 }
814
815         /* get new highest message number in room to set lrp for goto... */
816         maxmsgnum = msg_arr[num_msgs - 1];
817
818         /* now see if anyone else has posted in here */
819         b=(-1);
820         for (a=0; a<num_msgs; ++a) if (msg_arr[a]>highmsg) ++b;
821
822         /* in the Mail> room, this algorithm always counts one message
823          * higher than in public rooms, so we decrement it by one */
824         if (need_recp) --b;
825
826         if (b==1) printf(
827 "*** 1 additional message has been entered in this room by another user.\n");
828         if (b>1) printf(
829 "*** %d additional messages have been entered in this room by other users.\n",b);
830
831         return(0);
832         }
833
834 void process_quote(void) {      /* do editing on quoted file */
835 FILE *qfile,*tfile;
836 char buf[128];
837 int line,qstart,qend;
838
839         qfile = fopen(temp2,"r");
840         line = 0;
841         fgets(buf,128,qfile);
842         while (fgets(buf,128,qfile)!=NULL) {
843                 printf("%2d %s",++line,buf);
844                 }
845         printf("Begin quoting at [ 1] : ");
846         getline(buf,3);
847         qstart = (buf[0]==0) ? (1) : atoi(buf);
848         printf("  End quoting at [%d] : ",line);
849         getline(buf,3);
850         qend = (buf[0]==0) ? (line) : atoi(buf);
851         rewind(qfile);
852         line=0;
853         fgets(buf,128,qfile);
854         tfile=fopen(temp,"w");
855         while(fgets(buf,128,qfile)!=NULL) {
856                 if ((++line>=qstart)&&(line<=qend)) fprintf(tfile," >%s",buf);
857                 }
858         fprintf(tfile," \n");
859         fclose(qfile);
860         fclose(tfile);
861         unlink(temp2);
862         chmod(temp,0666);
863         }
864
865
866 void readmsgs(int c, int rdir, int q)   /* read contents of a room */
867                 /* 0=Read all  1=Read new  2=Read old 3=Read last q */
868                 /* 1=Forward (-1)=Reverse */
869                 /* Number of msgs to read (if c==3) */
870         {
871         int a,b,e,f,g,start;
872         int hold_sw = 0;
873         char arcflag = 0;
874         char quotflag = 0;
875         char prtfile[16];
876         char pagin;
877         char cmd[256];
878         char targ[20];
879
880         signal(SIGINT,SIG_IGN);
881         signal(SIGQUIT,SIG_IGN);
882
883         if (c<0) b=(MAXMSGS-1);
884         else b=0;
885
886         sprintf(prtfile,"/tmp/CPrt%d",getpid());
887
888         num_msgs = 0;
889         strcpy(cmd,"MSGS ");
890         switch(c) {
891                 case 0: strcat(cmd,"ALL");
892                         break;
893                 case 1: strcat(cmd,"NEW");
894                         break;
895                 case 2: strcat(cmd,"OLD");
896                         break;
897                 case 3: sprintf(&cmd[strlen(cmd)], "LAST|%d", q);
898                         break;
899                 }
900         serv_puts(cmd);
901         serv_gets(cmd);
902         if (cmd[0]!='1') {
903                 printf("%s\n",&cmd[5]);
904                 }
905         else {
906                 while (serv_gets(cmd), strcmp(cmd,"000")) {
907                         msg_arr[num_msgs++] = atol(cmd);
908                         }
909                 }
910
911         lines_printed = 0;
912
913         /* this loop cycles through each message... */
914         start = ( (rdir==1) ? 0 : (num_msgs-1) );
915         for (a=start; ((a<num_msgs)&&(a>=0)); a=a+rdir) {
916                 while (msg_arr[a]==0L) {
917                         a=a+rdir; if ((a==MAXMSGS)||(a==(-1))) return;
918                         }
919
920 RAGAIN:         pagin=((arcflag==0)&&(quotflag==0)&&
921                         (userflags & US_PAGINATOR)) ? 1 : 0;
922
923         /* if we're doing a quote, set the screenwidth to 72 temporarily */
924                 if (quotflag) {
925                         hold_sw = screenwidth;
926                         screenwidth = 72;
927                         }
928
929         /* now read the message... */
930                 e=read_message(msg_arr[a],pagin);
931
932         /* ...and set the screenwidth back if we have to */
933                 if (quotflag) {
934                         screenwidth = hold_sw;
935                         }
936 RMSGREAD:       fflush(stdout);
937                 highest_msg_read = msg_arr[a];
938                 if (quotflag) {
939                         freopen("/dev/tty","r+",stdout);
940                         quotflag=0;
941                         process_quote();
942                         }
943                 if (arcflag) {
944                         freopen("/dev/tty","r+",stdout);
945                         arcflag=0;
946                         f=fork();
947                         if (f==0) {
948                                 freopen(prtfile,"r",stdin);
949                                 sttybbs(SB_RESTORE);
950                                 ka_system(printcmd);
951                                 sttybbs(SB_NO_INTR);
952                                 unlink(prtfile);
953                                 exit(0);
954                                 }
955                         if (f>0) do {
956                                 g=wait(NULL);
957                                 } while((g!=f)&&(g>=0));
958                         printf("Message printed.\n");
959                         }
960                 if (e==3) return;
961                 if ((userflags&US_NOPROMPT)||(e==2)) e='n';
962                 else {
963                         printf("(%d) ",num_msgs-a-1);
964                         if (is_mail==1) printf("<R>eply ");
965                         if (strlen(printcmd)>0) printf("<P>rint ");
966                 printf("<B>ack <A>gain <Q>uote <H>eader <N>ext <S>top -> ");
967                         do {
968                                 lines_printed = 2;
969                                 e=(inkey()&127); e=tolower(e);
970 /* return key same as <N> */    if (e==13) e='n';
971 /* del/move for aides only */   if (!is_room_aide) if ((e=='d')||(e=='m')) e=0;
972 /* print only if available */   if ((e=='p')&&(strlen(printcmd)==0)) e=0;
973 /* can't move from Mail> */     if ((e=='m')&&(is_mail==1)) e=0;
974 /* can't reply in public rms */ if ((e=='r')&&(is_mail!=1)) e=0;
975                                 } while((e!='a')&&(e!='n')&&(e!='s')
976                                         &&(e!='d')&&(e!='m')&&(e!='p')
977                                         &&(e!='q')&&(e!='b')&&(e!='h')
978                                         &&(e!='r'));
979                         switch(e) {
980                                 case 's':       printf("Stop\r");       break;
981                                 case 'a':       printf("Again\r");      break;
982                                 case 'd':       printf("Delete\r");     break;
983                                 case 'm':       printf("Move\r");       break;
984                                 case 'n':       printf("Next\r");       break;
985                                 case 'p':       printf("Print\r");      break;
986                                 case 'q':       printf("Quote\r");      break;
987                                 case 'b':       printf("Back\r");       break;
988                                 case 'h':       printf("Header\r");     break;
989                                 case 'r':       printf("Reply\r");      break;
990                                 }
991                         if (userflags & US_DISAPPEAR)
992                                 printf("%75s\r","");
993                         else
994                                 printf("\n");
995                         fflush(stdout);
996                         }
997                 switch(e) {
998                    case 'p':    fflush(stdout);
999                                 freopen(prtfile,"w",stdout);
1000                                 arcflag = 1;
1001                                 goto RAGAIN;
1002                    case 'q':    fflush(stdout);
1003                                 freopen(temp2,"w",stdout);
1004                                 quotflag = 1;
1005                                 goto RAGAIN;
1006                    case 's':    return;
1007                    case 'a':    goto RAGAIN;
1008                    case 'b':    a=a-(rdir*2);
1009                                 break;
1010                    case 'm':    newprompt("Enter target room: ",targ,19);
1011                                 if (strlen(targ)>0) {
1012                                         sprintf(cmd,"MOVE %ld|%s",
1013                                                 msg_arr[a],targ);
1014                                         serv_puts(cmd);
1015                                         serv_gets(cmd);
1016                                         printf("%s\n",&cmd[4]);
1017                                         if (cmd[0]=='2') msg_arr[a]=0L;
1018                                         }
1019                                 else {
1020                                         goto RMSGREAD;
1021                                         }
1022                                 if (cmd[0]!='2') goto RMSGREAD;
1023                                 break;
1024                    case 'd':    printf("*** Delete this message? ");
1025                                 if (yesno()==1) {
1026                                         sprintf(cmd,"DELE %ld",msg_arr[a]);
1027                                         serv_puts(cmd);
1028                                         serv_gets(cmd);
1029                                         printf("%s\n",&cmd[4]);
1030                                         if (cmd[0]=='2') msg_arr[a]=0L;
1031                                         }
1032                                 else {
1033                                         goto RMSGREAD;
1034                                         }
1035                                 break;
1036                    case 'h':    read_message(msg_arr[a],READ_HEADER);
1037                                 goto RMSGREAD;
1038                    case 'r':    entmsg(1,(DEFAULT_ENTRY==46 ? 2 : 0));
1039                                 goto RMSGREAD;
1040                         }
1041                 } /* end for loop */
1042         } /* end read routine */
1043
1044
1045
1046
1047 /*
1048  * View and edit a system message
1049  */
1050 void edit_system_message(char *which_message)
1051 {
1052         char desc[64];
1053         char read_cmd[64];
1054         char write_cmd[64];
1055
1056         sprintf(desc, "system message '%s'", which_message);
1057         sprintf(read_cmd, "MESG %s", which_message);
1058         sprintf(write_cmd, "EMSG %s", which_message);
1059         do_edit(desc, read_cmd, "NOOP", write_cmd);
1060         }