all repos — honk @ f7bd1d575517bc3e6c312d5a1f112323c2207673

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