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/sha512"
21 "fmt"
22 "html/template"
23 "io"
24 "log"
25 "net/http"
26 "net/url"
27 "os"
28 "regexp"
29 "strings"
30 "time"
31
32 "golang.org/x/net/html"
33 "humungus.tedunangst.com/r/webs/cache"
34 "humungus.tedunangst.com/r/webs/htfilter"
35 "humungus.tedunangst.com/r/webs/httpsig"
36 "humungus.tedunangst.com/r/webs/mz"
37 "humungus.tedunangst.com/r/webs/templates"
38)
39
40var allowedclasses = make(map[string]bool)
41
42func init() {
43 allowedclasses["kw"] = true
44 allowedclasses["bi"] = true
45 allowedclasses["st"] = true
46 allowedclasses["nm"] = true
47 allowedclasses["tp"] = true
48 allowedclasses["op"] = true
49 allowedclasses["cm"] = true
50 allowedclasses["al"] = true
51 allowedclasses["dl"] = true
52}
53
54func reverbolate(userid int64, honks []*Honk) {
55 var user *WhatAbout
56 somenumberedusers.Get(userid, &user)
57 for _, h := range honks {
58 h.What += "ed"
59 if h.What == "tonked" {
60 h.What = "honked back"
61 h.Style += " subtle"
62 }
63 if !h.Public {
64 h.Style += " limited"
65 }
66 translate(h)
67 local := false
68 if h.Whofore == 2 || h.Whofore == 3 {
69 local = true
70 }
71 if local && h.What != "bonked" {
72 h.Noise = re_memes.ReplaceAllString(h.Noise, "")
73 }
74 h.Username, h.Handle = handles(h.Honker)
75 if !local {
76 short := shortname(userid, h.Honker)
77 if short != "" {
78 h.Username = short
79 } else {
80 h.Username = h.Handle
81 if len(h.Username) > 20 {
82 h.Username = h.Username[:20] + ".."
83 }
84 }
85 }
86 if user.Options.MentionAll {
87 hset := []string{"@" + h.Handle}
88 for _, a := range h.Audience {
89 if a == h.Honker || a == user.URL {
90 continue
91 }
92 _, hand := handles(a)
93 if hand != "" {
94 hand = "@" + hand
95 hset = append(hset, hand)
96 }
97 }
98 h.Handles = strings.Join(hset, " ")
99 } else if h.Honker != user.URL {
100 h.Handles = "@" + h.Handle
101 }
102 if h.URL == "" {
103 h.URL = h.XID
104 }
105 if h.Oonker != "" {
106 _, h.Oondle = handles(h.Oonker)
107 }
108 h.Precis = demoji(h.Precis)
109 h.Noise = demoji(h.Noise)
110 h.Open = "open"
111
112 zap := make(map[string]bool)
113 {
114 var htf htfilter.Filter
115 htf.Imager = replaceimgsand(zap, false)
116 htf.SpanClasses = allowedclasses
117 htf.BaseURL, _ = url.Parse(h.XID)
118 emuxifier := func(e string) string {
119 for _, d := range h.Donks {
120 if d.Name == e {
121 zap[d.XID] = true
122 if d.Local {
123 return fmt.Sprintf(`<img class="emu" title="%s" src="/d/%s">`, d.Name, d.XID)
124 }
125 }
126 }
127 if local && h.What != "bonked" {
128 var emu Emu
129 emucache.Get(e, &emu)
130 if emu.ID != "" {
131 return fmt.Sprintf(`<img class="emu" title="%s" src="%s">`, emu.Name, emu.ID)
132 }
133 }
134 return e
135 }
136 htf.FilterText = func(w io.Writer, data string) {
137 data = htfilter.EscapeText(data)
138 data = re_emus.ReplaceAllStringFunc(data, emuxifier)
139 io.WriteString(w, data)
140 }
141 p, _ := htf.String(h.Precis)
142 n, _ := htf.String(h.Noise)
143 h.Precis = string(p)
144 h.Noise = string(n)
145 }
146 j := 0
147 for i := 0; i < len(h.Donks); i++ {
148 if !zap[h.Donks[i].XID] {
149 h.Donks[j] = h.Donks[i]
150 j++
151 }
152 }
153 h.Donks = h.Donks[:j]
154 }
155
156 unsee(honks, userid)
157
158 for _, h := range honks {
159 renderflags(h)
160
161 h.HTPrecis = template.HTML(h.Precis)
162 h.HTML = template.HTML(h.Noise)
163 }
164}
165
166func replaceimgsand(zap map[string]bool, absolute bool) func(node *html.Node) string {
167 return func(node *html.Node) string {
168 src := htfilter.GetAttr(node, "src")
169 alt := htfilter.GetAttr(node, "alt")
170 //title := GetAttr(node, "title")
171 if htfilter.HasClass(node, "Emoji") && alt != "" {
172 return alt
173 }
174 d := finddonk(src)
175 if d != nil {
176 zap[d.XID] = true
177 base := ""
178 if absolute {
179 base = "https://" + serverName
180 }
181 return string(templates.Sprintf(`<img alt="%s" title="%s" src="%s/d/%s">`, alt, alt, base, d.XID))
182 }
183 return string(templates.Sprintf(`<img alt="%s" src="<a href="%s">%s</a>">`, alt, src, src))
184 }
185}
186
187func translatechonk(ch *Chonk) {
188 noise := ch.Noise
189 if ch.Format == "markdown" {
190 noise = markitzero(noise)
191 }
192 var htf htfilter.Filter
193 htf.SpanClasses = allowedclasses
194 htf.BaseURL, _ = url.Parse(ch.XID)
195 ch.HTML, _ = htf.String(noise)
196}
197
198func filterchonk(ch *Chonk) {
199 translatechonk(ch)
200
201 noise := string(ch.HTML)
202
203 local := originate(ch.XID) == serverName
204
205 zap := make(map[string]bool)
206 emuxifier := func(e string) string {
207 for _, d := range ch.Donks {
208 if d.Name == e {
209 zap[d.XID] = true
210 if d.Local {
211 return fmt.Sprintf(`<img class="emu" title="%s" src="/d/%s">`, d.Name, d.XID)
212 }
213 }
214 }
215 if local {
216 var emu Emu
217 emucache.Get(e, &emu)
218 if emu.ID != "" {
219 return fmt.Sprintf(`<img class="emu" title="%s" src="%s">`, emu.Name, emu.ID)
220 }
221 }
222 return e
223 }
224 noise = re_emus.ReplaceAllStringFunc(noise, emuxifier)
225 j := 0
226 for i := 0; i < len(ch.Donks); i++ {
227 if !zap[ch.Donks[i].XID] {
228 ch.Donks[j] = ch.Donks[i]
229 j++
230 }
231 }
232 ch.Donks = ch.Donks[:j]
233
234 if strings.HasPrefix(noise, "<p>") {
235 noise = noise[3:]
236 }
237 ch.HTML = template.HTML(noise)
238 if short := shortname(ch.UserID, ch.Who); short != "" {
239 ch.Handle = short
240 } else {
241 ch.Handle, _ = handles(ch.Who)
242 }
243
244}
245
246func inlineimgsfor(honk *Honk) func(node *html.Node) string {
247 return func(node *html.Node) string {
248 src := htfilter.GetAttr(node, "src")
249 alt := htfilter.GetAttr(node, "alt")
250 d := savedonk(src, "image", alt, "image", true)
251 if d != nil {
252 honk.Donks = append(honk.Donks, d)
253 }
254 log.Printf("inline img with src: %s", src)
255 return ""
256 }
257}
258
259func imaginate(honk *Honk) {
260 var htf htfilter.Filter
261 htf.Imager = inlineimgsfor(honk)
262 htf.BaseURL, _ = url.Parse(honk.XID)
263 htf.String(honk.Noise)
264}
265
266func translate(honk *Honk) {
267 if honk.Format == "html" {
268 return
269 }
270 noise := honk.Noise
271 if strings.HasPrefix(noise, "DZ:") {
272 idx := strings.Index(noise, "\n")
273 if idx == -1 {
274 honk.Precis = noise
275 noise = ""
276 } else {
277 honk.Precis = noise[:idx]
278 noise = noise[idx+1:]
279 }
280 }
281 honk.Precis = markitzero(strings.TrimSpace(honk.Precis))
282
283 var marker mz.Marker
284 marker.HashLinker = ontoreplacer
285 marker.AtLinker = attoreplacer
286 noise = strings.TrimSpace(noise)
287 noise = marker.Mark(noise)
288 honk.Noise = noise
289 honk.Onts = oneofakind(marker.HashTags)
290 honk.Mentions = bunchofgrapes(marker.Mentions)
291}
292
293func redoimages(honk *Honk) {
294 zap := make(map[string]bool)
295 {
296 var htf htfilter.Filter
297 htf.Imager = replaceimgsand(zap, true)
298 htf.SpanClasses = allowedclasses
299 p, _ := htf.String(honk.Precis)
300 n, _ := htf.String(honk.Noise)
301 honk.Precis = string(p)
302 honk.Noise = string(n)
303 }
304 j := 0
305 for i := 0; i < len(honk.Donks); i++ {
306 if !zap[honk.Donks[i].XID] {
307 honk.Donks[j] = honk.Donks[i]
308 j++
309 }
310 }
311 honk.Donks = honk.Donks[:j]
312
313 honk.Noise = re_memes.ReplaceAllString(honk.Noise, "")
314 honk.Noise = strings.Replace(honk.Noise, "<a href=", "<a class=\"mention u-url\" href=", -1)
315}
316
317func xcelerate(b []byte) string {
318 letters := "BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz1234567891234567891234"
319 for i, c := range b {
320 b[i] = letters[c&63]
321 }
322 s := string(b)
323 return s
324}
325
326func shortxid(xid string) string {
327 h := sha512.New512_256()
328 io.WriteString(h, xid)
329 return xcelerate(h.Sum(nil)[:20])
330}
331
332func xfiltrate() string {
333 var b [18]byte
334 rand.Read(b[:])
335 return xcelerate(b[:])
336}
337
338func grapevine(mentions []Mention) []string {
339 var s []string
340 for _, m := range mentions {
341 s = append(s, m.Where)
342 }
343 return s
344}
345
346func bunchofgrapes(m []string) []Mention {
347 var mentions []Mention
348 for i := range m {
349 where := gofish(m[i])
350 if where != "" {
351 mentions = append(mentions, Mention{Who: m[i], Where: where})
352 }
353 }
354 return mentions
355}
356
357type Emu struct {
358 ID string
359 Name string
360}
361
362var re_emus = regexp.MustCompile(`:[[:alnum:]_-]+:`)
363
364var emucache = cache.New(cache.Options{Filler: func(ename string) (Emu, bool) {
365 fname := ename[1 : len(ename)-1]
366 _, err := os.Stat(dataDir + "/emus/" + fname + ".png")
367 if err != nil {
368 return Emu{Name: ename, ID: ""}, true
369 }
370 url := fmt.Sprintf("https://%s/emu/%s.png", serverName, fname)
371 return Emu{ID: url, Name: ename}, true
372}, Duration: 10 * time.Second})
373
374func herdofemus(noise string) []Emu {
375 m := re_emus.FindAllString(noise, -1)
376 m = oneofakind(m)
377 var emus []Emu
378 for _, e := range m {
379 var emu Emu
380 emucache.Get(e, &emu)
381 if emu.ID == "" {
382 continue
383 }
384 emus = append(emus, emu)
385 }
386 return emus
387}
388
389var re_memes = regexp.MustCompile("meme: ?([^\n]+)")
390var re_avatar = regexp.MustCompile("avatar: ?([^\n]+)")
391
392func memetize(honk *Honk) {
393 repl := func(x string) string {
394 name := x[5:]
395 if name[0] == ' ' {
396 name = name[1:]
397 }
398 fd, err := os.Open("memes/" + name)
399 if err != nil {
400 log.Printf("no meme for %s", name)
401 return x
402 }
403 var peek [512]byte
404 n, _ := fd.Read(peek[:])
405 ct := http.DetectContentType(peek[:n])
406 fd.Close()
407
408 url := fmt.Sprintf("https://%s/meme/%s", serverName, name)
409 fileid, err := savefile("", name, name, url, ct, false, nil)
410 if err != nil {
411 log.Printf("error saving meme: %s", err)
412 return x
413 }
414 d := &Donk{
415 FileID: fileid,
416 XID: "",
417 Name: name,
418 Media: ct,
419 URL: url,
420 Local: false,
421 }
422 honk.Donks = append(honk.Donks, d)
423 return ""
424 }
425 honk.Noise = re_memes.ReplaceAllStringFunc(honk.Noise, repl)
426}
427
428var re_quickmention = regexp.MustCompile("(^|[ \n])@[[:alnum:]]+([ \n.]|$)")
429
430func quickrename(s string, userid int64) string {
431 nonstop := true
432 for nonstop {
433 nonstop = false
434 s = re_quickmention.ReplaceAllStringFunc(s, func(m string) string {
435 prefix := ""
436 if m[0] == ' ' || m[0] == '\n' {
437 prefix = m[:1]
438 m = m[1:]
439 }
440 prefix += "@"
441 m = m[1:]
442 tail := ""
443 if last := m[len(m)-1]; last == ' ' || last == '\n' || last == '.' {
444 tail = m[len(m)-1:]
445 m = m[:len(m)-1]
446 }
447
448 xid := fullname(m, userid)
449
450 if xid != "" {
451 _, name := handles(xid)
452 if name != "" {
453 nonstop = true
454 m = name
455 }
456 }
457 return prefix + m + tail
458 })
459 }
460 return s
461}
462
463var shortnames = cache.New(cache.Options{Filler: func(userid int64) (map[string]string, bool) {
464 honkers := gethonkers(userid)
465 m := make(map[string]string)
466 for _, h := range honkers {
467 m[h.XID] = h.Name
468 }
469 return m, true
470}, Invalidator: &honkerinvalidator})
471
472func shortname(userid int64, xid string) string {
473 var m map[string]string
474 ok := shortnames.Get(userid, &m)
475 if ok {
476 return m[xid]
477 }
478 return ""
479}
480
481var fullnames = cache.New(cache.Options{Filler: func(userid int64) (map[string]string, bool) {
482 honkers := gethonkers(userid)
483 m := make(map[string]string)
484 for _, h := range honkers {
485 m[h.Name] = h.XID
486 }
487 return m, true
488}, Invalidator: &honkerinvalidator})
489
490func fullname(name string, userid int64) string {
491 var m map[string]string
492 ok := fullnames.Get(userid, &m)
493 if ok {
494 return m[name]
495 }
496 return ""
497}
498
499func attoreplacer(m string) string {
500 fill := `<span class="h-card"><a class="u-url mention" href="%s">%s</a></span>`
501 where := gofish(m)
502 if where == "" {
503 return m
504 }
505 who := m[0 : 1+strings.IndexByte(m[1:], '@')]
506 return fmt.Sprintf(fill, html.EscapeString(where), html.EscapeString(who))
507}
508
509func ontoreplacer(h string) string {
510 return fmt.Sprintf(`<a href="https://%s/o/%s">%s</a>`, serverName,
511 strings.ToLower(h[1:]), h)
512}
513
514var re_unurl = regexp.MustCompile("https://([^/]+).*/([^/]+)")
515var re_urlhost = regexp.MustCompile("https://([^/ #)]+)")
516
517func originate(u string) string {
518 m := re_urlhost.FindStringSubmatch(u)
519 if len(m) > 1 {
520 return m[1]
521 }
522 return ""
523}
524
525var allhandles = cache.New(cache.Options{Filler: func(xid string) (string, bool) {
526 var handle string
527 row := stmtGetXonker.QueryRow(xid, "handle")
528 err := row.Scan(&handle)
529 if err != nil {
530 log.Printf("need to get a handle: %s", xid)
531 info, err := investigate(xid)
532 if err != nil {
533 m := re_unurl.FindStringSubmatch(xid)
534 if len(m) > 2 {
535 handle = m[2]
536 } else {
537 handle = xid
538 }
539 } else {
540 handle = info.Name
541 }
542 }
543 return handle, true
544}})
545
546// handle, handle@host
547func handles(xid string) (string, string) {
548 if xid == "" || xid == thewholeworld || strings.HasSuffix(xid, "/followers") {
549 return "", ""
550 }
551 var handle string
552 allhandles.Get(xid, &handle)
553 if handle == xid {
554 return xid, xid
555 }
556 return handle, handle + "@" + originate(xid)
557}
558
559func butnottooloud(aud []string) {
560 for i, a := range aud {
561 if strings.HasSuffix(a, "/followers") {
562 aud[i] = ""
563 }
564 }
565}
566
567func loudandproud(aud []string) bool {
568 for _, a := range aud {
569 if a == thewholeworld {
570 return true
571 }
572 }
573 return false
574}
575
576func firstclass(honk *Honk) bool {
577 return honk.Audience[0] == thewholeworld
578}
579
580func oneofakind(a []string) []string {
581 seen := make(map[string]bool)
582 seen[""] = true
583 j := 0
584 for _, s := range a {
585 if !seen[s] {
586 seen[s] = true
587 a[j] = s
588 j++
589 }
590 }
591 return a[:j]
592}
593
594var ziggies = cache.New(cache.Options{Filler: func(userid int64) (*KeyInfo, bool) {
595 var user *WhatAbout
596 ok := somenumberedusers.Get(userid, &user)
597 if !ok {
598 return nil, false
599 }
600 ki := new(KeyInfo)
601 ki.keyname = user.URL + "#key"
602 ki.seckey = user.SecKey
603 return ki, true
604}})
605
606func ziggy(userid int64) *KeyInfo {
607 var ki *KeyInfo
608 ziggies.Get(userid, &ki)
609 return ki
610}
611
612var zaggies = cache.New(cache.Options{Filler: func(keyname string) (httpsig.PublicKey, bool) {
613 var data string
614 row := stmtGetXonker.QueryRow(keyname, "pubkey")
615 err := row.Scan(&data)
616 var key httpsig.PublicKey
617 if err != nil {
618 log.Printf("hitting the webs for missing pubkey: %s", keyname)
619 j, err := GetJunk(keyname)
620 if err != nil {
621 log.Printf("error getting %s pubkey: %s", keyname, err)
622 when := time.Now().UTC().Format(dbtimeformat)
623 stmtSaveXonker.Exec(keyname, "failed", "pubkey", when)
624 return key, true
625 }
626 allinjest(originate(keyname), j)
627 row = stmtGetXonker.QueryRow(keyname, "pubkey")
628 err = row.Scan(&data)
629 if err != nil {
630 log.Printf("key not found after ingesting")
631 when := time.Now().UTC().Format(dbtimeformat)
632 stmtSaveXonker.Exec(keyname, "failed", "pubkey", when)
633 return key, true
634 }
635 }
636 _, key, err = httpsig.DecodeKey(data)
637 if err != nil {
638 log.Printf("error decoding %s pubkey: %s", keyname, err)
639 return key, true
640 }
641 return key, true
642}, Limit: 512})
643
644func zaggy(keyname string) httpsig.PublicKey {
645 var key httpsig.PublicKey
646 zaggies.Get(keyname, &key)
647 return key
648}
649
650func savingthrow(keyname string) {
651 when := time.Now().UTC().Add(-30 * time.Minute).Format(dbtimeformat)
652 stmtDeleteXonker.Exec(keyname, "pubkey", when)
653 zaggies.Clear(keyname)
654}
655
656func keymatch(keyname string, actor string) string {
657 hash := strings.IndexByte(keyname, '#')
658 if hash == -1 {
659 hash = len(keyname)
660 }
661 owner := keyname[0:hash]
662 if owner == actor {
663 return originate(actor)
664 }
665 return ""
666}