~foura/wired

ref: 9147e8fd46653341e51a7c77f83fd42c06588d6a wired/main.c -rw-r--r-- 4.0 KiB
9147e8fdjames palmer prevent blank messages being sent. 1 year, 6 days ago
                                                                                
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
#include <u.h>
#include <libc.h>
#include <thread.h>
#include <acme.h>

char *srv = "tcp!9p.zone!9990";
char *mtpt = "/n/wired";
char *chan = "chat";
char *sep = "•";
char *nick;

void
connectsrv(void)
{
	int fd;

	fd = dial(srv, nil, nil, nil);
	if(fd < 0)
		sysfatal("dial: %r");
	if(mount(fd, -1, mtpt, MREPL, "") < 0)
		sysfatal("mount: %r");
}

AWin *
makewin(char *name, Channel *c)
{
	AWin *win;

	win = awincreate();
	win->aux = c;

	fprint(win->ctlfd, "name /wired/%s/%s\n", chan, name);
	fprint(win->ctlfd, "scratch\nnomenu\n");

	return win;
}

void
readproc(void *aux)
{
	AWin *win;
	char *file;
	char buf[8192];
	int fd;
	long n;

	threadsetname("read");

	win = aux;
	file = smprint("%s/%s", mtpt, chan);
	if(!file) {
		awincloseall();
		sysfatal("smprint failed");
	}

	fd = open(file, OREAD);
	if(fd < 0) {
		awincloseall();
		sysfatal("channel not found");
	}

	while((n = read(fd, buf, sizeof(buf))) > 0)
		write(win->bodyfd, buf, n);

	awincloseall();
	sysfatal("failed to read channel");
}

void
eventproc(void *aux)
{
	AWin *win;
	Channel *events;
	AEvent ev;

	threadsetname("event");

	win = aux;
	events = win->aux;
	win->eventfd = awinfsopen(win, "event", ORDWR);

	while(aeventnext(win, &ev)) {
		switch(ev.type) {
		case 'L':
		case 'l':
			aeventsend(win, &ev);
			break;
		default:
			send(events, &ev);
		}
	}

	threadexits(nil);
}

void
filter(char *s)
{
	int len;
	int i, j;

	/* strip eot */
	j = 0; len = strlen(s);
	for(i = 0; i < len; i++) {
		if(s[i] != '') {
			s[j] = s[i];
			j++;
		}
	}
	s[j] = 0;
	
	/* strip trailing nl */
	len = strlen(s);
	for(i = len-1; i > 0; i--)
		if(s[i] == '\n')
			s[i] = 0;
		else
			break;
}

void
writemessage(char *prefix, AWin *win)
{
	int fd;
	char *file;
	char buf[4096];
	long n;

	file = smprint("%s/%s", mtpt, chan);
	if(!file) {
		awincloseall();
		sysfatal("smprint failed");
	}

	fd = open(file, OWRITE);
	if(fd < 0) {
		awincloseall();
		sysfatal("couldn't write channel");
	}

	/* read the input window in 4096 byte chunks.
	 * if the message is too large it will be split awkwardly :(
	 * hopefully nobody needs a message that large */
	fprint(win->addrfd, ",");
	while((n = read(win->datafd, buf, sizeof(buf)-1)) > 0) {

		/* null terminate and filter */
		buf[n] = '\0'; 
		filter(buf); 

		/* no blank messages */
		if(buf[0] == 0)
			return;

		fprint(fd, "%s %s\n", prefix, buf);
	};


	/* clear: use of buf here means nothing, the write is 0 bytes.
	 * it is only there to prevent a crash due to a bad syscall. */
	fprint(win->addrfd, ",");
	write(win->datafd, buf, 0);
}

void
sendmessage(AWin *win)
{
	char *prefix;

	prefix = smprint("%s %s", nick, sep);
	if(!prefix) {
		awincloseall();
		sysfatal("smprint failed");
	}

	writemessage(prefix, win);
	free(prefix);
}

void
sendaction(AWin *win)
{
	char *prefix;
	
	prefix = smprint("* %s", nick);
	if(!prefix) {
		awincloseall();
		sysfatal("smprint failed");
	}
	
	writemessage(prefix, win);
	free(prefix);
}

void
usage(void)
{
	fprint(2, "usage: %s [-s srv] [-n nick] [channel...]", argv0);
	threadexitsall(nil);
}

void
threadmain(int argc, char *argv[])
{
	AWin *log, *input;
	Channel *events;
	AEvent ev;

	nick = getuser();

	ARGBEGIN {
	case 'n':
		nick = EARGF(usage());
		break;
	case 's':
		srv = EARGF(usage());
		break;
	case 'c':
		chan = EARGF(usage());
		break;
	} ARGEND

	connectsrv();
	events = chancreate(sizeof(AEvent), 0);

	log = makewin("log", events);
	input = makewin("input", events);
	
	awinsettag(input, " Send Act ");

	proccreate(readproc, log, 16*1024);
	proccreate(eventproc, log, 8*1024);
	proccreate(eventproc, input, 8*1024);

	while(recv(events, &ev)) {
		switch(ev.type) {
		case 'x':
		case 'X':
			if(strcmp(ev.text, "Del") == 0)
				goto quit;
			if(strcmp(ev.text, "Send") == 0)
				sendmessage(input);
			if(strcmp(ev.text, "Act") == 0)
				sendaction(input);

			break;
		case 'I':
			/* there is an EOT char here, it allows ctrl+d to send */
			if(strchr(ev.text, ''))
				sendmessage(input);
		}
	}

quit:
	unmount(nil, mtpt);
	awincloseall();
	threadexitsall(nil);
}