8933687bfc55dc694b662262d592e38495c64cef
[citadel.git] / textclient / src / client_chat.c
1 /*
2  * front end for multiuser chat
3  *
4  * Copyright (c) 1987-2016 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_ipc.h"
43 #include "client_chat.h"
44 #include "commands.h"
45 #include "routines.h"
46 #include "citadel_decls.h"
47 #include "rooms.h"
48 #include "messages.h"
49 #include "screen.h"
50
51 #define MIN(a, b) ((a) < (b) ? (a) : (b))
52
53 extern char temp[];
54 char last_paged[SIZ] = "";
55
56 void chatmode(CtdlIPC *ipc)
57 {
58         char wbuf[SIZ];
59         char buf[SIZ];
60         char response[SIZ];
61         char c_user[SIZ];
62         char c_text[SIZ];
63         char last_user[SIZ];
64         int send_complete_line;
65         char ch;
66         int a, pos;
67         int seq = 0;
68
69         fd_set rfds;
70         struct timeval tv;
71         int retval;
72
73         CtdlIPC_chat_send(ipc, "RCHT enter");
74         CtdlIPC_chat_recv(ipc, buf);
75         if (buf[0] != '2') {
76                 scr_printf("%s\n", &buf[4]);
77                 return;
78         }
79         scr_printf("Entering chat mode (type /quit to exit)\n");
80
81         strcpy(buf, "");
82         strcpy(wbuf, "");
83         strcpy(last_user, ""); 
84         color(BRIGHT_YELLOW);
85         scr_printf("\n");
86         scr_printf("> ");
87         send_complete_line = 0;
88
89         while (1) {
90                 scr_flush();
91                 FD_ZERO(&rfds);
92                 FD_SET(0, &rfds);
93                 tv.tv_sec = 1;
94                 tv.tv_usec = 0;
95                 retval = select(1, &rfds, NULL, NULL, &tv);
96
97                 if (retval < 0) {
98                         color(BRIGHT_WHITE);
99                         scr_printf("Server gone Exiting chat mode\n");
100                         scr_flush();
101                         return;
102                 }
103
104                 /* If there's data from the keyboard... */
105                 if (FD_ISSET(0, &rfds)) {
106                         ch = scr_getc(SCR_BLOCK);
107                         if ((ch == 10) || (ch == 13)) {
108                                 send_complete_line = 1;
109                         } else if ((ch == 8) || (ch == 127)) {
110                                 if (!IsEmptyStr(wbuf)) {
111                                         wbuf[strlen(wbuf) - 1] = 0;
112                                         scr_printf("%c %c", 8, 8);
113                                 }
114                         } else {
115                                 scr_putc(ch);
116                                 wbuf[strlen(wbuf) + 1] = 0;
117                                 wbuf[strlen(wbuf)] = ch;
118                         }
119                 }
120
121                 /* if the user hit return, send the line */
122                 if (send_complete_line) {
123
124                         if (!strcasecmp(wbuf, "/quit")) {
125                                 CtdlIPC_chat_send(ipc, "RCHT exit");
126                                 CtdlIPC_chat_recv(ipc, response);       /* don't care about the result */
127                                 color(BRIGHT_WHITE);
128                                 scr_printf("\rExiting chat mode\n");
129                                 scr_flush();
130                                 return;
131                         }
132
133                         CtdlIPC_chat_send(ipc, "RCHT send");
134                         CtdlIPC_chat_recv(ipc, response);
135                         if (response[0] == '4') {
136                                 CtdlIPC_chat_send(ipc, wbuf);
137                                 CtdlIPC_chat_send(ipc, "000");
138                         }
139                         strcpy(wbuf, "");
140                         send_complete_line = 0;
141                 }
142
143                 /* if it's time to word wrap, send a partial line */
144                 if (strlen(wbuf) >= (77 - strlen(fullname))) {
145                         pos = 0;
146                         for (a = 0; !IsEmptyStr(&wbuf[a]); ++a) {
147                                 if (wbuf[a] == 32)
148                                         pos = a;
149                         }
150                         if (pos == 0) {
151                                 CtdlIPC_chat_send(ipc, "RCHT send");
152                                 CtdlIPC_chat_recv(ipc, response);
153                                 if (response[0] == '4') {
154                                         CtdlIPC_chat_send(ipc, wbuf);
155                                         CtdlIPC_chat_send(ipc, "000");
156                                 }
157                                 strcpy(wbuf, "");
158                                 send_complete_line = 0;
159                         } else {
160                                 wbuf[pos] = 0;
161                                 CtdlIPC_chat_send(ipc, "RCHT send");
162                                 CtdlIPC_chat_recv(ipc, response);
163                                 if (response[0] == '4') {
164                                         CtdlIPC_chat_send(ipc, wbuf);
165                                         CtdlIPC_chat_send(ipc, "000");
166                                 }
167                                 strcpy(wbuf, &wbuf[pos + 1]);
168                         }
169                 }
170
171                 /* poll for incoming chat messages */
172                 snprintf(buf, sizeof buf, "RCHT poll|%d", seq);
173                 CtdlIPC_chat_send(ipc, buf);
174                 CtdlIPC_chat_recv(ipc, response);
175         
176                 if (response[0] == '1') {
177                         seq = extract_int(&response[4], 0);
178                         extract_token(c_user, &response[4], 2, '|', sizeof c_user);
179                         while (CtdlIPC_chat_recv(ipc, c_text), strcmp(c_text, "000")) {
180                                 scr_printf("\r%79s\r", "");
181                                 if (!strcmp(c_user, fullname)) {
182                                         color(BRIGHT_YELLOW);
183                                 } else if (!strcmp(c_user, ":")) {
184                                         color(BRIGHT_RED);
185                                 } else {
186                                         color(BRIGHT_GREEN);
187                                 }
188                                 if (strcmp(c_user, last_user)) {
189                                         snprintf(buf, sizeof buf, "%s: %s", c_user, c_text);
190                                 } else {
191                                         size_t i = MIN(sizeof buf - 1, strlen(c_user) + 2);
192                                         memset(buf, ' ', i);
193                                         safestrncpy(&buf[i], c_text, sizeof buf - i);
194                                 }
195                                 while (strlen(buf) < 79) {
196                                         strcat(buf, " ");
197                                 }
198                                 if (strcmp(c_user, last_user)) {
199                                         scr_printf("\r%79s\n", "");
200                                         strcpy(last_user, c_user);
201                                 }
202                                 scr_printf("\r%s\n", buf);
203                                 scr_flush();
204                         }
205                 }
206                 color(BRIGHT_YELLOW);
207                 scr_printf("\r> %s", wbuf);
208                 scr_flush();
209                 strcpy(buf, "");
210         }
211 }
212
213
214 /*
215  * send an instant message
216  */
217 void page_user(CtdlIPC *ipc)
218 {
219         char buf[SIZ], touser[SIZ], msg[SIZ];
220         FILE *pagefp;
221
222         strcpy(touser, last_paged);
223         strprompt("Page who", touser, 30);
224
225         /* old server -- use inline paging */
226         if (ipc->ServInfo.paging_level == 0) {
227                 newprompt("Message: ", msg, 69);
228                 snprintf(buf, sizeof buf, "SEXP %s|%s", touser, msg);
229                 CtdlIPC_chat_send(ipc, buf);
230                 CtdlIPC_chat_recv(ipc, buf);
231                 if (!strncmp(buf, "200", 3)) {
232                         strcpy(last_paged, touser);
233                 }
234                 scr_printf("%s\n", &buf[4]);
235                 return;
236         }
237         /* new server -- use extended paging */
238         else if (ipc->ServInfo.paging_level >= 1) {
239                 snprintf(buf, sizeof buf, "SEXP %s||", touser);
240                 CtdlIPC_chat_send(ipc, buf);
241                 CtdlIPC_chat_recv(ipc, buf);
242                 if (buf[0] != '2') {
243                         scr_printf("%s\n", &buf[4]);
244                         return;
245                 }
246                 if (client_make_message(ipc, temp, touser, 0, 0, 0, NULL, 0) != 0) {
247                         scr_printf("No message sent.\n");
248                         return;
249                 }
250                 pagefp = fopen(temp, "r");
251                 unlink(temp);
252                 snprintf(buf, sizeof buf, "SEXP %s|-", touser);
253                 CtdlIPC_chat_send(ipc, buf);
254                 CtdlIPC_chat_recv(ipc, buf);
255                 if (buf[0] == '4') {
256                         strcpy(last_paged, touser);
257                         while (fgets(buf, sizeof buf, pagefp) != NULL) {
258                                 buf[strlen(buf) - 1] = 0;
259                                 CtdlIPC_chat_send(ipc, buf);
260                         }
261                         fclose(pagefp);
262                         CtdlIPC_chat_send(ipc, "000");
263                         scr_printf("Message sent.\n");
264                 } else {
265                         scr_printf("%s\n", &buf[4]);
266                 }
267         }
268 }
269
270
271 void quiet_mode(CtdlIPC *ipc)
272 {
273         static int quiet = 0;
274         char cret[SIZ];
275         int r;
276
277         r = CtdlIPCEnableInstantMessageReceipt(ipc, !quiet, cret);
278         if (r / 100 == 2) {
279                 quiet = !quiet;
280                 scr_printf("Quiet mode %sabled (%sother users may page you)\n",
281                                 (quiet) ? "en" : "dis",
282                                 (quiet) ? "no " : "");
283         } else {
284                 scr_printf("Unable to change quiet mode: %s\n", cret);
285         }
286 }
287
288
289 void stealth_mode(CtdlIPC *ipc)
290 {
291         static int stealth = 0;
292         char cret[SIZ];
293         int r;
294
295         r = CtdlIPCStealthMode(ipc, !stealth, cret);
296         if (r / 100 == 2) {
297                 stealth = !stealth;
298                 scr_printf("Stealth mode %sabled (you are %s)\n",
299                                 (stealth) ? "en" : "dis",
300                                 (stealth) ? "invisible" : "listed as online");
301         } else {
302                 scr_printf("Unable to change stealth mode: %s\n", cret);
303         }
304 }