2 * Citadel/UX message support routines
3 * see copyright.txt for copyright information
23 #define MAXWORDBUF 256
28 char text[MAXWORDBUF];
32 struct AttachedFile *next;
36 void sttybbs(int cmd);
37 int struncmp(char *lstr, char *rstr, int len);
38 int fmout(int width, FILE *fp, char pagin, int height, int starting_lp, char subst);
39 int haschar(char *st, int ch);
40 int checkpagin(int lp, int pagin, int height);
41 void getline(char *string, int lim);
42 void formout(char *name);
44 void newprompt(char *prompt, char *str, int len);
45 int file_checksum(char *filename);
46 void color(int colornum);
47 void do_edit(char *desc, char *read_cmd, char *check_cmd, char *write_cmd);
50 long msg_arr[MAXMSGS];
52 extern char room_name[];
53 extern unsigned room_flags;
54 extern long highest_msg_read;
55 extern struct CtdlServInfo serv_info;
58 extern int screenwidth;
59 extern int screenheight;
60 extern long maxmsgnum;
63 extern char is_room_aide;
64 extern char fullname[];
66 extern unsigned userflags;
67 extern char sigcaught;
68 extern char editor_path[];
69 extern char printcmd[];
70 extern int rc_allow_attachments;
71 extern int rc_display_message_numbers;
72 extern int rc_force_mail_prompts;
74 extern int editor_pid;
78 void ka_sigcatch(int signum) {
81 signal(SIGALRM, ka_sigcatch);
88 * server keep-alive version of wait() (needed for external editor)
90 pid_t ka_wait(pid_t *kstatus)
95 signal(SIGALRM, ka_sigcatch);
99 } while (errno==EINTR);
100 signal(SIGALRM,SIG_IGN);
107 * version of system() that uses ka_wait()
109 int ka_system(char *shc)
118 perror("Cannot fork");
120 return((pid_t)childpid);
124 execlp("/bin/sh","sh","-c",shc,NULL);
130 waitpid = ka_wait(&retcode);
131 } while (waitpid != childpid);
141 * add a newline to the buffer...
143 void add_newline(struct cittext *textlist)
148 while (ptr->next != NULL) ptr = ptr->next;
150 while (ptr->text[strlen(ptr->text)-1]==32)
151 ptr->text[strlen(ptr->text)-1] = 0;
152 /* strcat(ptr->text,"\n"); */
154 ptr->next = (struct cittext *)
155 malloc(sizeof(struct cittext));
158 strcpy(ptr->text,"");
163 * add a word to the buffer...
165 void add_word(struct cittext *textlist, char *wordbuf)
171 while (ptr->next != NULL) ptr = ptr->next;
173 if (3+strlen(ptr->text)+strlen(wordbuf) > screenwidth) {
174 ptr->next = (struct cittext *)
175 malloc(sizeof(struct cittext));
178 strcpy(ptr->text,"");
181 strcat(ptr->text,wordbuf);
182 strcat(ptr->text," ");
187 * begin editing of an opened file pointed to by fp, beginning at position pos.
189 void citedit(FILE *fp, long int base_pos)
191 int a,prev,finished,b,last_space;
193 struct cittext *textlist = NULL;
195 char wordbuf[MAXWORDBUF];
197 time_t last_server_msg,now;
200 * we're going to keep track of the last time we talked to
201 * the server, so we can periodically send keep-alive messages
202 * out so it doesn't time out.
204 time(&last_server_msg);
206 /* first, load the text into the buffer */
207 fseek(fp,base_pos,0);
208 textlist = (struct cittext *)malloc(sizeof(struct cittext));
209 textlist->next = NULL;
210 strcpy(textlist->text,"");
214 while (a=getc(fp), a>=0) {
216 if ((a==32)||(a==9)||(a==13)||(a==10)) {
217 add_word(textlist,wordbuf);
219 if ((prev==13)||(prev==10)) {
220 add_word(textlist,"\n");
221 add_newline(textlist);
222 add_word(textlist,"");
226 wordbuf[strlen(wordbuf)+1] = 0;
227 wordbuf[strlen(wordbuf)] = a;
229 if (strlen(wordbuf)+3 > screenwidth) {
230 add_word(textlist,wordbuf);
238 prev = (appending ? 13 : (-1));
245 if ((a==32)&&(prev==13)) {
246 add_word(textlist,"\n");
247 add_newline(textlist);
250 if (strlen(wordbuf)>0) {
251 wordbuf[strlen(wordbuf)-1] = 0;
259 if (strlen(wordbuf)==0) finished = 1;
261 for (b=0; b<strlen(wordbuf); ++b)
262 if (wordbuf[b]==32) {
264 add_word(textlist,wordbuf);
265 strcpy(wordbuf,&wordbuf[b+1]);
268 add_word(textlist,wordbuf);
274 wordbuf[strlen(wordbuf)+1] = 0;
275 wordbuf[strlen(wordbuf)] = a;
277 if ((strlen(wordbuf)+3) > screenwidth) {
279 for (b=0; b<strlen(wordbuf); ++b)
280 if (wordbuf[b]==32) last_space = b;
282 for (b=0; b<strlen(wordbuf); ++b)
283 if (wordbuf[b]==32) {
285 add_word(textlist,wordbuf);
286 strcpy(wordbuf,&wordbuf[b+1]);
289 for (b=0; b<strlen(wordbuf); ++b) {
294 printf("\n%s",wordbuf);
297 add_word(textlist,wordbuf);
304 /* this check implements the server keep-alive */
306 if ( (now - last_server_msg) > S_KEEPALIVE ) {
309 last_server_msg = now;
312 } while (finished==0);
314 /* write the buffer back to disk */
315 fseek(fp,base_pos,0);
316 for (ptr=textlist; ptr!=NULL; ptr=ptr->next) {
317 fprintf(fp,"%s",ptr->text);
322 /* and deallocate the memory we used */
323 while (textlist!=NULL) {
331 int read_message(long int num, char pagin) /* Read a message from the server */
333 /* 0 = normal read, 1 = read with pagination, 2 = header */
347 sprintf(buf,"MSG0 %ld|%d",num,(pagin==READ_HEADER ? 1 : 0));
351 printf("*** msg #%ld: %s\n",num,buf);
353 lines_printed = checkpagin(lines_printed,pagin,screenheight);
358 strcpy(m_subject,"");
361 lines_printed = checkpagin(lines_printed,pagin,screenheight);
363 if (pagin == 1) color(3);
366 while(serv_gets(buf), strcmp(buf,"000")) {
371 checkpagin(lines_printed,
379 strcpy(reply_to,"nobody...xxxxx");
380 while(serv_gets(buf), struncmp(buf,"text",4)) {
381 if (!struncmp(buf,"nhdr=yes",8)) nhdr=1;
382 if (!struncmp(buf,"from=",5)) {
383 strcpy(from,&buf[5]);
385 if (nhdr==1) buf[0]='_';
386 if (!struncmp(buf,"type=",5))
387 format_type=atoi(&buf[5]);
388 if ((!struncmp(buf,"msgn=",5))&&(rc_display_message_numbers)) {
390 printf("[#%s] ",&buf[5]);
392 if (!struncmp(buf,"from=",5)) {
394 printf("from %s ",&buf[5]);
396 if (!struncmp(buf,"subj=",5))
397 strcpy(m_subject,&buf[5]);
398 if ((!struncmp(buf,"hnod=",5))
399 && (strucmp(&buf[5],serv_info.serv_humannode))) {
401 printf("(%s) ",&buf[5]);
403 if ((!struncmp(buf,"room=",5))
404 && (strucmp(&buf[5],room_name))) {
406 printf("in %s> ",&buf[5]);
409 if (!struncmp(buf,"node=",5)) {
410 if ( (room_flags&QR_NETWORK)
411 || ((strucmp(&buf[5],serv_info.serv_nodename)
412 &&(strucmp(&buf[5],serv_info.serv_fqdn)))))
415 printf("@%s ",&buf[5]);
417 if ((!strucmp(&buf[5],serv_info.serv_nodename))
418 ||(!strucmp(&buf[5],serv_info.serv_fqdn)))
420 strcpy(reply_to,from);
423 sprintf(reply_to,"%s @ %s",from,&buf[5]);
427 if (!struncmp(buf,"rcpt=",5)) {
429 printf("to %s ",&buf[5]);
431 if (!struncmp(buf,"time=",5)) {
433 tm=(struct tm *)localtime(&now);
434 strcpy(buf,asctime(tm)); buf[strlen(buf)-1]=0;
435 strcpy(&buf[16],&buf[19]);
437 printf("%s ",&buf[4]);
450 if (pagin == 1 ) color(2);
452 lines_printed = checkpagin(lines_printed,pagin,screenheight);
454 if (strlen(m_subject)>0) {
455 printf("Subject: %s\n",m_subject);
457 lines_printed = checkpagin(lines_printed,pagin,screenheight);
460 if (format_type == 0) {
461 fr=fmout(screenwidth,NULL,
462 ((pagin==1) ? 1 : 0),
463 screenheight,(-1),1);
466 while(serv_gets(buf), strcmp(buf,"000")) {
469 lines_printed = lines_printed + 1 +
470 (strlen(buf)/screenwidth);
472 checkpagin(lines_printed,pagin,screenheight);
479 lines_printed = checkpagin(lines_printed,pagin,screenheight);
481 if (pagin == 1) color(7);
487 * replace string function for the built-in editor
489 void replace_string(char *filename, long int startpos)
498 int substitutions = 0;
501 printf("Enter text to be replaced:\n: ");
502 getline(srch_str,128);
503 if (strlen(srch_str)==0) return;
505 printf("Enter text to replace it with:\n: ");
506 getline(rplc_str,128);
508 fp=fopen(filename,"r+");
509 if (fp==NULL) return;
512 fseek(fp,startpos,0);
514 while (a=getc(fp), a>0) {
516 buf[strlen(buf)+1] = 0;
517 buf[strlen(buf)] = a;
518 if ( strlen(buf) >= strlen(srch_str) ) {
519 ptr=&buf[strlen(buf)-strlen(srch_str)];
520 if (!struncmp(ptr,srch_str,strlen(srch_str))) {
521 strcpy(ptr,rplc_str);
525 if (strlen(buf)>384) {
528 fwrite((char *)buf,128,1,fp);
529 strcpy(buf,&buf[128]);
535 if (strlen(buf)>0) fwrite((char *)buf,strlen(buf),1,fp);
538 printf("<R>eplace made %d substitution(s).\n\n",substitutions);
542 int make_message(char *filename, char *recipient, int anon_type, int format_type, int mode, char *boundary)
543 /* temporary file name */
544 /* NULL if it's not mail */
545 /* see MES_ types in header file */
555 struct AttachedFile *AttachList = NULL;
556 struct AttachedFile *Aptr;
559 if (mode==2) if (strlen(editor_path)==0) {
560 printf("*** No editor available, using built-in editor\n");
565 strcpy(datestr,asctime(localtime(&now)));
566 datestr[strlen(datestr)-1] = 0;
568 if (room_flags & QR_ANONONLY) {
572 printf(" %s from %s",datestr,fullname);
573 if (strlen(recipient)>0) printf(" to %s",recipient);
579 if (mode==1) printf("(Press ctrl-d when finished)\n");
581 fp=fopen(filename,"r");
583 fmout(screenwidth,fp,0,screenheight,0,0);
588 fp=fopen(filename,"w");
596 fp=fopen(filename,"r+");
603 fp=fopen(filename,"w");
605 a=inkey(); if (a==255) a=32;
611 if (a==10) putc(13,stdout);
617 e_ex_code = 1; /* start with a failed exit code */
619 cksum = file_checksum(filename);
621 chmod(filename,0600);
623 execlp(editor_path,editor_path,filename,NULL);
626 if (editor_pid>0) do {
628 b=ka_wait(&e_ex_code);
629 } while((b!=editor_pid)&&(b>=0));
636 if (file_checksum(filename) == cksum) {
637 printf("*** Aborted message.\n");
640 if (e_ex_code==0) goto MEFIN;
643 MECR1: printf("Entry cmd (? for options) -> ");
645 if (b==NEXT_KEY) b='n';
646 if (b==STOP_KEY) b='s';
647 b=(b&127); b=tolower(b);
653 if (b=='a') { printf("Abort\n"); goto MEABT; }
654 if (b=='c') { printf("Continue\n"); goto ME1; }
655 if (b=='s') { printf("Save message\n"); goto MEFIN; }
657 printf("Print formatted\n");
658 printf(" %s from %s",datestr,fullname);
659 if (strlen(recipient)>0) printf(" to %s",recipient);
661 fp=fopen(filename,"r");
663 fmout(screenwidth,fp,
664 ((userflags & US_PAGINATOR) ? 1 : 0),
672 printf("Replace string\n");
673 replace_string(filename,0L);
677 printf("Hold message\n");
680 if ((b=='f')&&(rc_allow_attachments==1)) {
681 printf("attach File\n");
682 if (strlen(boundary)==0) {
683 sprintf(boundary, "Citadel-Attachment-%ld.%d",
684 (long)time(NULL), getpid() );
686 newprompt("Filename: ", buf, 68);
687 if (access(buf, R_OK)==0) {
688 Aptr = (struct AttachedFile *)
689 malloc(sizeof(struct AttachedFile));
690 strcpy(Aptr->filename, buf);
691 Aptr->next = AttachList;
695 printf("*** Cannot open %s: %s\n",
696 buf, strerror(errno));
702 MEFIN: /* Now we're done typing the message. Before returning, append any
703 * attachments the user has selected
705 if (strlen(boundary)==0) goto SKIPAT;
706 fp = fopen(filename, "a");
707 while (AttachList != NULL) {
708 sprintf(buf, "uuencode %s <%s",
709 AttachList->filename, AttachList->filename);
710 atpipe = popen(buf, "r");
711 if (atpipe != NULL) {
712 fprintf(fp,"--%s\n", boundary);
713 fprintf(fp,"Content-type: application/octet-stream;\n");
714 fprintf(fp,"%cname=%c%s%c\n",
715 9, 34, AttachList->filename, 34);
716 fprintf(fp,"Content-Transfer-Encoding: x-uudecode\n");
717 fprintf(fp,"Content-Disposition: attachment;\n");
718 fprintf(fp,"%cfilename=%c%s%c\n\n",
719 9, 34, AttachList->filename, 34);
720 while (fgets(buf, 256, atpipe)!=NULL) {
721 buf[strlen(buf)-1]=0;
722 fprintf(fp, "%s\n", buf);
727 printf("*** Cannot open %s: %s\n",
728 AttachList->filename, strerror(errno));
730 Aptr = AttachList->next;
734 fprintf(fp, "--%s--\n", boundary); /* end of attachments */
739 MEABT: printf("Are you sure? ");
740 if (yesno()==0) goto ME1;
741 MEABT2: unlink(filename);
746 * transmit message text to the server
748 void transmit_message(FILE *fp)
754 while (ch=getc(fp), (ch>=0)) {
756 if (!strcmp(buf,"000")) strcpy(buf,">000");
764 if ((ch==32)&&(strlen(buf)>200)) {
766 if (!strcmp(buf,"000")) strcpy(buf,">000");
770 if (strlen(buf)>250) {
771 if (!strcmp(buf,"000")) strcpy(buf,">000");
783 * entmsg() - edit and create a message
784 * returns 0 if message was saved
786 int entmsg(int is_reply, int c)
787 /* nonzero if this was a <R>eply command */
801 sprintf(cmd,"ENT0 0||0|%d",mode);
805 if ((strncmp(cmd,"570",3)) && (strncmp(cmd,"200",3))) {
806 printf("%s\n",&cmd[4]);
810 if (!strncmp(cmd,"570",3)) need_recp = 1;
812 if ((userflags & US_EXPERT) == 0) formout("entermsg");
818 strcpy(buf,reply_to);
821 printf("Enter recipient: ");
823 if (strlen(buf)==0) return(1);
826 else strcpy(buf,"sysop");
830 if (room_flags&QR_ANONOPT) {
831 printf("Anonymous (Y/N)? ");
835 /* if it's mail, we've got to check the validity of the recipient... */
837 sprintf(cmd,"ENT0 0|%s|%d|%d",buf,b,mode);
841 printf("%s\n",&cmd[4]);
846 /* learn the number of the newest message in in the room, so we can tell
847 * upon saving whether someone else has posted too
850 serv_puts("MSGS LAST|1");
853 printf("%s\n",&cmd[5]);
856 while (serv_gets(cmd), strcmp(cmd,"000")) {
857 msg_arr[num_msgs++] = atol(cmd);
861 /* now put together the message */
862 strcpy(boundary, "");
863 if ( make_message(temp,buf,b,0,c,boundary) != 0 ) return(2);
865 /* and send it to the server */
866 sprintf(cmd,"ENT0 1|%s|%d|%d||%s|",buf,b,mode,boundary);
870 printf("%s\n",&cmd[4]);
875 transmit_message(fp);
881 highmsg = msg_arr[num_msgs - 1];
883 serv_puts("MSGS NEW");
886 printf("%s\n",&cmd[5]);
889 while (serv_gets(cmd), strcmp(cmd,"000")) {
890 msg_arr[num_msgs++] = atol(cmd);
894 /* get new highest message number in room to set lrp for goto... */
895 maxmsgnum = msg_arr[num_msgs - 1];
897 /* now see if anyone else has posted in here */
899 for (a=0; a<num_msgs; ++a) if (msg_arr[a]>highmsg) ++b;
901 /* in the Mail> room, this algorithm always counts one message
902 * higher than in public rooms, so we decrement it by one */
906 "*** 1 additional message has been entered in this room by another user.\n");
908 "*** %d additional messages have been entered in this room by other users.\n",b);
913 void process_quote(void) { /* do editing on quoted file */
916 int line,qstart,qend;
918 /* Unlink the second temp file as soon as it's opened, so it'll get
919 * deleted even if the program dies
921 qfile = fopen(temp2,"r");
924 /* Display the quotable text with line numbers added */
926 fgets(buf,128,qfile);
927 while (fgets(buf,128,qfile)!=NULL) {
928 printf("%2d %s",++line,buf);
930 printf("Begin quoting at [ 1] : ");
932 qstart = (buf[0]==0) ? (1) : atoi(buf);
933 printf(" End quoting at [%d] : ",line);
935 qend = (buf[0]==0) ? (line) : atoi(buf);
938 fgets(buf,128,qfile);
939 tfile=fopen(temp,"w");
940 while(fgets(buf,128,qfile)!=NULL) {
941 if ((++line>=qstart)&&(line<=qend)) fprintf(tfile," >%s",buf);
943 fprintf(tfile," \n");
950 void readmsgs(int c, int rdir, int q) /* read contents of a room */
951 /* 0=Read all 1=Read new 2=Read old 3=Read last q */
952 /* 1=Forward (-1)=Reverse */
953 /* Number of msgs to read (if c==3) */
963 char targ[ROOMNAMELEN];
965 signal(SIGINT,SIG_IGN);
966 signal(SIGQUIT,SIG_IGN);
968 if (c<0) b=(MAXMSGS-1);
971 sprintf(prtfile,"/tmp/CPrt%d",getpid());
976 case 0: strcat(cmd,"ALL");
978 case 1: strcat(cmd,"NEW");
980 case 2: strcat(cmd,"OLD");
982 case 3: sprintf(&cmd[strlen(cmd)], "LAST|%d", q);
988 printf("%s\n",&cmd[5]);
991 while (serv_gets(cmd), strcmp(cmd,"000")) {
992 msg_arr[num_msgs++] = atol(cmd);
998 /* this loop cycles through each message... */
999 start = ( (rdir==1) ? 0 : (num_msgs-1) );
1000 for (a=start; ((a<num_msgs)&&(a>=0)); a=a+rdir) {
1001 while (msg_arr[a]==0L) {
1002 a=a+rdir; if ((a==MAXMSGS)||(a==(-1))) return;
1005 RAGAIN: pagin=((arcflag==0)&&(quotflag==0)&&
1006 (userflags & US_PAGINATOR)) ? 1 : 0;
1008 /* If we're doing a quote, set the screenwidth to 72 temporarily */
1010 hold_sw = screenwidth;
1014 /* If printing or archiving, set the screenwidth to 80 temporarily */
1016 hold_sw = screenwidth;
1020 /* now read the message... */
1021 e=read_message(msg_arr[a],pagin);
1023 /* ...and set the screenwidth back if we have to */
1024 if ((quotflag)||(arcflag)) {
1025 screenwidth = hold_sw;
1027 RMSGREAD: fflush(stdout);
1028 highest_msg_read = msg_arr[a];
1030 freopen("/dev/tty","r+",stdout);
1032 enable_color = hold_color;
1036 freopen("/dev/tty","r+",stdout);
1038 enable_color = hold_color;
1041 freopen(prtfile,"r",stdin);
1042 sttybbs(SB_RESTORE);
1043 ka_system(printcmd);
1044 sttybbs(SB_NO_INTR);
1050 } while((g!=f)&&(g>=0));
1051 printf("Message printed.\n");
1054 if ( ((userflags&US_NOPROMPT)||(e==2))
1055 && (((room_flags&QR_MAILBOX)==0)
1056 ||(rc_force_mail_prompts==0)) ) {
1060 printf("(%d) ",num_msgs-a-1);
1061 if (is_mail==1) printf("<R>eply ");
1062 if (strlen(printcmd)>0) printf("<P>rint ");
1063 printf("<B>ack <A>gain <Q>uote <H>eader <N>ext <S>top -> ");
1066 e=(inkey()&127); e=tolower(e);
1067 /* return key same as <N> */ if (e==13) e='n';
1068 /* space key same as <N> */ if (e==32) e='n';
1069 /* del/move for aides only */ if ((!is_room_aide)
1070 &&((room_flags&QR_MAILBOX)==0)) {
1071 if ((e=='d')||(e=='m')) e=0;
1073 /* print only if available */ if ((e=='p')&&(strlen(printcmd)==0)) e=0;
1074 /* can't move from Mail> */ if ((e=='m')&&(is_mail==1)) e=0;
1075 /* can't reply in public rms */ if ((e=='r')&&(is_mail!=1)) e=0;
1076 } while((e!='a')&&(e!='n')&&(e!='s')
1077 &&(e!='d')&&(e!='m')&&(e!='p')
1078 &&(e!='q')&&(e!='b')&&(e!='h')
1081 case 's': printf("Stop\r"); break;
1082 case 'a': printf("Again\r"); break;
1083 case 'd': printf("Delete\r"); break;
1084 case 'm': printf("Move\r"); break;
1085 case 'n': printf("Next\r"); break;
1086 case 'p': printf("Print\r"); break;
1087 case 'q': printf("Quote\r"); break;
1088 case 'b': printf("Back\r"); break;
1089 case 'h': printf("Header\r"); break;
1090 case 'r': printf("Reply\r"); break;
1092 if (userflags & US_DISAPPEAR)
1093 printf("%75s\r","");
1099 case 'p': fflush(stdout);
1100 freopen(prtfile,"w",stdout);
1102 hold_color = enable_color;
1105 case 'q': fflush(stdout);
1106 freopen(temp2,"w",stdout);
1108 hold_color = enable_color;
1112 case 'a': goto RAGAIN;
1113 case 'b': a=a-(rdir*2);
1115 case 'm': newprompt("Enter target room: ",
1116 targ,ROOMNAMELEN-1);
1117 if (strlen(targ)>0) {
1118 sprintf(cmd,"MOVE %ld|%s",
1122 printf("%s\n",&cmd[4]);
1123 if (cmd[0]=='2') msg_arr[a]=0L;
1128 if (cmd[0]!='2') goto RMSGREAD;
1130 case 'd': printf("*** Delete this message? ");
1132 sprintf(cmd,"DELE %ld",msg_arr[a]);
1135 printf("%s\n",&cmd[4]);
1136 if (cmd[0]=='2') msg_arr[a]=0L;
1142 case 'h': read_message(msg_arr[a],READ_HEADER);
1144 case 'r': entmsg(1,(DEFAULT_ENTRY==46 ? 2 : 0));
1147 } /* end for loop */
1148 } /* end read routine */
1154 * View and edit a system message
1156 void edit_system_message(char *which_message)
1162 sprintf(desc, "system message '%s'", which_message);
1163 sprintf(read_cmd, "MESG %s", which_message);
1164 sprintf(write_cmd, "EMSG %s", which_message);
1165 do_edit(desc, read_cmd, "NOOP", write_cmd);