all repos — honk @ 1cc4b5c22f2da9673e07d811c33630df6802d111

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