all repos — honk @ 5762f930ccf14b7f06fe306553ea616ed53570fb

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