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