2 * Citadel/UX message support routines
3 * see copyright.txt for copyright information
23 #define MAXWORDBUF 256
28 char text[MAXWORDBUF];
31 void sttybbs(int cmd);
32 int struncmp(char *lstr, char *rstr, int len);
33 int fmout(int width, FILE *fp, char pagin, int height, int starting_lp, char subst);
34 int haschar(char *st, int ch);
35 int checkpagin(int lp, int pagin, int height);
36 void getline(char *string, int lim);
37 void formout(char *name);
39 void newprompt(char *prompt, char *str, int len);
40 int file_checksum(char *filename);
41 void color(int colornum);
42 void do_edit(char *desc, char *read_cmd, char *check_cmd, char *write_cmd);
45 long msg_arr[MAXMSGS];
47 extern char room_name[];
48 extern unsigned room_flags;
49 extern long highest_msg_read;
50 extern struct CtdlServInfo serv_info;
53 extern int screenwidth;
54 extern int screenheight;
55 extern long maxmsgnum;
58 extern char is_room_aide;
59 extern char fullname[];
61 extern unsigned userflags;
62 extern char sigcaught;
63 extern char editor_path[];
64 extern char printcmd[];
65 extern int rc_allow_attachments;
66 extern int rc_display_message_numbers;
67 extern int rc_force_mail_prompts;
69 extern int editor_pid;
73 void ka_sigcatch(int signum) {
76 signal(SIGALRM, ka_sigcatch);
83 * server keep-alive version of wait() (needed for external editor)
85 pid_t ka_wait(pid_t *kstatus)
90 signal(SIGALRM, ka_sigcatch);
94 } while (errno==EINTR);
95 signal(SIGALRM,SIG_IGN);
102 * version of system() that uses ka_wait()
104 int ka_system(char *shc)
113 perror("Cannot fork");
115 return((pid_t)childpid);
119 execlp("/bin/sh","sh","-c",shc,NULL);
125 waitpid = ka_wait(&retcode);
126 } while (waitpid != childpid);
136 * add a newline to the buffer...
138 void add_newline(struct cittext *textlist)
143 while (ptr->next != NULL) ptr = ptr->next;
145 while (ptr->text[strlen(ptr->text)-1]==32)
146 ptr->text[strlen(ptr->text)-1] = 0;
147 /* strcat(ptr->text,"\n"); */
149 ptr->next = (struct cittext *)
150 malloc(sizeof(struct cittext));
153 strcpy(ptr->text,"");
158 * add a word to the buffer...
160 void add_word(struct cittext *textlist, char *wordbuf)
166 while (ptr->next != NULL) ptr = ptr->next;
168 if (3+strlen(ptr->text)+strlen(wordbuf) > screenwidth) {
169 ptr->next = (struct cittext *)
170 malloc(sizeof(struct cittext));
173 strcpy(ptr->text,"");
176 strcat(ptr->text,wordbuf);
177 strcat(ptr->text," ");
182 * begin editing of an opened file pointed to by fp, beginning at position pos.
184 void citedit(FILE *fp, long int base_pos)
186 int a,prev,finished,b,last_space;
188 struct cittext *textlist = NULL;
190 char wordbuf[MAXWORDBUF];
192 time_t last_server_msg,now;
195 * we're going to keep track of the last time we talked to
196 * the server, so we can periodically send keep-alive messages
197 * out so it doesn't time out.
199 time(&last_server_msg);
201 /* first, load the text into the buffer */
202 fseek(fp,base_pos,0);
203 textlist = (struct cittext *)malloc(sizeof(struct cittext));
204 textlist->next = NULL;
205 strcpy(textlist->text,"");
209 while (a=getc(fp), a>=0) {
211 if ((a==32)||(a==9)||(a==13)||(a==10)) {
212 add_word(textlist,wordbuf);
214 if ((prev==13)||(prev==10)) {
215 add_word(textlist,"\n");
216 add_newline(textlist);
217 add_word(textlist,"");
221 wordbuf[strlen(wordbuf)+1] = 0;
222 wordbuf[strlen(wordbuf)] = a;
224 if (strlen(wordbuf)+3 > screenwidth) {
225 add_word(textlist,wordbuf);
233 prev = (appending ? 13 : (-1));
240 if ((a==32)&&(prev==13)) {
241 add_word(textlist,"\n");
242 add_newline(textlist);
245 if (strlen(wordbuf)>0) {
246 wordbuf[strlen(wordbuf)-1] = 0;
254 if (strlen(wordbuf)==0) finished = 1;
256 for (b=0; b<strlen(wordbuf); ++b)
257 if (wordbuf[b]==32) {
259 add_word(textlist,wordbuf);
260 strcpy(wordbuf,&wordbuf[b+1]);
263 add_word(textlist,wordbuf);
269 wordbuf[strlen(wordbuf)+1] = 0;
270 wordbuf[strlen(wordbuf)] = a;
272 if ((strlen(wordbuf)+3) > screenwidth) {
274 for (b=0; b<strlen(wordbuf); ++b)
275 if (wordbuf[b]==32) last_space = b;
277 for (b=0; b<strlen(wordbuf); ++b)
278 if (wordbuf[b]==32) {
280 add_word(textlist,wordbuf);
281 strcpy(wordbuf,&wordbuf[b+1]);
284 for (b=0; b<strlen(wordbuf); ++b) {
289 printf("\n%s",wordbuf);
292 add_word(textlist,wordbuf);
299 /* this check implements the server keep-alive */
301 if ( (now - last_server_msg) > S_KEEPALIVE ) {
304 last_server_msg = now;
307 } while (finished==0);
309 /* write the buffer back to disk */
310 fseek(fp,base_pos,0);
311 for (ptr=textlist; ptr!=NULL; ptr=ptr->next) {
312 fprintf(fp,"%s",ptr->text);
317 /* and deallocate the memory we used */
318 while (textlist!=NULL) {
326 int read_message(long int num, char pagin) /* Read a message from the server */
328 /* 0 = normal read, 1 = read with pagination, 2 = header */
342 sprintf(buf,"MSG0 %ld|%d",num,(pagin==READ_HEADER ? 1 : 0));
346 printf("*** msg #%ld: %s\n",num,buf);
348 lines_printed = checkpagin(lines_printed,pagin,screenheight);
353 strcpy(m_subject,"");
356 lines_printed = checkpagin(lines_printed,pagin,screenheight);
358 if (pagin == 1) color(3);
361 while(serv_gets(buf), strcmp(buf,"000")) {
366 checkpagin(lines_printed,
374 strcpy(reply_to,"nobody...xxxxx");
375 while(serv_gets(buf), struncmp(buf,"text",4)) {
376 if (!struncmp(buf,"nhdr=yes",8)) nhdr=1;
377 if (!struncmp(buf,"from=",5)) {
378 strcpy(from,&buf[5]);
380 if (nhdr==1) buf[0]='_';
381 if (!struncmp(buf,"type=",5))
382 format_type=atoi(&buf[5]);
383 if ((!struncmp(buf,"msgn=",5))&&(rc_display_message_numbers)) {
385 printf("[#%s] ",&buf[5]);
387 if (!struncmp(buf,"from=",5)) {
389 printf("from %s ",&buf[5]);
391 if (!struncmp(buf,"subj=",5))
392 strcpy(m_subject,&buf[5]);
393 if ((!struncmp(buf,"hnod=",5))
394 && (strucmp(&buf[5],serv_info.serv_humannode))) {
396 printf("(%s) ",&buf[5]);
398 if ((!struncmp(buf,"room=",5))
399 && (strucmp(&buf[5],room_name))) {
401 printf("in %s> ",&buf[5]);
404 if (!struncmp(buf,"node=",5)) {
405 if ( (room_flags&QR_NETWORK)
406 || ((strucmp(&buf[5],serv_info.serv_nodename)
407 &&(strucmp(&buf[5],serv_info.serv_fqdn)))))
410 printf("@%s ",&buf[5]);
412 if ((!strucmp(&buf[5],serv_info.serv_nodename))
413 ||(!strucmp(&buf[5],serv_info.serv_fqdn)))
415 strcpy(reply_to,from);
418 sprintf(reply_to,"%s @ %s",from,&buf[5]);
422 if (!struncmp(buf,"rcpt=",5)) {
424 printf("to %s ",&buf[5]);
426 if (!struncmp(buf,"time=",5)) {
428 tm=(struct tm *)localtime(&now);
429 strcpy(buf,asctime(tm)); buf[strlen(buf)-1]=0;
430 strcpy(&buf[16],&buf[19]);
432 printf("%s ",&buf[4]);
445 if (pagin == 1 ) color(2);
447 lines_printed = checkpagin(lines_printed,pagin,screenheight);
449 if (strlen(m_subject)>0) {
450 printf("Subject: %s\n",m_subject);
452 lines_printed = checkpagin(lines_printed,pagin,screenheight);
455 if (format_type == 0) {
456 fr=fmout(screenwidth,NULL,
457 ((pagin==1) ? 1 : 0),
458 screenheight,(-1),1);
461 while(serv_gets(buf), strcmp(buf,"000")) {
464 lines_printed = lines_printed + 1 +
465 (strlen(buf)/screenwidth);
467 checkpagin(lines_printed,pagin,screenheight);
474 lines_printed = checkpagin(lines_printed,pagin,screenheight);
476 if (pagin == 1) color(7);
482 * replace string function for the built-in editor
484 void replace_string(char *filename, long int startpos)
493 int substitutions = 0;
496 printf("Enter text to be replaced:\n: ");
497 getline(srch_str,128);
498 if (strlen(srch_str)==0) return;
500 printf("Enter text to replace it with:\n: ");
501 getline(rplc_str,128);
503 fp=fopen(filename,"r+");
504 if (fp==NULL) return;
507 fseek(fp,startpos,0);
509 while (a=getc(fp), a>0) {
511 buf[strlen(buf)+1] = 0;
512 buf[strlen(buf)] = a;
513 if ( strlen(buf) >= strlen(srch_str) ) {
514 ptr=&buf[strlen(buf)-strlen(srch_str)];
515 if (!struncmp(ptr,srch_str,strlen(srch_str))) {
516 strcpy(ptr,rplc_str);
520 if (strlen(buf)>384) {
523 fwrite((char *)buf,128,1,fp);
524 strcpy(buf,&buf[128]);
530 if (strlen(buf)>0) fwrite((char *)buf,strlen(buf),1,fp);
533 printf("<R>eplace made %d substitution(s).\n\n",substitutions);
537 int make_message(char *filename, char *recipient, int anon_type, int format_type, int mode)
538 /* temporary file name */
539 /* NULL if it's not mail */
540 /* see MES_ types in header file */
551 if (mode==2) if (strlen(editor_path)==0) {
552 printf("*** No editor available, using built-in editor\n");
557 strcpy(datestr,asctime(localtime(&now)));
558 datestr[strlen(datestr)-1] = 0;
560 if (room_flags & QR_ANONONLY) {
564 printf(" %s from %s",datestr,fullname);
565 if (strlen(recipient)>0) printf(" to %s",recipient);
571 if (mode==1) printf("(Press ctrl-d when finished)\n");
573 fp=fopen(filename,"r");
575 fmout(screenwidth,fp,0,screenheight,0,0);
580 fp=fopen(filename,"w");
588 fp=fopen(filename,"r+");
595 fp=fopen(filename,"w");
597 a=inkey(); if (a==255) a=32;
603 if (a==10) putc(13,stdout);
609 e_ex_code = 1; /* start with a failed exit code */
611 cksum = file_checksum(filename);
613 chmod(filename,0600);
615 execlp(editor_path,editor_path,filename,NULL);
618 if (editor_pid>0) do {
620 b=ka_wait(&e_ex_code);
621 } while((b!=editor_pid)&&(b>=0));
628 if (file_checksum(filename) == cksum) {
629 printf("*** Aborted message.\n");
632 if (e_ex_code==0) goto MEFIN;
635 MECR1: printf("Entry cmd (? for options) -> ");
637 if (b==NEXT_KEY) b='n';
638 if (b==STOP_KEY) b='s';
639 b=(b&127); b=tolower(b);
645 if (b=='a') { printf("Abort\n"); goto MEABT; }
646 if (b=='c') { printf("Continue\n"); goto ME1; }
647 if (b=='s') { printf("Save message\n"); goto MEFIN; }
649 printf("Print formatted\n");
650 printf(" %s from %s",datestr,fullname);
651 if (strlen(recipient)>0) printf(" to %s",recipient);
653 fp=fopen(filename,"r");
655 fmout(screenwidth,fp,
656 ((userflags & US_PAGINATOR) ? 1 : 0),
664 printf("Replace string\n");
665 replace_string(filename,0L);
669 printf("Hold message\n");
676 MEABT: printf("Are you sure? ");
677 if (yesno()==0) goto ME1;
678 MEABT2: unlink(filename);
683 * transmit message text to the server
685 void transmit_message(FILE *fp)
691 while (ch=getc(fp), (ch>=0)) {
693 if (!strcmp(buf,"000")) strcpy(buf,">000");
701 if ((ch==32)&&(strlen(buf)>200)) {
703 if (!strcmp(buf,"000")) strcpy(buf,">000");
707 if (strlen(buf)>250) {
708 if (!strcmp(buf,"000")) strcpy(buf,">000");
720 * entmsg() - edit and create a message
721 * returns 0 if message was saved
723 int entmsg(int is_reply, int c)
724 /* nonzero if this was a <R>eply command */
737 sprintf(cmd,"ENT0 0||0|%d",mode);
741 if ((strncmp(cmd,"570",3)) && (strncmp(cmd,"200",3))) {
742 printf("%s\n",&cmd[4]);
746 if (!strncmp(cmd,"570",3)) need_recp = 1;
748 if ((userflags & US_EXPERT) == 0) formout("entermsg");
754 strcpy(buf,reply_to);
757 printf("Enter recipient: ");
759 if (strlen(buf)==0) return(1);
762 else strcpy(buf,"sysop");
766 if (room_flags&QR_ANONOPT) {
767 printf("Anonymous (Y/N)? ");
771 /* if it's mail, we've got to check the validity of the recipient... */
773 sprintf(cmd,"ENT0 0|%s|%d|%d",buf,b,mode);
777 printf("%s\n",&cmd[4]);
782 /* learn the number of the newest message in in the room, so we can tell
783 * upon saving whether someone else has posted too
786 serv_puts("MSGS LAST|1");
789 printf("%s\n",&cmd[5]);
792 while (serv_gets(cmd), strcmp(cmd,"000")) {
793 msg_arr[num_msgs++] = atol(cmd);
797 /* now put together the message */
798 if ( make_message(temp,buf,b,0,c) != 0 ) return(2);
800 /* and send it to the server */
801 sprintf(cmd,"ENT0 1|%s|%d|%d||",buf,b,mode);
805 printf("%s\n",&cmd[4]);
810 transmit_message(fp);
816 highmsg = msg_arr[num_msgs - 1];
818 serv_puts("MSGS NEW");
821 printf("%s\n",&cmd[5]);
824 while (serv_gets(cmd), strcmp(cmd,"000")) {
825 msg_arr[num_msgs++] = atol(cmd);
829 /* get new highest message number in room to set lrp for goto... */
830 maxmsgnum = msg_arr[num_msgs - 1];
832 /* now see if anyone else has posted in here */
834 for (a=0; a<num_msgs; ++a) if (msg_arr[a]>highmsg) ++b;
836 /* in the Mail> room, this algorithm always counts one message
837 * higher than in public rooms, so we decrement it by one */
841 "*** 1 additional message has been entered in this room by another user.\n");
843 "*** %d additional messages have been entered in this room by other users.\n",b);
848 void process_quote(void) { /* do editing on quoted file */
851 int line,qstart,qend;
853 /* Unlink the second temp file as soon as it's opened, so it'll get
854 * deleted even if the program dies
856 qfile = fopen(temp2,"r");
859 /* Display the quotable text with line numbers added */
861 fgets(buf,128,qfile);
862 while (fgets(buf,128,qfile)!=NULL) {
863 printf("%2d %s",++line,buf);
865 printf("Begin quoting at [ 1] : ");
867 qstart = (buf[0]==0) ? (1) : atoi(buf);
868 printf(" End quoting at [%d] : ",line);
870 qend = (buf[0]==0) ? (line) : atoi(buf);
873 fgets(buf,128,qfile);
874 tfile=fopen(temp,"w");
875 while(fgets(buf,128,qfile)!=NULL) {
876 if ((++line>=qstart)&&(line<=qend)) fprintf(tfile," >%s",buf);
878 fprintf(tfile," \n");
885 void readmsgs(int c, int rdir, int q) /* read contents of a room */
886 /* 0=Read all 1=Read new 2=Read old 3=Read last q */
887 /* 1=Forward (-1)=Reverse */
888 /* Number of msgs to read (if c==3) */
898 char targ[ROOMNAMELEN];
900 signal(SIGINT,SIG_IGN);
901 signal(SIGQUIT,SIG_IGN);
903 if (c<0) b=(MAXMSGS-1);
906 sprintf(prtfile,"/tmp/CPrt%d",getpid());
911 case 0: strcat(cmd,"ALL");
913 case 1: strcat(cmd,"NEW");
915 case 2: strcat(cmd,"OLD");
917 case 3: sprintf(&cmd[strlen(cmd)], "LAST|%d", q);
923 printf("%s\n",&cmd[5]);
926 while (serv_gets(cmd), strcmp(cmd,"000")) {
927 msg_arr[num_msgs++] = atol(cmd);
933 /* this loop cycles through each message... */
934 start = ( (rdir==1) ? 0 : (num_msgs-1) );
935 for (a=start; ((a<num_msgs)&&(a>=0)); a=a+rdir) {
936 while (msg_arr[a]==0L) {
937 a=a+rdir; if ((a==MAXMSGS)||(a==(-1))) return;
940 RAGAIN: pagin=((arcflag==0)&&(quotflag==0)&&
941 (userflags & US_PAGINATOR)) ? 1 : 0;
943 /* If we're doing a quote, set the screenwidth to 72 temporarily */
945 hold_sw = screenwidth;
949 /* If printing or archiving, set the screenwidth to 80 temporarily */
951 hold_sw = screenwidth;
955 /* now read the message... */
956 e=read_message(msg_arr[a],pagin);
958 /* ...and set the screenwidth back if we have to */
959 if ((quotflag)||(arcflag)) {
960 screenwidth = hold_sw;
962 RMSGREAD: fflush(stdout);
963 highest_msg_read = msg_arr[a];
965 freopen("/dev/tty","r+",stdout);
967 enable_color = hold_color;
971 freopen("/dev/tty","r+",stdout);
973 enable_color = hold_color;
976 freopen(prtfile,"r",stdin);
985 } while((g!=f)&&(g>=0));
986 printf("Message printed.\n");
989 if ( ((userflags&US_NOPROMPT)||(e==2))
990 && (((room_flags&QR_MAILBOX)==0)
991 ||(rc_force_mail_prompts==0)) ) {
995 printf("(%d) ",num_msgs-a-1);
996 if (is_mail==1) printf("<R>eply ");
997 if (strlen(printcmd)>0) printf("<P>rint ");
998 printf("<B>ack <A>gain <Q>uote <H>eader <N>ext <S>top -> ");
1001 e=(inkey()&127); e=tolower(e);
1002 /* return key same as <N> */ if (e==13) e='n';
1003 /* space key same as <N> */ if (e==32) e='n';
1004 /* del/move for aides only */ if ((!is_room_aide)
1005 &&((room_flags&QR_MAILBOX)==0)) {
1006 if ((e=='d')||(e=='m')) e=0;
1008 /* print only if available */ if ((e=='p')&&(strlen(printcmd)==0)) e=0;
1009 /* can't move from Mail> */ if ((e=='m')&&(is_mail==1)) e=0;
1010 /* can't reply in public rms */ if ((e=='r')&&(is_mail!=1)) e=0;
1011 } while((e!='a')&&(e!='n')&&(e!='s')
1012 &&(e!='d')&&(e!='m')&&(e!='p')
1013 &&(e!='q')&&(e!='b')&&(e!='h')
1016 case 's': printf("Stop\r"); break;
1017 case 'a': printf("Again\r"); break;
1018 case 'd': printf("Delete\r"); break;
1019 case 'm': printf("Move\r"); break;
1020 case 'n': printf("Next\r"); break;
1021 case 'p': printf("Print\r"); break;
1022 case 'q': printf("Quote\r"); break;
1023 case 'b': printf("Back\r"); break;
1024 case 'h': printf("Header\r"); break;
1025 case 'r': printf("Reply\r"); break;
1027 if (userflags & US_DISAPPEAR)
1028 printf("%75s\r","");
1034 case 'p': fflush(stdout);
1035 freopen(prtfile,"w",stdout);
1037 hold_color = enable_color;
1040 case 'q': fflush(stdout);
1041 freopen(temp2,"w",stdout);
1043 hold_color = enable_color;
1047 case 'a': goto RAGAIN;
1048 case 'b': a=a-(rdir*2);
1050 case 'm': newprompt("Enter target room: ",
1051 targ,ROOMNAMELEN-1);
1052 if (strlen(targ)>0) {
1053 sprintf(cmd,"MOVE %ld|%s",
1057 printf("%s\n",&cmd[4]);
1058 if (cmd[0]=='2') msg_arr[a]=0L;
1063 if (cmd[0]!='2') goto RMSGREAD;
1065 case 'd': printf("*** Delete this message? ");
1067 sprintf(cmd,"DELE %ld",msg_arr[a]);
1070 printf("%s\n",&cmd[4]);
1071 if (cmd[0]=='2') msg_arr[a]=0L;
1077 case 'h': read_message(msg_arr[a],READ_HEADER);
1079 case 'r': entmsg(1,(DEFAULT_ENTRY==46 ? 2 : 0));
1082 } /* end for loop */
1083 } /* end read routine */
1089 * View and edit a system message
1091 void edit_system_message(char *which_message)
1097 sprintf(desc, "system message '%s'", which_message);
1098 sprintf(read_cmd, "MESG %s", which_message);
1099 sprintf(write_cmd, "EMSG %s", which_message);
1100 do_edit(desc, read_cmd, "NOOP", write_cmd);