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