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 "citadel_ipc.h"
37 #include "client_chat.h"
40 #include "citadel_decls.h"
49 #define MIN(a, b) ((a) < (b) ? (a) : (b))
52 void ctdl_getline(char *, int);
54 char last_paged[SIZ] = "";
56 void chatmode(CtdlIPC *ipc)
64 int send_complete_line;
65 int recv_complete_line;
74 CtdlIPC_chat_send(ipc, "CHAT");
75 CtdlIPC_chat_recv(ipc, buf);
77 scr_printf("%s\n", &buf[4]);
80 scr_printf("Entering chat mode "
81 "(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(CtdlIPC_getsockfd(ipc), &rfds);
98 tv.tv_sec = S_KEEPALIVE;
100 retval = select(CtdlIPC_getsockfd(ipc) + 1, &rfds,
103 /* If there's data from the server... */
104 if (FD_ISSET(CtdlIPC_getsockfd(ipc), &rfds)) {
105 CtdlIPC_chat_recv(ipc, buf);
106 recv_complete_line = 1;
107 goto RCL; /* ugly, but we've gotta get out! */
110 /* If there's data from the keyboard... */
111 if (FD_ISSET(0, &rfds)) {
112 ch = scr_getc(SCR_BLOCK);
113 if ((ch == 10) || (ch == 13)) {
114 send_complete_line = 1;
115 } else if ((ch == 8) || (ch == 127)) {
116 if (!IsEmptyStr(wbuf)) {
117 wbuf[strlen(wbuf) - 1] = 0;
118 sln_printf("%c %c", 8, 8);
122 wbuf[strlen(wbuf) + 1] = 0;
123 wbuf[strlen(wbuf)] = ch;
127 /* if the user hit return, send the line */
128 RCL: if (send_complete_line) {
129 CtdlIPC_chat_send(ipc, wbuf);
130 last_transmit = time(NULL);
132 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) {
143 CtdlIPC_chat_send(ipc, wbuf);
144 last_transmit = time(NULL);
146 send_complete_line = 0;
149 CtdlIPC_chat_send(ipc, wbuf);
150 last_transmit = time(NULL);
151 strcpy(wbuf, &wbuf[pos + 1]);
155 if (recv_complete_line) {
156 sln_printf("\r%79s\r", "");
157 if (!strcmp(buf, "000")) {
159 sln_printf("\rExiting chat mode\n");
161 set_keepalives(KA_YES);
163 /* Some users complained about the client and
164 * server losing protocol synchronization when
165 * exiting chat. This little dialog forces
166 * everything to be hunky-dory.
168 CtdlIPC_chat_send(ipc, "ECHO __ExitingChat__");
170 CtdlIPC_chat_recv(ipc, buf);
171 } while (strcmp(buf, "200 __ExitingChat__"));
174 if (num_parms(buf) >= 2) {
175 extract_token(c_user, buf, 0, '|', sizeof c_user);
176 extract_token(c_text, buf, 1, '|', sizeof c_text);
177 if (num_parms(buf) > 2) {
178 extract_token(c_room, buf, 2, '|', sizeof c_room);
179 scr_printf("Got room %s\n", c_room);
181 if (strcasecmp(c_text, "NOOP")) {
182 if (!strcmp(c_user, fullname)) {
183 color(BRIGHT_YELLOW);
184 } else if (!strcmp(c_user, ":")) {
189 if (strcmp(c_user, last_user)) {
190 snprintf(buf, sizeof buf, "%s: %s", c_user, c_text);
192 size_t i = MIN(sizeof buf - 1,
196 safestrncpy(&buf[i], c_text,
199 while (strlen(buf) < 79)
201 if (strcmp(c_user, last_user)) {
202 sln_printf("\r%79s\n", "");
203 strcpy(last_user, c_user);
205 scr_printf("\r%s\n", buf);
209 color(BRIGHT_YELLOW);
210 sln_printf("\r> %s", wbuf);
212 recv_complete_line = 0;
216 /* If the user is sitting idle, send a half-keepalive to the
217 * server to prevent session timeout.
219 if ((time(NULL) - last_transmit) >= S_KEEPALIVE) {
220 CtdlIPC_chat_send(ipc, "NOOP");
221 last_transmit = time(NULL);
228 * send an instant message
230 void page_user(CtdlIPC *ipc)
232 char buf[SIZ], touser[SIZ], msg[SIZ];
235 strcpy(touser, last_paged);
236 strprompt("Page who", touser, 30);
238 /* old server -- use inline paging */
239 if (ipc->ServInfo.paging_level == 0) {
240 newprompt("Message: ", msg, 69);
241 snprintf(buf, sizeof buf, "SEXP %s|%s", touser, msg);
242 CtdlIPC_chat_send(ipc, buf);
243 CtdlIPC_chat_recv(ipc, buf);
244 if (!strncmp(buf, "200", 3)) {
245 strcpy(last_paged, touser);
247 scr_printf("%s\n", &buf[4]);
250 /* new server -- use extended paging */
251 else if (ipc->ServInfo.paging_level >= 1) {
252 snprintf(buf, sizeof buf, "SEXP %s||", touser);
253 CtdlIPC_chat_send(ipc, buf);
254 CtdlIPC_chat_recv(ipc, buf);
256 scr_printf("%s\n", &buf[4]);
259 if (client_make_message(ipc, temp, touser, 0, 0, 0, NULL, 0) != 0) {
260 scr_printf("No message sent.\n");
263 pagefp = fopen(temp, "r");
265 snprintf(buf, sizeof buf, "SEXP %s|-", touser);
266 CtdlIPC_chat_send(ipc, buf);
267 CtdlIPC_chat_recv(ipc, buf);
269 strcpy(last_paged, touser);
270 while (fgets(buf, sizeof buf, pagefp) != NULL) {
271 buf[strlen(buf) - 1] = 0;
272 CtdlIPC_chat_send(ipc, buf);
275 CtdlIPC_chat_send(ipc, "000");
276 scr_printf("Message sent.\n");
278 scr_printf("%s\n", &buf[4]);
284 void quiet_mode(CtdlIPC *ipc)
286 static int quiet = 0;
290 r = CtdlIPCEnableInstantMessageReceipt(ipc, !quiet, cret);
293 scr_printf("Quiet mode %sabled (%sother users may page you)\n",
294 (quiet) ? "en" : "dis",
295 (quiet) ? "no " : "");
297 scr_printf("Unable to change quiet mode: %s\n", cret);
302 void stealth_mode(CtdlIPC *ipc)
304 static int stealth = 0;
308 r = CtdlIPCStealthMode(ipc, !stealth, cret);
311 scr_printf("Stealth mode %sabled (you are %s)\n",
312 (stealth) ? "en" : "dis",
313 (stealth) ? "invisible" : "listed as online");
315 scr_printf("Unable to change stealth mode: %s\n", cret);