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