* Full-screen curses support for Citadel text client
[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 "client_chat.h"
37 #include "commands.h"
38 #include "routines.h"
39 #include "ipc.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 struct CtdlServInfo serv_info;
52 extern char temp[];
53 void getline(char *, int);
54
55 char last_paged[SIZ] = "";
56
57 void chatmode(void)
58 {
59         char wbuf[SIZ];
60         char buf[SIZ];
61         char c_user[SIZ];
62         char c_text[SIZ];
63         char c_room[SIZ];
64         char last_user[SIZ];
65         int send_complete_line;
66         int recv_complete_line;
67         char ch;
68         int a, pos;
69         time_t last_transmit;
70
71         fd_set rfds;
72         struct timeval tv;
73         int retval;
74
75         serv_puts("CHAT");
76         serv_gets(buf);
77         if (buf[0] != '8') {
78                 scr_printf("%s\n", &buf[4]);
79                 return;
80         }
81         scr_printf("Entering chat mode (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         scr_printf("> ");
89         send_complete_line = 0;
90         recv_complete_line = 0;
91
92         while (1) {
93                 fflush(stdout);
94                 FD_ZERO(&rfds);
95                 FD_SET(0, &rfds);
96                 FD_SET(getsockfd(), &rfds);
97                 tv.tv_sec = S_KEEPALIVE;
98                 tv.tv_usec = 0;
99                 retval = select(getsockfd() + 1, &rfds, NULL, NULL, &tv);
100
101                 if (FD_ISSET(getsockfd(), &rfds)) {
102                         ch = serv_getc();
103                         if (ch == 10) {
104                                 recv_complete_line = 1;
105                                 goto RCL;       /* ugly, but we've gotta get out! */
106                         } else {
107                                 buf[strlen(buf) + 1] = 0;
108                                 buf[strlen(buf)] = ch;
109                         }
110                         goto RCL;
111                 }
112                 if (FD_ISSET(0, &rfds)) {
113                         ch = inkey();
114                         if ((ch == 10) || (ch == 13)) {
115                                 send_complete_line = 1;
116                         } else if ((ch == 8) || (ch == 127)) {
117                                 if (strlen(wbuf) > 0) {
118                                         wbuf[strlen(wbuf) - 1] = 0;
119                                         scr_printf("%c %c", 8, 8);
120                                 }
121                         } else {
122                                 putc(ch, stdout);
123                                 wbuf[strlen(wbuf) + 1] = 0;
124                                 wbuf[strlen(wbuf)] = ch;
125                         }
126                 }
127                 /* if the user hit return, send the line */
128               RCL:if (send_complete_line) {
129                         serv_puts(wbuf);
130                         last_transmit = time(NULL);
131                         strcpy(wbuf, "");
132                         send_complete_line = 0;
133                 }
134                 /* if it's time to word wrap, send a partial line */
135                 if (strlen(wbuf) >= (77 - strlen(fullname))) {
136                         pos = 0;
137                         for (a = 0; a < strlen(wbuf); ++a) {
138                                 if (wbuf[a] == 32)
139                                         pos = a;
140                         }
141                         if (pos == 0) {
142                                 serv_puts(wbuf);
143                                 last_transmit = time(NULL);
144                                 strcpy(wbuf, "");
145                                 send_complete_line = 0;
146                         } else {
147                                 wbuf[pos] = 0;
148                                 serv_puts(wbuf);
149                                 last_transmit = time(NULL);
150                                 strcpy(wbuf, &wbuf[pos + 1]);
151                         }
152                 }
153                 if (recv_complete_line) {
154                         scr_printf("\r%79s\r", "");
155                         if (!strcmp(buf, "000")) {
156                                 color(BRIGHT_WHITE);
157                                 scr_printf("Exiting chat mode\n");
158
159                                 scr_flush();
160                                 set_keepalives(KA_YES);
161
162
163                                 /* Some users complained about the client and server
164                                  * losing protocol synchronization when exiting chat.
165                                  * This little dialog forces everything to be
166                                  * hunky-dory.
167                                  */
168                                 serv_puts("ECHO __ExitingChat__");
169                                 do {
170                                         serv_gets(buf);
171                                 } while (strcmp(buf, "200 __ExitingChat__"));
172
173
174                                 return;
175                         }
176                         if (num_parms(buf) >= 2) {
177                                 extract(c_user, buf, 0);
178                                 extract(c_text, buf, 1);
179                                 if (num_parms(buf) > 2) {
180                                         extract(c_room, buf, 2);
181                                         scr_printf("Got room %s\n", c_room);
182                                 }
183                                 if (strcasecmp(c_text, "NOOP")) {
184                                         if (!strcmp(c_user, fullname)) {
185                                                 color(BRIGHT_YELLOW);
186                                         } else if (!strcmp(c_user, ":")) {
187                                                 color(BRIGHT_RED);
188                                         } else {
189                                                 color(BRIGHT_GREEN);
190                                         }
191                                         if (strcmp(c_user, last_user)) {
192                                                 snprintf(buf, sizeof buf, "%s: %s", c_user, c_text);
193                                         } else {
194                                                 size_t i = MIN(sizeof buf - 1,
195                                                      strlen(c_user) + 2);
196
197                                                 memset(buf, ' ', i);
198                                                 safestrncpy(&buf[i], c_text,
199                                                          sizeof buf - i);
200                                         }
201                                         while (strlen(buf) < 79)
202                                                 strcat(buf, " ");
203                                         if (strcmp(c_user, last_user)) {
204                                                 scr_printf("\r%79s\n", "");
205                                                 strcpy(last_user, c_user);
206                                         }
207                                         scr_printf("\r%s\n", buf);
208                                         fflush(stdout);
209                                 }
210                         }
211                         color(BRIGHT_YELLOW);
212                         scr_printf("> %s", wbuf);
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                         serv_puts("NOOP");
222                         last_transmit = time(NULL);
223                 }
224
225         }
226 }
227
228 /*
229  * send an express message
230  */
231 void page_user()
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 (serv_info.serv_paging_level == 0) {
241                 newprompt("Message:  ", msg, 69);
242                 snprintf(buf, sizeof buf, "SEXP %s|%s", touser, msg);
243                 serv_puts(buf);
244                 serv_gets(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 (serv_info.serv_paging_level >= 1) {
253                 snprintf(buf, sizeof buf, "SEXP %s||", touser);
254                 serv_puts(buf);
255                 serv_gets(buf);
256                 if (buf[0] != '2') {
257                         scr_printf("%s\n", &buf[4]);
258                         return;
259                 }
260                 if (client_make_message(temp, touser, 0, 0, 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                 serv_puts(buf);
268                 serv_gets(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                                 serv_puts(buf);
274                         }
275                         fclose(pagefp);
276                         serv_puts("000");
277                         scr_printf("Message sent.\n");
278                 } else {
279                         scr_printf("%s\n", &buf[4]);
280                 }
281         }
282 }
283
284
285
286
287 void quiet_mode(void)
288 {
289         int qstate;
290         char buf[SIZ];
291
292         serv_puts("DEXP 2");
293         serv_gets(buf);
294         if (buf[0] != '2') {
295                 scr_printf("%s\n", &buf[4]);
296                 return;
297         }
298         qstate = atoi(&buf[4]);
299         if (qstate == 0)
300                 qstate = 1;
301         else
302                 qstate = 0;
303         sprintf(buf, "DEXP %d", qstate);
304         serv_puts(buf);
305         serv_gets(buf);
306         if (buf[0] != '2') {
307                 scr_printf("%s\n", &buf[4]);
308                 return;
309         }
310         qstate = atoi(&buf[4]);
311         if (qstate) {
312                 scr_printf("Quiet mode enabled (no other users may page you)\n");
313         } else {
314                 scr_printf("Quiet mode disabled (other users may page you)\n");
315         }
316 }