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