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