all repos — honk @ a96588b4643747fab14389ce3c8b13ed31f590fe

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