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 onepepper = string([]byte{0xf0, 0x9f, 0x8c, 0xb6})
683var re_spicy = regexp.MustCompile("^(" + onepepper + "\ufe0f?){3}")
684
685func jonkjonk(user *WhatAbout, h *Honk) (junk.Junk, junk.Junk) {
686 dt := h.Date.Format(time.RFC3339)
687 var jo junk.Junk
688 j := junk.New()
689 j["id"] = user.URL + "/" + h.What + "/" + shortxid(h.XID)
690 j["actor"] = user.URL
691 j["published"] = dt
692 j["to"] = h.Audience[0]
693 if len(h.Audience) > 1 {
694 j["cc"] = h.Audience[1:]
695 }
696
697 switch h.What {
698 case "tonk":
699 fallthrough
700 case "honk":
701 j["type"] = "Create"
702
703 jo = junk.New()
704 jo["id"] = h.XID
705 jo["type"] = "Note"
706 jo["published"] = dt
707 jo["url"] = h.XID
708 jo["attributedTo"] = user.URL
709 if h.RID != "" {
710 jo["inReplyTo"] = h.RID
711 }
712 if h.Convoy != "" {
713 jo["context"] = h.Convoy
714 jo["conversation"] = h.Convoy
715 }
716 jo["to"] = h.Audience[0]
717 if len(h.Audience) > 1 {
718 jo["cc"] = h.Audience[1:]
719 }
720 if !h.Public {
721 jo["directMessage"] = true
722 }
723 jo["summary"] = h.Precis
724 jo["content"] = mentionize(h.Noise)
725 if strings.HasPrefix(h.Precis, "DZ:") || re_spicy.MatchString(h.Noise) {
726 jo["sensitive"] = true
727 }
728 var tags []interface{}
729 g := bunchofgrapes(h.Noise)
730 for _, m := range g {
731 t := junk.New()
732 t["type"] = "Mention"
733 t["name"] = m.who
734 t["href"] = m.where
735 tags = append(tags, t)
736 }
737 herd := herdofemus(h.Noise)
738 for _, e := range herd {
739 t := junk.New()
740 t["id"] = e.ID
741 t["type"] = "Emoji"
742 t["name"] = e.Name
743 i := junk.New()
744 i["type"] = "Image"
745 i["mediaType"] = "image/png"
746 i["url"] = e.ID
747 t["icon"] = i
748 tags = append(tags, t)
749 }
750 if len(tags) > 0 {
751 jo["tag"] = tags
752 }
753 var atts []interface{}
754 for _, d := range h.Donks {
755 if re_emus.MatchString(d.Name) {
756 continue
757 }
758 jd := junk.New()
759 jd["mediaType"] = d.Media
760 jd["name"] = d.Name
761 jd["type"] = "Document"
762 jd["url"] = d.URL
763 atts = append(atts, jd)
764 }
765 if len(atts) > 0 {
766 jo["attachment"] = atts
767 }
768 j["object"] = jo
769 case "bonk":
770 j["type"] = "Announce"
771 j["object"] = h.XID
772 case "zonk":
773 j["type"] = "Delete"
774 j["object"] = h.XID
775 }
776
777 return j, jo
778}
779
780func honkworldwide(user *WhatAbout, honk *Honk) {
781 jonk, _ := jonkjonk(user, honk)
782 jonk["@context"] = itiswhatitis
783 var buf bytes.Buffer
784 jonk.Write(&buf)
785 msg := buf.Bytes()
786
787 rcpts := make(map[string]bool)
788 for _, a := range honk.Audience {
789 if a != thewholeworld && a != user.URL && !strings.HasSuffix(a, "/followers") {
790 box, _ := getboxes(a)
791 if box != nil {
792 if honk.Public && box.Shared != "" {
793 rcpts["%"+box.Shared] = true
794 } else {
795 rcpts["%"+box.In] = true
796 }
797 } else {
798 rcpts[a] = true
799 }
800 }
801 }
802 if honk.Public {
803 for _, f := range getdubs(user.ID) {
804 box, _ := getboxes(f.XID)
805 if box != nil && box.Shared != "" {
806 rcpts["%"+box.Shared] = true
807 } else {
808 rcpts[f.XID] = true
809 }
810 }
811 }
812 for a := range rcpts {
813 go deliverate(0, user.Name, a, msg)
814 }
815}
816
817func asjonker(user *WhatAbout) junk.Junk {
818 about := obfusbreak(user.About)
819
820 j := junk.New()
821 j["@context"] = itiswhatitis
822 j["id"] = user.URL
823 j["type"] = "Person"
824 j["inbox"] = user.URL + "/inbox"
825 j["outbox"] = user.URL + "/outbox"
826 j["followers"] = user.URL + "/followers"
827 j["following"] = user.URL + "/following"
828 j["name"] = user.Display
829 j["preferredUsername"] = user.Name
830 j["summary"] = about
831 j["url"] = user.URL
832 a := junk.New()
833 a["type"] = "icon"
834 a["mediaType"] = "image/png"
835 a["url"] = fmt.Sprintf("https://%s/a?a=%s", serverName, url.QueryEscape(user.URL))
836 j["icon"] = a
837 k := junk.New()
838 k["id"] = user.URL + "#key"
839 k["owner"] = user.URL
840 k["publicKeyPem"] = user.Key
841 j["publicKey"] = k
842
843 return j
844}
845
846var handfull = make(map[string]string)
847var handlock sync.Mutex
848
849func gofish(name string) string {
850 if name[0] == '@' {
851 name = name[1:]
852 }
853 m := strings.Split(name, "@")
854 if len(m) != 2 {
855 log.Printf("bad fish name: %s", name)
856 return ""
857 }
858 handlock.Lock()
859 ref, ok := handfull[name]
860 handlock.Unlock()
861 if ok {
862 return ref
863 }
864 row := stmtGetXonker.QueryRow(name, "fishname")
865 var href string
866 err := row.Scan(&href)
867 if err == nil {
868 handlock.Lock()
869 handfull[name] = href
870 handlock.Unlock()
871 return href
872 }
873 log.Printf("fishing for %s", name)
874 j, err := GetJunk(fmt.Sprintf("https://%s/.well-known/webfinger?resource=acct:%s", m[1], name))
875 if err != nil {
876 log.Printf("failed to go fish %s: %s", name, err)
877 handlock.Lock()
878 handfull[name] = ""
879 handlock.Unlock()
880 return ""
881 }
882 links, _ := j.GetArray("links")
883 for _, li := range links {
884 l, ok := li.(junk.Junk)
885 if !ok {
886 continue
887 }
888 href, _ := l.GetString("href")
889 rel, _ := l.GetString("rel")
890 t, _ := l.GetString("type")
891 if rel == "self" && friendorfoe(t) {
892 stmtSaveXonker.Exec(name, href, "fishname")
893 handlock.Lock()
894 handfull[name] = href
895 handlock.Unlock()
896 return href
897 }
898 }
899 handlock.Lock()
900 handfull[name] = ""
901 handlock.Unlock()
902 return ""
903}
904
905func investigate(name string) string {
906 if name == "" {
907 return ""
908 }
909 if name[0] == '@' {
910 name = gofish(name)
911 }
912 if name == "" {
913 return ""
914 }
915 obj, err := GetJunk(name)
916 if err != nil {
917 log.Printf("error investigating honker: %s", err)
918 return ""
919 }
920 t, _ := obj.GetString("type")
921 id, _ := obj.GetString("id")
922 if t != "Person" {
923 log.Printf("it's not a person! %s", name)
924 return ""
925 }
926 return id
927}