590d8b4c96445498a679a3b6331576f35722f2bd
[citadel.git] / citadel / textclient / client_chat.c
1 /*
2  * $Id$
3  *
4  * front end for chat mode
5  * (the "single process" version - no more fork() anymore)
6  *
7  * Copyright (c) 1987-2010 by the citadel.org team
8  *
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.
13  *
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.
18  *
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
22  */
23
24 #include "sysdep.h"
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <stdio.h>
28 #include <fcntl.h>
29 #include <ctype.h>
30 #include <string.h>
31 #include <signal.h>
32 #include <errno.h>
33
34 #if TIME_WITH_SYS_TIME
35 # include <sys/time.h>
36 # include <time.h>
37 #else
38 # if HAVE_SYS_TIME_H
39 #  include <sys/time.h>
40 # else
41 #  include <time.h>
42 # endif
43 #endif
44
45 #include <sys/types.h>
46 #ifdef HAVE_SYS_SELECT_H
47 #include <sys/select.h>
48 #endif
49 #include <stdarg.h>
50 #include <libcitadel.h>
51 #include "citadel.h"
52 #include "citadel_ipc.h"
53 #include "client_chat.h"
54 #include "commands.h"
55 #include "routines.h"
56 #include "citadel_decls.h"
57 #include "rooms.h"
58 #include "messages.h"
59 #ifndef HAVE_SNPRINTF
60 #include "snprintf.h"
61 #endif
62 #include "screen.h"
63
64 #define MIN(a, b) ((a) < (b) ? (a) : (b))
65
66 extern char temp[];
67 void ctdl_getline(char *, int);
68
69
70 char last_paged[SIZ] = "";
71
72 void chatmode(CtdlIPC *ipc)
73 {
74         char wbuf[SIZ];
75         char buf[SIZ];
76         char response[SIZ];
77         char c_user[SIZ];
78         char c_text[SIZ];
79         char last_user[SIZ];
80         int send_complete_line;
81         char ch;
82         int a, pos;
83         int seq = 0;
84
85         fd_set rfds;
86         struct timeval tv;
87         int retval;
88
89         CtdlIPC_chat_send(ipc, "RCHT enter");
90         CtdlIPC_chat_recv(ipc, buf);
91         if (buf[0] != '2') {
92                 scr_printf("%s\n", &buf[4]);
93                 return;
94         }
95         scr_printf("Entering chat mode (type /quit to exit)\n");
96
97         strcpy(buf, "");
98         strcpy(wbuf, "");
99         strcpy(last_user, ""); 
100         color(BRIGHT_YELLOW);
101         sln_printf_if("\n");
102         sln_printf("> ");
103         send_complete_line = 0;
104
105         while (1) {
106                 sln_flush();
107                 FD_ZERO(&rfds);
108                 FD_SET(0, &rfds);
109                 tv.tv_sec = 1;
110                 tv.tv_usec = 0;
111                 retval = select(1, &rfds, NULL, NULL, &tv);
112
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);
122                                 }
123                         } else {
124                                 sln_putc(ch);
125                                 wbuf[strlen(wbuf) + 1] = 0;
126                                 wbuf[strlen(wbuf)] = ch;
127                         }
128                 }
129
130                 /* if the user hit return, send the line */
131                 if (send_complete_line) {
132
133                         if (!strcasecmp(wbuf, "/quit")) {
134                                 CtdlIPC_chat_send(ipc, "RCHT exit");
135                                 CtdlIPC_chat_recv(ipc, response);       /* don't care about the result */
136                                 color(BRIGHT_WHITE);
137                                 sln_printf("\rExiting chat mode\n");
138                                 sln_flush();
139                                 return;
140                         }
141
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");
147                         }
148                         strcpy(wbuf, "");
149                         send_complete_line = 0;
150                 }
151
152                 /* if it's time to word wrap, send a partial line */
153                 if (strlen(wbuf) >= (77 - strlen(fullname))) {
154                         pos = 0;
155                         for (a = 0; !IsEmptyStr(&wbuf[a]); ++a) {
156                                 if (wbuf[a] == 32)
157                                         pos = a;
158                         }
159                         if (pos == 0) {
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");
165                                 }
166                                 strcpy(wbuf, "");
167                                 send_complete_line = 0;
168                         } else {
169                                 wbuf[pos] = 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");
175                                 }
176                                 strcpy(wbuf, &wbuf[pos + 1]);
177                         }
178                 }
179
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);
184         
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, ":")) {
193                                         color(BRIGHT_RED);
194                                 } else {
195                                         color(BRIGHT_GREEN);
196                                 }
197                                 if (strcmp(c_user, last_user)) {
198                                         snprintf(buf, sizeof buf, "%s: %s", c_user, c_text);
199                                 } else {
200                                         size_t i = MIN(sizeof buf - 1, strlen(c_user) + 2);
201                                         memset(buf, ' ', i);
202                                         safestrncpy(&buf[i], c_text, sizeof buf - i);
203                                 }
204                                 while (strlen(buf) < 79) {
205                                         strcat(buf, " ");
206                                 }
207                                 if (strcmp(c_user, last_user)) {
208                                         sln_printf("\r%79s\n", "");
209                                         strcpy(last_user, c_user);
210                                 }
211                                 scr_printf("\r%s\n", buf);
212                                 scr_flush();
213                         }
214                 }
215                 color(BRIGHT_YELLOW);
216                 sln_printf("\r> %s", wbuf);
217                 sln_flush();
218                 strcpy(buf, "");
219         }
220 }
221
222
223 /*
224  * send an instant message
225  */
226 void page_user(CtdlIPC *ipc)
227 {
228         char buf[SIZ], touser[SIZ], msg[SIZ];
229         FILE *pagefp;
230
231         strcpy(touser, last_paged);
232         strprompt("Page who", touser, 30);
233
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);
242                 }
243                 scr_printf("%s\n", &buf[4]);
244                 return;
245         }
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);
251                 if (buf[0] != '2') {
252                         scr_printf("%s\n", &buf[4]);
253                         return;
254                 }
255                 if (client_make_message(ipc, temp, touser, 0, 0, 0, NULL, 0) != 0) {
256                         scr_printf("No message sent.\n");
257                         return;
258                 }
259                 pagefp = fopen(temp, "r");
260                 unlink(temp);
261                 snprintf(buf, sizeof buf, "SEXP %s|-", touser);
262                 CtdlIPC_chat_send(ipc, buf);
263                 CtdlIPC_chat_recv(ipc, buf);
264                 if (buf[0] == '4') {
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);
269                         }
270                         fclose(pagefp);
271                         CtdlIPC_chat_send(ipc, "000");
272                         scr_printf("Message sent.\n");
273                 } else {
274                         scr_printf("%s\n", &buf[4]);
275                 }
276         }
277 }
278
279
280 void quiet_mode(CtdlIPC *ipc)
281 {
282         static int quiet = 0;
283         char cret[SIZ];
284         int r;
285
286         r = CtdlIPCEnableInstantMessageReceipt(ipc, !quiet, cret);
287         if (r / 100 == 2) {
288                 quiet = !quiet;
289                 scr_printf("Quiet mode %sabled (%sother users may page you)\n",
290                                 (quiet) ? "en" : "dis",
291                                 (quiet) ? "no " : "");
292         } else {
293                 scr_printf("Unable to change quiet mode: %s\n", cret);
294         }
295 }
296
297
298 void stealth_mode(CtdlIPC *ipc)
299 {
300         static int stealth = 0;
301         char cret[SIZ];
302         int r;
303
304         r = CtdlIPCStealthMode(ipc, !stealth, cret);
305         if (r / 100 == 2) {
306                 stealth = !stealth;
307                 scr_printf("Stealth mode %sabled (you are %s)\n",
308                                 (stealth) ? "en" : "dis",
309                                 (stealth) ? "invisible" : "listed as online");
310         } else {
311                 scr_printf("Unable to change stealth mode: %s\n", cret);
312         }
313 }