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