summaryrefslogtreecommitdiff
path: root/appl/spree/clients/lobby.b
blob: 1af52827d227646534620ad1574d8f7dd5f7aee0 (plain)
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
implement Lobby;

include "sys.m";
	sys: Sys;
include "draw.m";
	draw: Draw;
	Point, Rect, Display, Image, Font: import draw;
include "tk.m";
	tk: Tk;
include "tkclient.m";
	tkclient: Tkclient;
include "../join.m";
	join: Join;
include "dividers.m";
	dividers: Dividers;
	Divider: import dividers;
include "commandline.m";
	commandline: Commandline;
	Cmdline: import commandline;
include "sh.m";

Lobby: module {
	init: fn(ctxt: ref Draw->Context, argv: list of string);
};

CLIENTDIR: con "/dis/spree/clients";
NAMEFONT: con "/fonts/charon/plain.small.font";
TITLEFONT: con "/fonts/charon/bold.normal.font";
HEADERFONT: con "/fonts/charon/italic.normal.font";

Object: adt {
	id:	int;
	pick {
	Session =>
		filename:		string;
		owner:		string;
		invitations: 	list of string;
		members:		list of string;
		invited:		int;
	Sessiontype =>
		start:			string;
		name:		string;
		title:			string;
		clienttype:	string;
	Invite =>
		session:		ref Object.Session;
		name:		string;
	Member =>
		parentid:		int;
		name:		string;
	Archive =>
	Other =>
	}
};

drawctxt: ref Draw->Context;
cliquefd: ref Sys->FD;
objects: array of ref Object;
myname: string;
maxid := 0;

badmodule(m: string)
{
	sys->fprint(sys->fildes(2), "lobby: cannot load %s: %r\n", m);
	raise "fail:bad module";
}

init(ctxt: ref Draw->Context, nil: list of string)
{
	sys = load Sys Sys->PATH;
	draw = load Draw Draw->PATH;
	tk = load Tk Tk->PATH;

	tkclient = load Tkclient Tkclient->PATH;
	if (tkclient == nil)
		badmodule(Tkclient->PATH);
	tkclient->init();

	commandline = load Commandline Commandline->PATH;
	if(commandline == nil)
		badmodule(Commandline->PATH);
	commandline->init();

	dividers = load Dividers Dividers->PATH;
	if (dividers == nil)
		badmodule(Dividers->PATH);
	dividers->init();

	join = load Join Join->PATH;
	if (join == nil)
		badmodule(Join->PATH);

	drawctxt = ctxt;
	cliquefd = sys->fildes(0);
	sys->pctl(Sys->NEWPGRP, nil);
	client1();
}

columns := array[] of {("name", ""), ("members", ""), ("watch", "Watch"), ("join", "Join"), ("invite", "Invite")};

reqwidth(win: ref Tk->Toplevel, w: string): int
{
	return 2 * int cmd(win, w + " cget -bd") + int cmd(win, w + " cget -width");
}

client1()
{
	(win, winctl) := tkclient->toplevel(drawctxt, nil, "Lobby", Tkclient->Appl);
	ech := chan of string;
	tk->namechan(win, ech, "e");
	(chat, chatevent) := Cmdline.new(win, ".d2", nil);
	updatech := chan of list of string;
	spawn readproc(updatech);

	cmd(win, "frame .buts");
	cmd(win, "menubutton .buts.start -text New -menu .buts.start.m");
	cmd(win, "menu .buts.start.m");
	cmd(win, "pack .buts.start -side left");
	cmd(win, "button .buts.kick -text Kick -command {send e kick}");
	cmd(win, "pack .buts.kick -side left");
	cmd(win, "pack .buts -side top -fill x");

	cmd(win, "frame .d1");

	cmd(win, "scrollbar .d1.s -orient vertical -command {.d1.c yview}");
	cmd(win, "canvas .d1.c -yscrollcommand {.d1.s set}");
	cmd(win, "pack .d1.s -side left -fill y");
	cmd(win, "pack .d1.c -side top -fill both -expand 1");
	cmd(win, "frame .t");
	cmd(win, ".d1.c create window 0 0 -anchor nw -window .t");
	cmd(win, "frame .t.f1 -bd 2 -relief sunken");
	cmd(win, "pack .t.f1 -side top -fill both -expand 1");

	cmd(win, "label .t.f1.sessionlabel -text Sessions -font " + TITLEFONT);
	cmd(win, "pack .t.f1.sessionlabel");
	cmd(win, "frame .t.s");
	cmd(win, "pack .t.s -in .t.f1 -side top -fill both -expand 1");

	cmd(win, "frame .t.f2 -bd 2 -relief sunken");
	cmd(win, "label .t.archiveslabel -text Archives -font " + TITLEFONT);
	cmd(win, "pack .t.archiveslabel");
	cmd(win, "frame .t.a");
	cmd(win, "pack .t.a -in .t.f2 -side top -fill both -expand 1 -anchor w");
	cmd(win, "pack .t.f2 -side top -fill both -expand 1");

	cmd(win, "label .t.a.title0 -text Title -font " + HEADERFONT);
	cmd(win, "label .t.a.title1 -text Members -font " + HEADERFONT);
	cmd(win, "grid .t.a.title0 .t.a.title1 -sticky w");
	cmd(win, "grid columnconfigure .t.a 1 -weight 1");

	cmd(win, "bind .t <Configure> {.d1.c configure -scrollregion {0 0 [.t cget -width] [.t cget -height]}}");

	cmd(win, "button .tmp");
	for (i := 0; i < len columns; i++) {
		(name, mintext) := columns[i];
		cmd(win, ".tmp configure -text '" + mintext);
		cmd(win, "grid columnconfigure .t.s " + string i +
			" -name " + name +
			" -minsize " + string reqwidth(win, ".tmp"));
	}
	cmd(win, "grid columnconfigure .t.s members -weight 1");
	cmd(win, "destroy .tmp");
	cmd(win, "menu .invite");

	(divider, dividerevent) := Divider.new(win, ".d", ".d1" :: ".d2" :: nil, Dividers->NS);
	cmd(win, "pack .d -side top -fill both");
	cmd(win, "pack propagate . 0");
	tkclient->onscreen(win, nil);
	tkclient->startinput(win, "kbd"::"ptr"::nil);
	for (;;) {
		alt {
		s := <-win.ctxt.kbd =>
			tk->keyboard(win, s);
		s := <-win.ctxt.ptr =>
			tk->pointer(win, *s);
		s := <-win.ctxt.ctl or
		s = <-win.wreq or
		s = <-winctl =>
			tkclient->wmctl(win, s);
		c := <-dividerevent =>
			divider.event(c);
		c := <-chatevent =>
			lines := chat.event(c);
			for (; lines != nil; lines = tl lines) {
				line := hd lines;
				if (len line > 0 && line[len line-1]=='\n')
					line = line[0:len line-1];
				cliquecmd("chat " + line);
			}
		lines := <-updatech =>
#sys->print("++\n");
			for (; lines != nil; lines = tl lines) {
#sys->print("+%s\n", hd lines);
				doupdate(win, chat, hd lines);
			}
			cmd(win, "update");
		c := <-ech =>
			(n, toks) := sys->tokenize(c, " ");
			case hd toks {
			"watch" =>
				joinclique(win, chat, int hd tl toks, "watch");
			"join" =>
				joinclique(win, chat, int hd tl toks, "join");
			"start" =>
				start(win, chat, int hd tl toks);
			"postinvite" =>
				postinvite(win, int hd tl toks, hd tl tl toks);
			"unarchive" =>
				e := cliquecmd("unarchive " + hd tl toks);
				if (e != nil)
					chat.addtext("failed to unarchive: " + e + "\n");
			"invite" =>
				# invite sessionid name
				(id, name) := (hd tl toks, hd tl tl toks);
				vname := "inv." + name;
				v := int cmd(win, "variable " + vname);
				s := "invite";
				if (!v)
					s = "uninvite";
				e := cliquecmd(s + " " + string id + " " + name);
				if (e != nil) {
					chat.addtext("invite failed: " + e + "\n");
					cmd(win, "variable " + vname + " " + string !v);
				}
			"kick" =>
				e := cliquecmd("kick");
				if (e != nil)
					chat.addtext("kick failed: " + e + "\n");
			* =>
				sys->print("unknown msg %s\n", c);
			}
			cmd(win, "update");
		}
	}
}

joinclique(nil: ref Tk->Toplevel, chat: ref Cmdline, id: int, how: string)
{
	pick o := objects[id] {
	Session =>
		e := join->join(drawctxt, "/n/remote", o.filename, how);
		if (e != nil)
			chat.addtext("couldn't join clique: " + e + "\n");
		else
			chat.addtext("joined clique ok\n");
	* =>
		sys->print("join bad id %d (type %d)\n", id, tagof objects[id]);
	}
}

start(nil: ref Tk->Toplevel, chat: ref Cmdline, id: int)
{
	pick o := objects[id] {
	Sessiontype =>
		e := cliquecmd("start " + o.start);
		if (e != nil)
			chat.addtext("failed to start clique: " + e + "\n");
	* =>
		sys->print("start bad id %d (type %d)\n", id, tagof objects[id]);
	}
}

postinvite(win: ref Tk->Toplevel, id: int, widget: string)
{
	pick o := objects[id] {
	Session =>
		cmd(win, ".invite delete 0 end");
		cmd(win, ".invite add checkbutton -text All -variable inv.all -command {send e invite " + string id + " all}");
		for (invites := o.invitations; invites != nil; invites = tl invites)
			if (hd invites == "all")
				break;
		cmd(win, "variable inv.all " + string (invites != nil));

		for (i := 0; i < len objects; i++) {
			if (objects[i] == nil)
				continue;
			pick p := objects[i] {
			Member =>
				if (tagof(objects[p.parentid]) != tagof(Object.Session) && p.name != o.owner) {
					for (invites = o.invitations; invites != nil; invites = tl invites)
						if (hd invites == p.name)
							break;
					invited := invites != nil;
					cmd(win, "variable inv." + p.name + " " + string invited);
					cmd(win, ".invite add checkbutton -variable inv." + p.name +
						" -command {send e invite " + string id + " " + p.name + "}" +
						" -text '" + p.name);
				}
			}
		}
		x := int cmd(win, widget + " cget -actx");
		y := int cmd(win, widget + " cget -acty");
		h := 2 * int cmd(win, widget + " cget -bd") + int cmd(win, widget + " cget -actheight");
		cmd(win, ".invite post " + string x + " " + string (y + h));
	* =>
		sys->print("bad invited id %d (type %d)\n", id, tagof objects[id]);
	}
}

panic(s: string)
{
	sys->print("lobby panic: %s\n", s);
	raise "panic";
}

doupdate(win: ref Tk->Toplevel, chat: ref Cmdline, line: string)
{
	(n, toks) := sys->tokenize(line, " ");
	if (n == 0)
		return;
	case hd toks {
	"chat" =>
		chat.addtext(sys->sprint("%s: %s\n", hd tl toks, concat(tl tl toks)));
	"create" =>
		# create id parentid vis type
		id := int hd tl toks;
		if (id >= len objects)
			objects = (array[len objects + 10] of ref Object)[0:] = objects;
		if (objects[id] != nil)
			panic(sys->sprint("object %d already exists!", id));
		parentid := int hd tl tl toks;
		objtype := tl tl tl tl toks;
		o: ref Object;
		case hd objtype {
		"sessiontype" =>
			o = ref Object.Sessiontype(id, nil, nil, nil, nil);
		"session" =>
			cmd(win, "grid rowinsert .t.s 0");
			cmd(win, "grid rowconfigure .t.s 0 -name id" + string id);
			f := ".t.s.f" + string id;
			cmd(win, "frame " + f);			# dummy, so we can destroy row easily
			cmd(win, "label "+f+".name");
			cmd(win, "grid "+f+".name -row id" + string id + " -column name -in .t.s");
			cmd(win, "button "+f+".watch -text Watch -command {send e watch " + string id + "}");
			cmd(win, "grid "+f+".watch -row id" + string id + " -column watch -in .t.s");
			cmd(win, "label "+f+".members -font " + NAMEFONT);
			cmd(win, "grid "+f+".members -row id" + string id + " -column members -in .t.s");
			o = ref Object.Session(id, nil, nil, nil, nil, 0);
		"member" =>
			o = ref Object.Member(id, parentid, nil);
		"invite" =>
			pick parent := objects[parentid] {
			Session =>
				o = ref Object.Invite(id, parent, nil);
			* =>
				panic("invite not under session");
			}
		"archive" =>
			cmd(win, "grid rowinsert .t.a 1");
			cmd(win, "grid rowconfigure .t.a 1 -name id" + string id);
			f := ".t.a.f" + string id;
			cmd(win, "frame " + f);
			cmd(win, "label "+f+".name");
			cmd(win, "grid "+f+".name -row id" + string id + " -column 0 -in .t.a -sticky w");
			cmd(win, "label "+f+".members -anchor w -font " + NAMEFONT);
			cmd(win, "grid "+f+".members -row id" + string id + " -column 1 -in .t.a -sticky ew");
			cmd(win, "button "+f+".unarchive -text Unarchive -command {send e unarchive " + string id + "}");
			cmd(win, "grid "+f+".unarchive -row id" + string id + " -column 2 -in .t.a");
			o = ref Object.Archive(id);
		* =>
			o = ref Object.Other(id);
		}
		objects[id] = o;

	"del" =>
		# del parent start end objs...
		for (objs := tl tl tl tl toks; objs != nil; objs = tl objs) {
			id := int hd objs;
			pick o := objects[id] {
			Session =>
				cmd(win, "grid rowdelete .t.s id" + string id);
				cmd(win, "destroy .t.s.f" + string id);
			Archive =>
				cmd(win, "grid rowdelete .t.a id" + string id);
				cmd(win, "destroy .t.a.f" + string id);
			Sessiontype =>
				sys->print("cannot destroy sessiontypes yet\n");
			Member =>
				pick parent := objects[o.parentid] {
				Session =>
					parent.members = removeitem(parent.members, o.name);
					cmd(win, sys->sprint(".t.s.f%d.members configure -text '%s", o.parentid, concat(parent.members)));
				* =>
					chat.addtext(o.name + " has left\n");
				}
			Invite =>
				s := o.session;
				invites := s.invitations;
				invited := 0;
				for (s.invitations = nil; invites != nil; invites = tl invites) {
					inv := hd invites;
					if (inv != o.name) {
						s.invitations = inv :: s.invitations;
						if (inv == "all" || inv == myname)
							invited = 1;
					}
				}
				if (!invited && s.invited) {
					cmd(win, "destroy .t.s.f" + hd tl toks + ".join");
					s.invited = 0;
				}
			}
			objects[id] = nil;
		}

	"name" =>
		myname = hd tl toks;
		tkclient->settitle(win, "Lobby (" + myname + ")");

	"set" =>
		# set obj attr val
		id := int hd tl toks;
		(attr, val) := (hd tl tl toks, tl tl tl toks);
		pick o := objects[id] {
		Session =>
			f := ".t.s.f" + string id;
			case attr {
			"filename" =>
				o.filename = hd val;
			"owner" =>
				if (hd val == myname) {
					cmd(win, "label "+f+".invite -text Invite -bd 2 -relief raised");
					cmd(win, "bind "+f+".invite <Button-1> {send e postinvite " + string id + " %W}");
					cmd(win, "grid "+f+".invite -row id" + string id + " -column invite -in .t.s");
				}
				o.owner = hd val;
			"title" =>
				cmd(win, f + ".name configure -text '" + concat(val));
			}
		Archive =>
			f := ".t.a.f" + string id;
			case attr {
			"name" =>
				cmd(win, f + ".name configure -text '" + concat(val));
			"members" =>
				cmd(win, f + ".members configure -text '" + concat(val));
			}
		Sessiontype =>
			case attr {
			"start" =>
				o.start = concat(val);
			"clienttype" =>
				o.clienttype = hd val;
			"title" =>
				if (o.title != nil)
					panic("can't change sessiontype name!");
				else {
					o.title = concat(val);
					cmd(win, ".buts.start.m add command" +
							" -command {send e start " + string id + "}" +
							" -text '" + o.title);
				}
			"name" =>
				o.name = hd val;
			}
		Member =>
			case attr {
			"name" =>
				if (o.name != nil)
					panic("cannot change member name!");
				o.name = hd val;
				pick parent := objects[o.parentid] {
				Session =>
					parent.members = o.name :: parent.members;
					cmd(win, sys->sprint(".t.s.f%d.members configure -text '%s", o.parentid, concat(parent.members)));
				* =>
					chat.addtext(o.name + " has arrived\n");
				}
			}
		Invite  =>
			case attr {
			"name" =>
				o.name = hd val;
				s := o.session;
				sid := string s.id;
				f := ".t.s.f" + sid;
				invited := o.name == myname || o.name == "all";
				s.invitations = o.name :: s.invitations;
				if (invited && !s.invited) {
					cmd(win, "button "+f+".join -text Join -command {send e join " + sid + "}");
					cmd(win, "grid "+f+".join -row id" + sid + " -column join -in .t.s");
					s.invited = 1;
				}
			}
		}
	}
}

removeitem(l: list of string, i: string): list of string
{
	rl: list of string;
	for (; l != nil; l = tl l)
		if (hd l != i)
			rl = hd l :: rl;
	return rl;
}

numsplit(s: string): (string, int)
{
	for (i := len s - 1; i >= 0; i--)
		if (s[i] < '0' || s[i] > '9')
			break;
	if (i == len s -1)
		return (s, 0);
	return (s[0:i+1], int s[i+1:]);
}

cliquecmd(s: string): string
{
	if (sys->fprint(cliquefd, "%s", s) == -1) {
		e := sys->sprint("%r");
		sys->print("error on '%s': %s\n", s, e);
		return e;
	}
	return nil;
}

prefixed(s: string, prefix: string): int
{
	return len s >= len prefix && s[0:len prefix] == prefix;
}

readproc(updatech: chan of list of string)
{
	buf := array[Sys->ATOMICIO] of byte;
	while ((n := sys->read(cliquefd, buf, Sys->ATOMICIO)) > 0) {
		(nil, lines) := sys->tokenize(string buf[0:n], "\n");
		if (lines != nil)
			updatech <-= lines;
	}
	updatech <-= nil;
}

startclient(mod: Command, argv: list of string)
{
	{
		mod->init(drawctxt, argv);
	} exception e {
	"*" =>
		sys->print("client %s broken: %s\n", hd argv, e);
		exit;
	}
	mod->init(drawctxt, argv);
}

cmd(win: ref Tk->Toplevel, s: string): string
{
	r := tk->cmd(win, s);
	if(len r > 0 && r[0] == '!')
		sys->print("error executing '%s': %s\n", s, r[1:]);
	return r;
}

concat(l: list of string): string
{
	if (l == nil)
		return nil;
	s := hd l;
	for (l = tl l; l != nil; l = tl l)
		s += " " + hd l;
	return s;
}