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