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))
35 extern struct CtdlServInfo serv_info;
37 void citedit(FILE *fp, long int base_pos);
46 int send_complete_line;
47 int recv_complete_line;
58 printf("%s\n",&buf[4]);
62 printf("Entering chat mode (type /quit to exit, /help for other cmds)\n");
63 set_keepalives(KA_CHAT);
69 send_complete_line = 0;
70 recv_complete_line = 0;
76 FD_SET(getsockfd(),&rfds);
77 tv.tv_sec = S_KEEPALIVE;
79 retval = select(getsockfd()+1, &rfds, NULL, NULL, &tv);
81 if (FD_ISSET(getsockfd(), &rfds)) {
84 recv_complete_line = 1;
85 goto RCL; /* ugly, but we've gotta get out! */
88 buf[strlen(buf) + 1] = 0;
89 buf[strlen(buf)] = ch;
94 if (FD_ISSET(0, &rfds)) {
96 if ((ch == 10) || (ch == 13)) {
97 send_complete_line = 1;
99 else if ((ch == 8) || (ch == 127)) {
100 if (strlen(wbuf) > 0) {
101 wbuf[strlen(wbuf)-1] = 0;
107 wbuf[strlen(wbuf) + 1] = 0;
108 wbuf[strlen(wbuf)] = ch;
113 /* if the user hit return, send the line */
114 RCL: if (send_complete_line) {
117 send_complete_line = 0;
120 /* if it's time to word wrap, send a partial line */
121 if ( strlen(wbuf) >= (77-strlen(fullname)) ) {
123 for (a=0; a<strlen(wbuf); ++a) {
124 if (wbuf[a] == 32) pos = a;
129 send_complete_line = 0;
134 strcpy(wbuf,&wbuf[pos+1]);
138 if (recv_complete_line) {
139 printf("\r%79s\r","");
140 if (!strcmp(buf,"000")) {
142 printf("Exiting chat mode\n");
145 set_keepalives(KA_YES);
148 if (num_parms(buf)>=2) {
149 extract(c_user,buf,0);
150 extract(c_text,buf,1);
151 if (num_parms(buf)>2)
153 extract(c_room,buf,2);
154 printf("Got room %s\n", c_room);
157 if (strucmp(c_text,"NOOP")) {
158 if (!strcmp(c_user, fullname)) {
161 else if (!strcmp(c_user,":")) {
167 if (strcmp(c_user,last_user)) {
168 snprintf(buf,sizeof buf,"%s: %s",c_user,c_text);
171 size_t i = MIN(sizeof buf - 1,
175 safestrncpy(&buf[i], c_text,
178 while (strlen(buf)<79) strcat(buf," ");
179 if (strcmp(c_user,last_user)) {
180 printf("\r%79s\n","");
181 strcpy(last_user,c_user);
183 printf("\r%s\n",buf);
189 recv_complete_line = 0;
196 * send an express message
199 static char last_paged[32] = "";
200 char buf[256], touser[256], msg[256];
203 strcpy(touser, last_paged);
204 strprompt("Page who", touser, 30);
206 /* old server -- use inline paging */
207 if (serv_info.serv_paging_level == 0) {
208 newprompt("Message: ",msg,69);
209 snprintf(buf,sizeof buf,"SEXP %s|%s",touser,msg);
212 if (!strncmp(buf, "200", 3)) {
213 strcpy(last_paged, touser);
215 printf("%s\n", &buf[4]);
219 /* new server -- use extended paging */
220 else if (serv_info.serv_paging_level >= 1) {
221 snprintf(buf, sizeof buf, "SEXP %s||", touser);
225 printf("%s\n", &buf[4]);
228 fp = fopen(temp, "w");
229 fp = freopen(temp, "rb+", fp);
230 if (fp==NULL) printf("Error: %s\n", strerror(errno));
232 printf("Type message to send. Enter a blank line when finished.\n");
234 snprintf(buf, sizeof buf, "SEXP %s|-", touser);
238 strcpy(last_paged, touser);
240 while (fgets(buf, sizeof buf, fp) != NULL) {
241 buf[strlen(buf)-1] = 0;
242 if (strcmp(buf, "000")) serv_puts(buf);
246 printf("Message sent.\n");
249 printf("%s\n", &buf[4]);