6d9261e3216094c7454252e305b2aef9251ca7b0
[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         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(getsockfd(), &rfds);
98                 tv.tv_sec = S_KEEPALIVE;
99                 tv.tv_usec = 0;
100                 retval = select(getsockfd() + 1, &rfds, NULL, NULL, &tv);
101
102                 if (FD_ISSET(getsockfd(), &rfds)) {
103                         ch = serv_getc();
104                         if (ch == 10) {
105                                 recv_complete_line = 1;
106                                 goto RCL;       /* ugly, but we've gotta get out! */
107                         } else {
108                                 buf[strlen(buf) + 1] = 0;
109                                 buf[strlen(buf)] = ch;
110                         }
111                         goto RCL;
112                 }
113                 if (FD_ISSET(0, &rfds)) {
114                         ch = scr_getc();
115                         if ((ch == 10) || (ch == 13)) {
116                                 send_complete_line = 1;
117                         } else if ((ch == 8) || (ch == 127)) {
118                                 if (strlen(wbuf) > 0) {
119                                         wbuf[strlen(wbuf) - 1] = 0;
120                                         sln_printf("%c %c", 8, 8);
121                                 }
122                         } else {
123                                 sln_putc(ch);
124                                 wbuf[strlen(wbuf) + 1] = 0;
125                                 wbuf[strlen(wbuf)] = ch;
126                         }
127                 }
128                 /* if the user hit return, send the line */
129               RCL:if (send_complete_line) {
130                         serv_puts(wbuf);
131                         last_transmit = time(NULL);
132                         strcpy(wbuf, "");
133                         send_complete_line = 0;
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                                 serv_puts(wbuf);
144                                 last_transmit = time(NULL);
145                                 strcpy(wbuf, "");
146                                 send_complete_line = 0;
147                         } else {
148                                 wbuf[pos] = 0;
149                                 serv_puts(wbuf);
150                                 last_transmit = time(NULL);
151                                 strcpy(wbuf, &wbuf[pos + 1]);
152                         }
153                 }
154                 if (recv_complete_line) {
155                         sln_printf("\r%79s\r", "");
156                         if (!strcmp(buf, "000")) {
157                                 color(BRIGHT_WHITE);
158                                 sln_printf("\rExiting chat mode\n");
159                                 sln_flush();
160                                 set_keepalives(KA_YES);
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                                         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                         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, NULL) != 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 }