]> code.citadel.org Git - citadel.git/blob - citadel/messages.c
* Remove nulls appended to editor files during replace, edit, and print
[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 <limits.h>
19 #include <sys/wait.h>
20 #include <sys/stat.h>
21 #include <stdarg.h>
22 #include "citadel.h"
23 #include "messages.h"
24 #include "commands.h"
25 #include "rooms.h"
26 #include "tools.h"
27 #ifndef HAVE_SNPRINTF
28 #include "snprintf.h"
29 #endif
30
31 #define MAXWORDBUF 256
32 #define MAXMSGS 512
33
34 struct cittext {
35         struct cittext *next;
36         char text[MAXWORDBUF];
37         };
38
39 void sttybbs(int cmd);
40 int struncmp(char *lstr, char *rstr, int len);
41 int fmout(int width, FILE *fp, char pagin, int height, int starting_lp, char subst);
42 int haschar(char *st, int ch);
43 int checkpagin(int lp, int pagin, int height);
44 void getline(char *string, int lim);
45 void formout(char *name);
46 int yesno(void);
47 void newprompt(char *prompt, char *str, int len);
48 int file_checksum(char *filename);
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(BRIGHT_RED);
120                 perror("Cannot fork");
121                 color(DIM_WHITE);
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
190  */
191 void citedit(FILE *fp)
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, 0L, 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, 0L, 0);
318         for (ptr=textlist; ptr!=NULL; ptr=ptr->next) {
319                 fprintf(fp,"%s",ptr->text);
320                 }
321         putc(10,fp);
322         fflush(fp);
323         ftruncate(fileno(fp), ftell(fp));
324
325         /* and deallocate the memory we used */
326         while (textlist!=NULL) {
327                 ptr=textlist->next;
328                 free(textlist);
329                 textlist=ptr;
330                 }
331         }
332
333
334 int read_message(long int num, char pagin) /* Read a message from the server */
335                                            /* message number */
336                 /* 0 = normal read, 1 = read with pagination, 2 = header */
337 {
338         char buf[256];
339         char m_subject[256];
340         char from[256];
341         time_t now;
342         struct tm *tm;
343         int format_type = 0;
344         int fr = 0;
345         int nhdr = 0;
346         int have_rfca = 0;
347
348         sigcaught = 0;
349         sttybbs(1);
350
351         sprintf(buf,"MSG0 %ld|%d",num,(pagin==READ_HEADER ? 1 : 0));
352         serv_puts(buf);
353         serv_gets(buf);
354         if (buf[0]!='1') {
355                 printf("*** msg #%ld: %s\n",num,buf);
356                 ++lines_printed;
357                 lines_printed = checkpagin(lines_printed,pagin,screenheight);
358                 sttybbs(0);
359                 return(0);
360                 }
361
362         strcpy(m_subject,"");
363         strcpy(reply_to, "nobody ... xxxxx");
364         printf("\n");
365         ++lines_printed;
366         lines_printed = checkpagin(lines_printed,pagin,screenheight);
367         printf(" ");
368         if (pagin == 1) color(BRIGHT_CYAN);
369
370         if (pagin==2) {
371                 while(serv_gets(buf), strcmp(buf,"000")) {
372                         if (buf[4]=='=') {
373                                 printf("%s\n",buf);
374                                 ++lines_printed;
375                                 lines_printed = 
376                                         checkpagin(lines_printed,
377                                                 pagin,screenheight);
378                                 }
379                         }
380                 sttybbs(0);
381                 return(0);
382                 }
383
384         while(serv_gets(buf), struncmp(buf,"text",4)) {
385                 if (!struncmp(buf,"nhdr=yes",8)) nhdr=1;
386                 if (!struncmp(buf,"from=",5)) {
387                         strcpy(from,&buf[5]);
388                         }
389                 if (nhdr==1) buf[0]='_';
390                 if (!struncmp(buf,"type=",5))
391                         format_type=atoi(&buf[5]);
392                 if ((!struncmp(buf,"msgn=",5))&&(rc_display_message_numbers)) {
393                         color(DIM_WHITE);
394                         printf("[");
395                         color(BRIGHT_WHITE);
396                         printf("#%s",&buf[5]);
397                         color(DIM_WHITE);
398                         printf("] ");
399                         }
400                 if (!struncmp(buf,"from=",5)) {
401                         color(DIM_WHITE);
402                         printf("from ");
403                         color(BRIGHT_CYAN);
404                         printf("%s ",&buf[5]);
405                         }
406                 if (!struncmp(buf,"subj=",5))
407                         strcpy(m_subject,&buf[5]);
408
409                 if (!struncmp(buf,"rfca=",5)) {
410                         strcpy(reply_to, &buf[5]);
411                         have_rfca = 1;
412                         color(DIM_WHITE);
413                         printf("<");
414                         color(BRIGHT_BLUE);
415                         printf("%s",&buf[5]);
416                         color(DIM_WHITE);
417                         printf("> ");
418                         }
419                 if ((!struncmp(buf,"hnod=",5)) 
420                    && (strucmp(&buf[5],serv_info.serv_humannode))
421                    && (have_rfca == 0) ) {
422                         color(DIM_WHITE);
423                         printf("(");
424                         color(BRIGHT_WHITE);
425                         printf("%s",&buf[5]);
426                         color(DIM_WHITE);
427                         printf(") ");
428                         }
429                 if ((!struncmp(buf,"room=",5))
430                    && (strucmp(&buf[5],room_name)) 
431                    && (have_rfca == 0)) {
432                         color(DIM_WHITE);
433                         printf("in ");
434                         color(BRIGHT_MAGENTA);
435                         printf("%s> ",&buf[5]);
436                         }
437
438                 if (!struncmp(buf,"node=",5)) {
439                         if ( (room_flags&QR_NETWORK)
440                            || ((strucmp(&buf[5],serv_info.serv_nodename)
441                            &&(strucmp(&buf[5],serv_info.serv_fqdn)))) ) 
442                                 {
443                                 if (have_rfca == 0) {
444                                         color(DIM_WHITE);
445                                         printf("@");
446                                         color(BRIGHT_YELLOW);
447                                         printf("%s ",&buf[5]);
448                                 }
449                         }
450                         if (have_rfca == 0) {
451                          if ((!strucmp(&buf[5],serv_info.serv_nodename))
452                            ||(!strucmp(&buf[5],serv_info.serv_fqdn)))
453                                 {
454                                 strcpy(reply_to,from);
455                                 }
456                         }
457                         else {
458                                 sprintf(reply_to,"%s @ %s",from,&buf[5]);
459                                 }
460                         }
461
462                 if (!struncmp(buf,"rcpt=",5)) {
463                         color(DIM_WHITE);
464                         printf("to ");
465                         color(BRIGHT_CYAN);
466                         printf("%s ",&buf[5]);
467                         }
468                 if (!struncmp(buf,"time=",5)) {
469                         now=atol(&buf[5]);
470                         tm=(struct tm *)localtime(&now);
471                         strcpy(buf,asctime(tm)); buf[strlen(buf)-1]=0;
472                         strcpy(&buf[16],&buf[19]);
473                         color(BRIGHT_MAGENTA);
474                         printf("%s ",&buf[4]);
475                         }
476                 }
477
478         if (nhdr==1) {
479                 if (!is_room_aide) {
480                         printf(" ****");
481                         }
482                 else {
483                         printf(" %s",from);
484                         }
485                 }
486         printf("\n");
487         if (pagin == 1) color(BRIGHT_WHITE);
488         ++lines_printed;
489         lines_printed = checkpagin(lines_printed,pagin,screenheight);
490
491         if (strlen(m_subject)>0) {
492                 printf("Subject: %s\n",m_subject);
493                 ++lines_printed;
494                 lines_printed = checkpagin(lines_printed,pagin,screenheight);
495                 }
496
497         if (format_type == 0) {
498                 fr=fmout(screenwidth,NULL,
499                         ((pagin==1) ? 1 : 0),
500                         screenheight,(-1),1);
501                 }
502         else {
503                 while(serv_gets(buf), strcmp(buf,"000")) {
504                         if (sigcaught==0) {
505                                 printf("%s\n",buf);
506                                 lines_printed = lines_printed + 1 +
507                                         (strlen(buf)/screenwidth);
508                                 lines_printed =
509                                         checkpagin(lines_printed,pagin,screenheight);
510                                 }
511                         }
512                 fr = sigcaught;
513                 }
514         printf("\n");
515         ++lines_printed;
516         lines_printed = checkpagin(lines_printed,pagin,screenheight);
517
518         if (pagin == 1) color(DIM_WHITE);
519         sttybbs(0);
520         return(fr);
521         }
522
523 /*
524  * replace string function for the built-in editor
525  */
526 void replace_string(char *filename, long int startpos)
527 {
528         char buf[512];
529         char srch_str[128];
530         char rplc_str[128];
531         FILE *fp;
532         int a;
533         long rpos,wpos;
534         char *ptr;
535         int substitutions = 0;
536         long msglen = 0L;
537
538         printf("Enter text to be replaced:\n: ");
539         getline(srch_str,128);
540         if (strlen(srch_str)==0) return;
541         
542         printf("Enter text to replace it with:\n: ");
543         getline(rplc_str,128);
544
545         fp=fopen(filename,"r+");
546         if (fp==NULL) return;
547
548         wpos=startpos;
549         fseek(fp,startpos,0);
550         strcpy(buf,"");
551         while (a=getc(fp), a>0) {
552                 ++msglen;
553                 buf[strlen(buf)+1] = 0;
554                 buf[strlen(buf)] = a;
555                 if ( strlen(buf) >= strlen(srch_str) ) {
556                         ptr=&buf[strlen(buf)-strlen(srch_str)];
557                         if (!struncmp(ptr,srch_str,strlen(srch_str))) {
558                                 strcpy(ptr,rplc_str);
559                                 ++substitutions;
560                                 }
561                         }
562                 if (strlen(buf)>384) {
563                         rpos=ftell(fp);
564                         fseek(fp,wpos,0);
565                         fwrite((char *)buf,128,1,fp);
566                         strcpy(buf,&buf[128]);
567                         wpos = ftell(fp);
568                         fseek(fp,rpos,0);
569                         }
570                 }
571         fseek(fp,wpos,0);
572         if (strlen(buf)>0) fwrite((char *)buf,strlen(buf),1,fp);
573         wpos = ftell(fp);
574         fclose(fp);
575         truncate(filename, wpos);
576         printf("<R>eplace made %d substitution(s).\n\n",substitutions);
577         }
578
579
580 int make_message(char *filename,        /* temporary file name */
581                 char *recipient,        /* NULL if it's not mail */
582                 int anon_type,          /* see MES_ types in header file */
583                 int format_type,
584                 int mode)
585
586         FILE *fp;
587         int a,b,e_ex_code;
588         time_t now;
589         long beg;
590         char datestr[64];
591         int cksum = 0;
592
593         if (mode==2) if (strlen(editor_path)==0) {
594                 printf("*** No editor available, using built-in editor\n");
595                 mode=0;
596                 }
597
598         time(&now);
599         strcpy(datestr,asctime(localtime(&now)));
600         datestr[strlen(datestr)-1] = 0;
601
602         if (room_flags & QR_ANONONLY) {
603                 printf(" ****");
604                 }
605         else {
606                 printf(" %s from %s",datestr,fullname);
607                 if (strlen(recipient)>0) printf(" to %s",recipient);
608                 }
609         printf("\n");
610
611         beg = 0L;
612
613         if (mode==1) printf("(Press ctrl-d when finished)\n");
614         if (mode==0) {
615                 fp=fopen(filename,"r");
616                 if (fp!=NULL) {
617                         fmout(screenwidth,fp,0,screenheight,0,0);
618                         beg = ftell(fp);
619                         fclose(fp);
620                         }
621                 else {
622                         fp=fopen(filename,"w");
623                         fclose(fp);
624                         }
625                 }
626
627 ME1:    switch(mode) {
628
629            case 0:
630                 fp=fopen(filename,"r+");
631                 citedit(fp);
632                 fclose(fp);
633                 goto MECR;
634
635            case 1:
636                 fp=fopen(filename,"w");
637                 do {
638                         a=inkey(); if (a==255) a=32;
639                         if (a==13) a=10;
640                         if (a!=4) {
641                                 putc(a,fp);
642                                 putc(a,stdout);
643                                 }
644                         if (a==10) putc(13,stdout);
645                         } while(a!=4);
646                 fclose(fp);
647                 break;
648
649            case 2:
650                 e_ex_code = 1;  /* start with a failed exit code */
651                 editor_pid=fork();
652                 cksum = file_checksum(filename);
653                 if (editor_pid==0) {
654                         chmod(filename,0600);
655                         sttybbs(SB_RESTORE);
656                         execlp(editor_path,editor_path,filename,NULL);
657                         exit(1);
658                         }
659                 if (editor_pid>0) do {
660                         e_ex_code = 0;
661                         b=ka_wait(&e_ex_code);
662                         } while((b!=editor_pid)&&(b>=0));
663                 editor_pid = (-1);
664                 sttybbs(0);
665                 break;
666                 }
667
668 MECR:   if (mode==2) {
669                 if (file_checksum(filename) == cksum) {
670                         printf("*** Aborted message.\n");
671                         e_ex_code = 1;
672                         }
673                 if (e_ex_code==0) goto MEFIN;
674                 goto MEABT2;
675                 }
676
677         b = keymenu("Entry command (? for options)",
678                 "<A>bort|<C>ontinue|<S>ave message|<P>rint formatted|"
679                 "<R>eplace string|<H>old message");
680
681         if (b=='a') goto MEABT;
682         if (b=='c') goto ME1;
683         if (b=='s') goto MEFIN;
684         if (b=='p') {
685                 printf(" %s from %s",datestr,fullname);
686                 if (strlen(recipient)>0) printf(" to %s",recipient);
687                 printf("\n");
688                 fp=fopen(filename,"r");
689                 if (fp!=NULL) {
690                         fmout(screenwidth,fp,
691                                 ((userflags & US_PAGINATOR) ? 1 : 0),
692                                 screenheight,0,0);
693                         beg = ftell(fp);
694                         fclose(fp);
695                         }
696                 goto MECR;
697                 }
698         if (b=='r') {
699                 replace_string(filename,0L);
700                 goto MECR;
701                 }
702         if (b=='h') {
703                 return(2);
704                 }
705
706 MEFIN:  return(0);
707
708 MEABT:  printf("Are you sure? ");
709         if (yesno()==0) goto ME1;
710 MEABT2: unlink(filename);
711         return(2);
712         }
713
714 /*
715  * transmit message text to the server
716  */
717 void transmit_message(FILE *fp)
718 {
719         char buf[256];
720         int ch,a;
721         long msglen;
722         time_t lasttick;
723
724         fseek(fp, 0L, SEEK_END);
725         msglen = ftell(fp);
726         rewind(fp);
727         lasttick = time(NULL);
728         strcpy(buf,"");
729         while (ch=getc(fp), (ch>=0)) {
730                 if (ch==10) {
731                         if (!strcmp(buf,"000")) strcpy(buf,">000");
732                         serv_puts(buf);
733                         strcpy(buf,"");
734                         }
735                 else {
736                         a = strlen(buf);
737                         buf[a+1] = 0;
738                         buf[a] = ch;
739                         if ((ch==32)&&(strlen(buf)>200)) {
740                                 buf[a]=0;
741                                 if (!strcmp(buf,"000")) strcpy(buf,">000");
742                                 serv_puts(buf);
743                                 strcpy(buf,"");
744                                 }
745                         if (strlen(buf)>250) {
746                                 if (!strcmp(buf,"000")) strcpy(buf,">000");
747                                 serv_puts(buf);
748                                 strcpy(buf,"");
749                                 }
750                         }
751
752                 if ( (time(NULL) - lasttick) > 2L ) {
753                         printf(" %3ld%% completed\r",
754                                 ((ftell(fp) * 100L) / msglen) );
755                         fflush(stdout);
756                         lasttick = time(NULL);
757                         }
758
759                 }
760         serv_puts(buf);
761         printf("                \r");
762         fflush(stdout);
763         }
764
765
766
767 /*
768  * entmsg()  -  edit and create a message
769  *              returns 0 if message was saved
770  */
771 int entmsg(int is_reply, int c)
772                         /* nonzero if this was a <R>eply command */
773        {                /* */
774         char buf[300];
775         char cmd[256];
776         int a,b;
777         int need_recp = 0;
778         int mode;
779         long highmsg;
780         FILE *fp;
781
782         if (c>0) mode=1;
783         else mode=0;
784
785         sprintf(cmd,"ENT0 0||0|%d",mode);
786         serv_puts(cmd);
787         serv_gets(cmd);
788
789         if ((strncmp(cmd,"570",3)) && (strncmp(cmd,"200",3))) {
790                 printf("%s\n",&cmd[4]);
791                 return(1);
792                 }
793         need_recp = 0;
794         if (!strncmp(cmd,"570",3)) need_recp = 1;
795
796         if ((userflags & US_EXPERT) == 0) formout("entermsg");
797                 
798         strcpy(buf,"");
799         if (need_recp==1) {
800                 if (axlevel>=2) {
801                         if (is_reply) {
802                                 strcpy(buf,reply_to);
803                                 }
804                         else {
805                                 printf("Enter recipient: ");
806                                 getline(buf, 60);
807                                 if (strlen(buf)==0) return(1);
808                                 }
809                         }
810                 else strcpy(buf,"sysop");
811                 }
812
813         b=0;
814         if (room_flags&QR_ANONOPT) {
815                 printf("Anonymous (Y/N)? ");
816                 if (yesno()==1) b=1;
817                 }
818
819 /* if it's mail, we've got to check the validity of the recipient... */
820         if (strlen(buf)>0) {
821                 sprintf(cmd,"ENT0 0|%s|%d|%d",buf,b,mode);
822                 serv_puts(cmd);
823                 serv_gets(cmd);
824                 if (cmd[0]!='2') {
825                         printf("%s\n",&cmd[4]);
826                         return(1);
827                         }
828                 }
829
830 /* learn the number of the newest message in in the room, so we can tell
831  * upon saving whether someone else has posted too
832  */
833         num_msgs = 0;
834         serv_puts("MSGS LAST|1");
835         serv_gets(cmd);
836         if (cmd[0]!='1') {
837                 printf("%s\n",&cmd[5]);
838                 }
839         else {
840                 while (serv_gets(cmd), strcmp(cmd,"000")) {
841                         msg_arr[num_msgs++] = atol(cmd);
842                         }
843                 }
844
845 /* now put together the message */
846         if ( make_message(temp,buf,b,0,c) != 0 ) return(2);
847
848 /* and send it to the server */
849         sprintf(cmd,"ENT0 1|%s|%d|%d||",buf,b,mode);
850         serv_puts(cmd);
851         serv_gets(cmd);
852         if (cmd[0]!='4') {
853                 printf("%s\n",&cmd[4]);
854                 return(1);
855                 }
856         fp=fopen(temp,"r");
857         if (fp!=NULL) {
858                 transmit_message(fp);
859                 fclose(fp);
860                 }
861         serv_puts("000");
862         unlink(temp);
863         
864         highmsg = msg_arr[num_msgs - 1];
865         num_msgs = 0;
866         serv_puts("MSGS NEW");
867         serv_gets(cmd);
868         if (cmd[0]!='1') {
869                 printf("%s\n",&cmd[5]);
870                 }
871         else {
872                 while (serv_gets(cmd), strcmp(cmd,"000")) {
873                         msg_arr[num_msgs++] = atol(cmd);
874                         }
875                 }
876
877         /* get new highest message number in room to set lrp for goto... */
878         maxmsgnum = msg_arr[num_msgs - 1];
879
880         /* now see if anyone else has posted in here */
881         b=(-1);
882         for (a=0; a<num_msgs; ++a) if (msg_arr[a]>highmsg) ++b;
883
884         /* in the Mail> room, this algorithm always counts one message
885          * higher than in public rooms, so we decrement it by one */
886         if (need_recp) --b;
887
888         if (b==1) printf(
889 "*** 1 additional message has been entered in this room by another user.\n");
890         if (b>1) printf(
891 "*** %d additional messages have been entered in this room by other users.\n",b);
892
893         return(0);
894         }
895
896 void process_quote(void) {      /* do editing on quoted file */
897 FILE *qfile,*tfile;
898 char buf[128];
899 int line,qstart,qend;
900
901         /* Unlink the second temp file as soon as it's opened, so it'll get
902          * deleted even if the program dies
903          */
904         qfile = fopen(temp2,"r");
905         unlink(temp2);
906
907         /* Display the quotable text with line numbers added */
908         line = 0;
909         fgets(buf,128,qfile);
910         while (fgets(buf,128,qfile)!=NULL) {
911                 printf("%2d %s",++line,buf);
912                 }
913         printf("Begin quoting at [ 1] : ");
914         getline(buf,3);
915         qstart = (buf[0]==0) ? (1) : atoi(buf);
916         printf("  End quoting at [%d] : ",line);
917         getline(buf,3);
918         qend = (buf[0]==0) ? (line) : atoi(buf);
919         rewind(qfile);
920         line=0;
921         fgets(buf,128,qfile);
922         tfile=fopen(temp,"w");
923         while(fgets(buf,128,qfile)!=NULL) {
924                 if ((++line>=qstart)&&(line<=qend)) fprintf(tfile," >%s",buf);
925                 }
926         fprintf(tfile," \n");
927         fclose(qfile);
928         fclose(tfile);
929         chmod(temp,0666);
930         }
931
932
933
934 /*
935  * List the URL's which were embedded in the previous message
936  */
937 void list_urls() {
938         int i;
939         char cmd[256];
940
941         if (num_urls == 0) {
942                 printf("There were no URL's in the previous message.\n\n");
943                 return;
944         }
945
946         for (i=0; i<num_urls; ++i) {
947                 printf("%3d %s\n", i+1, urls[i]);
948         }
949
950         if((i = num_urls) != 1)
951                 i = intprompt("Display which one", 1, 1, num_urls);
952
953         sprintf(cmd, rc_url_cmd, urls[i-1]);
954         system(cmd);
955         printf("\n");
956 }
957
958
959 void readmsgs(int c, int rdir, int q)   /* read contents of a room */
960                 /* 0=Read all  1=Read new  2=Read old 3=Read last q */
961                 /* 1=Forward (-1)=Reverse */
962                 /* Number of msgs to read (if c==3) */
963         {
964         int a,b,e,f,g,start;
965         int hold_sw = 0;
966         char arcflag = 0;
967         char quotflag = 0;
968         int hold_color = 0;
969         char prtfile[PATH_MAX];
970         char pagin;
971         char cmd[256];
972         char targ[ROOMNAMELEN];
973         char filename[256];
974
975         signal(SIGINT,SIG_IGN);
976         signal(SIGQUIT,SIG_IGN);
977
978         if (c<0) b=(MAXMSGS-1);
979         else b=0;
980
981         sprintf(prtfile, tmpnam(NULL));
982
983         num_msgs = 0;
984         strcpy(cmd,"MSGS ");
985         switch(c) {
986                 case 0: strcat(cmd,"ALL");
987                         break;
988                 case 1: strcat(cmd,"NEW");
989                         break;
990                 case 2: strcat(cmd,"OLD");
991                         break;
992                 case 3: sprintf(&cmd[strlen(cmd)], "LAST|%d", q);
993                         break;
994                 }
995         serv_puts(cmd);
996         serv_gets(cmd);
997         if (cmd[0]!='1') {
998                 printf("%s\n",&cmd[5]);
999                 }
1000         else {
1001                 while (serv_gets(cmd), strcmp(cmd,"000")) {
1002                         if (num_msgs == MAXMSGS) {
1003                                 memcpy(&msg_arr[0], &msg_arr[1],
1004                                         (sizeof(long) * (MAXMSGS - 1)) );
1005                                 --num_msgs;
1006                                 }
1007                         msg_arr[num_msgs++] = atol(cmd);
1008                         }
1009                 }
1010
1011         lines_printed = 0;
1012
1013         /* this loop cycles through each message... */
1014         start = ( (rdir==1) ? 0 : (num_msgs-1) );
1015         for (a=start; ((a<num_msgs)&&(a>=0)); a=a+rdir) {
1016                 while (msg_arr[a]==0L) {
1017                         a=a+rdir; if ((a==MAXMSGS)||(a==(-1))) return;
1018                         }
1019
1020 RAGAIN:         pagin=((arcflag==0)&&(quotflag==0)&&
1021                         (userflags & US_PAGINATOR)) ? 1 : 0;
1022
1023         /* If we're doing a quote, set the screenwidth to 72 temporarily */
1024                 if (quotflag) {
1025                         hold_sw = screenwidth;
1026                         screenwidth = 72;
1027                         }
1028
1029         /* If printing or archiving, set the screenwidth to 80 temporarily */
1030                 if (arcflag) {
1031                         hold_sw = screenwidth;
1032                         screenwidth = 80;
1033                         }
1034
1035         /* now read the message... */
1036                 e=read_message(msg_arr[a],pagin);
1037
1038         /* ...and set the screenwidth back if we have to */
1039                 if ((quotflag)||(arcflag)) {
1040                         screenwidth = hold_sw;
1041                         }
1042 RMSGREAD:       fflush(stdout);
1043                 highest_msg_read = msg_arr[a];
1044                 if (quotflag) {
1045                         freopen("/dev/tty","r+",stdout);
1046                         quotflag=0;
1047                         enable_color = hold_color;
1048                         process_quote();
1049                         }
1050                 if (arcflag) {
1051                         freopen("/dev/tty","r+",stdout);
1052                         arcflag=0;
1053                         enable_color = hold_color;
1054                         f=fork();
1055                         if (f==0) {
1056                                 freopen(prtfile, "r", stdin);
1057                                 sttybbs(SB_RESTORE);
1058                                 ka_system(printcmd);
1059                                 sttybbs(SB_NO_INTR);
1060                                 unlink(prtfile);
1061                                 exit(0);
1062                                 }
1063                         if (f>0) do {
1064                                 g=wait(NULL);
1065                                 } while((g!=f)&&(g>=0));
1066                         printf("Message printed.\n");
1067                         }
1068                 if (e==3) return;
1069                 if ( ((userflags&US_NOPROMPT)||(e==2)) 
1070                    && (((room_flags&QR_MAILBOX)==0)
1071                      ||(rc_force_mail_prompts==0))  ) {
1072                         e='n';
1073                         }
1074                 else {
1075                         color(DIM_WHITE);
1076                         printf("(");
1077                         color(BRIGHT_WHITE);
1078                         printf("%d",num_msgs-a-1);
1079                         color(DIM_WHITE);
1080                         printf(") ");
1081
1082                         if (is_mail==1) keyopt("<R>eply ");
1083                         keyopt("<B>ack <A>gain <Q>uote <N>ext <S>top ");
1084                         if (rc_url_cmd[0] && num_urls) keyopt("<U>RL View ");
1085                         keyopt("<?>Help/others -> ");
1086                         
1087                         do {
1088                                 lines_printed = 2;
1089                                 e=(inkey()&127); e=tolower(e);
1090 /* return key same as <N> */    if (e==13) e='n';
1091 /* space key same as <N> */     if (e==32) e='n';
1092 /* del/move for aides only */   if ((!is_room_aide)
1093                                     &&((room_flags&QR_MAILBOX)==0)) {
1094                                         if ((e=='d')||(e=='m')||(e=='c')) e=0;
1095                                         }
1096 /* print only if available */   if ((e=='p')&&(strlen(printcmd)==0)) e=0;
1097 /* can't reply in public rms */ if ((e=='r')&&(is_mail!=1)) e=0;
1098 /* can't file if not allowed */ if ((e=='f')&&(rc_allow_attachments==0)) e=0;
1099 /* link only if browser avail*/ if ((e=='u')&&(strlen(rc_url_cmd)==0)) e=0;
1100                                 } while((e!='a')&&(e!='n')&&(e!='s')
1101                                         &&(e!='d')&&(e!='m')&&(e!='p')
1102                                         &&(e!='q')&&(e!='b')&&(e!='h')
1103                                         &&(e!='r')&&(e!='f')&&(e!='?')
1104                                         &&(e!='u')&&(e!='c'));
1105                         switch(e) {
1106                                 case 's':       printf("Stop\r");       break;
1107                                 case 'a':       printf("Again\r");      break;
1108                                 case 'd':       printf("Delete\r");     break;
1109                                 case 'm':       printf("Move\r");       break;
1110                                 case 'c':       printf("Copy\r");       break;
1111                                 case 'n':       printf("Next\r");       break;
1112                                 case 'p':       printf("Print\r");      break;
1113                                 case 'q':       printf("Quote\r");      break;
1114                                 case 'b':       printf("Back\r");       break;
1115                                 case 'h':       printf("Header\r");     break;
1116                                 case 'r':       printf("Reply\r");      break;
1117                                 case 'f':       printf("File\r");       break;
1118                                 case 'u':       printf("URL's\r");      break;
1119                                 case '?':       printf("? <help>\r");   break;
1120                                 }
1121                         if (userflags & US_DISAPPEAR)
1122                                 printf("\r%79s\r","");
1123                         else
1124                                 printf("\n");
1125                         fflush(stdout);
1126                         }
1127                 switch(e) {
1128                    case '?':    printf("Options available here:\n");
1129                                 printf(" ?  Help (prints this message)\n");
1130                                 printf(" S  Stop reading immediately\n");
1131                                 printf(" A  Again (repeats last message)\n");
1132                                 printf(" N  Next (continue with next message)\n");
1133                                 printf(" B  Back (go back to previous message)\n");
1134                                 if ((is_room_aide)
1135                                     ||(room_flags&QR_MAILBOX)) {
1136                                         printf(" D  Delete this message\n");
1137                                         printf(" M  Move message to another room\n");
1138                                         printf(" C  Copy message to another room\n");
1139                                         }
1140                                 if (strlen(printcmd)>0)
1141                                         printf(" P  Print this message\n");
1142                                 printf(" Q  Quote portions of this message for your next post\n");
1143                                 printf(" H  Headers (display message headers only)\n");
1144                                 if (is_mail)
1145                                         printf(" R  Reply to this message\n");
1146                                 if (rc_allow_attachments)
1147                                         printf(" F  (save attachments to a file)\n");
1148                                 if (strlen(rc_url_cmd)>0)
1149                                         printf(" U  (list URL's for display)\n");
1150                                 printf("\n");
1151                                 goto RMSGREAD;
1152                    case 'p':    fflush(stdout);
1153                                 freopen(prtfile,"w",stdout);
1154                                 arcflag = 1;
1155                                 hold_color = enable_color;
1156                                 enable_color = 0;
1157                                 goto RAGAIN;
1158                    case 'q':    fflush(stdout);
1159                                 freopen(temp2,"w",stdout);
1160                                 quotflag = 1;
1161                                 hold_color = enable_color;
1162                                 enable_color = 0;
1163                                 goto RAGAIN;
1164                    case 's':    return;
1165                    case 'a':    goto RAGAIN;
1166                    case 'b':    a=a-(rdir*2);
1167                                 break;
1168                    case 'm':
1169                    case 'c':
1170                                 newprompt("Enter target room: ",
1171                                         targ,ROOMNAMELEN-1);
1172                                 if (strlen(targ)>0) {
1173                                         sprintf(cmd,"MOVE %ld|%s|%d",
1174                                                 msg_arr[a],targ,
1175                                                 (e=='c' ? 1 : 0) );
1176                                         serv_puts(cmd);
1177                                         serv_gets(cmd);
1178                                         printf("%s\n",&cmd[4]);
1179                                         if (cmd[0]=='2') msg_arr[a]=0L;
1180                                         }
1181                                 else {
1182                                         goto RMSGREAD;
1183                                         }
1184                                 if (cmd[0]!='2') goto RMSGREAD;
1185                                 break;
1186                    case 'f':    newprompt("Which section? ", filename,
1187                                         ((sizeof filename) -1));
1188                                 snprintf(cmd, sizeof cmd,
1189                                         "OPNA %ld|%s", msg_arr[a], filename);
1190                                 serv_puts(cmd);
1191                                 serv_gets(cmd);
1192                                 if (cmd[0]=='2') {
1193                                         extract(filename, &cmd[4], 2);
1194                                         download_to_local_disk(filename,
1195                                                 extract_int(&cmd[4], 0));
1196                                         }
1197                                 else {
1198                                         printf("%s\n",&cmd[4]);
1199                                         }
1200                                 goto RMSGREAD;
1201                    case 'd':    printf("*** Delete this message? ");
1202                                 if (yesno()==1) {
1203                                         sprintf(cmd,"DELE %ld",msg_arr[a]);
1204                                         serv_puts(cmd);
1205                                         serv_gets(cmd);
1206                                         printf("%s\n",&cmd[4]);
1207                                         if (cmd[0]=='2') msg_arr[a]=0L;
1208                                         }
1209                                 else {
1210                                         goto RMSGREAD;
1211                                         }
1212                                 break;
1213                    case 'h':    read_message(msg_arr[a],READ_HEADER);
1214                                 goto RMSGREAD;
1215                    case 'r':    entmsg(1,(DEFAULT_ENTRY==46 ? 2 : 0));
1216                                 goto RMSGREAD;
1217                    case 'u':    list_urls();
1218                                 goto RMSGREAD;
1219                         }
1220                 } /* end for loop */
1221         } /* end read routine */
1222
1223
1224
1225
1226 /*
1227  * View and edit a system message
1228  */
1229 void edit_system_message(char *which_message)
1230 {
1231         char desc[64];
1232         char read_cmd[64];
1233         char write_cmd[64];
1234
1235         sprintf(desc, "system message '%s'", which_message);
1236         sprintf(read_cmd, "MESG %s", which_message);
1237         sprintf(write_cmd, "EMSG %s", which_message);
1238         do_edit(desc, read_cmd, "NOOP", write_cmd);
1239         }
1240
1241
1242
1243
1244 /*
1245  * Verify the message base
1246  */
1247 void check_message_base(void) {
1248         char buf[256];
1249
1250         printf("Please read the documentation before running this command.\n");
1251         printf("Having done so, do you still want to check the message base? ");
1252         if (yesno()==0) return;
1253
1254         serv_puts("FSCK");
1255         serv_gets(buf);
1256         if (buf[0] != '1') {
1257                 printf("%s\n", &buf[4]);
1258                 return;
1259         }
1260
1261         while (serv_gets(buf), strcmp(buf, "000")) {
1262                 printf("%s\n", buf);
1263         }
1264 }