-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsg.fs
2607 lines (2262 loc) · 78.6 KB
/
msg.fs
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
\ messages 06aug2014py
\ Copyright © 2014-2023 Bernd Paysan
\ This program is free software: you can redistribute it and/or modify
\ it under the terms of the GNU Affero General Public License as published by
\ the Free Software Foundation, either version 3 of the License, or
\ (at your option) any later version.
\ This program is distributed in the hope that it will be useful,
\ but WITHOUT ANY WARRANTY; without even the implied warranty of
\ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\ GNU Affero General Public License for more details.
\ You should have received a copy of the GNU Affero General Public License
\ along with this program. If not, see <http://www.gnu.org/licenses/>.
Forward avalanche-to ( o:context -- )
Forward pk1-connect? ( key u cmdlen datalen -- flag )
Forward pk-connect-dests?
Forward addr-connect ( key+addr u cmdlen datalen xt -- )
Forward pk-peek? ( addr u0 -- flag )
: ?hash ( addr u hash -- ) >r
2dup r@ #@ d0= IF "" 2swap r> #! ELSE 2drop rdrop THEN ;
Variable otr-mode \ global otr mode
: >group ( addr u -- )
2dup msg-group# #@ d0= IF
net2o:new-msg >o 2dup to msg:name$
otr-mode @ IF msg:+otr THEN
o o>
cell- [ msg:class >osize @ cell+ ]L
2over msg-group# #!
THEN last# cell+ $@ drop cell+ to msg-group-o
2drop ;
User ihave$
User push[]
$300 Constant max#have
: (avalanche-msg) ( o:connect -- )
msg-group-o .msg:peers[] $@
bounds ?DO I @ o <> IF I @ .avalanche-to THEN
cell +LOOP ;
: cleanup-msg ( -- )
push[] $[]free ;
: avalanche-msg ( o:connect -- )
\G forward message to all next nodes of that message group
(avalanche-msg) cleanup-msg ;
Variable msg-group$
User replay-mode
User skip-sig?
Sema msglog-sema
: ?msg-context ( -- o )
msging-context @ dup 0= IF
drop
net2o:new-msging dup msging-context !
THEN ;
: >chatid ( group u -- id u ) defaultkey sec@ keyed-hash#128 ;
: msg-log@ ( -- addr u )
[: msg-group-o .msg:log[] $@ save-mem ;] msglog-sema c-section ;
: #msg-log@ ( i -- addr u )
[: msg-group-o .msg:log[] $[]@ ;] msglog-sema c-section ;
: purge-log ( -- )
[: msg-group-o .msg:log[] { a[] }
0 BEGIN dup a[] $[]# u< WHILE
dup a[] $[]@ check-date nip nip
over a[] $[]@ "\x60\x03\x00\x61" string-prefix? or IF
dup a[] $[] $free
a[] over cells cell $del
ELSE
1+
THEN
REPEAT drop ;] msglog-sema c-section ;
forward msg-scan-hash
forward msg-add-hashs
: serialize-log ( addr u -- $addr )
[: bounds ?DO
I $@ check-date 0= IF
net2o-base:$, net2o-base:nestsig
ELSE msg( ." removed entry " dump )else( 2drop ) THEN
cell +LOOP ;] gen-cmd ;
: scan-log-hashs ( -- )
msg-log@ over >r
[: bounds ?DO
I $@ msg:display
cell +LOOP
msg-add-hashs
;] msg-scan-hash r> free throw ;
Variable saved-msg$
64Variable saved-msg-ticks
: save-msgs ( group-o -- ) to msg-group-o
msg( ." Save messages in group " msg-group-o dup h. .msg:name$ type cr )
?.net2o/chats net2o:new-msging >o
msg-log@ over >r serialize-log enc-file $!buf
r> free throw dispose o>
msg-group-o .msg:name$ >chatid .chats/ enc-filename $!
pk-off key-list encfile-rest ;
: save-all-msgs ( -- )
saved-msg$ $@ bounds ?DO I @ save-msgs cell +LOOP
saved-msg$ $free ;
: save-msgs? ( -- )
saved-msg-ticks 64@ ticker 64@ 64u<= IF save-all-msgs
ticks config:savedelta& 2@ d>64 64+ saved-msg-ticks 64! THEN ;
: next-saved-msg ( -- time )
saved-msg-ticks 64@ 64dup 64#0 64= IF
64drop ticks 64dup saved-msg-ticks 64! THEN ;
: msg-eval ( addr u -- )
net2o:new-msging >o 0 to parent do-cmd-loop dispose o> ;
: load-msg ( group u -- ) 2dup >group
>chatid .chats/ [: type ." .v2o" ;] $tmp
2dup file-status nip no-file# = IF 2drop EXIT THEN
replay-mode on skip-sig? on
['] decrypt@ catch
?dup-IF DoError 2drop
\ try read backup instead
[: enc-filename $. '~' emit ;] $tmp ['] decrypt@ catch
?dup-IF DoError 2drop
ELSE msg-eval THEN
ELSE msg-eval THEN
replay-mode off skip-sig? off enc-file $free ;
: >load-group ( group u -- )
>group msg-group-o .msg:log[] $@len 0=
IF msg-group-o [{: xo :}h1 xo .msg:name$ load-msg ;]
parent .wait-task @
dup 0= IF drop ?file-task THEN send-event THEN ;
: !save-all-msgs ( -- )
syncfile( save-all-msgs )else(
['] save-all-msgs ?file-task send-event ?file-task event-block ) ;
: save-msgs& ( -- )
syncfile( msg-group-o saved-msg$ +unique$ )else(
msg-group-o [{: group-o :}h1 group-o saved-msg$ +unique$ ;]
?file-task send-event ) ;
0 Value log#
2Variable last-msg
: +msg-log ( addr u -- addr' u' / 0 0 )
[: msg-group-o .msg:log[] $ins[]date dup dup 0< xor to log#
log# msg-group-o .msg:log[] $[]@ last-msg 2!
0< IF #0. ELSE last-msg 2@ THEN
;] msglog-sema c-section ;
: ?save-msg ( -- )
msg( ." saving messages in group " msg-group-o dup h. .msg:name$ type cr )
msg-group-o .msg:?otr replay-mode @ or 0= IF save-msgs& THEN ;
Sema queue-sema
\ peer queue, in msg context
: peer> ( -- addr / 0 )
[: msg:peers[] back> ;] queue-sema c-section ;
: >peer ( addr u -- )
[: msg:peers[] $+[]! ;] queue-sema c-section ;
\ events
msg:class class end-class msg-notify-class
msg-notify-class ' new static-a with-allocater Constant msg-notify-o
: >msg-log ( addr u -- addr' u )
+msg-log ?save-msg ;
: do-msg-nestsig ( addr u -- )
2dup msg-group-o .msg:display
msg-notify-o .msg:display ;
: display-lastn ( n -- )
msg-group-o .msg:redisplay ;
: display-sync-done ( -- )
rows msg-group-o .msg:redisplay ;
: display-one-msg { d: msgt -- }
msg-group-o >o
msgt ['] msg:display catch-nobt IF ." invalid entry" cr 2drop THEN
o> ;
Forward silent-join
\ !!FIXME!! should use an asynchronous "do-when-connected" thing
: +unique-con ( -- ) o msg-group-o .msg:peers[] +unique$ ;
Forward +chat-control
: chat-silent-join ( -- )
reconnect( ." silent join " o h. connection h. cr )
o to connection
?msg-context >o silent-last# @ to last# o>
reconnect( ." join: " last# $. cr )
+unique-con silent-join ;
: chat-silent-rqd ( n -- )
reconnect( ." silent requst" cr )
clean-request chat-silent-join ;
: ?nat ( -- ) o to connection
net2o-code nat-punch end-code ;
: ?chat-nat ( -- )
['] chat-silent-rqd rqd! ?nat ;
: chat-rqd-nat ( n -- )
reconnect( ." chat req done, start nat traversal" cr )
connect-rest +flow-control +resend ?chat-nat ;
: chat-rqd-nonat ( n -- )
reconnect( ." chat req done, start silent join" cr )
connect-rest +flow-control +resend chat-silent-join ;
User peer-buf
: reconnect-chat ( addr u $chat -- )
peer-buf $!buf last# peer-buf $@
reconnect( ." reconnect " 2dup 2dup + 1- c@ 1+ - .addr$ cr )
reconnect( ." in group: " last# dup h. $. cr )
0 >o $A $A [: reconnect( ." prepare reconnection" cr )
?msg-context >o silent-last# ! o>
['] chat-rqd-nat ['] chat-rqd-nonat ind-addr @ select rqd! ;]
addr-connect 2dup d0= IF 2drop ELSE push[] $+[]! avalanche-to THEN o> ;
\ coordinates
6 sfloats buffer: coord"
90e coord" sfloat+ sf!
: coord@ ( -- addr u ) coord" 6 sfloats ;
: sf[]@ ( addr i -- sf ) sfloats + sf@ ;
: sf[]! ( addr i -- sf ) sfloats + sf! ;
[IFDEF] android
require unix/jni-location.fs
also android
: coord! ( -- ) location ?dup-IF >o
getLatitude coord" 0 sf[]!
getLongitude coord" 1 sf[]!
getAltitude coord" 2 sf[]!
getSpeed coord" 3 sf[]!
getBearing coord" 4 sf[]!
getAccuracy coord" 5 sf[]!
o>
ELSE
start-gps
THEN ;
:is aback level# @ 0> IF -1 level# +!
ELSE ctrl U inskey ctrl D inskey THEN ;
previous
[ELSE]
[IFDEF] has-gpsd?
s" unix/gpslib.fs" ' required catch [IF]
2drop : coord! ;
[ELSE]
0 Value gps-opened?
: coord! ( -- ) gps-opened? 0= IF
gps-local-open 0= to gps-opened?
gps-opened? 0= ?EXIT
THEN
gps-fix { fix }
fix gps:gps_fix_t-latitude df@ coord" 0 sf[]!
fix gps:gps_fix_t-longitude df@ coord" 1 sf[]!
fix [IFDEF] gps:gps_fix_t-altHAE gps:gps_fix_t-altHAE
[ELSE] [IFDEF] gps:gps_fix_t-altMSL gps:gps_fix_t-altMSL
[ELSE] [IFDEF] gps:gps_fix_t-altitude gps:gps_fix_t-altitude
[ELSE] drop 0e { f^ dummy } dummy
[THEN]
[THEN]
[THEN] df@ coord" 2 sf[]!
fix gps:gps_fix_t-speed df@ coord" 3 sf[]!
fix gps:gps_fix_t-track df@ coord" 4 sf[]!
fix gps:gps_fix_t-epx df@ f**2
fix gps:gps_fix_t-epy df@ f**2
f+ fsqrt coord" 5 sf[]! ;
[THEN]
[ELSE]
: coord! ( -- ) ;
[THEN]
[THEN]
: .coords ( addr u -- ) $>align drop
dup 0 sf[]@ fdup fabs .deg f0< 'S' 'N' rot select emit space
dup 1 sf[]@ fdup fabs .deg f0< 'W' 'E' rot select emit space
dup 2 sf[]@ 7 1 0 f.rdp ." m "
dup 3 sf[]@ 8 2 0 f.rdp ." km/h "
dup 4 sf[]@ 8 2 0 f.rdp ." ° ~"
dup 5 sf[]@ fsplit 0 .r '.' emit 100e f* f>s .## ." m"
drop ;
: pk.host ( -- addr u ) [: pk@ type host$ $. ;] $tmp ;
: pk1.host ( -- addr u ) [: pk@ key| type host$ $. ;] $tmp ;
Forward msg:last?
Forward msg:last
hash: have# \ list of owner ids per hash
hash: have-group# \ list of interested groups per hash
hash: fetch# \ list of wanted hashs->fetcher objects
\ state: want, fetching, got it
\ methods: want->fetch, fetching-progress, fetch->got it
hash: fetch-finish#
Variable fetch-queue[]
: >send-have ( addr u -- )
msg-group-o >r [:
have-group# #@ dup IF
bounds ?DO
fetch( ." send have to group '"
msg-group-o .msg:name$ forth:type
." ' about hash '" ihave$ $@ 85type forth:cr )
I @ to msg-group-o 0 .(avalanche-msg)
push[] ['] >msg-log $[]map
cell +LOOP
ELSE 2drop THEN
;] catch
r> to msg-group-o cleanup-msg throw ;
forward ihave>push
also fetcher
fetcher:class :method fetch fetching# to state ;
' 2drop fetcher:class is fetching
fetcher:class :method got-it have# to state
last# $@ 2dup ihave$ $+! ihave>push
>send-have ;
previous
: [email protected] ( pk+host u -- )
'@' emit
2dup keysize safe/string type '.' emit
key| .simple-id ;
: .ihaves ( -- )
." ====== hash owend by ======" cr
have# [: dup $@ 85type ." :"
cell+ $@ bounds U+DO
space I $@ [email protected]
cell +LOOP cr ;] #map ;
: check-ihave ( sig u1 hash u2 -- sig u1 hash u2 )
c:0key 2dup c:hash 2over dup sigpksize# u< IF sig-unsigned !!sig!! THEN
2dup sigpksize# - 2dup c:hash + date-sig? !!sig!! 2drop ;
: gen-ihave ( hash u1 -- sig u2 )
host$ $@ [: .pk type ;] $tmp ;
: >ihave.id ( hash u1 pk.id u2 -- )
2swap bounds U+DO 2dup I keysize have# #!ins[] keysize +LOOP 2drop ;
: >ihave ( hash u -- )
0 .gen-ihave >ihave.id ;
: msg-pack ( -- xt )
0 push[] !@ 0 ihave$ !@
[{: push ihave :}h1 push push[] ! ihave ihave$ ! ;] ;
: push-msg ( o:parent -- )
up@ receiver-task <> IF
avalanche-msg
ELSE wait-task @ ?dup-IF
>r
o msg-group-o msg-pack [{: xo group-o xt: pack :}h1
pack
avalanche( ." Avalanche to: " group-o h. cr )
group-o to msg-group-o xo .avalanche-msg ;]
r> send-event
THEN
THEN ;
: show-msg ( addr u -- )
parent dup IF .wait-task @ dup up@ <> and THEN
?dup-IF
dup >r <hide>
$make o msg-group-o
[{: w^ m xo group-o :}h1
group-o to msg-group-o
xo >o m $@ do-msg-nestsig m $free o>
ctrl L inskey ;]
r> send-event
ELSE do-msg-nestsig THEN ;
: date>i ( date -- i )
msg-group-o .msg:log[] dup $[]# >r $search[]date r> 1- umin ;
: date>i' ( date -- i )
msg-group-o .msg:log[] dup $[]# >r $search[]date r> umin ;
: sighash? ( addr u -- flag )
over le-64@ date>i
dup 0< IF drop 2drop false EXIT THEN >r
over le-64@ 64#1 64+ date>i' >r [ 1 64s ]L /string
r> r> U+DO
c:0key I msg-group-o .msg:log[] $[]@ sigonly@ >hash
2dup hashtmp over str= IF
verbose( ." match @" i . forth:cr )
I to log# 2drop true UNLOOP EXIT
ELSE verbose( 2dup 85type ." <> " hashtmp over 85type ) THEN
LOOP
2drop false ;
: msg-key! ( addr u -- )
0 msg-group-o .msg:keys[] [: rot >r 2over str= r> or ;] $[]map
IF 2drop ELSE \ ." msg-key+ " 2dup 85type forth:cr
$make msg-group-o .msg:keys[] >back THEN ;
\ message commands
scope{ net2o-base
\g
\g ### message commands ###
\g
reply-table $@ inherit-table msg-table
$20 net2o: msg-start ( $:pksig -- ) \g start message
1 !!>order? $> msg:start ;
+net2o: msg-tag ( $:tag -- ) \g tagging (can be anywhere)
2 !!>=order? $> msg:tag ;
+net2o: msg-id ( $:id -- ) \g a hash id
2 !!>=order? $> msg:id ;
+net2o: msg-chain ( $:dates,sighash -- ) \g chained to message[s]
2 !!>=order? $> msg:chain ;
+net2o: msg-signal ( $:pubkey -- ) \g signal message to one person
2 !!>=order? $> msg:signal ;
+net2o: msg-re ( $:hash ) \g relate to some object
2 !!>=order? $> msg:re ;
+net2o: msg-text ( $:msg -- ) \g specify message string
2 !!>=order? $> msg:text ;
+net2o: msg-object ( $:object type -- ) \g specify an object, e.g. an image
2 !!>=order? 64>n $> rot msg:object ;
+net2o: msg-action ( $:msg -- ) \g specify action string
2 !!>=order? $> msg:action ;
+net2o: msg-payment ( $:contract -- ) \g payment transaction
2 !!>=order? $> msg:payment ;
+net2o: msg-otrify ( $:date+sig $:newdate+sig -- ) \g turn a past message into OTR
$> $> msg:otrify ;
+net2o: msg-coord ( $:gps -- ) \g GPS coordinates
2 !!>=order? $> msg:coord ;
+net2o: msg-url ( $:url -- ) \g specify message URL
$> msg:url ;
+net2o: msg-like ( xchar -- ) \g add a like
64>n msg:like ;
+net2o: msg-lock ( $:key -- ) \g lock down communciation
$> msg:lock ;
+net2o: msg-unlock ( -- ) \g unlock communication
msg:unlock ;
+net2o: msg-perms ( $:pk perm -- ) \g permissions
$> msg:perms ;
+net2o: msg-vote ( xchar -- ) \g add a vote tag; votes are set by likes
64>n msg:vote ;
+net2o: msg-text+format ( $text format -- )
64>n >r $> r> msg:text+format ;
$60 net2o: msg-silent-start ( $:pksig -- ) \g silent message tag
1 !!>order? $40 c-state ! $> msg:silent-start ;
+net2o: msg-hashs ( $:hashs -- ) \g ihave part 1 within signed message
( $40 !!order? ) $> msg:hashs ;
+net2o: msg-hash-id ( $:id -- ) \g ihave part 2 within signed message
( $41 !!order? ) $> msg:hash-id ;
+net2o: msg-otrify2 ( $:date+sig $:newdate+sig -- ) \g turn a past message into OTR, silent version
$40 !!order? $> $> msg:otrify ;
+net2o: msg-updates ( $:fileinfo $:hash -- ) \g Files got an update.
\g The fileinfo string contains fileno:len tuples in command encoding.
\g Each additional context is hashed to a 64 byte hash, and all the hashs
\g are hashed together sequentially in the same order as the fileinfo
\g describes.
$40 !!order? $> $> msg:updates ;
}scope
msg-table $save
' context-table is gen-table
\ Code for displaying messages: logstyle for TUI deferred-based
: .otr-info ( -- )
<info> ." [otr] " <default> "[otr] " notify+ notify-otr? on ;
: .otr-err ( -- )
<err> ." [exp] " <default> 1 notify-otr? ! ;
: .otr ( tick -- )
64dup 64#-1 64= IF 64drop notify-otr? off EXIT THEN
ticks 64- 64dup fuzzedtime# 64negate 64< IF 64drop .otr-err EXIT THEN
otrsig-delta# fuzzedtime# 64+ 64< IF .otr-info THEN ;
: >notify-otr? ( tick -- )
64dup 64#-1 64= IF 64drop notify-otr? off EXIT THEN
ticks 64- 64dup fuzzedtime# 64negate 64< IF 64drop 1 notify-otr? ! EXIT THEN
otrsig-delta# fuzzedtime# 64+ 64< IF notify-otr? on THEN ;
config:logmask-tui# Value logmask#
: .log-num ( -- )
logmask# @ log:num and IF '#' emit log# u. THEN ;
: .log-date ( 64ticks -- )
logmask# @ log:date and IF .ticks space ELSE 64drop THEN ;
: .log-end ( 64ticks -- )
logmask# @ log:end and IF 64dup .ticks space THEN .otr ;
\ logstyle for GUI bitmask-based
Defer update-log
' noop is update-log
: .group ( addr u -- )
2dup printable? IF forth:type ELSE ." @" .key-id THEN ;
: +style: ( n -- )
Create , DOES> @ logmask# or! update-log ;
: -style: ( n -- )
Create , DOES> @ invert logmask# and! update-log ;
: create-styles ( -- )
[: { nt }
nt name>interpret execute dup
nt name>string [: '+' emit type ;] $tmp nextname +style:
nt name>string [: '-' emit type ;] $tmp nextname -style:
true ;] ['] log >wordlist traverse-wordlist ;
scope: logstyles
create-styles
}scope
msg-notify-class :method msg:start ( addr u -- )
2dup key| 0 .pk@ key| str= IF 2drop un-cmd EXIT THEN
last# >r 2dup key| to msg:id$
[: .simple-id ." : " ;] $tmp notify-nick!
r> to last# ;
' 2drop msg-notify-class is msg:silent-start
msg-notify-class :method msg:tag ( addr u -- ) "#" notify+ $utf8> notify+
;
msg-notify-class :method msg:signal ( addr u -- )
2dup [: ." @" .simple-id ;] $tmp notify+ ;
msg-notify-class :method msg:text ( addr u -- ) $utf8> notify+ ;
msg-notify-class :method msg:text+format ( addr u format -- ) drop $utf8> notify+ ;
msg-notify-class :method msg:url ( addr u -- ) $utf8> notify+ ;
msg-notify-class :method msg:action ( addr u -- ) $utf8> notify+ ;
' 2drop msg-notify-class is msg:chain
' 2drop msg-notify-class is msg:re
' 2drop msg-notify-class is msg:lock
' noop msg-notify-class is msg:unlock
msg-notify-class :method msg:perms 2drop 64drop ;
' drop msg-notify-class is msg:away
' 2drop msg-notify-class is msg:coord
msg-notify-class :method msg:otrify 2drop 2drop ." otrify " ;
' 2drop msg-notify-class is msg:hashs
' 2drop msg-notify-class is msg:hash-id
msg-notify-class :method msg:object case
msg:image# of 2drop "img[] " notify+ endof
msg:thumbnail# of 2drop "thumb[] " notify+ endof
msg:audio# of 2drop "audio[] " notify+ endof
msg:video# of 2drop "video[] " notify+ endof
2drop
endcase ;
msg-notify-class :method msg:end ( -- )
msg-notify ;
msg-notify-class :method msg:like ( xchar -- ) ['] xemit $tmp notify+ ;
msg-notify-class :method msg:vote ( xchar -- ) [: cr ." vote: " xemit ;] $tmp notify+ ;
\ msg scan for hashes class
msg:class class
field: ?hashs[]
end-class msg-?hash-class
' 2drop msg-?hash-class is msg:start
' noop msg-?hash-class is msg:end
' 2drop msg-?hash-class is msg:tag
' 2drop msg-?hash-class is msg:signal
' 2drop msg-?hash-class is msg:chain
' 2drop msg-?hash-class is msg:id
' 2drop msg-?hash-class is msg:re
' 2drop msg-?hash-class is msg:text
msg-?hash-class :method msg:text+format 2drop drop ;
' 2drop msg-?hash-class is msg:url
' drop msg-?hash-class is msg:like
' drop msg-?hash-class is msg:vote
msg-?hash-class :method msg:lock ( addr u -- )
0 .v-dec$ dup IF
msg-key! msg-group-o .msg:+lock THEN ;
msg-?hash-class :method msg:unlock ( -- )
msg-group-o .msg:-lock ;
' drop msg-?hash-class is msg:away
msg-?hash-class :method msg:perms 2drop 64drop ;
msg-?hash-class :method msg:object ( addr u id -- )
case
msg:image# of key| ?hashs[] $+[]! endof
msg:thumbnail# of key| ?hashs[] $+[]! endof
msg:patch# of key| ?hashs[] $+[]! endof
msg:snapshot# of key| ?hashs[] $+[]! endof
msg:audio# of key| ?hashs[] $+[]! endof
msg:audio-idx# of key| ?hashs[] $+[]! endof
msg:video# of key| ?hashs[] $+[]! endof
msg:video-idx# of key| ?hashs[] $+[]! endof
2drop
endcase ;
: msg-scan-hash ( ... xt -- ... )
msg-?hash-class new >o
msg-table @ token-table !
catch dispose o> throw ;
\ main message class
msg:class :method msg:start ( addr u -- )
last# >r 2dup key| to msg:id$
false to msg:silent?
.log-num
2dup startdate@ .log-date
2dup enddate@ .log-end
.key-id ." : "
r> to last# ;
msg:class :method msg:silent-start ( addr u -- )
silent( 2dup startdate@ .log-date 2dup .key-id ." : Silent: " )
key| to msg:id$ true to msg:silent? ;
msg:class :method msg:tag ( addr u -- ) $utf8>
<warn> '#' forth:emit .group <default> ;
msg:class :method msg:signal ( addr u -- ) last# >r
key| 2dup 0 .pk@ key| str=
IF <err> THEN ." @" .key-id? <default>
r> to last# ;
msg:class :method msg:chain ( addr u -- )
2dup sighash? IF <info> ELSE <err> THEN
." <" over le-64@ .ticks
verbose( dup keysize - /string ." ," 85type )else( 2drop ) <default>
;
msg:class :method msg:re ( addr u -- )
space <warn> ." [" 85type ." ]->" <default> ;
msg:class :method msg:id ( addr u -- )
space <warn> ." [" 85type ." ]:" <default> ;
msg:class :method msg:text ( addr u -- ) utf8-sanitize ;
: format>ansi ( format -- ansi )
0
over msg:#bold and 0<> Bold and or
over msg:#italic and 0<> Italic and or
over msg:#underline and 0<> Underline and or
swap msg:#strikethrough and 0<> Strikethrough and or ;
msg:class :method msg:text+format ( addr u format -- )
format>ansi attr!
utf8-sanitize 0 attr! ;
msg:class :method msg:url ( addr u -- ) $utf8>
<warn> encode-% forth:type <default> ;
msg:class :method msg:like ( xchar -- )
<info> utf8emit <default> ;
msg:class :method msg:vote ( xchar -- )
<info> cr ." vote: " utf8emit <default> ;
msg:class :method msg:lock ( addr u -- )
0 .v-dec$ dup IF
msg-key! msg-group-o .msg:+lock
<info> ." chat is locked" <default>
ELSE 2drop
<err> ." locked out of chat" <default>
THEN ;
msg:class :method msg:unlock ( -- ) msg-group-o .msg:-lock
<info> ." chat is free for all" <default> ;
' drop msg:class is msg:away
: .perms ( n -- )
"👹" bounds U+DO
dup 1 and IF I xc@ xemit THEN 2/
I delta-I x-size +LOOP drop ;
msg:class :method msg:perms { 64^ perm d: pk -- }
perm [ 1 64s ]L pk msg-group-o .msg:perms# #!
pk .key-id ." : " perm 64@ 64>n .perms space
;
msg:class :method msg:hashs ( hash u -- )
silent( ." hash: " 2dup 85type forth:cr )
to msg:hashs$
;
msg:class :method msg:hash-id ( id u -- )
silent( ." id: " 2dup forth:type forth:cr )
fetch( ." ihave:" msg:id$ .key-id '.' emit 2dup type msg:hashs$ bounds U+DO
forth:cr I keysize 85type keysize +LOOP forth:cr )
msg:id$ key| [: type type ;] $tmp
msg:hashs$ 2swap >ihave.id
;
: >hash-finish { w^ xt d: hash -- }
xt cell hash key| fetch-finish# #+! ;
: hash-finished { d: hash -- }
fetch( ." finished " 2dup 85type forth:cr )
hash fetch# #@ IF cell+ .fetcher:got-it ELSE drop THEN
hash >ihave
hash fetch-finish# #@ bounds U+DO
hash I @ execute
cell +LOOP
last# bucket-off
hash drop free throw ;
: fetch-hash ( hashaddr u tsk -- )
>r save-mem
fetch( ." fetching " 2dup 85type forth:cr )
2dup fetch# #@ IF cell+ .fetcher:fetch ELSE drop THEN
2dup net2o:copy#
r> [{: d: hash tsk :}h1
hash [{: d: hash :}h1 hash hash-finished ;]
tsk send-event ;]
lastfile@ >o is file-xt o> ;
: fetch-hashs ( addr u tsk pk$ -- )
{ tsk pk$ | hashs }
fetch( ." fetch from " pk$ $@ [email protected] forth:cr )
bounds U+DO
net2o-code expect+slurp $10 blocksize! $A blockalign!
I' I U+DO
false I keysize have# #@ bounds U+DO
I $@ pk$ $@ str= or
cell +LOOP
IF
I keysize tsk fetch-hash
1 +to hashs
THEN
hashs $10 u>= ?LEAVE
keysize +LOOP
end-code| net2o:close-all
keysize hashs * 0 to hashs +LOOP ;
: fetch-queue { tsk w^ want# -- }
0 .pk1.host $make { w^ pk$ }
want# tsk pk$ [{: tsk pk$ :}l { want }
want $@ pk$ $@ str= IF
msg( ." I really should have this myself" forth:cr )
\ don't fetch from myself
ELSE
want $@ [: $8 $E pk1-connect? ;] catch-nobt 0=
IF
IF
+resend +flow-control
want cell+ $@ tsk want fetch-hashs
disconnect-me
THEN
ELSE
fetch( ." failed, doesn't connect" forth:cr )
2drop
THEN
THEN ;] #map
want# #frees
pk$ $free ;
: fetch>want ( -- want# )
{ | w^ want# }
fetch# want# [{: want# :}l
dup cell+ $@ drop cell+ >o fetcher:state o> 0= IF
$@ 2dup have# #@ bounds U+DO
2dup I $@ want# #+!
cell +LOOP 2drop
ELSE drop THEN ;] #map
want# @ ;
fetcher:class ' new static-a with-allocater Constant fetcher-prototype
: >fetch# ( addr u -- )
[: 2dup fetch# #@ d0= IF
fetcher-prototype cell- [ fetcher:class >osize @ cell+ ]L
2over fetch# #!
THEN ;] resize-sema c-section 2drop ;
: transmit-queue ( queue -- )
up@ [{: w^ queue[] task :}h1
queue[] ['] >fetch# $[]map
task fetch>want fetch-queue ;]
?query-task send-event ;
Variable queue?
: enqueue ( -- )
-1 queue? !@ 0= IF
[: 0 fetch-queue[] !@ queue? off transmit-queue ;]
up@ send-event
THEN ;
forward need-hashed?
: >have-group ( addr u -- )
last# >r
msg-group-o { w^ grp }
2dup have-group# #@ nip IF
grp last# cell+ +unique$ 2drop
ELSE
grp cell 2swap have-group# #!
THEN r> to last# ;
: >fetch-queue ( addr u -- )
2dup need-hashed? IF
fetch-queue[] $ins[] drop
ELSE >ihave THEN ;
: ?fetch ( addr u -- )
key| 2dup >have-group >fetch-queue ;
: .posting ( addr u -- )
2dup keysize /string
2dup printable? IF '[' emit type '@' emit
ELSE ." #[" 85type ." /@" THEN
key| .key-id? ;
: rotate@ ( addr u -- rotate )
keysize safe/string IF c@ ELSE 0 THEN ;
msg:class :method msg:object ( addr u type -- )
space <warn> case
msg:image# of ." img[" 2dup 85type ?fetch endof
msg:thumbnail# of ." thumb[" 2dup key| 85type
space 2dup rotate@ 0 ['] u.r $10 base-execute
?fetch endof
msg:audio# of ." audio[" 2dup 85type ?fetch endof
msg:video# of ." video[" 2dup 85type ?fetch endof
msg:audio-idx# of ." audio-idx[" 2dup 85type ?fetch endof
msg:video-idx# of ." video-idx[" 2dup 85type ?fetch endof
msg:patch# of ." patch[" 85type endof
msg:snapshot# of ." snapshot[" 85type endof
msg:message# of ." message[" 85type endof
msg:posting# of ." posting" .posting endof
msg:filename# of ." filename[" encode-% type endof
drop .posting
0
endcase ." ]" <default> ;
msg:class :method msg:action ( addr u -- ) $utf8>
<warn> forth:type <default> ;
msg:class :method msg:coord ( addr u -- )
<warn> ." GPS: " .coords <default> ;
: wait-2s-key ( -- )
ntime 50 0 DO key? ?LEAVE
2dup i #40000000 um* d+ deadline LOOP 2drop ;
: xclear ( addr u -- ) x-width 1+ x-erase ;
msg:class :method msg:.nobody ( -- )
<info>
[: ." nobody's online" msg-group-o .msg:?otr 0= IF ." , saving away" THEN ;] $tmp
2dup type <default>
wait-2s-key xclear ;
\ encrypt+sign
\ features: signature verification only when key is known
\ identity only revealed when correctly decrypted
: msg-dec-sig? ( addr u -- addr' u' flag )
sigpksize# - 2dup + { pksig }
msg-group-o .msg:keys[] $@ bounds U+DO
I $@ 2over pksig decrypt-sig?
dup -5 <> IF
>r 2nip r> unloop EXIT
THEN drop 2drop
cell +LOOP
sigpksize# + -5 ;
: msg-sig? ( addr u -- addr u' flag )
skip-sig? @ IF quicksig( pk-quick-sig? )else( pk-date? )
ELSE pk-sig? THEN ;
: msg-dec?-sig? ( addr u -- addr' u' flag )
2dup 2 - + c@ $80 and IF msg-dec-sig? ELSE msg-sig? THEN ;
: msg-dec?-sig?-fast ( addr u -- addr' u' flag )
2dup 2 - + c@ $80 and IF msg-dec-sig? ELSE pk-date? THEN ;
: ?msg-dec-sig? ( addr u -- addr' u' )
2dup 2 - + c@ $80 and IF msg-dec-sig? !!sig!! THEN ;
: msg-log-dec@ ( index -- addr u )
msg-group-o .msg:log[] $[]@ ?msg-dec-sig? ;
: replace-sig { addrsig usig addrmsg umsg -- }
addrsig usig addrmsg umsg usig - [: type type ;] $tmp
2dup msg-dec?-sig? !!sig!! 2drop addrmsg umsg smove ;
: new-otrsig ( addr u flag -- addrsig usig )
>r 2dup startdate@ old>otr
predate-key keccak# c:key@ c:key# smove
[: sktmp pkmod sk@ drop >modkey .encsign-rest ;]
['] .sig r@ select $tmp
2dup + 2 - r> swap cor!
( 2dup dump ) 1 64s /string ;
msg:class :method msg:otrify { sig u' addr u -- }
u' 64'+ u = u sigsize# = and IF
addr u startdate@ 64dup date>i >r 64#1 64+ date>i' r>
\ 2dup = IF ." [otrified] " addr u startdate@ .ticks THEN
U+DO
I msg-log-dec@
2dup dup sigpksize# - /string key| msg:id$ str= IF
dup u - /string addr u str= IF
otrify( I [: ." [OTRifying] #" u. forth:cr ;] do-debug )
I [: ." [OTRify] #" u. ;] $tmp forth:type
sig u' I msg-group-o .msg:log[] $[]@ replace-sig
save-msgs&
ELSE
." [OTRified] #" I u.
THEN
ELSE
otrify( I [: ." [OTRifignore] #" u. forth:cr ;] do-debug )
2drop
THEN
LOOP
THEN ;
msg:class :method msg:end ( -- )
msg:silent? 0= IF forth:cr THEN enqueue ;
\g
\g ### group description commands ###
\g
hash: group#
static-a to allocater
align here
groups:class new Constant group-o
dynamic-a to allocater
here over - 2Constant sample-group$
: last>o ( -- )
\G use last hash access as object
last# cell+ $@ drop cell+ >o rdrop ;
: make-group ( addr u -- o:group )
sample-group$ 2over group# #! last>o to groups:id$ ;
cmd-table $@ inherit-table group-table
scope{ net2o-base
$20 net2o: group-name ( $:name -- ) \g group symbolic name
$> make-group ;
+net2o: group-id ( $:group -- ) \g group id, is a pubkey
group-o o = !!no-group-name!! $> to groups:id$ ;
+net2o: group-member ( $:memberkey -- ) \g add member key
group-o o = !!no-group-name!! $> groups:member[] $+[]! ;
+net2o: group-admin ( $:adminkey -- ) \g set admin key
group-o o = !!no-group-name!! $> groups:admin sec! ;
+net2o: group-perms ( 64u -- ) \g permission/modes bitmask
group-o o = !!no-group-name!! to groups:perms# ;
}scope
group-table $save
group-table @ group-o .token-table !
' context-table is gen-table
: .chats/group ( -- addr u )
pk@ pkc swap move sk@ skc swap move \ normalize pkc
pkc keysize 3 * \ hash of pkc+pk1+skc keyed with "group"
"group" keyed-hash#128 .chats/ ;
: read-chatgroups ( -- )
0 ..chats/group [: type ." .v2o" ;] $tmp
2dup file-status nip no-file# = IF 2drop EXIT THEN
decrypt@ group-o .do-cmd-loop enc-file $free ;
also net2o-base
: serialize-chatgroup ( last# -- )
dup $@ 2dup $, group-name
rot cell+ $@ drop cell+ >o
groups:id$ dup IF
2tuck str= 0= IF $, group-id ELSE 2drop THEN
ELSE 2drop 2drop THEN
groups:member[] [: $, group-member ;] $[]map
groups:admin sec@ dup IF sec$, group-admin ELSE 2drop THEN
groups:perms# 64dup 64-0<> IF lit, group-perms ELSE 64drop THEN
o> ;
previous
: admin>pk ( -- )
groups:admin sec@ drop dup sk-mask
keysize addr groups:id$ $!len
groups:id$ drop sk>pk ;
: gen-admin-key ( -- )
$20 rng$ groups:admin sec! admin>pk ;
: save-chatgroups ( -- )
0 ..chats/group enc-filename $!
[: group# ['] serialize-chatgroup #map ;] gen-cmd enc-file $!buf
pk-off key-list encfile-rest ;
Variable group-list[]
: $ins[]group ( o:group $array -- pos )
\G insert O(log(n)) into pre-sorted array
\G @var{pos} is the insertion offset or -1 if not inserted
{ a[] } 0 a[] $[]#
BEGIN 2dup u< WHILE 2dup + 2/ { left right $# }
o $@ $# a[] $[] @ $@ compare dup 0= IF
drop o cell+ $@ drop cell+ .groups:id$
$# a[] $[] @ cell+ $@ drop cell+ .groups:id$ compare THEN
0< IF left $# ELSE $# 1+ right THEN
REPEAT drop >r
o { w^ ins$0 } ins$0 cell a[] r@ cells $ins r> ;
: groups>sort[] ( -- ) group-list[] $free
group# [: >o group-list[] $ins[]group o> drop ;] #map ;
: .chatgroup ( last# -- )
dup $. space dup $@ rot cell+ $@ drop cell+ >o
groups:id$ 2tuck str=
IF ." =" 2drop
ELSE ''' emit <info> 85type <default> ''' emit THEN space