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