fix all the <time.h> vs. <sys/time.h> issues, hopefully
[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
48 #define MIN(a, b) ((a) < (b) ? (a) : (b))
49
50 extern struct CtdlServInfo serv_info;
51 extern char temp[];
52 void getline(char *, int);
53
54 char last_paged[SIZ] = "";
55
56 void chatmode(void)
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         serv_puts("CHAT");
75         serv_gets(buf);
76         if (buf[0] != '8') {
77                 printf("%s\n", &buf[4]);
78                 return;
79         }
80         printf("Entering chat mode (type /quit to exit, /help for other cmds)\n");
81         set_keepalives(KA_NO);
82         last_transmit = time(NULL);
83
84         strcpy(buf, "");
85         strcpy(wbuf, "");
86         color(BRIGHT_YELLOW);
87         printf("> ");
88         send_complete_line = 0;
89         recv_complete_line = 0;
90
91         while (1) {
92                 fflush(stdout);
93                 FD_ZERO(&rfds);
94                 FD_SET(0, &rfds);
95                 FD_SET(getsockfd(), &rfds);
96                 tv.tv_sec = S_KEEPALIVE;
97                 tv.tv_usec = 0;
98                 retval = select(getsockfd() + 1, &rfds, NULL, NULL, &tv);
99
100                 if (FD_ISSET(getsockfd(), &rfds)) {
101                         ch = serv_getc();
102                         if (ch == 10) {
103                                 recv_complete_line = 1;
104                                 goto RCL;       /* ugly, but we've gotta get out! */
105                         } else {
106                                 buf[strlen(buf) + 1] = 0;
107                                 buf[strlen(buf)] = ch;
108                         }
109                         goto RCL;
110                 }
111                 if (FD_ISSET(0, &rfds)) {
112                         ch = inkey();
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                                         printf("%c %c", 8, 8);
119                                 }
120                         } else {
121                                 putc(ch, stdout);
122                                 wbuf[strlen(wbuf) + 1] = 0;
123                                 wbuf[strlen(wbuf)] = ch;
124                         }
125                 }
126                 /* if the user hit return, send the line */
127               RCL:if (send_complete_line) {
128                         serv_puts(wbuf);
129                         last_transmit = time(NULL);
130                         strcpy(wbuf, "");
131                         send_complete_line = 0;
132                 }
133                 /* if it's time to word wrap, send a partial line */
134                 if (strlen(wbuf) >= (77 - strlen(fullname))) {
135                         pos = 0;
136                         for (a = 0; a < strlen(wbuf); ++a) {
137                                 if (wbuf[a] == 32)
138                                         pos = a;
139                         }
140                         if (pos == 0) {
141                                 serv_puts(wbuf);
142                                 last_transmit = time(NULL);
143                                 strcpy(wbuf, "");
144                                 send_complete_line = 0;
145                         } else {
146                                 wbuf[pos] = 0;
147                                 serv_puts(wbuf);
148                                 last_transmit = time(NULL);
149                                 strcpy(wbuf, &wbuf[pos + 1]);
150                         }
151                 }
152                 if (recv_complete_line) {
153                         printf("\r%79s\r", "");
154                         if (!strcmp(buf, "000")) {
155                                 color(BRIGHT_WHITE);
156                                 printf("Exiting chat mode\n");
157
158                                 fflush(stdout);
159                                 set_keepalives(KA_YES);
160
161
162                                 /* Some users complained about the client and server
163                                  * losing protocol synchronization when exiting chat.
164                                  * This little dialog forces everything to be
165                                  * hunky-dory.
166                                  */
167                                 serv_puts("ECHO __ExitingChat__");
168                                 do {
169                                         serv_gets(buf);
170                                 } while (strcmp(buf, "200 __ExitingChat__"));
171
172
173                                 return;
174                         }
175                         if (num_parms(buf) >= 2) {
176                                 extract(c_user, buf, 0);
177                                 extract(c_text, buf, 1);
178                                 if (num_parms(buf) > 2) {
179                                         extract(c_room, buf, 2);
180                                         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                                                 printf("\r%79s\n", "");
204                                                 strcpy(last_user, c_user);
205                                         }
206                                         printf("\r%s\n", buf);
207                                         fflush(stdout);
208                                 }
209                         }
210                         color(BRIGHT_YELLOW);
211                         printf("> %s", wbuf);
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                         serv_puts("NOOP");
221                         last_transmit = time(NULL);
222                 }
223
224         }
225 }
226
227 /*
228  * send an express message
229  */
230 void page_user()
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 (serv_info.serv_paging_level == 0) {
240                 newprompt("Message:  ", msg, 69);
241                 snprintf(buf, sizeof buf, "SEXP %s|%s", touser, msg);
242                 serv_puts(buf);
243                 serv_gets(buf);
244                 if (!strncmp(buf, "200", 3)) {
245                         strcpy(last_paged, touser);
246                 }
247                 printf("%s\n", &buf[4]);
248                 return;
249         }
250         /* new server -- use extended paging */
251         else if (serv_info.serv_paging_level >= 1) {
252                 snprintf(buf, sizeof buf, "SEXP %s||", touser);
253                 serv_puts(buf);
254                 serv_gets(buf);
255                 if (buf[0] != '2') {
256                         printf("%s\n", &buf[4]);
257                         return;
258                 }
259                 if (make_message(temp, touser, 0, 0, 0) != 0) {
260                         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                 serv_puts(buf);
267                 serv_gets(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                                 serv_puts(buf);
273                         }
274                         fclose(pagefp);
275                         serv_puts("000");
276                         printf("Message sent.\n");
277                 } else {
278                         printf("%s\n", &buf[4]);
279                 }
280         }
281 }
282
283
284
285
286 void quiet_mode(void)
287 {
288         int qstate;
289         char buf[SIZ];
290
291         serv_puts("DEXP 2");
292         serv_gets(buf);
293         if (buf[0] != '2') {
294                 printf("%s\n", &buf[4]);
295                 return;
296         }
297         qstate = atoi(&buf[4]);
298         if (qstate == 0)
299                 qstate = 1;
300         else
301                 qstate = 0;
302         sprintf(buf, "DEXP %d", qstate);
303         serv_puts(buf);
304         serv_gets(buf);
305         if (buf[0] != '2') {
306                 printf("%s\n", &buf[4]);
307                 return;
308         }
309         qstate = atoi(&buf[4]);
310         if (qstate) {
311                 printf("Quiet mode enabled (no other users may page you)\n");
312         } else {
313                 printf("Quiet mode disabled (other users may page you)\n");
314         }
315 }