HUGE PATCH. This moves all of mime_parser.c and all
[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 <libcitadel.h>
36 #include "citadel.h"
37 #include "citadel_ipc.h"
38 #include "client_chat.h"
39 #include "commands.h"
40 #include "routines.h"
41 #include "citadel_decls.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         strcpy(last_user, ""); 
88         color(BRIGHT_YELLOW);
89         sln_printf_if("\n");
90         sln_printf("> ");
91         send_complete_line = 0;
92         recv_complete_line = 0;
93
94         while (1) {
95                 sln_flush();
96                 FD_ZERO(&rfds);
97                 FD_SET(0, &rfds);
98                 FD_SET(CtdlIPC_getsockfd(ipc), &rfds);
99                 tv.tv_sec = S_KEEPALIVE;
100                 tv.tv_usec = 0;
101                 retval = select(CtdlIPC_getsockfd(ipc) + 1, &rfds,
102                         NULL, NULL, &tv);
103
104                 /* If there's data from the server... */
105                 if (FD_ISSET(CtdlIPC_getsockfd(ipc), &rfds)) {
106                         CtdlIPC_chat_recv(ipc, buf);
107                         recv_complete_line = 1;
108                         goto RCL;       /* ugly, but we've gotta get out! */
109                 }
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 RCL:            if (send_complete_line) {
130                         CtdlIPC_chat_send(ipc, wbuf);
131                         last_transmit = time(NULL);
132                         strcpy(wbuf, "");
133                         send_complete_line = 0;
134                 }
135
136                 /* if it's time to word wrap, send a partial line */
137                 if (strlen(wbuf) >= (77 - strlen(fullname))) {
138                         pos = 0;
139                         for (a = 0; !IsEmptyStr(&wbuf[a]); ++a) {
140                                 if (wbuf[a] == 32)
141                                         pos = a;
142                         }
143                         if (pos == 0) {
144                                 CtdlIPC_chat_send(ipc, wbuf);
145                                 last_transmit = time(NULL);
146                                 strcpy(wbuf, "");
147                                 send_complete_line = 0;
148                         } else {
149                                 wbuf[pos] = 0;
150                                 CtdlIPC_chat_send(ipc, wbuf);
151                                 last_transmit = time(NULL);
152                                 strcpy(wbuf, &wbuf[pos + 1]);
153                         }
154                 }
155
156                 if (recv_complete_line) {
157                         sln_printf("\r%79s\r", "");
158                         if (!strcmp(buf, "000")) {
159                                 color(BRIGHT_WHITE);
160                                 sln_printf("\rExiting chat mode\n");
161                                 sln_flush();
162                                 set_keepalives(KA_YES);
163
164                                 /* Some users complained about the client and
165                                  * server losing protocol synchronization when
166                                  * exiting chat.  This little dialog forces
167                                  * everything to be hunky-dory.
168                                  */
169                                 CtdlIPC_chat_send(ipc, "ECHO __ExitingChat__");
170                                 do {
171                                         CtdlIPC_chat_recv(ipc, buf);
172                                 } while (strcmp(buf, "200 __ExitingChat__"));
173                                 return;
174                         }
175                         if (num_parms(buf) >= 2) {
176                                 extract_token(c_user, buf, 0, '|', sizeof c_user);
177                                 extract_token(c_text, buf, 1, '|', sizeof c_text);
178                                 if (num_parms(buf) > 2) {
179                                         extract_token(c_room, buf, 2, '|', sizeof c_room);
180                                         scr_printf("Got room %s\n", c_room);
181                                 }
182                                 if (strcasecmp(c_text, "NOOP")) {
183                                         if (!strcmp(c_user, fullname)) {
184                                                 color(BRIGHT_YELLOW);
185                                         } else if (!strcmp(c_user, ":")) {
186                                                 color(BRIGHT_RED);
187                                         } else {
188                                                 color(BRIGHT_GREEN);
189                                         }
190                                         if (strcmp(c_user, last_user)) {
191                                                 snprintf(buf, sizeof buf, "%s: %s", c_user, c_text);
192                                         } else {
193                                                 size_t i = MIN(sizeof buf - 1,
194                                                      strlen(c_user) + 2);
195
196                                                 memset(buf, ' ', i);
197                                                 safestrncpy(&buf[i], c_text,
198                                                          sizeof buf - i);
199                                         }
200                                         while (strlen(buf) < 79)
201                                                 strcat(buf, " ");
202                                         if (strcmp(c_user, last_user)) {
203                                                 sln_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                         sln_printf("\r> %s", wbuf);
212                         sln_flush();
213                         recv_complete_line = 0;
214                         strcpy(buf, "");
215                 }
216
217                 /* If the user is sitting idle, send a half-keepalive to the
218                  * server to prevent session timeout.
219                  */
220                 if ((time(NULL) - last_transmit) >= S_KEEPALIVE) {
221                         CtdlIPC_chat_send(ipc, "NOOP");
222                         last_transmit = time(NULL);
223                 }
224
225         }
226 }
227
228 /*
229  * send an instant message
230  */
231 void page_user(CtdlIPC *ipc)
232 {
233         char buf[SIZ], touser[SIZ], msg[SIZ];
234         FILE *pagefp;
235
236         strcpy(touser, last_paged);
237         strprompt("Page who", touser, 30);
238
239         /* old server -- use inline paging */
240         if (ipc->ServInfo.paging_level == 0) {
241                 newprompt("Message: ", msg, 69);
242                 snprintf(buf, sizeof buf, "SEXP %s|%s", touser, msg);
243                 CtdlIPC_chat_send(ipc, buf);
244                 CtdlIPC_chat_recv(ipc, buf);
245                 if (!strncmp(buf, "200", 3)) {
246                         strcpy(last_paged, touser);
247                 }
248                 scr_printf("%s\n", &buf[4]);
249                 return;
250         }
251         /* new server -- use extended paging */
252         else if (ipc->ServInfo.paging_level >= 1) {
253                 snprintf(buf, sizeof buf, "SEXP %s||", touser);
254                 CtdlIPC_chat_send(ipc, buf);
255                 CtdlIPC_chat_recv(ipc, buf);
256                 if (buf[0] != '2') {
257                         scr_printf("%s\n", &buf[4]);
258                         return;
259                 }
260                 if (client_make_message(ipc, temp, touser, 0, 0, 0, NULL, 0) != 0) {
261                         scr_printf("No message sent.\n");
262                         return;
263                 }
264                 pagefp = fopen(temp, "r");
265                 unlink(temp);
266                 snprintf(buf, sizeof buf, "SEXP %s|-", touser);
267                 CtdlIPC_chat_send(ipc, buf);
268                 CtdlIPC_chat_recv(ipc, buf);
269                 if (buf[0] == '4') {
270                         strcpy(last_paged, touser);
271                         while (fgets(buf, sizeof buf, pagefp) != NULL) {
272                                 buf[strlen(buf) - 1] = 0;
273                                 CtdlIPC_chat_send(ipc, buf);
274                         }
275                         fclose(pagefp);
276                         CtdlIPC_chat_send(ipc, "000");
277                         scr_printf("Message sent.\n");
278                 } else {
279                         scr_printf("%s\n", &buf[4]);
280                 }
281         }
282 }
283
284
285 void quiet_mode(CtdlIPC *ipc)
286 {
287         static int quiet = 0;
288         char cret[SIZ];
289         int r;
290
291         r = CtdlIPCEnableInstantMessageReceipt(ipc, !quiet, cret);
292         if (r / 100 == 2) {
293                 quiet = !quiet;
294                 scr_printf("Quiet mode %sabled (%sother users may page you)\n",
295                                 (quiet) ? "en" : "dis",
296                                 (quiet) ? "no " : "");
297         } else {
298                 scr_printf("Unable to change quiet mode: %s\n", cret);
299         }
300 }
301
302
303 void stealth_mode(CtdlIPC *ipc)
304 {
305         static int stealth = 0;
306         char cret[SIZ];
307         int r;
308
309         r = CtdlIPCStealthMode(ipc, !stealth, cret);
310         if (r / 100 == 2) {
311                 stealth = !stealth;
312                 scr_printf("Stealth mode %sabled (you are %s)\n",
313                                 (stealth) ? "en" : "dis",
314                                 (stealth) ? "invisible" : "listed as online");
315         } else {
316                 scr_printf("Unable to change stealth mode: %s\n", cret);
317         }
318 }