activity.go (view raw)
1//
2// Copyright (c) 2019 Ted Unangst <tedu@tedunangst.com>
3//
4// Permission to use, copy, modify, and distribute this software for any
5// purpose with or without fee is hereby granted, provided that the above
6// copyright notice and this permission notice appear in all copies.
7//
8// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15
16package main
17
18import (
19 "bytes"
20 "compress/gzip"
21 "crypto/rsa"
22 "database/sql"
23 "fmt"
24 "io"
25 "log"
26 "net/http"
27 "net/url"
28 "os"
29 "regexp"
30 "strings"
31 "sync"
32 "time"
33
34 "humungus.tedunangst.com/r/webs/image"
35 "humungus.tedunangst.com/r/webs/junk"
36)
37
38var theonetruename = `application/ld+json; profile="https://www.w3.org/ns/activitystreams"`
39var thefakename = `application/activity+json`
40var falsenames = []string{
41 `application/ld+json`,
42 `application/activity+json`,
43}
44var itiswhatitis = "https://www.w3.org/ns/activitystreams"
45var thewholeworld = "https://www.w3.org/ns/activitystreams#Public"
46
47func friendorfoe(ct string) bool {
48 ct = strings.ToLower(ct)
49 for _, at := range falsenames {
50 if strings.HasPrefix(ct, at) {
51 return true
52 }
53 }
54 return false
55}
56
57func PostJunk(keyname string, key *rsa.PrivateKey, url string, j junk.Junk) error {
58 var buf bytes.Buffer
59 j.Write(&buf)
60 return PostMsg(keyname, key, url, buf.Bytes())
61}
62
63func PostMsg(keyname string, key *rsa.PrivateKey, url string, msg []byte) error {
64 client := http.DefaultClient
65 req, err := http.NewRequest("POST", url, bytes.NewReader(msg))
66 if err != nil {
67 return err
68 }
69 req.Header.Set("User-Agent", "honksnonk/5.0")
70 req.Header.Set("Content-Type", theonetruename)
71 zig(keyname, key, req, msg)
72 resp, err := client.Do(req)
73 if err != nil {
74 return err
75 }
76 resp.Body.Close()
77 switch resp.StatusCode {
78 case 200:
79 case 201:
80 case 202:
81 default:
82 return fmt.Errorf("http post status: %d", resp.StatusCode)
83 }
84 log.Printf("successful post: %s %d", url, resp.StatusCode)
85 return nil
86}
87
88type gzCloser struct {
89 r *gzip.Reader
90 under io.ReadCloser
91}
92
93func (gz *gzCloser) Read(p []byte) (int, error) {
94 return gz.r.Read(p)
95}
96
97func (gz *gzCloser) Close() error {
98 defer gz.under.Close()
99 return gz.r.Close()
100}
101
102func GetJunk(url string) (junk.Junk, error) {
103 client := http.DefaultClient
104 req, err := http.NewRequest("GET", url, nil)
105 if err != nil {
106 return nil, err
107 }
108 at := thefakename
109 if strings.Contains(url, ".well-known/webfinger?resource") {
110 at = "application/jrd+json"
111 }
112 req.Header.Set("Accept", at)
113 req.Header.Set("Accept-Encoding", "gzip")
114 req.Header.Set("User-Agent", "honksnonk/5.0")
115 resp, err := client.Do(req)
116 if err != nil {
117 log.Printf("first get failed: %s", err)
118 resp, err = client.Do(req)
119 if err != nil {
120 return nil, err
121 }
122 log.Printf("retry succeeded!")
123 }
124 if resp.StatusCode != 200 {
125 resp.Body.Close()
126 return nil, fmt.Errorf("http get status: %d", resp.StatusCode)
127 }
128 if strings.EqualFold(resp.Header.Get("Content-Encoding"), "gzip") {
129 gz, err := gzip.NewReader(resp.Body)
130 if err != nil {
131 resp.Body.Close()
132 return nil, err
133 }
134 resp.Body = &gzCloser{r: gz, under: resp.Body}
135 }
136 defer resp.Body.Close()
137 j, err := junk.Read(resp.Body)
138 return j, err
139}
140
141func savedonk(url string, name, media string, localize bool) *Donk {
142 if url == "" {
143 return nil
144 }
145 var donk Donk
146 row := stmtFindFile.QueryRow(url)
147 err := row.Scan(&donk.FileID)
148 if err == nil {
149 return &donk
150 }
151 log.Printf("saving donk: %s", url)
152 if err != nil && err != sql.ErrNoRows {
153 log.Printf("error querying: %s", err)
154 }
155 xid := xfiltrate()
156 data := []byte{}
157 if localize {
158 resp, err := http.Get(url)
159 if err != nil {
160 log.Printf("error fetching %s: %s", url, err)
161 localize = false
162 goto saveit
163 }
164 defer resp.Body.Close()
165 if resp.StatusCode != 200 {
166 localize = false
167 goto saveit
168 }
169 var buf bytes.Buffer
170 io.Copy(&buf, resp.Body)
171
172 data = buf.Bytes()
173 if strings.HasPrefix(media, "image") {
174 img, err := image.Vacuum(&buf, image.Params{MaxWidth: 2048, MaxHeight: 2048})
175 if err != nil {
176 log.Printf("unable to decode image: %s", err)
177 localize = false
178 goto saveit
179 }
180 data = img.Data
181 media = "image/" + img.Format
182 }
183 }
184saveit:
185 res, err := stmtSaveFile.Exec(xid, name, url, media, localize, data)
186 if err != nil {
187 log.Printf("error saving file %s: %s", url, err)
188 return nil
189 }
190 donk.FileID, _ = res.LastInsertId()
191 return &donk
192}
193
194func needxonk(user *WhatAbout, x *Honk) bool {
195 if x.What == "eradicate" {
196 return true
197 }
198 if thoudostbitethythumb(user.ID, x.Audience, x.XID) {
199 log.Printf("not saving thumb biter? %s via %s", x.XID, x.Honker)
200 return false
201 }
202 return needxonkid(user, x.XID)
203}
204func needxonkid(user *WhatAbout, xid string) bool {
205 if strings.HasPrefix(xid, user.URL+"/h/") {
206 return false
207 }
208 row := stmtFindXonk.QueryRow(user.ID, xid)
209 var id int64
210 err := row.Scan(&id)
211 if err == nil {
212 return false
213 }
214 if err != sql.ErrNoRows {
215 log.Printf("err querying xonk: %s", err)
216 }
217 return true
218}
219
220func savexonk(user *WhatAbout, x *Honk) {
221 if x.What == "eradicate" {
222 log.Printf("eradicating %s by %s", x.XID, x.Honker)
223 xonk := getxonk(user.ID, x.XID)
224 if xonk != nil {
225 stmtZonkDonks.Exec(xonk.ID)
226 _, err := stmtZonkIt.Exec(user.ID, x.XID)
227 if err != nil {
228 log.Printf("error eradicating: %s", err)
229 }
230 }
231 return
232 }
233 log.Printf("saving xonk: %s", x.XID)
234 dt := x.Date.UTC().Format(dbtimeformat)
235 aud := strings.Join(x.Audience, " ")
236 whofore := 0
237 if strings.Contains(aud, user.URL) {
238 whofore = 1
239 }
240 res, err := stmtSaveHonk.Exec(x.UserID, x.What, x.Honker, x.XID, x.RID, dt, x.URL, aud,
241 x.Noise, x.Convoy, whofore, "html", x.Precis, x.Oonker)
242 if err != nil {
243 log.Printf("err saving xonk: %s", err)
244 return
245 }
246 x.ID, _ = res.LastInsertId()
247 for _, d := range x.Donks {
248 _, err = stmtSaveDonk.Exec(x.ID, d.FileID)
249 if err != nil {
250 log.Printf("err saving donk: %s", err)
251 return
252 }
253 }
254}
255
256type Box struct {
257 In string
258 Out string
259 Shared string
260}
261
262var boxofboxes = make(map[string]*Box)
263var boxlock sync.Mutex
264var boxinglock sync.Mutex
265
266func getboxes(ident string) (*Box, error) {
267 boxlock.Lock()
268 b, ok := boxofboxes[ident]
269 boxlock.Unlock()
270 if ok {
271 return b, nil
272 }
273
274 boxinglock.Lock()
275 defer boxinglock.Unlock()
276
277 boxlock.Lock()
278 b, ok = boxofboxes[ident]
279 boxlock.Unlock()
280 if ok {
281 return b, nil
282 }
283
284 var info string
285 row := stmtGetXonker.QueryRow(ident, "boxes")
286 err := row.Scan(&info)
287 if err != nil {
288 j, err := GetJunk(ident)
289 if err != nil {
290 return nil, err
291 }
292 inbox, _ := j.GetString("inbox")
293 outbox, _ := j.GetString("outbox")
294 sbox, _ := j.FindString([]string{"endpoints", "sharedInbox"})
295 b = &Box{In: inbox, Out: outbox, Shared: sbox}
296 if inbox != "" {
297 m := strings.Join([]string{inbox, outbox, sbox}, " ")
298 _, err = stmtSaveXonker.Exec(ident, m, "boxes")
299 if err != nil {
300 log.Printf("error saving boxes: %s", err)
301 }
302 }
303 } else {
304 m := strings.Split(info, " ")
305 b = &Box{In: m[0], Out: m[1], Shared: m[2]}
306 }
307
308 boxlock.Lock()
309 boxofboxes[ident] = b
310 boxlock.Unlock()
311 return b, nil
312}
313
314func peeppeep() {
315 user, _ := butwhatabout("htest")
316 honkers := gethonkers(user.ID)
317 for _, f := range honkers {
318 if f.Flavor != "peep" {
319 continue
320 }
321 log.Printf("getting updates: %s", f.XID)
322 box, err := getboxes(f.XID)
323 if err != nil {
324 log.Printf("error getting outbox: %s", err)
325 continue
326 }
327 log.Printf("getting outbox")
328 j, err := GetJunk(box.Out)
329 if err != nil {
330 log.Printf("err: %s", err)
331 continue
332 }
333 t, _ := j.GetString("type")
334 origin := originate(f.XID)
335 if t == "OrderedCollection" {
336 items, _ := j.GetArray("orderedItems")
337 if items == nil {
338 page1, _ := j.GetString("first")
339 j, err = GetJunk(page1)
340 if err != nil {
341 log.Printf("err: %s", err)
342 continue
343 }
344 items, _ = j.GetArray("orderedItems")
345 }
346
347 for _, item := range items {
348 obj, ok := item.(junk.Junk)
349 if !ok {
350 continue
351 }
352 xonk := xonkxonk(user, obj, origin)
353 if xonk != nil {
354 savexonk(user, xonk)
355 }
356 }
357 }
358 }
359}
360
361func whosthere(xid string) ([]string, string) {
362 obj, err := GetJunk(xid)
363 if err != nil {
364 log.Printf("error getting remote xonk: %s", err)
365 return nil, ""
366 }
367 convoy, _ := obj.GetString("context")
368 if convoy == "" {
369 convoy, _ = obj.GetString("conversation")
370 }
371 return newphone(nil, obj), convoy
372}
373
374func newphone(a []string, obj junk.Junk) []string {
375 for _, addr := range []string{"to", "cc", "attributedTo"} {
376 who, _ := obj.GetString(addr)
377 if who != "" {
378 a = append(a, who)
379 }
380 whos, _ := obj.GetArray(addr)
381 for _, w := range whos {
382 who, _ := w.(string)
383 if who != "" {
384 a = append(a, who)
385 }
386 }
387 }
388 return a
389}
390
391func consumeactivity(user *WhatAbout, j junk.Junk, origin string) {
392 xonk := xonkxonk(user, j, origin)
393 if xonk != nil {
394 savexonk(user, xonk)
395 }
396}
397
398func xonkxonk(user *WhatAbout, item junk.Junk, origin string) *Honk {
399 depth := 0
400 maxdepth := 4
401 currenttid := ""
402 var xonkxonkfn func(item junk.Junk, origin string) *Honk
403
404 saveoneup := func(xid string) {
405 log.Printf("getting oneup: %s", xid)
406 if depth >= maxdepth {
407 log.Printf("in too deep")
408 return
409 }
410 obj, err := GetJunk(xid)
411 if err != nil {
412 log.Printf("error getting oneup: %s", err)
413 return
414 }
415 depth++
416 xonk := xonkxonkfn(obj, originate(xid))
417 if xonk != nil {
418 savexonk(user, xonk)
419 }
420 depth--
421 }
422
423 xonkxonkfn = func(item junk.Junk, origin string) *Honk {
424 // id, _ := item.GetString( "id")
425 what, _ := item.GetString("type")
426 dt, _ := item.GetString("published")
427
428 var audience []string
429 var err error
430 var xid, rid, url, content, precis, convoy, oonker string
431 var obj junk.Junk
432 var ok bool
433 switch what {
434 case "Announce":
435 obj, ok = item.GetMap("object")
436 if ok {
437 xid, _ = obj.GetString("id")
438 } else {
439 xid, _ = item.GetString("object")
440 }
441 if !needxonkid(user, xid) {
442 return nil
443 }
444 log.Printf("getting bonk: %s", xid)
445 obj, err = GetJunk(xid)
446 if err != nil {
447 log.Printf("error regetting: %s", err)
448 }
449 origin = originate(xid)
450 what = "bonk"
451 case "Create":
452 obj, _ = item.GetMap("object")
453 what = "honk"
454 case "Delete":
455 obj, _ = item.GetMap("object")
456 xid, _ = item.GetString("object")
457 what = "eradicate"
458 case "Question":
459 fallthrough
460 case "Note":
461 fallthrough
462 case "Article":
463 fallthrough
464 case "Page":
465 obj = item
466 what = "honk"
467 default:
468 log.Printf("unknown activity: %s", what)
469 fd, _ := os.OpenFile("savedinbox.json", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
470 item.Write(fd)
471 io.WriteString(fd, "\n")
472 fd.Close()
473 return nil
474 }
475
476 var xonk Honk
477 who, _ := item.GetString("actor")
478 if obj != nil {
479 if who == "" {
480 who, _ = obj.GetString("attributedTo")
481 }
482 oonker, _ = obj.GetString("attributedTo")
483 ot, _ := obj.GetString("type")
484 url, _ = obj.GetString("url")
485 if ot == "Note" || ot == "Article" || ot == "Page" || ot == "Question" {
486 audience = newphone(audience, obj)
487 xid, _ = obj.GetString("id")
488 precis, _ = obj.GetString("summary")
489 content, _ = obj.GetString("content")
490 if !strings.HasPrefix(content, "<p>") {
491 content = "<p>" + content
492 }
493 rid, _ = obj.GetString("inReplyTo")
494 convoy, _ = obj.GetString("context")
495 if convoy == "" {
496 convoy, _ = obj.GetString("conversation")
497 }
498 if ot == "Question" {
499 what = "qonk"
500 content += "<ul>"
501 ans, _ := obj.GetArray("oneOf")
502 for _, ai := range ans {
503 a, ok := ai.(junk.Junk)
504 if !ok {
505 continue
506 }
507 as, _ := a.GetString("name")
508 content += "<li>" + as
509 }
510 ans, _ = obj.GetArray("anyOf")
511 for _, ai := range ans {
512 a, ok := ai.(junk.Junk)
513 if !ok {
514 continue
515 }
516 as, _ := a.GetString("name")
517 content += "<li>" + as
518 }
519 content += "</ul>"
520 }
521 if what == "honk" && rid != "" {
522 what = "tonk"
523 }
524 }
525 if ot == "Tombstone" {
526 xid, _ = obj.GetString("id")
527 }
528 atts, _ := obj.GetArray("attachment")
529 for i, atti := range atts {
530 att, ok := atti.(junk.Junk)
531 if !ok {
532 continue
533 }
534 at, _ := att.GetString("type")
535 mt, _ := att.GetString("mediaType")
536 u, _ := att.GetString("url")
537 name, _ := att.GetString("name")
538 localize := false
539 if i > 4 {
540 log.Printf("excessive attachment: %s", at)
541 } else if at == "Document" || at == "Image" {
542 mt = strings.ToLower(mt)
543 log.Printf("attachment: %s %s", mt, u)
544 if mt == "image/jpeg" || mt == "image/png" ||
545 mt == "text/plain" {
546 localize = true
547 }
548 } else {
549 log.Printf("unknown attachment: %s", at)
550 }
551 donk := savedonk(u, name, mt, localize)
552 if donk != nil {
553 xonk.Donks = append(xonk.Donks, donk)
554 }
555 }
556 tags, _ := obj.GetArray("tag")
557 for _, tagi := range tags {
558 tag, ok := tagi.(junk.Junk)
559 if !ok {
560 continue
561 }
562 tt, _ := tag.GetString("type")
563 name, _ := tag.GetString("name")
564 if tt == "Emoji" {
565 icon, _ := tag.GetMap("icon")
566 mt, _ := icon.GetString("mediaType")
567 if mt == "" {
568 mt = "image/png"
569 }
570 u, _ := icon.GetString("url")
571 donk := savedonk(u, name, mt, true)
572 if donk != nil {
573 xonk.Donks = append(xonk.Donks, donk)
574 }
575 }
576 }
577 }
578 if originate(xid) != origin {
579 log.Printf("original sin: %s <> %s", xid, origin)
580 item.Write(os.Stdout)
581 return nil
582 }
583 audience = append(audience, who)
584
585 audience = oneofakind(audience)
586
587 if currenttid == "" {
588 currenttid = convoy
589 }
590 if convoy == "" {
591 convoy = currenttid
592 }
593
594 if oonker == who {
595 oonker = ""
596 }
597 xonk.UserID = user.ID
598 xonk.What = what
599 xonk.Honker = who
600 xonk.XID = xid
601 xonk.RID = rid
602 xonk.Date, _ = time.Parse(time.RFC3339, dt)
603 xonk.URL = url
604 xonk.Noise = content
605 xonk.Precis = precis
606 xonk.Audience = audience
607 xonk.Convoy = convoy
608 xonk.Oonker = oonker
609
610 if needxonk(user, &xonk) {
611 if what == "tonk" {
612 if needxonkid(user, rid) {
613 saveoneup(rid)
614 }
615 }
616 return &xonk
617 }
618 return nil
619 }
620
621 return xonkxonkfn(item, origin)
622}
623
624func rubadubdub(user *WhatAbout, req junk.Junk) {
625 xid, _ := req.GetString("id")
626 actor, _ := req.GetString("actor")
627 j := junk.New()
628 j["@context"] = itiswhatitis
629 j["id"] = user.URL + "/dub/" + xid
630 j["type"] = "Accept"
631 j["actor"] = user.URL
632 j["to"] = actor
633 j["published"] = time.Now().UTC().Format(time.RFC3339)
634 j["object"] = req
635
636 var buf bytes.Buffer
637 j.Write(&buf)
638 msg := buf.Bytes()
639
640 deliverate(0, user.Name, actor, msg)
641}
642
643func itakeitallback(user *WhatAbout, xid string) {
644 j := junk.New()
645 j["@context"] = itiswhatitis
646 j["id"] = user.URL + "/unsub/" + xid
647 j["type"] = "Undo"
648 j["actor"] = user.URL
649 j["to"] = xid
650 f := junk.New()
651 f["id"] = user.URL + "/sub/" + xid
652 f["type"] = "Follow"
653 f["actor"] = user.URL
654 f["to"] = xid
655 j["object"] = f
656 j["published"] = time.Now().UTC().Format(time.RFC3339)
657
658 var buf bytes.Buffer
659 j.Write(&buf)
660 msg := buf.Bytes()
661
662 deliverate(0, user.Name, xid, msg)
663}
664
665func subsub(user *WhatAbout, xid string) {
666 j := junk.New()
667 j["@context"] = itiswhatitis
668 j["id"] = user.URL + "/sub/" + xid
669 j["type"] = "Follow"
670 j["actor"] = user.URL
671 j["to"] = xid
672 j["object"] = xid
673 j["published"] = time.Now().UTC().Format(time.RFC3339)
674
675 var buf bytes.Buffer
676 j.Write(&buf)
677 msg := buf.Bytes()
678
679 deliverate(0, user.Name, xid, msg)
680}
681
682var re_spicy = regexp.MustCompile("^(\U0001f336\ufe0f?){3}")
683
684func jonkjonk(user *WhatAbout, h *Honk) (junk.Junk, junk.Junk) {
685 dt := h.Date.Format(time.RFC3339)
686 var jo junk.Junk
687 j := junk.New()
688 j["id"] = user.URL + "/" + h.What + "/" + shortxid(h.XID)
689 j["actor"] = user.URL
690 j["published"] = dt
691 j["to"] = h.Audience[0]
692 if len(h.Audience) > 1 {
693 j["cc"] = h.Audience[1:]
694 }
695
696 switch h.What {
697 case "tonk":
698 fallthrough
699 case "honk":
700 j["type"] = "Create"
701
702 jo = junk.New()
703 jo["id"] = h.XID
704 jo["type"] = "Note"
705 jo["published"] = dt
706 jo["url"] = h.XID
707 jo["attributedTo"] = user.URL
708 if h.RID != "" {
709 jo["inReplyTo"] = h.RID
710 }
711 if h.Convoy != "" {
712 jo["context"] = h.Convoy
713 jo["conversation"] = h.Convoy
714 }
715 jo["to"] = h.Audience[0]
716 if len(h.Audience) > 1 {
717 jo["cc"] = h.Audience[1:]
718 }
719 if !h.Public {
720 jo["directMessage"] = true
721 }
722 jo["summary"] = h.Precis
723 jo["content"] = mentionize(h.Noise)
724 if strings.HasPrefix(h.Precis, "DZ:") || re_spicy.MatchString(h.Noise) {
725 jo["sensitive"] = true
726 }
727 var tags []interface{}
728 g := bunchofgrapes(h.Noise)
729 for _, m := range g {
730 t := junk.New()
731 t["type"] = "Mention"
732 t["name"] = m.who
733 t["href"] = m.where
734 tags = append(tags, t)
735 }
736 herd := herdofemus(h.Noise)
737 for _, e := range herd {
738 t := junk.New()
739 t["id"] = e.ID
740 t["type"] = "Emoji"
741 t["name"] = e.Name
742 i := junk.New()
743 i["type"] = "Image"
744 i["mediaType"] = "image/png"
745 i["url"] = e.ID
746 t["icon"] = i
747 tags = append(tags, t)
748 }
749 if len(tags) > 0 {
750 jo["tag"] = tags
751 }
752 var atts []interface{}
753 for _, d := range h.Donks {
754 if re_emus.MatchString(d.Name) {
755 continue
756 }
757 jd := junk.New()
758 jd["mediaType"] = d.Media
759 jd["name"] = d.Name
760 jd["type"] = "Document"
761 jd["url"] = d.URL
762 atts = append(atts, jd)
763 }
764 if len(atts) > 0 {
765 jo["attachment"] = atts
766 }
767 j["object"] = jo
768 case "bonk":
769 j["type"] = "Announce"
770 j["object"] = h.XID
771 case "zonk":
772 j["type"] = "Delete"
773 j["object"] = h.XID
774 }
775
776 return j, jo
777}
778
779func honkworldwide(user *WhatAbout, honk *Honk) {
780 jonk, _ := jonkjonk(user, honk)
781 jonk["@context"] = itiswhatitis
782 var buf bytes.Buffer
783 jonk.Write(&buf)
784 msg := buf.Bytes()
785
786 rcpts := make(map[string]bool)
787 for _, a := range honk.Audience {
788 if a != thewholeworld && a != user.URL && !strings.HasSuffix(a, "/followers") {
789 box, _ := getboxes(a)
790 if box != nil {
791 if honk.Public && box.Shared != "" {
792 rcpts["%"+box.Shared] = true
793 } else {
794 rcpts["%"+box.In] = true
795 }
796 } else {
797 rcpts[a] = true
798 }
799 }
800 }
801 if honk.Public {
802 for _, f := range getdubs(user.ID) {
803 box, _ := getboxes(f.XID)
804 if box != nil && box.Shared != "" {
805 rcpts["%"+box.Shared] = true
806 } else {
807 rcpts[f.XID] = true
808 }
809 }
810 }
811 for a := range rcpts {
812 go deliverate(0, user.Name, a, msg)
813 }
814}
815
816func asjonker(user *WhatAbout) junk.Junk {
817 about := obfusbreak(user.About)
818
819 j := junk.New()
820 j["@context"] = itiswhatitis
821 j["id"] = user.URL
822 j["type"] = "Person"
823 j["inbox"] = user.URL + "/inbox"
824 j["outbox"] = user.URL + "/outbox"
825 j["followers"] = user.URL + "/followers"
826 j["following"] = user.URL + "/following"
827 j["name"] = user.Display
828 j["preferredUsername"] = user.Name
829 j["summary"] = about
830 j["url"] = user.URL
831 a := junk.New()
832 a["type"] = "icon"
833 a["mediaType"] = "image/png"
834 a["url"] = fmt.Sprintf("https://%s/a?a=%s", serverName, url.QueryEscape(user.URL))
835 j["icon"] = a
836 k := junk.New()
837 k["id"] = user.URL + "#key"
838 k["owner"] = user.URL
839 k["publicKeyPem"] = user.Key
840 j["publicKey"] = k
841
842 return j
843}
844
845var handfull = make(map[string]string)
846var handlock sync.Mutex
847
848func gofish(name string) string {
849 if name[0] == '@' {
850 name = name[1:]
851 }
852 m := strings.Split(name, "@")
853 if len(m) != 2 {
854 log.Printf("bad fish name: %s", name)
855 return ""
856 }
857 handlock.Lock()
858 ref, ok := handfull[name]
859 handlock.Unlock()
860 if ok {
861 return ref
862 }
863 row := stmtGetXonker.QueryRow(name, "fishname")
864 var href string
865 err := row.Scan(&href)
866 if err == nil {
867 handlock.Lock()
868 handfull[name] = href
869 handlock.Unlock()
870 return href
871 }
872 log.Printf("fishing for %s", name)
873 j, err := GetJunk(fmt.Sprintf("https://%s/.well-known/webfinger?resource=acct:%s", m[1], name))
874 if err != nil {
875 log.Printf("failed to go fish %s: %s", name, err)
876 handlock.Lock()
877 handfull[name] = ""
878 handlock.Unlock()
879 return ""
880 }
881 links, _ := j.GetArray("links")
882 for _, li := range links {
883 l, ok := li.(junk.Junk)
884 if !ok {
885 continue
886 }
887 href, _ := l.GetString("href")
888 rel, _ := l.GetString("rel")
889 t, _ := l.GetString("type")
890 if rel == "self" && friendorfoe(t) {
891 stmtSaveXonker.Exec(name, href, "fishname")
892 handlock.Lock()
893 handfull[name] = href
894 handlock.Unlock()
895 return href
896 }
897 }
898 handlock.Lock()
899 handfull[name] = ""
900 handlock.Unlock()
901 return ""
902}
903
904func investigate(name string) string {
905 if name == "" {
906 return ""
907 }
908 if name[0] == '@' {
909 name = gofish(name)
910 }
911 if name == "" {
912 return ""
913 }
914 obj, err := GetJunk(name)
915 if err != nil {
916 log.Printf("error investigating honker: %s", err)
917 return ""
918 }
919 t, _ := obj.GetString("type")
920 id, _ := obj.GetString("id")
921 if t != "Person" {
922 log.Printf("it's not a person! %s", name)
923 return ""
924 }
925 return id
926}