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>
35 #include <libcitadel.h>
37 #include "citadel_ipc.h"
38 #include "client_chat.h"
41 #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);
87 strcpy(last_user, "");
91 send_complete_line = 0;
92 recv_complete_line = 0;
98 FD_SET(CtdlIPC_getsockfd(ipc), &rfds);
99 tv.tv_sec = S_KEEPALIVE;
101 retval = select(CtdlIPC_getsockfd(ipc) + 1, &rfds,
104 /* If there's data from the server... */
105 if (FD_ISSET(CtdlIPC_getsockfd(ipc), &rfds)) {
106 CtdlIPC_chat_recv(ipc, buf);
107 recv_complete_line = 1;
108 goto RCL; /* ugly, but we've gotta get out! */
111 /* If there's data from the keyboard... */
112 if (FD_ISSET(0, &rfds)) {
113 ch = scr_getc(SCR_BLOCK);
114 if ((ch == 10) || (ch == 13)) {
115 send_complete_line = 1;
116 } else if ((ch == 8) || (ch == 127)) {
117 if (!IsEmptyStr(wbuf)) {
118 wbuf[strlen(wbuf) - 1] = 0;
119 sln_printf("%c %c", 8, 8);
123 wbuf[strlen(wbuf) + 1] = 0;
124 wbuf[strlen(wbuf)] = ch;
128 /* if the user hit return, send the line */
129 RCL: if (send_complete_line) {
130 CtdlIPC_chat_send(ipc, wbuf);
131 last_transmit = time(NULL);
133 send_complete_line = 0;
136 /* if it's time to word wrap, send a partial line */
137 if (strlen(wbuf) >= (77 - strlen(fullname))) {
139 for (a = 0; !IsEmptyStr(&wbuf[a]); ++a) {
144 CtdlIPC_chat_send(ipc, wbuf);
145 last_transmit = time(NULL);
147 send_complete_line = 0;
150 CtdlIPC_chat_send(ipc, wbuf);
151 last_transmit = time(NULL);
152 strcpy(wbuf, &wbuf[pos + 1]);
156 if (recv_complete_line) {
157 sln_printf("\r%79s\r", "");
158 if (!strcmp(buf, "000")) {
160 sln_printf("\rExiting chat mode\n");
162 set_keepalives(KA_YES);
164 /* Some users complained about the client and
165 * server losing protocol synchronization when
166 * exiting chat. This little dialog forces
167 * everything to be hunky-dory.
169 CtdlIPC_chat_send(ipc, "ECHO __ExitingChat__");
171 CtdlIPC_chat_recv(ipc, buf);
172 } while (strcmp(buf, "200 __ExitingChat__"));
175 if (num_parms(buf) >= 2) {
176 extract_token(c_user, buf, 0, '|', sizeof c_user);
177 extract_token(c_text, buf, 1, '|', sizeof c_text);
178 if (num_parms(buf) > 2) {
179 extract_token(c_room, buf, 2, '|', sizeof c_room);
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) {
221 CtdlIPC_chat_send(ipc, "NOOP");
222 last_transmit = time(NULL);
229 * send an instant message
231 void page_user(CtdlIPC *ipc)
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 (ipc->ServInfo.paging_level == 0) {
241 newprompt("Message: ", msg, 69);
242 snprintf(buf, sizeof buf, "SEXP %s|%s", touser, msg);
243 CtdlIPC_chat_send(ipc, buf);
244 CtdlIPC_chat_recv(ipc, buf);
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 (ipc->ServInfo.paging_level >= 1) {
253 snprintf(buf, sizeof buf, "SEXP %s||", touser);
254 CtdlIPC_chat_send(ipc, buf);
255 CtdlIPC_chat_recv(ipc, buf);
257 scr_printf("%s\n", &buf[4]);
260 if (client_make_message(ipc, temp, touser, 0, 0, 0, NULL, 0) != 0) {
261 scr_printf("No message sent.\n");
264 pagefp = fopen(temp, "r");
266 snprintf(buf, sizeof buf, "SEXP %s|-", touser);
267 CtdlIPC_chat_send(ipc, buf);
268 CtdlIPC_chat_recv(ipc, buf);
270 strcpy(last_paged, touser);
271 while (fgets(buf, sizeof buf, pagefp) != NULL) {
272 buf[strlen(buf) - 1] = 0;
273 CtdlIPC_chat_send(ipc, buf);
276 CtdlIPC_chat_send(ipc, "000");
277 scr_printf("Message sent.\n");
279 scr_printf("%s\n", &buf[4]);
285 void quiet_mode(CtdlIPC *ipc)
287 static int quiet = 0;
291 r = CtdlIPCEnableInstantMessageReceipt(ipc, !quiet, cret);
294 scr_printf("Quiet mode %sabled (%sother users may page you)\n",
295 (quiet) ? "en" : "dis",
296 (quiet) ? "no " : "");
298 scr_printf("Unable to change quiet mode: %s\n", cret);
303 void stealth_mode(CtdlIPC *ipc)
305 static int stealth = 0;
309 r = CtdlIPCStealthMode(ipc, !stealth, cret);
312 scr_printf("Stealth mode %sabled (you are %s)\n",
313 (stealth) ? "en" : "dis",
314 (stealth) ? "invisible" : "listed as online");
316 scr_printf("Unable to change stealth mode: %s\n", cret);