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