stringbuf.c: random idle style cleanup.
[citadel.git] / textclient / client_chat.c
1 // front end for multiuser chat
2 //
3 // Copyright (c) 1987-2016 by the citadel.org team
4 //
5 // This program is open source software.  Use, duplication, or disclosure is subject to the GNU General Public License version 3.
6
7 #include "textclient.h"
8
9 #define MIN(a, b) ((a) < (b) ? (a) : (b))
10
11 extern char temp[];
12 char last_paged[SIZ] = "";
13
14 void chatmode(CtdlIPC * ipc) {
15         char wbuf[SIZ];
16         char buf[SIZ];
17         char response[SIZ];
18         char c_user[SIZ];
19         char c_text[SIZ];
20         char last_user[SIZ];
21         int send_complete_line;
22         char ch;
23         int a, pos;
24         int seq = 0;
25
26         fd_set rfds;
27         struct timeval tv;
28         int retval;
29
30         CtdlIPC_chat_send(ipc, "RCHT enter");
31         CtdlIPC_chat_recv(ipc, buf);
32         if (buf[0] != '2') {
33                 scr_printf("%s\n", &buf[4]);
34                 return;
35         }
36         scr_printf("Entering chat mode (type /quit to exit)\n");
37
38         strcpy(buf, "");
39         strcpy(wbuf, "");
40         strcpy(last_user, "");
41         color(BRIGHT_YELLOW);
42         scr_printf("\n");
43         scr_printf("> ");
44         send_complete_line = 0;
45
46         while (1) {
47                 scr_flush();
48                 FD_ZERO(&rfds);
49                 FD_SET(0, &rfds);
50                 tv.tv_sec = 1;
51                 tv.tv_usec = 0;
52                 retval = select(1, &rfds, NULL, NULL, &tv);
53
54                 if (retval < 0) {
55                         color(BRIGHT_WHITE);
56                         scr_printf("Server gone Exiting chat mode\n");
57                         scr_flush();
58                         return;
59                 }
60
61                 /* If there's data from the keyboard... */
62                 if (FD_ISSET(0, &rfds)) {
63                         ch = scr_getc(SCR_BLOCK);
64                         if ((ch == 10) || (ch == 13)) {
65                                 send_complete_line = 1;
66                         }
67                         else if ((ch == 8) || (ch == 127)) {
68                                 if (!IsEmptyStr(wbuf)) {
69                                         wbuf[strlen(wbuf) - 1] = 0;
70                                         scr_printf("%c %c", 8, 8);
71                                 }
72                         }
73                         else {
74                                 scr_putc(ch);
75                                 wbuf[strlen(wbuf) + 1] = 0;
76                                 wbuf[strlen(wbuf)] = ch;
77                         }
78                 }
79
80                 /* if the user hit return, send the line */
81                 if (send_complete_line) {
82
83                         if (!strcasecmp(wbuf, "/quit")) {
84                                 CtdlIPC_chat_send(ipc, "RCHT exit");
85                                 CtdlIPC_chat_recv(ipc, response);       /* don't care about the result */
86                                 color(BRIGHT_WHITE);
87                                 scr_printf("\rExiting chat mode\n");
88                                 scr_flush();
89                                 return;
90                         }
91
92                         CtdlIPC_chat_send(ipc, "RCHT send");
93                         CtdlIPC_chat_recv(ipc, response);
94                         if (response[0] == '4') {
95                                 CtdlIPC_chat_send(ipc, wbuf);
96                                 CtdlIPC_chat_send(ipc, "000");
97                         }
98                         strcpy(wbuf, "");
99                         send_complete_line = 0;
100                 }
101
102                 /* if it's time to word wrap, send a partial line */
103                 if (strlen(wbuf) >= (77 - strlen(fullname))) {
104                         pos = 0;
105                         for (a = 0; !IsEmptyStr(&wbuf[a]); ++a) {
106                                 if (wbuf[a] == 32)
107                                         pos = a;
108                         }
109                         if (pos == 0) {
110                                 CtdlIPC_chat_send(ipc, "RCHT send");
111                                 CtdlIPC_chat_recv(ipc, response);
112                                 if (response[0] == '4') {
113                                         CtdlIPC_chat_send(ipc, wbuf);
114                                         CtdlIPC_chat_send(ipc, "000");
115                                 }
116                                 strcpy(wbuf, "");
117                                 send_complete_line = 0;
118                         }
119                         else {
120                                 wbuf[pos] = 0;
121                                 CtdlIPC_chat_send(ipc, "RCHT send");
122                                 CtdlIPC_chat_recv(ipc, response);
123                                 if (response[0] == '4') {
124                                         CtdlIPC_chat_send(ipc, wbuf);
125                                         CtdlIPC_chat_send(ipc, "000");
126                                 }
127                                 strcpy(wbuf, &wbuf[pos + 1]);
128                         }
129                 }
130
131                 /* poll for incoming chat messages */
132                 snprintf(buf, sizeof buf, "RCHT poll|%d", seq);
133                 CtdlIPC_chat_send(ipc, buf);
134                 CtdlIPC_chat_recv(ipc, response);
135
136                 if (response[0] == '1') {
137                         seq = extract_int(&response[4], 0);
138                         extract_token(c_user, &response[4], 2, '|', sizeof c_user);
139                         while (CtdlIPC_chat_recv(ipc, c_text), strcmp(c_text, "000")) {
140                                 scr_printf("\r%79s\r", "");
141                                 if (!strcmp(c_user, fullname)) {
142                                         color(BRIGHT_YELLOW);
143                                 }
144                                 else if (!strcmp(c_user, ":")) {
145                                         color(BRIGHT_RED);
146                                 }
147                                 else {
148                                         color(BRIGHT_GREEN);
149                                 }
150                                 if (strcmp(c_user, last_user)) {
151                                         snprintf(buf, sizeof buf, "%s: %s", c_user, c_text);
152                                 }
153                                 else {
154                                         size_t i = MIN(sizeof buf - 1, strlen(c_user) + 2);
155                                         memset(buf, ' ', i);
156                                         strncpy(&buf[i], c_text, sizeof buf - i);
157                                 }
158                                 while (strlen(buf) < 79) {
159                                         strcat(buf, " ");
160                                 }
161                                 if (strcmp(c_user, last_user)) {
162                                         scr_printf("\r%79s\n", "");
163                                         strcpy(last_user, c_user);
164                                 }
165                                 scr_printf("\r%s\n", buf);
166                                 scr_flush();
167                         }
168                 }
169                 color(BRIGHT_YELLOW);
170                 scr_printf("\r> %s", wbuf);
171                 scr_flush();
172                 strcpy(buf, "");
173         }
174 }
175
176
177 // send an instant message
178 void page_user(CtdlIPC * ipc) {
179         char buf[SIZ], touser[SIZ], msg[SIZ];
180         FILE *pagefp;
181
182         strcpy(touser, last_paged);
183         strprompt("Page who", touser, 30);
184
185         snprintf(buf, sizeof buf, "SEXP %s||", touser);
186         CtdlIPC_chat_send(ipc, buf);
187         CtdlIPC_chat_recv(ipc, buf);
188         if (buf[0] != '2') {
189                 scr_printf("%s\n", &buf[4]);
190                 return;
191         }
192         if (client_make_message(ipc, temp, touser, 0, 0, 0, NULL, 0) != 0) {
193                 scr_printf("No message sent.\n");
194                 return;
195         }
196         pagefp = fopen(temp, "r");
197         unlink(temp);
198         snprintf(buf, sizeof buf, "SEXP %s|-", touser);
199         CtdlIPC_chat_send(ipc, buf);
200         CtdlIPC_chat_recv(ipc, buf);
201         if (buf[0] == '4') {
202                 strcpy(last_paged, touser);
203                 while (fgets(buf, sizeof buf, pagefp) != NULL) {
204                         buf[strlen(buf) - 1] = 0;
205                         CtdlIPC_chat_send(ipc, buf);
206                 }
207                 fclose(pagefp);
208                 CtdlIPC_chat_send(ipc, "000");
209                 scr_printf("Message sent.\n");
210         }
211         else {
212                 scr_printf("%s\n", &buf[4]);
213         }
214 }
215
216
217 void quiet_mode(CtdlIPC * ipc) {
218         static int quiet = 0;
219         char cret[SIZ];
220         int r;
221
222         r = CtdlIPCEnableInstantMessageReceipt(ipc, !quiet, cret);
223         if (r / 100 == 2) {
224                 quiet = !quiet;
225                 scr_printf("Quiet mode %sabled (%sother users may page you)\n", (quiet) ? "en" : "dis", (quiet) ? "no " : "");
226         }
227         else {
228                 scr_printf("Unable to change quiet mode: %s\n", cret);
229         }
230 }
231
232
233 void stealth_mode(CtdlIPC * ipc) {
234         static int stealth = 0;
235         char cret[SIZ];
236         int r;
237
238         r = CtdlIPCStealthMode(ipc, !stealth, cret);
239         if (r / 100 == 2) {
240                 stealth = !stealth;
241                 scr_printf("Stealth mode %sabled (you are %s)\n",
242                            (stealth) ? "en" : "dis", (stealth) ? "invisible" : "listed as online");
243         }
244         else {
245                 scr_printf("Unable to change stealth mode: %s\n", cret);
246         }
247 }