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 gimmexonks(user *WhatAbout, outbox string) {
315 log.Printf("getting outbox: %s", outbox)
316 j, err := GetJunk(outbox)
317 if err != nil {
318 log.Printf("error getting outbox: %s", err)
319 return
320 }
321 t, _ := j.GetString("type")
322 origin := originate(outbox)
323 if t == "OrderedCollection" {
324 items, _ := j.GetArray("orderedItems")
325 if items == nil {
326 obj, ok := j.GetMap("first")
327 if ok {
328 items, _ = obj.GetArray("orderedItems")
329 } else {
330 page1, _ := j.GetString("first")
331 j, err = GetJunk(page1)
332 if err != nil {
333 log.Printf("error gettings page1: %s", err)
334 return
335 }
336 items, _ = j.GetArray("orderedItems")
337 }
338 }
339 if len(items) > 20 {
340 items = items[0:20]
341 }
342 for i, j := 0, len(items)-1; i < j; i, j = i+1, j-1 {
343 items[i], items[j] = items[j], items[i]
344 }
345 for _, item := range items {
346 obj, ok := item.(junk.Junk)
347 if !ok {
348 continue
349 }
350 xonk := xonkxonk(user, obj, origin)
351 if xonk != nil {
352 savexonk(user, xonk)
353 }
354 }
355 }
356}
357
358func peeppeep() {
359 user, _ := butwhatabout("htest")
360 honkers := gethonkers(user.ID)
361 for _, f := range honkers {
362 if f.Flavor != "peep" {
363 continue
364 }
365 log.Printf("getting updates: %s", f.XID)
366 box, err := getboxes(f.XID)
367 if err != nil {
368 log.Printf("error getting outbox: %s", err)
369 continue
370 }
371 gimmexonks(user, box.Out)
372 }
373}
374func whosthere(xid string) ([]string, string) {
375 obj, err := GetJunk(xid)
376 if err != nil {
377 log.Printf("error getting remote xonk: %s", err)
378 return nil, ""
379 }
380 convoy, _ := obj.GetString("context")
381 if convoy == "" {
382 convoy, _ = obj.GetString("conversation")
383 }
384 return newphone(nil, obj), convoy
385}
386
387func newphone(a []string, obj junk.Junk) []string {
388 for _, addr := range []string{"to", "cc", "attributedTo"} {
389 who, _ := obj.GetString(addr)
390 if who != "" {
391 a = append(a, who)
392 }
393 whos, _ := obj.GetArray(addr)
394 for _, w := range whos {
395 who, _ := w.(string)
396 if who != "" {
397 a = append(a, who)
398 }
399 }
400 }
401 return a
402}
403
404func consumeactivity(user *WhatAbout, j junk.Junk, origin string) {
405 xonk := xonkxonk(user, j, origin)
406 if xonk != nil {
407 savexonk(user, xonk)
408 }
409}
410
411func xonkxonk(user *WhatAbout, item junk.Junk, origin string) *Honk {
412 depth := 0
413 maxdepth := 4
414 currenttid := ""
415 var xonkxonkfn func(item junk.Junk, origin string) *Honk
416
417 saveoneup := func(xid string) {
418 log.Printf("getting oneup: %s", xid)
419 if depth >= maxdepth {
420 log.Printf("in too deep")
421 return
422 }
423 obj, err := GetJunk(xid)
424 if err != nil {
425 log.Printf("error getting oneup: %s", err)
426 return
427 }
428 depth++
429 xonk := xonkxonkfn(obj, originate(xid))
430 if xonk != nil {
431 savexonk(user, xonk)
432 }
433 depth--
434 }
435
436 xonkxonkfn = func(item junk.Junk, origin string) *Honk {
437 // id, _ := item.GetString( "id")
438 what, _ := item.GetString("type")
439 dt, _ := item.GetString("published")
440
441 var audience []string
442 var err error
443 var xid, rid, url, content, precis, convoy, oonker string
444 var obj junk.Junk
445 var ok bool
446 switch what {
447 case "Announce":
448 obj, ok = item.GetMap("object")
449 if ok {
450 xid, _ = obj.GetString("id")
451 } else {
452 xid, _ = item.GetString("object")
453 }
454 if !needxonkid(user, xid) {
455 return nil
456 }
457 log.Printf("getting bonk: %s", xid)
458 obj, err = GetJunk(xid)
459 if err != nil {
460 log.Printf("error regetting: %s", err)
461 }
462 origin = originate(xid)
463 what = "bonk"
464 case "Create":
465 obj, _ = item.GetMap("object")
466 what = "honk"
467 case "Delete":
468 obj, _ = item.GetMap("object")
469 xid, _ = item.GetString("object")
470 what = "eradicate"
471 case "Question":
472 fallthrough
473 case "Note":
474 fallthrough
475 case "Article":
476 fallthrough
477 case "Page":
478 obj = item
479 what = "honk"
480 default:
481 log.Printf("unknown activity: %s", what)
482 fd, _ := os.OpenFile("savedinbox.json", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
483 item.Write(fd)
484 io.WriteString(fd, "\n")
485 fd.Close()
486 return nil
487 }
488
489 var xonk Honk
490 who, _ := item.GetString("actor")
491 if obj != nil {
492 if who == "" {
493 who, _ = obj.GetString("attributedTo")
494 }
495 oonker, _ = obj.GetString("attributedTo")
496 ot, _ := obj.GetString("type")
497 url, _ = obj.GetString("url")
498 if ot == "Note" || ot == "Article" || ot == "Page" || ot == "Question" {
499 audience = newphone(audience, obj)
500 xid, _ = obj.GetString("id")
501 precis, _ = obj.GetString("summary")
502 content, _ = obj.GetString("content")
503 if !strings.HasPrefix(content, "<p>") {
504 content = "<p>" + content
505 }
506 rid, _ = obj.GetString("inReplyTo")
507 convoy, _ = obj.GetString("context")
508 if convoy == "" {
509 convoy, _ = obj.GetString("conversation")
510 }
511 if ot == "Question" {
512 what = "qonk"
513 content += "<ul>"
514 ans, _ := obj.GetArray("oneOf")
515 for _, ai := range ans {
516 a, ok := ai.(junk.Junk)
517 if !ok {
518 continue
519 }
520 as, _ := a.GetString("name")
521 content += "<li>" + as
522 }
523 ans, _ = obj.GetArray("anyOf")
524 for _, ai := range ans {
525 a, ok := ai.(junk.Junk)
526 if !ok {
527 continue
528 }
529 as, _ := a.GetString("name")
530 content += "<li>" + as
531 }
532 content += "</ul>"
533 }
534 if what == "honk" && rid != "" {
535 what = "tonk"
536 }
537 }
538 if ot == "Tombstone" {
539 xid, _ = obj.GetString("id")
540 }
541 atts, _ := obj.GetArray("attachment")
542 for i, atti := range atts {
543 att, ok := atti.(junk.Junk)
544 if !ok {
545 continue
546 }
547 at, _ := att.GetString("type")
548 mt, _ := att.GetString("mediaType")
549 u, _ := att.GetString("url")
550 name, _ := att.GetString("name")
551 localize := false
552 if i > 4 {
553 log.Printf("excessive attachment: %s", at)
554 } else if at == "Document" || at == "Image" {
555 mt = strings.ToLower(mt)
556 log.Printf("attachment: %s %s", mt, u)
557 if mt == "image/jpeg" || mt == "image/png" ||
558 mt == "text/plain" {
559 localize = true
560 }
561 } else {
562 log.Printf("unknown attachment: %s", at)
563 }
564 donk := savedonk(u, name, mt, localize)
565 if donk != nil {
566 xonk.Donks = append(xonk.Donks, donk)
567 }
568 }
569 tags, _ := obj.GetArray("tag")
570 for _, tagi := range tags {
571 tag, ok := tagi.(junk.Junk)
572 if !ok {
573 continue
574 }
575 tt, _ := tag.GetString("type")
576 name, _ := tag.GetString("name")
577 if tt == "Emoji" {
578 icon, _ := tag.GetMap("icon")
579 mt, _ := icon.GetString("mediaType")
580 if mt == "" {
581 mt = "image/png"
582 }
583 u, _ := icon.GetString("url")
584 donk := savedonk(u, name, mt, true)
585 if donk != nil {
586 xonk.Donks = append(xonk.Donks, donk)
587 }
588 }
589 }
590 }
591 if originate(xid) != origin {
592 log.Printf("original sin: %s <> %s", xid, origin)
593 item.Write(os.Stdout)
594 return nil
595 }
596 audience = append(audience, who)
597
598 audience = oneofakind(audience)
599
600 if currenttid == "" {
601 currenttid = convoy
602 }
603 if convoy == "" {
604 convoy = currenttid
605 }
606
607 if oonker == who {
608 oonker = ""
609 }
610 xonk.UserID = user.ID
611 xonk.What = what
612 xonk.Honker = who
613 xonk.XID = xid
614 xonk.RID = rid
615 xonk.Date, _ = time.Parse(time.RFC3339, dt)
616 xonk.URL = url
617 xonk.Noise = content
618 xonk.Precis = precis
619 xonk.Audience = audience
620 xonk.Convoy = convoy
621 xonk.Oonker = oonker
622
623 if needxonk(user, &xonk) {
624 if what == "tonk" {
625 if needxonkid(user, rid) {
626 saveoneup(rid)
627 }
628 }
629 return &xonk
630 }
631 return nil
632 }
633
634 return xonkxonkfn(item, origin)
635}
636
637func rubadubdub(user *WhatAbout, req junk.Junk) {
638 xid, _ := req.GetString("id")
639 actor, _ := req.GetString("actor")
640 j := junk.New()
641 j["@context"] = itiswhatitis
642 j["id"] = user.URL + "/dub/" + xid
643 j["type"] = "Accept"
644 j["actor"] = user.URL
645 j["to"] = actor
646 j["published"] = time.Now().UTC().Format(time.RFC3339)
647 j["object"] = req
648
649 var buf bytes.Buffer
650 j.Write(&buf)
651 msg := buf.Bytes()
652
653 deliverate(0, user.Name, actor, msg)
654}
655
656func itakeitallback(user *WhatAbout, xid string) {
657 j := junk.New()
658 j["@context"] = itiswhatitis
659 j["id"] = user.URL + "/unsub/" + xid
660 j["type"] = "Undo"
661 j["actor"] = user.URL
662 j["to"] = xid
663 f := junk.New()
664 f["id"] = user.URL + "/sub/" + xid
665 f["type"] = "Follow"
666 f["actor"] = user.URL
667 f["to"] = xid
668 j["object"] = f
669 j["published"] = time.Now().UTC().Format(time.RFC3339)
670
671 var buf bytes.Buffer
672 j.Write(&buf)
673 msg := buf.Bytes()
674
675 deliverate(0, user.Name, xid, msg)
676}
677
678func subsub(user *WhatAbout, xid string) {
679 j := junk.New()
680 j["@context"] = itiswhatitis
681 j["id"] = user.URL + "/sub/" + xid
682 j["type"] = "Follow"
683 j["actor"] = user.URL
684 j["to"] = xid
685 j["object"] = xid
686 j["published"] = time.Now().UTC().Format(time.RFC3339)
687
688 var buf bytes.Buffer
689 j.Write(&buf)
690 msg := buf.Bytes()
691
692 deliverate(0, user.Name, xid, msg)
693}
694
695var re_spicy = regexp.MustCompile("^(\U0001f336\ufe0f?){3}")
696
697func jonkjonk(user *WhatAbout, h *Honk) (junk.Junk, junk.Junk) {
698 dt := h.Date.Format(time.RFC3339)
699 var jo junk.Junk
700 j := junk.New()
701 j["id"] = user.URL + "/" + h.What + "/" + shortxid(h.XID)
702 j["actor"] = user.URL
703 j["published"] = dt
704 j["to"] = h.Audience[0]
705 if len(h.Audience) > 1 {
706 j["cc"] = h.Audience[1:]
707 }
708
709 switch h.What {
710 case "tonk":
711 fallthrough
712 case "honk":
713 j["type"] = "Create"
714
715 jo = junk.New()
716 jo["id"] = h.XID
717 jo["type"] = "Note"
718 jo["published"] = dt
719 jo["url"] = h.XID
720 jo["attributedTo"] = user.URL
721 if h.RID != "" {
722 jo["inReplyTo"] = h.RID
723 }
724 if h.Convoy != "" {
725 jo["context"] = h.Convoy
726 jo["conversation"] = h.Convoy
727 }
728 jo["to"] = h.Audience[0]
729 if len(h.Audience) > 1 {
730 jo["cc"] = h.Audience[1:]
731 }
732 if !h.Public {
733 jo["directMessage"] = true
734 }
735 jo["summary"] = h.Precis
736 jo["content"] = mentionize(h.Noise)
737 if strings.HasPrefix(h.Precis, "DZ:") || re_spicy.MatchString(h.Noise) {
738 jo["sensitive"] = true
739 }
740 var tags []interface{}
741 g := bunchofgrapes(h.Noise)
742 for _, m := range g {
743 t := junk.New()
744 t["type"] = "Mention"
745 t["name"] = m.who
746 t["href"] = m.where
747 tags = append(tags, t)
748 }
749 ooo := ontologies(h.Noise)
750 for _, o := range ooo {
751 t := junk.New()
752 t["type"] = "Hashtag"
753 t["name"] = o
754 tags = append(tags, t)
755 }
756 herd := herdofemus(h.Noise)
757 for _, e := range herd {
758 t := junk.New()
759 t["id"] = e.ID
760 t["type"] = "Emoji"
761 t["name"] = e.Name
762 i := junk.New()
763 i["type"] = "Image"
764 i["mediaType"] = "image/png"
765 i["url"] = e.ID
766 t["icon"] = i
767 tags = append(tags, t)
768 }
769 if len(tags) > 0 {
770 jo["tag"] = tags
771 }
772 var atts []interface{}
773 for _, d := range h.Donks {
774 if re_emus.MatchString(d.Name) {
775 continue
776 }
777 jd := junk.New()
778 jd["mediaType"] = d.Media
779 jd["name"] = d.Name
780 jd["type"] = "Document"
781 jd["url"] = d.URL
782 atts = append(atts, jd)
783 }
784 if len(atts) > 0 {
785 jo["attachment"] = atts
786 }
787 j["object"] = jo
788 case "bonk":
789 j["type"] = "Announce"
790 j["object"] = h.XID
791 case "zonk":
792 j["type"] = "Delete"
793 j["object"] = h.XID
794 }
795
796 return j, jo
797}
798
799func honkworldwide(user *WhatAbout, honk *Honk) {
800 jonk, _ := jonkjonk(user, honk)
801 jonk["@context"] = itiswhatitis
802 var buf bytes.Buffer
803 jonk.Write(&buf)
804 msg := buf.Bytes()
805
806 rcpts := make(map[string]bool)
807 for _, a := range honk.Audience {
808 if a != thewholeworld && a != user.URL && !strings.HasSuffix(a, "/followers") {
809 box, _ := getboxes(a)
810 if box != nil {
811 if honk.Public && box.Shared != "" {
812 rcpts["%"+box.Shared] = true
813 } else {
814 rcpts["%"+box.In] = true
815 }
816 } else {
817 rcpts[a] = true
818 }
819 }
820 }
821 if honk.Public {
822 for _, f := range getdubs(user.ID) {
823 box, _ := getboxes(f.XID)
824 if box != nil && box.Shared != "" {
825 rcpts["%"+box.Shared] = true
826 } else {
827 rcpts[f.XID] = true
828 }
829 }
830 }
831 for a := range rcpts {
832 go deliverate(0, user.Name, a, msg)
833 }
834}
835
836func asjonker(user *WhatAbout) junk.Junk {
837 about := obfusbreak(user.About)
838
839 j := junk.New()
840 j["@context"] = itiswhatitis
841 j["id"] = user.URL
842 j["type"] = "Person"
843 j["inbox"] = user.URL + "/inbox"
844 j["outbox"] = user.URL + "/outbox"
845 j["followers"] = user.URL + "/followers"
846 j["following"] = user.URL + "/following"
847 j["name"] = user.Display
848 j["preferredUsername"] = user.Name
849 j["summary"] = about
850 j["url"] = user.URL
851 a := junk.New()
852 a["type"] = "icon"
853 a["mediaType"] = "image/png"
854 a["url"] = fmt.Sprintf("https://%s/a?a=%s", serverName, url.QueryEscape(user.URL))
855 j["icon"] = a
856 k := junk.New()
857 k["id"] = user.URL + "#key"
858 k["owner"] = user.URL
859 k["publicKeyPem"] = user.Key
860 j["publicKey"] = k
861
862 return j
863}
864
865var handfull = make(map[string]string)
866var handlock sync.Mutex
867
868func gofish(name string) string {
869 if name[0] == '@' {
870 name = name[1:]
871 }
872 m := strings.Split(name, "@")
873 if len(m) != 2 {
874 log.Printf("bad fish name: %s", name)
875 return ""
876 }
877 handlock.Lock()
878 ref, ok := handfull[name]
879 handlock.Unlock()
880 if ok {
881 return ref
882 }
883 row := stmtGetXonker.QueryRow(name, "fishname")
884 var href string
885 err := row.Scan(&href)
886 if err == nil {
887 handlock.Lock()
888 handfull[name] = href
889 handlock.Unlock()
890 return href
891 }
892 log.Printf("fishing for %s", name)
893 j, err := GetJunk(fmt.Sprintf("https://%s/.well-known/webfinger?resource=acct:%s", m[1], name))
894 if err != nil {
895 log.Printf("failed to go fish %s: %s", name, err)
896 handlock.Lock()
897 handfull[name] = ""
898 handlock.Unlock()
899 return ""
900 }
901 links, _ := j.GetArray("links")
902 for _, li := range links {
903 l, ok := li.(junk.Junk)
904 if !ok {
905 continue
906 }
907 href, _ := l.GetString("href")
908 rel, _ := l.GetString("rel")
909 t, _ := l.GetString("type")
910 if rel == "self" && friendorfoe(t) {
911 stmtSaveXonker.Exec(name, href, "fishname")
912 handlock.Lock()
913 handfull[name] = href
914 handlock.Unlock()
915 return href
916 }
917 }
918 handlock.Lock()
919 handfull[name] = ""
920 handlock.Unlock()
921 return ""
922}
923
924func investigate(name string) string {
925 if name == "" {
926 return ""
927 }
928 if name[0] == '@' {
929 name = gofish(name)
930 }
931 if name == "" {
932 return ""
933 }
934 obj, err := GetJunk(name)
935 if err != nil {
936 log.Printf("error investigating honker: %s", err)
937 return ""
938 }
939 t, _ := obj.GetString("type")
940 id, _ := obj.GetString("id")
941 if t != "Person" {
942 log.Printf("it's not a person! %s", name)
943 return ""
944 }
945 return id
946}