all repos — honk @ v0.8.1

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	"crypto/sha512"
 22	"fmt"
 23	"html/template"
 24	"io"
 25	"log"
 26	"net/http"
 27	"os"
 28	"regexp"
 29	"strings"
 30
 31	"golang.org/x/net/html"
 32	"humungus.tedunangst.com/r/webs/cache"
 33	"humungus.tedunangst.com/r/webs/htfilter"
 34	"humungus.tedunangst.com/r/webs/httpsig"
 35	"humungus.tedunangst.com/r/webs/templates"
 36)
 37
 38var allowedclasses = make(map[string]bool)
 39
 40func init() {
 41	allowedclasses["kw"] = true
 42	allowedclasses["bi"] = true
 43	allowedclasses["st"] = true
 44	allowedclasses["nm"] = true
 45	allowedclasses["tp"] = true
 46	allowedclasses["op"] = true
 47	allowedclasses["cm"] = true
 48	allowedclasses["al"] = true
 49	allowedclasses["dl"] = true
 50}
 51
 52func reverbolate(userid int64, honks []*Honk) {
 53	for _, h := range honks {
 54		h.What += "ed"
 55		if h.What == "tonked" {
 56			h.What = "honked back"
 57			h.Style += " subtle"
 58		}
 59		if !h.Public {
 60			h.Style += " limited"
 61		}
 62		translate(h, false)
 63		if h.Whofore == 2 || h.Whofore == 3 {
 64			h.URL = h.XID
 65			if h.What != "bonked" {
 66				h.Noise = re_memes.ReplaceAllString(h.Noise, "")
 67				h.Noise = mentionize(h.Noise)
 68				h.Noise = ontologize(h.Noise)
 69			}
 70			h.Username, h.Handle = handles(h.Honker)
 71		} else {
 72			_, h.Handle = handles(h.Honker)
 73			short := shortname(userid, h.Honker)
 74			if short != "" {
 75				h.Username = short
 76			} else {
 77				h.Username = h.Handle
 78				if len(h.Username) > 20 {
 79					h.Username = h.Username[:20] + ".."
 80				}
 81			}
 82			if h.URL == "" {
 83				h.URL = h.XID
 84			}
 85		}
 86		if h.Oonker != "" {
 87			_, h.Oondle = handles(h.Oonker)
 88		}
 89		h.Precis = demoji(h.Precis)
 90		h.Noise = demoji(h.Noise)
 91		h.Open = "open"
 92
 93		zap := make(map[string]bool)
 94		{
 95			var htf htfilter.Filter
 96			htf.Imager = replaceimgsand(zap, false)
 97			htf.SpanClasses = allowedclasses
 98			p, _ := htf.String(h.Precis)
 99			n, _ := htf.String(h.Noise)
100			h.Precis = string(p)
101			h.Noise = string(n)
102		}
103
104		if userid == -1 {
105			if h.Precis != "" {
106				h.Open = ""
107			}
108		} else {
109			unsee(userid, h)
110			if h.Open == "open" && h.Precis == "unspecified horror" {
111				h.Precis = ""
112			}
113		}
114		if len(h.Noise) > 6000 && h.Open == "open" {
115			if h.Precis == "" {
116				h.Precis = "really freaking long"
117			}
118			h.Open = ""
119		}
120
121		emuxifier := func(e string) string {
122			for _, d := range h.Donks {
123				if d.Name == e {
124					zap[d.XID] = true
125					if d.Local {
126						return fmt.Sprintf(`<img class="emu" title="%s" src="/d/%s">`, d.Name, d.XID)
127					}
128				}
129			}
130			return e
131		}
132		h.Precis = re_emus.ReplaceAllStringFunc(h.Precis, emuxifier)
133		h.Noise = re_emus.ReplaceAllStringFunc(h.Noise, emuxifier)
134
135		j := 0
136		for i := 0; i < len(h.Donks); i++ {
137			if !zap[h.Donks[i].XID] {
138				h.Donks[j] = h.Donks[i]
139				j++
140			}
141		}
142		h.Donks = h.Donks[:j]
143
144		h.HTPrecis = template.HTML(h.Precis)
145		h.HTML = template.HTML(h.Noise)
146	}
147}
148
149func replaceimgsand(zap map[string]bool, absolute bool) func(node *html.Node) string {
150	return func(node *html.Node) string {
151		src := htfilter.GetAttr(node, "src")
152		alt := htfilter.GetAttr(node, "alt")
153		//title := GetAttr(node, "title")
154		if htfilter.HasClass(node, "Emoji") && alt != "" {
155			return alt
156		}
157		d := finddonk(src)
158		if d != nil {
159			zap[d.XID] = true
160			base := ""
161			if absolute {
162				base = "https://" + serverName
163			}
164			return string(templates.Sprintf(`<img alt="%s" title="%s" src="%s/d/%s">`, alt, alt, base, d.XID))
165		}
166		return string(templates.Sprintf(`&lt;img alt="%s" src="<a href="%s">%s<a>"&gt;`, alt, src, src))
167	}
168}
169
170func inlineimgsfor(honk *Honk) func(node *html.Node) string {
171	return func(node *html.Node) string {
172		src := htfilter.GetAttr(node, "src")
173		alt := htfilter.GetAttr(node, "alt")
174		d := savedonk(src, "image", alt, "image", true)
175		if d != nil {
176			honk.Donks = append(honk.Donks, d)
177		}
178		log.Printf("inline img with src: %s", src)
179		return ""
180	}
181}
182
183func imaginate(honk *Honk) {
184	var htf htfilter.Filter
185	htf.Imager = inlineimgsfor(honk)
186	htf.String(honk.Noise)
187}
188
189func translate(honk *Honk, redoimages bool) {
190	if honk.Format == "html" {
191		return
192	}
193	noise := honk.Noise
194	if strings.HasPrefix(noise, "DZ:") {
195		idx := strings.Index(noise, "\n")
196		if idx == -1 {
197			honk.Precis = noise
198			noise = ""
199		} else {
200			honk.Precis = noise[:idx]
201			noise = noise[idx+1:]
202		}
203	}
204	honk.Precis = markitzero(strings.TrimSpace(honk.Precis))
205
206	noise = strings.TrimSpace(noise)
207	noise = quickrename(noise, honk.UserID)
208	noise = markitzero(noise)
209	honk.Noise = noise
210	honk.Onts = oneofakind(ontologies(honk.Noise))
211
212	if redoimages {
213		zap := make(map[string]bool)
214		{
215			var htf htfilter.Filter
216			htf.Imager = replaceimgsand(zap, true)
217			htf.SpanClasses = allowedclasses
218			p, _ := htf.String(honk.Precis)
219			n, _ := htf.String(honk.Noise)
220			honk.Precis = string(p)
221			honk.Noise = string(n)
222		}
223		j := 0
224		for i := 0; i < len(honk.Donks); i++ {
225			if !zap[honk.Donks[i].XID] {
226				honk.Donks[j] = honk.Donks[i]
227				j++
228			}
229		}
230		honk.Donks = honk.Donks[:j]
231
232		honk.Noise = re_memes.ReplaceAllString(honk.Noise, "")
233		honk.Noise = ontologize(mentionize(honk.Noise))
234		honk.Noise = strings.Replace(honk.Noise, "<a href=", "<a class=\"mention u-url\" href=", -1)
235	}
236}
237
238func xcelerate(b []byte) string {
239	letters := "BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz1234567891234567891234"
240	for i, c := range b {
241		b[i] = letters[c&63]
242	}
243	s := string(b)
244	return s
245}
246
247func shortxid(xid string) string {
248	h := sha512.New512_256()
249	io.WriteString(h, xid)
250	return xcelerate(h.Sum(nil)[:20])
251}
252
253func xfiltrate() string {
254	var b [18]byte
255	rand.Read(b[:])
256	return xcelerate(b[:])
257}
258
259var re_hashes = regexp.MustCompile(`(?:^| )#[[:alnum:]]*[[:alpha:]][[:alnum:]_-]*`)
260
261func ontologies(s string) []string {
262	m := re_hashes.FindAllString(s, -1)
263	j := 0
264	for _, h := range m {
265		if h[0] == '&' {
266			continue
267		}
268		if h[0] != '#' {
269			h = h[1:]
270		}
271		m[j] = h
272		j++
273	}
274	return m[:j]
275}
276
277type Mention struct {
278	who   string
279	where string
280}
281
282var re_mentions = regexp.MustCompile(`@[[:alnum:]._-]+@[[:alnum:].-]*[[:alnum:]]`)
283var re_urltions = regexp.MustCompile(`@https://\S+`)
284
285func grapevine(s string) []string {
286	var mentions []string
287	m := re_mentions.FindAllString(s, -1)
288	for i := range m {
289		where := gofish(m[i])
290		if where != "" {
291			mentions = append(mentions, where)
292		}
293	}
294	m = re_urltions.FindAllString(s, -1)
295	for i := range m {
296		mentions = append(mentions, m[i][1:])
297	}
298	return mentions
299}
300
301func bunchofgrapes(s string) []Mention {
302	m := re_mentions.FindAllString(s, -1)
303	var mentions []Mention
304	for i := range m {
305		where := gofish(m[i])
306		if where != "" {
307			mentions = append(mentions, Mention{who: m[i], where: where})
308		}
309	}
310	m = re_urltions.FindAllString(s, -1)
311	for i := range m {
312		mentions = append(mentions, Mention{who: m[i][1:], where: m[i][1:]})
313	}
314	return mentions
315}
316
317type Emu struct {
318	ID   string
319	Name string
320}
321
322var re_emus = regexp.MustCompile(`:[[:alnum:]_-]+:`)
323
324func herdofemus(noise string) []Emu {
325	m := re_emus.FindAllString(noise, -1)
326	m = oneofakind(m)
327	var emus []Emu
328	for _, e := range m {
329		fname := e[1 : len(e)-1]
330		_, err := os.Stat("emus/" + fname + ".png")
331		if err != nil {
332			continue
333		}
334		url := fmt.Sprintf("https://%s/emu/%s.png", serverName, fname)
335		emus = append(emus, Emu{ID: url, Name: e})
336	}
337	return emus
338}
339
340var re_memes = regexp.MustCompile("meme: ?([[:alnum:]_.-]+)")
341
342func memetize(honk *Honk) {
343	repl := func(x string) string {
344		name := x[5:]
345		if name[0] == ' ' {
346			name = name[1:]
347		}
348		fd, err := os.Open("memes/" + name)
349		if err != nil {
350			log.Printf("no meme for %s", name)
351			return x
352		}
353		var peek [512]byte
354		n, _ := fd.Read(peek[:])
355		ct := http.DetectContentType(peek[:n])
356		fd.Close()
357
358		url := fmt.Sprintf("https://%s/meme/%s", serverName, name)
359		fileid, err := savefile("", name, name, url, ct, false, nil)
360		if err != nil {
361			log.Printf("error saving meme: %s", err)
362			return x
363		}
364		d := &Donk{
365			FileID: fileid,
366			XID:    "",
367			Name:   name,
368			Media:  ct,
369			URL:    url,
370			Local:  false,
371		}
372		honk.Donks = append(honk.Donks, d)
373		return ""
374	}
375	honk.Noise = re_memes.ReplaceAllStringFunc(honk.Noise, repl)
376}
377
378var re_quickmention = regexp.MustCompile("(^| )@[[:alnum:]]+( |$)")
379
380func quickrename(s string, userid int64) string {
381	nonstop := true
382	for nonstop {
383		nonstop = false
384		s = re_quickmention.ReplaceAllStringFunc(s, func(m string) string {
385			prefix := ""
386			if m[0] == ' ' {
387				prefix = " "
388				m = m[1:]
389			}
390			prefix += "@"
391			m = m[1:]
392			if m[len(m)-1] == ' ' {
393				m = m[:len(m)-1]
394			}
395
396			xid := fullname(m, userid)
397
398			if xid != "" {
399				_, name := handles(xid)
400				if name != "" {
401					nonstop = true
402					m = name
403				}
404			}
405			return prefix + m + " "
406		})
407	}
408	return s
409}
410
411var shortnames = cache.New(cache.Options{Filler: func(userid int64) (map[string]string, bool) {
412	honkers := gethonkers(userid)
413	m := make(map[string]string)
414	for _, h := range honkers {
415		m[h.XID] = h.Name
416	}
417	return m, true
418}, Invalidator: &honkerinvalidator})
419
420func shortname(userid int64, xid string) string {
421	var m map[string]string
422	ok := shortnames.Get(userid, &m)
423	if ok {
424		return m[xid]
425	}
426	return ""
427}
428
429var fullnames = cache.New(cache.Options{Filler: func(userid int64) (map[string]string, bool) {
430	honkers := gethonkers(userid)
431	m := make(map[string]string)
432	for _, h := range honkers {
433		m[h.Name] = h.XID
434	}
435	return m, true
436}, Invalidator: &honkerinvalidator})
437
438func fullname(name string, userid int64) string {
439	var m map[string]string
440	ok := fullnames.Get(userid, &m)
441	if ok {
442		return m[name]
443	}
444	return ""
445}
446
447func mentionize(s string) string {
448	s = re_mentions.ReplaceAllStringFunc(s, func(m string) string {
449		where := gofish(m)
450		if where == "" {
451			return m
452		}
453		who := m[0 : 1+strings.IndexByte(m[1:], '@')]
454		return fmt.Sprintf(`<span class="h-card"><a class="u-url mention" href="%s">%s</a></span>`,
455			html.EscapeString(where), html.EscapeString(who))
456	})
457	s = re_urltions.ReplaceAllStringFunc(s, func(m string) string {
458		return fmt.Sprintf(`<span class="h-card"><a class="u-url mention" href="%s">%s</a></span>`,
459			html.EscapeString(m[1:]), html.EscapeString(m))
460	})
461	return s
462}
463
464func ontologize(s string) string {
465	s = re_hashes.ReplaceAllStringFunc(s, func(o string) string {
466		if o[0] == '&' {
467			return o
468		}
469		p := ""
470		h := o
471		if h[0] != '#' {
472			p = h[:1]
473			h = h[1:]
474		}
475		return fmt.Sprintf(`%s<a href="https://%s/o/%s">%s</a>`, p, serverName,
476			strings.ToLower(h[1:]), h)
477	})
478	return s
479}
480
481var re_unurl = regexp.MustCompile("https://([^/]+).*/([^/]+)")
482var re_urlhost = regexp.MustCompile("https://([^/ ]+)")
483
484func originate(u string) string {
485	m := re_urlhost.FindStringSubmatch(u)
486	if len(m) > 1 {
487		return m[1]
488	}
489	return ""
490}
491
492var allhandles = cache.New(cache.Options{Filler: func(xid string) (string, bool) {
493	row := stmtGetXonker.QueryRow(xid, "handle")
494	var handle string
495	err := row.Scan(&handle)
496	if err != nil {
497		info, err := investigate(xid)
498		if err != nil {
499			m := re_unurl.FindStringSubmatch(xid)
500			if len(m) > 2 {
501				handle = m[2]
502			} else {
503				handle = xid
504			}
505		} else {
506			handle = info.Name
507			_, err = stmtSaveXonker.Exec(xid, handle, "handle")
508			if err != nil {
509				log.Printf("error saving handle: %s", err)
510			}
511		}
512	}
513	return handle, true
514}})
515
516// handle, handle@host
517func handles(xid string) (string, string) {
518	if xid == "" {
519		return "", ""
520	}
521	var handle string
522	allhandles.Get(xid, &handle)
523	if handle == xid {
524		return xid, xid
525	}
526	return handle, handle + "@" + originate(xid)
527}
528
529func prepend(s string, x []string) []string {
530	return append([]string{s}, x...)
531}
532
533func butnottooloud(aud []string) {
534	for i, a := range aud {
535		if strings.HasSuffix(a, "/followers") {
536			aud[i] = ""
537		}
538	}
539}
540
541func loudandproud(aud []string) bool {
542	for _, a := range aud {
543		if a == thewholeworld {
544			return true
545		}
546	}
547	return false
548}
549
550func firstclass(honk *Honk) bool {
551	return honk.Audience[0] == thewholeworld
552}
553
554func oneofakind(a []string) []string {
555	seen := make(map[string]bool)
556	seen[""] = true
557	j := 0
558	for _, s := range a {
559		if !seen[s] {
560			seen[s] = true
561			a[j] = s
562			j++
563		}
564	}
565	return a[:j]
566}
567
568var ziggies = cache.New(cache.Options{Filler: func(userid int64) (*KeyInfo, bool) {
569	var user *WhatAbout
570	ok := somenumberedusers.Get(userid, &user)
571	if !ok {
572		return nil, false
573	}
574	ki := new(KeyInfo)
575	ki.keyname = user.URL + "#key"
576	ki.seckey = user.SecKey
577	return ki, true
578}})
579
580func ziggy(userid int64) *KeyInfo {
581	var ki *KeyInfo
582	ziggies.Get(userid, &ki)
583	return ki
584}
585
586var zaggies = cache.New(cache.Options{Filler: func(keyname string) (*rsa.PublicKey, bool) {
587	row := stmtGetXonker.QueryRow(keyname, "pubkey")
588	var data string
589	err := row.Scan(&data)
590	if err == nil {
591		_, key, err := httpsig.DecodeKey(data)
592		if err != nil {
593			log.Printf("error decoding %s pubkey: %s", keyname, err)
594		}
595		return key, true
596	}
597	log.Printf("hitting the webs for missing pubkey: %s", keyname)
598	j, err := GetJunk(keyname)
599	if err != nil {
600		log.Printf("error getting %s pubkey: %s", keyname, err)
601		return nil, true
602	}
603	keyobj, ok := j.GetMap("publicKey")
604	if ok {
605		j = keyobj
606	}
607	data, ok = j.GetString("publicKeyPem")
608	if !ok {
609		log.Printf("error finding %s pubkey", keyname)
610		return nil, true
611	}
612	_, ok = j.GetString("owner")
613	if !ok {
614		log.Printf("error finding %s pubkey owner", keyname)
615		return nil, true
616	}
617	_, key, err := httpsig.DecodeKey(data)
618	if err != nil {
619		log.Printf("error decoding %s pubkey: %s", keyname, err)
620		return nil, true
621	}
622	_, err = stmtSaveXonker.Exec(keyname, data, "pubkey")
623	if err != nil {
624		log.Printf("error saving key: %s", err)
625	}
626	return key, true
627}})
628
629func zaggy(keyname string) *rsa.PublicKey {
630	var key *rsa.PublicKey
631	zaggies.Get(keyname, &key)
632	return key
633}
634
635func keymatch(keyname string, actor string) string {
636	hash := strings.IndexByte(keyname, '#')
637	if hash == -1 {
638		hash = len(keyname)
639	}
640	owner := keyname[0:hash]
641	if owner == actor {
642		return originate(actor)
643	}
644	return ""
645}