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[];
65 extern int editor_pid;
69 void ka_sigcatch(void) {
72 signal(SIGALRM, (void *)ka_sigcatch);
79 * server keep-alive version of wait() (needed for external editor)
81 pid_t ka_wait(pid_t *kstatus)
86 signal(SIGALRM, (void *)ka_sigcatch);
90 } while (errno==EINTR);
91 signal(SIGALRM,SIG_IGN);
98 * version of system() that uses ka_wait()
100 int ka_system(char *shc)
109 perror("Cannot fork");
111 return((pid_t)childpid);
115 execlp("/bin/sh","sh","-c",shc,NULL);
121 waitpid = ka_wait(&retcode);
122 } while (waitpid != childpid);
132 * add a newline to the buffer...
134 void add_newline(struct cittext *textlist)
139 while (ptr->next != NULL) ptr = ptr->next;
141 while (ptr->text[strlen(ptr->text)-1]==32)
142 ptr->text[strlen(ptr->text)-1] = 0;
143 /* strcat(ptr->text,"\n"); */
145 ptr->next = (struct cittext *)
146 malloc(sizeof(struct cittext));
149 strcpy(ptr->text,"");
154 * add a word to the buffer...
156 void add_word(struct cittext *textlist, char *wordbuf)
162 while (ptr->next != NULL) ptr = ptr->next;
164 if (3+strlen(ptr->text)+strlen(wordbuf) > screenwidth) {
165 ptr->next = (struct cittext *)
166 malloc(sizeof(struct cittext));
169 strcpy(ptr->text,"");
172 strcat(ptr->text,wordbuf);
173 strcat(ptr->text," ");
178 * begin editing of an opened file pointed to by fp, beginning at position pos.
180 void citedit(FILE *fp, long int base_pos)
182 int a,prev,finished,b,last_space;
184 struct cittext *textlist = NULL;
186 char wordbuf[MAXWORDBUF];
188 long last_server_msg,now;
191 * we're going to keep track of the last time we talked to
192 * the server, so we can periodically send keep-alive messages
193 * out so it doesn't time out.
195 time(&last_server_msg);
197 /* first, load the text into the buffer */
198 fseek(fp,base_pos,0);
199 textlist = (struct cittext *)malloc(sizeof(struct cittext));
200 textlist->next = NULL;
201 strcpy(textlist->text,"");
205 while (a=getc(fp), a>=0) {
207 if ((a==32)||(a==9)||(a==13)||(a==10)) {
208 add_word(textlist,wordbuf);
210 if ((prev==13)||(prev==10)) {
211 add_word(textlist,"\n");
212 add_newline(textlist);
213 add_word(textlist,"");
217 wordbuf[strlen(wordbuf)+1] = 0;
218 wordbuf[strlen(wordbuf)] = a;
220 if (strlen(wordbuf)+3 > screenwidth) {
221 add_word(textlist,wordbuf);
229 prev = (appending ? 13 : (-1));
236 if ((a==32)&&(prev==13)) {
237 add_word(textlist,"\n");
238 add_newline(textlist);
241 if (strlen(wordbuf)>0) {
242 wordbuf[strlen(wordbuf)-1] = 0;
250 if (strlen(wordbuf)==0) finished = 1;
252 for (b=0; b<strlen(wordbuf); ++b)
253 if (wordbuf[b]==32) {
255 add_word(textlist,wordbuf);
256 strcpy(wordbuf,&wordbuf[b+1]);
259 add_word(textlist,wordbuf);
265 wordbuf[strlen(wordbuf)+1] = 0;
266 wordbuf[strlen(wordbuf)] = a;
268 if ((strlen(wordbuf)+3) > screenwidth) {
270 for (b=0; b<strlen(wordbuf); ++b)
271 if (wordbuf[b]==32) last_space = b;
273 for (b=0; b<strlen(wordbuf); ++b)
274 if (wordbuf[b]==32) {
276 add_word(textlist,wordbuf);
277 strcpy(wordbuf,&wordbuf[b+1]);
280 for (b=0; b<strlen(wordbuf); ++b) {
285 printf("\n%s",wordbuf);
288 add_word(textlist,wordbuf);
295 /* this check implements the server keep-alive */
297 if ( (now - last_server_msg) > S_KEEPALIVE ) {
300 last_server_msg = now;
303 } while (finished==0);
305 /* write the buffer back to disk */
306 fseek(fp,base_pos,0);
307 for (ptr=textlist; ptr!=NULL; ptr=ptr->next) {
308 fprintf(fp,"%s",ptr->text);
313 /* and deallocate the memory we used */
314 while (textlist!=NULL) {
322 int read_message(long int num, char pagin) /* Read a message from the server */
324 /* 0 = normal read, 1 = read with pagination, 2 = header */
338 sprintf(buf,"MSG0 %ld|%d",num,(pagin==READ_HEADER ? 1 : 0));
342 printf("*** msg #%ld: %s\n",num,buf);
344 lines_printed = checkpagin(lines_printed,pagin,screenheight);
349 strcpy(m_subject,"");
352 lines_printed = checkpagin(lines_printed,pagin,screenheight);
354 if (pagin == 1) color(3);
357 while(serv_gets(buf), strcmp(buf,"000")) {
362 checkpagin(lines_printed,
370 strcpy(reply_to,"nobody...xxxxx");
371 while(serv_gets(buf), struncmp(buf,"text",4)) {
372 if (!struncmp(buf,"nhdr=yes",8)) nhdr=1;
373 if (!struncmp(buf,"from=",5)) {
374 strcpy(from,&buf[5]);
376 if (nhdr==1) buf[0]='_';
377 if (!struncmp(buf,"type=",5))
378 format_type=atoi(&buf[5]);
379 if (!struncmp(buf,"from=",5)) {
380 printf("from %s ",&buf[5]);
382 if (!struncmp(buf,"subj=",5))
383 strcpy(m_subject,&buf[5]);
384 if ((!struncmp(buf,"hnod=",5))
385 && (strucmp(&buf[5],serv_info.serv_humannode)))
386 printf("(%s) ",&buf[5]);
387 if ((!struncmp(buf,"room=",5))
388 && (strucmp(&buf[5],room_name)))
389 printf("in %s> ",&buf[5]);
391 if (!struncmp(buf,"node=",5)) {
392 if ( (room_flags&QR_NETWORK)
393 || ((strucmp(&buf[5],serv_info.serv_nodename)
394 &&(strucmp(&buf[5],serv_info.serv_fqdn)))))
396 printf("@%s ",&buf[5]);
398 if ((!strucmp(&buf[5],serv_info.serv_nodename))
399 ||(!strucmp(&buf[5],serv_info.serv_fqdn)))
401 strcpy(reply_to,from);
404 sprintf(reply_to,"%s @ %s",from,&buf[5]);
408 if (!struncmp(buf,"rcpt=",5))
409 printf("to %s ",&buf[5]);
410 if (!struncmp(buf,"time=",5)) {
412 tm=(struct tm *)localtime(&now);
413 strcpy(buf,asctime(tm)); buf[strlen(buf)-1]=0;
414 strcpy(&buf[16],&buf[19]);
415 printf("%s ",&buf[4]);
428 if (pagin == 1 ) color(2);
430 lines_printed = checkpagin(lines_printed,pagin,screenheight);
432 if (strlen(m_subject)>0) {
433 printf("Subject: %s\n",m_subject);
435 lines_printed = checkpagin(lines_printed,pagin,screenheight);
438 if (format_type == 0) {
439 fr=fmout(screenwidth,NULL,
440 ((pagin==1) ? 1 : 0),
441 screenheight,(-1),1);
444 while(serv_gets(buf), strcmp(buf,"000")) {
447 lines_printed = lines_printed + 1 +
448 (strlen(buf)/screenwidth);
450 checkpagin(lines_printed,pagin,screenheight);
457 lines_printed = checkpagin(lines_printed,pagin,screenheight);
459 if (pagin == 1) color(7);
465 * replace string function for the built-in editor
467 void replace_string(char *filename, long int startpos)
476 int substitutions = 0;
479 printf("Enter text to be replaced:\n: ");
480 getline(srch_str,128);
481 if (strlen(srch_str)==0) return;
483 printf("Enter text to replace it with:\n: ");
484 getline(rplc_str,128);
486 fp=fopen(filename,"r+");
487 if (fp==NULL) return;
490 fseek(fp,startpos,0);
492 while (a=getc(fp), a>0) {
494 buf[strlen(buf)+1] = 0;
495 buf[strlen(buf)] = a;
496 if ( strlen(buf) >= strlen(srch_str) ) {
497 ptr=&buf[strlen(buf)-strlen(srch_str)];
498 if (!struncmp(ptr,srch_str,strlen(srch_str))) {
499 strcpy(ptr,rplc_str);
503 if (strlen(buf)>384) {
506 fwrite((char *)buf,128,1,fp);
507 strcpy(buf,&buf[128]);
513 if (strlen(buf)>0) fwrite((char *)buf,strlen(buf),1,fp);
516 printf("<R>eplace made %d substitution(s).\n\n",substitutions);
520 int make_message(char *filename, char *recipient, int anon_type, int format_type, int mode)
521 /* temporary file name */
522 /* NULL if it's not mail */
523 /* see MES_ types in header file */
533 if (mode==2) if (strlen(editor_path)==0) {
534 printf("*** No editor available, using built-in editor\n");
539 strcpy(datestr,asctime(localtime(&now)));
540 datestr[strlen(datestr)-1] = 0;
542 if (room_flags & QR_ANONONLY) {
546 printf(" %s from %s",datestr,fullname);
547 if (strlen(recipient)>0) printf(" to %s",recipient);
553 if (mode==1) printf("(Press ctrl-d when finished)\n");
555 fp=fopen(filename,"r");
557 fmout(screenwidth,fp,0,screenheight,0,0);
562 fp=fopen(filename,"w");
570 fp=fopen(filename,"r+");
577 fp=fopen(filename,"w");
579 a=inkey(); if (a==255) a=32;
585 if (a==10) putc(13,stdout);
591 e_ex_code = 1; /* start with a failed exit code */
593 cksum = file_checksum(filename);
595 chmod(filename,0600);
597 execlp(editor_path,editor_path,filename,NULL);
600 if (editor_pid>0) do {
602 b=ka_wait(&e_ex_code);
603 } while((b!=editor_pid)&&(b>=0));
610 if (file_checksum(filename) == cksum) {
611 printf("*** Aborted message.\n");
614 if (e_ex_code==0) goto MEFIN;
617 MECR1: printf("Entry cmd (? for options) -> ");
619 if (b==NEXT_KEY) b='n';
620 if (b==STOP_KEY) b='s';
621 b=(b&127); b=tolower(b);
627 if (b=='a') { printf("Abort\n"); goto MEABT; }
628 if (b=='c') { printf("Continue\n"); goto ME1; }
629 if (b=='s') { printf("Save message\n"); goto MEFIN; }
631 printf("Print formatted\n");
632 printf(" %s from %s",datestr,fullname);
633 if (strlen(recipient)>0) printf(" to %s",recipient);
635 fp=fopen(filename,"r");
637 fmout(screenwidth,fp,
638 ((userflags & US_PAGINATOR) ? 1 : 0),
646 printf("Replace string\n");
647 replace_string(filename,0L);
651 printf("Hold message\n");
658 MEABT: printf("Are you sure? ");
659 if (yesno()==0) goto ME1;
660 MEABT2: unlink(filename);
665 * transmit message text to the server
667 void transmit_message(FILE *fp)
673 while (ch=getc(fp), (ch>=0)) {
675 if (!strcmp(buf,"000")) strcpy(buf,">000");
683 if ((ch==32)&&(strlen(buf)>200)) {
685 if (!strcmp(buf,"000")) strcpy(buf,">000");
689 if (strlen(buf)>250) {
690 if (!strcmp(buf,"000")) strcpy(buf,">000");
702 * entmsg() - edit and create a message
703 * returns 0 if message was saved
705 int entmsg(int is_reply, int c)
706 /* nonzero if this was a <R>eply command */
719 sprintf(cmd,"ENT0 0||0|%d",mode);
723 if ((strncmp(cmd,"570",3)) && (strncmp(cmd,"200",3))) {
724 printf("%s\n",&cmd[4]);
728 if (!strncmp(cmd,"570",3)) need_recp = 1;
730 if ((userflags & US_EXPERT) == 0) formout("entermsg");
736 strcpy(buf,reply_to);
739 printf("Enter recipient: ");
741 if (strlen(buf)==0) return(1);
744 else strcpy(buf,"sysop");
748 if (room_flags&QR_ANON2) {
749 printf("Anonymous (Y/N)? ");
753 /* if it's mail, we've got to check the validity of the recipient... */
755 sprintf(cmd,"ENT0 0|%s|%d|%d",buf,b,mode);
759 printf("%s\n",&cmd[4]);
764 /* learn the number of the newest message in in the room, so we can tell
765 * upon saving whether someone else has posted too
768 serv_puts("MSGS LAST|1");
771 printf("%s\n",&cmd[5]);
774 while (serv_gets(cmd), strcmp(cmd,"000")) {
775 msg_arr[num_msgs++] = atol(cmd);
779 /* now put together the message */
780 a=make_message(temp,buf,b,0,c);
786 /* and send it to the server */
787 sprintf(cmd,"ENT0 1|%s|%d|%d",buf,b,mode);
791 printf("%s\n",&cmd[4]);
796 transmit_message(fp);
802 highmsg = msg_arr[num_msgs - 1];
804 serv_puts("MSGS NEW");
807 printf("%s\n",&cmd[5]);
810 while (serv_gets(cmd), strcmp(cmd,"000")) {
811 msg_arr[num_msgs++] = atol(cmd);
815 /* get new highest message number in room to set lrp for goto... */
816 maxmsgnum = msg_arr[num_msgs - 1];
818 /* now see if anyone else has posted in here */
820 for (a=0; a<num_msgs; ++a) if (msg_arr[a]>highmsg) ++b;
822 /* in the Mail> room, this algorithm always counts one message
823 * higher than in public rooms, so we decrement it by one */
827 "*** 1 additional message has been entered in this room by another user.\n");
829 "*** %d additional messages have been entered in this room by other users.\n",b);
834 void process_quote(void) { /* do editing on quoted file */
837 int line,qstart,qend;
839 /* Unlink the second temp file as soon as it's opened, so it'll get
840 * deleted even if the program dies
842 qfile = fopen(temp2,"r");
845 /* Display the quotable text with line numbers added */
847 fgets(buf,128,qfile);
848 while (fgets(buf,128,qfile)!=NULL) {
849 printf("%2d %s",++line,buf);
851 printf("Begin quoting at [ 1] : ");
853 qstart = (buf[0]==0) ? (1) : atoi(buf);
854 printf(" End quoting at [%d] : ",line);
856 qend = (buf[0]==0) ? (line) : atoi(buf);
859 fgets(buf,128,qfile);
860 tfile=fopen(temp,"w");
861 while(fgets(buf,128,qfile)!=NULL) {
862 if ((++line>=qstart)&&(line<=qend)) fprintf(tfile," >%s",buf);
864 fprintf(tfile," \n");
871 void readmsgs(int c, int rdir, int q) /* read contents of a room */
872 /* 0=Read all 1=Read new 2=Read old 3=Read last q */
873 /* 1=Forward (-1)=Reverse */
874 /* Number of msgs to read (if c==3) */
885 signal(SIGINT,SIG_IGN);
886 signal(SIGQUIT,SIG_IGN);
888 if (c<0) b=(MAXMSGS-1);
891 sprintf(prtfile,"/tmp/CPrt%d",getpid());
896 case 0: strcat(cmd,"ALL");
898 case 1: strcat(cmd,"NEW");
900 case 2: strcat(cmd,"OLD");
902 case 3: sprintf(&cmd[strlen(cmd)], "LAST|%d", q);
908 printf("%s\n",&cmd[5]);
911 while (serv_gets(cmd), strcmp(cmd,"000")) {
912 msg_arr[num_msgs++] = atol(cmd);
918 /* this loop cycles through each message... */
919 start = ( (rdir==1) ? 0 : (num_msgs-1) );
920 for (a=start; ((a<num_msgs)&&(a>=0)); a=a+rdir) {
921 while (msg_arr[a]==0L) {
922 a=a+rdir; if ((a==MAXMSGS)||(a==(-1))) return;
925 RAGAIN: pagin=((arcflag==0)&&(quotflag==0)&&
926 (userflags & US_PAGINATOR)) ? 1 : 0;
928 /* if we're doing a quote, set the screenwidth to 72 temporarily */
930 hold_sw = screenwidth;
934 /* now read the message... */
935 e=read_message(msg_arr[a],pagin);
937 /* ...and set the screenwidth back if we have to */
939 screenwidth = hold_sw;
941 RMSGREAD: fflush(stdout);
942 highest_msg_read = msg_arr[a];
944 freopen("/dev/tty","r+",stdout);
949 freopen("/dev/tty","r+",stdout);
953 freopen(prtfile,"r",stdin);
962 } while((g!=f)&&(g>=0));
963 printf("Message printed.\n");
966 if ((userflags&US_NOPROMPT)||(e==2)) e='n';
968 printf("(%d) ",num_msgs-a-1);
969 if (is_mail==1) printf("<R>eply ");
970 if (strlen(printcmd)>0) printf("<P>rint ");
971 printf("<B>ack <A>gain <Q>uote <H>eader <N>ext <S>top -> ");
974 e=(inkey()&127); e=tolower(e);
975 /* return key same as <N> */ if (e==13) e='n';
976 /* del/move for aides only */ if (!is_room_aide) if ((e=='d')||(e=='m')) e=0;
977 /* print only if available */ if ((e=='p')&&(strlen(printcmd)==0)) e=0;
978 /* can't move from Mail> */ if ((e=='m')&&(is_mail==1)) e=0;
979 /* can't reply in public rms */ if ((e=='r')&&(is_mail!=1)) e=0;
980 } while((e!='a')&&(e!='n')&&(e!='s')
981 &&(e!='d')&&(e!='m')&&(e!='p')
982 &&(e!='q')&&(e!='b')&&(e!='h')
985 case 's': printf("Stop\r"); break;
986 case 'a': printf("Again\r"); break;
987 case 'd': printf("Delete\r"); break;
988 case 'm': printf("Move\r"); break;
989 case 'n': printf("Next\r"); break;
990 case 'p': printf("Print\r"); break;
991 case 'q': printf("Quote\r"); break;
992 case 'b': printf("Back\r"); break;
993 case 'h': printf("Header\r"); break;
994 case 'r': printf("Reply\r"); break;
996 if (userflags & US_DISAPPEAR)
1003 case 'p': fflush(stdout);
1004 freopen(prtfile,"w",stdout);
1007 case 'q': fflush(stdout);
1008 freopen(temp2,"w",stdout);
1012 case 'a': goto RAGAIN;
1013 case 'b': a=a-(rdir*2);
1015 case 'm': newprompt("Enter target room: ",targ,19);
1016 if (strlen(targ)>0) {
1017 sprintf(cmd,"MOVE %ld|%s",
1021 printf("%s\n",&cmd[4]);
1022 if (cmd[0]=='2') msg_arr[a]=0L;
1027 if (cmd[0]!='2') goto RMSGREAD;
1029 case 'd': printf("*** Delete this message? ");
1031 sprintf(cmd,"DELE %ld",msg_arr[a]);
1034 printf("%s\n",&cmd[4]);
1035 if (cmd[0]=='2') msg_arr[a]=0L;
1041 case 'h': read_message(msg_arr[a],READ_HEADER);
1043 case 'r': entmsg(1,(DEFAULT_ENTRY==46 ? 2 : 0));
1046 } /* end for loop */
1047 } /* end read routine */
1053 * View and edit a system message
1055 void edit_system_message(char *which_message)
1061 sprintf(desc, "system message '%s'", which_message);
1062 sprintf(read_cmd, "MESG %s", which_message);
1063 sprintf(write_cmd, "EMSG %s", which_message);
1064 do_edit(desc, read_cmd, "NOOP", write_cmd);