all repos — honk @ fc18a40ac61b407d0749fbf6960993fa3334f587

my fork of honk

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