File: client_chat.c

package info (click to toggle)
citadel 7.83-2squeeze2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 4,572 kB
  • ctags: 3,756
  • sloc: ansic: 54,870; sh: 4,298; yacc: 660; makefile: 450; xml: 40
file content (313 lines) | stat: -rw-r--r-- 7,472 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
 * $Id: client_chat.c 8504 2010-04-08 04:33:45Z ajc $
 *
 * front end for chat mode
 * (the "single process" version - no more fork() anymore)
 *
 * Copyright (c) 1987-2010 by the citadel.org team
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "sysdep.h"
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <ctype.h>
#include <string.h>
#include <signal.h>
#include <errno.h>

#if TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# if HAVE_SYS_TIME_H
#  include <sys/time.h>
# else
#  include <time.h>
# endif
#endif

#include <sys/types.h>
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
#include <stdarg.h>
#include <libcitadel.h>
#include "citadel.h"
#include "citadel_ipc.h"
#include "client_chat.h"
#include "commands.h"
#include "routines.h"
#include "citadel_decls.h"
#include "rooms.h"
#include "messages.h"
#ifndef HAVE_SNPRINTF
#include "snprintf.h"
#endif
#include "screen.h"

#define MIN(a, b) ((a) < (b) ? (a) : (b))

extern char temp[];
void ctdl_getline(char *, int);


char last_paged[SIZ] = "";

void chatmode(CtdlIPC *ipc)
{
	char wbuf[SIZ];
	char buf[SIZ];
	char response[SIZ];
	char c_user[SIZ];
	char c_text[SIZ];
	char last_user[SIZ];
	int send_complete_line;
	char ch;
	int a, pos;
	int seq = 0;

	fd_set rfds;
	struct timeval tv;
	int retval;

	CtdlIPC_chat_send(ipc, "RCHT enter");
	CtdlIPC_chat_recv(ipc, buf);
	if (buf[0] != '2') {
		scr_printf("%s\n", &buf[4]);
		return;
	}
	scr_printf("Entering chat mode (type /quit to exit)\n");

	strcpy(buf, "");
	strcpy(wbuf, "");
	strcpy(last_user, ""); 
	color(BRIGHT_YELLOW);
	sln_printf_if("\n");
	sln_printf("> ");
	send_complete_line = 0;

	while (1) {
		sln_flush();
		FD_ZERO(&rfds);
		FD_SET(0, &rfds);
		tv.tv_sec = 1;
		tv.tv_usec = 0;
		retval = select(1, &rfds, NULL, NULL, &tv);

		/* If there's data from the keyboard... */
		if (FD_ISSET(0, &rfds)) {
			ch = scr_getc(SCR_BLOCK);
			if ((ch == 10) || (ch == 13)) {
				send_complete_line = 1;
			} else if ((ch == 8) || (ch == 127)) {
				if (!IsEmptyStr(wbuf)) {
					wbuf[strlen(wbuf) - 1] = 0;
					sln_printf("%c %c", 8, 8);
				}
			} else {
				sln_putc(ch);
				wbuf[strlen(wbuf) + 1] = 0;
				wbuf[strlen(wbuf)] = ch;
			}
		}

		/* if the user hit return, send the line */
		if (send_complete_line) {

			if (!strcasecmp(wbuf, "/quit")) {
				CtdlIPC_chat_send(ipc, "RCHT exit");
				CtdlIPC_chat_recv(ipc, response);	/* don't care about the result */
				color(BRIGHT_WHITE);
				sln_printf("\rExiting chat mode\n");
				sln_flush();
				return;
			}

			CtdlIPC_chat_send(ipc, "RCHT send");
			CtdlIPC_chat_recv(ipc, response);
			if (response[0] == '4') {
				CtdlIPC_chat_send(ipc, wbuf);
				CtdlIPC_chat_send(ipc, "000");
			}
			strcpy(wbuf, "");
			send_complete_line = 0;
		}

		/* if it's time to word wrap, send a partial line */
		if (strlen(wbuf) >= (77 - strlen(fullname))) {
			pos = 0;
			for (a = 0; !IsEmptyStr(&wbuf[a]); ++a) {
				if (wbuf[a] == 32)
					pos = a;
			}
			if (pos == 0) {
				CtdlIPC_chat_send(ipc, "RCHT send");
				CtdlIPC_chat_recv(ipc, response);
				if (response[0] == '4') {
					CtdlIPC_chat_send(ipc, wbuf);
					CtdlIPC_chat_send(ipc, "000");
				}
				strcpy(wbuf, "");
				send_complete_line = 0;
			} else {
				wbuf[pos] = 0;
				CtdlIPC_chat_send(ipc, "RCHT send");
				CtdlIPC_chat_recv(ipc, response);
				if (response[0] == '4') {
					CtdlIPC_chat_send(ipc, wbuf);
					CtdlIPC_chat_send(ipc, "000");
				}
				strcpy(wbuf, &wbuf[pos + 1]);
			}
		}

		/* poll for incoming chat messages */
		snprintf(buf, sizeof buf, "RCHT poll|%d", seq);
		CtdlIPC_chat_send(ipc, buf);
		CtdlIPC_chat_recv(ipc, response);
	
		if (response[0] == '1') {
			seq = extract_int(&response[4], 0);
			extract_token(c_user, &response[4], 2, '|', sizeof c_user);
			while (CtdlIPC_chat_recv(ipc, c_text), strcmp(c_text, "000")) {
				sln_printf("\r%79s\r", "");
				if (!strcmp(c_user, fullname)) {
					color(BRIGHT_YELLOW);
				} else if (!strcmp(c_user, ":")) {
					color(BRIGHT_RED);
				} else {
					color(BRIGHT_GREEN);
				}
				if (strcmp(c_user, last_user)) {
					snprintf(buf, sizeof buf, "%s: %s", c_user, c_text);
				} else {
					size_t i = MIN(sizeof buf - 1, strlen(c_user) + 2);
					memset(buf, ' ', i);
					safestrncpy(&buf[i], c_text, sizeof buf - i);
				}
				while (strlen(buf) < 79) {
					strcat(buf, " ");
				}
				if (strcmp(c_user, last_user)) {
					sln_printf("\r%79s\n", "");
					strcpy(last_user, c_user);
				}
				scr_printf("\r%s\n", buf);
				scr_flush();
			}
		}
		color(BRIGHT_YELLOW);
		sln_printf("\r> %s", wbuf);
		sln_flush();
		strcpy(buf, "");
	}
}


/*
 * send an instant message
 */
void page_user(CtdlIPC *ipc)
{
	char buf[SIZ], touser[SIZ], msg[SIZ];
	FILE *pagefp;

	strcpy(touser, last_paged);
	strprompt("Page who", touser, 30);

	/* old server -- use inline paging */
	if (ipc->ServInfo.paging_level == 0) {
		newprompt("Message: ", msg, 69);
		snprintf(buf, sizeof buf, "SEXP %s|%s", touser, msg);
		CtdlIPC_chat_send(ipc, buf);
		CtdlIPC_chat_recv(ipc, buf);
		if (!strncmp(buf, "200", 3)) {
			strcpy(last_paged, touser);
		}
		scr_printf("%s\n", &buf[4]);
		return;
	}
	/* new server -- use extended paging */
	else if (ipc->ServInfo.paging_level >= 1) {
		snprintf(buf, sizeof buf, "SEXP %s||", touser);
		CtdlIPC_chat_send(ipc, buf);
		CtdlIPC_chat_recv(ipc, buf);
		if (buf[0] != '2') {
			scr_printf("%s\n", &buf[4]);
			return;
		}
		if (client_make_message(ipc, temp, touser, 0, 0, 0, NULL, 0) != 0) {
			scr_printf("No message sent.\n");
			return;
		}
		pagefp = fopen(temp, "r");
		unlink(temp);
		snprintf(buf, sizeof buf, "SEXP %s|-", touser);
		CtdlIPC_chat_send(ipc, buf);
		CtdlIPC_chat_recv(ipc, buf);
		if (buf[0] == '4') {
			strcpy(last_paged, touser);
			while (fgets(buf, sizeof buf, pagefp) != NULL) {
				buf[strlen(buf) - 1] = 0;
				CtdlIPC_chat_send(ipc, buf);
			}
			fclose(pagefp);
			CtdlIPC_chat_send(ipc, "000");
			scr_printf("Message sent.\n");
		} else {
			scr_printf("%s\n", &buf[4]);
		}
	}
}


void quiet_mode(CtdlIPC *ipc)
{
	static int quiet = 0;
	char cret[SIZ];
	int r;

	r = CtdlIPCEnableInstantMessageReceipt(ipc, !quiet, cret);
	if (r / 100 == 2) {
		quiet = !quiet;
		scr_printf("Quiet mode %sabled (%sother users may page you)\n",
				(quiet) ? "en" : "dis",
				(quiet) ? "no " : "");
	} else {
		scr_printf("Unable to change quiet mode: %s\n", cret);
	}
}


void stealth_mode(CtdlIPC *ipc)
{
	static int stealth = 0;
	char cret[SIZ];
	int r;

	r = CtdlIPCStealthMode(ipc, !stealth, cret);
	if (r / 100 == 2) {
		stealth = !stealth;
		scr_printf("Stealth mode %sabled (you are %s)\n",
				(stealth) ? "en" : "dis",
				(stealth) ? "invisible" : "listed as online");
	} else {
		scr_printf("Unable to change stealth mode: %s\n", cret);
	}
}