~aleteoryx/ntalk

ref: 54f5c578573ee15fa33c6726e9a3d1970d56d6fb ntalk/ntalk.tcl -rwxr-xr-x 17.8 KiB
54f5c578Aleteoryx fix a netcode deadlock, add a "copy sixel" menu 2 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
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
#!/bin/env wish


set confdir ~/.config/ntalk
set scriptpath "${confdir}/cscript.tcl"
set sixelpath "${confdir}/sixels.txt"
file mkdir $confdir


proc quit {} {exit 0}
proc restart {} {
	global argv0
	exec [info nameofexecutable] $argv0 &
	exit 0
}

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


### ICON ###

# don't worry!! it is just an image!!!!
set nanooo {
iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAAAAACPAi4CAAAABGdBTUEAALGPC/xhBQAAACBjSFJN
AAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAAAmJLR0QA/4ePzL8AAAAHdElN
RQfpCRUAHTfQmewlAAAGbUlEQVRYw6WXfVSV9R3AP/dyedUAm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A2h7vnsXu/9eUl38tIiLHN5hEGo9oc6su
kCv/bBc/8l/mOtXeHS1s7wAAACV0RVh0ZGF0ZTpjcmVhdGUAMjAyNS0wOS0yMVQwMDoyOTo1NSsw
MDowMF2jvnEAAAAldEVYdGRhdGU6bW9kaWZ5ADIwMjUtMDktMjFUMDA6Mjk6NTUrMDA6MDAs/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 {
			- {
				set row [string trimright $row "? "]
				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
	}
}
make16


### 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 . <Control-q> quit
bind . <Control-R> restart
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


### MENU ###

menu .menu
menu .menu.nt -tearoff 0
menu .menu.opt -tearoff 1
menu .menu.sixels -tearoff 1 -title "sixel picker"
menu .menu.sixels.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-09-21" \
		-icon "info"
}
.menu.nt add separator
.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 "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}]
}

. 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
	
	.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
	wm title .namesixel "<  |  name sixel  |  >"
	
	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

	canvas .namesixel.img -height [image height $clickedimage] \
		-width [image width $clickedimage]
	.namesixel.img create image 1 1 -anchor nw -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]
		.savesixel post %X %Y
		bind . <Button> {
			.savesixel unpost
			bind . <Button> {}

			set clickedimage {}
		}
	}}
}


### CONNECTING ###

set user marmalade
set cmds {}
set sok {}
set server "the series of tubes"
if [file readable $scriptpath] {
	set fp [open $scriptpath]
	.buffer insert 1.0 [read $fp]
	close $fp
} else {
	.buffer insert 1.0 {# input connection script, then hit C-RET. your changes will be saved.
set server localhost
set sok [socket $server 44322]
set user marmalade
}
}

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> {}
.buffer configure -state disabled

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

fconfigure $sok -translation lf; # dammit
set user [string trim $user]
.foot.name configure -text "${user}:"

settitle "nanochatting on $server"


### NETCODE ###

proc setclients {newc} {
	global clients
	set clients $newc
}
proc setlastmsg {new} {
	global lastmsg
	set lastmsg $new
}

set lastmsg 0
set netcode {
set lastmsg 0

proc sendl {line} {
	global sok inrecv
	if {$inrecv} return
	regsub "\n" $line " " line
	if [catch {
		puts $sok $line
		flush $sok
	}] { restart }
}
proc recvl {} {
	global sok
	gets $sok ret
#	if [catch { gets $sok ret }] { restart }
	return $ret
}
set inrecv 0
proc recvlines {{bd 0}} {
	global lastmsg inrecv
	if {$inrecv} return
	set inrecv 1
	set n [recvl]
	for {set i 0} {$i < $n} {incr i} {
		bufpush [recvl]
		if $bd bufdown
	}
	setlastmsg [recvl]
	set inrecv 0
}

proc send {line} {
	global lastmsg
	sendl "SEND $line"
	set msgid [recvl]
	if {$msgid == $lastmsg+1} {
		setlastmsg $msgid
		return 1
	}
	after idle skip
	return 0
}

proc poll {} {
	global lastmsg
	sendl "POLL $lastmsg"
	recvl
}

proc hist {} {
	bufclear
	sendl HIST
	recvlines 1
}

proc last {n} {
	bufclear
	sendl "LAST $n"
	recvlines 1
}

proc skip {} {
	global lastmsg
	sendl "SKIP $lastmsg"
	recvlines
}

proc quit {} {
	sendl QUIT
	exit 0
}

proc stat {} {
	sendl STAT
	lassign [recvl] msgs
	lassign [recvl] bytes
	lassign [recvl] clients
	
	setclients $clients
}

set pmid {}
proc pollmsgs {delay} {
	global inrecv pmid
	after cancel $pmid
	set pmid [after $delay doskip $delay]
}
proc doskip {delay} {
	global inrecv
	if !$inrecv {
		stat
		skip
	}
	set pmid [after $delay doskip $delay]
}
}

if {[catch {package require Thread}] == 0} { # threading supported!
	set threads 1
	set main [thread::id]

	set nett [thread::create]
	thread::transfer $nett $sok
	
	thread::send $nett [concat [subst -nocommands {	
		proc restart {} {
			global argv0
			exec [info nameofexecutable] $argv0 &
			exit 0
		}
		set sok $sok
		interp alias {} bufdown {} thread::send -async $main bufdown
		interp alias {} bufclear {} thread::send -async $main bufclear
		proc bufpush {x} { thread::send -async $main [list bufpush [set x]] }
		proc setclients {x} { thread::send -async $main [list setclients [set x]] }
		proc setlastmsg {x} {
			global lastmsg
			set lastmsg [set x]
			thread::send -async $main [list setlastmsg [set x]]
		}
	}] ";" $netcode]


	interp alias {} hist {} thread::send -async $nett hist
	proc send {line} [subst -nocommands { thread::send $nett [list send [set line]] }]
	proc pollmsgs {n} [subst -nocommands { thread::send -async $nett [list pollmsgs [set n]] }]
	proc last {n} [subst -nocommands { thread::send -async $nett [list last [set n]] }]
} else {
	set threads 0
	eval $netcode
}


### ACTUAL CLIENT CODE LMAO ###

proc bufpush {line} {
	global images motd user threads

	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}\[^\\w.\]" $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}
		}
	}
	.buffer insert end "\n" $tag
	.buffer configure -state disabled
	if !$threads update
}
proc bufdown {} {
	global threads
	.buffer yview moveto 1
	if !$threads update
}
proc bufclear {} {
	global threads
	.buffer configure -state normal
	.buffer replace 1.0 end {}
	.buffer configure -state disabled
	setmotd "no MOTD yet! maybe you should send one..."
	if !$threads update
}

proc sendmsg {msg} {
	global lastmsg
	if {[send $msg]} {
		bufpush "$msg"
		bufdown
	}
}

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]
}


### BOOT ###

last 64
pollmsgs 15000

set typingid {}
bind .foot.input <KeyPress> {
	after cancel $typingid
	pollmsgs 3000
	set typingid [after 15000 pollmsgs 15000]
}


bind .foot.input <Return> [concat [bind .foot.input <Return>] ";" {
	set line [.foot.input get]
	.foot.input delete 0 end
	switch -glob -- $line [concat $cmds {
		/hist {
			hist
		}
		/quit {
			quit
		}
		/restart {
			restart
		}
		{/last *} {
			last [string range $line 6 end]
		}
		{/send *} {
			sendmsg [string range $line 6 end]
		}
		{/motd *} {
			sendmsg "MOTD [string range $line 6 end]"
		}
		/me* {
			sendmsg "${user}[string range $line 3 end]"
		}
		{/my *} {
			sendmsg "${user}'s [string range $line 4 end]"
		}
		{/nick *} {
			set user [string trim [string range $line 6 end]]
			.foot.name configure -text "${user}:"
		}
		{/eval *} {
			.foot.input insert 0 [eval [string range $line 6 end]]
		}
		{/exec *} {
			.foot.input insert 0 [exec sh -c [string range $line 6 end]]
		}
		{/calc *} {
			.foot.input insert 0 [expr [string range $line 6 end]]
		}
		{/n64k} {
			.foot.input insert 0 [n64k_date]
		}
		default {
			sendmsg "$user: $line"
		}
	}]
}]

focus .foot.input