all repos — honk @ fa3d0d55470be7452a4f317c26634cedeb05d986

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