~aleteoryx/ntalk

ref: b9b06524b09f10f0a413f911665f45f7ff88866d ntalk/ntalk.tcl -rwxr-xr-x 24.6 KiB
b9b06524Aleteoryx fixes a month 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
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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
#!/bin/env wish


set confdir [file normalize ~/.config/ntalk]
set persisted "${confdir}/persist.tcldict"
set config "${confdir}/config.tcl"
set scriptdir "${confdir}/cscript"
set sixelpath "${confdir}/sixels.txt"
file mkdir $confdir

set bootargs $argv

proc quit {} {exit 0}
proc runself args {
	global argv0
	exec [info nameofexecutable] $argv0 {*}$args &
}
proc restart args {
	global bootargs
	if {$args == {}} { set args $bootargs }
	runself {*}$args
	exit 0
}

proc settitle {title} {
	wm title . ".  o  (  $title  )  o  ."
}
settitle ntalk
tk appname ntalk


### PERSISTED DATA ###

set persistwhat {
	talktype irc
}
foreach {var val} $persistwhat {
	set $var $val
}
if {[file readable $persisted]} {
	set fp [open $persisted r]
	set pdata [read $fp]
	close $fp
	foreach {var _} $persistwhat {
		if {[dict exists $pdata $var]} {
			set $var [dict get $pdata $var]
		}
	}
}

proc savepersist {} {
	global persistwhat persisted
	foreach {var _} $persistwhat {
		global $var
		dict set pdata $var [set $var]
	}
	set fp [open $persisted w]
	puts $fp $pdata
	close $fp
}


### ICON ###

# do not be alarmed!! it is just an image!!!!
set nanooo {
iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAAAAACPAi4CAAAABGdBTUEAALGPC/xhBQAAACBjSFJN
AAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAAAmJLR0QA/4ePzL8AAAAHdElN
RQfpCRUAHTfQmewlAAAGbUlEQVRYw6WXfVSV9R3AP/dyedUAmRpK6rSBw9TlnAknTr7RsbNRWtLB
tnLNVp10EZi9OJtzs9k8xx0FbRnNKWmFutDlW7hBOlmmJlxUjoCACBMBUXm/3Lfnuz/ufe597mub
ff95nuf78nm+39/7TycEE7MF9EOCeRgCWvq6ja1UbIPkHB1M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5ADIwMjUtMDktMjFUMDA6Mjk6NTUrMDA6MDAs/gbN
AAAAAElFTkSuQmCC}
image create photo nanooo -data [binary decode base64 $nanooo]
wm iconphoto . nanooo


### SIXEL PARSING LIB ###

set images {}
proc char2n {char} {
	binary scan $char c n
	expr {($n&0x7F)-0x3F}
}

proc chars2bytes {sixels} {
	set ret {}
	set nums {}
	foreach sixel $sixels { lappend nums [char2n $sixel] }
	for {set i 0} {$i < 6} {incr i} {
		set s ""
		foreach n $nums {
			append s [expr {($n>>$i)&1}]
		}
		append s [string repeat "0" [expr {7 - (([string length $s] + 7) % 8)}]]
		binary scan [binary format b* $s] c* bytes
		foreach byte $bytes {
			lappend ret [format "%02x" [expr {$byte&0xff}]]
		}
	}
	return $ret
}

proc splitsixels {str} {
	set ret {}
	set row {}

	foreach c [split $str {}] {
		switch -regexp -- $c {
			- {
				lappend ret $row
				set row {}
			}
			[?-~] {
				lappend row $c
			}
			default { # ignored }
		}
	}
	if {$row != {}} { lappend ret $row }

	return $ret
}

proc sixels2xbm {sixels} {
	set rows [splitsixels $sixels]
	set ba {}
	if {[llength $rows] == 0} { return {} }
#	if {[llength $rows] > 3} { set rows [lrange $rows 0 2] }
	
	set height [expr {min(48, [llength $rows]*6)}]
	set width 0
	foreach row $rows {
		set width [expr {max($width, [llength $row])}]
	}
	if {$width == 0} { return {} }
	
	foreach row $rows {
		append row [string repeat " ?" [expr {$width - [llength $row]}]]
		lappend bytes {*}[chars2bytes $row]
	}
	
	set nbytes [expr {int(ceil($width / 8.0)) * $height}]
	set bytes [lrange $bytes 0 $nbytes-1]
	
	set ret "#define img_width $width\n#define img_height $height\n"
	append ret "static unsigned char img_bits\[\] = {\n\t"
	foreach byte $bytes {
		append ret "0x" $byte ", "
	}
	set ret [string range $ret 0 end-2]
	append ret "\n\t};\n"
	return $ret
}

proc sixels2image {sixels} {
	global images
	if {[dict exists $images "sixel:$sixels"]} {
		return [dict get $images "sixel:$sixels"]
	}

	set xbm [sixels2xbm $sixels]
	if {[dict exists $images "xbm:$xbm"]} {
		return [dict get $images "xbm:$xbm"]
	}

	set image [image create bitmap -data $xbm]
	image create bitmap motd$image -data $xbm -foreground DarkOliveGreen
	image create bitmap mention$image -data $xbm -foreground DarkOrchid4
	dict set images "sixel:$sixels" $image
	dict set images "xbm:$xbm" $image
	dict set images "image:$image" $sixels
	dict set images "image:motd$image" $sixels
	dict set images "image:mention$image" $sixels

	return $image
}

proc parseline {line} {
	set chunks {}

	set idx1 -1
	set idx2 -1
	while {[set idx2 [string first "\\(" $line $idx1]] != -1} {
		# insert the prefix text
		lappend chunks [string range $line $idx1 $idx2-1]

		# get the sixels
		set idx1 $idx2
		set idx2 [string first ")" $line $idx1+2]
		if {$idx2 == -1} { set idx2 [string length $line] }

		# insert them
		set image [sixels2image [string range $line $idx1+2 $idx2-1]]
		lappend chunks [list [string range $line $idx1 $idx2] $image]
		
		set idx1 [expr {$idx2 + 1}]
	}
	lappend chunks [string range $line $idx1 end]
	
	return $chunks
}


### FONT STUFF ###

font create testingFont -size 100

proc make16 {} {
	foreach font {TkDefaultFont TkFixedFont TkTextFont} {
		font configure testingFont -family [font configure $font -family]
		set fontsize [expr {int(ceil(16.0 / [font metrics testingFont -linespace] * 100))}]
		font configure $font -size $fontsize
	}
}


### UI SETUP ###

frame .motd

frame .foot
entry .foot.input
label .foot.msgs -textvariable lastmsg
label .foot.sep -text " // "
label .foot.ppl -textvariable clients
label .foot.name

scrollbar .scroll -command {.buffer yview}
text .buffer -height 24 -width 128 -yscrollcommand {.scroll set}

pack .foot.ppl .foot.sep .foot.msgs -side right
pack .foot.name -side left
pack .foot.input -side bottom -fill x

pack .motd -side top
pack .foot -side bottom -fill x
pack .scroll -side right -fill y
pack .buffer -fill both

if {[catch {package require history}] == 0} {
	history::init .foot.input
	bind .foot.input <Return> {
		if {[.foot.input get] != {}} {
			history::add .foot.input [.foot.input get]
		}
	}
}

bind . <Destroy> {if {[list %W] == "."} quit}
bind . <Control-q> quit
bind . <Control-R> restart
bind . <Control-N> runself
bind . <Control-s> {.menu.opt invoke "show raw sixel codes"}

.buffer tag configure rawsixel -elide true -foreground DarkSlateGrey
.buffer tag configure motd -foreground DarkOliveGreen -justify center -spacing1 5 -spacing3 5 -underline 1
.buffer tag configure mention -foreground DarkOrchid4
.buffer tag configure url -underline 1 -foreground "dark cyan"
.buffer tag configure url2 -underline 1 -foreground "dark blue"

.buffer tag bind url <Enter> {
	set range [.buffer tag prevrange url current]
	.buffer tag add url2 {*}$range
	.buffer tag remove url {*}$range
	.buffer configure -cursor hand2
}
.buffer tag bind url2 <Leave> {
	set range [.buffer tag prevrange url2 current]
	.buffer tag add url {*}$range
	.buffer tag remove url2 {*}$range
	.buffer configure -cursor xterm
}
.buffer tag bind url2 <Button-1> {
	set range [.buffer tag prevrange url2 current]
	exec xdg-open [.buffer get {*}$range] &
}


### MENU ###

menu .menu
menu .menu.nt -tearoff 0
menu .menu.opt -tearoff 1
menu .menu.opt.style -tearoff 0
menu .menu.sixels -tearoff 1 -title "sixel picker"
menu .menu.sixels.rm -tearoff 0
menu .menu.server -tearoff 0
menu .menu.server.rm -tearoff 0

.menu add cascade -label "ntalk" -menu .menu.nt
.menu.nt add command -label "about ntalk" -command {
	tk_messageBox -title "about ntalk" \
		-message "ntalk\nby aleteoryx" \
		-detail "last updated 2025-10-21" \
		-icon "info"
}
.menu.nt add separator
.menu.nt add command -label "new window" -command runself -accelerator "Ctrl-Shift-N"
.menu.nt add command -label "restart" -command restart -accelerator "Ctrl-Shift-R"
.menu.nt add command -label "quit" -command quit -accelerator "Ctrl-Q"

.menu add cascade -label "sixels" -menu .menu.sixels
.menu.sixels add separator
.menu.sixels add cascade -label "delete a sixel..." -menu .menu.sixels.rm

.menu add cascade -label "servers" -menu .menu.server
.menu.server add separator
.menu.server add command -label "add a server" -command addserver_open
.menu.server add cascade -label "delete a server..." -menu .menu.server.rm

.menu add cascade -label "options" -menu .menu.opt
.menu.opt add checkbutton -label "show raw sixel codes" \
	-accelerator "Ctrl-s" -variable showsixel -command {
		.buffer tag configure rawsixel -elide [expr {!$showsixel}]
	}
.menu.opt add separator
.menu.opt add cascade -label "message style..." -menu .menu.opt.style
.menu.opt.style add radiobutton -label "IRC" -value irc \
	-variable talktype -command savepersist
.menu.opt.style add radiobutton -label "MOO" -value moo \
	-variable talktype -command savepersist

. configure -menu .menu

### MOTD ###

proc setmotd {newmotd} {
	global motd

	destroy {*}[winfo children .motd]
	foreach {text image} [parseline $newmotd] {
		set tlabel [label .motd.[incr i] -text $text]
		pack $tlabel -side left
		
		if {$image != {}} {
			lassign $image _ image
			set ilabel [label .motd.[incr i] -image $image]
			pack $ilabel -side left
		}
	}
	
	set motd $newmotd
}


### USER SIXEL LIBRARY ###

set sixellib {}
proc savesixels {} {
	global sixellib sixelpath
	
	set fp [open $sixelpath w]
	foreach line [lreverse $sixellib] {
		if {$line == {}} {
			puts $fp ""
			continue
		}
		lassign $line name data
		puts $fp "$name = $data"
	}
	close $fp
}
proc rmsixel {n} {
	global sixellib
	set sixellib [lreplace $sixellib $n $n]
	savesixels
	regensixelmenu
}
proc getsubmenu {menu name} {
	while {[set idx [string first / $name]] != -1} {
		set chunk [string trim [string range $name 0 $idx-1]]
		set name [string trim [string range $name $idx+1 end]]
		set submenu "${menu}.u$chunk"
		catch {
			menu $submenu -tearoff 0
			$menu insert 0 cascade -menu $submenu -label $chunk
		}
		set menu $submenu
	}
	return [list $menu $name]
}
proc regensixelmenu {} {
	global sixellib
	
	if {[.menu.sixels index end] > 2} {
		.menu.sixels delete 0 [expr {[.menu.sixels index end]-2}]
		.menu.sixels.rm delete 0 end
		foreach menu [winfo children .menu.sixels] {
			if {$menu == ".menu.sixels.rm"} continue
			destroy $menu
		}
		destroy {*}[winfo children .menu.sixels.rm]
	}
	
	for {set i 0} {$i < [llength $sixellib]} {incr i} {
		if {[lindex $sixellib $i] == {}} continue
		lassign [lindex $sixellib $i] name data
		set name [string trim $name]
		set data [string trim $data]
		set escdata [list "\\($data)"]
		lassign [getsubmenu .menu.sixels $name] menu label
		$menu insert 0 command -image [sixels2image $data] -hidemargin 1 -command [subst {
			.foot.input insert insert $escdata
		}]
		lassign [getsubmenu .menu.sixels.rm $name] menu label
		$menu insert 0 command -image [sixels2image $data] -hidemargin 1 -command [subst {
			rmsixel $i
		}]
	}
}

if {[file readable $sixelpath]} {
	set fp [open $sixelpath]
	while {![eof $fp]} {
		gets $fp line
		if {$line == {}} {
			lappend sixellib {}
			continue
		}
		set idx [string first "=" $line]
		set name [string trim [string range $line 0 $idx-1]]
		set data [string trim [string range $line $idx+1 end]]
		lappend sixellib [list $name $data]
	}
	set sixellib [lreverse $sixellib]
	close $fp
} else {
	set sixellib {}
}
regensixelmenu

set clickedimage {}
proc finishsixel {} {
	global clickedimage images sixellib
	set data [dict get $images "image:$clickedimage"]
	regsub "=" [.namesixel.entry get] ":" name
	
	lappend sixellib [list $name $data]
	savesixels
	regensixelmenu
	
	destroy .namesixel
}


menu .savesixel -tearoff 0
.savesixel add command -label "copy sixel" -command {
	clipboard clear
	clipboard append "\\([dict get $images "image:$clickedimage"])"
	set clickedsixel {}
}
.savesixel add command -label "save sixel..." -command {
	bind . <Button> {}

	toplevel .namesixel -pady 5 -padx 5
	wm title .namesixel "<  |  name sixel  |  >"
	wm attributes .namesixel -type dialog
	
	entry .namesixel.entry
	button .namesixel.ok -text ok -command finishsixel
	pack .namesixel.ok -side bottom -padx 5 -pady 5
	pack .namesixel.entry -side bottom -padx 5 -pady 5 -fill x

	label .namesixel.img -image $clickedimage

	label .namesixel.blurb -text "choose what to save this sixel as..."
	pack .namesixel.img -side left -padx 5 -pady 5
	pack .namesixel.blurb -side right -padx 5 -pady 5
	
	bind .namesixel <Destroy> { set clickedimage "" }
}

bind .buffer <Button-3> {
	if {$clickedimage == {}} {catch {
		set clickedimage [.buffer image cget @%x,%y -image]
		set clickedimage [dict get $images sixel:[dict get $images image:$clickedimage]]
		.savesixel post %X %Y
		bind . <Button> {
			.savesixel unpost
			bind . <Button> {}

			set clickedimage {}
		}
	}}
}


### SERVER MANAGEMENT ###

proc gencscript {{server localhost} {port 44322}} {
	set server [list $server]
	set port [list $port]
	
	set ret [subst \
{# the entire connection is run through sok, make sure to set it up right.
# press C-RET to connect
# your changes will be saved

# set servername "johann smith's server"

set server $server
set port $port
}]
	append ret {set sok [socket $server $port]}
	return [string trim $ret]
}

# figure out what server to connect to
proc locatecscript {} {
	global scriptpath
	set scriptpath [_locatecscript]
	# we might have created or moved files
	regenservlist
	return $scriptpath
}
proc _locatecscript {} {
	global confdir scriptdir defaultserver argv

	# migrate old config
	set scriptpath "${confdir}/cscript.tcl"
	if {[file readable $scriptpath] && ![file exists $scriptdir]} {
		file mkdir $scriptdir
		file rename $scriptpath "$scriptdir/default.tcl"
		return "$scriptdir/default.tcl"
	}

	file mkdir $scriptdir

	# handle argument
	if {[llength $argv] > 0 && [file readable "${scriptdir}/[lindex $argv 0].tcl"]} {
		return "${scriptdir}/[lindex $argv 0].tcl";
	}

	# try using the default
	set scriptpath [file join $scriptdir "${defaultserver}.tcl"]
	if {[file readable $scriptpath]} {
		return $scriptpath
	}

	# try picking a default
	set servlist [lsort [glob -directory $scriptdir "*.tcl"]]
	if {[llength $servlist] > 0} {
		return [lindex $servlist 0]
	}
	
	# fine, create a new one
	set fp [open "${scriptdir}/default.tcl" w]
	puts $fp [gencscript]
	close $fp
	return "${scriptdir}/default.tcl"
}

proc regenservlist {} {
	global scriptdir scriptpath

	if {[.menu.server index end] > 2} {
		.menu.server delete 0 [expr {[.menu.server index end]-3}]
		.menu.server.rm delete 0 end
	}
	
	foreach filename [lsort -decreasing [glob -directory $scriptdir *.tcl]] {
		set slug [file tail [file rootname $filename]]

		if {$filename == $scriptpath} {
			set slug "$slug (current)"
			set en disabled
		} else {
			set en normal
		}

		.menu.server insert 0 command -label $slug -state $en \
			-command [list restart $slug]
		.menu.server.rm insert 0 command -label $slug -state $en \
			-command [concat [list file delete $filename] ";" regenservlist]
	}
}

proc addserver_open {} {
	destroy .addserver
	toplevel .addserver -pady 5 -padx 5
	wm title .addserver "<  |  add server  |  >"
	wm attributes .addserver -type dialog

	frame .addserver.i

	frame .addserver.i.name
	label .addserver.i.name.label -text "short name: "
	entry .addserver.i.name.entry
	pack .addserver.i.name.entry .addserver.i.name.label -side right

	frame .addserver.i.host
	label .addserver.i.host.label -text "hostname: "
	entry .addserver.i.host.entry
	.addserver.i.host.entry insert 0 localhost
	pack .addserver.i.host.entry .addserver.i.host.label -side right

	frame .addserver.i.port
	label .addserver.i.port.label -text "port: "
	entry .addserver.i.port.entry
	.addserver.i.port.entry insert 0 44322
	pack .addserver.i.port.entry .addserver.i.port.label -side right
	
	pack .addserver.i.name .addserver.i.host .addserver.i.port -side top -fill x

	label .addserver.err -foreground red

	frame .addserver.btn
	button .addserver.btn.cancel -text "cancel" -command {destroy .addserver}
	button .addserver.btn.ok -text "add" -command addserver_finish
	pack .addserver.btn.cancel .addserver.btn.ok -side left
	
	pack .addserver.i .addserver.btn -side top -pady 5 -padx 5
	
	focus .addserver.i.name.entry
}

proc addserver_finish {} {
	global scriptdir

	set name [.addserver.i.name.entry get]
	set host [.addserver.i.host.entry get]
	set port [.addserver.i.port.entry get]

	if {$name == {}} {
		.addserver.err configure -text "short name cannot be empty!"
		return
	} elseif {[string first / $name] != -1} {
		.addserver.err configure -text "short name cannot include slashes!"
		return
	}

	set path [file join $scriptdir "${name}.tcl"]
	if [file readable $path] {
		.addserver.err configure -text "there's already a server called ${name}!"
		return
	}

	set fp [open $path w]
	puts $fp [gencscript $host $port]
	close $fp
	runself $name
	
	destroy .addserver
	regenservlist
}


### LOAD BASE CONFIG ###

set servername "the series of tubes"
set sok {}

set baseconfig \
{# these settings are applied to all connections
# hit C-RET to apply and restart
set user marmalade
set cmds {}
set defaultserver default

font configure TkDefaultFont -family monospace
font configure TkTextFont -family monospace
font configure TkFixedFont -family monospace
}

eval $baseconfig
if [file readable $config] {
	set fp [open $config]
	set $configtext [read $fp]
	close $fp
	eval $configtext; # TODO: wrap in catch or something
}
make16


### CONNECTING ###

locatecscript
set fp [open $scriptpath]
.buffer insert 1.0 [read $fp]
close $fp

bind . <Control-Return> {
	eval [.buffer get 1.0 end]
	make16
	if {$sok != {}} { set cscript [.buffer get 1.0 end] }
}
.buffer mark set insert end
focus .buffer
vwait cscript
bind . <Control-Return> {}

set fp [open $scriptpath w]
puts $fp [string trim $cscript]
close $fp

fconfigure $sok -translation lf -blocking false -encoding iso8859-1
fileevent $sok readable [list incoming $sok]
set user [string trim $user]
.foot.name configure -text "${user}:"

settitle "nanochatting on $servername"


### NETCODE ###

proc neterr {} {
	restart
}

set nettimeout {}
proc heartbeat {} {
	global nettimeout
	killtimeout
	# double-wait, in case we're coming out of suspend
	set nettimeout [after 10000 {set nettimeout [after 20000 neterr]}]
}
proc killtimeout {} {
	global nettimeout
	after cancel $nettimeout
}

set next {}
set sendqueue {}

proc incoming {sok} {
	global next
	if [catch {
		while {[gets $sok line] != -1} {
			heartbeat; # yay networking!
			eval $next [list $line]
		}
		if {[eof $sok]} { neterr }
	}] { neterr }
}

proc sendl {line} {
	global sok
	regsub "\n" $line " " line
	if [catch {
		puts $sok $line
		flush $sok
	}] { neterr }
	
	heartbeat; # start timeout
}
proc pumpq {} {
	global next sendqueue
	set sendqueue [lassign $sendqueue send recv]
	set next $recv
	killtimeout; # in case $send == {}
	eval $send
}
proc sendq {send recv} {
	global next sendqueue
	if {$next == {}} {
		set next $recv
		eval $send
	} else {
		lappend sendqueue $send $recv
	}
}

proc sendr {line id} {
	global lastmsg 
	if {$lastmsg+1 == $id} {
		set lastmsg $id
		bufpush $line
		bufdown
	}
	pumpq
}
proc send {line} {
	sendq [list sendl "SEND $line"] [list sendr $line]
}

proc lines {post n line} {
	global lastmsg next
	if {$n == 0} {
		set lastmsg $line
		eval $post
		pumpq
	} else {
		set next [list lines $post [expr {$n - 1}]]
		bufpush $line
	}
}
proc appended {post n} {
	global next
	set next [list lines $post $n]
}
proc cleared {n} {
	bufclear
	appended bufdown $n
}

proc hist {} {
	sendq {sendl HIST} cleared
}
proc last {n} {
	sendq [list sendl "LAST $n"] cleared
}

set skipid {}
proc skipr {delay} {
	global skipid
	set skipid [after $delay skip $delay]
}
proc skip {delay} {
	global skipid
	after cancel $skipid

	sendq {
		global lastmsg
		sendl "SKIP $lastmsg"
	} [list appended [list skipr $delay]]
}

proc quit {} {
	sendl QUIT
	exit 0
}

proc statr {line} {
	global clients
	lassign $line n type
	switch -- $type {
		clients {
			set clients $n
			after 10000 stat
			pumpq
		}
	}
}
proc stat {} {
	sendq {sendl STAT} {statr}
}


### ACTUAL CLIENT CODE LMAO ###

proc bufpush {line} {
	global images motd user

	set tag {}
	if {[string first "MOTD:" $line] == 0} {
		setmotd [string trim [string range $line 5 end]]
		set line "<<< $motd >>>"
		set tag motd
	} elseif {[string first "MOTD" $line] == 0} {
		setmotd [string trim [string range $line 4 end]]
		set line "<<< $motd >>>"
		set tag motd
	} elseif {[regexp "\[^\\w\]${user}\\M" $line]} {
		set tag mention
	}

	.buffer configure -state normal
	foreach {text image} [parseline $line] {
		.buffer insert end $text $tag

		if {$image != {}} {
			lassign $image raw image
			set image $tag$image
			.buffer insert end $raw [concat rawsixel $tag]
			.buffer image create end -image $image
			.buffer tag add $tag {end -1 chars}
		}
	}

	set urlre {(https?:(//)?)?([^ ./?#]+\.)+[^ ./?#]{2,}([/?#][^\s]*)?}
	set urls [.buffer search -all -strictlimits -regexp -- $urlre "end - 1 lines" end]
	foreach idx $urls {
		set idx2 [.buffer search -regexp -- {\s} "$idx + 1 chars"]
		.buffer tag add url $idx $idx2
	}
	

	.buffer insert end "\n"
	.buffer configure -state disabled
}
proc bufdown {} {
	.buffer yview moveto 1
}
proc bufclear {} {
	.buffer configure -state normal
	.buffer replace 1.0 end {}
	.buffer configure -state disabled
	setmotd "no MOTD yet! maybe you should send one..."
}

proc n64k_secs {msgcount} {
	# 2025-09-19 01:59:38 GMT
	set proto_epoch 1758247178
	expr {$proto_epoch + (([clock seconds] - $proto_epoch) * 65535 / $msgcount)}
}
proc n64k_date {} {
	global lastmsg
	clock format [n64k_secs $lastmsg]
}

proc setuser {new} {
	global user
	set user [string trim $new]
	.foot.name configure -text "${user}:"
}
proc say {msg} {
	global talktype user
	if {$talktype == "moo"} {
		send "${user} says, \"$msg\""
	} else {
		send "${user}: $msg"
	}
}


### BOOT ###

.buffer configure -state disabled
bind .buffer <KeyPress> {
	set k %A
	if {$k == " " || ![string is control $k]} {
		focus .foot.input
		.foot.input insert insert $k
	} elseif {$k == "\b" && [selection own] == ".foot.input"} {
		focus .foot.input
		.foot.input delete sel.first sel.last
	} elseif {$k == "\b"} {
		focus .foot.input
		.foot.input delete [expr {max(0,[.foot.input index insert]-1)}]
	}
}

set typingid {}
bind .foot.input <KeyPress> {
	if {$typingid == {}} {skip 3000}

	after cancel $typingid
	set typingid [after 15000 {
		set typingid {}
		skip 15000
	}]
}


bind .foot.input <Return> [concat [bind .foot.input <Return>] ";" {
	set line [.foot.input get]
	.foot.input delete 0 end

	set line [string trim $line]
	if {[string length $line] == 0} {
		return
	} elseif {[string first "//" $line] == 0} {
		say [string range $line 1 end]
		return
	} elseif {[string first "/" $line] != 0} {
		say $line
		return
	}

	set idx [string first " " $line]
	if {$idx == -1} {
		set idx end
	} else {
		incr idx -1
	}
	set cmd [string range $line 1 $idx]
	set line [string range $line $idx+2 end]
	switch -nocase -- $cmd [concat $cmds {
		hist { hist }
		quit { quit }
		restart { restart }

		nick { setuser $line }
		name { setuser $line }
		user { setuser $line }

		last { last $line }
		send { send $line }

		motd { send "MOTD $line" }
		me   { send "${user} $line" }
		my   { send "${user}'s $line" }
		idea { send "${user} . o ( $line )" }
		<=   { send "${user} <= $line" }

		eval { .foot.input insert 0 [eval $line] }
		exec { .foot.input insert 0 [exec sh -c $line] }
		calc { .foot.input insert 0 [expr $line] }
		
		n64k { .foot.input insert 0 [n64k_date] }
	}]
}]

focus .foot.input

stat
last 64
skip 15000