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