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