all repos — honk @ 7ff15d164af060074fcf73ae3c54c516cf516fef

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