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