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