activity.go (view raw)
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 |
// // Copyright (c) 2019 Ted Unangst <tedu@tedunangst.com> // // Permission to use, copy, modify, and distribute this software for any // purpose with or without fee is hereby granted, provided that the above // copyright notice and this permission notice appear in all copies. // // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR // ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. package main import ( "bytes" "compress/gzip" "crypto/rsa" "database/sql" "encoding/json" "fmt" "io" "log" "net/http" "net/url" "os" "strconv" "strings" "sync" "time" "humungus.tedunangst.com/r/webs/image" ) func NewJunk() map[string]interface{} { return make(map[string]interface{}) } func WriteJunk(w io.Writer, j map[string]interface{}) error { e := json.NewEncoder(w) e.SetEscapeHTML(false) e.SetIndent("", " ") err := e.Encode(j) return err } func ReadJunk(r io.Reader) (map[string]interface{}, error) { decoder := json.NewDecoder(r) var j map[string]interface{} err := decoder.Decode(&j) if err != nil { return nil, err } return j, nil } var theonetruename = `application/ld+json; profile="https://www.w3.org/ns/activitystreams"` var thefakename = `application/activity+json` var falsenames = []string{ `application/ld+json`, `application/activity+json`, } var itiswhatitis = "https://www.w3.org/ns/activitystreams" var thewholeworld = "https://www.w3.org/ns/activitystreams#Public" func friendorfoe(ct string) bool { ct = strings.ToLower(ct) for _, at := range falsenames { if strings.HasPrefix(ct, at) { return true } } return false } func PostJunk(keyname string, key *rsa.PrivateKey, url string, j map[string]interface{}) error { var buf bytes.Buffer WriteJunk(&buf, j) return PostMsg(keyname, key, url, buf.Bytes()) } func PostMsg(keyname string, key *rsa.PrivateKey, url string, msg []byte) error { client := http.DefaultClient req, err := http.NewRequest("POST", url, bytes.NewReader(msg)) if err != nil { return err } req.Header.Set("User-Agent", "honksnonk/5.0") req.Header.Set("Content-Type", theonetruename) zig(keyname, key, req, msg) resp, err := client.Do(req) if err != nil { return err } resp.Body.Close() switch resp.StatusCode { case 200: case 201: case 202: default: return fmt.Errorf("http post status: %d", resp.StatusCode) } log.Printf("successful post: %s %d", url, resp.StatusCode) return nil } type gzCloser struct { r *gzip.Reader under io.ReadCloser } func (gz *gzCloser) Read(p []byte) (int, error) { return gz.r.Read(p) } func (gz *gzCloser) Close() error { defer gz.under.Close() return gz.r.Close() } func GetJunk(url string) (map[string]interface{}, error) { client := http.DefaultClient req, err := http.NewRequest("GET", url, nil) if err != nil { return nil, err } at := thefakename if strings.Contains(url, ".well-known/webfinger?resource") { at = "application/jrd+json" } req.Header.Set("Accept", at) req.Header.Set("Accept-Encoding", "gzip") req.Header.Set("User-Agent", "honksnonk/5.0") resp, err := client.Do(req) if err != nil { log.Printf("first get failed: %s", err) resp, err = client.Do(req) if err != nil { return nil, err } log.Printf("retry succeeded!") } if resp.StatusCode != 200 { resp.Body.Close() return nil, fmt.Errorf("http get status: %d", resp.StatusCode) } if strings.EqualFold(resp.Header.Get("Content-Encoding"), "gzip") { gz, err := gzip.NewReader(resp.Body) if err != nil { resp.Body.Close() return nil, err } resp.Body = &gzCloser{r: gz, under: resp.Body} } defer resp.Body.Close() j, err := ReadJunk(resp.Body) return j, err } func jsonfindinterface(ii interface{}, keys []string) interface{} { for _, key := range keys { idx, err := strconv.Atoi(key) if err == nil { m := ii.([]interface{}) if idx >= len(m) { return nil } ii = m[idx] } else { m := ii.(map[string]interface{}) ii = m[key] if ii == nil { return nil } } } return ii } func jsonfindstring(j interface{}, keys []string) (string, bool) { s, ok := jsonfindinterface(j, keys).(string) return s, ok } func jsonfindarray(j interface{}, keys []string) ([]interface{}, bool) { a, ok := jsonfindinterface(j, keys).([]interface{}) return a, ok } func jsonfindmap(j interface{}, keys []string) (map[string]interface{}, bool) { m, ok := jsonfindinterface(j, keys).(map[string]interface{}) return m, ok } func jsongetstring(j interface{}, key string) (string, bool) { return jsonfindstring(j, []string{key}) } func jsongetarray(j interface{}, key string) ([]interface{}, bool) { return jsonfindarray(j, []string{key}) } func jsongetmap(j interface{}, key string) (map[string]interface{}, bool) { return jsonfindmap(j, []string{key}) } func savedonk(url string, name, media string, localize bool) *Donk { if url == "" { return nil } var donk Donk row := stmtFindFile.QueryRow(url) err := row.Scan(&donk.FileID) if err == nil { return &donk } log.Printf("saving donk: %s", url) if err != nil && err != sql.ErrNoRows { log.Printf("error querying: %s", err) } xid := xfiltrate() data := []byte{} if localize { resp, err := http.Get(url) if err != nil { log.Printf("error fetching %s: %s", url, err) localize = false goto saveit } defer resp.Body.Close() if resp.StatusCode != 200 { localize = false goto saveit } var buf bytes.Buffer io.Copy(&buf, resp.Body) data = buf.Bytes() if strings.HasPrefix(media, "image") { img, err := image.Vacuum(&buf, image.Params{MaxWidth: 2048, MaxHeight: 2048}) if err != nil { log.Printf("unable to decode image: %s", err) localize = false goto saveit } data = img.Data media = "image/" + img.Format } } saveit: res, err := stmtSaveFile.Exec(xid, name, url, media, localize, data) if err != nil { log.Printf("error saving file %s: %s", url, err) return nil } donk.FileID, _ = res.LastInsertId() return &donk } func needxonk(user *WhatAbout, x *Honk) bool { if x.What == "eradicate" { return true } if thoudostbitethythumb(user.ID, x.Audience, x.XID) { log.Printf("not saving thumb biter? %s via %s", x.XID, x.Honker) return false } return needxonkid(user, x.XID) } func needxonkid(user *WhatAbout, xid string) bool { if strings.HasPrefix(xid, user.URL+"/h/") { return false } row := stmtFindXonk.QueryRow(user.ID, xid) var id int64 err := row.Scan(&id) if err == nil { return false } if err != sql.ErrNoRows { log.Printf("err querying xonk: %s", err) } return true } func savexonk(user *WhatAbout, x *Honk) { if x.What == "eradicate" { log.Printf("eradicating %s by %s", x.XID, x.Honker) xonk := getxonk(user.ID, x.XID) if xonk != nil { stmtZonkDonks.Exec(xonk.ID) _, err := stmtZonkIt.Exec(user.ID, x.XID) if err != nil { log.Printf("error eradicating: %s", err) } } return } log.Printf("saving xonk: %s", x.XID) dt := x.Date.UTC().Format(dbtimeformat) aud := strings.Join(x.Audience, " ") whofore := 0 if strings.Contains(aud, user.URL) { whofore = 1 } res, err := stmtSaveHonk.Exec(x.UserID, x.What, x.Honker, x.XID, x.RID, dt, x.URL, aud, x.Noise, x.Convoy, whofore, "html", x.Precis, x.Oonker) if err != nil { log.Printf("err saving xonk: %s", err) return } x.ID, _ = res.LastInsertId() for _, d := range x.Donks { _, err = stmtSaveDonk.Exec(x.ID, d.FileID) if err != nil { log.Printf("err saving donk: %s", err) return } } } type Box struct { In string Out string Shared string } var boxofboxes = make(map[string]*Box) var boxlock sync.Mutex var boxinglock sync.Mutex func getboxes(ident string) (*Box, error) { boxlock.Lock() b, ok := boxofboxes[ident] boxlock.Unlock() if ok { return b, nil } boxinglock.Lock() defer boxinglock.Unlock() boxlock.Lock() b, ok = boxofboxes[ident] boxlock.Unlock() if ok { return b, nil } var info string row := stmtGetXonker.QueryRow(ident, "boxes") err := row.Scan(&info) if err != nil { j, err := GetJunk(ident) if err != nil { return nil, err } inbox, _ := jsongetstring(j, "inbox") outbox, _ := jsongetstring(j, "outbox") sbox, _ := jsonfindstring(j, []string{"endpoints", "sharedInbox"}) b = &Box{In: inbox, Out: outbox, Shared: sbox} if inbox != "" { m := strings.Join([]string{inbox, outbox, sbox}, " ") _, err = stmtSaveXonker.Exec(ident, m, "boxes") if err != nil { log.Printf("error saving boxes: %s", err) } } } else { m := strings.Split(info, " ") b = &Box{In: m[0], Out: m[1], Shared: m[2]} } boxlock.Lock() boxofboxes[ident] = b boxlock.Unlock() return b, nil } func peeppeep() { user, _ := butwhatabout("htest") honkers := gethonkers(user.ID) for _, f := range honkers { if f.Flavor != "peep" { continue } log.Printf("getting updates: %s", f.XID) box, err := getboxes(f.XID) if err != nil { log.Printf("error getting outbox: %s", err) continue } log.Printf("getting outbox") j, err := GetJunk(box.Out) if err != nil { log.Printf("err: %s", err) continue } t, _ := jsongetstring(j, "type") origin := originate(f.XID) if t == "OrderedCollection" { items, _ := jsongetarray(j, "orderedItems") if items == nil { page1, _ := jsongetstring(j, "first") j, err = GetJunk(page1) if err != nil { log.Printf("err: %s", err) continue } items, _ = jsongetarray(j, "orderedItems") } for _, item := range items { xonk := xonkxonk(user, item, origin) if xonk != nil { savexonk(user, xonk) } } } } } func whosthere(xid string) ([]string, string) { obj, err := GetJunk(xid) if err != nil { log.Printf("error getting remote xonk: %s", err) return nil, "" } convoy, _ := jsongetstring(obj, "context") if convoy == "" { convoy, _ = jsongetstring(obj, "conversation") } return newphone(nil, obj), convoy } func newphone(a []string, obj map[string]interface{}) []string { for _, addr := range []string{"to", "cc", "attributedTo"} { who, _ := jsongetstring(obj, addr) if who != "" { a = append(a, who) } whos, _ := jsongetarray(obj, addr) for _, w := range whos { who, _ := w.(string) if who != "" { a = append(a, who) } } } return a } func consumeactivity(user *WhatAbout, j interface{}, origin string) { xonk := xonkxonk(user, j, origin) if xonk != nil { savexonk(user, xonk) } } func xonkxonk(user *WhatAbout, item interface{}, origin string) *Honk { depth := 0 maxdepth := 4 currenttid := "" var xonkxonkfn func(item interface{}, origin string) *Honk saveoneup := func(xid string) { log.Printf("getting oneup: %s", xid) if depth >= maxdepth { log.Printf("in too deep") return } obj, err := GetJunk(xid) if err != nil { log.Printf("error getting oneup: %s", err) return } depth++ xonk := xonkxonkfn(obj, originate(xid)) if xonk != nil { savexonk(user, xonk) } depth-- } xonkxonkfn = func(item interface{}, origin string) *Honk { // id, _ := jsongetstring(item, "id") what, _ := jsongetstring(item, "type") dt, _ := jsongetstring(item, "published") var audience []string var err error var xid, rid, url, content, precis, convoy, oonker string var obj map[string]interface{} var ok bool switch what { case "Announce": obj, ok = jsongetmap(item, "object") if ok { xid, _ = jsongetstring(obj, "id") } else { xid, _ = jsongetstring(item, "object") } if !needxonkid(user, xid) { return nil } log.Printf("getting bonk: %s", xid) obj, err = GetJunk(xid) if err != nil { log.Printf("error regetting: %s", err) } origin = originate(xid) what = "bonk" case "Create": obj, _ = jsongetmap(item, "object") what = "honk" case "Delete": obj, _ = jsongetmap(item, "object") xid, _ = jsongetstring(item, "object") what = "eradicate" case "Note": fallthrough case "Article": fallthrough case "Page": obj = item.(map[string]interface{}) what = "honk" default: log.Printf("unknown activity: %s", what) fd, _ := os.OpenFile("savedinbox.json", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666) WriteJunk(fd, item.(map[string]interface{})) io.WriteString(fd, "\n") fd.Close() return nil } var xonk Honk who, _ := jsongetstring(item, "actor") if obj != nil { if who == "" { who, _ = jsongetstring(obj, "attributedTo") } oonker, _ = jsongetstring(obj, "attributedTo") ot, _ := jsongetstring(obj, "type") url, _ = jsongetstring(obj, "url") if ot == "Note" || ot == "Article" || ot == "Page" { audience = newphone(audience, obj) xid, _ = jsongetstring(obj, "id") precis, _ = jsongetstring(obj, "summary") content, _ = jsongetstring(obj, "content") if !strings.HasPrefix(content, "<p>") { content = "<p>" + content } rid, _ = jsongetstring(obj, "inReplyTo") convoy, _ = jsongetstring(obj, "context") if convoy == "" { convoy, _ = jsongetstring(obj, "conversation") } if what == "honk" && rid != "" { what = "tonk" } } if ot == "Tombstone" { xid, _ = jsongetstring(obj, "id") } atts, _ := jsongetarray(obj, "attachment") for i, att := range atts { at, _ := jsongetstring(att, "type") mt, _ := jsongetstring(att, "mediaType") u, _ := jsongetstring(att, "url") name, _ := jsongetstring(att, "name") localize := false if i > 4 { log.Printf("excessive attachment: %s", at) } else if at == "Document" || at == "Image" { mt = strings.ToLower(mt) log.Printf("attachment: %s %s", mt, u) if mt == "image/jpeg" || mt == "image/png" || mt == "text/plain" { localize = true } } else { log.Printf("unknown attachment: %s", at) } donk := savedonk(u, name, mt, localize) if donk != nil { xonk.Donks = append(xonk.Donks, donk) } } tags, _ := jsongetarray(obj, "tag") for _, tag := range tags { tt, _ := jsongetstring(tag, "type") name, _ := jsongetstring(tag, "name") if tt == "Emoji" { icon, _ := jsongetmap(tag, "icon") mt, _ := jsongetstring(icon, "mediaType") if mt == "" { mt = "image/png" } u, _ := jsongetstring(icon, "url") donk := savedonk(u, name, mt, true) if donk != nil { xonk.Donks = append(xonk.Donks, donk) } } } } if originate(xid) != origin { log.Printf("original sin: %s <> %s", xid, origin) return nil } audience = append(audience, who) audience = oneofakind(audience) if currenttid == "" { currenttid = convoy } if convoy == "" { convoy = currenttid } if oonker == who { oonker = "" } xonk.UserID = user.ID xonk.What = what xonk.Honker = who xonk.XID = xid xonk.RID = rid xonk.Date, _ = time.Parse(time.RFC3339, dt) xonk.URL = url xonk.Noise = content xonk.Precis = precis xonk.Audience = audience xonk.Convoy = convoy xonk.Oonker = oonker if needxonk(user, &xonk) { if what == "tonk" { if needxonkid(user, rid) { saveoneup(rid) } } return &xonk } return nil } return xonkxonkfn(item, origin) } func rubadubdub(user *WhatAbout, req map[string]interface{}) { xid, _ := jsongetstring(req, "id") reqactor, _ := jsongetstring(req, "actor") j := NewJunk() j["@context"] = itiswhatitis j["id"] = user.URL + "/dub/" + xid j["type"] = "Accept" j["actor"] = user.URL j["to"] = reqactor j["published"] = time.Now().UTC().Format(time.RFC3339) j["object"] = req actor, _ := jsongetstring(req, "actor") box, err := getboxes(actor) if err != nil { log.Printf("can't get dub box: %s", err) return } keyname, key := ziggy(user.Name) err = PostJunk(keyname, key, box.In, j) if err != nil { log.Printf("can't rub a dub: %s", err) return } stmtSaveDub.Exec(user.ID, actor, actor, "dub") } func itakeitallback(user *WhatAbout, xid string) error { j := NewJunk() j["@context"] = itiswhatitis j["id"] = user.URL + "/unsub/" + xid j["type"] = "Undo" j["actor"] = user.URL j["to"] = xid f := NewJunk() f["id"] = user.URL + "/sub/" + xid f["type"] = "Follow" f["actor"] = user.URL f["to"] = xid j["object"] = f j["published"] = time.Now().UTC().Format(time.RFC3339) box, err := getboxes(xid) if err != nil { return err } keyname, key := ziggy(user.Name) err = PostJunk(keyname, key, box.In, j) if err != nil { return err } return nil } func subsub(user *WhatAbout, xid string) { j := NewJunk() j["@context"] = itiswhatitis j["id"] = user.URL + "/sub/" + xid j["type"] = "Follow" j["actor"] = user.URL j["to"] = xid j["object"] = xid j["published"] = time.Now().UTC().Format(time.RFC3339) box, err := getboxes(xid) if err != nil { log.Printf("can't send follow: %s", err) return } keyname, key := ziggy(user.Name) err = PostJunk(keyname, key, box.In, j) if err != nil { log.Printf("failed to subsub: %s", err) } } func jonkjonk(user *WhatAbout, h *Honk) (map[string]interface{}, map[string]interface{}) { dt := h.Date.Format(time.RFC3339) var jo map[string]interface{} j := NewJunk() j["id"] = user.URL + "/" + h.What + "/" + shortxid(h.XID) j["actor"] = user.URL j["published"] = dt j["to"] = h.Audience[0] if len(h.Audience) > 1 { j["cc"] = h.Audience[1:] } switch h.What { case "tonk": fallthrough case "honk": j["type"] = "Create" jo = NewJunk() jo["id"] = h.XID jo["type"] = "Note" jo["published"] = dt jo["url"] = h.XID jo["attributedTo"] = user.URL if h.RID != "" { jo["inReplyTo"] = h.RID } if h.Convoy != "" { jo["context"] = h.Convoy jo["conversation"] = h.Convoy } jo["to"] = h.Audience[0] if len(h.Audience) > 1 { jo["cc"] = h.Audience[1:] } if !h.Public { jo["directMessage"] = true } jo["summary"] = h.Precis jo["content"] = mentionize(h.Noise) if strings.HasPrefix(h.Precis, "DZ:") { jo["sensitive"] = true } var tags []interface{} g := bunchofgrapes(h.Noise) for _, m := range g { t := NewJunk() t["type"] = "Mention" t["name"] = m.who t["href"] = m.where tags = append(tags, t) } herd := herdofemus(h.Noise) for _, e := range herd { t := NewJunk() t["id"] = e.ID t["type"] = "Emoji" t["name"] = e.Name i := NewJunk() i["type"] = "Image" i["mediaType"] = "image/png" i["url"] = e.ID t["icon"] = i tags = append(tags, t) } if len(tags) > 0 { jo["tag"] = tags } var atts []interface{} for _, d := range h.Donks { if re_emus.MatchString(d.Name) { continue } jd := NewJunk() jd["mediaType"] = d.Media jd["name"] = d.Name jd["type"] = "Document" jd["url"] = d.URL atts = append(atts, jd) } if len(atts) > 0 { jo["attachment"] = atts } j["object"] = jo case "bonk": j["type"] = "Announce" j["object"] = h.XID case "zonk": j["type"] = "Delete" j["object"] = h.XID } return j, jo } func honkworldwide(user *WhatAbout, honk *Honk) { jonk, _ := jonkjonk(user, honk) jonk["@context"] = itiswhatitis var buf bytes.Buffer WriteJunk(&buf, jonk) msg := buf.Bytes() rcpts := make(map[string]bool) for _, a := range honk.Audience { if a != thewholeworld && a != user.URL && !strings.HasSuffix(a, "/followers") { box, _ := getboxes(a) if box != nil { if honk.Public && box.Shared != "" { rcpts["%"+box.Shared] = true } else { rcpts["%"+box.In] = true } } else { rcpts[a] = true } } } if honk.Public { for _, f := range getdubs(user.ID) { box, _ := getboxes(f.XID) if box != nil && box.Shared != "" { rcpts["%"+box.Shared] = true } else { rcpts[f.XID] = true } } } for a := range rcpts { go deliverate(0, user.Name, a, msg) } } func asjonker(user *WhatAbout) map[string]interface{} { about := obfusbreak(user.About) j := NewJunk() j["@context"] = itiswhatitis j["id"] = user.URL j["type"] = "Person" j["inbox"] = user.URL + "/inbox" j["outbox"] = user.URL + "/outbox" j["followers"] = user.URL + "/followers" j["following"] = user.URL + "/following" j["name"] = user.Display j["preferredUsername"] = user.Name j["summary"] = about j["url"] = user.URL a := NewJunk() a["type"] = "icon" a["mediaType"] = "image/png" a["url"] = fmt.Sprintf("https://%s/a?a=%s", serverName, url.QueryEscape(user.URL)) j["icon"] = a k := NewJunk() k["id"] = user.URL + "#key" k["owner"] = user.URL k["publicKeyPem"] = user.Key j["publicKey"] = k return j } var handfull = make(map[string]string) var handlock sync.Mutex func gofish(name string) string { if name[0] == '@' { name = name[1:] } m := strings.Split(name, "@") if len(m) != 2 { log.Printf("bad fish name: %s", name) return "" } handlock.Lock() ref, ok := handfull[name] handlock.Unlock() if ok { return ref } row := stmtGetXonker.QueryRow(name, "fishname") var href string err := row.Scan(&href) if err == nil { handlock.Lock() handfull[name] = href handlock.Unlock() return href } log.Printf("fishing for %s", name) j, err := GetJunk(fmt.Sprintf("https://%s/.well-known/webfinger?resource=acct:%s", m[1], name)) if err != nil { log.Printf("failed to go fish %s: %s", name, err) handlock.Lock() handfull[name] = "" handlock.Unlock() return "" } links, _ := jsongetarray(j, "links") for _, l := range links { href, _ := jsongetstring(l, "href") rel, _ := jsongetstring(l, "rel") t, _ := jsongetstring(l, "type") if rel == "self" && friendorfoe(t) { stmtSaveXonker.Exec(name, href, "fishname") handlock.Lock() handfull[name] = href handlock.Unlock() return href } } handlock.Lock() handfull[name] = "" handlock.Unlock() return "" } func investigate(name string) string { if name == "" { return "" } if name[0] == '@' { name = gofish(name) } if name == "" { return "" } obj, err := GetJunk(name) if err != nil { log.Printf("error investigating honker: %s", err) return "" } t, _ := jsongetstring(obj, "type") id, _ := jsongetstring(obj, "id") if t != "Person" { log.Printf("it's not a person! %s", name) return "" } return id } |