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