all repos — honk @ 8dfa16ccc19e03a5ed5980d993909f69cf359d19

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