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