all repos — honk @ 34bfa758b2c8dbcd332d0f0083c642d5a028f5b4

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