all repos — honk @ fb041c8ad9a9d86390275c95b38643420b0b3d5d

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