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 "context"
21 "crypto/tls"
22 "database/sql"
23 "fmt"
24 "html"
25 "io"
26 "log"
27 notrand "math/rand"
28 "net/http"
29 "net/url"
30 "os"
31 "strings"
32 "time"
33
34 "humungus.tedunangst.com/r/webs/cache"
35 "humungus.tedunangst.com/r/webs/gate"
36 "humungus.tedunangst.com/r/webs/httpsig"
37 "humungus.tedunangst.com/r/webs/junk"
38 "humungus.tedunangst.com/r/webs/templates"
39)
40
41var theonetruename = `application/ld+json; profile="https://www.w3.org/ns/activitystreams"`
42var thefakename = `application/activity+json`
43var falsenames = []string{
44 `application/ld+json`,
45 `application/activity+json`,
46}
47var itiswhatitis = "https://www.w3.org/ns/activitystreams"
48var thewholeworld = "https://www.w3.org/ns/activitystreams#Public"
49
50func friendorfoe(ct string) bool {
51 ct = strings.ToLower(ct)
52 for _, at := range falsenames {
53 if strings.HasPrefix(ct, at) {
54 return true
55 }
56 }
57 return false
58}
59
60var debugClient = &http.Client{
61 Transport: &http.Transport{
62 TLSClientConfig: &tls.Config{
63 InsecureSkipVerify: true,
64 },
65 },
66}
67
68func PostJunk(keyname string, key httpsig.PrivateKey, url string, j junk.Junk) error {
69 return PostMsg(keyname, key, url, j.ToBytes())
70}
71
72func PostMsg(keyname string, key httpsig.PrivateKey, url string, msg []byte) error {
73 client := http.DefaultClient
74 if debugMode {
75 client = debugClient
76 }
77 req, err := http.NewRequest("POST", url, bytes.NewReader(msg))
78 if err != nil {
79 return err
80 }
81 req.Header.Set("User-Agent", "honksnonk/5.0; "+serverName)
82 req.Header.Set("Content-Type", theonetruename)
83 httpsig.SignRequest(keyname, key, req, msg)
84 ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
85 defer cancel()
86 req = req.WithContext(ctx)
87 resp, err := client.Do(req)
88 if err != nil {
89 return err
90 }
91 resp.Body.Close()
92 switch resp.StatusCode {
93 case 200:
94 case 201:
95 case 202:
96 default:
97 return fmt.Errorf("http post status: %d", resp.StatusCode)
98 }
99 log.Printf("successful post: %s %d", url, resp.StatusCode)
100 return nil
101}
102
103type JunkError struct {
104 Junk junk.Junk
105 Err error
106}
107
108func GetJunk(url string) (junk.Junk, error) {
109 return GetJunkTimeout(url, 30*time.Second)
110}
111
112func GetJunkFast(url string) (junk.Junk, error) {
113 return GetJunkTimeout(url, 5*time.Second)
114}
115
116func GetJunkHardMode(url string) (junk.Junk, error) {
117 j, err := GetJunk(url)
118 if err != nil {
119 emsg := err.Error()
120 if emsg == "http get status: 502" || strings.Contains(emsg, "timeout") {
121 log.Printf("trying again after error: %s", emsg)
122 time.Sleep(time.Duration(60+notrand.Int63n(60)) * time.Second)
123 j, err = GetJunk(url)
124 if err != nil {
125 log.Printf("still couldn't get it")
126 } else {
127 log.Printf("retry success!")
128 }
129 }
130 }
131 return j, err
132}
133
134var flightdeck = gate.NewSerializer()
135
136func GetJunkTimeout(url string, timeout time.Duration) (junk.Junk, error) {
137
138 fn := func() (interface{}, error) {
139 at := thefakename
140 if strings.Contains(url, ".well-known/webfinger?resource") {
141 at = "application/jrd+json"
142 }
143 j, err := junk.Get(url, junk.GetArgs{
144 Accept: at,
145 Agent: "honksnonk/5.0; " + serverName,
146 Timeout: timeout,
147 })
148 return j, err
149 }
150
151 ji, err := flightdeck.Call(url, fn)
152 if err != nil {
153 return nil, err
154 }
155 j := ji.(junk.Junk)
156 return j, nil
157}
158
159func fetchsome(url string) ([]byte, error) {
160 client := http.DefaultClient
161 if debugMode {
162 client = debugClient
163 }
164 req, err := http.NewRequest("GET", url, nil)
165 if err != nil {
166 log.Printf("error fetching %s: %s", url, err)
167 return nil, err
168 }
169 req.Header.Set("User-Agent", "honksnonk/5.0; "+serverName)
170 ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
171 defer cancel()
172 req = req.WithContext(ctx)
173 resp, err := client.Do(req)
174 if err != nil {
175 log.Printf("error fetching %s: %s", url, err)
176 return nil, err
177 }
178 defer resp.Body.Close()
179 switch resp.StatusCode {
180 case 200:
181 case 201:
182 case 202:
183 default:
184 return nil, fmt.Errorf("http get not 200: %d %s", resp.StatusCode, url)
185 }
186 var buf bytes.Buffer
187 limiter := io.LimitReader(resp.Body, 10*1024*1024)
188 io.Copy(&buf, limiter)
189 return buf.Bytes(), nil
190}
191
192func savedonk(url string, name, desc, media string, localize bool) *Donk {
193 if url == "" {
194 return nil
195 }
196 if donk := finddonk(url); donk != nil {
197 return donk
198 }
199 log.Printf("saving donk: %s", url)
200 data := []byte{}
201 if localize {
202 fn := func() (interface{}, error) {
203 return fetchsome(url)
204 }
205 ii, err := flightdeck.Call(url, fn)
206 if err != nil {
207 log.Printf("error fetching donk: %s", err)
208 localize = false
209 goto saveit
210 }
211 data = ii.([]byte)
212
213 if len(data) == 10*1024*1024 {
214 log.Printf("truncation likely")
215 }
216 if strings.HasPrefix(media, "image") {
217 img, err := shrinkit(data)
218 if err != nil {
219 log.Printf("unable to decode image: %s", err)
220 localize = false
221 data = []byte{}
222 goto saveit
223 }
224 data = img.Data
225 media = "image/" + img.Format
226 } else if media == "application/pdf" {
227 if len(data) > 1000000 {
228 log.Printf("not saving large pdf")
229 localize = false
230 data = []byte{}
231 }
232 } else if len(data) > 100000 {
233 log.Printf("not saving large attachment")
234 localize = false
235 data = []byte{}
236 }
237 }
238saveit:
239 fileid, err := savefile(name, desc, url, media, localize, data)
240 if err != nil {
241 log.Printf("error saving file %s: %s", url, err)
242 return nil
243 }
244 donk := new(Donk)
245 donk.FileID = fileid
246 return donk
247}
248
249func iszonked(userid int64, xid string) bool {
250 var id int64
251 row := stmtFindZonk.QueryRow(userid, xid)
252 err := row.Scan(&id)
253 if err == nil {
254 return true
255 }
256 if err != sql.ErrNoRows {
257 log.Printf("error querying zonk: %s", err)
258 }
259 return false
260}
261
262func needxonk(user *WhatAbout, x *Honk) bool {
263 if rejectxonk(x) {
264 return false
265 }
266 return needxonkid(user, x.XID)
267}
268func needbonkid(user *WhatAbout, xid string) bool {
269 return needxonkidX(user, xid, true)
270}
271func needxonkid(user *WhatAbout, xid string) bool {
272 return needxonkidX(user, xid, false)
273}
274func needxonkidX(user *WhatAbout, xid string, isannounce bool) bool {
275 if !strings.HasPrefix(xid, "https://") {
276 return false
277 }
278 if strings.HasPrefix(xid, user.URL+"/") {
279 return false
280 }
281 if rejectorigin(user.ID, xid, isannounce) {
282 log.Printf("rejecting origin: %s", xid)
283 return false
284 }
285 if iszonked(user.ID, xid) {
286 log.Printf("already zonked: %s", xid)
287 return false
288 }
289 var id int64
290 row := stmtFindXonk.QueryRow(user.ID, xid)
291 err := row.Scan(&id)
292 if err == nil {
293 return false
294 }
295 if err != sql.ErrNoRows {
296 log.Printf("error querying xonk: %s", err)
297 }
298 return true
299}
300
301func eradicatexonk(userid int64, xid string) {
302 xonk := getxonk(userid, xid)
303 if xonk != nil {
304 deletehonk(xonk.ID)
305 }
306 _, err := stmtSaveZonker.Exec(userid, xid, "zonk")
307 if err != nil {
308 log.Printf("error eradicating: %s", err)
309 }
310}
311
312func savexonk(x *Honk) {
313 log.Printf("saving xonk: %s", x.XID)
314 go handles(x.Honker)
315 go handles(x.Oonker)
316 savehonk(x)
317}
318
319type Box struct {
320 In string
321 Out string
322 Shared string
323}
324
325var boxofboxes = cache.New(cache.Options{Filler: func(ident string) (*Box, bool) {
326 var info string
327 row := stmtGetXonker.QueryRow(ident, "boxes")
328 err := row.Scan(&info)
329 if err != nil {
330 log.Printf("need to get boxes for %s", ident)
331 var j junk.Junk
332 j, err = GetJunk(ident)
333 if err != nil {
334 log.Printf("error getting boxes: %s", err)
335 return nil, false
336 }
337 allinjest(originate(ident), j)
338 row = stmtGetXonker.QueryRow(ident, "boxes")
339 err = row.Scan(&info)
340 }
341 if err == nil {
342 m := strings.Split(info, " ")
343 b := &Box{In: m[0], Out: m[1], Shared: m[2]}
344 return b, true
345 }
346 return nil, false
347}})
348
349func gimmexonks(user *WhatAbout, outbox string) {
350 log.Printf("getting outbox: %s", outbox)
351 j, err := GetJunk(outbox)
352 if err != nil {
353 log.Printf("error getting outbox: %s", err)
354 return
355 }
356 t, _ := j.GetString("type")
357 origin := originate(outbox)
358 if t == "OrderedCollection" {
359 items, _ := j.GetArray("orderedItems")
360 if items == nil {
361 items, _ = j.GetArray("items")
362 }
363 if items == nil {
364 obj, ok := j.GetMap("first")
365 if ok {
366 items, _ = obj.GetArray("orderedItems")
367 } else {
368 page1, ok := j.GetString("first")
369 if ok {
370 j, err = GetJunk(page1)
371 if err != nil {
372 log.Printf("error gettings page1: %s", err)
373 return
374 }
375 items, _ = j.GetArray("orderedItems")
376 }
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 xonksaver(user, obj, origin)
389 continue
390 }
391 xid, ok := item.(string)
392 if ok {
393 if !needxonkid(user, xid) {
394 continue
395 }
396 obj, err = GetJunk(xid)
397 if err != nil {
398 log.Printf("error getting item: %s", err)
399 continue
400 }
401 xonksaver(user, obj, originate(xid))
402 }
403 }
404 }
405}
406
407func newphone(a []string, obj junk.Junk) []string {
408 for _, addr := range []string{"to", "cc", "attributedTo"} {
409 who, _ := obj.GetString(addr)
410 if who != "" {
411 a = append(a, who)
412 }
413 whos, _ := obj.GetArray(addr)
414 for _, w := range whos {
415 who, _ := w.(string)
416 if who != "" {
417 a = append(a, who)
418 }
419 }
420 }
421 return a
422}
423
424func extractattrto(obj junk.Junk) string {
425 who, _ := obj.GetString("attributedTo")
426 if who != "" {
427 return who
428 }
429 o, ok := obj.GetMap("attributedTo")
430 if ok {
431 id, ok := o.GetString("id")
432 if ok {
433 return id
434 }
435 }
436 arr, _ := obj.GetArray("attributedTo")
437 for _, a := range arr {
438 o, ok := a.(junk.Junk)
439 if ok {
440 t, _ := o.GetString("type")
441 id, _ := o.GetString("id")
442 if t == "Person" || t == "" {
443 return id
444 }
445 }
446 s, ok := a.(string)
447 if ok {
448 return s
449 }
450 }
451 return ""
452}
453
454func firstofmany(obj junk.Junk, key string) string {
455 if val, _ := obj.GetString(key); val != "" {
456 return val
457 }
458 if arr, _ := obj.GetArray(key); len(arr) > 0 {
459 val, ok := arr[0].(string)
460 if ok {
461 return val
462 }
463 }
464 return ""
465}
466
467func xonksaver(user *WhatAbout, item junk.Junk, origin string) *Honk {
468 depth := 0
469 maxdepth := 10
470 currenttid := ""
471 goingup := 0
472 var xonkxonkfn func(item junk.Junk, origin string, isUpdate bool) *Honk
473
474 saveonemore := func(xid string) {
475 log.Printf("getting onemore: %s", xid)
476 if depth >= maxdepth {
477 log.Printf("in too deep")
478 return
479 }
480 obj, err := GetJunkHardMode(xid)
481 if err != nil {
482 log.Printf("error getting onemore: %s: %s", xid, err)
483 return
484 }
485 depth++
486 xonkxonkfn(obj, originate(xid), false)
487 depth--
488 }
489
490 xonkxonkfn = func(item junk.Junk, origin string, isUpdate bool) *Honk {
491 id, _ := item.GetString("id")
492 what := firstofmany(item, "type")
493 dt, ok := item.GetString("published")
494 if !ok {
495 dt = time.Now().Format(time.RFC3339)
496 }
497
498 var err error
499 var xid, rid, url, convoy string
500 var replies []string
501 var obj junk.Junk
502 switch what {
503 case "Delete":
504 obj, ok = item.GetMap("object")
505 if ok {
506 xid, _ = obj.GetString("id")
507 } else {
508 xid, _ = item.GetString("object")
509 }
510 if xid == "" {
511 return nil
512 }
513 if originate(xid) != origin {
514 log.Printf("forged delete: %s", xid)
515 return nil
516 }
517 log.Printf("eradicating %s", xid)
518 eradicatexonk(user.ID, xid)
519 return nil
520 case "Remove":
521 xid, _ = item.GetString("object")
522 targ, _ := obj.GetString("target")
523 log.Printf("remove %s from %s", obj, targ)
524 return nil
525 case "Tombstone":
526 xid, _ = item.GetString("id")
527 if xid == "" {
528 return nil
529 }
530 if originate(xid) != origin {
531 log.Printf("forged delete: %s", xid)
532 return nil
533 }
534 log.Printf("eradicating %s", xid)
535 eradicatexonk(user.ID, xid)
536 return nil
537 case "Announce":
538 obj, ok = item.GetMap("object")
539 if ok {
540 xid, _ = obj.GetString("id")
541 } else {
542 xid, _ = item.GetString("object")
543 }
544 if !needbonkid(user, xid) {
545 return nil
546 }
547 log.Printf("getting bonk: %s", xid)
548 obj, err = GetJunkHardMode(xid)
549 if err != nil {
550 log.Printf("error getting bonk: %s: %s", xid, err)
551 }
552 origin = originate(xid)
553 what = "bonk"
554 case "Update":
555 isUpdate = true
556 fallthrough
557 case "Create":
558 obj, ok = item.GetMap("object")
559 if !ok {
560 xid, _ = item.GetString("object")
561 log.Printf("getting created honk: %s", xid)
562 if originate(xid) != origin {
563 log.Printf("out of bounds %s not from %s", xid, origin)
564 return nil
565 }
566 obj, err = GetJunkHardMode(xid)
567 if err != nil {
568 log.Printf("error getting creation: %s", err)
569 }
570 }
571 if obj == nil {
572 log.Printf("no object for creation %s", id)
573 return nil
574 }
575 return xonkxonkfn(obj, origin, isUpdate)
576 case "Read":
577 xid, ok = item.GetString("object")
578 if ok {
579 if !needxonkid(user, xid) {
580 log.Printf("don't need read obj: %s", xid)
581 return nil
582 }
583 obj, err = GetJunkHardMode(xid)
584 if err != nil {
585 log.Printf("error getting read: %s", err)
586 return nil
587 }
588 return xonkxonkfn(obj, originate(xid), false)
589 }
590 return nil
591 case "Add":
592 xid, ok = item.GetString("object")
593 if ok {
594 // check target...
595 if !needxonkid(user, xid) {
596 log.Printf("don't need added obj: %s", xid)
597 return nil
598 }
599 obj, err = GetJunkHardMode(xid)
600 if err != nil {
601 log.Printf("error getting add: %s", err)
602 return nil
603 }
604 return xonkxonkfn(obj, originate(xid), false)
605 }
606 return nil
607 case "Move":
608 obj = item
609 what = "move"
610 case "Audio":
611 fallthrough
612 case "Image":
613 fallthrough
614 case "Video":
615 fallthrough
616 case "Question":
617 fallthrough
618 case "Note":
619 fallthrough
620 case "Article":
621 fallthrough
622 case "Page":
623 obj = item
624 what = "honk"
625 case "Event":
626 obj = item
627 what = "event"
628 case "ChatMessage":
629 obj = item
630 what = "chonk"
631 default:
632 log.Printf("unknown activity: %s", what)
633 dumpactivity(item)
634 return nil
635 }
636
637 if obj != nil {
638 xid, _ = obj.GetString("id")
639 }
640
641 if xid == "" {
642 log.Printf("don't know what xid is")
643 item.Write(os.Stdout)
644 return nil
645 }
646 if originate(xid) != origin {
647 log.Printf("original sin: %s not from %s", xid, origin)
648 item.Write(os.Stdout)
649 return nil
650 }
651
652 var xonk Honk
653 // early init
654 xonk.XID = xid
655 xonk.UserID = user.ID
656 xonk.Honker, _ = item.GetString("actor")
657 if xonk.Honker == "" {
658 xonk.Honker, _ = item.GetString("attributedTo")
659 }
660 if obj != nil {
661 if xonk.Honker == "" {
662 xonk.Honker = extractattrto(obj)
663 }
664 xonk.Oonker = extractattrto(obj)
665 if xonk.Oonker == xonk.Honker {
666 xonk.Oonker = ""
667 }
668 xonk.Audience = newphone(nil, obj)
669 }
670 xonk.Audience = append(xonk.Audience, xonk.Honker)
671 xonk.Audience = oneofakind(xonk.Audience)
672
673 var mentions []Mention
674 if obj != nil {
675 ot, _ := obj.GetString("type")
676 url, _ = obj.GetString("url")
677 if dt2, ok := obj.GetString("published"); ok {
678 dt = dt2
679 }
680 content, _ := obj.GetString("content")
681 if !strings.HasPrefix(content, "<p>") {
682 content = "<p>" + content
683 }
684 precis, _ := obj.GetString("summary")
685 if name, ok := obj.GetString("name"); ok {
686 if precis != "" {
687 content = precis + "<p>" + content
688 }
689 precis = html.EscapeString(name)
690 }
691 if sens, _ := obj["sensitive"].(bool); sens && precis == "" {
692 precis = "unspecified horror"
693 }
694 rid, ok = obj.GetString("inReplyTo")
695 if !ok {
696 if robj, ok := obj.GetMap("inReplyTo"); ok {
697 rid, _ = robj.GetString("id")
698 }
699 }
700 convoy, _ = obj.GetString("context")
701 if convoy == "" {
702 convoy, _ = obj.GetString("conversation")
703 }
704 if ot == "Question" {
705 if what == "honk" {
706 what = "qonk"
707 }
708 content += "<ul>"
709 ans, _ := obj.GetArray("oneOf")
710 for _, ai := range ans {
711 a, ok := ai.(junk.Junk)
712 if !ok {
713 continue
714 }
715 as, _ := a.GetString("name")
716 content += "<li>" + as
717 }
718 ans, _ = obj.GetArray("anyOf")
719 for _, ai := range ans {
720 a, ok := ai.(junk.Junk)
721 if !ok {
722 continue
723 }
724 as, _ := a.GetString("name")
725 content += "<li>" + as
726 }
727 content += "</ul>"
728 }
729 if ot == "Move" {
730 targ, _ := obj.GetString("target")
731 content += string(templates.Sprintf(`<p>Moved to <a href="%s">%s</a>`, targ, targ))
732 }
733 if what == "honk" && rid != "" {
734 what = "tonk"
735 }
736 if len(content) > 90001 {
737 log.Printf("content too long. truncating")
738 content = content[:90001]
739 }
740
741 xonk.Noise = content
742 xonk.Precis = precis
743 if rejectxonk(&xonk) {
744 log.Printf("fast reject: %s", xid)
745 return nil
746 }
747
748 numatts := 0
749 procatt := func(att junk.Junk) {
750 at, _ := att.GetString("type")
751 mt, _ := att.GetString("mediaType")
752 u, ok := att.GetString("url")
753 if !ok {
754 if ua, ok := att.GetArray("url"); ok && len(ua) > 0 {
755 u, ok = ua[0].(string)
756 if !ok {
757 if uu, ok := ua[0].(junk.Junk); ok {
758 u, _ = uu.GetString("href")
759 if mt == "" {
760 mt, _ = uu.GetString("mediaType")
761 }
762 }
763 }
764 } else if uu, ok := att.GetMap("url"); ok {
765 u, _ = uu.GetString("href")
766 if mt == "" {
767 mt, _ = uu.GetString("mediaType")
768 }
769 }
770 }
771 name, _ := att.GetString("name")
772 desc, _ := att.GetString("summary")
773 desc = html.UnescapeString(desc)
774 if desc == "" {
775 desc = name
776 }
777 localize := false
778 if numatts > 4 {
779 log.Printf("excessive attachment: %s", at)
780 } else if at == "Document" || at == "Image" {
781 mt = strings.ToLower(mt)
782 log.Printf("attachment: %s %s", mt, u)
783 if mt == "text/plain" || mt == "application/pdf" ||
784 strings.HasPrefix(mt, "image") {
785 localize = true
786 }
787 } else {
788 log.Printf("unknown attachment: %s", at)
789 }
790 if skipMedia(&xonk) {
791 localize = false
792 }
793 donk := savedonk(u, name, desc, mt, localize)
794 if donk != nil {
795 xonk.Donks = append(xonk.Donks, donk)
796 }
797 numatts++
798 }
799 atts, _ := obj.GetArray("attachment")
800 for _, atti := range atts {
801 att, ok := atti.(junk.Junk)
802 if !ok {
803 log.Printf("attachment that wasn't map?")
804 continue
805 }
806 procatt(att)
807 }
808 if att, ok := obj.GetMap("attachment"); ok {
809 procatt(att)
810 }
811 tags, _ := obj.GetArray("tag")
812 for _, tagi := range tags {
813 tag, ok := tagi.(junk.Junk)
814 if !ok {
815 continue
816 }
817 tt, _ := tag.GetString("type")
818 name, _ := tag.GetString("name")
819 desc, _ := tag.GetString("summary")
820 desc = html.UnescapeString(desc)
821 if desc == "" {
822 desc = name
823 }
824 if tt == "Emoji" {
825 icon, _ := tag.GetMap("icon")
826 mt, _ := icon.GetString("mediaType")
827 if mt == "" {
828 mt = "image/png"
829 }
830 u, _ := icon.GetString("url")
831 donk := savedonk(u, name, desc, mt, true)
832 if donk != nil {
833 xonk.Donks = append(xonk.Donks, donk)
834 }
835 }
836 if tt == "Hashtag" {
837 if name == "" || name == "#" {
838 // skip it
839 } else {
840 if name[0] != '#' {
841 name = "#" + name
842 }
843 xonk.Onts = append(xonk.Onts, name)
844 }
845 }
846 if tt == "Place" {
847 p := new(Place)
848 p.Name = name
849 p.Latitude, _ = tag.GetNumber("latitude")
850 p.Longitude, _ = tag.GetNumber("longitude")
851 p.Url, _ = tag.GetString("url")
852 xonk.Place = p
853 }
854 if tt == "Mention" {
855 var m Mention
856 m.Who, _ = tag.GetString("name")
857 m.Where, _ = tag.GetString("href")
858 mentions = append(mentions, m)
859 }
860 }
861 if starttime, ok := obj.GetString("startTime"); ok {
862 if start, err := time.Parse(time.RFC3339, starttime); err == nil {
863 t := new(Time)
864 t.StartTime = start
865 endtime, _ := obj.GetString("endTime")
866 t.EndTime, _ = time.Parse(time.RFC3339, endtime)
867 dura, _ := obj.GetString("duration")
868 if strings.HasPrefix(dura, "PT") {
869 dura = strings.ToLower(dura[2:])
870 d, _ := time.ParseDuration(dura)
871 t.Duration = Duration(d)
872 }
873 xonk.Time = t
874 }
875 }
876 if loca, ok := obj.GetMap("location"); ok {
877 if tt, _ := loca.GetString("type"); tt == "Place" {
878 p := new(Place)
879 p.Name, _ = loca.GetString("name")
880 p.Latitude, _ = loca.GetNumber("latitude")
881 p.Longitude, _ = loca.GetNumber("longitude")
882 p.Url, _ = loca.GetString("url")
883 xonk.Place = p
884 }
885 }
886
887 xonk.Onts = oneofakind(xonk.Onts)
888 replyobj, ok := obj.GetMap("replies")
889 if ok {
890 items, ok := replyobj.GetArray("items")
891 if !ok {
892 first, ok := replyobj.GetMap("first")
893 if ok {
894 items, _ = first.GetArray("items")
895 }
896 }
897 for _, repl := range items {
898 s, ok := repl.(string)
899 if ok {
900 replies = append(replies, s)
901 }
902 }
903 }
904
905 }
906
907 if currenttid == "" {
908 currenttid = convoy
909 }
910
911 // init xonk
912 xonk.What = what
913 xonk.RID = rid
914 xonk.Date, _ = time.Parse(time.RFC3339, dt)
915 xonk.URL = url
916 xonk.Format = "html"
917 xonk.Convoy = convoy
918 xonk.Mentions = mentions
919 for _, m := range mentions {
920 if m.Where == user.URL {
921 xonk.Whofore = 1
922 }
923 }
924 imaginate(&xonk)
925
926 if what == "chonk" {
927 ch := Chonk{
928 UserID: xonk.UserID,
929 XID: xid,
930 Who: xonk.Honker,
931 Target: xonk.Honker,
932 Date: xonk.Date,
933 Noise: xonk.Noise,
934 Format: xonk.Format,
935 Donks: xonk.Donks,
936 }
937 savechonk(&ch)
938 return nil
939 }
940
941 if isUpdate {
942 log.Printf("something has changed! %s", xonk.XID)
943 prev := getxonk(user.ID, xonk.XID)
944 if prev == nil {
945 log.Printf("didn't find old version for update: %s", xonk.XID)
946 isUpdate = false
947 } else {
948 xonk.ID = prev.ID
949 updatehonk(&xonk)
950 }
951 }
952 if !isUpdate && needxonk(user, &xonk) {
953 if rid != "" {
954 if needxonkid(user, rid) {
955 goingup++
956 saveonemore(rid)
957 goingup--
958 }
959 if convoy == "" {
960 xx := getxonk(user.ID, rid)
961 if xx != nil {
962 convoy = xx.Convoy
963 }
964 }
965 }
966 if convoy == "" {
967 convoy = currenttid
968 }
969 if convoy == "" {
970 convoy = "data:,missing-" + xfiltrate()
971 currenttid = convoy
972 }
973 xonk.Convoy = convoy
974 savexonk(&xonk)
975 }
976 if goingup == 0 {
977 for _, replid := range replies {
978 if needxonkid(user, replid) {
979 log.Printf("missing a reply: %s", replid)
980 saveonemore(replid)
981 }
982 }
983 }
984 return &xonk
985 }
986
987 return xonkxonkfn(item, origin, false)
988}
989
990func dumpactivity(item junk.Junk) {
991 fd, err := os.OpenFile("savedinbox.json", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
992 if err != nil {
993 log.Printf("error opening inbox! %s", err)
994 return
995 }
996 defer fd.Close()
997 item.Write(fd)
998 io.WriteString(fd, "\n")
999}
1000
1001func rubadubdub(user *WhatAbout, req junk.Junk) {
1002 actor, _ := req.GetString("actor")
1003 j := junk.New()
1004 j["@context"] = itiswhatitis
1005 j["id"] = user.URL + "/dub/" + xfiltrate()
1006 j["type"] = "Accept"
1007 j["actor"] = user.URL
1008 j["to"] = actor
1009 j["published"] = time.Now().UTC().Format(time.RFC3339)
1010 j["object"] = req
1011
1012 deliverate(0, user.ID, actor, j.ToBytes(), true)
1013}
1014
1015func itakeitallback(user *WhatAbout, xid string, owner string, folxid string) {
1016 j := junk.New()
1017 j["@context"] = itiswhatitis
1018 j["id"] = user.URL + "/unsub/" + folxid
1019 j["type"] = "Undo"
1020 j["actor"] = user.URL
1021 j["to"] = owner
1022 f := junk.New()
1023 f["id"] = user.URL + "/sub/" + folxid
1024 f["type"] = "Follow"
1025 f["actor"] = user.URL
1026 f["to"] = owner
1027 f["object"] = xid
1028 j["object"] = f
1029 j["published"] = time.Now().UTC().Format(time.RFC3339)
1030
1031 deliverate(0, user.ID, owner, j.ToBytes(), true)
1032}
1033
1034func subsub(user *WhatAbout, xid string, owner string, folxid string) {
1035 if xid == "" {
1036 log.Printf("can't subscribe to empty")
1037 return
1038 }
1039 j := junk.New()
1040 j["@context"] = itiswhatitis
1041 j["id"] = user.URL + "/sub/" + folxid
1042 j["type"] = "Follow"
1043 j["actor"] = user.URL
1044 j["to"] = owner
1045 j["object"] = xid
1046 j["published"] = time.Now().UTC().Format(time.RFC3339)
1047
1048 deliverate(0, user.ID, owner, j.ToBytes(), true)
1049}
1050
1051func activatedonks(donks []*Donk) []junk.Junk {
1052 var atts []junk.Junk
1053 for _, d := range donks {
1054 if re_emus.MatchString(d.Name) {
1055 continue
1056 }
1057 jd := junk.New()
1058 jd["mediaType"] = d.Media
1059 jd["name"] = d.Name
1060 jd["summary"] = html.EscapeString(d.Desc)
1061 jd["type"] = "Document"
1062 jd["url"] = d.URL
1063 atts = append(atts, jd)
1064 }
1065 return atts
1066}
1067
1068// returns activity, object
1069func jonkjonk(user *WhatAbout, h *Honk) (junk.Junk, junk.Junk) {
1070 dt := h.Date.Format(time.RFC3339)
1071 var jo junk.Junk
1072 j := junk.New()
1073 j["id"] = user.URL + "/" + h.What + "/" + shortxid(h.XID)
1074 j["actor"] = user.URL
1075 j["published"] = dt
1076 j["to"] = h.Audience[0]
1077 if len(h.Audience) > 1 {
1078 j["cc"] = h.Audience[1:]
1079 }
1080
1081 switch h.What {
1082 case "update":
1083 fallthrough
1084 case "tonk":
1085 fallthrough
1086 case "event":
1087 fallthrough
1088 case "honk":
1089 j["type"] = "Create"
1090 if h.What == "update" {
1091 j["type"] = "Update"
1092 }
1093
1094 jo = junk.New()
1095 jo["id"] = h.XID
1096 jo["type"] = "Note"
1097 if h.What == "event" {
1098 jo["type"] = "Event"
1099 }
1100 jo["published"] = dt
1101 jo["url"] = h.XID
1102 jo["attributedTo"] = user.URL
1103 if h.RID != "" {
1104 jo["inReplyTo"] = h.RID
1105 }
1106 if h.Convoy != "" {
1107 jo["context"] = h.Convoy
1108 jo["conversation"] = h.Convoy
1109 }
1110 jo["to"] = h.Audience[0]
1111 if len(h.Audience) > 1 {
1112 jo["cc"] = h.Audience[1:]
1113 }
1114 if !h.Public {
1115 jo["directMessage"] = true
1116 }
1117 translate(h)
1118 redoimages(h)
1119 if h.Precis != "" {
1120 jo["sensitive"] = true
1121 }
1122
1123 var replies []string
1124 for _, reply := range h.Replies {
1125 replies = append(replies, reply.XID)
1126 }
1127 if len(replies) > 0 {
1128 jr := junk.New()
1129 jr["type"] = "Collection"
1130 jr["totalItems"] = len(replies)
1131 jr["items"] = replies
1132 jo["replies"] = jr
1133 }
1134
1135 var tags []junk.Junk
1136 for _, m := range h.Mentions {
1137 t := junk.New()
1138 t["type"] = "Mention"
1139 t["name"] = m.Who
1140 t["href"] = m.Where
1141 tags = append(tags, t)
1142 }
1143 for _, o := range h.Onts {
1144 t := junk.New()
1145 t["type"] = "Hashtag"
1146 o = strings.ToLower(o)
1147 t["href"] = fmt.Sprintf("https://%s/o/%s", serverName, o[1:])
1148 t["name"] = o
1149 tags = append(tags, t)
1150 }
1151 for _, e := range herdofemus(h.Noise) {
1152 t := junk.New()
1153 t["id"] = e.ID
1154 t["type"] = "Emoji"
1155 t["name"] = e.Name
1156 i := junk.New()
1157 i["type"] = "Image"
1158 i["mediaType"] = "image/png"
1159 i["url"] = e.ID
1160 t["icon"] = i
1161 tags = append(tags, t)
1162 }
1163 for _, e := range fixupflags(h) {
1164 t := junk.New()
1165 t["id"] = e.ID
1166 t["type"] = "Emoji"
1167 t["name"] = e.Name
1168 i := junk.New()
1169 i["type"] = "Image"
1170 i["mediaType"] = "image/png"
1171 i["url"] = e.ID
1172 t["icon"] = i
1173 tags = append(tags, t)
1174 }
1175 if len(tags) > 0 {
1176 jo["tag"] = tags
1177 }
1178 if p := h.Place; p != nil {
1179 t := junk.New()
1180 t["type"] = "Place"
1181 if p.Name != "" {
1182 t["name"] = p.Name
1183 }
1184 if p.Latitude != 0 {
1185 t["latitude"] = p.Latitude
1186 }
1187 if p.Longitude != 0 {
1188 t["longitude"] = p.Longitude
1189 }
1190 if p.Url != "" {
1191 t["url"] = p.Url
1192 }
1193 jo["location"] = t
1194 }
1195 if t := h.Time; t != nil {
1196 jo["startTime"] = t.StartTime.Format(time.RFC3339)
1197 if t.Duration != 0 {
1198 jo["duration"] = "PT" + strings.ToUpper(t.Duration.String())
1199 }
1200 }
1201 atts := activatedonks(h.Donks)
1202 if len(atts) > 0 {
1203 jo["attachment"] = atts
1204 }
1205 jo["summary"] = html.EscapeString(h.Precis)
1206 jo["content"] = h.Noise
1207 j["object"] = jo
1208 case "bonk":
1209 j["type"] = "Announce"
1210 if h.Convoy != "" {
1211 j["context"] = h.Convoy
1212 }
1213 j["object"] = h.XID
1214 case "unbonk":
1215 b := junk.New()
1216 b["id"] = user.URL + "/" + "bonk" + "/" + shortxid(h.XID)
1217 b["type"] = "Announce"
1218 b["actor"] = user.URL
1219 if h.Convoy != "" {
1220 b["context"] = h.Convoy
1221 }
1222 b["object"] = h.XID
1223 j["type"] = "Undo"
1224 j["object"] = b
1225 case "zonk":
1226 j["type"] = "Delete"
1227 j["object"] = h.XID
1228 case "ack":
1229 j["type"] = "Read"
1230 j["object"] = h.XID
1231 if h.Convoy != "" {
1232 j["context"] = h.Convoy
1233 }
1234 case "react":
1235 j["type"] = "EmojiReact"
1236 j["object"] = h.XID
1237 if h.Convoy != "" {
1238 j["context"] = h.Convoy
1239 }
1240 j["content"] = h.Noise
1241 case "deack":
1242 b := junk.New()
1243 b["id"] = user.URL + "/" + "ack" + "/" + shortxid(h.XID)
1244 b["type"] = "Read"
1245 b["actor"] = user.URL
1246 b["object"] = h.XID
1247 if h.Convoy != "" {
1248 b["context"] = h.Convoy
1249 }
1250 j["type"] = "Undo"
1251 j["object"] = b
1252 }
1253
1254 return j, jo
1255}
1256
1257var oldjonks = cache.New(cache.Options{Filler: func(xid string) ([]byte, bool) {
1258 row := stmtAnyXonk.QueryRow(xid)
1259 honk := scanhonk(row)
1260 if honk == nil || !honk.Public {
1261 return nil, true
1262 }
1263 user, _ := butwhatabout(honk.Username)
1264 rawhonks := gethonksbyconvoy(honk.UserID, honk.Convoy, 0)
1265 reversehonks(rawhonks)
1266 for _, h := range rawhonks {
1267 if h.RID == honk.XID && h.Public && (h.Whofore == 2 || h.IsAcked()) {
1268 honk.Replies = append(honk.Replies, h)
1269 }
1270 }
1271 donksforhonks([]*Honk{honk})
1272 _, j := jonkjonk(user, honk)
1273 j["@context"] = itiswhatitis
1274
1275 return j.ToBytes(), true
1276}, Limit: 128})
1277
1278func gimmejonk(xid string) ([]byte, bool) {
1279 var j []byte
1280 ok := oldjonks.Get(xid, &j)
1281 return j, ok
1282}
1283
1284func boxuprcpts(user *WhatAbout, addresses []string, useshared bool) map[string]bool {
1285 rcpts := make(map[string]bool)
1286 for _, a := range addresses {
1287 if a == "" || a == thewholeworld || a == user.URL || strings.HasSuffix(a, "/followers") {
1288 continue
1289 }
1290 if a[0] == '%' {
1291 rcpts[a] = true
1292 continue
1293 }
1294 var box *Box
1295 ok := boxofboxes.Get(a, &box)
1296 if ok && useshared && box.Shared != "" {
1297 rcpts["%"+box.Shared] = true
1298 } else {
1299 rcpts[a] = true
1300 }
1301 }
1302 return rcpts
1303}
1304
1305func chonkifymsg(user *WhatAbout, ch *Chonk) []byte {
1306 dt := ch.Date.Format(time.RFC3339)
1307 aud := []string{ch.Target}
1308
1309 jo := junk.New()
1310 jo["id"] = ch.XID
1311 jo["type"] = "ChatMessage"
1312 jo["published"] = dt
1313 jo["attributedTo"] = user.URL
1314 jo["to"] = aud
1315 jo["content"] = ch.HTML
1316 atts := activatedonks(ch.Donks)
1317 if len(atts) > 0 {
1318 jo["attachment"] = atts
1319 }
1320 var tags []junk.Junk
1321 for _, e := range herdofemus(ch.Noise) {
1322 t := junk.New()
1323 t["id"] = e.ID
1324 t["type"] = "Emoji"
1325 t["name"] = e.Name
1326 i := junk.New()
1327 i["type"] = "Image"
1328 i["mediaType"] = "image/png"
1329 i["url"] = e.ID
1330 t["icon"] = i
1331 tags = append(tags, t)
1332 }
1333 if len(tags) > 0 {
1334 jo["tag"] = tags
1335 }
1336
1337 j := junk.New()
1338 j["@context"] = itiswhatitis
1339 j["id"] = user.URL + "/" + "honk" + "/" + shortxid(ch.XID)
1340 j["type"] = "Create"
1341 j["actor"] = user.URL
1342 j["published"] = dt
1343 j["to"] = aud
1344 j["object"] = jo
1345
1346 return j.ToBytes()
1347}
1348
1349func sendchonk(user *WhatAbout, ch *Chonk) {
1350 msg := chonkifymsg(user, ch)
1351
1352 rcpts := make(map[string]bool)
1353 rcpts[ch.Target] = true
1354 for a := range rcpts {
1355 go deliverate(0, user.ID, a, msg, true)
1356 }
1357}
1358
1359func honkworldwide(user *WhatAbout, honk *Honk) {
1360 jonk, _ := jonkjonk(user, honk)
1361 jonk["@context"] = itiswhatitis
1362 msg := jonk.ToBytes()
1363
1364 rcpts := boxuprcpts(user, honk.Audience, honk.Public)
1365
1366 if honk.Public {
1367 for _, h := range getdubs(user.ID) {
1368 if h.XID == user.URL {
1369 continue
1370 }
1371 var box *Box
1372 ok := boxofboxes.Get(h.XID, &box)
1373 if ok && box.Shared != "" {
1374 rcpts["%"+box.Shared] = true
1375 } else {
1376 rcpts[h.XID] = true
1377 }
1378 }
1379 for _, f := range getbacktracks(honk.XID) {
1380 var box *Box
1381 ok := boxofboxes.Get(f, &box)
1382 if ok && box.Shared != "" {
1383 rcpts["%"+box.Shared] = true
1384 } else {
1385 rcpts[f] = true
1386 }
1387 }
1388 }
1389 for a := range rcpts {
1390 go deliverate(0, user.ID, a, msg, doesitmatter(honk.What))
1391 }
1392 if honk.Public && len(honk.Onts) > 0 {
1393 collectiveaction(honk)
1394 }
1395}
1396
1397func doesitmatter(what string) bool {
1398 switch what {
1399 case "ack":
1400 return false
1401 case "react":
1402 return false
1403 case "deack":
1404 return false
1405 }
1406 return true
1407}
1408
1409func collectiveaction(honk *Honk) {
1410 user := getserveruser()
1411 for _, ont := range honk.Onts {
1412 dubs := getnameddubs(serverUID, ont)
1413 if len(dubs) == 0 {
1414 continue
1415 }
1416 j := junk.New()
1417 j["@context"] = itiswhatitis
1418 j["type"] = "Add"
1419 j["id"] = user.URL + "/add/" + shortxid(ont+honk.XID)
1420 j["actor"] = user.URL
1421 j["object"] = honk.XID
1422 j["target"] = fmt.Sprintf("https://%s/o/%s", serverName, ont[1:])
1423 rcpts := make(map[string]bool)
1424 for _, dub := range dubs {
1425 var box *Box
1426 ok := boxofboxes.Get(dub.XID, &box)
1427 if ok && box.Shared != "" {
1428 rcpts["%"+box.Shared] = true
1429 } else {
1430 rcpts[dub.XID] = true
1431 }
1432 }
1433 msg := j.ToBytes()
1434 for a := range rcpts {
1435 go deliverate(0, user.ID, a, msg, false)
1436 }
1437 }
1438}
1439
1440func junkuser(user *WhatAbout) junk.Junk {
1441 j := junk.New()
1442 j["@context"] = itiswhatitis
1443 j["id"] = user.URL
1444 j["inbox"] = user.URL + "/inbox"
1445 j["outbox"] = user.URL + "/outbox"
1446 j["name"] = user.Display
1447 j["preferredUsername"] = user.Name
1448 j["summary"] = user.HTAbout
1449 var tags []junk.Junk
1450 for _, o := range user.Onts {
1451 t := junk.New()
1452 t["type"] = "Hashtag"
1453 o = strings.ToLower(o)
1454 t["href"] = fmt.Sprintf("https://%s/o/%s", serverName, o[1:])
1455 t["name"] = o
1456 tags = append(tags, t)
1457 }
1458 if len(tags) > 0 {
1459 j["tag"] = tags
1460 }
1461
1462 if user.ID > 0 {
1463 j["type"] = "Person"
1464 j["url"] = user.URL
1465 j["followers"] = user.URL + "/followers"
1466 j["following"] = user.URL + "/following"
1467 a := junk.New()
1468 a["type"] = "Image"
1469 a["mediaType"] = "image/png"
1470 if ava := user.Options.Avatar; ava != "" {
1471 a["url"] = ava
1472 } else {
1473 u := fmt.Sprintf("https://%s/a?a=%s", serverName, url.QueryEscape(user.URL))
1474 if user.Options.Avahex {
1475 u += "&hex=1"
1476 }
1477 a["url"] = u
1478 }
1479 j["icon"] = a
1480 } else {
1481 j["type"] = "Service"
1482 }
1483 k := junk.New()
1484 k["id"] = user.URL + "#key"
1485 k["owner"] = user.URL
1486 k["publicKeyPem"] = user.Key
1487 j["publicKey"] = k
1488
1489 return j
1490}
1491
1492var oldjonkers = cache.New(cache.Options{Filler: func(name string) ([]byte, bool) {
1493 user, err := butwhatabout(name)
1494 if err != nil {
1495 return nil, false
1496 }
1497 var buf bytes.Buffer
1498 j := junkuser(user)
1499 j.Write(&buf)
1500 return buf.Bytes(), true
1501}, Duration: 1 * time.Minute})
1502
1503func asjonker(name string) ([]byte, bool) {
1504 var j []byte
1505 ok := oldjonkers.Get(name, &j)
1506 return j, ok
1507}
1508
1509var handfull = cache.New(cache.Options{Filler: func(name string) (string, bool) {
1510 m := strings.Split(name, "@")
1511 if len(m) != 2 {
1512 log.Printf("bad fish name: %s", name)
1513 return "", true
1514 }
1515 var href string
1516 row := stmtGetXonker.QueryRow(name, "fishname")
1517 err := row.Scan(&href)
1518 if err == nil {
1519 return href, true
1520 }
1521 log.Printf("fishing for %s", name)
1522 j, err := GetJunkFast(fmt.Sprintf("https://%s/.well-known/webfinger?resource=acct:%s", m[1], name))
1523 if err != nil {
1524 log.Printf("failed to go fish %s: %s", name, err)
1525 return "", true
1526 }
1527 links, _ := j.GetArray("links")
1528 for _, li := range links {
1529 l, ok := li.(junk.Junk)
1530 if !ok {
1531 continue
1532 }
1533 href, _ := l.GetString("href")
1534 rel, _ := l.GetString("rel")
1535 t, _ := l.GetString("type")
1536 if rel == "self" && friendorfoe(t) {
1537 when := time.Now().UTC().Format(dbtimeformat)
1538 _, err := stmtSaveXonker.Exec(name, href, "fishname", when)
1539 if err != nil {
1540 log.Printf("error saving fishname: %s", err)
1541 }
1542 return href, true
1543 }
1544 }
1545 return href, true
1546}, Duration: 1 * time.Minute})
1547
1548func gofish(name string) string {
1549 if name[0] == '@' {
1550 name = name[1:]
1551 }
1552 var href string
1553 handfull.Get(name, &href)
1554 return href
1555}
1556
1557func investigate(name string) (*SomeThing, error) {
1558 if name == "" {
1559 return nil, fmt.Errorf("no name")
1560 }
1561 if name[0] == '@' {
1562 name = gofish(name)
1563 }
1564 if name == "" {
1565 return nil, fmt.Errorf("no name")
1566 }
1567 obj, err := GetJunkFast(name)
1568 if err != nil {
1569 return nil, err
1570 }
1571 allinjest(originate(name), obj)
1572 return somethingabout(obj)
1573}
1574
1575func somethingabout(obj junk.Junk) (*SomeThing, error) {
1576 info := new(SomeThing)
1577 t, _ := obj.GetString("type")
1578 switch t {
1579 case "Person":
1580 fallthrough
1581 case "Organization":
1582 fallthrough
1583 case "Application":
1584 fallthrough
1585 case "Service":
1586 info.What = SomeActor
1587 case "OrderedCollection":
1588 fallthrough
1589 case "Collection":
1590 info.What = SomeCollection
1591 default:
1592 return nil, fmt.Errorf("unknown object type")
1593 }
1594 info.XID, _ = obj.GetString("id")
1595 info.Name, _ = obj.GetString("preferredUsername")
1596 if info.Name == "" {
1597 info.Name, _ = obj.GetString("name")
1598 }
1599 info.Owner, _ = obj.GetString("attributedTo")
1600 if info.Owner == "" {
1601 info.Owner = info.XID
1602 }
1603 return info, nil
1604}
1605
1606func allinjest(origin string, obj junk.Junk) {
1607 keyobj, ok := obj.GetMap("publicKey")
1608 if ok {
1609 ingestpubkey(origin, keyobj)
1610 }
1611 ingestboxes(origin, obj)
1612 ingesthandle(origin, obj)
1613}
1614
1615func ingestpubkey(origin string, obj junk.Junk) {
1616 keyobj, ok := obj.GetMap("publicKey")
1617 if ok {
1618 obj = keyobj
1619 }
1620 keyname, ok := obj.GetString("id")
1621 var data string
1622 row := stmtGetXonker.QueryRow(keyname, "pubkey")
1623 err := row.Scan(&data)
1624 if err == nil {
1625 return
1626 }
1627 if !ok || origin != originate(keyname) {
1628 log.Printf("bad key origin %s <> %s", origin, keyname)
1629 return
1630 }
1631 log.Printf("ingesting a needed pubkey: %s", keyname)
1632 owner, ok := obj.GetString("owner")
1633 if !ok {
1634 log.Printf("error finding %s pubkey owner", keyname)
1635 return
1636 }
1637 data, ok = obj.GetString("publicKeyPem")
1638 if !ok {
1639 log.Printf("error finding %s pubkey", keyname)
1640 return
1641 }
1642 if originate(owner) != origin {
1643 log.Printf("bad key owner: %s <> %s", owner, origin)
1644 return
1645 }
1646 _, _, err = httpsig.DecodeKey(data)
1647 if err != nil {
1648 log.Printf("error decoding %s pubkey: %s", keyname, err)
1649 return
1650 }
1651 when := time.Now().UTC().Format(dbtimeformat)
1652 _, err = stmtSaveXonker.Exec(keyname, data, "pubkey", when)
1653 if err != nil {
1654 log.Printf("error saving key: %s", err)
1655 }
1656}
1657
1658func ingestboxes(origin string, obj junk.Junk) {
1659 ident, _ := obj.GetString("id")
1660 if ident == "" {
1661 return
1662 }
1663 if originate(ident) != origin {
1664 return
1665 }
1666 var info string
1667 row := stmtGetXonker.QueryRow(ident, "boxes")
1668 err := row.Scan(&info)
1669 if err == nil {
1670 return
1671 }
1672 log.Printf("ingesting boxes: %s", ident)
1673 inbox, _ := obj.GetString("inbox")
1674 outbox, _ := obj.GetString("outbox")
1675 sbox, _ := obj.GetString("endpoints", "sharedInbox")
1676 if inbox != "" {
1677 when := time.Now().UTC().Format(dbtimeformat)
1678 m := strings.Join([]string{inbox, outbox, sbox}, " ")
1679 _, err = stmtSaveXonker.Exec(ident, m, "boxes", when)
1680 if err != nil {
1681 log.Printf("error saving boxes: %s", err)
1682 }
1683 }
1684}
1685
1686func ingesthandle(origin string, obj junk.Junk) {
1687 xid, _ := obj.GetString("id")
1688 if xid == "" {
1689 return
1690 }
1691 if originate(xid) != origin {
1692 return
1693 }
1694 var handle string
1695 row := stmtGetXonker.QueryRow(xid, "handle")
1696 err := row.Scan(&handle)
1697 if err == nil {
1698 return
1699 }
1700 handle, _ = obj.GetString("preferredUsername")
1701 if handle != "" {
1702 when := time.Now().UTC().Format(dbtimeformat)
1703 _, err = stmtSaveXonker.Exec(xid, handle, "handle", when)
1704 if err != nil {
1705 log.Printf("error saving handle: %s", err)
1706 }
1707 }
1708}
1709
1710func updateMe(username string) {
1711 var user *WhatAbout
1712 somenamedusers.Get(username, &user)
1713 dt := time.Now().UTC().Format(time.RFC3339)
1714 j := junk.New()
1715 j["@context"] = itiswhatitis
1716 j["id"] = fmt.Sprintf("%s/upme/%s/%d", user.URL, user.Name, time.Now().Unix())
1717 j["actor"] = user.URL
1718 j["published"] = dt
1719 j["to"] = thewholeworld
1720 j["type"] = "Update"
1721 j["object"] = junkuser(user)
1722
1723 msg := j.ToBytes()
1724
1725 rcpts := make(map[string]bool)
1726 for _, f := range getdubs(user.ID) {
1727 if f.XID == user.URL {
1728 continue
1729 }
1730 var box *Box
1731 boxofboxes.Get(f.XID, &box)
1732 if box != nil && box.Shared != "" {
1733 rcpts["%"+box.Shared] = true
1734 } else {
1735 rcpts[f.XID] = true
1736 }
1737 }
1738 for a := range rcpts {
1739 go deliverate(0, user.ID, a, msg, false)
1740 }
1741}
1742
1743func followme(user *WhatAbout, who string, name string, j junk.Junk) {
1744 folxid, _ := j.GetString("id")
1745
1746 log.Printf("updating honker follow: %s %s", who, folxid)
1747
1748 var x string
1749 db := opendatabase()
1750 row := db.QueryRow("select xid from honkers where name = ? and xid = ? and userid = ? and flavor in ('dub', 'undub')", name, who, user.ID)
1751 err := row.Scan(&x)
1752 if err != sql.ErrNoRows {
1753 log.Printf("duplicate follow request: %s", who)
1754 _, err = stmtUpdateFlavor.Exec("dub", folxid, user.ID, name, who, "undub")
1755 if err != nil {
1756 log.Printf("error updating honker: %s", err)
1757 }
1758 } else {
1759 stmtSaveDub.Exec(user.ID, name, who, "dub", folxid)
1760 }
1761 go rubadubdub(user, j)
1762}
1763
1764func unfollowme(user *WhatAbout, who string, name string, j junk.Junk) {
1765 var folxid string
1766 if who == "" {
1767 folxid, _ = j.GetString("object")
1768
1769 db := opendatabase()
1770 row := db.QueryRow("select xid, name from honkers where userid = ? and folxid = ? and flavor in ('dub', 'undub')", user.ID, folxid)
1771 err := row.Scan(&who, &name)
1772 if err != nil {
1773 if err != sql.ErrNoRows {
1774 log.Printf("error scanning honker: %s", err)
1775 }
1776 return
1777 }
1778 }
1779
1780 log.Printf("updating honker undo: %s %s", who, folxid)
1781 _, err := stmtUpdateFlavor.Exec("undub", folxid, user.ID, name, who, "dub")
1782 if err != nil {
1783 log.Printf("error updating honker: %s", err)
1784 return
1785 }
1786}
1787
1788func followyou(user *WhatAbout, honkerid int64) {
1789 var url, owner string
1790 db := opendatabase()
1791 row := db.QueryRow("select xid, owner from honkers where honkerid = ? and userid = ? and flavor in ('unsub', 'peep', 'presub', 'sub')",
1792 honkerid, user.ID)
1793 err := row.Scan(&url, &owner)
1794 if err != nil {
1795 log.Printf("can't get honker xid: %s", err)
1796 return
1797 }
1798 folxid := xfiltrate()
1799 log.Printf("subscribing to %s", url)
1800 _, err = db.Exec("update honkers set flavor = ?, folxid = ? where honkerid = ?", "presub", folxid, honkerid)
1801 if err != nil {
1802 log.Printf("error updating honker: %s", err)
1803 return
1804 }
1805 go subsub(user, url, owner, folxid)
1806
1807}
1808func unfollowyou(user *WhatAbout, honkerid int64) {
1809 db := opendatabase()
1810 row := db.QueryRow("select xid, owner, folxid from honkers where honkerid = ? and userid = ? and flavor in ('sub')",
1811 honkerid, user.ID)
1812 var url, owner, folxid string
1813 err := row.Scan(&url, &owner, &folxid)
1814 if err != nil {
1815 log.Printf("can't get honker xid: %s", err)
1816 return
1817 }
1818 log.Printf("unsubscribing from %s", url)
1819 _, err = db.Exec("update honkers set flavor = ? where honkerid = ?", "unsub", honkerid)
1820 if err != nil {
1821 log.Printf("error updating honker: %s", err)
1822 return
1823 }
1824 go itakeitallback(user, url, owner, folxid)
1825}
1826
1827func followyou2(user *WhatAbout, j junk.Junk) {
1828 who, _ := j.GetString("actor")
1829
1830 log.Printf("updating honker accept: %s", who)
1831 db := opendatabase()
1832 row := db.QueryRow("select name, folxid from honkers where userid = ? and xid = ? and flavor in ('presub')",
1833 user.ID, who)
1834 var name, folxid string
1835 err := row.Scan(&name, &folxid)
1836 if err != nil {
1837 log.Printf("can't get honker name: %s", err)
1838 return
1839 }
1840 _, err = stmtUpdateFlavor.Exec("sub", folxid, user.ID, name, who, "presub")
1841 if err != nil {
1842 log.Printf("error updating honker: %s", err)
1843 return
1844 }
1845}
1846
1847func nofollowyou2(user *WhatAbout, j junk.Junk) {
1848 who, _ := j.GetString("actor")
1849
1850 log.Printf("updating honker reject: %s", who)
1851 db := opendatabase()
1852 row := db.QueryRow("select name, folxid from honkers where userid = ? and xid = ? and flavor in ('presub', 'sub')",
1853 user.ID, who)
1854 var name, folxid string
1855 err := row.Scan(&name, &folxid)
1856 if err != nil {
1857 log.Printf("can't get honker name: %s", err)
1858 return
1859 }
1860 _, err = stmtUpdateFlavor.Exec("unsub", folxid, user.ID, name, who, "presub")
1861 _, err = stmtUpdateFlavor.Exec("unsub", folxid, user.ID, name, who, "sub")
1862 if err != nil {
1863 log.Printf("error updating honker: %s", err)
1864 return
1865 }
1866}