2 * front end for multiuser chat
4 * Copyright (c) 1987-2012 by the citadel.org team
6 * This program is open source software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 3.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
25 #if TIME_WITH_SYS_TIME
26 # include <sys/time.h>
30 # include <sys/time.h>
36 #include <sys/types.h>
37 #ifdef HAVE_SYS_SELECT_H
38 #include <sys/select.h>
41 #include <libcitadel.h>
42 //#include "citadel.h"
43 #include "citadel_ipc.h"
44 #include "client_chat.h"
47 #include "citadel_decls.h"
50 //#ifndef HAVE_SNPRINTF
51 //#include "snprintf.h"
55 #define MIN(a, b) ((a) < (b) ? (a) : (b))
58 char last_paged[SIZ] = "";
60 void chatmode(CtdlIPC *ipc)
68 int send_complete_line;
77 CtdlIPC_chat_send(ipc, "RCHT enter");
78 CtdlIPC_chat_recv(ipc, buf);
80 scr_printf("%s\n", &buf[4]);
83 scr_printf("Entering chat mode (type /quit to exit)\n");
87 strcpy(last_user, "");
91 send_complete_line = 0;
99 retval = select(1, &rfds, NULL, NULL, &tv);
103 scr_printf("Server gone Exiting chat mode\n");
108 /* If there's data from the keyboard... */
109 if (FD_ISSET(0, &rfds)) {
110 ch = scr_getc(SCR_BLOCK);
111 if ((ch == 10) || (ch == 13)) {
112 send_complete_line = 1;
113 } else if ((ch == 8) || (ch == 127)) {
114 if (!IsEmptyStr(wbuf)) {
115 wbuf[strlen(wbuf) - 1] = 0;
116 scr_printf("%c %c", 8, 8);
120 wbuf[strlen(wbuf) + 1] = 0;
121 wbuf[strlen(wbuf)] = ch;
125 /* if the user hit return, send the line */
126 if (send_complete_line) {
128 if (!strcasecmp(wbuf, "/quit")) {
129 CtdlIPC_chat_send(ipc, "RCHT exit");
130 CtdlIPC_chat_recv(ipc, response); /* don't care about the result */
132 scr_printf("\rExiting chat mode\n");
137 CtdlIPC_chat_send(ipc, "RCHT send");
138 CtdlIPC_chat_recv(ipc, response);
139 if (response[0] == '4') {
140 CtdlIPC_chat_send(ipc, wbuf);
141 CtdlIPC_chat_send(ipc, "000");
144 send_complete_line = 0;
147 /* if it's time to word wrap, send a partial line */
148 if (strlen(wbuf) >= (77 - strlen(fullname))) {
150 for (a = 0; !IsEmptyStr(&wbuf[a]); ++a) {
155 CtdlIPC_chat_send(ipc, "RCHT send");
156 CtdlIPC_chat_recv(ipc, response);
157 if (response[0] == '4') {
158 CtdlIPC_chat_send(ipc, wbuf);
159 CtdlIPC_chat_send(ipc, "000");
162 send_complete_line = 0;
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");
171 strcpy(wbuf, &wbuf[pos + 1]);
175 /* poll for incoming chat messages */
176 snprintf(buf, sizeof buf, "RCHT poll|%d", seq);
177 CtdlIPC_chat_send(ipc, buf);
178 CtdlIPC_chat_recv(ipc, response);
180 if (response[0] == '1') {
181 seq = extract_int(&response[4], 0);
182 extract_token(c_user, &response[4], 2, '|', sizeof c_user);
183 while (CtdlIPC_chat_recv(ipc, c_text), strcmp(c_text, "000")) {
184 scr_printf("\r%79s\r", "");
185 if (!strcmp(c_user, fullname)) {
186 color(BRIGHT_YELLOW);
187 } else if (!strcmp(c_user, ":")) {
192 if (strcmp(c_user, last_user)) {
193 snprintf(buf, sizeof buf, "%s: %s", c_user, c_text);
195 size_t i = MIN(sizeof buf - 1, strlen(c_user) + 2);
197 safestrncpy(&buf[i], c_text, sizeof buf - i);
199 while (strlen(buf) < 79) {
202 if (strcmp(c_user, last_user)) {
203 scr_printf("\r%79s\n", "");
204 strcpy(last_user, c_user);
206 scr_printf("\r%s\n", buf);
210 color(BRIGHT_YELLOW);
211 scr_printf("\r> %s", wbuf);
219 * send an instant message
221 void page_user(CtdlIPC *ipc)
223 char buf[SIZ], touser[SIZ], msg[SIZ];
226 strcpy(touser, last_paged);
227 strprompt("Page who", touser, 30);
229 /* old server -- use inline paging */
230 if (ipc->ServInfo.paging_level == 0) {
231 newprompt("Message: ", msg, 69);
232 snprintf(buf, sizeof buf, "SEXP %s|%s", touser, msg);
233 CtdlIPC_chat_send(ipc, buf);
234 CtdlIPC_chat_recv(ipc, buf);
235 if (!strncmp(buf, "200", 3)) {
236 strcpy(last_paged, touser);
238 scr_printf("%s\n", &buf[4]);
241 /* new server -- use extended paging */
242 else if (ipc->ServInfo.paging_level >= 1) {
243 snprintf(buf, sizeof buf, "SEXP %s||", touser);
244 CtdlIPC_chat_send(ipc, buf);
245 CtdlIPC_chat_recv(ipc, buf);
247 scr_printf("%s\n", &buf[4]);
250 if (client_make_message(ipc, temp, touser, 0, 0, 0, NULL, 0) != 0) {
251 scr_printf("No message sent.\n");
254 pagefp = fopen(temp, "r");
256 snprintf(buf, sizeof buf, "SEXP %s|-", touser);
257 CtdlIPC_chat_send(ipc, buf);
258 CtdlIPC_chat_recv(ipc, buf);
260 strcpy(last_paged, touser);
261 while (fgets(buf, sizeof buf, pagefp) != NULL) {
262 buf[strlen(buf) - 1] = 0;
263 CtdlIPC_chat_send(ipc, buf);
266 CtdlIPC_chat_send(ipc, "000");
267 scr_printf("Message sent.\n");
269 scr_printf("%s\n", &buf[4]);
275 void quiet_mode(CtdlIPC *ipc)
277 static int quiet = 0;
281 r = CtdlIPCEnableInstantMessageReceipt(ipc, !quiet, cret);
284 scr_printf("Quiet mode %sabled (%sother users may page you)\n",
285 (quiet) ? "en" : "dis",
286 (quiet) ? "no " : "");
288 scr_printf("Unable to change quiet mode: %s\n", cret);
293 void stealth_mode(CtdlIPC *ipc)
295 static int stealth = 0;
299 r = CtdlIPCStealthMode(ipc, !stealth, cret);
302 scr_printf("Stealth mode %sabled (you are %s)\n",
303 (stealth) ? "en" : "dis",
304 (stealth) ? "invisible" : "listed as online");
306 scr_printf("Unable to change stealth mode: %s\n", cret);