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);
113 scr_printf("Server gone Exiting chat mode\n");
118 /* If there's data from the keyboard... */
119 if (FD_ISSET(0, &rfds)) {
120 ch = scr_getc(SCR_BLOCK);
121 if ((ch == 10) || (ch == 13)) {
122 send_complete_line = 1;
123 } else if ((ch == 8) || (ch == 127)) {
124 if (!IsEmptyStr(wbuf)) {
125 wbuf[strlen(wbuf) - 1] = 0;
126 scr_printf("%c %c", 8, 8);
130 wbuf[strlen(wbuf) + 1] = 0;
131 wbuf[strlen(wbuf)] = ch;
135 /* if the user hit return, send the line */
136 if (send_complete_line) {
138 if (!strcasecmp(wbuf, "/quit")) {
139 CtdlIPC_chat_send(ipc, "RCHT exit");
140 CtdlIPC_chat_recv(ipc, response); /* don't care about the result */
142 scr_printf("\rExiting chat mode\n");
147 CtdlIPC_chat_send(ipc, "RCHT send");
148 CtdlIPC_chat_recv(ipc, response);
149 if (response[0] == '4') {
150 CtdlIPC_chat_send(ipc, wbuf);
151 CtdlIPC_chat_send(ipc, "000");
154 send_complete_line = 0;
157 /* if it's time to word wrap, send a partial line */
158 if (strlen(wbuf) >= (77 - strlen(fullname))) {
160 for (a = 0; !IsEmptyStr(&wbuf[a]); ++a) {
165 CtdlIPC_chat_send(ipc, "RCHT send");
166 CtdlIPC_chat_recv(ipc, response);
167 if (response[0] == '4') {
168 CtdlIPC_chat_send(ipc, wbuf);
169 CtdlIPC_chat_send(ipc, "000");
172 send_complete_line = 0;
175 CtdlIPC_chat_send(ipc, "RCHT send");
176 CtdlIPC_chat_recv(ipc, response);
177 if (response[0] == '4') {
178 CtdlIPC_chat_send(ipc, wbuf);
179 CtdlIPC_chat_send(ipc, "000");
181 strcpy(wbuf, &wbuf[pos + 1]);
185 /* poll for incoming chat messages */
186 snprintf(buf, sizeof buf, "RCHT poll|%d", seq);
187 CtdlIPC_chat_send(ipc, buf);
188 CtdlIPC_chat_recv(ipc, response);
190 if (response[0] == '1') {
191 seq = extract_int(&response[4], 0);
192 extract_token(c_user, &response[4], 2, '|', sizeof c_user);
193 while (CtdlIPC_chat_recv(ipc, c_text), strcmp(c_text, "000")) {
194 scr_printf("\r%79s\r", "");
195 if (!strcmp(c_user, fullname)) {
196 color(BRIGHT_YELLOW);
197 } else if (!strcmp(c_user, ":")) {
202 if (strcmp(c_user, last_user)) {
203 snprintf(buf, sizeof buf, "%s: %s", c_user, c_text);
205 size_t i = MIN(sizeof buf - 1, strlen(c_user) + 2);
207 safestrncpy(&buf[i], c_text, sizeof buf - i);
209 while (strlen(buf) < 79) {
212 if (strcmp(c_user, last_user)) {
213 scr_printf("\r%79s\n", "");
214 strcpy(last_user, c_user);
216 scr_printf("\r%s\n", buf);
220 color(BRIGHT_YELLOW);
221 scr_printf("\r> %s", wbuf);
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);