~foura/wired

ref: 783a075cd2cec169631759a815deaf3717edb925 wired/main.c -rw-r--r-- 3.5 KiB
783a075c — james palmer fix segfault if the input box was empty 1 year, 3 months 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
#include <u.h>
#include <libc.h>
#include <bio.h>
#include <thread.h>
#include <acme.h>

typedef struct Session Session;
struct Session {
	AWin *cwin;
	AWin *iwin;
	
	char *mtpt;
	char *chan;
	char *usr;
	
	Channel *ctl;
	
	int fd;
};

enum { Stack = 4*1024, };

void
chatcatproc(void *aux)
{
	Session *s;
	AWin *w;
	
	w = aux;
	s = w->aux;
	
	awincat(w, s->fd);
}

void
wineventproc(void *aux)
{
	AWin *w;
	AEvent ev;
	Session *s;
	Channel *ctl;
	Channel *eventc;
	
	w = aux;
	s = w->aux;
	ctl = s->ctl;
	eventc = aeventlisten(w);
	
	while(recv(eventc, &ev)) {
		switch(ev.type) {
		case 'x':
			if(strcmp(ev.text, "Del") == 0)
				sendp(ctl, "quit");
			if(strcmp(ev.text, "Send") == 0)
				sendp(ctl, "send");
			if(strcmp(ev.text, "Action") == 0)
				sendp(ctl, "act");
				
			break;
		case 'I':
			if(strcmp(ev.text, "") == 0)
				sendp(ctl, "send");
				
			break;
		case 'l':
		case 'L':
			aeventsend(w, &ev);
		}
	}	
}

char *
readbody(AWin *w)
{
	Biobuf *fd;
	char *buf;
	int i;
	
	fd = awinfsbopen(w, "body", OREAD);
	buf = Brdstr(fd, '\0', 0);
	Bterm(fd);
	
	awinclear(w);
	
	if(!buf)
		return nil;
	
	for(i = strlen(buf)-1; i > 0; i--) {
		if(buf[i] == '' || buf[i] == '\n')
			buf[i] = '\0';
		else
			break;
	}
	
	return buf;
}

void
domessage(Session *s, char *msg)
{
	char *buf;
	
	if(!msg)
		return;
	
	buf = smprint("%s • %s\n", s->usr, msg);
	write(s->fd, buf, strlen(buf));
	
	free(buf);
	free(msg);
}

void
doaction(Session *s, char *msg)
{
	char *buf;
	
	if(!msg)
		return;
	
	buf = smprint("* %s %s\n", s->usr, msg);
	write(s->fd, buf, strlen(buf));
	
	free(buf);
	free(msg);
}

void
dochat(char *mtpt, char *chan, char *usr)
{
	Session *s;
	char buf[128];
	char *ctl;
	
	s = mallocz(sizeof(Session), 1);

	s->mtpt = strdup(mtpt);
	s->chan = strdup(chan);
	s->usr = strdup(usr);
	
	snprint(buf, sizeof(buf), "%s/%s", s->mtpt, s->chan);
	s->fd = open(buf, ORDWR);
	if(s->fd < 0) {
		free(s->mtpt);
		free(s->chan);
		free(s->usr);
		free(s);
		
		sysfatal("couldn't read channel: %r");
	};
		
	s->cwin = awincreate();
	awinctl(s->cwin, "name /wired/chat\n");
	awinctl(s->cwin, "scratch\n");
	awinctl(s->cwin, "nomenu\n");
	s->cwin->aux = s;
	
	s->iwin = awincreate();
	awinctl(s->iwin, "name /wired/input\n");
	awinctl(s->iwin, "scratch\n");
	awinctl(s->iwin, "nomenu\n");
	awinsettag(s->iwin, " Send Action ");
	s->iwin->aux = s;
	
	s->ctl = chancreate(sizeof(char*), 0);
	
	proccreate(chatcatproc, s->cwin, Stack);
	proccreate(wineventproc, s->cwin, Stack);
	proccreate(wineventproc, s->iwin, Stack);
	
	while(ctl = recvp(s->ctl)) {
		if(strcmp(ctl, "send") == 0)
			domessage(s, readbody(s->iwin));
		if(strcmp(ctl, "act") == 0)
			doaction(s, readbody(s->iwin));
		if(strcmp(ctl, "quit") == 0)
			break;
	}
}

void
dialchat(char *srv, char *mtpt)
{
	int fd;
	
	if((fd = dial(srv, nil, nil, nil)) < 0)
		sysfatal("couldn't dial chat server: %r");
	if(mount(fd, -1, mtpt, MREPL, "") == -1)
		sysfatal("couldn't mount chat server: %r");
}

void
usage(void)
{
	fprint(2, "usage: %s [-s srv] [-c chan] [-u user]\n", argv0);
	exits(nil);
}

void
threadmain(int argc, char *argv[])
{
	char *srv, *mtpt, *user, *chan;
	
	srv = "tcp!chat.9p.zone!9990";
	mtpt = "/n/chat";
	chan = "chat";
	user = getuser();
	
	ARGBEGIN {
	case 's':
		srv = EARGF(usage());
		break;
	case 'm':
		mtpt = EARGF(usage());
		break;
	case 'c':
		chan = EARGF(usage());
		break;
	case 'u':
		user = EARGF(usage());
		break;
	default:
		usage();
	} ARGEND
	
	dialchat(srv, mtpt);
	dochat(mtpt, chan, user);
	
	awincloseall();
	threadexitsall(nil);
}