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))
51 extern struct CtdlServInfo serv_info;
53 void getline(char *, int);
55 char last_paged[SIZ] = "";
57 void chatmode(CtdlIPC *ipc)
65 int send_complete_line;
66 int recv_complete_line;
75 CtdlIPC_putline(ipc, "CHAT");
76 CtdlIPC_getline(ipc, buf);
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(CtdlIPC_getsockfd(ipc), &rfds);
98 tv.tv_sec = S_KEEPALIVE;
100 retval = select(CtdlIPC_getsockfd(ipc) + 1, &rfds, NULL, NULL, &tv);
102 if (FD_ISSET(CtdlIPC_getsockfd(ipc), &rfds)) {
103 ch = CtdlIPC_get(ipc);
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)) {
114 ch = scr_getc(SCR_BLOCK);
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) {
130 CtdlIPC_putline(ipc, wbuf);
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) {
143 CtdlIPC_putline(ipc, wbuf);
144 last_transmit = time(NULL);
146 send_complete_line = 0;
149 CtdlIPC_putline(ipc, wbuf);
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 CtdlIPC_putline(ipc, "ECHO __ExitingChat__");
169 CtdlIPC_getline(ipc, buf);
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) {
221 CtdlIPC_putline(ipc, "NOOP");
222 last_transmit = time(NULL);
229 * send an express 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 (serv_info.serv_paging_level == 0) {
241 newprompt("Message: ", msg, 69);
242 snprintf(buf, sizeof buf, "SEXP %s|%s", touser, msg);
243 CtdlIPC_putline(ipc, buf);
244 CtdlIPC_getline(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 (serv_info.serv_paging_level >= 1) {
253 snprintf(buf, sizeof buf, "SEXP %s||", touser);
254 CtdlIPC_putline(ipc, buf);
255 CtdlIPC_getline(ipc, buf);
257 scr_printf("%s\n", &buf[4]);
260 if (client_make_message(ipc, 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);
267 CtdlIPC_putline(ipc, buf);
268 CtdlIPC_getline(ipc, buf);
270 strcpy(last_paged, touser);
271 while (fgets(buf, sizeof buf, pagefp) != NULL) {
272 buf[strlen(buf) - 1] = 0;
273 CtdlIPC_putline(ipc, buf);
276 CtdlIPC_putline(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);