4 * front end for chat mode
5 * (the "single process" version - no more fork() anymore)
19 #if TIME_WITH_SYS_TIME
20 # include <sys/time.h>
24 # include <sys/time.h>
30 #include <sys/types.h>
31 #ifdef HAVE_SYS_SELECT_H
32 #include <sys/select.h>
36 #include "client_chat.h"
40 #include "citadel_decls.h"
49 #define MIN(a, b) ((a) < (b) ? (a) : (b))
51 extern struct CtdlServInfo serv_info;
53 void getline(char *, int);
55 char last_paged[SIZ] = "";
65 int send_complete_line;
66 int recv_complete_line;
78 scr_printf("%s\n", &buf[4]);
81 scr_printf("Entering chat mode (type /quit to exit, /help for other cmds)\n");
82 set_keepalives(KA_NO);
83 last_transmit = time(NULL);
90 send_complete_line = 0;
91 recv_complete_line = 0;
97 FD_SET(getsockfd(), &rfds);
98 tv.tv_sec = S_KEEPALIVE;
100 retval = select(getsockfd() + 1, &rfds, NULL, NULL, &tv);
102 if (FD_ISSET(getsockfd(), &rfds)) {
105 recv_complete_line = 1;
106 goto RCL; /* ugly, but we've gotta get out! */
108 buf[strlen(buf) + 1] = 0;
109 buf[strlen(buf)] = ch;
113 if (FD_ISSET(0, &rfds)) {
115 if ((ch == 10) || (ch == 13)) {
116 send_complete_line = 1;
117 } else if ((ch == 8) || (ch == 127)) {
118 if (strlen(wbuf) > 0) {
119 wbuf[strlen(wbuf) - 1] = 0;
120 sln_printf("%c %c", 8, 8);
124 wbuf[strlen(wbuf) + 1] = 0;
125 wbuf[strlen(wbuf)] = ch;
128 /* if the user hit return, send the line */
129 RCL:if (send_complete_line) {
131 last_transmit = time(NULL);
133 send_complete_line = 0;
135 /* if it's time to word wrap, send a partial line */
136 if (strlen(wbuf) >= (77 - strlen(fullname))) {
138 for (a = 0; a < strlen(wbuf); ++a) {
144 last_transmit = time(NULL);
146 send_complete_line = 0;
150 last_transmit = time(NULL);
151 strcpy(wbuf, &wbuf[pos + 1]);
154 if (recv_complete_line) {
155 sln_printf("\r%79s\r", "");
156 if (!strcmp(buf, "000")) {
158 sln_printf("\rExiting chat mode\n");
160 set_keepalives(KA_YES);
162 /* Some users complained about the client and server
163 * losing protocol synchronization when exiting chat.
164 * This little dialog forces everything to be
167 serv_puts("ECHO __ExitingChat__");
170 } while (strcmp(buf, "200 __ExitingChat__"));
175 if (num_parms(buf) >= 2) {
176 extract(c_user, buf, 0);
177 extract(c_text, buf, 1);
178 if (num_parms(buf) > 2) {
179 extract(c_room, buf, 2);
180 scr_printf("Got room %s\n", c_room);
182 if (strcasecmp(c_text, "NOOP")) {
183 if (!strcmp(c_user, fullname)) {
184 color(BRIGHT_YELLOW);
185 } else if (!strcmp(c_user, ":")) {
190 if (strcmp(c_user, last_user)) {
191 snprintf(buf, sizeof buf, "%s: %s", c_user, c_text);
193 size_t i = MIN(sizeof buf - 1,
197 safestrncpy(&buf[i], c_text,
200 while (strlen(buf) < 79)
202 if (strcmp(c_user, last_user)) {
203 sln_printf("\r%79s\n", "");
204 strcpy(last_user, c_user);
206 scr_printf("\r%s\n", buf);
210 color(BRIGHT_YELLOW);
211 sln_printf("\r> %s", wbuf);
213 recv_complete_line = 0;
217 /* If the user is sitting idle, send a half-keepalive to the
218 * server to prevent session timeout.
220 if ((time(NULL) - last_transmit) >= S_KEEPALIVE) {
222 last_transmit = time(NULL);
229 * send an express message
233 char buf[SIZ], touser[SIZ], msg[SIZ];
236 strcpy(touser, last_paged);
237 strprompt("Page who", touser, 30);
239 /* old server -- use inline paging */
240 if (serv_info.serv_paging_level == 0) {
241 newprompt("Message: ", msg, 69);
242 snprintf(buf, sizeof buf, "SEXP %s|%s", touser, msg);
245 if (!strncmp(buf, "200", 3)) {
246 strcpy(last_paged, touser);
248 scr_printf("%s\n", &buf[4]);
251 /* new server -- use extended paging */
252 else if (serv_info.serv_paging_level >= 1) {
253 snprintf(buf, sizeof buf, "SEXP %s||", touser);
257 scr_printf("%s\n", &buf[4]);
260 if (client_make_message(temp, touser, 0, 0, 0, NULL) != 0) {
261 scr_printf("No message sent.\n");
264 pagefp = fopen(temp, "r");
266 snprintf(buf, sizeof buf, "SEXP %s|-", touser);
270 strcpy(last_paged, touser);
271 while (fgets(buf, sizeof buf, pagefp) != NULL) {
272 buf[strlen(buf) - 1] = 0;
277 scr_printf("Message sent.\n");
279 scr_printf("%s\n", &buf[4]);
287 void quiet_mode(void)
295 scr_printf("%s\n", &buf[4]);
298 qstate = atoi(&buf[4]);
303 sprintf(buf, "DEXP %d", qstate);
307 scr_printf("%s\n", &buf[4]);
310 qstate = atoi(&buf[4]);
312 scr_printf("Quiet mode enabled (no other users may page you)\n");
314 scr_printf("Quiet mode disabled (other users may page you)\n");