interplane-forge
INTERPLANE Forge
Brought by drakestapleton
interplane-forge / social / 30store.om
| 1 | note social/30store.om: the social store. ~/forge-data/social.log is the only source of truth: |
| 2 | note one JSON event per line, {"seq":N,"ts":S,"did":"did:key:z…","action":A,"target":T,"prev":H} |
| 3 | note where H = hex sha256 of the previous line's bytes (64 zeros for line 1). social.head holds |
| 4 | note "<seq> <hex sha256 of line seq>\n" and is replaced (rename) after every append, so a changed, |
| 5 | note dropped or truncated line anywhere is detected by soload, which then disables social (fail closed). |
| 6 | note Memory: string intern pool (ids 1..), relation set (kind, actor, target), counts, accounts, |
| 7 | note names, sessions, used nonces, per-DID write counter. All rebuilt from the log at start / SIGHUP. |
| 8 | note ---- key buffer gskb and the intern pool |
| 9 | fn sokclr to void |
| 10 | put gskbl 0 0 |
| 11 | put gskov 0 0 |
| 12 | end |
| 13 | fn sokc c uchar to void |
| 14 | n = get gskbl 0 |
| 15 | if ge n len gskb |
| 16 | put gskov 0 1 |
| 17 | ret |
| 18 | end |
| 19 | put gskb n c |
| 20 | put gskbl 0 add n 1 |
| 21 | end |
| 22 | fn sokt t view uchar * to void |
| 23 | for i ulong len t bound 512 |
| 24 | sokc get t i |
| 25 | end |
| 26 | end |
| 27 | note sokid: append the bytes of string id |
| 28 | fn sokid id ulong to void |
| 29 | o = get gsso id |
| 30 | for i ulong get gssl id bound 4096 |
| 31 | sokc get gsstr add o i |
| 32 | end |
| 33 | end |
| 34 | fn sokhash to ulong |
| 35 | h = ulong 14695981039346656037 |
| 36 | for i ulong get gskbl 0 bound 512 |
| 37 | h = xor h ulong get gskb i |
| 38 | h = mul h 1099511628211 |
| 39 | end |
| 40 | ret h |
| 41 | end |
| 42 | fn sokeq id ulong to bool |
| 43 | if ne get gssl id get gskbl 0 |
| 44 | ret false |
| 45 | end |
| 46 | o = get gsso id |
| 47 | for i ulong get gskbl 0 bound 512 |
| 48 | if ne get gsstr add o i get gskb i |
| 49 | ret false |
| 50 | end |
| 51 | end |
| 52 | ret true |
| 53 | end |
| 54 | fn soslot to ulong |
| 55 | m = sub len gssh 1 |
| 56 | s = and sokhash m |
| 57 | for it ulong len gssh |
| 58 | id = get gssh s |
| 59 | if eq id 0 |
| 60 | ret s |
| 61 | end |
| 62 | if sokeq id |
| 63 | ret s |
| 64 | end |
| 65 | s = and add s 1 m |
| 66 | end |
| 67 | ret len gssh |
| 68 | end |
| 69 | note sofind: id of the string in gskb, 0 when absent |
| 70 | fn sofind to ulong |
| 71 | if ne get gskov 0 0 |
| 72 | ret 0 |
| 73 | end |
| 74 | s = soslot |
| 75 | if ge s len gssh |
| 76 | ret 0 |
| 77 | end |
| 78 | ret get gssh s |
| 79 | end |
| 80 | note sointern: id of the string in gskb, created when absent; 0 when the pool is full |
| 81 | fn sointern to ulong |
| 82 | if ne get gskov 0 0 |
| 83 | ret 0 |
| 84 | end |
| 85 | s = soslot |
| 86 | if ge s len gssh |
| 87 | ret 0 |
| 88 | end |
| 89 | id = get gssh s |
| 90 | if ne id 0 |
| 91 | ret id |
| 92 | end |
| 93 | id = get gsns 0 |
| 94 | kl = get gskbl 0 |
| 95 | o = get gsstrl 0 |
| 96 | if or ge id len gsso gt add o kl len gsstr |
| 97 | ret 0 |
| 98 | end |
| 99 | for i ulong kl bound 512 |
| 100 | put gsstr add o i get gskb i |
| 101 | end |
| 102 | put gsso id o |
| 103 | put gssl id kl |
| 104 | put gsstrl 0 add o kl |
| 105 | put gsns 0 add id 1 |
| 106 | put gssh s id |
| 107 | ret id |
| 108 | end |
| 109 | note sokpre: gskb starts with t |
| 110 | fn sokpre t view uchar * to bool |
| 111 | if lt get gskbl 0 len t |
| 112 | ret false |
| 113 | end |
| 114 | for i ulong len t bound 512 |
| 115 | if ne get gskb i get t i |
| 116 | ret false |
| 117 | end |
| 118 | end |
| 119 | ret true |
| 120 | end |
| 121 | note ---- relations: kind 1 star, 2 fav, 3 watch, 4 follow; actor and target are string ids |
| 122 | fn sorkey kind ulong a ulong b ulong to ulong |
| 123 | ret or or shl kind 48 shl a 24 b |
| 124 | end |
| 125 | fn sorslot k ulong to ulong |
| 126 | m = sub len gsrk 1 |
| 127 | s = and shr mul k 11400714819323198485 40 m |
| 128 | for it ulong len gsrk |
| 129 | v = get gsrk s |
| 130 | if or eq v 0 eq v k |
| 131 | ret s |
| 132 | end |
| 133 | s = and add s 1 m |
| 134 | end |
| 135 | ret len gsrk |
| 136 | end |
| 137 | fn soron kind ulong a ulong b ulong to bool |
| 138 | k = sorkey kind a b |
| 139 | s = sorslot k |
| 140 | if ge s len gsrk |
| 141 | ret false |
| 142 | end |
| 143 | ret and eq get gsrk s k ne get gsrv s 0 |
| 144 | end |
| 145 | note soset: set the relation; true when it changed (counts follow) |
| 146 | fn soset kind ulong a ulong b ulong on bool to bool |
| 147 | k = sorkey kind a b |
| 148 | s = sorslot k |
| 149 | if ge s len gsrk |
| 150 | ret false |
| 151 | end |
| 152 | cur = and eq get gsrk s k ne get gsrv s 0 |
| 153 | if and cur on |
| 154 | ret false |
| 155 | end |
| 156 | if and not cur not on |
| 157 | ret false |
| 158 | end |
| 159 | put gsrk s k |
| 160 | v = uchar 0 |
| 161 | if on |
| 162 | v = uchar 1 |
| 163 | end |
| 164 | put gsrv s v |
| 165 | sobump kind a b on |
| 166 | ret true |
| 167 | end |
| 168 | fn soinc v ulong on bool to ulong |
| 169 | if on |
| 170 | ret add v 1 |
| 171 | end |
| 172 | if eq v 0 |
| 173 | ret v |
| 174 | end |
| 175 | ret sub v 1 |
| 176 | end |
| 177 | fn sobump kind ulong a ulong b ulong on bool to void |
| 178 | if eq kind 1 |
| 179 | put gscst b soinc get gscst b on |
| 180 | end |
| 181 | if eq kind 2 |
| 182 | put gscfv b soinc get gscfv b on |
| 183 | end |
| 184 | if eq kind 3 |
| 185 | put gscwt b soinc get gscwt b on |
| 186 | end |
| 187 | if eq kind 4 |
| 188 | put gscfl b soinc get gscfl b on |
| 189 | put gscfg a soinc get gscfg a on |
| 190 | end |
| 191 | end |
| 192 | note ---- strict line parser over ln (cursor gspc, failure flag gsbad) |
| 193 | fn sofail to void |
| 194 | put gsbad 0 1 |
| 195 | end |
| 196 | fn solit ln view uchar * t view uchar * to void |
| 197 | p = get gspc 0 |
| 198 | if gt add p len t len ln |
| 199 | sofail |
| 200 | ret |
| 201 | end |
| 202 | for i ulong len t bound 64 |
| 203 | if ne get ln add p i get t i |
| 204 | sofail |
| 205 | ret |
| 206 | end |
| 207 | end |
| 208 | put gspc 0 add p len t |
| 209 | end |
| 210 | fn sonum ln view uchar * to ulong |
| 211 | p = get gspc 0 |
| 212 | v = ulong 0 |
| 213 | k = ulong 0 |
| 214 | for it ulong 16 |
| 215 | if ge add p k len ln |
| 216 | break |
| 217 | end |
| 218 | c = get ln add p k |
| 219 | if or lt c 48 gt c 57 |
| 220 | break |
| 221 | end |
| 222 | v = add mul v 10 ulong sub c 48 |
| 223 | k = add k 1 |
| 224 | end |
| 225 | if or eq k 0 gt k 15 |
| 226 | sofail |
| 227 | end |
| 228 | if and gt k 1 eq get ln p 48 |
| 229 | sofail |
| 230 | end |
| 231 | put gspc 0 add p k |
| 232 | ret v |
| 233 | end |
| 234 | fn sostrc c uchar to bool |
| 235 | if and ge c 97 le c 122 |
| 236 | ret true |
| 237 | end |
| 238 | if and ge c 65 le c 90 |
| 239 | ret true |
| 240 | end |
| 241 | if and ge c 48 le c 58 |
| 242 | ret true |
| 243 | end |
| 244 | ret or or eq c 45 eq c 46 or eq c 95 eq c 47 |
| 245 | end |
| 246 | note sostr: append the string at the cursor (charset [A-Za-z0-9:._/-]) to gskb up to the closing quote |
| 247 | fn sostr ln view uchar * to ulong |
| 248 | p = get gspc 0 |
| 249 | k = ulong 0 |
| 250 | for it ulong 400 |
| 251 | if ge add p k len ln |
| 252 | sofail |
| 253 | ret k |
| 254 | end |
| 255 | c = get ln add p k |
| 256 | if eq c 34 |
| 257 | break |
| 258 | end |
| 259 | if not sostrc c |
| 260 | sofail |
| 261 | ret k |
| 262 | end |
| 263 | sokc c |
| 264 | k = add k 1 |
| 265 | end |
| 266 | put gspc 0 add p k |
| 267 | ret k |
| 268 | end |
| 269 | note soact: action code of the bytes at the cursor (1..12), 0 when unknown |
| 270 | fn soact ln view uchar * to ulong |
| 271 | a = ulong 0 |
| 272 | a = sotrya ln a tsastar 1 |
| 273 | a = sotrya ln a tsaunst 2 |
| 274 | a = sotrya ln a tsafav 3 |
| 275 | a = sotrya ln a tsaunfv 4 |
| 276 | a = sotrya ln a tsawat 5 |
| 277 | a = sotrya ln a tsaunwt 6 |
| 278 | a = sotrya ln a tsafol 7 |
| 279 | a = sotrya ln a tsaunfl 8 |
| 280 | a = sotrya ln a tsaccnt 9 |
| 281 | a = sotrya ln a tsaname 10 |
| 282 | a = sotrya ln a tsasess 11 |
| 283 | a = sotrya ln a tsalogo 12 |
| 284 | if eq a 0 |
| 285 | sofail |
| 286 | ret 0 |
| 287 | end |
| 288 | put gspc 0 add get gspc 0 soatx a false |
| 289 | ret a |
| 290 | end |
| 291 | fn sotrya ln view uchar * a ulong t view uchar * code ulong to ulong |
| 292 | if ne a 0 |
| 293 | ret a |
| 294 | end |
| 295 | p = get gspc 0 |
| 296 | if gt add p add len t 1 len ln |
| 297 | ret a |
| 298 | end |
| 299 | for i ulong len t bound 16 |
| 300 | if ne get ln add p i get t i |
| 301 | ret a |
| 302 | end |
| 303 | end |
| 304 | if ne get ln add p len t 34 |
| 305 | ret a |
| 306 | end |
| 307 | ret code |
| 308 | end |
| 309 | note soatx: length of action a's name; when w, also append it to the log line |
| 310 | fn soatx a ulong w bool to ulong |
| 311 | if eq a 1 |
| 312 | ret soatw tsastar w |
| 313 | end |
| 314 | if eq a 2 |
| 315 | ret soatw tsaunst w |
| 316 | end |
| 317 | if eq a 3 |
| 318 | ret soatw tsafav w |
| 319 | end |
| 320 | if eq a 4 |
| 321 | ret soatw tsaunfv w |
| 322 | end |
| 323 | if eq a 5 |
| 324 | ret soatw tsawat w |
| 325 | end |
| 326 | if eq a 6 |
| 327 | ret soatw tsaunwt w |
| 328 | end |
| 329 | if eq a 7 |
| 330 | ret soatw tsafol w |
| 331 | end |
| 332 | if eq a 8 |
| 333 | ret soatw tsaunfl w |
| 334 | end |
| 335 | if eq a 9 |
| 336 | ret soatw tsaccnt w |
| 337 | end |
| 338 | if eq a 10 |
| 339 | ret soatw tsaname w |
| 340 | end |
| 341 | if eq a 11 |
| 342 | ret soatw tsasess w |
| 343 | end |
| 344 | ret soatw tsalogo w |
| 345 | end |
| 346 | fn soatw t view uchar * w bool to ulong |
| 347 | if w |
| 348 | solt t |
| 349 | end |
| 350 | ret len t |
| 351 | end |
| 352 | note soprevok: the 64 hex chars at the cursor equal hex(gsprev) |
| 353 | fn soprevok ln view uchar * to void |
| 354 | p = get gspc 0 |
| 355 | if gt add p 64 len ln |
| 356 | sofail |
| 357 | ret |
| 358 | end |
| 359 | for i ulong 32 |
| 360 | b = ulong get gsprev i |
| 361 | if ne get ln add p mul i 2 sohexc shr b 4 |
| 362 | sofail |
| 363 | end |
| 364 | if ne get ln add add p mul i 2 1 sohexc and b 15 |
| 365 | sofail |
| 366 | end |
| 367 | end |
| 368 | put gspc 0 add p 64 |
| 369 | end |
| 370 | note soapply: parse and apply one line (no newline); false = corrupt (state may be partly applied) |
| 371 | fn soapply ln view uchar * to bool |
| 372 | put gsbad 0 0 |
| 373 | put gspc 0 0 |
| 374 | solit ln tsl1 |
| 375 | seq = sonum ln |
| 376 | solit ln tsl2 |
| 377 | ts = sonum ln |
| 378 | solit ln tsl3 |
| 379 | sokclr |
| 380 | sokt tsdidp |
| 381 | dl = sostr ln |
| 382 | if or lt dl 40 gt dl 60 |
| 383 | sofail |
| 384 | end |
| 385 | did = sointern |
| 386 | solit ln tsl4 |
| 387 | a = soact ln |
| 388 | solit ln tsl5 |
| 389 | sokclr |
| 390 | tl = sostr ln |
| 391 | solit ln tsl6 |
| 392 | soprevok ln |
| 393 | solit ln tsl7 |
| 394 | if or ne get gspc 0 len ln ne seq add get gsseq 0 1 |
| 395 | sofail |
| 396 | end |
| 397 | if or eq did 0 ne get gsbad 0 0 |
| 398 | ret false |
| 399 | end |
| 400 | if not soevent a did ts |
| 401 | ret false |
| 402 | end |
| 403 | sochain ln seq |
| 404 | ret true |
| 405 | end |
| 406 | fn sochain ln view uchar * seq ulong to void |
| 407 | c = new ulong 40 0 |
| 408 | h = new uchar 32 0 |
| 409 | sha256_init c |
| 410 | sha256_update c ln |
| 411 | sha256_final c h |
| 412 | for i ulong 32 |
| 413 | put gsprv2 i get gsprev i |
| 414 | put gsprev i get h i |
| 415 | end |
| 416 | put gsseq 0 seq |
| 417 | end |
| 418 | note soevent: apply action a by did at ts with the target in gskb |
| 419 | fn soevent a ulong did ulong ts ulong to bool |
| 420 | soratev did ts |
| 421 | if le a 6 |
| 422 | if not sokpre tsradp |
| 423 | if or fradsrc not sokpre tsgitp |
| 424 | ret false |
| 425 | end |
| 426 | end |
| 427 | t = sointern |
| 428 | if eq t 0 |
| 429 | ret false |
| 430 | end |
| 431 | x = soset add div sub a 1 2 1 did t eq rem a 2 1 |
| 432 | ret true |
| 433 | end |
| 434 | if le a 8 |
| 435 | if not sokpre tsdidp |
| 436 | ret false |
| 437 | end |
| 438 | t = sointern |
| 439 | if or eq t 0 eq t did |
| 440 | ret false |
| 441 | end |
| 442 | x = soset 4 did t eq a 7 |
| 443 | ret true |
| 444 | end |
| 445 | if eq a 9 |
| 446 | ret soevacc did |
| 447 | end |
| 448 | if eq a 10 |
| 449 | ret soevname did |
| 450 | end |
| 451 | if eq a 11 |
| 452 | ret soevsess did ts |
| 453 | end |
| 454 | ret soevlogo did |
| 455 | end |
| 456 | fn soevacc did ulong to bool |
| 457 | if ne get gskbl 0 0 |
| 458 | ret false |
| 459 | end |
| 460 | put gsacc did 1 |
| 461 | ret true |
| 462 | end |
| 463 | fn soevname did ulong to bool |
| 464 | if not sonameok |
| 465 | ret false |
| 466 | end |
| 467 | n = sointern |
| 468 | if eq n 0 |
| 469 | ret false |
| 470 | end |
| 471 | o = get gsnmo n |
| 472 | if and ne o 0 ne o did |
| 473 | ret false |
| 474 | end |
| 475 | old = get gsnm did |
| 476 | if ne old 0 |
| 477 | put gsnmo old 0 |
| 478 | end |
| 479 | put gsnm did n |
| 480 | put gsnmo n did |
| 481 | ret true |
| 482 | end |
| 483 | note sonameok: gskb is a valid display name [a-z0-9-]{1,39}, no leading/trailing '-' |
| 484 | fn sonameok to bool |
| 485 | n = get gskbl 0 |
| 486 | if or eq n 0 gt n 39 |
| 487 | ret false |
| 488 | end |
| 489 | if or eq get gskb 0 45 eq get gskb sub n 1 45 |
| 490 | ret false |
| 491 | end |
| 492 | for i ulong n bound 64 |
| 493 | c = get gskb i |
| 494 | ok = or and ge c 97 le c 122 and ge c 48 le c 57 |
| 495 | if not or ok eq c 45 |
| 496 | ret false |
| 497 | end |
| 498 | end |
| 499 | ret true |
| 500 | end |
| 501 | note session target "<64 hex session hash>.<32 hex nonce>" |
| 502 | fn soevsess did ulong ts ulong to bool |
| 503 | if ne get gskbl 0 97 |
| 504 | ret false |
| 505 | end |
| 506 | if ne get gskb 64 46 |
| 507 | ret false |
| 508 | end |
| 509 | nb = new uchar 34 0 |
| 510 | put nb 0 110 |
| 511 | put nb 1 58 |
| 512 | for i ulong 32 |
| 513 | put nb add i 2 get gskb add i 65 |
| 514 | end |
| 515 | put gskbl 0 64 |
| 516 | s = sointern |
| 517 | sokclr |
| 518 | sokt nb |
| 519 | n = sointern |
| 520 | if or eq s 0 eq n 0 |
| 521 | ret false |
| 522 | end |
| 523 | put gssd s did |
| 524 | put gsse s add ts 2592000 |
| 525 | put gsnu n 1 |
| 526 | ret true |
| 527 | end |
| 528 | fn soevlogo did ulong to bool |
| 529 | if ne get gskbl 0 64 |
| 530 | ret false |
| 531 | end |
| 532 | s = sofind |
| 533 | if ne s 0 |
| 534 | if eq get gssd s did |
| 535 | put gssd s 0 |
| 536 | end |
| 537 | end |
| 538 | ret true |
| 539 | end |
| 540 | note ---- per-DID write counter (rebuilt from the log; 30 writes per minute) |
| 541 | fn soratev did ulong ts ulong to void |
| 542 | m = div ts 60 |
| 543 | if eq get gsrlm did m |
| 544 | put gsrlc did add get gsrlc did 1 |
| 545 | ret |
| 546 | end |
| 547 | put gsrlm did m |
| 548 | put gsrlc did 1 |
| 549 | end |
| 550 | fn sordok did ulong to bool |
| 551 | m = div get gsnow 0 60 |
| 552 | if ne get gsrlm did m |
| 553 | ret true |
| 554 | end |
| 555 | ret lt get gsrlc did 30 |
| 556 | end |
| 557 | note ---- reset and replay |
| 558 | fn soreset to void |
| 559 | for i ulong len gssh |
| 560 | put gssh i 0 |
| 561 | end |
| 562 | for i ulong len gsrk |
| 563 | put gsrk i 0 |
| 564 | put gsrv i 0 |
| 565 | end |
| 566 | for i ulong len gsso |
| 567 | soclrid i |
| 568 | end |
| 569 | put gsns 0 1 |
| 570 | put gsstrl 0 0 |
| 571 | put gsseq 0 0 |
| 572 | for i ulong 32 |
| 573 | put gsprev i 0 |
| 574 | put gsprv2 i 0 |
| 575 | end |
| 576 | end |
| 577 | fn soclrid i ulong to void |
| 578 | put gscst i 0 |
| 579 | put gscfv i 0 |
| 580 | put gscwt i 0 |
| 581 | put gscfl i 0 |
| 582 | put gscfg i 0 |
| 583 | put gsacc i 0 |
| 584 | put gsnm i 0 |
| 585 | put gsnmo i 0 |
| 586 | put gssd i 0 |
| 587 | put gsse i 0 |
| 588 | put gsnu i 0 |
| 589 | put gsrlm i 0 |
| 590 | put gsrlc i 0 |
| 591 | end |
| 592 | note soreplay: 0 when every line parses and chains; else -(line number); -1000000-errno on read error |
| 593 | fn soreplay to long |
| 594 | sopdd |
| 595 | sopt tslog |
| 596 | fd = fopen gspth 0 get gspthl 0 524288 0 |
| 597 | if eq fd -2 |
| 598 | ret 0 |
| 599 | end |
| 600 | if lt fd 0 |
| 601 | ret sub -1000000 fd |
| 602 | end |
| 603 | ln = new uchar 1024 0 |
| 604 | n = ulong 0 |
| 605 | line = ulong 0 |
| 606 | for it ulong 1000000 |
| 607 | r = read int fd gsrb 0 len gsrb |
| 608 | if lt r 0 |
| 609 | x = fclose int fd |
| 610 | ret sub -1000000 r |
| 611 | end |
| 612 | if eq r 0 |
| 613 | break |
| 614 | end |
| 615 | for i ulong ulong r |
| 616 | c = get gsrb i |
| 617 | if eq c 10 |
| 618 | line = add line 1 |
| 619 | if not soapply part ln 0 n |
| 620 | x = fclose int fd |
| 621 | ret neg long line |
| 622 | end |
| 623 | n = ulong 0 |
| 624 | else |
| 625 | if ge n 1023 |
| 626 | x = fclose int fd |
| 627 | ret neg long add line 1 |
| 628 | end |
| 629 | put ln n c |
| 630 | n = add n 1 |
| 631 | end |
| 632 | end |
| 633 | end |
| 634 | x = fclose int fd |
| 635 | if ne n 0 |
| 636 | ret neg long add line 1 |
| 637 | end |
| 638 | ret 0 |
| 639 | end |
| 640 | note sohdchk: social.head agrees with the log (absent head = "0 <zeros>"); the head may lag by |
| 641 | note one line (crash between append and head rename) and is then rewritten |
| 642 | fn sohdchk to bool |
| 643 | sopdd |
| 644 | sopt tshead |
| 645 | fd = fopen gspth 0 get gspthl 0 524288 0 |
| 646 | hs = ulong 0 |
| 647 | hl = long 0 |
| 648 | hd = new uchar 128 0 |
| 649 | if ge fd 0 |
| 650 | hl = read int fd gshd 0 len gshd |
| 651 | x = fclose int fd |
| 652 | if or lt hl 66 gt hl 90 |
| 653 | ret false |
| 654 | end |
| 655 | end |
| 656 | if lt fd 0 |
| 657 | if ne fd -2 |
| 658 | ret false |
| 659 | end |
| 660 | end |
| 661 | if ge fd 0 |
| 662 | put gspc 0 0 |
| 663 | put gsbad 0 0 |
| 664 | note hd: a local copy, since a global is never a function argument |
| 665 | for i ulong ulong hl |
| 666 | put hd i get gshd i |
| 667 | end |
| 668 | hs = sonum part hd 0 ulong hl |
| 669 | if ne get gsbad 0 0 |
| 670 | ret false |
| 671 | end |
| 672 | end |
| 673 | ret sohdcmp hs ulong hl |
| 674 | end |
| 675 | fn sohdcmp hs ulong hl ulong to bool |
| 676 | s = get gsseq 0 |
| 677 | p = get gspc 0 |
| 678 | if eq hs s |
| 679 | if eq s 0 |
| 680 | ret true |
| 681 | end |
| 682 | ret sohdhex p hl 0 |
| 683 | end |
| 684 | if ne add hs 1 s |
| 685 | ret false |
| 686 | end |
| 687 | if ne hs 0 |
| 688 | if not sohdhex p hl 1 |
| 689 | ret false |
| 690 | end |
| 691 | end |
| 692 | ret sohdput |
| 693 | end |
| 694 | note sohdhex: gshd[p] is ' ', then 64 hex of gsprev (w 0) or gsprv2 (w 1), then '\n' at the end |
| 695 | fn sohdhex p ulong hl ulong w ulong to bool |
| 696 | if ne hl add p 66 |
| 697 | ret false |
| 698 | end |
| 699 | if or ne get gshd p 32 ne get gshd add p 65 10 |
| 700 | ret false |
| 701 | end |
| 702 | d = new uchar 32 0 |
| 703 | for i ulong 32 |
| 704 | v = get gsprev i |
| 705 | if eq w 1 |
| 706 | v = get gsprv2 i |
| 707 | end |
| 708 | put d i v |
| 709 | end |
| 710 | x = new uchar 64 0 |
| 711 | sohex d x |
| 712 | for i ulong 64 |
| 713 | if ne get gshd add add p 1 i get x i |
| 714 | ret false |
| 715 | end |
| 716 | end |
| 717 | ret true |
| 718 | end |
| 719 | note sohdput: write social.head.tmp then rename over social.head |
| 720 | fn sohdput to bool |
| 721 | d = new uchar 96 0 |
| 722 | n = sodec get gsseq 0 d |
| 723 | put d n 32 |
| 724 | x = new uchar 64 0 |
| 725 | soprevhx x |
| 726 | for i ulong 64 |
| 727 | put d add add n 1 i get x i |
| 728 | end |
| 729 | put d add n 65 10 |
| 730 | sopdd |
| 731 | sopt tshtmp |
| 732 | r = unlink gspth 0 get gspthl 0 |
| 733 | if not sowfile part d 0 add n 66 |
| 734 | ret false |
| 735 | end |
| 736 | soavb |
| 737 | soavpth |
| 738 | sopdd |
| 739 | sopt tshead |
| 740 | soavpth |
| 741 | r = renames gspk 0 get gspkl 0 0 |
| 742 | ret eq r 0 |
| 743 | end |
| 744 | note soprevhx: 64 lowercase hex chars of gsprev into x |
| 745 | fn soprevhx x edit uchar * to void |
| 746 | for i ulong 32 |
| 747 | b = ulong get gsprev i |
| 748 | put x mul i 2 sohexc shr b 4 |
| 749 | put x add mul i 2 1 sohexc and b 15 |
| 750 | end |
| 751 | end |
| 752 | note sodec: decimal of v into d at 0; returns the length |
| 753 | fn sodec v ulong d edit uchar * to ulong |
| 754 | t = new uchar 20 0 |
| 755 | k = ulong 0 |
| 756 | x = v |
| 757 | for it ulong 20 |
| 758 | put t k uchar add rem x 10 48 |
| 759 | k = add k 1 |
| 760 | x = div x 10 |
| 761 | if eq x 0 |
| 762 | break |
| 763 | end |
| 764 | end |
| 765 | for i ulong k |
| 766 | put d i get t sub sub k 1 i |
| 767 | end |
| 768 | ret k |
| 769 | end |
| 770 | note ---- appending one event (single writer; flock guards against a second instance) |
| 771 | fn solc c uchar to void |
| 772 | n = get gslnl 0 |
| 773 | if lt n len gsln |
| 774 | put gsln n c |
| 775 | put gslnl 0 add n 1 |
| 776 | end |
| 777 | end |
| 778 | fn solt t view uchar * to void |
| 779 | for i ulong len t bound 512 |
| 780 | solc get t i |
| 781 | end |
| 782 | end |
| 783 | fn solnum v ulong to void |
| 784 | d = new uchar 20 0 |
| 785 | k = sodec v d |
| 786 | for i ulong k |
| 787 | solc get d i |
| 788 | end |
| 789 | end |
| 790 | note soemit: append event a by string id did (a "did:key:" string) with the target in gskb |
| 791 | fn soemit a ulong did ulong to bool |
| 792 | if eq get gsok 0 0 |
| 793 | ret false |
| 794 | end |
| 795 | put gslnl 0 0 |
| 796 | solt tsl1 |
| 797 | solnum add get gsseq 0 1 |
| 798 | solt tsl2 |
| 799 | solnum get gsnow 0 |
| 800 | solt tsl3 |
| 801 | o = get gsso did |
| 802 | for i ulong sub get gssl did len tsdidp bound 64 |
| 803 | solc get gsstr add add o len tsdidp i |
| 804 | end |
| 805 | solt tsl4 |
| 806 | x = soatx a true |
| 807 | solt tsl5 |
| 808 | for i ulong get gskbl 0 bound 512 |
| 809 | solc get gskb i |
| 810 | end |
| 811 | solt tsl6 |
| 812 | hx = new uchar 64 0 |
| 813 | soprevhx hx |
| 814 | solt hx |
| 815 | solt tsl7 |
| 816 | ret soemitw |
| 817 | end |
| 818 | fn soemitw to bool |
| 819 | n = get gslnl 0 |
| 820 | if ge n sub len gsln 1 |
| 821 | ret false |
| 822 | end |
| 823 | ln = new uchar 1024 0 |
| 824 | for i ulong n |
| 825 | put ln i get gsln i |
| 826 | end |
| 827 | put gsln n 10 |
| 828 | sopdd |
| 829 | sopt tslog |
| 830 | fd = fopen gspth 0 get gspthl 0 525377 384 |
| 831 | if lt fd 0 |
| 832 | ret false |
| 833 | end |
| 834 | l = flock int fd 2 |
| 835 | w = write int fd gsln 0 add n 1 |
| 836 | s = fdsync int fd |
| 837 | u = flock int fd 8 |
| 838 | x = fclose int fd |
| 839 | if ne w long add n 1 |
| 840 | put gsok 0 0 |
| 841 | ret false |
| 842 | end |
| 843 | if not soapply part ln 0 n |
| 844 | put gsok 0 0 |
| 845 | ret false |
| 846 | end |
| 847 | if not sohdput |
| 848 | put gsok 0 0 |
| 849 | ret false |
| 850 | end |
| 851 | ret true |
| 852 | end |
| 853 | note ---- load (start and SIGHUP) |
| 854 | fn sokeyld to bool |
| 855 | k = new uchar 32 0 |
| 856 | sopdd |
| 857 | sopt tskeyf |
| 858 | fd = fopen gspth 0 get gspthl 0 524288 0 |
| 859 | if eq fd -2 |
| 860 | if not sorand k |
| 861 | ret false |
| 862 | end |
| 863 | if not sowfile k |
| 864 | ret false |
| 865 | end |
| 866 | fd = fopen gspth 0 get gspthl 0 524288 0 |
| 867 | end |
| 868 | if lt fd 0 |
| 869 | ret false |
| 870 | end |
| 871 | r = read int fd gskey 0 32 |
| 872 | x = fclose int fd |
| 873 | ret eq r 32 |
| 874 | end |
| 875 | fn soownb to void |
| 876 | sokclr |
| 877 | sokt tsdidp |
| 878 | sokt tsowndid |
| 879 | d = sointern |
| 880 | put gsown 0 d |
| 881 | sokclr |
| 882 | sokt tfowner |
| 883 | n = sointern |
| 884 | put gsnm d n |
| 885 | put gsnmo n d |
| 886 | end |
| 887 | note soload: 0 = loaded and verified (social on); <0 = social disabled (log tamper or I/O error) |
| 888 | fn soload to long |
| 889 | put gsok 0 0 |
| 890 | sodirset |
| 891 | sopclr |
| 892 | for i ulong sub get gsddl 0 1 bound 512 |
| 893 | sopc get gsdd i |
| 894 | end |
| 895 | r = makedir gspth 0 get gspthl 0 448 |
| 896 | sopdd |
| 897 | sopt tstmpd |
| 898 | r = makedir gspth 0 get gspthl 0 448 |
| 899 | if not sokeyld |
| 900 | flog tsekey |
| 901 | ret -1 |
| 902 | end |
| 903 | soreset |
| 904 | soownb |
| 905 | e = soreplay |
| 906 | if ne e 0 |
| 907 | soreplerr e |
| 908 | ret e |
| 909 | end |
| 910 | if not sohdchk |
| 911 | flog tsehead |
| 912 | ret -2 |
| 913 | end |
| 914 | put gsok 0 1 |
| 915 | ret 0 |
| 916 | end |
| 917 | fn soreplerr e long to void |
| 918 | d = new uchar 64 0 |
| 919 | for i ulong len tsetamp |
| 920 | put d i get tsetamp i |
| 921 | end |
| 922 | v = ulong 0 |
| 923 | if lt e 0 |
| 924 | v = ulong neg e |
| 925 | end |
| 926 | n = add len tsetamp sodec v part d len tsetamp 20 |
| 927 | put d n 10 |
| 928 | flog part d 0 add n 1 |
| 929 | end |