4 * client_chat.c -- front end for chat mode
5 * (the "single process" version - no more fork() anymore)
21 #include <sys/types.h>
22 #ifdef HAVE_SYS_SELECT_H
23 #include <sys/select.h>
26 #include "client_chat.h"
30 #include "citadel_decls.h"
33 #define MIN(a, b) ((a) < (b) ? (a) : (b))
42 int send_complete_line;
43 int recv_complete_line;
54 printf("%s\n",&buf[4]);
58 printf("Entering chat mode (type /quit to exit, /help for other cmds)\n");
59 set_keepalives(KA_CHAT);
65 send_complete_line = 0;
66 recv_complete_line = 0;
72 FD_SET(getsockfd(),&rfds);
73 tv.tv_sec = S_KEEPALIVE;
75 retval = select(getsockfd()+1, &rfds, NULL, NULL, &tv);
77 if (FD_ISSET(getsockfd(), &rfds)) {
80 recv_complete_line = 1;
81 goto RCL; /* ugly, but we've gotta get out! */
84 buf[strlen(buf) + 1] = 0;
85 buf[strlen(buf)] = ch;
90 if (FD_ISSET(0, &rfds)) {
92 if ((ch == 10) || (ch == 13)) {
93 send_complete_line = 1;
95 else if ((ch == 8) || (ch == 127)) {
96 if (strlen(wbuf) > 0) {
97 wbuf[strlen(wbuf)-1] = 0;
103 wbuf[strlen(wbuf) + 1] = 0;
104 wbuf[strlen(wbuf)] = ch;
109 /* if the user hit return, send the line */
110 RCL: if (send_complete_line) {
113 send_complete_line = 0;
116 /* if it's time to word wrap, send a partial line */
117 if ( strlen(wbuf) >= (77-strlen(fullname)) ) {
119 for (a=0; a<strlen(wbuf); ++a) {
120 if (wbuf[a] == 32) pos = a;
125 send_complete_line = 0;
130 strcpy(wbuf,&wbuf[pos+1]);
134 if (recv_complete_line) {
135 printf("\r%79s\r","");
136 if (!strcmp(buf,"000")) {
138 printf("Exiting chat mode\n");
141 set_keepalives(KA_YES);
144 if (num_parms(buf)>=2) {
145 extract(c_user,buf,0);
146 extract(c_text,buf,1);
147 if (num_parms(buf)>2)
149 extract(c_room,buf,2);
150 printf("Got room %s\n", c_room);
153 if (strucmp(c_text,"NOOP")) {
154 if (!strcmp(c_user, fullname)) {
157 else if (!strcmp(c_user,":")) {
163 if (strcmp(c_user,last_user)) {
164 snprintf(buf,sizeof buf,"%s: %s",c_user,c_text);
167 size_t i = MIN(sizeof buf - 1,
171 safestrncpy(&buf[i], c_text,
174 while (strlen(buf)<79) strcat(buf," ");
175 if (strcmp(c_user,last_user)) {
176 printf("\r%79s\n","");
177 strcpy(last_user,c_user);
179 printf("\r%s\n",buf);
185 recv_complete_line = 0;