Fix more warnings; output errors to the users.
[citadel.git] / citadel / textclient / client_chat.c
1 /*
2  * front end for chat mode
3  * (the "single process" version - no more fork() anymore)
4  *
5  * Copyright (c) 1987-2010 by the citadel.org team
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include "sysdep.h"
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <stdio.h>
26 #include <fcntl.h>
27 #include <ctype.h>
28 #include <string.h>
29 #include <signal.h>
30 #include <errno.h>
31
32 #if TIME_WITH_SYS_TIME
33 # include <sys/time.h>
34 # include <time.h>
35 #else
36 # if HAVE_SYS_TIME_H
37 #  include <sys/time.h>
38 # else
39 #  include <time.h>
40 # endif
41 #endif
42
43 #include <sys/types.h>
44 #ifdef HAVE_SYS_SELECT_H
45 #include <sys/select.h>
46 #endif
47 #include <stdarg.h>
48 #include <libcitadel.h>
49 #include "citadel.h"
50 #include "citadel_ipc.h"
51 #include "client_chat.h"
52 #include "commands.h"
53 #include "routines.h"
54 #include "citadel_decls.h"
55 #include "rooms.h"
56 #include "messages.h"
57 #ifndef HAVE_SNPRINTF
58 #include "snprintf.h"
59 #endif
60 #include "screen.h"
61
62 #define MIN(a, b) ((a) < (b) ? (a) : (b))
63
64 extern char temp[];
65 void ctdl_getline(char *, int);
66
67
68 char last_paged[SIZ] = "";
69
70 void chatmode(CtdlIPC *ipc)
71 {
72         char wbuf[SIZ];
73         char buf[SIZ];
74         char response[SIZ];
75         char c_user[SIZ];
76         char c_text[SIZ];
77         char last_user[SIZ];
78         int send_complete_line;
79         char ch;
80         int a, pos;
81         int seq = 0;
82
83         fd_set rfds;
84         struct timeval tv;
85         int retval;
86
87         CtdlIPC_chat_send(ipc, "RCHT enter");
88         CtdlIPC_chat_recv(ipc, buf);
89         if (buf[0] != '2') {
90                 scr_printf("%s\n", &buf[4]);
91                 return;
92         }
93         scr_printf("Entering chat mode (type /quit to exit)\n");
94
95         strcpy(buf, "");
96         strcpy(wbuf, "");
97         strcpy(last_user, ""); 
98         color(BRIGHT_YELLOW);
99         scr_printf("\n");
100         scr_printf("> ");
101         send_complete_line = 0;
102
103         while (1) {
104                 scr_flush();
105                 FD_ZERO(&rfds);
106                 FD_SET(0, &rfds);
107                 tv.tv_sec = 1;
108                 tv.tv_usec = 0;
109                 retval = select(1, &rfds, NULL, NULL, &tv);
110
111                 if (retval < 0) {
112                         color(BRIGHT_WHITE);
113                         scr_printf("Server gone Exiting chat mode\n");
114                         scr_flush();
115                         return;
116                 }
117
118                 /* If there's data from the keyboard... */
119                 if (FD_ISSET(0, &rfds)) {
120                         ch = scr_getc(SCR_BLOCK);
121                         if ((ch == 10) || (ch == 13)) {
122                                 send_complete_line = 1;
123                         } else if ((ch == 8) || (ch == 127)) {
124                                 if (!IsEmptyStr(wbuf)) {
125                                         wbuf[strlen(wbuf) - 1] = 0;
126                                         scr_printf("%c %c", 8, 8);
127                                 }
128                         } else {
129                                 scr_putc(ch);
130                                 wbuf[strlen(wbuf) + 1] = 0;
131                                 wbuf[strlen(wbuf)] = ch;
132                         }
133                 }
134
135                 /* if the user hit return, send the line */
136                 if (send_complete_line) {
137
138                         if (!strcasecmp(wbuf, "/quit")) {
139                                 CtdlIPC_chat_send(ipc, "RCHT exit");
140                                 CtdlIPC_chat_recv(ipc, response);       /* don't care about the result */
141                                 color(BRIGHT_WHITE);
142                                 scr_printf("\rExiting chat mode\n");
143                                 scr_flush();
144                                 return;
145                         }
146
147                         CtdlIPC_chat_send(ipc, "RCHT send");
148                         CtdlIPC_chat_recv(ipc, response);
149                         if (response[0] == '4') {
150                                 CtdlIPC_chat_send(ipc, wbuf);
151                                 CtdlIPC_chat_send(ipc, "000");
152                         }
153                         strcpy(wbuf, "");
154                         send_complete_line = 0;
155                 }
156
157                 /* if it's time to word wrap, send a partial line */
158                 if (strlen(wbuf) >= (77 - strlen(fullname))) {
159                         pos = 0;
160                         for (a = 0; !IsEmptyStr(&wbuf[a]); ++a) {
161                                 if (wbuf[a] == 32)
162                                         pos = a;
163                         }
164                         if (pos == 0) {
165                                 CtdlIPC_chat_send(ipc, "RCHT send");
166                                 CtdlIPC_chat_recv(ipc, response);
167                                 if (response[0] == '4') {
168                                         CtdlIPC_chat_send(ipc, wbuf);
169                                         CtdlIPC_chat_send(ipc, "000");
170                                 }
171                                 strcpy(wbuf, "");
172                                 send_complete_line = 0;
173                         } else {
174                                 wbuf[pos] = 0;
175                                 CtdlIPC_chat_send(ipc, "RCHT send");
176                                 CtdlIPC_chat_recv(ipc, response);
177                                 if (response[0] == '4') {
178                                         CtdlIPC_chat_send(ipc, wbuf);
179                                         CtdlIPC_chat_send(ipc, "000");
180                                 }
181                                 strcpy(wbuf, &wbuf[pos + 1]);
182                         }
183                 }
184
185                 /* poll for incoming chat messages */
186                 snprintf(buf, sizeof buf, "RCHT poll|%d", seq);
187                 CtdlIPC_chat_send(ipc, buf);
188                 CtdlIPC_chat_recv(ipc, response);
189         
190                 if (response[0] == '1') {
191                         seq = extract_int(&response[4], 0);
192                         extract_token(c_user, &response[4], 2, '|', sizeof c_user);
193                         while (CtdlIPC_chat_recv(ipc, c_text), strcmp(c_text, "000")) {
194                                 scr_printf("\r%79s\r", "");
195                                 if (!strcmp(c_user, fullname)) {
196                                         color(BRIGHT_YELLOW);
197                                 } else if (!strcmp(c_user, ":")) {
198                                         color(BRIGHT_RED);
199                                 } else {
200                                         color(BRIGHT_GREEN);
201                                 }
202                                 if (strcmp(c_user, last_user)) {
203                                         snprintf(buf, sizeof buf, "%s: %s", c_user, c_text);
204                                 } else {
205                                         size_t i = MIN(sizeof buf - 1, strlen(c_user) + 2);
206                                         memset(buf, ' ', i);
207                                         safestrncpy(&buf[i], c_text, sizeof buf - i);
208                                 }
209                                 while (strlen(buf) < 79) {
210                                         strcat(buf, " ");
211                                 }
212                                 if (strcmp(c_user, last_user)) {
213                                         scr_printf("\r%79s\n", "");
214                                         strcpy(last_user, c_user);
215                                 }
216                                 scr_printf("\r%s\n", buf);
217                                 scr_flush();
218                         }
219                 }
220                 color(BRIGHT_YELLOW);
221                 scr_printf("\r> %s", wbuf);
222                 scr_flush();
223                 strcpy(buf, "");
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 }