all repos — honk @ ecdd2d1c7e61d577342d260e3cff2ac0851da1a0

my fork of honk

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