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