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