activity.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 "bytes"
20 "compress/gzip"
21 "context"
22 "crypto/rsa"
23 "database/sql"
24 "fmt"
25 "io"
26 "log"
27 "net/http"
28 "net/url"
29 "os"
30 "regexp"
31 "strings"
32 "sync"
33 "time"
34
35 "humungus.tedunangst.com/r/webs/image"
36 "humungus.tedunangst.com/r/webs/junk"
37)
38
39var theonetruename = `application/ld+json; profile="https://www.w3.org/ns/activitystreams"`
40var thefakename = `application/activity+json`
41var falsenames = []string{
42 `application/ld+json`,
43 `application/activity+json`,
44}
45var itiswhatitis = "https://www.w3.org/ns/activitystreams"
46var thewholeworld = "https://www.w3.org/ns/activitystreams#Public"
47
48func friendorfoe(ct string) bool {
49 ct = strings.ToLower(ct)
50 for _, at := range falsenames {
51 if strings.HasPrefix(ct, at) {
52 return true
53 }
54 }
55 return false
56}
57
58func PostJunk(keyname string, key *rsa.PrivateKey, url string, j junk.Junk) error {
59 var buf bytes.Buffer
60 j.Write(&buf)
61 return PostMsg(keyname, key, url, buf.Bytes())
62}
63
64func PostMsg(keyname string, key *rsa.PrivateKey, url string, msg []byte) error {
65 client := http.DefaultClient
66 req, err := http.NewRequest("POST", url, bytes.NewReader(msg))
67 if err != nil {
68 return err
69 }
70 req.Header.Set("User-Agent", "honksnonk/5.0")
71 req.Header.Set("Content-Type", theonetruename)
72 zig(keyname, key, req, msg)
73 resp, err := client.Do(req)
74 if err != nil {
75 return err
76 }
77 resp.Body.Close()
78 switch resp.StatusCode {
79 case 200:
80 case 201:
81 case 202:
82 default:
83 return fmt.Errorf("http post status: %d", resp.StatusCode)
84 }
85 log.Printf("successful post: %s %d", url, resp.StatusCode)
86 return nil
87}
88
89type gzCloser struct {
90 r *gzip.Reader
91 under io.ReadCloser
92}
93
94func (gz *gzCloser) Read(p []byte) (int, error) {
95 return gz.r.Read(p)
96}
97
98func (gz *gzCloser) Close() error {
99 defer gz.under.Close()
100 return gz.r.Close()
101}
102
103func GetJunk(url string) (junk.Junk, error) {
104 return GetJunkTimeout(url, 0)
105}
106
107func GetJunkFast(url string) (junk.Junk, error) {
108 return GetJunkTimeout(url, 5*time.Second)
109}
110
111func GetJunkTimeout(url string, timeout time.Duration) (junk.Junk, error) {
112 client := http.DefaultClient
113 req, err := http.NewRequest("GET", url, nil)
114 if err != nil {
115 return nil, err
116 }
117 at := thefakename
118 if strings.Contains(url, ".well-known/webfinger?resource") {
119 at = "application/jrd+json"
120 }
121 req.Header.Set("Accept", at)
122 req.Header.Set("Accept-Encoding", "gzip")
123 req.Header.Set("User-Agent", "honksnonk/5.0")
124 if timeout > 0 {
125 ctx, cancel := context.WithTimeout(context.Background(), timeout)
126 defer cancel()
127 req = req.WithContext(ctx)
128 }
129 resp, err := client.Do(req)
130 if err != nil {
131 log.Printf("first get failed: %s", err)
132 if timeout > 0 || !strings.Contains(err.Error(), "TLS handshake timeout") {
133 return nil, err
134 }
135 resp, err = client.Do(req)
136 if err != nil {
137 return nil, err
138 }
139 log.Printf("retry succeeded!")
140 }
141 if resp.StatusCode != 200 {
142 resp.Body.Close()
143 return nil, fmt.Errorf("http get status: %d", resp.StatusCode)
144 }
145 if strings.EqualFold(resp.Header.Get("Content-Encoding"), "gzip") {
146 gz, err := gzip.NewReader(resp.Body)
147 if err != nil {
148 resp.Body.Close()
149 return nil, err
150 }
151 resp.Body = &gzCloser{r: gz, under: resp.Body}
152 }
153 defer resp.Body.Close()
154 j, err := junk.Read(resp.Body)
155 return j, err
156}
157
158func savedonk(url string, name, media string, localize bool) *Donk {
159 if url == "" {
160 return nil
161 }
162 var donk Donk
163 row := stmtFindFile.QueryRow(url)
164 err := row.Scan(&donk.FileID)
165 if err == nil {
166 return &donk
167 }
168 log.Printf("saving donk: %s", url)
169 if err != nil && err != sql.ErrNoRows {
170 log.Printf("error querying: %s", err)
171 }
172 xid := xfiltrate()
173 data := []byte{}
174 if localize {
175 resp, err := http.Get(url)
176 if err != nil {
177 log.Printf("error fetching %s: %s", url, err)
178 localize = false
179 goto saveit
180 }
181 defer resp.Body.Close()
182 if resp.StatusCode != 200 {
183 localize = false
184 goto saveit
185 }
186 var buf bytes.Buffer
187 io.Copy(&buf, resp.Body)
188
189 data = buf.Bytes()
190 if strings.HasPrefix(media, "image") {
191 img, err := image.Vacuum(&buf, image.Params{MaxWidth: 2048, MaxHeight: 2048})
192 if err != nil {
193 log.Printf("unable to decode image: %s", err)
194 localize = false
195 data = []byte{}
196 goto saveit
197 }
198 data = img.Data
199 media = "image/" + img.Format
200 } else if len(data) > 100000 {
201 log.Printf("not saving large attachment")
202 localize = false
203 data = []byte{}
204 }
205 }
206saveit:
207 res, err := stmtSaveFile.Exec(xid, name, url, media, localize, data)
208 if err != nil {
209 log.Printf("error saving file %s: %s", url, err)
210 return nil
211 }
212 donk.FileID, _ = res.LastInsertId()
213 return &donk
214}
215
216func iszonked(userid int64, xid string) bool {
217 row := stmtFindZonk.QueryRow(userid, xid)
218 var id int64
219 err := row.Scan(&id)
220 if err == nil {
221 return true
222 }
223 if err != sql.ErrNoRows {
224 log.Printf("err querying zonk: %s", err)
225 }
226 return false
227}
228
229func needxonk(user *WhatAbout, x *Honk) bool {
230 if x.What == "eradicate" {
231 return true
232 }
233 if thoudostbitethythumb(user.ID, x.Audience, x.XID) {
234 log.Printf("not saving thumb biter? %s via %s", x.XID, x.Honker)
235 return false
236 }
237 return needxonkid(user, x.XID)
238}
239func needxonkid(user *WhatAbout, xid string) bool {
240 if strings.HasPrefix(xid, user.URL+"/h/") {
241 return false
242 }
243 if iszonked(user.ID, xid) {
244 log.Printf("already zonked: %s", xid)
245 return false
246 }
247 row := stmtFindXonk.QueryRow(user.ID, xid)
248 var id int64
249 err := row.Scan(&id)
250 if err == nil {
251 return false
252 }
253 if err != sql.ErrNoRows {
254 log.Printf("err querying xonk: %s", err)
255 }
256 return true
257}
258
259func savexonk(user *WhatAbout, x *Honk) {
260 if x.What == "eradicate" {
261 log.Printf("eradicating %s by %s", x.XID, x.Honker)
262 xonk := getxonk(user.ID, x.XID)
263 if xonk != nil {
264 stmtZonkDonks.Exec(xonk.ID)
265 _, err := stmtZonkIt.Exec(user.ID, x.XID)
266 if err != nil {
267 log.Printf("error eradicating: %s", err)
268 }
269 }
270 stmtSaveZonker.Exec(user.ID, x.XID, "zonk")
271 return
272 }
273 log.Printf("saving xonk: %s", x.XID)
274 dt := x.Date.UTC().Format(dbtimeformat)
275 aud := strings.Join(x.Audience, " ")
276 whofore := 0
277 if strings.Contains(aud, user.URL) {
278 whofore = 1
279 }
280 res, err := stmtSaveHonk.Exec(x.UserID, x.What, x.Honker, x.XID, x.RID, dt, x.URL, aud,
281 x.Noise, x.Convoy, whofore, "html", x.Precis, x.Oonker)
282 if err != nil {
283 log.Printf("err saving xonk: %s", err)
284 return
285 }
286 x.ID, _ = res.LastInsertId()
287 for _, d := range x.Donks {
288 _, err = stmtSaveDonk.Exec(x.ID, d.FileID)
289 if err != nil {
290 log.Printf("err saving donk: %s", err)
291 return
292 }
293 }
294}
295
296type Box struct {
297 In string
298 Out string
299 Shared string
300}
301
302var boxofboxes = make(map[string]*Box)
303var boxlock sync.Mutex
304var boxinglock sync.Mutex
305
306func getboxes(ident string) (*Box, error) {
307 boxlock.Lock()
308 b, ok := boxofboxes[ident]
309 boxlock.Unlock()
310 if ok {
311 return b, nil
312 }
313
314 boxinglock.Lock()
315 defer boxinglock.Unlock()
316
317 boxlock.Lock()
318 b, ok = boxofboxes[ident]
319 boxlock.Unlock()
320 if ok {
321 return b, nil
322 }
323
324 var info string
325 row := stmtGetXonker.QueryRow(ident, "boxes")
326 err := row.Scan(&info)
327 if err != nil {
328 j, err := GetJunk(ident)
329 if err != nil {
330 return nil, err
331 }
332 inbox, _ := j.GetString("inbox")
333 outbox, _ := j.GetString("outbox")
334 sbox, _ := j.FindString([]string{"endpoints", "sharedInbox"})
335 b = &Box{In: inbox, Out: outbox, Shared: sbox}
336 if inbox != "" {
337 m := strings.Join([]string{inbox, outbox, sbox}, " ")
338 _, err = stmtSaveXonker.Exec(ident, m, "boxes")
339 if err != nil {
340 log.Printf("error saving boxes: %s", err)
341 }
342 }
343 } else {
344 m := strings.Split(info, " ")
345 b = &Box{In: m[0], Out: m[1], Shared: m[2]}
346 }
347
348 boxlock.Lock()
349 boxofboxes[ident] = b
350 boxlock.Unlock()
351 return b, nil
352}
353
354func gimmexonks(user *WhatAbout, outbox string) {
355 log.Printf("getting outbox: %s", outbox)
356 j, err := GetJunk(outbox)
357 if err != nil {
358 log.Printf("error getting outbox: %s", err)
359 return
360 }
361 t, _ := j.GetString("type")
362 origin := originate(outbox)
363 if t == "OrderedCollection" {
364 items, _ := j.GetArray("orderedItems")
365 if items == nil {
366 obj, ok := j.GetMap("first")
367 if ok {
368 items, _ = obj.GetArray("orderedItems")
369 } else {
370 page1, _ := j.GetString("first")
371 j, err = GetJunk(page1)
372 if err != nil {
373 log.Printf("error gettings page1: %s", err)
374 return
375 }
376 items, _ = j.GetArray("orderedItems")
377 }
378 }
379 if len(items) > 20 {
380 items = items[0:20]
381 }
382 for i, j := 0, len(items)-1; i < j; i, j = i+1, j-1 {
383 items[i], items[j] = items[j], items[i]
384 }
385 for _, item := range items {
386 obj, ok := item.(junk.Junk)
387 if !ok {
388 continue
389 }
390 xonk := xonkxonk(user, obj, origin)
391 if xonk != nil {
392 savexonk(user, xonk)
393 }
394 }
395 }
396}
397
398func peeppeep() {
399 user, _ := butwhatabout("htest")
400 honkers := gethonkers(user.ID)
401 for _, f := range honkers {
402 if f.Flavor != "peep" {
403 continue
404 }
405 log.Printf("getting updates: %s", f.XID)
406 box, err := getboxes(f.XID)
407 if err != nil {
408 log.Printf("error getting outbox: %s", err)
409 continue
410 }
411 gimmexonks(user, box.Out)
412 }
413}
414func whosthere(xid string) ([]string, string) {
415 obj, err := GetJunk(xid)
416 if err != nil {
417 log.Printf("error getting remote xonk: %s", err)
418 return nil, ""
419 }
420 convoy, _ := obj.GetString("context")
421 if convoy == "" {
422 convoy, _ = obj.GetString("conversation")
423 }
424 return newphone(nil, obj), convoy
425}
426
427func newphone(a []string, obj junk.Junk) []string {
428 for _, addr := range []string{"to", "cc", "attributedTo"} {
429 who, _ := obj.GetString(addr)
430 if who != "" {
431 a = append(a, who)
432 }
433 whos, _ := obj.GetArray(addr)
434 for _, w := range whos {
435 who, _ := w.(string)
436 if who != "" {
437 a = append(a, who)
438 }
439 }
440 }
441 return a
442}
443
444func extractattrto(obj junk.Junk) string {
445 who, _ := obj.GetString("attributedTo")
446 if who != "" {
447 return who
448 }
449 arr, _ := obj.GetArray("attributedTo")
450 for _, a := range arr {
451 o, ok := a.(junk.Junk)
452 if ok {
453 t, _ := o.GetString("type")
454 id, _ := o.GetString("id")
455 if t == "Person" || t == "" {
456 return id
457 }
458 }
459 }
460 return ""
461}
462
463func consumeactivity(user *WhatAbout, j junk.Junk, origin string) {
464 xonk := xonkxonk(user, j, origin)
465 if xonk != nil {
466 savexonk(user, xonk)
467 }
468}
469
470func xonkxonk(user *WhatAbout, item junk.Junk, origin string) *Honk {
471 depth := 0
472 maxdepth := 4
473 currenttid := ""
474 var xonkxonkfn func(item junk.Junk, origin string) *Honk
475
476 saveoneup := func(xid string) {
477 log.Printf("getting oneup: %s", xid)
478 if depth >= maxdepth {
479 log.Printf("in too deep")
480 return
481 }
482 obj, err := GetJunk(xid)
483 if err != nil {
484 log.Printf("error getting oneup: %s", err)
485 return
486 }
487 depth++
488 xonk := xonkxonkfn(obj, originate(xid))
489 if xonk != nil {
490 savexonk(user, xonk)
491 }
492 depth--
493 }
494
495 xonkxonkfn = func(item junk.Junk, origin string) *Honk {
496 // id, _ := item.GetString( "id")
497 what, _ := item.GetString("type")
498 dt, _ := item.GetString("published")
499
500 var audience []string
501 var err error
502 var xid, rid, url, content, precis, convoy, oonker string
503 var obj junk.Junk
504 var ok bool
505 switch what {
506 case "Announce":
507 obj, ok = item.GetMap("object")
508 if ok {
509 xid, _ = obj.GetString("id")
510 } else {
511 xid, _ = item.GetString("object")
512 }
513 if !needxonkid(user, xid) {
514 return nil
515 }
516 log.Printf("getting bonk: %s", xid)
517 obj, err = GetJunk(xid)
518 if err != nil {
519 log.Printf("error regetting: %s", err)
520 }
521 origin = originate(xid)
522 what = "bonk"
523 case "Create":
524 obj, _ = item.GetMap("object")
525 what = "honk"
526 case "Delete":
527 obj, _ = item.GetMap("object")
528 xid, _ = item.GetString("object")
529 what = "eradicate"
530 case "Video":
531 fallthrough
532 case "Question":
533 fallthrough
534 case "Note":
535 fallthrough
536 case "Article":
537 fallthrough
538 case "Page":
539 obj = item
540 what = "honk"
541 default:
542 log.Printf("unknown activity: %s", what)
543 fd, _ := os.OpenFile("savedinbox.json", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
544 item.Write(fd)
545 io.WriteString(fd, "\n")
546 fd.Close()
547 return nil
548 }
549
550 var xonk Honk
551 who, _ := item.GetString("actor")
552 if obj != nil {
553 if who == "" {
554 who = extractattrto(obj)
555 }
556 oonker = extractattrto(obj)
557 ot, _ := obj.GetString("type")
558 url, _ = obj.GetString("url")
559 if ot == "Tombstone" {
560 xid, _ = obj.GetString("id")
561 } else {
562 audience = newphone(audience, obj)
563 xid, _ = obj.GetString("id")
564 precis, _ = obj.GetString("summary")
565 content, _ = obj.GetString("content")
566 if !strings.HasPrefix(content, "<p>") {
567 content = "<p>" + content
568 }
569 sens, _ := obj["sensitive"].(bool)
570 if sens && precis == "" {
571 precis = "unspecified horror"
572 }
573 rid, _ = obj.GetString("inReplyTo")
574 convoy, _ = obj.GetString("context")
575 if convoy == "" {
576 convoy, _ = obj.GetString("conversation")
577 }
578 if ot == "Question" {
579 if what == "honk" {
580 what = "qonk"
581 }
582 content += "<ul>"
583 ans, _ := obj.GetArray("oneOf")
584 for _, ai := range ans {
585 a, ok := ai.(junk.Junk)
586 if !ok {
587 continue
588 }
589 as, _ := a.GetString("name")
590 content += "<li>" + as
591 }
592 ans, _ = obj.GetArray("anyOf")
593 for _, ai := range ans {
594 a, ok := ai.(junk.Junk)
595 if !ok {
596 continue
597 }
598 as, _ := a.GetString("name")
599 content += "<li>" + as
600 }
601 content += "</ul>"
602 }
603 if what == "honk" && rid != "" {
604 what = "tonk"
605 }
606 }
607 atts, _ := obj.GetArray("attachment")
608 for i, atti := range atts {
609 att, ok := atti.(junk.Junk)
610 if !ok {
611 continue
612 }
613 at, _ := att.GetString("type")
614 mt, _ := att.GetString("mediaType")
615 u, _ := att.GetString("url")
616 name, _ := att.GetString("name")
617 localize := false
618 if i > 4 {
619 log.Printf("excessive attachment: %s", at)
620 } else if at == "Document" || at == "Image" {
621 mt = strings.ToLower(mt)
622 log.Printf("attachment: %s %s", mt, u)
623 if mt == "text/plain" || strings.HasPrefix(mt, "image") {
624 localize = true
625 }
626 } else {
627 log.Printf("unknown attachment: %s", at)
628 }
629 donk := savedonk(u, name, mt, localize)
630 if donk != nil {
631 xonk.Donks = append(xonk.Donks, donk)
632 }
633 }
634 tags, _ := obj.GetArray("tag")
635 for _, tagi := range tags {
636 tag, ok := tagi.(junk.Junk)
637 if !ok {
638 continue
639 }
640 tt, _ := tag.GetString("type")
641 name, _ := tag.GetString("name")
642 if tt == "Emoji" {
643 icon, _ := tag.GetMap("icon")
644 mt, _ := icon.GetString("mediaType")
645 if mt == "" {
646 mt = "image/png"
647 }
648 u, _ := icon.GetString("url")
649 donk := savedonk(u, name, mt, true)
650 if donk != nil {
651 xonk.Donks = append(xonk.Donks, donk)
652 }
653 }
654 }
655 }
656 if originate(xid) != origin {
657 log.Printf("original sin: %s <> %s", xid, origin)
658 item.Write(os.Stdout)
659 return nil
660 }
661 audience = append(audience, who)
662
663 audience = oneofakind(audience)
664
665 if currenttid == "" {
666 currenttid = convoy
667 }
668
669 if oonker == who {
670 oonker = ""
671 }
672 xonk.UserID = user.ID
673 xonk.What = what
674 xonk.Honker = who
675 xonk.XID = xid
676 xonk.RID = rid
677 xonk.Date, _ = time.Parse(time.RFC3339, dt)
678 xonk.URL = url
679 xonk.Noise = content
680 xonk.Precis = precis
681 xonk.Audience = audience
682 xonk.Oonker = oonker
683
684 if needxonk(user, &xonk) {
685 if rid != "" {
686 if needxonkid(user, rid) {
687 saveoneup(rid)
688 }
689 }
690 if convoy == "" {
691 convoy = currenttid
692 }
693 xonk.Convoy = convoy
694 return &xonk
695 }
696 return nil
697 }
698
699 return xonkxonkfn(item, origin)
700}
701
702func rubadubdub(user *WhatAbout, req junk.Junk) {
703 xid, _ := req.GetString("id")
704 actor, _ := req.GetString("actor")
705 j := junk.New()
706 j["@context"] = itiswhatitis
707 j["id"] = user.URL + "/dub/" + url.QueryEscape(xid)
708 j["type"] = "Accept"
709 j["actor"] = user.URL
710 j["to"] = actor
711 j["published"] = time.Now().UTC().Format(time.RFC3339)
712 j["object"] = req
713
714 var buf bytes.Buffer
715 j.Write(&buf)
716 msg := buf.Bytes()
717
718 deliverate(0, user.Name, actor, msg)
719}
720
721func itakeitallback(user *WhatAbout, xid string) {
722 j := junk.New()
723 j["@context"] = itiswhatitis
724 j["id"] = user.URL + "/unsub/" + url.QueryEscape(xid)
725 j["type"] = "Undo"
726 j["actor"] = user.URL
727 j["to"] = xid
728 f := junk.New()
729 f["id"] = user.URL + "/sub/" + url.QueryEscape(xid)
730 f["type"] = "Follow"
731 f["actor"] = user.URL
732 f["to"] = xid
733 f["object"] = xid
734 j["object"] = f
735 j["published"] = time.Now().UTC().Format(time.RFC3339)
736
737 var buf bytes.Buffer
738 j.Write(&buf)
739 msg := buf.Bytes()
740
741 deliverate(0, user.Name, xid, msg)
742}
743
744func subsub(user *WhatAbout, xid string) {
745 j := junk.New()
746 j["@context"] = itiswhatitis
747 j["id"] = user.URL + "/sub/" + url.QueryEscape(xid)
748 j["type"] = "Follow"
749 j["actor"] = user.URL
750 j["to"] = xid
751 j["object"] = xid
752 j["published"] = time.Now().UTC().Format(time.RFC3339)
753
754 var buf bytes.Buffer
755 j.Write(&buf)
756 msg := buf.Bytes()
757
758 deliverate(0, user.Name, xid, msg)
759}
760
761var re_spicy = regexp.MustCompile("^(\U0001f336\ufe0f?){3,}")
762
763func jonkjonk(user *WhatAbout, h *Honk) (junk.Junk, junk.Junk) {
764 dt := h.Date.Format(time.RFC3339)
765 var jo junk.Junk
766 j := junk.New()
767 j["id"] = user.URL + "/" + h.What + "/" + shortxid(h.XID)
768 j["actor"] = user.URL
769 j["published"] = dt
770 if h.Public {
771 j["to"] = []string{h.Audience[0], user.URL + "/followers"}
772 } else {
773 j["to"] = h.Audience[0]
774 }
775 if len(h.Audience) > 1 {
776 j["cc"] = h.Audience[1:]
777 }
778
779 switch h.What {
780 case "tonk":
781 fallthrough
782 case "honk":
783 j["type"] = "Create"
784
785 jo = junk.New()
786 jo["id"] = h.XID
787 jo["type"] = "Note"
788 jo["published"] = dt
789 jo["url"] = h.XID
790 jo["attributedTo"] = user.URL
791 if h.RID != "" {
792 jo["inReplyTo"] = h.RID
793 }
794 if h.Convoy != "" {
795 jo["context"] = h.Convoy
796 jo["conversation"] = h.Convoy
797 }
798 jo["to"] = h.Audience[0]
799 if len(h.Audience) > 1 {
800 jo["cc"] = h.Audience[1:]
801 }
802 if !h.Public {
803 jo["directMessage"] = true
804 }
805 jo["summary"] = h.Precis
806 jo["content"] = mentionize(h.Noise)
807 if strings.HasPrefix(h.Precis, "DZ:") {
808 jo["sensitive"] = true
809 } else if peppers := re_spicy.FindString(h.Noise); peppers != "" {
810 jo["summary"] = peppers
811 jo["sensitive"] = true
812 }
813
814 var tags []interface{}
815 g := bunchofgrapes(h.Noise)
816 for _, m := range g {
817 t := junk.New()
818 t["type"] = "Mention"
819 t["name"] = m.who
820 t["href"] = m.where
821 tags = append(tags, t)
822 }
823 ooo := ontologies(h.Noise)
824 for _, o := range ooo {
825 t := junk.New()
826 t["type"] = "Hashtag"
827 t["name"] = o
828 tags = append(tags, t)
829 }
830 herd := herdofemus(h.Noise)
831 for _, e := range herd {
832 t := junk.New()
833 t["id"] = e.ID
834 t["type"] = "Emoji"
835 t["name"] = e.Name
836 i := junk.New()
837 i["type"] = "Image"
838 i["mediaType"] = "image/png"
839 i["url"] = e.ID
840 t["icon"] = i
841 tags = append(tags, t)
842 }
843 if len(tags) > 0 {
844 jo["tag"] = tags
845 }
846 var atts []interface{}
847 for _, d := range h.Donks {
848 if re_emus.MatchString(d.Name) {
849 continue
850 }
851 jd := junk.New()
852 jd["mediaType"] = d.Media
853 jd["name"] = d.Name
854 jd["type"] = "Document"
855 jd["url"] = d.URL
856 atts = append(atts, jd)
857 }
858 if len(atts) > 0 {
859 jo["attachment"] = atts
860 }
861 j["object"] = jo
862 case "bonk":
863 j["type"] = "Announce"
864 if h.Convoy != "" {
865 j["context"] = h.Convoy
866 }
867 j["object"] = h.XID
868 case "zonk":
869 j["type"] = "Delete"
870 j["object"] = h.XID
871 }
872
873 return j, jo
874}
875
876func honkworldwide(user *WhatAbout, honk *Honk) {
877 jonk, _ := jonkjonk(user, honk)
878 jonk["@context"] = itiswhatitis
879 var buf bytes.Buffer
880 jonk.Write(&buf)
881 msg := buf.Bytes()
882
883 rcpts := make(map[string]bool)
884 for _, a := range honk.Audience {
885 if a != thewholeworld && a != user.URL && !strings.HasSuffix(a, "/followers") {
886 box, _ := getboxes(a)
887 if box != nil {
888 if honk.Public && box.Shared != "" {
889 rcpts["%"+box.Shared] = true
890 } else {
891 rcpts["%"+box.In] = true
892 }
893 } else {
894 rcpts[a] = true
895 }
896 }
897 }
898 if honk.Public {
899 for _, f := range getdubs(user.ID) {
900 box, _ := getboxes(f.XID)
901 if box != nil && box.Shared != "" {
902 rcpts["%"+box.Shared] = true
903 } else {
904 rcpts[f.XID] = true
905 }
906 }
907 }
908 for a := range rcpts {
909 go deliverate(0, user.Name, a, msg)
910 }
911}
912
913func asjonker(user *WhatAbout) junk.Junk {
914 about := obfusbreak(user.About)
915
916 j := junk.New()
917 j["@context"] = itiswhatitis
918 j["id"] = user.URL
919 j["type"] = "Person"
920 j["inbox"] = user.URL + "/inbox"
921 j["outbox"] = user.URL + "/outbox"
922 j["followers"] = user.URL + "/followers"
923 j["following"] = user.URL + "/following"
924 j["name"] = user.Display
925 j["preferredUsername"] = user.Name
926 j["summary"] = about
927 j["url"] = user.URL
928 a := junk.New()
929 a["type"] = "Image"
930 a["mediaType"] = "image/png"
931 a["url"] = fmt.Sprintf("https://%s/a?a=%s", serverName, url.QueryEscape(user.URL))
932 j["icon"] = a
933 k := junk.New()
934 k["id"] = user.URL + "#key"
935 k["owner"] = user.URL
936 k["publicKeyPem"] = user.Key
937 j["publicKey"] = k
938
939 return j
940}
941
942var handfull = make(map[string]string)
943var handlock sync.Mutex
944
945func gofish(name string) string {
946 if name[0] == '@' {
947 name = name[1:]
948 }
949 m := strings.Split(name, "@")
950 if len(m) != 2 {
951 log.Printf("bad fish name: %s", name)
952 return ""
953 }
954 handlock.Lock()
955 ref, ok := handfull[name]
956 handlock.Unlock()
957 if ok {
958 return ref
959 }
960 row := stmtGetXonker.QueryRow(name, "fishname")
961 var href string
962 err := row.Scan(&href)
963 if err == nil {
964 handlock.Lock()
965 handfull[name] = href
966 handlock.Unlock()
967 return href
968 }
969 log.Printf("fishing for %s", name)
970 j, err := GetJunkFast(fmt.Sprintf("https://%s/.well-known/webfinger?resource=acct:%s", m[1], name))
971 if err != nil {
972 log.Printf("failed to go fish %s: %s", name, err)
973 handlock.Lock()
974 handfull[name] = ""
975 handlock.Unlock()
976 return ""
977 }
978 links, _ := j.GetArray("links")
979 for _, li := range links {
980 l, ok := li.(junk.Junk)
981 if !ok {
982 continue
983 }
984 href, _ := l.GetString("href")
985 rel, _ := l.GetString("rel")
986 t, _ := l.GetString("type")
987 if rel == "self" && friendorfoe(t) {
988 stmtSaveXonker.Exec(name, href, "fishname")
989 handlock.Lock()
990 handfull[name] = href
991 handlock.Unlock()
992 return href
993 }
994 }
995 handlock.Lock()
996 handfull[name] = ""
997 handlock.Unlock()
998 return ""
999}
1000
1001func isactor(t string) bool {
1002 switch t {
1003 case "Person":
1004 case "Application":
1005 case "Service":
1006 default:
1007 return false
1008 }
1009 return true
1010}
1011
1012func investigate(name string) string {
1013 if name == "" {
1014 return ""
1015 }
1016 if name[0] == '@' {
1017 name = gofish(name)
1018 }
1019 if name == "" {
1020 return ""
1021 }
1022 obj, err := GetJunkFast(name)
1023 if err != nil {
1024 log.Printf("error investigating honker: %s", err)
1025 return ""
1026 }
1027 t, _ := obj.GetString("type")
1028 if !isactor(t) {
1029 log.Printf("it's not a person! %s", name)
1030 return ""
1031 }
1032 id, _ := obj.GetString("id")
1033 return id
1034}