08eff91d854a83f283ca1899320badf04f147dce
[citadel.git] / citadel / client_chat.c
1 /*
2  * $Id$
3  *
4  * front end for chat mode
5  * (the "single process" version - no more fork() anymore)
6  *
7  */
8
9 #include "sysdep.h"
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <stdio.h>
13 #include <fcntl.h>
14 #include <ctype.h>
15 #include <string.h>
16 #include <signal.h>
17 #include <errno.h>
18
19 #if TIME_WITH_SYS_TIME
20 # include <sys/time.h>
21 # include <time.h>
22 #else
23 # if HAVE_SYS_TIME_H
24 #  include <sys/time.h>
25 # else
26 #  include <time.h>
27 # endif
28 #endif
29
30 #include <sys/types.h>
31 #ifdef HAVE_SYS_SELECT_H
32 #include <sys/select.h>
33 #endif
34 #include <stdarg.h>
35 #include "citadel.h"
36 #include "citadel_ipc.h"
37 #include "client_chat.h"
38 #include "commands.h"
39 #include "routines.h"
40 #include "citadel_decls.h"
41 #include "tools.h"
42 #include "rooms.h"
43 #include "messages.h"
44 #ifndef HAVE_SNPRINTF
45 #include "snprintf.h"
46 #endif
47 #include "screen.h"
48
49 #define MIN(a, b) ((a) < (b) ? (a) : (b))
50
51 extern char temp[];
52 void ctdl_getline(char *, int);
53
54 char last_paged[SIZ] = "";
55
56 void chatmode(CtdlIPC *ipc)
57 {
58         char wbuf[SIZ];
59         char buf[SIZ];
60         char c_user[SIZ];
61         char c_text[SIZ];
62         char c_room[SIZ];
63         char last_user[SIZ];
64         int send_complete_line;
65         int recv_complete_line;
66         char ch;
67         int a, pos;
68         time_t last_transmit;
69
70         fd_set rfds;
71         struct timeval tv;
72         int retval;
73
74         CtdlIPC_chat_send(ipc, "CHAT");
75         CtdlIPC_chat_recv(ipc, buf);
76         if (buf[0] != '8') {
77                 scr_printf("%s\n", &buf[4]);
78                 return;
79         }
80         scr_printf("Entering chat mode "
81                 "(type /quit to exit, /help for other cmds)\n");
82         set_keepalives(KA_NO);
83         last_transmit = time(NULL);
84
85         strcpy(buf, "");
86         strcpy(wbuf, "");
87         color(BRIGHT_YELLOW);
88         sln_printf_if("\n");
89         sln_printf("> ");
90         send_complete_line = 0;
91         recv_complete_line = 0;
92
93         while (1) {
94                 sln_flush();
95                 FD_ZERO(&rfds);
96                 FD_SET(0, &rfds);
97                 FD_SET(CtdlIPC_getsockfd(ipc), &rfds);
98                 tv.tv_sec = S_KEEPALIVE;
99                 tv.tv_usec = 0;
100                 retval = select(CtdlIPC_getsockfd(ipc) + 1, &rfds,
101                         NULL, NULL, &tv);
102
103                 /* If there's data from the server... */
104                 if (FD_ISSET(CtdlIPC_getsockfd(ipc), &rfds)) {
105                         CtdlIPC_chat_recv(ipc, buf);
106                         recv_complete_line = 1;
107                         goto RCL;       /* ugly, but we've gotta get out! */
108                 }
109
110                 /* If there's data from the keyboard... */
111                 if (FD_ISSET(0, &rfds)) {
112                         ch = scr_getc(SCR_BLOCK);
113                         if ((ch == 10) || (ch == 13)) {
114                                 send_complete_line = 1;
115                         } else if ((ch == 8) || (ch == 127)) {
116                                 if (strlen(wbuf) > 0) {
117                                         wbuf[strlen(wbuf) - 1] = 0;
118                                         sln_printf("%c %c", 8, 8);
119                                 }
120                         } else {
121                                 sln_putc(ch);
122                                 wbuf[strlen(wbuf) + 1] = 0;
123                                 wbuf[strlen(wbuf)] = ch;
124                         }
125                 }
126
127                 /* if the user hit return, send the line */
128 RCL:            if (send_complete_line) {
129                         CtdlIPC_chat_send(ipc, wbuf);
130                         last_transmit = time(NULL);
131                         strcpy(wbuf, "");
132                         send_complete_line = 0;
133                 }
134
135                 /* if it's time to word wrap, send a partial line */
136                 if (strlen(wbuf) >= (77 - strlen(fullname))) {
137                         pos = 0;
138                         for (a = 0; a < strlen(wbuf); ++a) {
139                                 if (wbuf[a] == 32)
140                                         pos = a;
141                         }
142                         if (pos == 0) {
143                                 CtdlIPC_chat_send(ipc, wbuf);
144                                 last_transmit = time(NULL);
145                                 strcpy(wbuf, "");
146                                 send_complete_line = 0;
147                         } else {
148                                 wbuf[pos] = 0;
149                                 CtdlIPC_chat_send(ipc, wbuf);
150                                 last_transmit = time(NULL);
151                                 strcpy(wbuf, &wbuf[pos + 1]);
152                         }
153                 }
154
155                 if (recv_complete_line) {
156                         sln_printf("\r%79s\r", "");
157                         if (!strcmp(buf, "000")) {
158                                 color(BRIGHT_WHITE);
159                                 sln_printf("\rExiting chat mode\n");
160                                 sln_flush();
161                                 set_keepalives(KA_YES);
162
163                                 /* Some users complained about the client and
164                                  * server losing protocol synchronization when
165                                  * exiting chat.  This little dialog forces
166                                  * everything to be hunky-dory.
167                                  */
168                                 CtdlIPC_chat_send(ipc, "ECHO __ExitingChat__");
169                                 do {
170                                         CtdlIPC_chat_recv(ipc, buf);
171                                 } while (strcmp(buf, "200 __ExitingChat__"));
172                                 return;
173                         }
174                         if (num_parms(buf) >= 2) {
175                                 extract_token(c_user, buf, 0, '|', sizeof c_user);
176                                 extract_token(c_text, buf, 1, '|', sizeof c_text);
177                                 if (num_parms(buf) > 2) {
178                                         extract_token(c_room, buf, 2, '|', sizeof c_room);
179                                         scr_printf("Got room %s\n", c_room);
180                                 }
181                                 if (strcasecmp(c_text, "NOOP")) {
182                                         if (!strcmp(c_user, fullname)) {
183                                                 color(BRIGHT_YELLOW);
184                                         } else if (!strcmp(c_user, ":")) {
185                                                 color(BRIGHT_RED);
186                                         } else {
187                                                 color(BRIGHT_GREEN);
188                                         }
189                                         if (strcmp(c_user, last_user)) {
190                                                 snprintf(buf, sizeof buf, "%s: %s", c_user, c_text);
191                                         } else {
192                                                 size_t i = MIN(sizeof buf - 1,
193                                                      strlen(c_user) + 2);
194
195                                                 memset(buf, ' ', i);
196                                                 safestrncpy(&buf[i], c_text,
197                                                          sizeof buf - i);
198                                         }
199                                         while (strlen(buf) < 79)
200                                                 strcat(buf, " ");
201                                         if (strcmp(c_user, last_user)) {
202                                                 sln_printf("\r%79s\n", "");
203                                                 strcpy(last_user, c_user);
204                                         }
205                                         scr_printf("\r%s\n", buf);
206                                         scr_flush();
207                                 }
208                         }
209                         color(BRIGHT_YELLOW);
210                         sln_printf("\r> %s", wbuf);
211                         sln_flush();
212                         recv_complete_line = 0;
213                         strcpy(buf, "");
214                 }
215
216                 /* If the user is sitting idle, send a half-keepalive to the
217                  * server to prevent session timeout.
218                  */
219                 if ((time(NULL) - last_transmit) >= S_KEEPALIVE) {
220                         CtdlIPC_chat_send(ipc, "NOOP");
221                         last_transmit = time(NULL);
222                 }
223
224         }
225 }
226
227 /*
228  * send an instant message
229  */
230 void page_user(CtdlIPC *ipc)
231 {
232         char buf[SIZ], touser[SIZ], msg[SIZ];
233         FILE *pagefp;
234
235         strcpy(touser, last_paged);
236         strprompt("Page who", touser, 30);
237
238         /* old server -- use inline paging */
239         if (ipc->ServInfo.paging_level == 0) {
240                 newprompt("Message: ", msg, 69);
241                 snprintf(buf, sizeof buf, "SEXP %s|%s", touser, msg);
242                 CtdlIPC_chat_send(ipc, buf);
243                 CtdlIPC_chat_recv(ipc, buf);
244                 if (!strncmp(buf, "200", 3)) {
245                         strcpy(last_paged, touser);
246                 }
247                 scr_printf("%s\n", &buf[4]);
248                 return;
249         }
250         /* new server -- use extended paging */
251         else if (ipc->ServInfo.paging_level >= 1) {
252                 snprintf(buf, sizeof buf, "SEXP %s||", touser);
253                 CtdlIPC_chat_send(ipc, buf);
254                 CtdlIPC_chat_recv(ipc, buf);
255                 if (buf[0] != '2') {
256                         scr_printf("%s\n", &buf[4]);
257                         return;
258                 }
259                 if (client_make_message(ipc, temp, touser, 0, 0, 0, NULL, 0) != 0) {
260                         scr_printf("No message sent.\n");
261                         return;
262                 }
263                 pagefp = fopen(temp, "r");
264                 unlink(temp);
265                 snprintf(buf, sizeof buf, "SEXP %s|-", touser);
266                 CtdlIPC_chat_send(ipc, buf);
267                 CtdlIPC_chat_recv(ipc, buf);
268                 if (buf[0] == '4') {
269                         strcpy(last_paged, touser);
270                         while (fgets(buf, sizeof buf, pagefp) != NULL) {
271                                 buf[strlen(buf) - 1] = 0;
272                                 CtdlIPC_chat_send(ipc, buf);
273                         }
274                         fclose(pagefp);
275                         CtdlIPC_chat_send(ipc, "000");
276                         scr_printf("Message sent.\n");
277                 } else {
278                         scr_printf("%s\n", &buf[4]);
279                 }
280         }
281 }
282
283
284 void quiet_mode(CtdlIPC *ipc)
285 {
286         static int quiet = 0;
287         char cret[SIZ];
288         int r;
289
290         r = CtdlIPCEnableInstantMessageReceipt(ipc, !quiet, cret);
291         if (r / 100 == 2) {
292                 quiet = !quiet;
293                 scr_printf("Quiet mode %sabled (%sother users may page you)\n",
294                                 (quiet) ? "en" : "dis",
295                                 (quiet) ? "no " : "");
296         } else {
297                 scr_printf("Unable to change quiet mode: %s\n", cret);
298         }
299 }
300
301
302 void stealth_mode(CtdlIPC *ipc)
303 {
304         static int stealth = 0;
305         char cret[SIZ];
306         int r;
307
308         r = CtdlIPCStealthMode(ipc, !stealth, cret);
309         if (r / 100 == 2) {
310                 stealth = !stealth;
311                 scr_printf("Stealth mode %sabled (you are %s)\n",
312                                 (stealth) ? "en" : "dis",
313                                 (stealth) ? "invisible" : "listed as online");
314         } else {
315                 scr_printf("Unable to change stealth mode: %s\n", cret);
316         }
317 }