all repos — honk @ a1f5e5b57cae0f06fad03d6a18ebf94a24912897

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"
 23	"html/template"
 24	"log"
 25	"net/http"
 26	"os"
 27	"regexp"
 28	"strings"
 29	"sync"
 30
 31	"humungus.tedunangst.com/r/webs/htfilter"
 32)
 33
 34func reverbolate(userid int64, honks []*Honk) {
 35	filt := htfilter.New()
 36	zilences := getzilences(userid)
 37	for _, h := range honks {
 38		h.What += "ed"
 39		if h.What == "tonked" {
 40			h.What = "honked back"
 41			h.Style = "subtle"
 42		}
 43		if !h.Public {
 44			h.Style = "limited"
 45		}
 46		if h.Whofore == 2 || h.Whofore == 3 {
 47			h.URL = h.XID
 48			h.Noise = mentionize(h.Noise)
 49			h.Username, h.Handle = honkerhandle(h.Honker)
 50		} else {
 51			_, h.Handle = honkerhandle(h.Honker)
 52			h.Username = h.Handle
 53			if len(h.Username) > 20 {
 54				h.Username = h.Username[:20] + ".."
 55			}
 56			if h.URL == "" {
 57				h.URL = h.XID
 58			}
 59		}
 60		if h.Oonker != "" {
 61			_, h.Oondle = honkerhandle(h.Oonker)
 62		}
 63		zap := make(map[*Donk]bool)
 64		h.Noise = unpucker(h.Noise)
 65		h.Open = "open"
 66		if badword := unsee(zilences, h.Precis, h.Noise); badword != "" {
 67			if h.Precis == "" {
 68				h.Precis = badword
 69			}
 70			h.Open = ""
 71		}
 72		h.HTML, _ = filt.String(h.Noise)
 73		emuxifier := func(e string) string {
 74			for _, d := range h.Donks {
 75				if d.Name == e {
 76					zap[d] = true
 77					if d.Local {
 78						return fmt.Sprintf(`<img class="emu" title="%s" src="/d/%s">`, d.Name, d.XID)
 79					}
 80				}
 81			}
 82			return e
 83		}
 84		h.HTML = template.HTML(re_emus.ReplaceAllStringFunc(string(h.HTML), emuxifier))
 85		j := 0
 86		for i := 0; i < len(h.Donks); i++ {
 87			if !zap[h.Donks[i]] {
 88				h.Donks[j] = h.Donks[i]
 89				j++
 90			}
 91		}
 92		h.Donks = h.Donks[:j]
 93	}
 94}
 95
 96func unsee(zilences []*regexp.Regexp, precis string, noise string) string {
 97	for _, z := range zilences {
 98		if z.MatchString(precis) || z.MatchString(noise) {
 99			if precis == "" {
100				w := z.String()
101				return w[6 : len(w)-3]
102			}
103			return precis
104		}
105	}
106	return ""
107}
108
109func osmosis(honks []*Honk, userid int64) []*Honk {
110	zords := getzords(userid)
111	for i, j := 0, len(honks)-1; i < j; i, j = i+1, j-1 {
112		honks[i], honks[j] = honks[j], honks[i]
113	}
114
115	j := 0
116	filtered := make(map[string]bool)
117outer:
118	for _, h := range honks {
119		if h.RID != "" && filtered[h.RID] {
120			filtered[h.XID] = true
121			continue outer
122		}
123
124		for _, z := range zords {
125			if z.MatchString(h.Precis) || z.MatchString(h.Noise) {
126				filtered[h.XID] = true
127				continue outer
128			}
129		}
130		honks[j] = h
131		j++
132	}
133	honks = honks[0:j]
134	for i, j := 0, len(honks)-1; i < j; i, j = i+1, j-1 {
135		honks[i], honks[j] = honks[j], honks[i]
136	}
137	return honks
138}
139
140func shortxid(xid string) string {
141	idx := strings.LastIndexByte(xid, '/')
142	if idx == -1 {
143		return xid
144	}
145	return xid[idx+1:]
146}
147
148func xfiltrate() string {
149	letters := "BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz1234567891234567891234"
150	var b [18]byte
151	rand.Read(b[:])
152	for i, c := range b {
153		b[i] = letters[c&63]
154	}
155	s := string(b[:])
156	return s
157}
158
159var re_hashes = regexp.MustCompile(`(?:^|\W)#[[:alnum:]]+`)
160
161func ontologies(s string) []string {
162	m := re_hashes.FindAllString(s, -1)
163	j := 0
164	for _, h := range m {
165		if h[0] == '&' {
166			continue
167		}
168		if h[0] != '#' {
169			h = h[1:]
170		}
171		m[j] = h
172		j++
173	}
174	return m[:j]
175}
176
177type Mention struct {
178	who   string
179	where string
180}
181
182var re_mentions = regexp.MustCompile(`@[[:alnum:]._-]+@[[:alnum:].-]*[[:alnum:]]`)
183var re_urltions = regexp.MustCompile(`@https://\S+`)
184
185func grapevine(s string) []string {
186	var mentions []string
187	m := re_mentions.FindAllString(s, -1)
188	for i := range m {
189		where := gofish(m[i])
190		if where != "" {
191			mentions = append(mentions, where)
192		}
193	}
194	m = re_urltions.FindAllString(s, -1)
195	for i := range m {
196		mentions = append(mentions, m[i][1:])
197	}
198	return mentions
199}
200
201func bunchofgrapes(s string) []Mention {
202	m := re_mentions.FindAllString(s, -1)
203	var mentions []Mention
204	for i := range m {
205		where := gofish(m[i])
206		if where != "" {
207			mentions = append(mentions, Mention{who: m[i], where: where})
208		}
209	}
210	m = re_urltions.FindAllString(s, -1)
211	for i := range m {
212		mentions = append(mentions, Mention{who: m[i][1:], where: m[i][1:]})
213	}
214	return mentions
215}
216
217type Emu struct {
218	ID   string
219	Name string
220}
221
222var re_link = regexp.MustCompile(`@?https?://[^\s"]+[\w/)]`)
223var re_emus = regexp.MustCompile(`:[[:alnum:]_-]+:`)
224
225func herdofemus(noise string) []Emu {
226	m := re_emus.FindAllString(noise, -1)
227	m = oneofakind(m)
228	var emus []Emu
229	for _, e := range m {
230		fname := e[1 : len(e)-1]
231		_, err := os.Stat("emus/" + fname + ".png")
232		if err != nil {
233			continue
234		}
235		url := fmt.Sprintf("https://%s/emu/%s.png", serverName, fname)
236		emus = append(emus, Emu{ID: url, Name: e})
237	}
238	return emus
239}
240
241var re_memes = regexp.MustCompile("meme: ?([[:alnum:]_.-]+)")
242
243func memetize(honk *Honk) {
244	repl := func(x string) string {
245		name := x[5:]
246		if name[0] == ' ' {
247			name = name[1:]
248		}
249		fd, err := os.Open("memes/" + name)
250		if err != nil {
251			log.Printf("no meme for %s", name)
252			return x
253		}
254		var peek [512]byte
255		n, _ := fd.Read(peek[:])
256		ct := http.DetectContentType(peek[:n])
257		fd.Close()
258
259		url := fmt.Sprintf("https://%s/meme/%s", serverName, name)
260		res, err := stmtSaveFile.Exec("", name, url, ct, 0, "")
261		if err != nil {
262			log.Printf("error saving meme: %s", err)
263			return x
264		}
265		var d Donk
266		d.FileID, _ = res.LastInsertId()
267		d.XID = ""
268		d.Name = name
269		d.Media = ct
270		d.URL = url
271		d.Local = false
272		honk.Donks = append(honk.Donks, &d)
273		log.Printf("replace with -")
274		return ""
275	}
276	honk.Noise = re_memes.ReplaceAllStringFunc(honk.Noise, repl)
277}
278
279var re_bolder = regexp.MustCompile(`(^|\W)\*\*([\w\s,.!?'-]+)\*\*($|\W)`)
280var re_italicer = regexp.MustCompile(`(^|\W)\*([\w\s,.!?'-]+)\*($|\W)`)
281var re_bigcoder = regexp.MustCompile("```\n?((?s:.*?))\n?```\n?")
282var re_coder = regexp.MustCompile("`([^`]*)`")
283var re_quoter = regexp.MustCompile(`(?m:^&gt; (.*)\n?)`)
284
285func markitzero(s string) string {
286	var bigcodes []string
287	bigsaver := func(code string) string {
288		bigcodes = append(bigcodes, code)
289		return "``````"
290	}
291	s = re_bigcoder.ReplaceAllStringFunc(s, bigsaver)
292	var lilcodes []string
293	lilsaver := func(code string) string {
294		lilcodes = append(lilcodes, code)
295		return "`x`"
296	}
297	s = re_coder.ReplaceAllStringFunc(s, lilsaver)
298	s = re_bolder.ReplaceAllString(s, "$1<b>$2</b>$3")
299	s = re_italicer.ReplaceAllString(s, "$1<i>$2</i>$3")
300	s = re_quoter.ReplaceAllString(s, "<blockquote>$1</blockquote><p>")
301	lilun := func(s string) string {
302		code := lilcodes[0]
303		lilcodes = lilcodes[1:]
304		return code
305	}
306	s = re_coder.ReplaceAllStringFunc(s, lilun)
307	bigun := func(s string) string {
308		code := bigcodes[0]
309		bigcodes = bigcodes[1:]
310		return code
311	}
312	s = re_bigcoder.ReplaceAllStringFunc(s, bigun)
313	s = re_bigcoder.ReplaceAllString(s, "<pre><code>$1</code></pre><p>")
314	s = re_coder.ReplaceAllString(s, "<code>$1</code>")
315	return s
316}
317
318func obfusbreak(s string) string {
319	s = strings.TrimSpace(s)
320	s = strings.Replace(s, "\r", "", -1)
321	s = html.EscapeString(s)
322	// dammit go
323	s = strings.Replace(s, "&#39;", "'", -1)
324	linkfn := func(url string) string {
325		if url[0] == '@' {
326			return url
327		}
328		addparen := false
329		adddot := false
330		if strings.HasSuffix(url, ")") && strings.IndexByte(url, '(') == -1 {
331			url = url[:len(url)-1]
332			addparen = true
333		}
334		if strings.HasSuffix(url, ".") {
335			url = url[:len(url)-1]
336			adddot = true
337		}
338		url = fmt.Sprintf(`<a class="mention" href="%s">%s</a>`, url, url)
339		if adddot {
340			url += "."
341		}
342		if addparen {
343			url += ")"
344		}
345		return url
346	}
347	s = re_link.ReplaceAllStringFunc(s, linkfn)
348
349	s = markitzero(s)
350
351	s = strings.Replace(s, "\n", "<br>", -1)
352	return s
353}
354
355func mentionize(s string) string {
356	s = re_mentions.ReplaceAllStringFunc(s, func(m string) string {
357		where := gofish(m)
358		if where == "" {
359			return m
360		}
361		who := m[0 : 1+strings.IndexByte(m[1:], '@')]
362		return fmt.Sprintf(`<span class="h-card"><a class="u-url mention" href="%s">%s</a></span>`,
363			html.EscapeString(where), html.EscapeString(who))
364	})
365	s = re_urltions.ReplaceAllStringFunc(s, func(m string) string {
366		return fmt.Sprintf(`<span class="h-card"><a class="u-url mention" href="%s">%s</a></span>`,
367			html.EscapeString(m[1:]), html.EscapeString(m))
368	})
369	return s
370}
371
372var re_unurl = regexp.MustCompile("https://([^/]+).*/([^/]+)")
373var re_urlhost = regexp.MustCompile("https://([^/]+)")
374
375func originate(u string) string {
376	m := re_urlhost.FindStringSubmatch(u)
377	if len(m) > 1 {
378		return m[1]
379	}
380	return ""
381}
382
383func honkerhandle(h string) (string, string) {
384	m := re_unurl.FindStringSubmatch(h)
385	if len(m) > 2 {
386		return m[2], fmt.Sprintf("%s@%s", m[2], m[1])
387	}
388	return h, h
389}
390
391func prepend(s string, x []string) []string {
392	return append([]string{s}, x...)
393}
394
395// pleroma leaks followers addressed posts to followers
396func butnottooloud(aud []string) {
397	for i, a := range aud {
398		if strings.HasSuffix(a, "/followers") {
399			aud[i] = ""
400		}
401	}
402}
403
404func keepitquiet(aud []string) bool {
405	for _, a := range aud {
406		if a == thewholeworld {
407			return false
408		}
409	}
410	return true
411}
412
413func oneofakind(a []string) []string {
414	var x []string
415	for n, s := range a {
416		if s != "" {
417			x = append(x, s)
418			for i := n + 1; i < len(a); i++ {
419				if a[i] == s {
420					a[i] = ""
421				}
422			}
423		}
424	}
425	return x
426}
427
428var ziggies = make(map[string]*rsa.PrivateKey)
429var zaggies = make(map[string]*rsa.PublicKey)
430var ziggylock sync.Mutex
431
432func ziggy(username string) (keyname string, key *rsa.PrivateKey) {
433	ziggylock.Lock()
434	key = ziggies[username]
435	ziggylock.Unlock()
436	if key == nil {
437		db := opendatabase()
438		row := db.QueryRow("select seckey from users where username = ?", username)
439		var data string
440		row.Scan(&data)
441		var err error
442		key, _, err = pez(data)
443		if err != nil {
444			log.Printf("error decoding %s seckey: %s", username, err)
445			return
446		}
447		ziggylock.Lock()
448		ziggies[username] = key
449		ziggylock.Unlock()
450	}
451	keyname = fmt.Sprintf("https://%s/u/%s#key", serverName, username)
452	return
453}
454
455func zaggy(keyname string) (key *rsa.PublicKey) {
456	ziggylock.Lock()
457	key = zaggies[keyname]
458	ziggylock.Unlock()
459	if key != nil {
460		return
461	}
462	row := stmtGetXonker.QueryRow(keyname, "pubkey")
463	var data string
464	err := row.Scan(&data)
465	if err != nil {
466		log.Printf("hitting the webs for missing pubkey: %s", keyname)
467		j, err := GetJunk(keyname)
468		if err != nil {
469			log.Printf("error getting %s pubkey: %s", keyname, err)
470			return
471		}
472		var ok bool
473		data, ok = j.FindString([]string{"publicKey", "publicKeyPem"})
474		if !ok {
475			log.Printf("error finding %s pubkey", keyname)
476			return
477		}
478		_, ok = j.FindString([]string{"publicKey", "owner"})
479		if !ok {
480			log.Printf("error finding %s pubkey owner", keyname)
481			return
482		}
483		_, key, err = pez(data)
484		if err != nil {
485			log.Printf("error decoding %s pubkey: %s", keyname, err)
486			return
487		}
488		_, err = stmtSaveXonker.Exec(keyname, data, "pubkey")
489		if err != nil {
490			log.Printf("error saving key: %s", err)
491		}
492	} else {
493		_, key, err = pez(data)
494		if err != nil {
495			log.Printf("error decoding %s pubkey: %s", keyname, err)
496			return
497		}
498	}
499	ziggylock.Lock()
500	zaggies[keyname] = key
501	ziggylock.Unlock()
502	return
503}
504
505func makeitworksomehowwithoutregardforkeycontinuity(keyname string, r *http.Request, payload []byte) (string, error) {
506	_, err := stmtDeleteXonker.Exec(keyname, "pubkey")
507	if err != nil {
508		log.Printf("error deleting key: %s", err)
509	}
510	ziggylock.Lock()
511	delete(zaggies, keyname)
512	ziggylock.Unlock()
513	return zag(r, payload)
514}
515
516var thumbbiters map[int64]map[string]bool
517var zordses map[int64][]*regexp.Regexp
518var zilences map[int64][]*regexp.Regexp
519var thumblock sync.Mutex
520
521func bitethethumbs() {
522	rows, err := stmtThumbBiters.Query()
523	if err != nil {
524		log.Printf("error getting thumbbiters: %s", err)
525		return
526	}
527	defer rows.Close()
528
529	thumblock.Lock()
530	defer thumblock.Unlock()
531	thumbbiters = make(map[int64]map[string]bool)
532	zordses = make(map[int64][]*regexp.Regexp)
533	zilences = make(map[int64][]*regexp.Regexp)
534	for rows.Next() {
535		var userid int64
536		var name, wherefore string
537		err = rows.Scan(&userid, &name, &wherefore)
538		if err != nil {
539			log.Printf("error scanning zonker: %s", err)
540			continue
541		}
542		if wherefore == "zord" || wherefore == "zilence" {
543			zord := "\\b(?i:" + name + ")\\b"
544			re, err := regexp.Compile(zord)
545			if err != nil {
546				log.Printf("error compiling zord: %s", err)
547			} else {
548				if wherefore == "zord" {
549					zordses[userid] = append(zordses[userid], re)
550				} else {
551					zilences[userid] = append(zilences[userid], re)
552				}
553			}
554			continue
555		}
556		m := thumbbiters[userid]
557		if m == nil {
558			m = make(map[string]bool)
559			thumbbiters[userid] = m
560		}
561		m[name] = true
562	}
563}
564
565func getzords(userid int64) []*regexp.Regexp {
566	thumblock.Lock()
567	defer thumblock.Unlock()
568	return zordses[userid]
569}
570
571func getzilences(userid int64) []*regexp.Regexp {
572	thumblock.Lock()
573	defer thumblock.Unlock()
574	return zilences[userid]
575}
576
577func thoudostbitethythumb(userid int64, who []string, objid string) bool {
578	thumblock.Lock()
579	biters := thumbbiters[userid]
580	thumblock.Unlock()
581	for _, w := range who {
582		if biters[w] {
583			return true
584		}
585		where := originate(w)
586		if where != "" {
587			if biters[where] {
588				return true
589			}
590		}
591	}
592	return false
593}
594
595func keymatch(keyname string, actor string) string {
596	hash := strings.IndexByte(keyname, '#')
597	if hash == -1 {
598		hash = len(keyname)
599	}
600	owner := keyname[0:hash]
601	if owner == actor {
602		return originate(actor)
603	}
604	return ""
605}