2 * Citadel/UX message support routines
3 * see copyright.txt for copyright information
21 #define MAXWORDBUF 256
26 char text[MAXWORDBUF];
30 struct AttachedFile *next;
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);
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);
49 long msg_arr[MAXMSGS];
51 extern char room_name[];
52 extern unsigned room_flags;
53 extern long highest_msg_read;
54 extern struct CtdlServInfo serv_info;
57 extern int screenwidth;
58 extern int screenheight;
59 extern long maxmsgnum;
62 extern char is_room_aide;
63 extern char fullname[];
65 extern unsigned userflags;
66 extern char sigcaught;
67 extern char editor_path[];
68 extern char printcmd[];
69 extern int rc_allow_attachments;
71 extern int editor_pid;
75 void ka_sigcatch(void) {
78 signal(SIGALRM, (void *)ka_sigcatch);
85 * server keep-alive version of wait() (needed for external editor)
87 pid_t ka_wait(pid_t *kstatus)
92 signal(SIGALRM, (void *)ka_sigcatch);
96 } while (errno==EINTR);
97 signal(SIGALRM,SIG_IGN);
104 * version of system() that uses ka_wait()
106 int ka_system(char *shc)
115 perror("Cannot fork");
117 return((pid_t)childpid);
121 execlp("/bin/sh","sh","-c",shc,NULL);
127 waitpid = ka_wait(&retcode);
128 } while (waitpid != childpid);
138 * add a newline to the buffer...
140 void add_newline(struct cittext *textlist)
145 while (ptr->next != NULL) ptr = ptr->next;
147 while (ptr->text[strlen(ptr->text)-1]==32)
148 ptr->text[strlen(ptr->text)-1] = 0;
149 /* strcat(ptr->text,"\n"); */
151 ptr->next = (struct cittext *)
152 malloc(sizeof(struct cittext));
155 strcpy(ptr->text,"");
160 * add a word to the buffer...
162 void add_word(struct cittext *textlist, char *wordbuf)
168 while (ptr->next != NULL) ptr = ptr->next;
170 if (3+strlen(ptr->text)+strlen(wordbuf) > screenwidth) {
171 ptr->next = (struct cittext *)
172 malloc(sizeof(struct cittext));
175 strcpy(ptr->text,"");
178 strcat(ptr->text,wordbuf);
179 strcat(ptr->text," ");
184 * begin editing of an opened file pointed to by fp, beginning at position pos.
186 void citedit(FILE *fp, long int base_pos)
188 int a,prev,finished,b,last_space;
190 struct cittext *textlist = NULL;
192 char wordbuf[MAXWORDBUF];
194 time_t last_server_msg,now;
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.
201 time(&last_server_msg);
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,"");
211 while (a=getc(fp), a>=0) {
213 if ((a==32)||(a==9)||(a==13)||(a==10)) {
214 add_word(textlist,wordbuf);
216 if ((prev==13)||(prev==10)) {
217 add_word(textlist,"\n");
218 add_newline(textlist);
219 add_word(textlist,"");
223 wordbuf[strlen(wordbuf)+1] = 0;
224 wordbuf[strlen(wordbuf)] = a;
226 if (strlen(wordbuf)+3 > screenwidth) {
227 add_word(textlist,wordbuf);
235 prev = (appending ? 13 : (-1));
242 if ((a==32)&&(prev==13)) {
243 add_word(textlist,"\n");
244 add_newline(textlist);
247 if (strlen(wordbuf)>0) {
248 wordbuf[strlen(wordbuf)-1] = 0;
256 if (strlen(wordbuf)==0) finished = 1;
258 for (b=0; b<strlen(wordbuf); ++b)
259 if (wordbuf[b]==32) {
261 add_word(textlist,wordbuf);
262 strcpy(wordbuf,&wordbuf[b+1]);
265 add_word(textlist,wordbuf);
271 wordbuf[strlen(wordbuf)+1] = 0;
272 wordbuf[strlen(wordbuf)] = a;
274 if ((strlen(wordbuf)+3) > screenwidth) {
276 for (b=0; b<strlen(wordbuf); ++b)
277 if (wordbuf[b]==32) last_space = b;
279 for (b=0; b<strlen(wordbuf); ++b)
280 if (wordbuf[b]==32) {
282 add_word(textlist,wordbuf);
283 strcpy(wordbuf,&wordbuf[b+1]);
286 for (b=0; b<strlen(wordbuf); ++b) {
291 printf("\n%s",wordbuf);
294 add_word(textlist,wordbuf);
301 /* this check implements the server keep-alive */
303 if ( (now - last_server_msg) > S_KEEPALIVE ) {
306 last_server_msg = now;
309 } while (finished==0);
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);
319 /* and deallocate the memory we used */
320 while (textlist!=NULL) {
328 int read_message(long int num, char pagin) /* Read a message from the server */
330 /* 0 = normal read, 1 = read with pagination, 2 = header */
344 sprintf(buf,"MSG0 %ld|%d",num,(pagin==READ_HEADER ? 1 : 0));
348 printf("*** msg #%ld: %s\n",num,buf);
350 lines_printed = checkpagin(lines_printed,pagin,screenheight);
355 strcpy(m_subject,"");
358 lines_printed = checkpagin(lines_printed,pagin,screenheight);
360 if (pagin == 1) color(3);
363 while(serv_gets(buf), strcmp(buf,"000")) {
368 checkpagin(lines_printed,
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]);
382 if (nhdr==1) buf[0]='_';
383 if (!struncmp(buf,"type=",5))
384 format_type=atoi(&buf[5]);
385 if (!struncmp(buf,"from=",5)) {
386 printf("from %s ",&buf[5]);
388 if (!struncmp(buf,"subj=",5))
389 strcpy(m_subject,&buf[5]);
390 if ((!struncmp(buf,"hnod=",5))
391 && (strucmp(&buf[5],serv_info.serv_humannode)))
392 printf("(%s) ",&buf[5]);
393 if ((!struncmp(buf,"room=",5))
394 && (strucmp(&buf[5],room_name)))
395 printf("in %s> ",&buf[5]);
397 if (!struncmp(buf,"node=",5)) {
398 if ( (room_flags&QR_NETWORK)
399 || ((strucmp(&buf[5],serv_info.serv_nodename)
400 &&(strucmp(&buf[5],serv_info.serv_fqdn)))))
402 printf("@%s ",&buf[5]);
404 if ((!strucmp(&buf[5],serv_info.serv_nodename))
405 ||(!strucmp(&buf[5],serv_info.serv_fqdn)))
407 strcpy(reply_to,from);
410 sprintf(reply_to,"%s @ %s",from,&buf[5]);
414 if (!struncmp(buf,"rcpt=",5))
415 printf("to %s ",&buf[5]);
416 if (!struncmp(buf,"time=",5)) {
418 tm=(struct tm *)localtime(&now);
419 strcpy(buf,asctime(tm)); buf[strlen(buf)-1]=0;
420 strcpy(&buf[16],&buf[19]);
421 printf("%s ",&buf[4]);
434 if (pagin == 1 ) color(2);
436 lines_printed = checkpagin(lines_printed,pagin,screenheight);
438 if (strlen(m_subject)>0) {
439 printf("Subject: %s\n",m_subject);
441 lines_printed = checkpagin(lines_printed,pagin,screenheight);
444 if (format_type == 0) {
445 fr=fmout(screenwidth,NULL,
446 ((pagin==1) ? 1 : 0),
447 screenheight,(-1),1);
450 while(serv_gets(buf), strcmp(buf,"000")) {
453 lines_printed = lines_printed + 1 +
454 (strlen(buf)/screenwidth);
456 checkpagin(lines_printed,pagin,screenheight);
463 lines_printed = checkpagin(lines_printed,pagin,screenheight);
465 if (pagin == 1) color(7);
471 * replace string function for the built-in editor
473 void replace_string(char *filename, long int startpos)
482 int substitutions = 0;
485 printf("Enter text to be replaced:\n: ");
486 getline(srch_str,128);
487 if (strlen(srch_str)==0) return;
489 printf("Enter text to replace it with:\n: ");
490 getline(rplc_str,128);
492 fp=fopen(filename,"r+");
493 if (fp==NULL) return;
496 fseek(fp,startpos,0);
498 while (a=getc(fp), a>0) {
500 buf[strlen(buf)+1] = 0;
501 buf[strlen(buf)] = a;
502 if ( strlen(buf) >= strlen(srch_str) ) {
503 ptr=&buf[strlen(buf)-strlen(srch_str)];
504 if (!struncmp(ptr,srch_str,strlen(srch_str))) {
505 strcpy(ptr,rplc_str);
509 if (strlen(buf)>384) {
512 fwrite((char *)buf,128,1,fp);
513 strcpy(buf,&buf[128]);
519 if (strlen(buf)>0) fwrite((char *)buf,strlen(buf),1,fp);
522 printf("<R>eplace made %d substitution(s).\n\n",substitutions);
526 int make_message(char *filename, char *recipient, int anon_type, int format_type, int mode, char *boundary)
527 /* temporary file name */
528 /* NULL if it's not mail */
529 /* see MES_ types in header file */
539 struct AttachedFile *AttachList = NULL;
540 struct AttachedFile *Aptr;
543 if (mode==2) if (strlen(editor_path)==0) {
544 printf("*** No editor available, using built-in editor\n");
549 strcpy(datestr,asctime(localtime(&now)));
550 datestr[strlen(datestr)-1] = 0;
552 if (room_flags & QR_ANONONLY) {
556 printf(" %s from %s",datestr,fullname);
557 if (strlen(recipient)>0) printf(" to %s",recipient);
563 if (mode==1) printf("(Press ctrl-d when finished)\n");
565 fp=fopen(filename,"r");
567 fmout(screenwidth,fp,0,screenheight,0,0);
572 fp=fopen(filename,"w");
580 fp=fopen(filename,"r+");
587 fp=fopen(filename,"w");
589 a=inkey(); if (a==255) a=32;
595 if (a==10) putc(13,stdout);
601 e_ex_code = 1; /* start with a failed exit code */
603 cksum = file_checksum(filename);
605 chmod(filename,0600);
607 execlp(editor_path,editor_path,filename,NULL);
610 if (editor_pid>0) do {
612 b=ka_wait(&e_ex_code);
613 } while((b!=editor_pid)&&(b>=0));
620 if (file_checksum(filename) == cksum) {
621 printf("*** Aborted message.\n");
624 if (e_ex_code==0) goto MEFIN;
627 MECR1: printf("Entry cmd (? for options) -> ");
629 if (b==NEXT_KEY) b='n';
630 if (b==STOP_KEY) b='s';
631 b=(b&127); b=tolower(b);
637 if (b=='a') { printf("Abort\n"); goto MEABT; }
638 if (b=='c') { printf("Continue\n"); goto ME1; }
639 if (b=='s') { printf("Save message\n"); goto MEFIN; }
641 printf("Print formatted\n");
642 printf(" %s from %s",datestr,fullname);
643 if (strlen(recipient)>0) printf(" to %s",recipient);
645 fp=fopen(filename,"r");
647 fmout(screenwidth,fp,
648 ((userflags & US_PAGINATOR) ? 1 : 0),
656 printf("Replace string\n");
657 replace_string(filename,0L);
661 printf("Hold message\n");
664 /* if ((b=='f')&&(rc_allow_attachments==1)) { */
666 printf("attach File\n");
667 if (strlen(boundary)==0) {
668 sprintf(boundary, "Citadel-Attachment-%ld.%d",
669 (long)time(NULL), getpid() );
671 newprompt("Filename: ", buf, 68);
672 if (access(buf, R_OK)==0) {
673 Aptr = (struct AttachedFile *)
674 malloc(sizeof(struct AttachedFile));
675 strcpy(Aptr->filename, buf);
676 Aptr->next = AttachList;
680 printf("*** Cannot open %s: %s\n",
681 buf, strerror(errno));
687 MEFIN: /* Now we're done typing the message. Before returning, append any
688 * attachments the user has selected
690 if (strlen(boundary)==0) goto SKIPAT;
691 fp = fopen(filename, "a");
692 while (AttachList != NULL) {
693 sprintf(buf, "uuencode %s <%s",
694 AttachList->filename, AttachList->filename);
695 atpipe = popen(buf, "r");
696 if (atpipe != NULL) {
697 fprintf(fp,"--%s\n", boundary);
698 fprintf(fp,"Content-type: application/octet-stream;\n");
699 fprintf(fp,"%cname=%c%s%c\n",
700 9, 34, AttachList->filename, 34);
701 fprintf(fp,"Content-Transfer-Encoding: x-uudecode\n");
702 fprintf(fp,"Content-Disposition: attachment;\n");
703 fprintf(fp,"%cfilename=%c%s%c\n\n",
704 9, 34, AttachList->filename, 34);
705 while (fgets(buf, 256, atpipe)!=NULL) {
706 buf[strlen(buf)-1]=0;
707 fprintf(fp, "%s\n", buf);
712 printf("*** Cannot open %s: %s\n",
713 AttachList->filename, strerror(errno));
715 Aptr = AttachList->next;
719 fprintf(fp, "--%s--\n", boundary); /* end of attachments */
724 MEABT: printf("Are you sure? ");
725 if (yesno()==0) goto ME1;
726 MEABT2: unlink(filename);
731 * transmit message text to the server
733 void transmit_message(FILE *fp)
739 while (ch=getc(fp), (ch>=0)) {
741 if (!strcmp(buf,"000")) strcpy(buf,">000");
749 if ((ch==32)&&(strlen(buf)>200)) {
751 if (!strcmp(buf,"000")) strcpy(buf,">000");
755 if (strlen(buf)>250) {
756 if (!strcmp(buf,"000")) strcpy(buf,">000");
768 * entmsg() - edit and create a message
769 * returns 0 if message was saved
771 int entmsg(int is_reply, int c)
772 /* nonzero if this was a <R>eply command */
786 sprintf(cmd,"ENT0 0||0|%d",mode);
790 if ((strncmp(cmd,"570",3)) && (strncmp(cmd,"200",3))) {
791 printf("%s\n",&cmd[4]);
795 if (!strncmp(cmd,"570",3)) need_recp = 1;
797 if ((userflags & US_EXPERT) == 0) formout("entermsg");
803 strcpy(buf,reply_to);
806 printf("Enter recipient: ");
808 if (strlen(buf)==0) return(1);
811 else strcpy(buf,"sysop");
815 if (room_flags&QR_ANON2) {
816 printf("Anonymous (Y/N)? ");
820 /* if it's mail, we've got to check the validity of the recipient... */
822 sprintf(cmd,"ENT0 0|%s|%d|%d",buf,b,mode);
826 printf("%s\n",&cmd[4]);
831 /* learn the number of the newest message in in the room, so we can tell
832 * upon saving whether someone else has posted too
835 serv_puts("MSGS LAST|1");
838 printf("%s\n",&cmd[5]);
841 while (serv_gets(cmd), strcmp(cmd,"000")) {
842 msg_arr[num_msgs++] = atol(cmd);
846 /* now put together the message */
847 strcpy(boundary, "");
848 if ( make_message(temp,buf,b,0,c,boundary) != 0 ) return(2);
850 /* and send it to the server */
851 sprintf(cmd,"ENT0 1|%s|%d|%d||%s|",buf,b,mode,boundary);
855 printf("%s\n",&cmd[4]);
860 transmit_message(fp);
866 highmsg = msg_arr[num_msgs - 1];
868 serv_puts("MSGS NEW");
871 printf("%s\n",&cmd[5]);
874 while (serv_gets(cmd), strcmp(cmd,"000")) {
875 msg_arr[num_msgs++] = atol(cmd);
879 /* get new highest message number in room to set lrp for goto... */
880 maxmsgnum = msg_arr[num_msgs - 1];
882 /* now see if anyone else has posted in here */
884 for (a=0; a<num_msgs; ++a) if (msg_arr[a]>highmsg) ++b;
886 /* in the Mail> room, this algorithm always counts one message
887 * higher than in public rooms, so we decrement it by one */
891 "*** 1 additional message has been entered in this room by another user.\n");
893 "*** %d additional messages have been entered in this room by other users.\n",b);
898 void process_quote(void) { /* do editing on quoted file */
901 int line,qstart,qend;
903 /* Unlink the second temp file as soon as it's opened, so it'll get
904 * deleted even if the program dies
906 qfile = fopen(temp2,"r");
909 /* Display the quotable text with line numbers added */
911 fgets(buf,128,qfile);
912 while (fgets(buf,128,qfile)!=NULL) {
913 printf("%2d %s",++line,buf);
915 printf("Begin quoting at [ 1] : ");
917 qstart = (buf[0]==0) ? (1) : atoi(buf);
918 printf(" End quoting at [%d] : ",line);
920 qend = (buf[0]==0) ? (line) : atoi(buf);
923 fgets(buf,128,qfile);
924 tfile=fopen(temp,"w");
925 while(fgets(buf,128,qfile)!=NULL) {
926 if ((++line>=qstart)&&(line<=qend)) fprintf(tfile," >%s",buf);
928 fprintf(tfile," \n");
935 void readmsgs(int c, int rdir, int q) /* read contents of a room */
936 /* 0=Read all 1=Read new 2=Read old 3=Read last q */
937 /* 1=Forward (-1)=Reverse */
938 /* Number of msgs to read (if c==3) */
949 signal(SIGINT,SIG_IGN);
950 signal(SIGQUIT,SIG_IGN);
952 if (c<0) b=(MAXMSGS-1);
955 sprintf(prtfile,"/tmp/CPrt%d",getpid());
960 case 0: strcat(cmd,"ALL");
962 case 1: strcat(cmd,"NEW");
964 case 2: strcat(cmd,"OLD");
966 case 3: sprintf(&cmd[strlen(cmd)], "LAST|%d", q);
972 printf("%s\n",&cmd[5]);
975 while (serv_gets(cmd), strcmp(cmd,"000")) {
976 msg_arr[num_msgs++] = atol(cmd);
982 /* this loop cycles through each message... */
983 start = ( (rdir==1) ? 0 : (num_msgs-1) );
984 for (a=start; ((a<num_msgs)&&(a>=0)); a=a+rdir) {
985 while (msg_arr[a]==0L) {
986 a=a+rdir; if ((a==MAXMSGS)||(a==(-1))) return;
989 RAGAIN: pagin=((arcflag==0)&&(quotflag==0)&&
990 (userflags & US_PAGINATOR)) ? 1 : 0;
992 /* if we're doing a quote, set the screenwidth to 72 temporarily */
994 hold_sw = screenwidth;
998 /* now read the message... */
999 e=read_message(msg_arr[a],pagin);
1001 /* ...and set the screenwidth back if we have to */
1003 screenwidth = hold_sw;
1005 RMSGREAD: fflush(stdout);
1006 highest_msg_read = msg_arr[a];
1008 freopen("/dev/tty","r+",stdout);
1013 freopen("/dev/tty","r+",stdout);
1017 freopen(prtfile,"r",stdin);
1018 sttybbs(SB_RESTORE);
1019 ka_system(printcmd);
1020 sttybbs(SB_NO_INTR);
1026 } while((g!=f)&&(g>=0));
1027 printf("Message printed.\n");
1030 if ((userflags&US_NOPROMPT)||(e==2)) e='n';
1032 printf("(%d) ",num_msgs-a-1);
1033 if (is_mail==1) printf("<R>eply ");
1034 if (strlen(printcmd)>0) printf("<P>rint ");
1035 printf("<B>ack <A>gain <Q>uote <H>eader <N>ext <S>top -> ");
1038 e=(inkey()&127); e=tolower(e);
1039 /* return key same as <N> */ if (e==13) e='n';
1040 /* del/move for aides only */ if (!is_room_aide) if ((e=='d')||(e=='m')) e=0;
1041 /* print only if available */ if ((e=='p')&&(strlen(printcmd)==0)) e=0;
1042 /* can't move from Mail> */ if ((e=='m')&&(is_mail==1)) e=0;
1043 /* can't reply in public rms */ if ((e=='r')&&(is_mail!=1)) e=0;
1044 } while((e!='a')&&(e!='n')&&(e!='s')
1045 &&(e!='d')&&(e!='m')&&(e!='p')
1046 &&(e!='q')&&(e!='b')&&(e!='h')
1049 case 's': printf("Stop\r"); break;
1050 case 'a': printf("Again\r"); break;
1051 case 'd': printf("Delete\r"); break;
1052 case 'm': printf("Move\r"); break;
1053 case 'n': printf("Next\r"); break;
1054 case 'p': printf("Print\r"); break;
1055 case 'q': printf("Quote\r"); break;
1056 case 'b': printf("Back\r"); break;
1057 case 'h': printf("Header\r"); break;
1058 case 'r': printf("Reply\r"); break;
1060 if (userflags & US_DISAPPEAR)
1061 printf("%75s\r","");
1067 case 'p': fflush(stdout);
1068 freopen(prtfile,"w",stdout);
1071 case 'q': fflush(stdout);
1072 freopen(temp2,"w",stdout);
1076 case 'a': goto RAGAIN;
1077 case 'b': a=a-(rdir*2);
1079 case 'm': newprompt("Enter target room: ",targ,19);
1080 if (strlen(targ)>0) {
1081 sprintf(cmd,"MOVE %ld|%s",
1085 printf("%s\n",&cmd[4]);
1086 if (cmd[0]=='2') msg_arr[a]=0L;
1091 if (cmd[0]!='2') goto RMSGREAD;
1093 case 'd': printf("*** Delete this message? ");
1095 sprintf(cmd,"DELE %ld",msg_arr[a]);
1098 printf("%s\n",&cmd[4]);
1099 if (cmd[0]=='2') msg_arr[a]=0L;
1105 case 'h': read_message(msg_arr[a],READ_HEADER);
1107 case 'r': entmsg(1,(DEFAULT_ENTRY==46 ? 2 : 0));
1110 } /* end for loop */
1111 } /* end read routine */
1117 * View and edit a system message
1119 void edit_system_message(char *which_message)
1125 sprintf(desc, "system message '%s'", which_message);
1126 sprintf(read_cmd, "MESG %s", which_message);
1127 sprintf(write_cmd, "EMSG %s", which_message);
1128 do_edit(desc, read_cmd, "NOOP", write_cmd);