2 * front end for chat mode
3 * (the "single process" version - no more fork() anymore)
5 * Copyright (c) 1987-2010 by the citadel.org team
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #if TIME_WITH_SYS_TIME
33 # include <sys/time.h>
37 # include <sys/time.h>
43 #include <sys/types.h>
44 #ifdef HAVE_SYS_SELECT_H
45 #include <sys/select.h>
48 #include <libcitadel.h>
50 #include "citadel_ipc.h"
51 #include "client_chat.h"
54 #include "citadel_decls.h"
62 #define MIN(a, b) ((a) < (b) ? (a) : (b))
65 void ctdl_getline(char *, int);
68 char last_paged[SIZ] = "";
70 void chatmode(CtdlIPC *ipc)
78 int send_complete_line;
87 CtdlIPC_chat_send(ipc, "RCHT enter");
88 CtdlIPC_chat_recv(ipc, buf);
90 scr_printf("%s\n", &buf[4]);
93 scr_printf("Entering chat mode (type /quit to exit)\n");
97 strcpy(last_user, "");
101 send_complete_line = 0;
109 retval = select(1, &rfds, NULL, NULL, &tv);
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 scr_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 if (send_complete_line) {
131 if (!strcasecmp(wbuf, "/quit")) {
132 CtdlIPC_chat_send(ipc, "RCHT exit");
133 CtdlIPC_chat_recv(ipc, response); /* don't care about the result */
135 scr_printf("\rExiting chat mode\n");
140 CtdlIPC_chat_send(ipc, "RCHT send");
141 CtdlIPC_chat_recv(ipc, response);
142 if (response[0] == '4') {
143 CtdlIPC_chat_send(ipc, wbuf);
144 CtdlIPC_chat_send(ipc, "000");
147 send_complete_line = 0;
150 /* if it's time to word wrap, send a partial line */
151 if (strlen(wbuf) >= (77 - strlen(fullname))) {
153 for (a = 0; !IsEmptyStr(&wbuf[a]); ++a) {
158 CtdlIPC_chat_send(ipc, "RCHT send");
159 CtdlIPC_chat_recv(ipc, response);
160 if (response[0] == '4') {
161 CtdlIPC_chat_send(ipc, wbuf);
162 CtdlIPC_chat_send(ipc, "000");
165 send_complete_line = 0;
168 CtdlIPC_chat_send(ipc, "RCHT send");
169 CtdlIPC_chat_recv(ipc, response);
170 if (response[0] == '4') {
171 CtdlIPC_chat_send(ipc, wbuf);
172 CtdlIPC_chat_send(ipc, "000");
174 strcpy(wbuf, &wbuf[pos + 1]);
178 /* poll for incoming chat messages */
179 snprintf(buf, sizeof buf, "RCHT poll|%d", seq);
180 CtdlIPC_chat_send(ipc, buf);
181 CtdlIPC_chat_recv(ipc, response);
183 if (response[0] == '1') {
184 seq = extract_int(&response[4], 0);
185 extract_token(c_user, &response[4], 2, '|', sizeof c_user);
186 while (CtdlIPC_chat_recv(ipc, c_text), strcmp(c_text, "000")) {
187 scr_printf("\r%79s\r", "");
188 if (!strcmp(c_user, fullname)) {
189 color(BRIGHT_YELLOW);
190 } else if (!strcmp(c_user, ":")) {
195 if (strcmp(c_user, last_user)) {
196 snprintf(buf, sizeof buf, "%s: %s", c_user, c_text);
198 size_t i = MIN(sizeof buf - 1, strlen(c_user) + 2);
200 safestrncpy(&buf[i], c_text, sizeof buf - i);
202 while (strlen(buf) < 79) {
205 if (strcmp(c_user, last_user)) {
206 scr_printf("\r%79s\n", "");
207 strcpy(last_user, c_user);
209 scr_printf("\r%s\n", buf);
213 color(BRIGHT_YELLOW);
214 scr_printf("\r> %s", wbuf);
222 * send an instant message
224 void page_user(CtdlIPC *ipc)
226 char buf[SIZ], touser[SIZ], msg[SIZ];
229 strcpy(touser, last_paged);
230 strprompt("Page who", touser, 30);
232 /* old server -- use inline paging */
233 if (ipc->ServInfo.paging_level == 0) {
234 newprompt("Message: ", msg, 69);
235 snprintf(buf, sizeof buf, "SEXP %s|%s", touser, msg);
236 CtdlIPC_chat_send(ipc, buf);
237 CtdlIPC_chat_recv(ipc, buf);
238 if (!strncmp(buf, "200", 3)) {
239 strcpy(last_paged, touser);
241 scr_printf("%s\n", &buf[4]);
244 /* new server -- use extended paging */
245 else if (ipc->ServInfo.paging_level >= 1) {
246 snprintf(buf, sizeof buf, "SEXP %s||", touser);
247 CtdlIPC_chat_send(ipc, buf);
248 CtdlIPC_chat_recv(ipc, buf);
250 scr_printf("%s\n", &buf[4]);
253 if (client_make_message(ipc, temp, touser, 0, 0, 0, NULL, 0) != 0) {
254 scr_printf("No message sent.\n");
257 pagefp = fopen(temp, "r");
259 snprintf(buf, sizeof buf, "SEXP %s|-", touser);
260 CtdlIPC_chat_send(ipc, buf);
261 CtdlIPC_chat_recv(ipc, buf);
263 strcpy(last_paged, touser);
264 while (fgets(buf, sizeof buf, pagefp) != NULL) {
265 buf[strlen(buf) - 1] = 0;
266 CtdlIPC_chat_send(ipc, buf);
269 CtdlIPC_chat_send(ipc, "000");
270 scr_printf("Message sent.\n");
272 scr_printf("%s\n", &buf[4]);
278 void quiet_mode(CtdlIPC *ipc)
280 static int quiet = 0;
284 r = CtdlIPCEnableInstantMessageReceipt(ipc, !quiet, cret);
287 scr_printf("Quiet mode %sabled (%sother users may page you)\n",
288 (quiet) ? "en" : "dis",
289 (quiet) ? "no " : "");
291 scr_printf("Unable to change quiet mode: %s\n", cret);
296 void stealth_mode(CtdlIPC *ipc)
298 static int stealth = 0;
302 r = CtdlIPCStealthMode(ipc, !stealth, cret);
305 scr_printf("Stealth mode %sabled (you are %s)\n",
306 (stealth) ? "en" : "dis",
307 (stealth) ? "invisible" : "listed as online");
309 scr_printf("Unable to change stealth mode: %s\n", cret);