]> code.citadel.org Git - citadel.git/blob - citadel/messages.c
Attachments continued...
[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         long 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         long 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;
534         int a,b,e_ex_code;
535         long now,beg;
536         char datestr[64];
537         int cksum = 0;
538         struct AttachedFile *AttachList = NULL;
539         struct AttachedFile *Aptr;
540         char buf[256];
541
542         if (mode==2) if (strlen(editor_path)==0) {
543                 printf("*** No editor available, using built-in editor\n");
544                 mode=0;
545                 }
546
547         time(&now);
548         strcpy(datestr,asctime(localtime(&now)));
549         datestr[strlen(datestr)-1] = 0;
550
551         if (room_flags & QR_ANONONLY) {
552                 printf(" ****");
553                 }
554         else {
555                 printf(" %s from %s",datestr,fullname);
556                 if (strlen(recipient)>0) printf(" to %s",recipient);
557                 }
558         printf("\n");
559
560         beg = 0L;
561
562         if (mode==1) printf("(Press ctrl-d when finished)\n");
563         if (mode==0) {
564                 fp=fopen(filename,"r");
565                 if (fp!=NULL) {
566                         fmout(screenwidth,fp,0,screenheight,0,0);
567                         beg = ftell(fp);
568                         fclose(fp);
569                         }
570                 else {
571                         fp=fopen(filename,"w");
572                         fclose(fp);
573                         }
574                 }
575
576 ME1:    switch(mode) {
577
578            case 0:
579                 fp=fopen(filename,"r+");
580                 citedit(fp,beg);
581                 fclose(fp);
582                 goto MECR;
583                 break;
584
585            case 1:
586                 fp=fopen(filename,"w");
587                 do {
588                         a=inkey(); if (a==255) a=32;
589                         if (a==13) a=10;
590                         if (a!=4) {
591                                 putc(a,fp);
592                                 putc(a,stdout);
593                                 }
594                         if (a==10) putc(13,stdout);
595                         } while(a!=4);
596                 fclose(fp);
597                 break;
598
599            case 2:
600                 e_ex_code = 1;  /* start with a failed exit code */
601                 editor_pid=fork();
602                 cksum = file_checksum(filename);
603                 if (editor_pid==0) {
604                         chmod(filename,0600);
605                         sttybbs(SB_RESTORE);
606                         execlp(editor_path,editor_path,filename,NULL);
607                         exit(1);
608                         }
609                 if (editor_pid>0) do {
610                         e_ex_code = 0;
611                         b=ka_wait(&e_ex_code);
612                         } while((b!=editor_pid)&&(b>=0));
613                 editor_pid = (-1);
614                 sttybbs(0);
615                 break;
616                 }
617
618 MECR:   if (mode==2) {
619                 if (file_checksum(filename) == cksum) {
620                         printf("*** Aborted message.\n");
621                         e_ex_code = 1;
622                         }
623                 if (e_ex_code==0) goto MEFIN;
624                 goto MEABT2;
625                 }
626 MECR1:  printf("Entry cmd (? for options) -> ");
627 MECR2:  b=inkey();
628         if (b==NEXT_KEY) b='n';
629         if (b==STOP_KEY) b='s';
630         b=(b&127); b=tolower(b);
631         if (b=='?') {
632                 printf("Help\n");
633                 formout("saveopt");
634                 goto MECR1;
635                 }
636         if (b=='a') { printf("Abort\n");        goto MEABT;     }
637         if (b=='c') { printf("Continue\n");     goto ME1;       }
638         if (b=='s') { printf("Save message\n"); goto MEFIN;     } 
639         if (b=='p') {
640                 printf("Print formatted\n");
641                 printf(" %s from %s",datestr,fullname);
642                 if (strlen(recipient)>0) printf(" to %s",recipient);
643                 printf("\n");
644                 fp=fopen(filename,"r");
645                 if (fp!=NULL) {
646                         fmout(screenwidth,fp,
647                                 ((userflags & US_PAGINATOR) ? 1 : 0),
648                                 screenheight,0,0);
649                         beg = ftell(fp);
650                         fclose(fp);
651                         }
652                 goto MECR;
653                 }
654         if (b=='r') {
655                 printf("Replace string\n");
656                 replace_string(filename,0L);
657                 goto MECR;
658                 }
659         if (b=='h') {
660                 printf("Hold message\n");
661                 return(2);
662                 }
663         /* if ((b=='f')&&(rc_allow_attachments==1)) { */
664         if (b=='f') {
665                 printf("attach File\n");
666                 if (strlen(boundary)==0) {
667                         sprintf(boundary, "Citadel-Attachment-%ld.%d",
668                                 time(NULL), getpid() );
669                         }
670                 newprompt("Filename: ", buf, 68);
671                 if (access(buf, R_OK)==0) {
672                         Aptr = (struct AttachedFile *)
673                                 malloc(sizeof(struct AttachedFile));
674                         strcpy(Aptr->filename, buf);
675                         Aptr->next = AttachList;
676                         AttachList = Aptr;
677                         }
678                 else {
679                         printf("*** Cannot open %s: %s\n",
680                                 buf, strerror(errno));
681                         }
682                 goto MECR;
683                 }
684         goto MECR2;
685
686 MEFIN:  return(0);
687
688 MEABT:  printf("Are you sure? ");
689         if (yesno()==0) goto ME1;
690 MEABT2: unlink(filename);
691         return(2);
692         }
693
694 /*
695  * transmit message text to the server
696  */
697 void transmit_message(FILE *fp)
698 {
699         char buf[256];
700         int ch,a;
701         
702         strcpy(buf,"");
703         while (ch=getc(fp), (ch>=0)) {
704                 if (ch==10) {
705                         if (!strcmp(buf,"000")) strcpy(buf,">000");
706                         serv_puts(buf);
707                         strcpy(buf,"");
708                         }
709                 else {
710                         a = strlen(buf);
711                         buf[a+1] = 0;
712                         buf[a] = ch;
713                         if ((ch==32)&&(strlen(buf)>200)) {
714                                 buf[a]=0;
715                                 if (!strcmp(buf,"000")) strcpy(buf,">000");
716                                 serv_puts(buf);
717                                 strcpy(buf,"");
718                                 }
719                         if (strlen(buf)>250) {
720                                 if (!strcmp(buf,"000")) strcpy(buf,">000");
721                                 serv_puts(buf);
722                                 strcpy(buf,"");
723                                 }
724                         }
725                 }
726         serv_puts(buf);
727         }
728
729
730
731 /*
732  * entmsg()  -  edit and create a message
733  *              returns 0 if message was saved
734  */
735 int entmsg(int is_reply, int c)
736                         /* nonzero if this was a <R>eply command */
737        {                /* */
738         char buf[300];
739         char cmd[256];
740         char boundary[256];
741         int a,b;
742         int need_recp = 0;
743         int mode;
744         long highmsg;
745         FILE *fp;
746
747         if (c>0) mode=1;
748         else mode=0;
749
750         sprintf(cmd,"ENT0 0||0|%d",mode);
751         serv_puts(cmd);
752         serv_gets(cmd);
753
754         if ((strncmp(cmd,"570",3)) && (strncmp(cmd,"200",3))) {
755                 printf("%s\n",&cmd[4]);
756                 return(1);
757                 }
758         need_recp = 0;
759         if (!strncmp(cmd,"570",3)) need_recp = 1;
760
761         if ((userflags & US_EXPERT) == 0) formout("entermsg");
762                 
763         strcpy(buf,"");
764         if (need_recp==1) {
765                 if (axlevel>=2) {
766                         if (is_reply) {
767                                 strcpy(buf,reply_to);
768                                 }
769                         else {
770                                 printf("Enter recipient: ");
771                                 getline(buf,299);
772                                 if (strlen(buf)==0) return(1);
773                                 }
774                         }
775                 else strcpy(buf,"sysop");
776                 }
777
778         b=0;
779         if (room_flags&QR_ANON2) {
780                 printf("Anonymous (Y/N)? ");
781                 if (yesno()==1) b=1;
782                 }
783
784 /* if it's mail, we've got to check the validity of the recipient... */
785         if (strlen(buf)>0) {
786                 sprintf(cmd,"ENT0 0|%s|%d|%d",buf,b,mode);
787                 serv_puts(cmd);
788                 serv_gets(cmd);
789                 if (cmd[0]!='2') {
790                         printf("%s\n",&cmd[4]);
791                         return(1);
792                         }
793                 }
794
795 /* learn the number of the newest message in in the room, so we can tell
796  * upon saving whether someone else has posted too
797  */
798         num_msgs = 0;
799         serv_puts("MSGS LAST|1");
800         serv_gets(cmd);
801         if (cmd[0]!='1') {
802                 printf("%s\n",&cmd[5]);
803                 }
804         else {
805                 while (serv_gets(cmd), strcmp(cmd,"000")) {
806                         msg_arr[num_msgs++] = atol(cmd);
807                         }
808                 }
809
810 /* now put together the message */
811         strcpy(boundary, "");
812         if ( make_message(temp,buf,b,0,c,boundary) != 0 ) return(2);
813
814 /* and send it to the server */
815         sprintf(cmd,"ENT0 1|%s|%d|%d||%s|",buf,b,mode,boundary);
816         serv_puts(cmd);
817         serv_gets(cmd);
818         if (cmd[0]!='4') {
819                 printf("%s\n",&cmd[4]);
820                 return(1);
821                 }
822         fp=fopen(temp,"r");
823         if (fp!=NULL) {
824                 transmit_message(fp);
825                 fclose(fp);
826                 }
827         serv_puts("000");
828         unlink(temp);
829         
830         highmsg = msg_arr[num_msgs - 1];
831         num_msgs = 0;
832         serv_puts("MSGS NEW");
833         serv_gets(cmd);
834         if (cmd[0]!='1') {
835                 printf("%s\n",&cmd[5]);
836                 }
837         else {
838                 while (serv_gets(cmd), strcmp(cmd,"000")) {
839                         msg_arr[num_msgs++] = atol(cmd);
840                         }
841                 }
842
843         /* get new highest message number in room to set lrp for goto... */
844         maxmsgnum = msg_arr[num_msgs - 1];
845
846         /* now see if anyone else has posted in here */
847         b=(-1);
848         for (a=0; a<num_msgs; ++a) if (msg_arr[a]>highmsg) ++b;
849
850         /* in the Mail> room, this algorithm always counts one message
851          * higher than in public rooms, so we decrement it by one */
852         if (need_recp) --b;
853
854         if (b==1) printf(
855 "*** 1 additional message has been entered in this room by another user.\n");
856         if (b>1) printf(
857 "*** %d additional messages have been entered in this room by other users.\n",b);
858
859         return(0);
860         }
861
862 void process_quote(void) {      /* do editing on quoted file */
863 FILE *qfile,*tfile;
864 char buf[128];
865 int line,qstart,qend;
866
867         /* Unlink the second temp file as soon as it's opened, so it'll get
868          * deleted even if the program dies
869          */
870         qfile = fopen(temp2,"r");
871         unlink(temp2);
872
873         /* Display the quotable text with line numbers added */
874         line = 0;
875         fgets(buf,128,qfile);
876         while (fgets(buf,128,qfile)!=NULL) {
877                 printf("%2d %s",++line,buf);
878                 }
879         printf("Begin quoting at [ 1] : ");
880         getline(buf,3);
881         qstart = (buf[0]==0) ? (1) : atoi(buf);
882         printf("  End quoting at [%d] : ",line);
883         getline(buf,3);
884         qend = (buf[0]==0) ? (line) : atoi(buf);
885         rewind(qfile);
886         line=0;
887         fgets(buf,128,qfile);
888         tfile=fopen(temp,"w");
889         while(fgets(buf,128,qfile)!=NULL) {
890                 if ((++line>=qstart)&&(line<=qend)) fprintf(tfile," >%s",buf);
891                 }
892         fprintf(tfile," \n");
893         fclose(qfile);
894         fclose(tfile);
895         chmod(temp,0666);
896         }
897
898
899 void readmsgs(int c, int rdir, int q)   /* read contents of a room */
900                 /* 0=Read all  1=Read new  2=Read old 3=Read last q */
901                 /* 1=Forward (-1)=Reverse */
902                 /* Number of msgs to read (if c==3) */
903         {
904         int a,b,e,f,g,start;
905         int hold_sw = 0;
906         char arcflag = 0;
907         char quotflag = 0;
908         char prtfile[16];
909         char pagin;
910         char cmd[256];
911         char targ[20];
912
913         signal(SIGINT,SIG_IGN);
914         signal(SIGQUIT,SIG_IGN);
915
916         if (c<0) b=(MAXMSGS-1);
917         else b=0;
918
919         sprintf(prtfile,"/tmp/CPrt%d",getpid());
920
921         num_msgs = 0;
922         strcpy(cmd,"MSGS ");
923         switch(c) {
924                 case 0: strcat(cmd,"ALL");
925                         break;
926                 case 1: strcat(cmd,"NEW");
927                         break;
928                 case 2: strcat(cmd,"OLD");
929                         break;
930                 case 3: sprintf(&cmd[strlen(cmd)], "LAST|%d", q);
931                         break;
932                 }
933         serv_puts(cmd);
934         serv_gets(cmd);
935         if (cmd[0]!='1') {
936                 printf("%s\n",&cmd[5]);
937                 }
938         else {
939                 while (serv_gets(cmd), strcmp(cmd,"000")) {
940                         msg_arr[num_msgs++] = atol(cmd);
941                         }
942                 }
943
944         lines_printed = 0;
945
946         /* this loop cycles through each message... */
947         start = ( (rdir==1) ? 0 : (num_msgs-1) );
948         for (a=start; ((a<num_msgs)&&(a>=0)); a=a+rdir) {
949                 while (msg_arr[a]==0L) {
950                         a=a+rdir; if ((a==MAXMSGS)||(a==(-1))) return;
951                         }
952
953 RAGAIN:         pagin=((arcflag==0)&&(quotflag==0)&&
954                         (userflags & US_PAGINATOR)) ? 1 : 0;
955
956         /* if we're doing a quote, set the screenwidth to 72 temporarily */
957                 if (quotflag) {
958                         hold_sw = screenwidth;
959                         screenwidth = 72;
960                         }
961
962         /* now read the message... */
963                 e=read_message(msg_arr[a],pagin);
964
965         /* ...and set the screenwidth back if we have to */
966                 if (quotflag) {
967                         screenwidth = hold_sw;
968                         }
969 RMSGREAD:       fflush(stdout);
970                 highest_msg_read = msg_arr[a];
971                 if (quotflag) {
972                         freopen("/dev/tty","r+",stdout);
973                         quotflag=0;
974                         process_quote();
975                         }
976                 if (arcflag) {
977                         freopen("/dev/tty","r+",stdout);
978                         arcflag=0;
979                         f=fork();
980                         if (f==0) {
981                                 freopen(prtfile,"r",stdin);
982                                 sttybbs(SB_RESTORE);
983                                 ka_system(printcmd);
984                                 sttybbs(SB_NO_INTR);
985                                 unlink(prtfile);
986                                 exit(0);
987                                 }
988                         if (f>0) do {
989                                 g=wait(NULL);
990                                 } while((g!=f)&&(g>=0));
991                         printf("Message printed.\n");
992                         }
993                 if (e==3) return;
994                 if ((userflags&US_NOPROMPT)||(e==2)) e='n';
995                 else {
996                         printf("(%d) ",num_msgs-a-1);
997                         if (is_mail==1) printf("<R>eply ");
998                         if (strlen(printcmd)>0) printf("<P>rint ");
999                 printf("<B>ack <A>gain <Q>uote <H>eader <N>ext <S>top -> ");
1000                         do {
1001                                 lines_printed = 2;
1002                                 e=(inkey()&127); e=tolower(e);
1003 /* return key same as <N> */    if (e==13) e='n';
1004 /* del/move for aides only */   if (!is_room_aide) if ((e=='d')||(e=='m')) e=0;
1005 /* print only if available */   if ((e=='p')&&(strlen(printcmd)==0)) e=0;
1006 /* can't move from Mail> */     if ((e=='m')&&(is_mail==1)) e=0;
1007 /* can't reply in public rms */ if ((e=='r')&&(is_mail!=1)) e=0;
1008                                 } while((e!='a')&&(e!='n')&&(e!='s')
1009                                         &&(e!='d')&&(e!='m')&&(e!='p')
1010                                         &&(e!='q')&&(e!='b')&&(e!='h')
1011                                         &&(e!='r'));
1012                         switch(e) {
1013                                 case 's':       printf("Stop\r");       break;
1014                                 case 'a':       printf("Again\r");      break;
1015                                 case 'd':       printf("Delete\r");     break;
1016                                 case 'm':       printf("Move\r");       break;
1017                                 case 'n':       printf("Next\r");       break;
1018                                 case 'p':       printf("Print\r");      break;
1019                                 case 'q':       printf("Quote\r");      break;
1020                                 case 'b':       printf("Back\r");       break;
1021                                 case 'h':       printf("Header\r");     break;
1022                                 case 'r':       printf("Reply\r");      break;
1023                                 }
1024                         if (userflags & US_DISAPPEAR)
1025                                 printf("%75s\r","");
1026                         else
1027                                 printf("\n");
1028                         fflush(stdout);
1029                         }
1030                 switch(e) {
1031                    case 'p':    fflush(stdout);
1032                                 freopen(prtfile,"w",stdout);
1033                                 arcflag = 1;
1034                                 goto RAGAIN;
1035                    case 'q':    fflush(stdout);
1036                                 freopen(temp2,"w",stdout);
1037                                 quotflag = 1;
1038                                 goto RAGAIN;
1039                    case 's':    return;
1040                    case 'a':    goto RAGAIN;
1041                    case 'b':    a=a-(rdir*2);
1042                                 break;
1043                    case 'm':    newprompt("Enter target room: ",targ,19);
1044                                 if (strlen(targ)>0) {
1045                                         sprintf(cmd,"MOVE %ld|%s",
1046                                                 msg_arr[a],targ);
1047                                         serv_puts(cmd);
1048                                         serv_gets(cmd);
1049                                         printf("%s\n",&cmd[4]);
1050                                         if (cmd[0]=='2') msg_arr[a]=0L;
1051                                         }
1052                                 else {
1053                                         goto RMSGREAD;
1054                                         }
1055                                 if (cmd[0]!='2') goto RMSGREAD;
1056                                 break;
1057                    case 'd':    printf("*** Delete this message? ");
1058                                 if (yesno()==1) {
1059                                         sprintf(cmd,"DELE %ld",msg_arr[a]);
1060                                         serv_puts(cmd);
1061                                         serv_gets(cmd);
1062                                         printf("%s\n",&cmd[4]);
1063                                         if (cmd[0]=='2') msg_arr[a]=0L;
1064                                         }
1065                                 else {
1066                                         goto RMSGREAD;
1067                                         }
1068                                 break;
1069                    case 'h':    read_message(msg_arr[a],READ_HEADER);
1070                                 goto RMSGREAD;
1071                    case 'r':    entmsg(1,(DEFAULT_ENTRY==46 ? 2 : 0));
1072                                 goto RMSGREAD;
1073                         }
1074                 } /* end for loop */
1075         } /* end read routine */
1076
1077
1078
1079
1080 /*
1081  * View and edit a system message
1082  */
1083 void edit_system_message(char *which_message)
1084 {
1085         char desc[64];
1086         char read_cmd[64];
1087         char write_cmd[64];
1088
1089         sprintf(desc, "system message '%s'", which_message);
1090         sprintf(read_cmd, "MESG %s", which_message);
1091         sprintf(write_cmd, "EMSG %s", which_message);
1092         do_edit(desc, read_cmd, "NOOP", write_cmd);
1093         }