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 "crypto/rsa"
22 "database/sql"
23 "encoding/json"
24 "fmt"
25 "io"
26 "log"
27 "net/http"
28 "net/url"
29 "os"
30 "strconv"
31 "strings"
32 "sync"
33 "time"
34)
35
36func NewJunk() map[string]interface{} {
37 return make(map[string]interface{})
38}
39
40func WriteJunk(w io.Writer, j map[string]interface{}) error {
41 e := json.NewEncoder(w)
42 e.SetEscapeHTML(false)
43 e.SetIndent("", " ")
44 err := e.Encode(j)
45 return err
46}
47
48func ReadJunk(r io.Reader) (map[string]interface{}, error) {
49 decoder := json.NewDecoder(r)
50 var j map[string]interface{}
51 err := decoder.Decode(&j)
52 if err != nil {
53 return nil, err
54 }
55 return j, nil
56}
57
58var theonetruename = `application/ld+json; profile="https://www.w3.org/ns/activitystreams"`
59var thefakename = `application/activity+json`
60var falsenames = []string{
61 `application/ld+json`,
62 `application/activity+json`,
63}
64var itiswhatitis = "https://www.w3.org/ns/activitystreams"
65var thewholeworld = "https://www.w3.org/ns/activitystreams#Public"
66
67func friendorfoe(ct string) bool {
68 ct = strings.ToLower(ct)
69 for _, at := range falsenames {
70 if strings.HasPrefix(ct, at) {
71 return true
72 }
73 }
74 return false
75}
76
77func PostJunk(keyname string, key *rsa.PrivateKey, url string, j map[string]interface{}) error {
78 var buf bytes.Buffer
79 WriteJunk(&buf, j)
80 return PostMsg(keyname, key, url, buf.Bytes())
81}
82
83func PostMsg(keyname string, key *rsa.PrivateKey, url string, msg []byte) error {
84 client := http.DefaultClient
85 req, err := http.NewRequest("POST", url, bytes.NewReader(msg))
86 if err != nil {
87 return err
88 }
89 req.Header.Set("Content-Type", theonetruename)
90 zig(keyname, key, req, msg)
91 resp, err := client.Do(req)
92 if err != nil {
93 return err
94 }
95 resp.Body.Close()
96 switch resp.StatusCode {
97 case 200:
98 case 201:
99 case 202:
100 default:
101 return fmt.Errorf("http post status: %d", resp.StatusCode)
102 }
103 log.Printf("successful post: %s %d", url, resp.StatusCode)
104 return nil
105}
106
107type gzCloser struct {
108 r *gzip.Reader
109 under io.ReadCloser
110}
111
112func (gz *gzCloser) Read(p []byte) (int, error) {
113 return gz.r.Read(p)
114}
115
116func (gz *gzCloser) Close() error {
117 defer gz.under.Close()
118 return gz.r.Close()
119}
120
121func GetJunk(url string) (map[string]interface{}, error) {
122 client := http.DefaultClient
123 req, err := http.NewRequest("GET", url, nil)
124 if err != nil {
125 return nil, err
126 }
127 at := thefakename
128 if strings.Contains(url, ".well-known/webfinger?resource") {
129 at = "application/jrd+json"
130 }
131 req.Header.Set("Accept", at)
132 req.Header.Set("Accept-Encoding", "gzip")
133 resp, err := client.Do(req)
134 if err != nil {
135 return nil, err
136 }
137 if resp.StatusCode != 200 {
138 resp.Body.Close()
139 return nil, fmt.Errorf("http get status: %d", resp.StatusCode)
140 }
141 if strings.EqualFold(resp.Header.Get("Content-Encoding"), "gzip") {
142 gz, err := gzip.NewReader(resp.Body)
143 if err != nil {
144 resp.Body.Close()
145 return nil, err
146 }
147 resp.Body = &gzCloser{r: gz, under: resp.Body}
148 }
149 defer resp.Body.Close()
150 j, err := ReadJunk(resp.Body)
151 return j, err
152}
153
154func jsonfindinterface(ii interface{}, keys []string) interface{} {
155 for _, key := range keys {
156 idx, err := strconv.Atoi(key)
157 if err == nil {
158 m := ii.([]interface{})
159 if idx >= len(m) {
160 return nil
161 }
162 ii = m[idx]
163 } else {
164 m := ii.(map[string]interface{})
165 ii = m[key]
166 if ii == nil {
167 return nil
168 }
169 }
170 }
171 return ii
172}
173func jsonfindstring(j interface{}, keys []string) (string, bool) {
174 s, ok := jsonfindinterface(j, keys).(string)
175 return s, ok
176}
177func jsonfindarray(j interface{}, keys []string) ([]interface{}, bool) {
178 a, ok := jsonfindinterface(j, keys).([]interface{})
179 return a, ok
180}
181func jsonfindmap(j interface{}, keys []string) (map[string]interface{}, bool) {
182 m, ok := jsonfindinterface(j, keys).(map[string]interface{})
183 return m, ok
184}
185func jsongetstring(j interface{}, key string) (string, bool) {
186 return jsonfindstring(j, []string{key})
187}
188func jsongetarray(j interface{}, key string) ([]interface{}, bool) {
189 return jsonfindarray(j, []string{key})
190}
191func jsongetmap(j interface{}, key string) (map[string]interface{}, bool) {
192 return jsonfindmap(j, []string{key})
193}
194
195func savedonk(url string, name, media string) *Donk {
196 var donk Donk
197 row := stmtFindFile.QueryRow(url)
198 err := row.Scan(&donk.FileID)
199 if err == nil {
200 return &donk
201 }
202 log.Printf("saving donk: %s", url)
203 if err != nil && err != sql.ErrNoRows {
204 log.Printf("error querying: %s", err)
205 }
206 resp, err := http.Get(url)
207 if err != nil {
208 log.Printf("error fetching %s: %s", url, err)
209 return nil
210 }
211 defer resp.Body.Close()
212 if resp.StatusCode != 200 {
213 return nil
214 }
215 var buf bytes.Buffer
216 io.Copy(&buf, resp.Body)
217
218 xid := xfiltrate()
219
220 res, err := stmtSaveFile.Exec(xid, name, url, media, buf.Bytes())
221 if err != nil {
222 log.Printf("error saving file %s: %s", url, err)
223 return nil
224 }
225 donk.FileID, _ = res.LastInsertId()
226 return &donk
227}
228
229func needxonk(user *WhatAbout, x *Honk) bool {
230 if x == nil {
231 return false
232 }
233 if x.What == "eradicate" {
234 return true
235 }
236 if thoudostbitethythumb(user.ID, x.Audience, x.XID) {
237 log.Printf("not saving thumb biter? %s", x.Honker)
238 return false
239 }
240 return needxonkid(user, x.XID)
241}
242func needxonkid(user *WhatAbout, xid string) bool {
243 if strings.HasPrefix(xid, user.URL+"/h/") {
244 return false
245 }
246 row := stmtFindXonk.QueryRow(user.ID, xid)
247 var id int64
248 err := row.Scan(&id)
249 if err == nil {
250 return false
251 }
252 if err != sql.ErrNoRows {
253 log.Printf("err querying xonk: %s", err)
254 }
255 return true
256}
257
258func savexonk(user *WhatAbout, x *Honk) {
259 if x.What == "eradicate" {
260 log.Printf("eradicating %s by %s", x.RID, x.Honker)
261 _, err := stmtDeleteHonk.Exec(x.RID, x.Honker, user.ID)
262 if err != nil {
263 log.Printf("error eradicating: %s", err)
264 }
265 return
266 }
267 dt := x.Date.UTC().Format(dbtimeformat)
268 aud := strings.Join(x.Audience, " ")
269 whofore := 0
270 if strings.Contains(aud, user.URL) {
271 whofore = 1
272 }
273 res, err := stmtSaveHonk.Exec(x.UserID, x.What, x.Honker, x.XID, x.RID, dt, x.URL, aud,
274 x.Noise, x.Convoy, whofore)
275 if err != nil {
276 log.Printf("err saving xonk: %s", err)
277 return
278 }
279 x.ID, _ = res.LastInsertId()
280 for _, d := range x.Donks {
281 _, err = stmtSaveDonk.Exec(x.ID, d.FileID)
282 if err != nil {
283 log.Printf("err saving donk: %s", err)
284 return
285 }
286 }
287}
288
289type Box struct {
290 In string
291 Out string
292 Shared string
293}
294
295var boxofboxes = make(map[string]*Box)
296var boxlock sync.Mutex
297var boxinglock sync.Mutex
298
299func getboxes(ident string) (*Box, error) {
300 boxlock.Lock()
301 b, ok := boxofboxes[ident]
302 boxlock.Unlock()
303 if ok {
304 return b, nil
305 }
306
307 boxinglock.Lock()
308 defer boxinglock.Unlock()
309
310 boxlock.Lock()
311 b, ok = boxofboxes[ident]
312 boxlock.Unlock()
313 if ok {
314 return b, nil
315 }
316
317 db := opendatabase()
318
319 row := db.QueryRow("select ibox, obox, sbox from xonkers where xid = ?", ident)
320 b = &Box{}
321 err := row.Scan(&b.In, &b.Out, &b.Shared)
322 if err != nil {
323 j, err := GetJunk(ident)
324 if err != nil {
325 return nil, err
326 }
327 inbox, _ := jsongetstring(j, "inbox")
328 outbox, _ := jsongetstring(j, "outbox")
329 sbox, _ := jsonfindstring(j, []string{"endpoints", "sharedInbox"})
330 b = &Box{In: inbox, Out: outbox, Shared: sbox}
331 if inbox != "" {
332 db.Exec("insert into xonkers (xid, ibox, obox, sbox, pubkey) values (?, ?, ?, ?, ?)",
333 ident, inbox, outbox, sbox, "")
334 }
335 }
336 boxlock.Lock()
337 boxofboxes[ident] = b
338 boxlock.Unlock()
339 return b, nil
340}
341
342func peeppeep() {
343 user, _ := butwhatabout("htest")
344 honkers := gethonkers(user.ID)
345 for _, f := range honkers {
346 if f.Flavor != "peep" {
347 continue
348 }
349 log.Printf("getting updates: %s", f.XID)
350 box, err := getboxes(f.XID)
351 if err != nil {
352 log.Printf("error getting outbox: %s", err)
353 continue
354 }
355 log.Printf("getting outbox")
356 j, err := GetJunk(box.Out)
357 if err != nil {
358 log.Printf("err: %s", err)
359 continue
360 }
361 t, _ := jsongetstring(j, "type")
362 if t == "OrderedCollection" {
363 items, _ := jsongetarray(j, "orderedItems")
364 if items == nil {
365 page1, _ := jsongetstring(j, "first")
366 j, err = GetJunk(page1)
367 if err != nil {
368 log.Printf("err: %s", err)
369 continue
370 }
371 items, _ = jsongetarray(j, "orderedItems")
372 }
373
374 for _, item := range items {
375 xonk := xonkxonk(user, item)
376 if needxonk(user, xonk) {
377 savexonk(user, xonk)
378 }
379 }
380 }
381 }
382}
383
384func whosthere(xid string) ([]string, string) {
385 obj, err := GetJunk(xid)
386 if err != nil {
387 log.Printf("error getting remote xonk: %s", err)
388 return nil, ""
389 }
390 convoy, _ := jsongetstring(obj, "context")
391 if convoy == "" {
392 convoy, _ = jsongetstring(obj, "conversation")
393 }
394 return newphone(nil, obj), convoy
395}
396
397func newphone(a []string, obj map[string]interface{}) []string {
398 for _, addr := range []string{"to", "cc", "attributedTo"} {
399 who, _ := jsongetstring(obj, addr)
400 if who != "" {
401 a = append(a, who)
402 }
403 whos, _ := jsongetarray(obj, addr)
404 for _, w := range whos {
405 who, _ := w.(string)
406 if who != "" {
407 a = append(a, who)
408 }
409 }
410 }
411 return a
412}
413
414func xonkxonk(user *WhatAbout, item interface{}) *Honk {
415 depth := 0
416 maxdepth := 4
417 var xonkxonkfn func(item interface{}) *Honk
418
419 saveoneup := func(xid string) {
420 log.Printf("getting oneup: %s", xid)
421 if depth >= maxdepth {
422 log.Printf("in too deep")
423 return
424 }
425 obj, err := GetJunk(xid)
426 if err != nil {
427 log.Printf("error getting oneup: %s", err)
428 return
429 }
430 depth++
431 xonk := xonkxonkfn(obj)
432 if needxonk(user, xonk) {
433 xonk.UserID = user.ID
434 savexonk(user, xonk)
435 }
436 depth--
437 }
438
439 xonkxonkfn = func(item interface{}) *Honk {
440 // id, _ := jsongetstring(item, "id")
441 what, _ := jsongetstring(item, "type")
442 dt, _ := jsongetstring(item, "published")
443
444 var audience []string
445 var err error
446 var xid, rid, url, content, convoy string
447 var obj map[string]interface{}
448 var ok bool
449 switch what {
450 case "Announce":
451 xid, ok = jsongetstring(item, "object")
452 if ok {
453 if !needxonkid(user, xid) {
454 return nil
455 }
456 log.Printf("getting bonk: %s", xid)
457 obj, err = GetJunk(xid)
458 if err != nil {
459 log.Printf("error regetting: %s", err)
460 }
461 } else {
462 obj, _ = jsongetmap(item, "object")
463 }
464 what = "bonk"
465 case "Create":
466 obj, _ = jsongetmap(item, "object")
467 what = "honk"
468 case "Note":
469 obj = item.(map[string]interface{})
470 what = "honk"
471 case "Delete":
472 obj, _ = jsongetmap(item, "object")
473 rid, _ = jsongetstring(item, "object")
474 what = "eradicate"
475 default:
476 log.Printf("unknown activity: %s", what)
477 return nil
478 }
479
480 var xonk Honk
481 who, _ := jsongetstring(item, "actor")
482 if obj != nil {
483 if who == "" {
484 who, _ = jsongetstring(obj, "attributedTo")
485 }
486 ot, _ := jsongetstring(obj, "type")
487 url, _ = jsongetstring(obj, "url")
488 if ot == "Note" || ot == "Article" {
489 audience = newphone(audience, obj)
490 xid, _ = jsongetstring(obj, "id")
491 content, _ = jsongetstring(obj, "content")
492 summary, _ := jsongetstring(obj, "summary")
493 if !strings.HasPrefix(content, "<p>") {
494 content = "<p>" + content
495 }
496 if summary != "" {
497 content = "<p>summary: " + summary + content
498 }
499 rid, _ = jsongetstring(obj, "inReplyTo")
500 convoy, _ = jsongetstring(obj, "context")
501 if convoy == "" {
502 convoy, _ = jsongetstring(obj, "conversation")
503 }
504 if what == "honk" && rid != "" {
505 what = "tonk"
506 if needxonkid(user, rid) {
507 saveoneup(rid)
508 }
509 }
510 }
511 if ot == "Tombstone" {
512 rid, _ = jsongetstring(obj, "id")
513 }
514 atts, _ := jsongetarray(obj, "attachment")
515 for _, att := range atts {
516 at, _ := jsongetstring(att, "type")
517 mt, _ := jsongetstring(att, "mediaType")
518 u, _ := jsongetstring(att, "url")
519 name, _ := jsongetstring(att, "name")
520 if at == "Document" {
521 mt = strings.ToLower(mt)
522 log.Printf("attachment: %s %s", mt, u)
523 if mt == "image/jpeg" || mt == "image/png" ||
524 mt == "image/gif" || mt == "text/plain" {
525 donk := savedonk(u, name, mt)
526 if donk != nil {
527 xonk.Donks = append(xonk.Donks, donk)
528 }
529 }
530 }
531 }
532 tags, _ := jsongetarray(obj, "tag")
533 for _, tag := range tags {
534 tt, _ := jsongetstring(tag, "type")
535 name, _ := jsongetstring(tag, "name")
536 if tt == "Emoji" {
537 icon, _ := jsongetmap(tag, "icon")
538 mt, _ := jsongetstring(icon, "mediaType")
539 if mt == "" {
540 mt = "image/png"
541 }
542 u, _ := jsongetstring(icon, "url")
543 donk := savedonk(u, name, mt)
544 if donk != nil {
545 xonk.Donks = append(xonk.Donks, donk)
546 }
547 }
548 }
549 }
550 audience = append(audience, who)
551
552 audience = oneofakind(audience)
553
554 xonk.UserID = user.ID
555 xonk.What = what
556 xonk.Honker = who
557 xonk.XID = xid
558 xonk.RID = rid
559 xonk.Date, _ = time.Parse(time.RFC3339, dt)
560 xonk.URL = url
561 xonk.Noise = content
562 xonk.Audience = audience
563 xonk.Convoy = convoy
564
565 return &xonk
566 }
567
568 return xonkxonkfn(item)
569}
570
571func rubadubdub(user *WhatAbout, req map[string]interface{}) {
572 xid, _ := jsongetstring(req, "id")
573 reqactor, _ := jsongetstring(req, "actor")
574 j := NewJunk()
575 j["@context"] = itiswhatitis
576 j["id"] = user.URL + "/dub/" + xid
577 j["type"] = "Accept"
578 j["actor"] = user.URL
579 j["to"] = reqactor
580 j["published"] = time.Now().UTC().Format(time.RFC3339)
581 j["object"] = req
582
583 WriteJunk(os.Stdout, j)
584
585 actor, _ := jsongetstring(req, "actor")
586 box, err := getboxes(actor)
587 if err != nil {
588 log.Printf("can't get dub box: %s", err)
589 return
590 }
591 keyname, key := ziggy(user.Name)
592 err = PostJunk(keyname, key, box.In, j)
593 if err != nil {
594 log.Printf("can't rub a dub: %s", err)
595 return
596 }
597 stmtSaveDub.Exec(user.ID, actor, actor, "dub")
598}
599
600func subsub(user *WhatAbout, xid string) {
601 j := NewJunk()
602 j["@context"] = itiswhatitis
603 j["id"] = user.URL + "/sub/" + xid
604 j["type"] = "Follow"
605 j["actor"] = user.URL
606 j["to"] = xid
607 j["object"] = xid
608 j["published"] = time.Now().UTC().Format(time.RFC3339)
609
610 box, err := getboxes(xid)
611 if err != nil {
612 log.Printf("can't send follow: %s", err)
613 return
614 }
615 WriteJunk(os.Stdout, j)
616 keyname, key := ziggy(user.Name)
617 err = PostJunk(keyname, key, box.In, j)
618 if err != nil {
619 log.Printf("failed to subsub: %s", err)
620 }
621}
622
623func jonkjonk(user *WhatAbout, h *Honk) (map[string]interface{}, map[string]interface{}) {
624 dt := h.Date.Format(time.RFC3339)
625 var jo map[string]interface{}
626 j := NewJunk()
627 j["id"] = user.URL + "/" + h.What + "/" + h.XID
628 j["actor"] = user.URL
629 j["published"] = dt
630 j["to"] = h.Audience[0]
631 if len(h.Audience) > 1 {
632 j["cc"] = h.Audience[1:]
633 }
634
635 switch h.What {
636 case "zonk":
637 fallthrough
638 case "tonk":
639 fallthrough
640 case "honk":
641 j["type"] = "Create"
642 if h.What == "zonk" {
643 j["type"] = "Delete"
644 }
645
646 jo = NewJunk()
647 jo["id"] = user.URL + "/h/" + h.XID
648 jo["type"] = "Note"
649 if h.What == "zonk" {
650 jo["type"] = "Tombstone"
651 }
652 jo["published"] = dt
653 jo["url"] = user.URL + "/h/" + h.XID
654 jo["attributedTo"] = user.URL
655 if h.RID != "" {
656 jo["inReplyTo"] = h.RID
657 }
658 if h.Convoy != "" {
659 jo["context"] = h.Convoy
660 jo["conversation"] = h.Convoy
661 }
662 jo["to"] = h.Audience[0]
663 if len(h.Audience) > 1 {
664 jo["cc"] = h.Audience[1:]
665 }
666 jo["content"] = mentionize(h.Noise)
667 jo["summary"] = nil
668 var tags []interface{}
669 g := bunchofgrapes(h.Noise)
670 for _, m := range g {
671 t := NewJunk()
672 t["type"] = "Mention"
673 t["name"] = m.who
674 t["href"] = m.where
675 tags = append(tags, t)
676 }
677 herd := herdofemus(h.Noise)
678 for _, e := range herd {
679 t := NewJunk()
680 t["id"] = e.ID
681 t["type"] = "Emoji"
682 t["name"] = e.Name
683 i := NewJunk()
684 i["type"] = "Image"
685 i["mediaType"] = "image/png"
686 i["url"] = e.ID
687 t["icon"] = i
688 tags = append(tags, t)
689 }
690 if len(tags) > 0 {
691 jo["tag"] = tags
692 }
693 var atts []interface{}
694 for _, d := range h.Donks {
695 if re_emus.MatchString(d.Name) {
696 continue
697 }
698 jd := NewJunk()
699 jd["mediaType"] = d.Media
700 jd["name"] = d.Name
701 jd["type"] = "Document"
702 jd["url"] = d.URL
703 atts = append(atts, jd)
704 }
705 if len(atts) > 0 {
706 jo["attachment"] = atts
707 }
708 j["object"] = jo
709 case "bonk":
710 j["type"] = "Announce"
711 j["object"] = h.XID
712 }
713
714 return j, jo
715}
716
717func honkworldwide(user *WhatAbout, honk *Honk) {
718 jonk, _ := jonkjonk(user, honk)
719 jonk["@context"] = itiswhatitis
720 var buf bytes.Buffer
721 WriteJunk(&buf, jonk)
722 msg := buf.Bytes()
723
724 rcpts := make(map[string]bool)
725 for _, a := range honk.Audience {
726 if a != thewholeworld && a != user.URL && !strings.HasSuffix(a, "/followers") {
727 box, _ := getboxes(a)
728 if box != nil && box.Shared != "" {
729 rcpts["%"+box.Shared] = true
730 } else {
731 rcpts[a] = true
732 }
733 }
734 }
735 for _, f := range getdubs(user.ID) {
736 box, _ := getboxes(f.XID)
737 if box != nil && box.Shared != "" {
738 rcpts["%"+box.Shared] = true
739 } else {
740 rcpts[f.XID] = true
741 }
742 }
743 for a := range rcpts {
744 go deliverate(0, user.Name, a, msg)
745 }
746}
747
748func asjonker(user *WhatAbout) map[string]interface{} {
749 about := obfusbreak(user.About)
750
751 j := NewJunk()
752 j["@context"] = itiswhatitis
753 j["id"] = user.URL
754 j["type"] = "Person"
755 j["inbox"] = user.URL + "/inbox"
756 j["outbox"] = user.URL + "/outbox"
757 j["followers"] = user.URL + "/followers"
758 j["following"] = user.URL + "/following"
759 j["name"] = user.Display
760 j["preferredUsername"] = user.Name
761 j["summary"] = about
762 j["url"] = user.URL
763 a := NewJunk()
764 a["type"] = "icon"
765 a["mediaType"] = "image/png"
766 a["url"] = fmt.Sprintf("https://%s/a?a=%s", serverName, url.QueryEscape(user.URL))
767 j["icon"] = a
768 k := NewJunk()
769 k["id"] = user.URL + "#key"
770 k["owner"] = user.URL
771 k["publicKeyPem"] = user.Key
772 j["publicKey"] = k
773
774 return j
775}