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