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