4 * front end for chat mode
5 * (the "single process" version - no more fork() anymore)
7 * Copyright (c) 1987-2010 by the citadel.org team
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 #if TIME_WITH_SYS_TIME
35 # include <sys/time.h>
39 # include <sys/time.h>
45 #include <sys/types.h>
46 #ifdef HAVE_SYS_SELECT_H
47 #include <sys/select.h>
50 #include <libcitadel.h>
52 #include "citadel_ipc.h"
53 #include "client_chat.h"
56 #include "citadel_decls.h"
64 #define MIN(a, b) ((a) < (b) ? (a) : (b))
67 void ctdl_getline(char *, int);
70 char last_paged[SIZ] = "";
72 void chatmode(CtdlIPC *ipc)
80 int send_complete_line;
89 CtdlIPC_chat_send(ipc, "RCHT enter");
90 CtdlIPC_chat_recv(ipc, buf);
92 scr_printf("%s\n", &buf[4]);
95 scr_printf("Entering chat mode (type /quit to exit)\n");
99 strcpy(last_user, "");
100 color(BRIGHT_YELLOW);
103 send_complete_line = 0;
111 retval = select(1, &rfds, NULL, NULL, &tv);
113 /* If there's data from the keyboard... */
114 if (FD_ISSET(0, &rfds)) {
115 ch = scr_getc(SCR_BLOCK);
116 if ((ch == 10) || (ch == 13)) {
117 send_complete_line = 1;
118 } else if ((ch == 8) || (ch == 127)) {
119 if (!IsEmptyStr(wbuf)) {
120 wbuf[strlen(wbuf) - 1] = 0;
121 sln_printf("%c %c", 8, 8);
125 wbuf[strlen(wbuf) + 1] = 0;
126 wbuf[strlen(wbuf)] = ch;
130 /* if the user hit return, send the line */
131 if (send_complete_line) {
133 if (!strcasecmp(wbuf, "/quit")) {
134 CtdlIPC_chat_send(ipc, "RCHT exit");
135 CtdlIPC_chat_recv(ipc, response); /* don't care about the result */
137 sln_printf("\rExiting chat mode\n");
142 CtdlIPC_chat_send(ipc, "RCHT send");
143 CtdlIPC_chat_recv(ipc, response);
144 if (response[0] == '4') {
145 CtdlIPC_chat_send(ipc, wbuf);
146 CtdlIPC_chat_send(ipc, "000");
149 send_complete_line = 0;
152 /* if it's time to word wrap, send a partial line */
153 if (strlen(wbuf) >= (77 - strlen(fullname))) {
155 for (a = 0; !IsEmptyStr(&wbuf[a]); ++a) {
160 CtdlIPC_chat_send(ipc, "RCHT send");
161 CtdlIPC_chat_recv(ipc, response);
162 if (response[0] == '4') {
163 CtdlIPC_chat_send(ipc, wbuf);
164 CtdlIPC_chat_send(ipc, "000");
167 send_complete_line = 0;
170 CtdlIPC_chat_send(ipc, "RCHT send");
171 CtdlIPC_chat_recv(ipc, response);
172 if (response[0] == '4') {
173 CtdlIPC_chat_send(ipc, wbuf);
174 CtdlIPC_chat_send(ipc, "000");
176 strcpy(wbuf, &wbuf[pos + 1]);
180 /* poll for incoming chat messages */
181 snprintf(buf, sizeof buf, "RCHT poll|%d", seq);
182 CtdlIPC_chat_send(ipc, buf);
183 CtdlIPC_chat_recv(ipc, response);
185 if (response[0] == '1') {
186 seq = extract_int(&response[4], 0);
187 extract_token(c_user, &response[4], 2, '|', sizeof c_user);
188 while (CtdlIPC_chat_recv(ipc, c_text), strcmp(c_text, "000")) {
189 sln_printf("\r%79s\r", "");
190 if (!strcmp(c_user, fullname)) {
191 color(BRIGHT_YELLOW);
192 } else if (!strcmp(c_user, ":")) {
197 if (strcmp(c_user, last_user)) {
198 snprintf(buf, sizeof buf, "%s: %s", c_user, c_text);
200 size_t i = MIN(sizeof buf - 1, strlen(c_user) + 2);
202 safestrncpy(&buf[i], c_text, sizeof buf - i);
204 while (strlen(buf) < 79) {
207 if (strcmp(c_user, last_user)) {
208 sln_printf("\r%79s\n", "");
209 strcpy(last_user, c_user);
211 scr_printf("\r%s\n", buf);
215 color(BRIGHT_YELLOW);
216 sln_printf("\r> %s", wbuf);
224 * send an instant message
226 void page_user(CtdlIPC *ipc)
228 char buf[SIZ], touser[SIZ], msg[SIZ];
231 strcpy(touser, last_paged);
232 strprompt("Page who", touser, 30);
234 /* old server -- use inline paging */
235 if (ipc->ServInfo.paging_level == 0) {
236 newprompt("Message: ", msg, 69);
237 snprintf(buf, sizeof buf, "SEXP %s|%s", touser, msg);
238 CtdlIPC_chat_send(ipc, buf);
239 CtdlIPC_chat_recv(ipc, buf);
240 if (!strncmp(buf, "200", 3)) {
241 strcpy(last_paged, touser);
243 scr_printf("%s\n", &buf[4]);
246 /* new server -- use extended paging */
247 else if (ipc->ServInfo.paging_level >= 1) {
248 snprintf(buf, sizeof buf, "SEXP %s||", touser);
249 CtdlIPC_chat_send(ipc, buf);
250 CtdlIPC_chat_recv(ipc, buf);
252 scr_printf("%s\n", &buf[4]);
255 if (client_make_message(ipc, temp, touser, 0, 0, 0, NULL, 0) != 0) {
256 scr_printf("No message sent.\n");
259 pagefp = fopen(temp, "r");
261 snprintf(buf, sizeof buf, "SEXP %s|-", touser);
262 CtdlIPC_chat_send(ipc, buf);
263 CtdlIPC_chat_recv(ipc, buf);
265 strcpy(last_paged, touser);
266 while (fgets(buf, sizeof buf, pagefp) != NULL) {
267 buf[strlen(buf) - 1] = 0;
268 CtdlIPC_chat_send(ipc, buf);
271 CtdlIPC_chat_send(ipc, "000");
272 scr_printf("Message sent.\n");
274 scr_printf("%s\n", &buf[4]);
280 void quiet_mode(CtdlIPC *ipc)
282 static int quiet = 0;
286 r = CtdlIPCEnableInstantMessageReceipt(ipc, !quiet, cret);
289 scr_printf("Quiet mode %sabled (%sother users may page you)\n",
290 (quiet) ? "en" : "dis",
291 (quiet) ? "no " : "");
293 scr_printf("Unable to change quiet mode: %s\n", cret);
298 void stealth_mode(CtdlIPC *ipc)
300 static int stealth = 0;
304 r = CtdlIPCStealthMode(ipc, !stealth, cret);
307 scr_printf("Stealth mode %sabled (you are %s)\n",
308 (stealth) ? "en" : "dis",
309 (stealth) ? "invisible" : "listed as online");
311 scr_printf("Unable to change stealth mode: %s\n", cret);