all repos — honk @ dea3cb9d2c80622a1ff6273f60ac0fa87e0d3847

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/rsa"
 21	"fmt"
 22	"html/template"
 23	"log"
 24	"net/http"
 25	"os"
 26	"regexp"
 27	"strings"
 28	"sync"
 29
 30	"golang.org/x/net/html"
 31	"humungus.tedunangst.com/r/webs/htfilter"
 32	"humungus.tedunangst.com/r/webs/httpsig"
 33)
 34
 35func reverbolate(userid int64, honks []*Honk) {
 36	filt := htfilter.New()
 37	filt.Imager = replaceimg
 38	zilences := getzilences(userid)
 39	for _, h := range honks {
 40		h.What += "ed"
 41		if h.What == "tonked" {
 42			h.What = "honked back"
 43			h.Style = "subtle"
 44		}
 45		if !h.Public {
 46			h.Style += " limited"
 47		}
 48		translate(h)
 49		if h.Whofore == 2 || h.Whofore == 3 {
 50			h.URL = h.XID
 51			if h.What != "bonked" {
 52				h.Noise = re_memes.ReplaceAllString(h.Noise, "")
 53				h.Noise = mentionize(h.Noise)
 54				h.Noise = ontologize(h.Noise)
 55			}
 56			h.Username, h.Handle = handles(h.Honker)
 57		} else {
 58			_, h.Handle = handles(h.Honker)
 59			h.Username = h.Handle
 60			if len(h.Username) > 20 {
 61				h.Username = h.Username[:20] + ".."
 62			}
 63			if h.URL == "" {
 64				h.URL = h.XID
 65			}
 66		}
 67		if h.Oonker != "" {
 68			_, h.Oondle = handles(h.Oonker)
 69		}
 70		zap := make(map[*Donk]bool)
 71		h.Precis = unpucker(h.Precis)
 72		h.Noise = unpucker(h.Noise)
 73		h.Open = "open"
 74		if userid == -1 {
 75			if h.Precis != "" {
 76				h.Open = ""
 77			}
 78		} else {
 79			if badword := unsee(zilences, h.Precis, h.Noise, h.Donks); badword != "" {
 80				if h.Precis == "" {
 81					h.Precis = badword
 82				}
 83				h.Open = ""
 84			} else if h.Precis == "unspecified horror" {
 85				h.Precis = ""
 86			}
 87		}
 88		h.HTPrecis, _ = filt.String(h.Precis)
 89		h.HTML, _ = filt.String(h.Noise)
 90		emuxifier := func(e string) string {
 91			for _, d := range h.Donks {
 92				if d.Name == e {
 93					zap[d] = true
 94					if d.Local {
 95						return fmt.Sprintf(`<img class="emu" title="%s" src="/d/%s">`, d.Name, d.XID)
 96					}
 97				}
 98			}
 99			return e
100		}
101		h.HTPrecis = template.HTML(re_emus.ReplaceAllStringFunc(string(h.HTPrecis), emuxifier))
102		h.HTML = template.HTML(re_emus.ReplaceAllStringFunc(string(h.HTML), emuxifier))
103		j := 0
104		for i := 0; i < len(h.Donks); i++ {
105			if !zap[h.Donks[i]] {
106				h.Donks[j] = h.Donks[i]
107				j++
108			}
109		}
110		h.Donks = h.Donks[:j]
111	}
112}
113
114func replaceimg(node *html.Node) string {
115	src := htfilter.GetAttr(node, "src")
116	alt := htfilter.GetAttr(node, "alt")
117	//title := GetAttr(node, "title")
118	if htfilter.HasClass(node, "Emoji") && alt != "" {
119		return alt
120	}
121	alt = html.EscapeString(alt)
122	src = html.EscapeString(src)
123	d := finddonk(src)
124	if d != nil {
125		src = fmt.Sprintf("https://%s/d/%s", serverName, d.XID)
126		return fmt.Sprintf(`<img alt="%s" title="%s" src="%s">`, alt, alt, src)
127	}
128	return fmt.Sprintf(`&lt;img alt="%s" src="<a href="%s">%s<a>"&gt;`, alt, src, src)
129}
130
131func inlineimgs(node *html.Node) string {
132	src := htfilter.GetAttr(node, "src")
133	alt := htfilter.GetAttr(node, "alt")
134	//title := GetAttr(node, "title")
135	if htfilter.HasClass(node, "Emoji") && alt != "" {
136		return alt
137	}
138	alt = html.EscapeString(alt)
139	src = html.EscapeString(src)
140	if !strings.HasPrefix(src, "https://"+serverName+"/") {
141		d := savedonk(src, "image", alt, "image", true)
142		if d != nil {
143			src = fmt.Sprintf("https://%s/d/%s", serverName, d.XID)
144		}
145	}
146	log.Printf("inline img with src: %s", src)
147	return fmt.Sprintf(`<img alt="%s" title="%s" src="%s>`, alt, alt, src)
148}
149
150func translate(honk *Honk) {
151	if honk.Format == "html" {
152		return
153	}
154	noise := honk.Noise
155	if strings.HasPrefix(noise, "DZ:") {
156		idx := strings.Index(noise, "\n")
157		if idx == -1 {
158			honk.Precis = noise
159			noise = ""
160		} else {
161			honk.Precis = noise[:idx]
162			noise = noise[idx+1:]
163		}
164	}
165	honk.Precis = strings.TrimSpace(honk.Precis)
166
167	noise = strings.TrimSpace(noise)
168	noise = quickrename(noise, honk.UserID)
169	noise = obfusbreak(noise)
170
171	honk.Noise = noise
172	honk.Onts = oneofakind(ontologies(honk.Noise))
173}
174
175func unsee(zilences []*regexp.Regexp, precis string, noise string, donks []*Donk) string {
176	for _, z := range zilences {
177		if z.MatchString(precis) || z.MatchString(noise) {
178			if precis == "" {
179				w := z.String()
180				return w[6 : len(w)-3]
181			}
182			return precis
183		}
184		for _, d := range donks {
185			if z.MatchString(d.Desc) {
186				if precis == "" {
187					w := z.String()
188					return w[6 : len(w)-3]
189				}
190				return precis
191			}
192		}
193	}
194	return ""
195}
196
197func osmosis(honks []*Honk, userid int64) []*Honk {
198	zords := getzords(userid)
199	j := 0
200outer:
201	for _, h := range honks {
202		for _, z := range zords {
203			if z.MatchString(h.Precis) || z.MatchString(h.Noise) {
204				continue outer
205			}
206			for _, d := range h.Donks {
207				if z.MatchString(d.Desc) {
208					continue outer
209				}
210			}
211		}
212		honks[j] = h
213		j++
214	}
215	honks = honks[0:j]
216	return honks
217}
218
219func shortxid(xid string) string {
220	idx := strings.LastIndexByte(xid, '/')
221	if idx == -1 {
222		return xid
223	}
224	return xid[idx+1:]
225}
226
227func xfiltrate() string {
228	letters := "BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz1234567891234567891234"
229	var b [18]byte
230	rand.Read(b[:])
231	for i, c := range b {
232		b[i] = letters[c&63]
233	}
234	s := string(b[:])
235	return s
236}
237
238var re_hashes = regexp.MustCompile(`(?:^| )#[[:alnum:]][[:alnum:]_-]*`)
239
240func ontologies(s string) []string {
241	m := re_hashes.FindAllString(s, -1)
242	j := 0
243	for _, h := range m {
244		if h[0] == '&' {
245			continue
246		}
247		if h[0] != '#' {
248			h = h[1:]
249		}
250		m[j] = h
251		j++
252	}
253	return m[:j]
254}
255
256type Mention struct {
257	who   string
258	where string
259}
260
261var re_mentions = regexp.MustCompile(`@[[:alnum:]._-]+@[[:alnum:].-]*[[:alnum:]]`)
262var re_urltions = regexp.MustCompile(`@https://\S+`)
263
264func grapevine(s string) []string {
265	var mentions []string
266	m := re_mentions.FindAllString(s, -1)
267	for i := range m {
268		where := gofish(m[i])
269		if where != "" {
270			mentions = append(mentions, where)
271		}
272	}
273	m = re_urltions.FindAllString(s, -1)
274	for i := range m {
275		mentions = append(mentions, m[i][1:])
276	}
277	return mentions
278}
279
280func bunchofgrapes(s string) []Mention {
281	m := re_mentions.FindAllString(s, -1)
282	var mentions []Mention
283	for i := range m {
284		where := gofish(m[i])
285		if where != "" {
286			mentions = append(mentions, Mention{who: m[i], where: where})
287		}
288	}
289	m = re_urltions.FindAllString(s, -1)
290	for i := range m {
291		mentions = append(mentions, Mention{who: m[i][1:], where: m[i][1:]})
292	}
293	return mentions
294}
295
296type Emu struct {
297	ID   string
298	Name string
299}
300
301var re_link = regexp.MustCompile(`@?https?://[^\s"]+[\w/)]`)
302var re_emus = regexp.MustCompile(`:[[:alnum:]_-]+:`)
303
304func herdofemus(noise string) []Emu {
305	m := re_emus.FindAllString(noise, -1)
306	m = oneofakind(m)
307	var emus []Emu
308	for _, e := range m {
309		fname := e[1 : len(e)-1]
310		_, err := os.Stat("emus/" + fname + ".png")
311		if err != nil {
312			continue
313		}
314		url := fmt.Sprintf("https://%s/emu/%s.png", serverName, fname)
315		emus = append(emus, Emu{ID: url, Name: e})
316	}
317	return emus
318}
319
320var re_memes = regexp.MustCompile("meme: ?([[:alnum:]_.-]+)")
321
322func memetize(honk *Honk) {
323	repl := func(x string) string {
324		name := x[5:]
325		if name[0] == ' ' {
326			name = name[1:]
327		}
328		fd, err := os.Open("memes/" + name)
329		if err != nil {
330			log.Printf("no meme for %s", name)
331			return x
332		}
333		var peek [512]byte
334		n, _ := fd.Read(peek[:])
335		ct := http.DetectContentType(peek[:n])
336		fd.Close()
337
338		url := fmt.Sprintf("https://%s/meme/%s", serverName, name)
339		fileid, err := savefile("", name, name, url, ct, false, nil)
340		if err != nil {
341			log.Printf("error saving meme: %s", err)
342			return x
343		}
344		var d Donk
345		d.FileID = fileid
346		d.XID = ""
347		d.Name = name
348		d.Media = ct
349		d.URL = url
350		d.Local = false
351		honk.Donks = append(honk.Donks, &d)
352		return ""
353	}
354	honk.Noise = re_memes.ReplaceAllStringFunc(honk.Noise, repl)
355}
356
357var re_bolder = regexp.MustCompile(`(^|\W)\*\*([\w\s,.!?':_-]+)\*\*($|\W)`)
358var re_italicer = regexp.MustCompile(`(^|\W)\*([\w\s,.!?':_-]+)\*($|\W)`)
359var re_bigcoder = regexp.MustCompile("```\n?((?s:.*?))\n?```\n?")
360var re_coder = regexp.MustCompile("`([^`]*)`")
361var re_quoter = regexp.MustCompile(`(?m:^&gt; (.*)\n?)`)
362
363func markitzero(s string) string {
364	var bigcodes []string
365	bigsaver := func(code string) string {
366		bigcodes = append(bigcodes, code)
367		return "``````"
368	}
369	s = re_bigcoder.ReplaceAllStringFunc(s, bigsaver)
370	var lilcodes []string
371	lilsaver := func(code string) string {
372		lilcodes = append(lilcodes, code)
373		return "`x`"
374	}
375	s = re_coder.ReplaceAllStringFunc(s, lilsaver)
376	s = re_bolder.ReplaceAllString(s, "$1<b>$2</b>$3")
377	s = re_italicer.ReplaceAllString(s, "$1<i>$2</i>$3")
378	s = re_quoter.ReplaceAllString(s, "<blockquote>$1</blockquote><p>")
379	lilun := func(s string) string {
380		code := lilcodes[0]
381		lilcodes = lilcodes[1:]
382		return code
383	}
384	s = re_coder.ReplaceAllStringFunc(s, lilun)
385	bigun := func(s string) string {
386		code := bigcodes[0]
387		bigcodes = bigcodes[1:]
388		return code
389	}
390	s = re_bigcoder.ReplaceAllStringFunc(s, bigun)
391	s = re_bigcoder.ReplaceAllString(s, "<pre><code>$1</code></pre><p>")
392	s = re_coder.ReplaceAllString(s, "<code>$1</code>")
393	return s
394}
395
396func obfusbreak(s string) string {
397	s = strings.TrimSpace(s)
398	s = strings.Replace(s, "\r", "", -1)
399	s = html.EscapeString(s)
400	// dammit go
401	s = strings.Replace(s, "&#39;", "'", -1)
402	linkfn := func(url string) string {
403		if url[0] == '@' {
404			return url
405		}
406		addparen := false
407		adddot := false
408		if strings.HasSuffix(url, ")") && strings.IndexByte(url, '(') == -1 {
409			url = url[:len(url)-1]
410			addparen = true
411		}
412		if strings.HasSuffix(url, ".") {
413			url = url[:len(url)-1]
414			adddot = true
415		}
416		url = fmt.Sprintf(`<a class="mention u-url" href="%s">%s</a>`, url, url)
417		if adddot {
418			url += "."
419		}
420		if addparen {
421			url += ")"
422		}
423		return url
424	}
425	s = re_link.ReplaceAllStringFunc(s, linkfn)
426
427	s = markitzero(s)
428
429	s = strings.Replace(s, "\n", "<br>", -1)
430	return s
431}
432
433var re_quickmention = regexp.MustCompile("(^| )@[[:alnum:]]+ ")
434
435func quickrename(s string, userid int64) string {
436	return re_quickmention.ReplaceAllStringFunc(s, func(m string) string {
437		prefix := ""
438		if m[0] == ' ' {
439			prefix = " "
440			m = m[1:]
441		}
442		prefix += "@"
443		m = m[1:]
444		m = m[:len(m)-1]
445
446		row := stmtOneHonker.QueryRow(m, userid)
447		var xid string
448		err := row.Scan(&xid)
449		if err == nil {
450			_, name := handles(xid)
451			if name != "" {
452				m = name
453			}
454		}
455		return prefix + m + " "
456	})
457}
458
459func mentionize(s string) string {
460	s = re_mentions.ReplaceAllStringFunc(s, func(m string) string {
461		where := gofish(m)
462		if where == "" {
463			return m
464		}
465		who := m[0 : 1+strings.IndexByte(m[1:], '@')]
466		return fmt.Sprintf(`<span class="h-card"><a class="u-url mention" href="%s">%s</a></span>`,
467			html.EscapeString(where), html.EscapeString(who))
468	})
469	s = re_urltions.ReplaceAllStringFunc(s, func(m string) string {
470		return fmt.Sprintf(`<span class="h-card"><a class="u-url mention" href="%s">%s</a></span>`,
471			html.EscapeString(m[1:]), html.EscapeString(m))
472	})
473	return s
474}
475
476func ontologize(s string) string {
477	s = re_hashes.ReplaceAllStringFunc(s, func(o string) string {
478		if o[0] == '&' {
479			return o
480		}
481		p := ""
482		h := o
483		if h[0] != '#' {
484			p = h[:1]
485			h = h[1:]
486		}
487		return fmt.Sprintf(`%s<a class="mention u-url" href="https://%s/o/%s">%s</a>`, p, serverName,
488			strings.ToLower(h[1:]), h)
489	})
490	return s
491}
492
493var re_unurl = regexp.MustCompile("https://([^/]+).*/([^/]+)")
494var re_urlhost = regexp.MustCompile("https://([^/ ]+)")
495
496func originate(u string) string {
497	m := re_urlhost.FindStringSubmatch(u)
498	if len(m) > 1 {
499		return m[1]
500	}
501	return ""
502}
503
504var allhandles = make(map[string]string)
505var handlelock sync.Mutex
506
507// handle, handle@host
508func handles(xid string) (string, string) {
509	if xid == "" {
510		return "", ""
511	}
512	handlelock.Lock()
513	handle := allhandles[xid]
514	handlelock.Unlock()
515	if handle == "" {
516		handle = findhandle(xid)
517		handlelock.Lock()
518		allhandles[xid] = handle
519		handlelock.Unlock()
520	}
521	if handle == xid {
522		return xid, xid
523	}
524	return handle, handle + "@" + originate(xid)
525}
526
527func findhandle(xid string) string {
528	row := stmtGetXonker.QueryRow(xid, "handle")
529	var handle string
530	err := row.Scan(&handle)
531	if err != nil {
532		p, _ := investigate(xid)
533		if p == nil {
534			m := re_unurl.FindStringSubmatch(xid)
535			if len(m) > 2 {
536				handle = m[2]
537			} else {
538				handle = xid
539			}
540		} else {
541			handle = p.Handle
542		}
543		_, err = stmtSaveXonker.Exec(xid, handle, "handle")
544		if err != nil {
545			log.Printf("error saving handle: %s", err)
546		}
547	}
548	return handle
549}
550
551var handleprelock sync.Mutex
552
553func prehandle(xid string) {
554	handleprelock.Lock()
555	defer handleprelock.Unlock()
556	handles(xid)
557}
558
559func prepend(s string, x []string) []string {
560	return append([]string{s}, x...)
561}
562
563// pleroma leaks followers addressed posts to followers
564func butnottooloud(aud []string) {
565	for i, a := range aud {
566		if strings.HasSuffix(a, "/followers") {
567			aud[i] = ""
568		}
569	}
570}
571
572func keepitquiet(aud []string) bool {
573	for _, a := range aud {
574		if a == thewholeworld {
575			return false
576		}
577	}
578	return true
579}
580
581func firstclass(honk *Honk) bool {
582	return honk.Audience[0] == thewholeworld
583}
584
585func oneofakind(a []string) []string {
586	var x []string
587	for n, s := range a {
588		if s != "" {
589			x = append(x, s)
590			for i := n + 1; i < len(a); i++ {
591				if a[i] == s {
592					a[i] = ""
593				}
594			}
595		}
596	}
597	return x
598}
599
600var ziggies = make(map[string]*rsa.PrivateKey)
601var zaggies = make(map[string]*rsa.PublicKey)
602var ziggylock sync.Mutex
603
604func ziggy(username string) (keyname string, key *rsa.PrivateKey) {
605	ziggylock.Lock()
606	key = ziggies[username]
607	ziggylock.Unlock()
608	if key == nil {
609		db := opendatabase()
610		row := db.QueryRow("select seckey from users where username = ?", username)
611		var data string
612		row.Scan(&data)
613		var err error
614		key, _, err = httpsig.DecodeKey(data)
615		if err != nil {
616			log.Printf("error decoding %s seckey: %s", username, err)
617			return
618		}
619		ziggylock.Lock()
620		ziggies[username] = key
621		ziggylock.Unlock()
622	}
623	keyname = fmt.Sprintf("https://%s/%s/%s#key", serverName, userSep, username)
624	return
625}
626
627func zaggy(keyname string) (key *rsa.PublicKey) {
628	ziggylock.Lock()
629	key = zaggies[keyname]
630	ziggylock.Unlock()
631	if key != nil {
632		return
633	}
634	row := stmtGetXonker.QueryRow(keyname, "pubkey")
635	var data string
636	err := row.Scan(&data)
637	if err != nil {
638		log.Printf("hitting the webs for missing pubkey: %s", keyname)
639		j, err := GetJunk(keyname)
640		if err != nil {
641			log.Printf("error getting %s pubkey: %s", keyname, err)
642			return
643		}
644		keyobj, ok := j.GetMap("publicKey")
645		if ok {
646			j = keyobj
647		}
648		data, ok = j.GetString("publicKeyPem")
649		if !ok {
650			log.Printf("error finding %s pubkey", keyname)
651			return
652		}
653		_, ok = j.GetString("owner")
654		if !ok {
655			log.Printf("error finding %s pubkey owner", keyname)
656			return
657		}
658		_, key, err = httpsig.DecodeKey(data)
659		if err != nil {
660			log.Printf("error decoding %s pubkey: %s", keyname, err)
661			return
662		}
663		_, err = stmtSaveXonker.Exec(keyname, data, "pubkey")
664		if err != nil {
665			log.Printf("error saving key: %s", err)
666		}
667	} else {
668		_, key, err = httpsig.DecodeKey(data)
669		if err != nil {
670			log.Printf("error decoding %s pubkey: %s", keyname, err)
671			return
672		}
673	}
674	ziggylock.Lock()
675	zaggies[keyname] = key
676	ziggylock.Unlock()
677	return
678}
679
680func makeitworksomehowwithoutregardforkeycontinuity(keyname string, r *http.Request, payload []byte) (string, error) {
681	_, err := stmtDeleteXonker.Exec(keyname, "pubkey")
682	if err != nil {
683		log.Printf("error deleting key: %s", err)
684	}
685	ziggylock.Lock()
686	delete(zaggies, keyname)
687	ziggylock.Unlock()
688	return httpsig.VerifyRequest(r, payload, zaggy)
689}
690
691var thumbbiters map[int64]map[string]bool
692var zoggles map[int64]map[string]bool
693var zordses map[int64][]*regexp.Regexp
694var zilences map[int64][]*regexp.Regexp
695var thumblock sync.Mutex
696
697func bitethethumbs() {
698	rows, err := stmtThumbBiters.Query()
699	if err != nil {
700		log.Printf("error getting thumbbiters: %s", err)
701		return
702	}
703	defer rows.Close()
704
705	thumblock.Lock()
706	defer thumblock.Unlock()
707	thumbbiters = make(map[int64]map[string]bool)
708	zoggles = make(map[int64]map[string]bool)
709	zordses = make(map[int64][]*regexp.Regexp)
710	zilences = make(map[int64][]*regexp.Regexp)
711	for rows.Next() {
712		var userid int64
713		var name, wherefore string
714		err = rows.Scan(&userid, &name, &wherefore)
715		if err != nil {
716			log.Printf("error scanning zonker: %s", err)
717			continue
718		}
719		if wherefore == "zord" || wherefore == "zilence" {
720			zord := "\\b(?i:" + name + ")\\b"
721			re, err := regexp.Compile(zord)
722			if err != nil {
723				log.Printf("error compiling zord: %s", err)
724			} else {
725				if wherefore == "zord" {
726					zordses[userid] = append(zordses[userid], re)
727				} else {
728					zilences[userid] = append(zilences[userid], re)
729				}
730			}
731		}
732		if wherefore == "zoggle" {
733			m := zoggles[userid]
734			if m == nil {
735				m = make(map[string]bool)
736				zoggles[userid] = m
737			}
738			m[name] = true
739		}
740		if wherefore == "zonker" || wherefore == "zomain" {
741			m := thumbbiters[userid]
742			if m == nil {
743				m = make(map[string]bool)
744				thumbbiters[userid] = m
745			}
746			m[name] = true
747		}
748	}
749}
750
751func getzords(userid int64) []*regexp.Regexp {
752	thumblock.Lock()
753	defer thumblock.Unlock()
754	return zordses[userid]
755}
756
757func getzilences(userid int64) []*regexp.Regexp {
758	thumblock.Lock()
759	defer thumblock.Unlock()
760	return zilences[userid]
761}
762
763func thoudostbitethythumb(userid int64, who []string, objid string) bool {
764	thumblock.Lock()
765	biters := thumbbiters[userid]
766	thumblock.Unlock()
767	objwhere := originate(objid)
768	if objwhere != "" && biters[objwhere] {
769		log.Printf("thumbbiter: %s", objid)
770		return true
771	}
772	for _, w := range who {
773		if biters[w] {
774			log.Printf("thumbbiter: %s", w)
775			return true
776		}
777		where := originate(w)
778		if where != "" {
779			if biters[where] {
780				log.Printf("thumbbiter: %s", w)
781				return true
782			}
783		}
784	}
785	return false
786}
787
788func stealthmode(userid int64, r *http.Request) bool {
789	agent := r.UserAgent()
790	agent = originate(agent)
791	addr := r.Header.Get("X-Forwarded-For")
792	thumblock.Lock()
793	biters := thumbbiters[userid]
794	thumblock.Unlock()
795	fake := (agent != "" && biters[agent]) || (addr != "" && biters[addr])
796	if fake {
797		log.Printf("faking 404 for %s from %s", agent, addr)
798	}
799	return fake
800}
801
802func keymatch(keyname string, actor string) string {
803	hash := strings.IndexByte(keyname, '#')
804	if hash == -1 {
805		hash = len(keyname)
806	}
807	owner := keyname[0:hash]
808	if owner == actor {
809		return originate(actor)
810	}
811	return ""
812}