all repos — honk @ 3c7de003820ee24bbe125c9f24e6e792d1bf0853

my fork of honk

fun.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	"crypto/rand"
 20	"crypto/sha512"
 21	"fmt"
 22	"html/template"
 23	"io"
 24	"net/http"
 25	"net/url"
 26	"os"
 27	"path"
 28	"regexp"
 29	"strconv"
 30	"strings"
 31	"time"
 32
 33	"github.com/dustin/go-humanize"
 34	"golang.org/x/net/html"
 35	"humungus.tedunangst.com/r/webs/gencache"
 36	"humungus.tedunangst.com/r/webs/htfilter"
 37	"humungus.tedunangst.com/r/webs/httpsig"
 38	"humungus.tedunangst.com/r/webs/login"
 39	"humungus.tedunangst.com/r/webs/mz"
 40	"humungus.tedunangst.com/r/webs/templates"
 41)
 42
 43var allowedclasses = make(map[string]bool)
 44
 45func init() {
 46	allowedclasses["kw"] = true
 47	allowedclasses["bi"] = true
 48	allowedclasses["st"] = true
 49	allowedclasses["nm"] = true
 50	allowedclasses["tp"] = true
 51	allowedclasses["op"] = true
 52	allowedclasses["cm"] = true
 53	allowedclasses["al"] = true
 54	allowedclasses["dl"] = true
 55}
 56
 57var relingo = make(map[string]string)
 58
 59func loadLingo() {
 60	for _, l := range []string{"honked", "bonked", "honked back", "qonked", "evented"} {
 61		v := l
 62		k := "lingo-" + strings.ReplaceAll(l, " ", "")
 63		getconfig(k, &v)
 64		relingo[l] = v
 65	}
 66}
 67
 68func prettifydate(d time.Time) string {
 69	var customMags = []humanize.RelTimeMagnitude{
 70		{time.Second, "now", time.Second},
 71		{2 * time.Second, "1s %s", 1},
 72		{time.Minute, "%ds %s", time.Second},
 73		{2 * time.Minute, "1m %s", 1},
 74		{time.Hour, "%dm %s", time.Minute},
 75		{2 * time.Hour, "1h %s", 1},
 76		{humanize.Day, "%dh %s", time.Hour},
 77		{2 * humanize.Day, "1d %s", 1},
 78		{humanize.Week, "%dd %s", humanize.Day},
 79		{2 * humanize.Week, "1w %s", 1},
 80		{humanize.Month, "%dw %s", humanize.Week},
 81	}
 82
 83	since := time.Since(d)
 84	// More than a month, return the actual date.
 85	if since.Hours() > 730 {
 86		return d.Format("02 Jan 2006 15:04")
 87	}
 88
 89	return humanize.CustomRelTime(d, time.Now(), "", "from now", customMags)
 90}
 91func reverbolate(userid UserID, honks []*Honk) {
 92	user, _ := somenumberedusers.Get(userid)
 93	for _, h := range honks {
 94		// idk where else to put this
 95		h.DatePretty = prettifydate(h.Date)
 96		h.What += "ed"
 97		if h.What == "honked" && h.RID != "" {
 98			h.What = "honked back"
 99			h.Style += " subtle"
100		}
101		if !h.Public {
102			h.Style += " limited"
103		}
104		if h.Whofore == 1 {
105			h.Style += " atme"
106		}
107		translate(h)
108		local := false
109		if h.Whofore == 2 || h.Whofore == 3 {
110			local = true
111		}
112		if local && h.What != "bonked" {
113			h.Noise = re_memes.ReplaceAllString(h.Noise, "")
114		}
115		h.Username, h.Handle = handles(h.Honker)
116		if !local {
117			short := shortname(userid, h.Honker)
118			if short != "" {
119				h.Username = short
120			} else {
121				h.Username = h.Handle
122				if len(h.Username) > 20 {
123					h.Username = h.Username[:20] + ".."
124				}
125			}
126		}
127		if user != nil {
128			hset := []string{}
129			if h.Honker != user.URL {
130				hset = append(hset, "@"+h.Handle)
131			}
132			if user.Options.MentionAll {
133				for _, a := range h.Audience {
134					if a == h.Honker || a == user.URL {
135						continue
136					}
137					_, hand := handles(a)
138					if hand != "" {
139						hand = "@" + hand
140						hset = append(hset, hand)
141					}
142				}
143			}
144			h.Handles = strings.Join(hset, " ")
145		}
146		if h.URL == "" {
147			h.URL = h.XID
148		}
149		if h.Oonker != "" {
150			_, h.Oondle = handles(h.Oonker)
151		}
152		h.Precis = demoji(h.Precis)
153		h.Noise = demoji(h.Noise)
154		h.Open = "open"
155		var misto string
156		for _, m := range h.Mentions {
157			if m.Where != h.Honker && !m.IsPresent(h.Noise) {
158				misto += " " + m.Who
159			}
160		}
161		var mistag string
162		for _, o := range h.Onts {
163			if !OntIsPresent(o, h.Noise) {
164				mistag += " " + o
165			}
166		}
167		if len(misto) > 0 || len(mistag) > 0 {
168			if len(misto) > 0 {
169				misto = "(" + misto[1:] + ")<p>"
170			}
171			if len(mistag) > 0 {
172				mistag = "<p>(" + mistag[1:] + ")"
173			}
174			h.Noise = misto + h.Noise + mistag
175		}
176
177		zap := make(map[string]bool)
178		{
179			var htf htfilter.Filter
180			htf.Imager = replaceimgsand(zap, false, h)
181			htf.SpanClasses = allowedclasses
182			htf.BaseURL, _ = url.Parse(h.XID)
183			emuxifier := func(e string) string {
184				for _, d := range h.Donks {
185					if d.Name == e {
186						zap[d.XID] = true
187						if d.Local {
188							return fmt.Sprintf(`<img class="emu" title="%s" src="/d/%s">`, d.Name, d.XID)
189						}
190					}
191				}
192				if local && h.What != "bonked" {
193					emu, _ := emucache.Get(e)
194					if emu != nil {
195						return fmt.Sprintf(`<img class="emu" title="%s" src="%s">`, emu.Name, emu.ID)
196					}
197				}
198				return e
199			}
200			htf.FilterText = func(w io.Writer, data string) {
201				data = htfilter.EscapeText(data)
202				data = re_emus.ReplaceAllStringFunc(data, emuxifier)
203				io.WriteString(w, data)
204			}
205			if user != nil {
206				htf.RetargetLink = func(href string) string {
207					h2 := strings.ReplaceAll(href, "/@", "/users/")
208					for _, m := range h.Mentions {
209						if h2 == m.Where || href == m.Where {
210							return "/h?xid=" + url.QueryEscape(m.Where)
211						}
212					}
213					return href
214				}
215			}
216			p, _ := htf.String(h.Precis)
217			n, _ := htf.String(h.Noise)
218			h.Precis = string(p)
219			h.Noise = string(n)
220		}
221		j := 0
222		for i := 0; i < len(h.Donks); i++ {
223			if !zap[h.Donks[i].XID] {
224				h.Donks[j] = h.Donks[i]
225				j++
226			}
227		}
228		h.Donks = h.Donks[:j]
229	}
230
231	unsee(honks, userid)
232
233	for _, h := range honks {
234		renderflags(h)
235
236		h.HTPrecis = template.HTML(h.Precis)
237		h.HTML = template.HTML(h.Noise)
238		if redo := relingo[h.What]; redo != "" {
239			h.What = redo
240		}
241	}
242}
243
244func replaceimgsand(zap map[string]bool, absolute bool, honk *Honk) func(node *html.Node) string {
245	return func(node *html.Node) string {
246		src := htfilter.GetAttr(node, "src")
247		alt := htfilter.GetAttr(node, "alt")
248		//title := GetAttr(node, "title")
249		if htfilter.HasClass(node, "Emoji") && alt != "" {
250			return alt
251		}
252		base := path.Base(src)
253		didx, _ := strconv.Atoi(base)
254		var d *Donk
255		if strings.HasPrefix(src, serverPrefix) && didx > 0 && didx <= len(honk.Donks) {
256			d = honk.Donks[didx-1]
257		} else {
258			d = finddonk(src)
259		}
260		if d != nil {
261			zap[d.XID] = true
262			base := ""
263			if absolute {
264				base = serverURL("")
265			}
266			return string(templates.Sprintf(`<img alt="%s" title="%s" src="%s/d/%s">`, alt, alt, base, d.XID))
267		}
268		return string(templates.Sprintf(`&lt;img alt="%s" src="<a href="%s">%s</a>"&gt;`, alt, src, src))
269	}
270}
271
272func translatechonk(ch *Chonk) {
273	noise := ch.Noise
274	if ch.Format == "markdown" {
275		var marker mz.Marker
276		noise = marker.Mark(noise)
277	}
278	var htf htfilter.Filter
279	htf.SpanClasses = allowedclasses
280	htf.BaseURL, _ = url.Parse(ch.XID)
281	ch.HTML, _ = htf.String(noise)
282}
283
284func filterchonk(ch *Chonk) {
285	translatechonk(ch)
286
287	noise := string(ch.HTML)
288
289	local := originate(ch.XID) == serverName
290
291	zap := make(map[string]bool)
292	emuxifier := func(e string) string {
293		for _, d := range ch.Donks {
294			if d.Name == e {
295				zap[d.XID] = true
296				if d.Local {
297					return fmt.Sprintf(`<img class="emu" title="%s" src="/d/%s">`, d.Name, d.XID)
298				}
299			}
300		}
301		if local {
302			emu, _ := emucache.Get(e)
303			if emu != nil {
304				return fmt.Sprintf(`<img class="emu" title="%s" src="%s">`, emu.Name, emu.ID)
305			}
306		}
307		return e
308	}
309	noise = re_emus.ReplaceAllStringFunc(noise, emuxifier)
310	j := 0
311	for i := 0; i < len(ch.Donks); i++ {
312		if !zap[ch.Donks[i].XID] {
313			ch.Donks[j] = ch.Donks[i]
314			j++
315		}
316	}
317	ch.Donks = ch.Donks[:j]
318
319	if strings.HasPrefix(noise, "<p>") {
320		noise = noise[3:]
321	}
322	ch.HTML = template.HTML(noise)
323	if short := shortname(ch.UserID, ch.Who); short != "" {
324		ch.Handle = short
325	} else {
326		ch.Handle, _ = handles(ch.Who)
327	}
328
329}
330
331func inlineimgsfor(honk *Honk) func(node *html.Node) string {
332	return func(node *html.Node) string {
333		src := htfilter.GetAttr(node, "src")
334		alt := htfilter.GetAttr(node, "alt")
335		base := path.Base(src)
336		didx, _ := strconv.Atoi(base)
337		if strings.HasPrefix(src, serverPrefix) && didx > 0 && didx <= len(honk.Donks) {
338			dlog.Printf("skipping inline image %s", src)
339			return ""
340		}
341		d := savedonk(src, "image", alt, "image", true)
342		if d != nil {
343			honk.Donks = append(honk.Donks, d)
344		}
345		dlog.Printf("inline img with src: %s", src)
346		return ""
347	}
348}
349
350func imaginate(honk *Honk) {
351	var htf htfilter.Filter
352	htf.Imager = inlineimgsfor(honk)
353	htf.BaseURL, _ = url.Parse(honk.XID)
354	htf.String(honk.Noise)
355}
356
357var re_dangerous = regexp.MustCompile("^[a-zA-Z]{2}:")
358
359func precipitate(honk *Honk) {
360	noise := honk.Noise
361	if re_dangerous.MatchString(noise) {
362		idx := strings.Index(noise, "\n")
363		if idx == -1 {
364			honk.Precis = noise
365			noise = ""
366		} else {
367			honk.Precis = noise[:idx]
368			noise = noise[idx+1:]
369		}
370		var marker mz.Marker
371		marker.Short = true
372		honk.Precis = marker.Mark(strings.TrimSpace(honk.Precis))
373		honk.Noise = noise
374	}
375}
376
377func translate(honk *Honk) {
378	if honk.Format == "html" {
379		return
380	}
381	noise := honk.Noise
382
383	var marker mz.Marker
384	marker.HashLinker = ontoreplacer
385	marker.AtLinker = attoreplacer
386	marker.AllowImages = true
387	noise = strings.TrimSpace(noise)
388	noise = marker.Mark(noise)
389	honk.Noise = noise
390	honk.Onts = append(honk.Onts, marker.HashTags...)
391	honk.Mentions = bunchofgrapes(marker.Mentions)
392	for _, t := range oneofakind(strings.Split(honk.Onties, " ")) {
393		if t[0] != '#' {
394			t = "#" + t
395		}
396		honk.Onts = append(honk.Onts, t)
397	}
398	honk.Onts = oneofakind(honk.Onts)
399	honk.Mentions = append(honk.Mentions, bunchofgrapes(oneofakind(strings.Split(honk.SeeAlso, " ")))...)
400}
401
402func redoimages(honk *Honk) {
403	zap := make(map[string]bool)
404	{
405		var htf htfilter.Filter
406		htf.Imager = replaceimgsand(zap, true, honk)
407		htf.SpanClasses = allowedclasses
408		htf.BaseURL, _ = url.Parse(honk.XID)
409		p, _ := htf.String(honk.Precis)
410		n, _ := htf.String(honk.Noise)
411		honk.Precis = string(p)
412		honk.Noise = string(n)
413	}
414	j := 0
415	for i := 0; i < len(honk.Donks); i++ {
416		if !zap[honk.Donks[i].XID] {
417			honk.Donks[j] = honk.Donks[i]
418			j++
419		}
420	}
421	honk.Donks = honk.Donks[:j]
422
423	honk.Noise = re_memes.ReplaceAllString(honk.Noise, "")
424	honk.Noise = strings.Replace(honk.Noise, "<a href=", "<a class=\"mention u-url\" href=", -1)
425}
426
427func xcelerate(b []byte) string {
428	letters := "BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz1234567891234567891234"
429	for i, c := range b {
430		b[i] = letters[c&63]
431	}
432	s := string(b)
433	return s
434}
435
436func shortxid(xid string) string {
437	h := sha512.New512_256()
438	io.WriteString(h, xid)
439	return xcelerate(h.Sum(nil)[:20])
440}
441
442func xfiltrate() string {
443	var b [18]byte
444	rand.Read(b[:])
445	return xcelerate(b[:])
446}
447
448func grapevine(mentions []Mention) []string {
449	var s []string
450	for _, m := range mentions {
451		s = append(s, m.Where)
452	}
453	return s
454}
455
456func bunchofgrapes(m []string) []Mention {
457	var mentions []Mention
458	for i := range m {
459		who := m[i]
460		if strings.HasPrefix(who, "@https://") {
461			mentions = append(mentions, Mention{Who: who, Where: who[1:]})
462			continue
463		}
464		where := gofish(who)
465		if where != "" {
466			mentions = append(mentions, Mention{Who: who, Where: where})
467		}
468	}
469	return mentions
470}
471
472type Emu struct {
473	ID   string
474	Name string
475	Type string
476}
477
478var re_emus = regexp.MustCompile(`:[[:alnum:]_-]+:`)
479
480var emucache = gencache.New(gencache.Options[string, *Emu]{Fill: func(ename string) (*Emu, bool) {
481	fname := ename[1 : len(ename)-1]
482	exts := []string{".png", ".gif"}
483	for _, ext := range exts {
484		_, err := os.Stat(dataDir + "/emus/" + fname + ext)
485		if err != nil {
486			continue
487		}
488		url := serverURL("/emu/%s%s", fname, ext)
489		if develMode {
490			url = fmt.Sprintf("/emu/%s%s", fname, ext)
491		}
492		return &Emu{ID: url, Name: ename, Type: "image/" + ext[1:]}, true
493	}
494	return nil, true
495}, Duration: 10 * time.Second})
496
497func herdofemus(noise string) []*Emu {
498	m := re_emus.FindAllString(noise, -1)
499	m = oneofakind(m)
500	var emus []*Emu
501	for _, e := range m {
502		emu, _ := emucache.Get(e)
503		if emu == nil {
504			continue
505		}
506		emus = append(emus, emu)
507	}
508	return emus
509}
510
511var re_memes = regexp.MustCompile("meme: ?([^\n]+)")
512var re_avatar = regexp.MustCompile("avatar: ?([^\n]+)")
513var re_banner = regexp.MustCompile("banner: ?([^\n]+)")
514var re_convoy = regexp.MustCompile("convoy: ?([^\n]+)")
515var re_convalidate = regexp.MustCompile("^(https?|tag|data):")
516
517func memetize(honk *Honk) {
518	repl := func(x string) string {
519		name := x[5:]
520		if name[0] == ' ' {
521			name = name[1:]
522		}
523		fd, err := os.Open(dataDir + "/memes/" + name)
524		if err != nil {
525			ilog.Printf("no meme for %s", name)
526			return x
527		}
528		var peek [512]byte
529		n, _ := fd.Read(peek[:])
530		ct := http.DetectContentType(peek[:n])
531		fd.Close()
532
533		url := serverURL("/meme/%s", name)
534		fileid, err := savefile(name, name, url, ct, false, nil, nil)
535		if err != nil {
536			elog.Printf("error saving meme: %s", err)
537			return x
538		}
539		d := &Donk{
540			FileID: fileid,
541			Name:   name,
542			Media:  ct,
543			URL:    url,
544			Local:  false,
545		}
546		honk.Donks = append(honk.Donks, d)
547		return ""
548	}
549	honk.Noise = re_memes.ReplaceAllStringFunc(honk.Noise, repl)
550}
551
552var re_quickmention = regexp.MustCompile("(^|[ \n])@[[:alnum:]_]+([ \n:;.,']|$)")
553
554func quickrename(s string, userid UserID) string {
555	nonstop := true
556	for nonstop {
557		nonstop = false
558		s = re_quickmention.ReplaceAllStringFunc(s, func(m string) string {
559			prefix := ""
560			if m[0] == ' ' || m[0] == '\n' {
561				prefix = m[:1]
562				m = m[1:]
563			}
564			prefix += "@"
565			m = m[1:]
566			tail := ""
567			if last := m[len(m)-1]; last == ' ' || last == '\n' ||
568				last == ':' || last == ';' ||
569				last == '.' || last == ',' || last == '\'' {
570				tail = m[len(m)-1:]
571				m = m[:len(m)-1]
572			}
573
574			xid := fullname(m, userid)
575
576			if xid != "" {
577				_, name := handles(xid)
578				if name != "" {
579					nonstop = true
580					m = name
581				}
582			}
583			return prefix + m + tail
584		})
585	}
586	return s
587}
588
589var shortnames = gencache.New(gencache.Options[UserID, map[string]string]{Fill: func(userid UserID) (map[string]string, bool) {
590	honkers := gethonkers(userid)
591	m := make(map[string]string)
592	for _, h := range honkers {
593		m[h.XID] = h.Name
594	}
595	return m, true
596}, Invalidator: &honkerinvalidator})
597
598func shortname(userid UserID, xid string) string {
599	m, ok := shortnames.Get(userid)
600	if ok {
601		return m[xid]
602	}
603	return ""
604}
605
606var fullnames = gencache.New(gencache.Options[UserID, map[string]string]{Fill: func(userid UserID) (map[string]string, bool) {
607	honkers := gethonkers(userid)
608	m := make(map[string]string)
609	for _, h := range honkers {
610		m[h.Name] = h.XID
611	}
612	return m, true
613}, Invalidator: &honkerinvalidator})
614
615func fullname(name string, userid UserID) string {
616	m, ok := fullnames.Get(userid)
617	if ok {
618		return m[name]
619	}
620	return ""
621}
622
623func attoreplacer(m string) string {
624	fill := `<span class="h-card"><a class="u-url mention" href="%s">%s</a></span>`
625	if strings.HasPrefix(m, "@https://") {
626		return fmt.Sprintf(fill, html.EscapeString(m[1:]), html.EscapeString(m))
627	}
628	where := gofish(m)
629	if where == "" {
630		return m
631	}
632	who := m[0 : 1+strings.IndexByte(m[1:], '@')]
633	return fmt.Sprintf(fill, html.EscapeString(where), html.EscapeString(who))
634}
635
636func ontoreplacer(h string) string {
637	return fmt.Sprintf(`<a class="mention hashtag" href="%s">%s</a>`,
638		serverURL("/o/%s", strings.ToLower(h[1:])), h)
639}
640
641var re_unurl = regexp.MustCompile("https://([^/]+).*/([^/]+)")
642var re_urlhost = regexp.MustCompile("https://([^/ #)]+)")
643
644func originate(u string) string {
645	m := re_urlhost.FindStringSubmatch(u)
646	if len(m) > 1 {
647		return m[1]
648	}
649	return ""
650}
651
652var allhandles = gencache.New(gencache.Options[string, string]{Fill: func(xid string) (string, bool) {
653	handle := getxonker(xid, "handle")
654	if handle == "" {
655		dlog.Printf("need to get a handle: %s", xid)
656		info, err := investigate(xid)
657		if err != nil {
658			m := re_unurl.FindStringSubmatch(xid)
659			if len(m) > 2 {
660				handle = m[2]
661			} else {
662				handle = xid
663			}
664		} else {
665			handle = info.Name
666		}
667	}
668	return handle, true
669}})
670
671// handle, handle@host
672func handles(xid string) (string, string) {
673	if xid == "" || xid == thewholeworld || strings.HasSuffix(xid, "/followers") {
674		return "", ""
675	}
676	handle, _ := allhandles.Get(xid)
677	if handle == xid {
678		return xid, xid
679	}
680	return handle, handle + "@" + originate(xid)
681}
682
683func butnottooloud(aud []string) {
684	for i, a := range aud {
685		if strings.HasSuffix(a, "/followers") {
686			aud[i] = ""
687		}
688	}
689}
690
691func loudandproud(aud []string) bool {
692	for _, a := range aud {
693		if a == thewholeworld {
694			return true
695		}
696	}
697	return false
698}
699
700func firstclass(honk *Honk) bool {
701	return honk.Audience[0] == thewholeworld
702}
703
704func oneofakind(a []string) []string {
705	seen := make(map[string]bool)
706	seen[""] = true
707	j := 0
708	for _, s := range a {
709		if !seen[s] {
710			seen[s] = true
711			a[j] = s
712			j++
713		}
714	}
715	if j < len(a)/2 {
716		rv := make([]string, j)
717		copy(rv, a[:j])
718		return rv
719	}
720	return a[:j]
721}
722
723var ziggies = gencache.New(gencache.Options[UserID, *KeyInfo]{Fill: func(userid UserID) (*KeyInfo, bool) {
724	user, ok := somenumberedusers.Get(userid)
725	if !ok {
726		return nil, false
727	}
728	ki := new(KeyInfo)
729	ki.keyname = user.URL + "#key"
730	ki.seckey = user.SecKey
731	return ki, true
732}})
733
734func ziggy(userid UserID) *KeyInfo {
735	ki, _ := ziggies.Get(userid)
736	return ki
737}
738
739var zaggies = gencache.New(gencache.Options[string, httpsig.PublicKey]{Fill: func(keyname string) (httpsig.PublicKey, bool) {
740	data := getxonker(keyname, "pubkey")
741	if data == "" {
742		dlog.Printf("hitting the webs for missing pubkey: %s", keyname)
743		j, err := GetJunk(readyLuserOne, keyname)
744		if err != nil {
745			ilog.Printf("error getting %s pubkey: %s", keyname, err)
746			when := time.Now().UTC().Format(dbtimeformat)
747			stmtSaveXonker.Exec(keyname, "failed", "pubkey", when)
748			return httpsig.PublicKey{}, true
749		}
750		allinjest(originate(keyname), j)
751		data = getxonker(keyname, "pubkey")
752		if data == "" {
753			ilog.Printf("key not found after ingesting")
754			when := time.Now().UTC().Format(dbtimeformat)
755			stmtSaveXonker.Exec(keyname, "failed", "pubkey", when)
756			return httpsig.PublicKey{}, true
757		}
758	}
759	if data == "failed" {
760		ilog.Printf("lookup previously failed key %s", keyname)
761		return httpsig.PublicKey{}, true
762	}
763	_, key, err := httpsig.DecodeKey(data)
764	if err != nil {
765		ilog.Printf("error decoding %s pubkey: %s", keyname, err)
766		return key, true
767	}
768	return key, true
769}, Limit: 512})
770
771func zaggy(keyname string) (httpsig.PublicKey, error) {
772	key, _ := zaggies.Get(keyname)
773	return key, nil
774}
775
776func savingthrow(keyname string) {
777	when := time.Now().Add(-30 * time.Minute).UTC().Format(dbtimeformat)
778	stmtDeleteXonker.Exec(keyname, "pubkey", when)
779	zaggies.Clear(keyname)
780}
781
782func keymatch(keyname string, actor string) string {
783	origin := originate(actor)
784	if origin == originate(keyname) {
785		return origin
786	}
787	return ""
788}
789
790func avatator(n string, uinfo *login.UserInfo) []byte {
791	var a []byte
792	if isurl(n) {
793		if uinfo != nil {
794			j, err := GetJunkFast(UserID(uinfo.UserID), n)
795			if err != nil {
796				dlog.Println("avatating: getting junk:", err)
797				a = avatateautogen(n)
798			}
799			pfpurl, _ := j.GetString("icon", "url")
800			res, err := http.Get(pfpurl)
801			if err != nil {
802				dlog.Println("avatating: getting pfp url:", err)
803				a = avatateautogen(n)
804			} else {
805				defer res.Body.Close()
806
807				pfpbytes, err := io.ReadAll(res.Body)
808				if err != nil {
809					dlog.Println("avatating: bruh shits clapped:", err)
810					a = avatateautogen(n)
811				}
812				a = pfpbytes
813			}
814		} else {
815			a = avatateautogen(n)
816		}
817	} else {
818		a = avatateautogen(n)
819	}
820
821	return a
822}