all repos — honk @ 0ab213ded4a5cfed0de9c435bcfe542f6c33c748

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