2 * Citadel/UX message support routines
3 * see copyright.txt for copyright information
21 #define MAXWORDBUF 256
26 char text[MAXWORDBUF];
30 void sttybbs(int cmd);
31 int struncmp(char *lstr, char *rstr, int len);
32 int fmout(int width, FILE *fp, char pagin, int height, int starting_lp, char subst);
33 int haschar(char *st, int ch);
34 int checkpagin(int lp, int pagin, int height);
35 void getline(char *string, int lim);
36 void formout(char *name);
38 void newprompt(char *prompt, char *str, int len);
39 int file_checksum(char *filename);
40 void color(int colornum);
41 void do_edit(char *desc, char *read_cmd, char *check_cmd, char *write_cmd);
44 long msg_arr[MAXMSGS];
46 extern char room_name[];
47 extern unsigned room_flags;
48 extern long highest_msg_read;
49 extern struct CtdlServInfo serv_info;
52 extern int screenwidth;
53 extern int screenheight;
54 extern long maxmsgnum;
57 extern char is_room_aide;
58 extern char fullname[];
60 extern unsigned userflags;
61 extern char sigcaught;
62 extern char editor_path[];
63 extern char printcmd[];
64 extern int rc_allow_attachments;
66 extern int editor_pid;
70 void ka_sigcatch(void) {
73 signal(SIGALRM, (void *)ka_sigcatch);
80 * server keep-alive version of wait() (needed for external editor)
82 pid_t ka_wait(pid_t *kstatus)
87 signal(SIGALRM, (void *)ka_sigcatch);
91 } while (errno==EINTR);
92 signal(SIGALRM,SIG_IGN);
99 * version of system() that uses ka_wait()
101 int ka_system(char *shc)
110 perror("Cannot fork");
112 return((pid_t)childpid);
116 execlp("/bin/sh","sh","-c",shc,NULL);
122 waitpid = ka_wait(&retcode);
123 } while (waitpid != childpid);
133 * add a newline to the buffer...
135 void add_newline(struct cittext *textlist)
140 while (ptr->next != NULL) ptr = ptr->next;
142 while (ptr->text[strlen(ptr->text)-1]==32)
143 ptr->text[strlen(ptr->text)-1] = 0;
144 /* strcat(ptr->text,"\n"); */
146 ptr->next = (struct cittext *)
147 malloc(sizeof(struct cittext));
150 strcpy(ptr->text,"");
155 * add a word to the buffer...
157 void add_word(struct cittext *textlist, char *wordbuf)
163 while (ptr->next != NULL) ptr = ptr->next;
165 if (3+strlen(ptr->text)+strlen(wordbuf) > screenwidth) {
166 ptr->next = (struct cittext *)
167 malloc(sizeof(struct cittext));
170 strcpy(ptr->text,"");
173 strcat(ptr->text,wordbuf);
174 strcat(ptr->text," ");
179 * begin editing of an opened file pointed to by fp, beginning at position pos.
181 void citedit(FILE *fp, long int base_pos)
183 int a,prev,finished,b,last_space;
185 struct cittext *textlist = NULL;
187 char wordbuf[MAXWORDBUF];
189 long last_server_msg,now;
192 * we're going to keep track of the last time we talked to
193 * the server, so we can periodically send keep-alive messages
194 * out so it doesn't time out.
196 time(&last_server_msg);
198 /* first, load the text into the buffer */
199 fseek(fp,base_pos,0);
200 textlist = (struct cittext *)malloc(sizeof(struct cittext));
201 textlist->next = NULL;
202 strcpy(textlist->text,"");
206 while (a=getc(fp), a>=0) {
208 if ((a==32)||(a==9)||(a==13)||(a==10)) {
209 add_word(textlist,wordbuf);
211 if ((prev==13)||(prev==10)) {
212 add_word(textlist,"\n");
213 add_newline(textlist);
214 add_word(textlist,"");
218 wordbuf[strlen(wordbuf)+1] = 0;
219 wordbuf[strlen(wordbuf)] = a;
221 if (strlen(wordbuf)+3 > screenwidth) {
222 add_word(textlist,wordbuf);
230 prev = (appending ? 13 : (-1));
237 if ((a==32)&&(prev==13)) {
238 add_word(textlist,"\n");
239 add_newline(textlist);
242 if (strlen(wordbuf)>0) {
243 wordbuf[strlen(wordbuf)-1] = 0;
251 if (strlen(wordbuf)==0) finished = 1;
253 for (b=0; b<strlen(wordbuf); ++b)
254 if (wordbuf[b]==32) {
256 add_word(textlist,wordbuf);
257 strcpy(wordbuf,&wordbuf[b+1]);
260 add_word(textlist,wordbuf);
266 wordbuf[strlen(wordbuf)+1] = 0;
267 wordbuf[strlen(wordbuf)] = a;
269 if ((strlen(wordbuf)+3) > screenwidth) {
271 for (b=0; b<strlen(wordbuf); ++b)
272 if (wordbuf[b]==32) last_space = b;
274 for (b=0; b<strlen(wordbuf); ++b)
275 if (wordbuf[b]==32) {
277 add_word(textlist,wordbuf);
278 strcpy(wordbuf,&wordbuf[b+1]);
281 for (b=0; b<strlen(wordbuf); ++b) {
286 printf("\n%s",wordbuf);
289 add_word(textlist,wordbuf);
296 /* this check implements the server keep-alive */
298 if ( (now - last_server_msg) > S_KEEPALIVE ) {
301 last_server_msg = now;
304 } while (finished==0);
306 /* write the buffer back to disk */
307 fseek(fp,base_pos,0);
308 for (ptr=textlist; ptr!=NULL; ptr=ptr->next) {
309 fprintf(fp,"%s",ptr->text);
314 /* and deallocate the memory we used */
315 while (textlist!=NULL) {
323 int read_message(long int num, char pagin) /* Read a message from the server */
325 /* 0 = normal read, 1 = read with pagination, 2 = header */
339 sprintf(buf,"MSG0 %ld|%d",num,(pagin==READ_HEADER ? 1 : 0));
343 printf("*** msg #%ld: %s\n",num,buf);
345 lines_printed = checkpagin(lines_printed,pagin,screenheight);
350 strcpy(m_subject,"");
353 lines_printed = checkpagin(lines_printed,pagin,screenheight);
355 if (pagin == 1) color(3);
358 while(serv_gets(buf), strcmp(buf,"000")) {
363 checkpagin(lines_printed,
371 strcpy(reply_to,"nobody...xxxxx");
372 while(serv_gets(buf), struncmp(buf,"text",4)) {
373 if (!struncmp(buf,"nhdr=yes",8)) nhdr=1;
374 if (!struncmp(buf,"from=",5)) {
375 strcpy(from,&buf[5]);
377 if (nhdr==1) buf[0]='_';
378 if (!struncmp(buf,"type=",5))
379 format_type=atoi(&buf[5]);
380 if (!struncmp(buf,"from=",5)) {
381 printf("from %s ",&buf[5]);
383 if (!struncmp(buf,"subj=",5))
384 strcpy(m_subject,&buf[5]);
385 if ((!struncmp(buf,"hnod=",5))
386 && (strucmp(&buf[5],serv_info.serv_humannode)))
387 printf("(%s) ",&buf[5]);
388 if ((!struncmp(buf,"room=",5))
389 && (strucmp(&buf[5],room_name)))
390 printf("in %s> ",&buf[5]);
392 if (!struncmp(buf,"node=",5)) {
393 if ( (room_flags&QR_NETWORK)
394 || ((strucmp(&buf[5],serv_info.serv_nodename)
395 &&(strucmp(&buf[5],serv_info.serv_fqdn)))))
397 printf("@%s ",&buf[5]);
399 if ((!strucmp(&buf[5],serv_info.serv_nodename))
400 ||(!strucmp(&buf[5],serv_info.serv_fqdn)))
402 strcpy(reply_to,from);
405 sprintf(reply_to,"%s @ %s",from,&buf[5]);
409 if (!struncmp(buf,"rcpt=",5))
410 printf("to %s ",&buf[5]);
411 if (!struncmp(buf,"time=",5)) {
413 tm=(struct tm *)localtime(&now);
414 strcpy(buf,asctime(tm)); buf[strlen(buf)-1]=0;
415 strcpy(&buf[16],&buf[19]);
416 printf("%s ",&buf[4]);
429 if (pagin == 1 ) color(2);
431 lines_printed = checkpagin(lines_printed,pagin,screenheight);
433 if (strlen(m_subject)>0) {
434 printf("Subject: %s\n",m_subject);
436 lines_printed = checkpagin(lines_printed,pagin,screenheight);
439 if (format_type == 0) {
440 fr=fmout(screenwidth,NULL,
441 ((pagin==1) ? 1 : 0),
442 screenheight,(-1),1);
445 while(serv_gets(buf), strcmp(buf,"000")) {
448 lines_printed = lines_printed + 1 +
449 (strlen(buf)/screenwidth);
451 checkpagin(lines_printed,pagin,screenheight);
458 lines_printed = checkpagin(lines_printed,pagin,screenheight);
460 if (pagin == 1) color(7);
466 * replace string function for the built-in editor
468 void replace_string(char *filename, long int startpos)
477 int substitutions = 0;
480 printf("Enter text to be replaced:\n: ");
481 getline(srch_str,128);
482 if (strlen(srch_str)==0) return;
484 printf("Enter text to replace it with:\n: ");
485 getline(rplc_str,128);
487 fp=fopen(filename,"r+");
488 if (fp==NULL) return;
491 fseek(fp,startpos,0);
493 while (a=getc(fp), a>0) {
495 buf[strlen(buf)+1] = 0;
496 buf[strlen(buf)] = a;
497 if ( strlen(buf) >= strlen(srch_str) ) {
498 ptr=&buf[strlen(buf)-strlen(srch_str)];
499 if (!struncmp(ptr,srch_str,strlen(srch_str))) {
500 strcpy(ptr,rplc_str);
504 if (strlen(buf)>384) {
507 fwrite((char *)buf,128,1,fp);
508 strcpy(buf,&buf[128]);
514 if (strlen(buf)>0) fwrite((char *)buf,strlen(buf),1,fp);
517 printf("<R>eplace made %d substitution(s).\n\n",substitutions);
521 int make_message(char *filename, char *recipient, int anon_type, int format_type, int mode, char *boundary)
522 /* temporary file name */
523 /* NULL if it's not mail */
524 /* see MES_ types in header file */
534 if (mode==2) if (strlen(editor_path)==0) {
535 printf("*** No editor available, using built-in editor\n");
540 strcpy(datestr,asctime(localtime(&now)));
541 datestr[strlen(datestr)-1] = 0;
543 if (room_flags & QR_ANONONLY) {
547 printf(" %s from %s",datestr,fullname);
548 if (strlen(recipient)>0) printf(" to %s",recipient);
554 if (mode==1) printf("(Press ctrl-d when finished)\n");
556 fp=fopen(filename,"r");
558 fmout(screenwidth,fp,0,screenheight,0,0);
563 fp=fopen(filename,"w");
571 fp=fopen(filename,"r+");
578 fp=fopen(filename,"w");
580 a=inkey(); if (a==255) a=32;
586 if (a==10) putc(13,stdout);
592 e_ex_code = 1; /* start with a failed exit code */
594 cksum = file_checksum(filename);
596 chmod(filename,0600);
598 execlp(editor_path,editor_path,filename,NULL);
601 if (editor_pid>0) do {
603 b=ka_wait(&e_ex_code);
604 } while((b!=editor_pid)&&(b>=0));
611 if (file_checksum(filename) == cksum) {
612 printf("*** Aborted message.\n");
615 if (e_ex_code==0) goto MEFIN;
618 MECR1: printf("Entry cmd (? for options) -> ");
620 if (b==NEXT_KEY) b='n';
621 if (b==STOP_KEY) b='s';
622 b=(b&127); b=tolower(b);
628 if (b=='a') { printf("Abort\n"); goto MEABT; }
629 if (b=='c') { printf("Continue\n"); goto ME1; }
630 if (b=='s') { printf("Save message\n"); goto MEFIN; }
632 printf("Print formatted\n");
633 printf(" %s from %s",datestr,fullname);
634 if (strlen(recipient)>0) printf(" to %s",recipient);
636 fp=fopen(filename,"r");
638 fmout(screenwidth,fp,
639 ((userflags & US_PAGINATOR) ? 1 : 0),
647 printf("Replace string\n");
648 replace_string(filename,0L);
652 printf("Hold message\n");
655 if ((b=='f')&&(rc_allow_attachments==1)) {
656 printf("attach File\n");
657 if (strlen(boundary)==0) {
658 sprintf(boundary, "Citadel-Attachment-%ld.%d",
659 time(NULL), getpid() );
661 /* FIX FIX now you have to attach the file, stupid */
667 MEABT: printf("Are you sure? ");
668 if (yesno()==0) goto ME1;
669 MEABT2: unlink(filename);
674 * transmit message text to the server
676 void transmit_message(FILE *fp)
682 while (ch=getc(fp), (ch>=0)) {
684 if (!strcmp(buf,"000")) strcpy(buf,">000");
692 if ((ch==32)&&(strlen(buf)>200)) {
694 if (!strcmp(buf,"000")) strcpy(buf,">000");
698 if (strlen(buf)>250) {
699 if (!strcmp(buf,"000")) strcpy(buf,">000");
711 * entmsg() - edit and create a message
712 * returns 0 if message was saved
714 int entmsg(int is_reply, int c)
715 /* nonzero if this was a <R>eply command */
729 sprintf(cmd,"ENT0 0||0|%d",mode);
733 if ((strncmp(cmd,"570",3)) && (strncmp(cmd,"200",3))) {
734 printf("%s\n",&cmd[4]);
738 if (!strncmp(cmd,"570",3)) need_recp = 1;
740 if ((userflags & US_EXPERT) == 0) formout("entermsg");
746 strcpy(buf,reply_to);
749 printf("Enter recipient: ");
751 if (strlen(buf)==0) return(1);
754 else strcpy(buf,"sysop");
758 if (room_flags&QR_ANON2) {
759 printf("Anonymous (Y/N)? ");
763 /* if it's mail, we've got to check the validity of the recipient... */
765 sprintf(cmd,"ENT0 0|%s|%d|%d",buf,b,mode);
769 printf("%s\n",&cmd[4]);
774 /* learn the number of the newest message in in the room, so we can tell
775 * upon saving whether someone else has posted too
778 serv_puts("MSGS LAST|1");
781 printf("%s\n",&cmd[5]);
784 while (serv_gets(cmd), strcmp(cmd,"000")) {
785 msg_arr[num_msgs++] = atol(cmd);
789 /* now put together the message */
790 strcpy(boundary, "");
791 if ( make_message(temp,buf,b,0,c,boundary) != 0 ) return(2);
793 /* and send it to the server */
794 sprintf(cmd,"ENT0 1|%s|%d|%d||%s|",buf,b,mode,boundary);
798 printf("%s\n",&cmd[4]);
803 transmit_message(fp);
809 highmsg = msg_arr[num_msgs - 1];
811 serv_puts("MSGS NEW");
814 printf("%s\n",&cmd[5]);
817 while (serv_gets(cmd), strcmp(cmd,"000")) {
818 msg_arr[num_msgs++] = atol(cmd);
822 /* get new highest message number in room to set lrp for goto... */
823 maxmsgnum = msg_arr[num_msgs - 1];
825 /* now see if anyone else has posted in here */
827 for (a=0; a<num_msgs; ++a) if (msg_arr[a]>highmsg) ++b;
829 /* in the Mail> room, this algorithm always counts one message
830 * higher than in public rooms, so we decrement it by one */
834 "*** 1 additional message has been entered in this room by another user.\n");
836 "*** %d additional messages have been entered in this room by other users.\n",b);
841 void process_quote(void) { /* do editing on quoted file */
844 int line,qstart,qend;
846 /* Unlink the second temp file as soon as it's opened, so it'll get
847 * deleted even if the program dies
849 qfile = fopen(temp2,"r");
852 /* Display the quotable text with line numbers added */
854 fgets(buf,128,qfile);
855 while (fgets(buf,128,qfile)!=NULL) {
856 printf("%2d %s",++line,buf);
858 printf("Begin quoting at [ 1] : ");
860 qstart = (buf[0]==0) ? (1) : atoi(buf);
861 printf(" End quoting at [%d] : ",line);
863 qend = (buf[0]==0) ? (line) : atoi(buf);
866 fgets(buf,128,qfile);
867 tfile=fopen(temp,"w");
868 while(fgets(buf,128,qfile)!=NULL) {
869 if ((++line>=qstart)&&(line<=qend)) fprintf(tfile," >%s",buf);
871 fprintf(tfile," \n");
878 void readmsgs(int c, int rdir, int q) /* read contents of a room */
879 /* 0=Read all 1=Read new 2=Read old 3=Read last q */
880 /* 1=Forward (-1)=Reverse */
881 /* Number of msgs to read (if c==3) */
892 signal(SIGINT,SIG_IGN);
893 signal(SIGQUIT,SIG_IGN);
895 if (c<0) b=(MAXMSGS-1);
898 sprintf(prtfile,"/tmp/CPrt%d",getpid());
903 case 0: strcat(cmd,"ALL");
905 case 1: strcat(cmd,"NEW");
907 case 2: strcat(cmd,"OLD");
909 case 3: sprintf(&cmd[strlen(cmd)], "LAST|%d", q);
915 printf("%s\n",&cmd[5]);
918 while (serv_gets(cmd), strcmp(cmd,"000")) {
919 msg_arr[num_msgs++] = atol(cmd);
925 /* this loop cycles through each message... */
926 start = ( (rdir==1) ? 0 : (num_msgs-1) );
927 for (a=start; ((a<num_msgs)&&(a>=0)); a=a+rdir) {
928 while (msg_arr[a]==0L) {
929 a=a+rdir; if ((a==MAXMSGS)||(a==(-1))) return;
932 RAGAIN: pagin=((arcflag==0)&&(quotflag==0)&&
933 (userflags & US_PAGINATOR)) ? 1 : 0;
935 /* if we're doing a quote, set the screenwidth to 72 temporarily */
937 hold_sw = screenwidth;
941 /* now read the message... */
942 e=read_message(msg_arr[a],pagin);
944 /* ...and set the screenwidth back if we have to */
946 screenwidth = hold_sw;
948 RMSGREAD: fflush(stdout);
949 highest_msg_read = msg_arr[a];
951 freopen("/dev/tty","r+",stdout);
956 freopen("/dev/tty","r+",stdout);
960 freopen(prtfile,"r",stdin);
969 } while((g!=f)&&(g>=0));
970 printf("Message printed.\n");
973 if ((userflags&US_NOPROMPT)||(e==2)) e='n';
975 printf("(%d) ",num_msgs-a-1);
976 if (is_mail==1) printf("<R>eply ");
977 if (strlen(printcmd)>0) printf("<P>rint ");
978 printf("<B>ack <A>gain <Q>uote <H>eader <N>ext <S>top -> ");
981 e=(inkey()&127); e=tolower(e);
982 /* return key same as <N> */ if (e==13) e='n';
983 /* del/move for aides only */ if (!is_room_aide) if ((e=='d')||(e=='m')) e=0;
984 /* print only if available */ if ((e=='p')&&(strlen(printcmd)==0)) e=0;
985 /* can't move from Mail> */ if ((e=='m')&&(is_mail==1)) e=0;
986 /* can't reply in public rms */ if ((e=='r')&&(is_mail!=1)) e=0;
987 } while((e!='a')&&(e!='n')&&(e!='s')
988 &&(e!='d')&&(e!='m')&&(e!='p')
989 &&(e!='q')&&(e!='b')&&(e!='h')
992 case 's': printf("Stop\r"); break;
993 case 'a': printf("Again\r"); break;
994 case 'd': printf("Delete\r"); break;
995 case 'm': printf("Move\r"); break;
996 case 'n': printf("Next\r"); break;
997 case 'p': printf("Print\r"); break;
998 case 'q': printf("Quote\r"); break;
999 case 'b': printf("Back\r"); break;
1000 case 'h': printf("Header\r"); break;
1001 case 'r': printf("Reply\r"); break;
1003 if (userflags & US_DISAPPEAR)
1004 printf("%75s\r","");
1010 case 'p': fflush(stdout);
1011 freopen(prtfile,"w",stdout);
1014 case 'q': fflush(stdout);
1015 freopen(temp2,"w",stdout);
1019 case 'a': goto RAGAIN;
1020 case 'b': a=a-(rdir*2);
1022 case 'm': newprompt("Enter target room: ",targ,19);
1023 if (strlen(targ)>0) {
1024 sprintf(cmd,"MOVE %ld|%s",
1028 printf("%s\n",&cmd[4]);
1029 if (cmd[0]=='2') msg_arr[a]=0L;
1034 if (cmd[0]!='2') goto RMSGREAD;
1036 case 'd': printf("*** Delete this message? ");
1038 sprintf(cmd,"DELE %ld",msg_arr[a]);
1041 printf("%s\n",&cmd[4]);
1042 if (cmd[0]=='2') msg_arr[a]=0L;
1048 case 'h': read_message(msg_arr[a],READ_HEADER);
1050 case 'r': entmsg(1,(DEFAULT_ENTRY==46 ? 2 : 0));
1053 } /* end for loop */
1054 } /* end read routine */
1060 * View and edit a system message
1062 void edit_system_message(char *which_message)
1068 sprintf(desc, "system message '%s'", which_message);
1069 sprintf(read_cmd, "MESG %s", which_message);
1070 sprintf(write_cmd, "EMSG %s", which_message);
1071 do_edit(desc, read_cmd, "NOOP", write_cmd);