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