all repos — honk @ d4f7fc985e96104824190a499715772482508d47

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