2019-04-09 13:59:33 +02:00
|
|
|
//
|
|
|
|
// Copyright (c) 2019 Ted Unangst <tedu@tedunangst.com>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"compress/gzip"
|
|
|
|
"crypto/rsa"
|
|
|
|
"database/sql"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2019-04-29 04:28:59 +02:00
|
|
|
"html"
|
2019-04-26 23:01:17 +02:00
|
|
|
"image"
|
2019-04-09 13:59:33 +02:00
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func NewJunk() map[string]interface{} {
|
|
|
|
return make(map[string]interface{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func WriteJunk(w io.Writer, j map[string]interface{}) error {
|
|
|
|
e := json.NewEncoder(w)
|
|
|
|
e.SetEscapeHTML(false)
|
|
|
|
e.SetIndent("", " ")
|
|
|
|
err := e.Encode(j)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReadJunk(r io.Reader) (map[string]interface{}, error) {
|
|
|
|
decoder := json.NewDecoder(r)
|
|
|
|
var j map[string]interface{}
|
|
|
|
err := decoder.Decode(&j)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return j, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var theonetruename = `application/ld+json; profile="https://www.w3.org/ns/activitystreams"`
|
2019-04-23 21:58:13 +02:00
|
|
|
var thefakename = `application/activity+json`
|
2019-04-09 13:59:33 +02:00
|
|
|
var falsenames = []string{
|
|
|
|
`application/ld+json`,
|
|
|
|
`application/activity+json`,
|
|
|
|
}
|
|
|
|
var itiswhatitis = "https://www.w3.org/ns/activitystreams"
|
|
|
|
var thewholeworld = "https://www.w3.org/ns/activitystreams#Public"
|
|
|
|
|
|
|
|
func friendorfoe(ct string) bool {
|
|
|
|
ct = strings.ToLower(ct)
|
|
|
|
for _, at := range falsenames {
|
|
|
|
if strings.HasPrefix(ct, at) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func PostJunk(keyname string, key *rsa.PrivateKey, url string, j map[string]interface{}) error {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
WriteJunk(&buf, j)
|
2019-04-14 16:06:26 +02:00
|
|
|
return PostMsg(keyname, key, url, buf.Bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
func PostMsg(keyname string, key *rsa.PrivateKey, url string, msg []byte) error {
|
|
|
|
client := http.DefaultClient
|
|
|
|
req, err := http.NewRequest("POST", url, bytes.NewReader(msg))
|
2019-04-09 13:59:33 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
req.Header.Set("Content-Type", theonetruename)
|
2019-04-14 16:06:26 +02:00
|
|
|
zig(keyname, key, req, msg)
|
2019-04-09 13:59:33 +02:00
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-04-18 22:09:10 +02:00
|
|
|
resp.Body.Close()
|
|
|
|
switch resp.StatusCode {
|
|
|
|
case 200:
|
|
|
|
case 201:
|
|
|
|
case 202:
|
|
|
|
default:
|
2019-04-09 13:59:33 +02:00
|
|
|
return fmt.Errorf("http post status: %d", resp.StatusCode)
|
|
|
|
}
|
|
|
|
log.Printf("successful post: %s %d", url, resp.StatusCode)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type gzCloser struct {
|
|
|
|
r *gzip.Reader
|
|
|
|
under io.ReadCloser
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gz *gzCloser) Read(p []byte) (int, error) {
|
|
|
|
return gz.r.Read(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gz *gzCloser) Close() error {
|
|
|
|
defer gz.under.Close()
|
|
|
|
return gz.r.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetJunk(url string) (map[string]interface{}, error) {
|
|
|
|
client := http.DefaultClient
|
|
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-04-23 21:58:13 +02:00
|
|
|
at := thefakename
|
2019-04-15 16:27:36 +02:00
|
|
|
if strings.Contains(url, ".well-known/webfinger?resource") {
|
|
|
|
at = "application/jrd+json"
|
|
|
|
}
|
|
|
|
req.Header.Set("Accept", at)
|
2019-04-09 13:59:33 +02:00
|
|
|
req.Header.Set("Accept-Encoding", "gzip")
|
2019-04-28 23:30:32 +02:00
|
|
|
req.Header.Set("User-Agent", "honksnonk/5.0")
|
2019-04-09 13:59:33 +02:00
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
resp.Body.Close()
|
|
|
|
return nil, fmt.Errorf("http get status: %d", resp.StatusCode)
|
|
|
|
}
|
|
|
|
if strings.EqualFold(resp.Header.Get("Content-Encoding"), "gzip") {
|
|
|
|
gz, err := gzip.NewReader(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
resp.Body.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
resp.Body = &gzCloser{r: gz, under: resp.Body}
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
j, err := ReadJunk(resp.Body)
|
|
|
|
return j, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func jsonfindinterface(ii interface{}, keys []string) interface{} {
|
|
|
|
for _, key := range keys {
|
|
|
|
idx, err := strconv.Atoi(key)
|
|
|
|
if err == nil {
|
|
|
|
m := ii.([]interface{})
|
|
|
|
if idx >= len(m) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ii = m[idx]
|
|
|
|
} else {
|
|
|
|
m := ii.(map[string]interface{})
|
|
|
|
ii = m[key]
|
|
|
|
if ii == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ii
|
|
|
|
}
|
|
|
|
func jsonfindstring(j interface{}, keys []string) (string, bool) {
|
|
|
|
s, ok := jsonfindinterface(j, keys).(string)
|
|
|
|
return s, ok
|
|
|
|
}
|
|
|
|
func jsonfindarray(j interface{}, keys []string) ([]interface{}, bool) {
|
|
|
|
a, ok := jsonfindinterface(j, keys).([]interface{})
|
|
|
|
return a, ok
|
|
|
|
}
|
|
|
|
func jsonfindmap(j interface{}, keys []string) (map[string]interface{}, bool) {
|
|
|
|
m, ok := jsonfindinterface(j, keys).(map[string]interface{})
|
|
|
|
return m, ok
|
|
|
|
}
|
|
|
|
func jsongetstring(j interface{}, key string) (string, bool) {
|
|
|
|
return jsonfindstring(j, []string{key})
|
|
|
|
}
|
|
|
|
func jsongetarray(j interface{}, key string) ([]interface{}, bool) {
|
|
|
|
return jsonfindarray(j, []string{key})
|
|
|
|
}
|
|
|
|
func jsongetmap(j interface{}, key string) (map[string]interface{}, bool) {
|
|
|
|
return jsonfindmap(j, []string{key})
|
|
|
|
}
|
|
|
|
|
|
|
|
func savedonk(url string, name, media string) *Donk {
|
|
|
|
var donk Donk
|
|
|
|
row := stmtFindFile.QueryRow(url)
|
|
|
|
err := row.Scan(&donk.FileID)
|
|
|
|
if err == nil {
|
|
|
|
return &donk
|
|
|
|
}
|
2019-04-15 04:53:26 +02:00
|
|
|
log.Printf("saving donk: %s", url)
|
2019-04-09 13:59:33 +02:00
|
|
|
if err != nil && err != sql.ErrNoRows {
|
2019-04-15 04:53:26 +02:00
|
|
|
log.Printf("error querying: %s", err)
|
2019-04-09 13:59:33 +02:00
|
|
|
}
|
|
|
|
resp, err := http.Get(url)
|
|
|
|
if err != nil {
|
2019-04-15 04:53:26 +02:00
|
|
|
log.Printf("error fetching %s: %s", url, err)
|
2019-04-09 13:59:33 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var buf bytes.Buffer
|
|
|
|
io.Copy(&buf, resp.Body)
|
|
|
|
|
|
|
|
xid := xfiltrate()
|
|
|
|
|
2019-04-26 23:01:17 +02:00
|
|
|
data := buf.Bytes()
|
|
|
|
if strings.HasPrefix(media, "image") {
|
|
|
|
img, format, err := image.Decode(&buf)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("unable to decode image: %s", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
data, format, err = vacuumwrap(img, format)
|
|
|
|
media = "image/" + format
|
|
|
|
}
|
|
|
|
res, err := stmtSaveFile.Exec(xid, name, url, media, data)
|
2019-04-09 13:59:33 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("error saving file %s: %s", url, err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
donk.FileID, _ = res.LastInsertId()
|
|
|
|
return &donk
|
|
|
|
}
|
|
|
|
|
2019-04-14 16:15:47 +02:00
|
|
|
func needxonk(user *WhatAbout, x *Honk) bool {
|
2019-04-25 07:18:39 +02:00
|
|
|
if x == nil {
|
2019-04-14 16:15:47 +02:00
|
|
|
return false
|
|
|
|
}
|
2019-04-14 19:38:05 +02:00
|
|
|
if x.What == "eradicate" {
|
|
|
|
return true
|
|
|
|
}
|
2019-04-26 21:11:24 +02:00
|
|
|
if thoudostbitethythumb(user.ID, x.Audience, x.XID) {
|
|
|
|
log.Printf("not saving thumb biter? %s", x.Honker)
|
2019-04-26 15:35:22 +02:00
|
|
|
return false
|
|
|
|
}
|
2019-04-25 07:18:39 +02:00
|
|
|
return needxonkid(user, x.XID)
|
|
|
|
}
|
|
|
|
func needxonkid(user *WhatAbout, xid string) bool {
|
|
|
|
if strings.HasPrefix(xid, user.URL+"/h/") {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
row := stmtFindXonk.QueryRow(user.ID, xid)
|
|
|
|
var id int64
|
|
|
|
err := row.Scan(&id)
|
2019-04-09 13:59:33 +02:00
|
|
|
if err == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if err != sql.ErrNoRows {
|
|
|
|
log.Printf("err querying xonk: %s", err)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-04-20 17:17:00 +02:00
|
|
|
func savexonk(user *WhatAbout, x *Honk) {
|
2019-04-09 13:59:33 +02:00
|
|
|
if x.What == "eradicate" {
|
|
|
|
log.Printf("eradicating %s by %s", x.RID, x.Honker)
|
2019-04-24 23:34:58 +02:00
|
|
|
_, err := stmtDeleteHonk.Exec(x.RID, x.Honker, user.ID)
|
2019-04-09 13:59:33 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("error eradicating: %s", err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
dt := x.Date.UTC().Format(dbtimeformat)
|
|
|
|
aud := strings.Join(x.Audience, " ")
|
2019-04-20 17:17:00 +02:00
|
|
|
whofore := 0
|
|
|
|
if strings.Contains(aud, user.URL) {
|
|
|
|
whofore = 1
|
|
|
|
}
|
|
|
|
res, err := stmtSaveHonk.Exec(x.UserID, x.What, x.Honker, x.XID, x.RID, dt, x.URL, aud,
|
|
|
|
x.Noise, x.Convoy, whofore)
|
2019-04-09 13:59:33 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("err saving xonk: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
x.ID, _ = res.LastInsertId()
|
|
|
|
for _, d := range x.Donks {
|
|
|
|
_, err = stmtSaveDonk.Exec(x.ID, d.FileID)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("err saving donk: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-16 20:40:23 +02:00
|
|
|
type Box struct {
|
2019-04-16 20:47:08 +02:00
|
|
|
In string
|
|
|
|
Out string
|
2019-04-16 20:40:23 +02:00
|
|
|
Shared string
|
|
|
|
}
|
|
|
|
|
|
|
|
var boxofboxes = make(map[string]*Box)
|
2019-04-09 13:59:33 +02:00
|
|
|
var boxlock sync.Mutex
|
2019-04-17 02:20:42 +02:00
|
|
|
var boxinglock sync.Mutex
|
2019-04-09 13:59:33 +02:00
|
|
|
|
2019-04-16 20:40:23 +02:00
|
|
|
func getboxes(ident string) (*Box, error) {
|
2019-04-09 13:59:33 +02:00
|
|
|
boxlock.Lock()
|
|
|
|
b, ok := boxofboxes[ident]
|
2019-04-14 19:23:27 +02:00
|
|
|
boxlock.Unlock()
|
2019-04-09 13:59:33 +02:00
|
|
|
if ok {
|
2019-04-16 20:40:23 +02:00
|
|
|
return b, nil
|
2019-04-09 13:59:33 +02:00
|
|
|
}
|
2019-04-17 02:20:42 +02:00
|
|
|
|
|
|
|
boxinglock.Lock()
|
|
|
|
defer boxinglock.Unlock()
|
|
|
|
|
|
|
|
boxlock.Lock()
|
|
|
|
b, ok = boxofboxes[ident]
|
|
|
|
boxlock.Unlock()
|
|
|
|
if ok {
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2019-04-29 04:48:46 +02:00
|
|
|
row := stmtGetBoxes.QueryRow(ident)
|
2019-04-20 17:38:16 +02:00
|
|
|
b = &Box{}
|
|
|
|
err := row.Scan(&b.In, &b.Out, &b.Shared)
|
2019-04-09 13:59:33 +02:00
|
|
|
if err != nil {
|
2019-04-20 17:38:16 +02:00
|
|
|
j, err := GetJunk(ident)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
inbox, _ := jsongetstring(j, "inbox")
|
|
|
|
outbox, _ := jsongetstring(j, "outbox")
|
|
|
|
sbox, _ := jsonfindstring(j, []string{"endpoints", "sharedInbox"})
|
|
|
|
b = &Box{In: inbox, Out: outbox, Shared: sbox}
|
|
|
|
if inbox != "" {
|
2019-04-29 04:48:46 +02:00
|
|
|
_, err = stmtSaveBoxes.Exec(ident, inbox, outbox, sbox, "")
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error saving boxes: %s", err)
|
|
|
|
}
|
2019-04-20 17:38:16 +02:00
|
|
|
}
|
2019-04-09 13:59:33 +02:00
|
|
|
}
|
2019-04-14 19:23:27 +02:00
|
|
|
boxlock.Lock()
|
2019-04-16 20:40:23 +02:00
|
|
|
boxofboxes[ident] = b
|
2019-04-14 19:23:27 +02:00
|
|
|
boxlock.Unlock()
|
2019-04-16 20:40:23 +02:00
|
|
|
return b, nil
|
2019-04-09 13:59:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func peeppeep() {
|
2019-04-19 17:50:35 +02:00
|
|
|
user, _ := butwhatabout("htest")
|
2019-04-09 13:59:33 +02:00
|
|
|
honkers := gethonkers(user.ID)
|
|
|
|
for _, f := range honkers {
|
|
|
|
if f.Flavor != "peep" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
log.Printf("getting updates: %s", f.XID)
|
2019-04-16 20:40:23 +02:00
|
|
|
box, err := getboxes(f.XID)
|
2019-04-09 13:59:33 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("error getting outbox: %s", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
log.Printf("getting outbox")
|
2019-04-16 20:40:23 +02:00
|
|
|
j, err := GetJunk(box.Out)
|
2019-04-09 13:59:33 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("err: %s", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
t, _ := jsongetstring(j, "type")
|
|
|
|
if t == "OrderedCollection" {
|
|
|
|
items, _ := jsongetarray(j, "orderedItems")
|
|
|
|
if items == nil {
|
|
|
|
page1, _ := jsongetstring(j, "first")
|
|
|
|
j, err = GetJunk(page1)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("err: %s", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
items, _ = jsongetarray(j, "orderedItems")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, item := range items {
|
2019-04-25 07:18:39 +02:00
|
|
|
xonk := xonkxonk(user, item)
|
|
|
|
if needxonk(user, xonk) {
|
2019-04-20 17:17:00 +02:00
|
|
|
savexonk(user, xonk)
|
2019-04-09 13:59:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-19 17:50:35 +02:00
|
|
|
func whosthere(xid string) ([]string, string) {
|
2019-04-11 16:44:50 +02:00
|
|
|
obj, err := GetJunk(xid)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error getting remote xonk: %s", err)
|
2019-04-19 17:50:35 +02:00
|
|
|
return nil, ""
|
2019-04-11 16:44:50 +02:00
|
|
|
}
|
2019-04-19 18:48:44 +02:00
|
|
|
convoy, _ := jsongetstring(obj, "context")
|
|
|
|
if convoy == "" {
|
|
|
|
convoy, _ = jsongetstring(obj, "conversation")
|
|
|
|
}
|
2019-04-19 17:50:35 +02:00
|
|
|
return newphone(nil, obj), convoy
|
2019-04-11 16:44:50 +02:00
|
|
|
}
|
|
|
|
|
2019-04-09 13:59:33 +02:00
|
|
|
func newphone(a []string, obj map[string]interface{}) []string {
|
|
|
|
for _, addr := range []string{"to", "cc", "attributedTo"} {
|
|
|
|
who, _ := jsongetstring(obj, addr)
|
|
|
|
if who != "" {
|
|
|
|
a = append(a, who)
|
|
|
|
}
|
|
|
|
whos, _ := jsongetarray(obj, addr)
|
|
|
|
for _, w := range whos {
|
|
|
|
who, _ := w.(string)
|
|
|
|
if who != "" {
|
|
|
|
a = append(a, who)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2019-04-25 07:18:39 +02:00
|
|
|
func xonkxonk(user *WhatAbout, item interface{}) *Honk {
|
2019-04-25 19:23:18 +02:00
|
|
|
depth := 0
|
|
|
|
maxdepth := 4
|
|
|
|
var xonkxonkfn func(item interface{}) *Honk
|
|
|
|
|
|
|
|
saveoneup := func(xid string) {
|
|
|
|
log.Printf("getting oneup: %s", xid)
|
|
|
|
if depth >= maxdepth {
|
|
|
|
log.Printf("in too deep")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
obj, err := GetJunk(xid)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error getting oneup: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
depth++
|
|
|
|
xonk := xonkxonkfn(obj)
|
|
|
|
if needxonk(user, xonk) {
|
|
|
|
xonk.UserID = user.ID
|
|
|
|
savexonk(user, xonk)
|
|
|
|
}
|
|
|
|
depth--
|
|
|
|
}
|
|
|
|
|
|
|
|
xonkxonkfn = func(item interface{}) *Honk {
|
|
|
|
// id, _ := jsongetstring(item, "id")
|
|
|
|
what, _ := jsongetstring(item, "type")
|
|
|
|
dt, _ := jsongetstring(item, "published")
|
|
|
|
|
|
|
|
var audience []string
|
|
|
|
var err error
|
|
|
|
var xid, rid, url, content, convoy string
|
|
|
|
var obj map[string]interface{}
|
|
|
|
var ok bool
|
|
|
|
switch what {
|
|
|
|
case "Announce":
|
|
|
|
xid, ok = jsongetstring(item, "object")
|
|
|
|
if ok {
|
|
|
|
if !needxonkid(user, xid) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
log.Printf("getting bonk: %s", xid)
|
|
|
|
obj, err = GetJunk(xid)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error regetting: %s", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
obj, _ = jsongetmap(item, "object")
|
2019-04-25 06:50:41 +02:00
|
|
|
}
|
2019-04-25 19:23:18 +02:00
|
|
|
what = "bonk"
|
|
|
|
case "Create":
|
2019-04-25 06:50:41 +02:00
|
|
|
obj, _ = jsongetmap(item, "object")
|
2019-04-25 19:23:18 +02:00
|
|
|
what = "honk"
|
|
|
|
case "Note":
|
|
|
|
obj = item.(map[string]interface{})
|
|
|
|
what = "honk"
|
|
|
|
case "Delete":
|
|
|
|
obj, _ = jsongetmap(item, "object")
|
|
|
|
rid, _ = jsongetstring(item, "object")
|
|
|
|
what = "eradicate"
|
|
|
|
default:
|
|
|
|
log.Printf("unknown activity: %s", what)
|
|
|
|
return nil
|
2019-04-09 13:59:33 +02:00
|
|
|
}
|
2019-04-25 19:23:18 +02:00
|
|
|
|
|
|
|
var xonk Honk
|
|
|
|
who, _ := jsongetstring(item, "actor")
|
|
|
|
if obj != nil {
|
|
|
|
if who == "" {
|
|
|
|
who, _ = jsongetstring(obj, "attributedTo")
|
2019-04-22 21:05:44 +02:00
|
|
|
}
|
2019-04-25 19:23:18 +02:00
|
|
|
ot, _ := jsongetstring(obj, "type")
|
|
|
|
url, _ = jsongetstring(obj, "url")
|
|
|
|
if ot == "Note" || ot == "Article" {
|
|
|
|
audience = newphone(audience, obj)
|
|
|
|
xid, _ = jsongetstring(obj, "id")
|
|
|
|
content, _ = jsongetstring(obj, "content")
|
|
|
|
summary, _ := jsongetstring(obj, "summary")
|
|
|
|
if !strings.HasPrefix(content, "<p>") {
|
|
|
|
content = "<p>" + content
|
|
|
|
}
|
|
|
|
if summary != "" {
|
|
|
|
content = "<p>summary: " + summary + content
|
|
|
|
}
|
|
|
|
rid, _ = jsongetstring(obj, "inReplyTo")
|
|
|
|
convoy, _ = jsongetstring(obj, "context")
|
|
|
|
if convoy == "" {
|
|
|
|
convoy, _ = jsongetstring(obj, "conversation")
|
|
|
|
}
|
|
|
|
if what == "honk" && rid != "" {
|
|
|
|
what = "tonk"
|
|
|
|
if needxonkid(user, rid) {
|
|
|
|
saveoneup(rid)
|
|
|
|
}
|
|
|
|
}
|
2019-04-13 19:21:32 +02:00
|
|
|
}
|
2019-04-25 19:23:18 +02:00
|
|
|
if ot == "Tombstone" {
|
|
|
|
rid, _ = jsongetstring(obj, "id")
|
2019-04-19 18:48:44 +02:00
|
|
|
}
|
2019-04-25 19:23:18 +02:00
|
|
|
atts, _ := jsongetarray(obj, "attachment")
|
|
|
|
for _, att := range atts {
|
|
|
|
at, _ := jsongetstring(att, "type")
|
|
|
|
mt, _ := jsongetstring(att, "mediaType")
|
|
|
|
u, _ := jsongetstring(att, "url")
|
|
|
|
name, _ := jsongetstring(att, "name")
|
|
|
|
if at == "Document" {
|
|
|
|
mt = strings.ToLower(mt)
|
|
|
|
log.Printf("attachment: %s %s", mt, u)
|
|
|
|
if mt == "image/jpeg" || mt == "image/png" ||
|
2019-04-26 21:46:55 +02:00
|
|
|
mt == "text/plain" {
|
2019-04-25 19:23:18 +02:00
|
|
|
donk := savedonk(u, name, mt)
|
|
|
|
if donk != nil {
|
|
|
|
xonk.Donks = append(xonk.Donks, donk)
|
|
|
|
}
|
2019-04-29 04:28:59 +02:00
|
|
|
} else {
|
|
|
|
u = html.EscapeString(u)
|
|
|
|
content += fmt.Sprintf(
|
|
|
|
`<p>External attachment: <a href="%s" rel=noreferrer>%s</a>`, u, u)
|
2019-04-25 19:23:18 +02:00
|
|
|
}
|
2019-04-29 04:28:59 +02:00
|
|
|
} else {
|
|
|
|
log.Printf("unknown attachment: %s", at)
|
2019-04-25 19:23:18 +02:00
|
|
|
}
|
2019-04-09 13:59:33 +02:00
|
|
|
}
|
2019-04-25 19:23:18 +02:00
|
|
|
tags, _ := jsongetarray(obj, "tag")
|
|
|
|
for _, tag := range tags {
|
|
|
|
tt, _ := jsongetstring(tag, "type")
|
|
|
|
name, _ := jsongetstring(tag, "name")
|
|
|
|
if tt == "Emoji" {
|
|
|
|
icon, _ := jsongetmap(tag, "icon")
|
|
|
|
mt, _ := jsongetstring(icon, "mediaType")
|
2019-04-25 20:18:33 +02:00
|
|
|
if mt == "" {
|
|
|
|
mt = "image/png"
|
|
|
|
}
|
2019-04-25 19:23:18 +02:00
|
|
|
u, _ := jsongetstring(icon, "url")
|
2019-04-09 13:59:33 +02:00
|
|
|
donk := savedonk(u, name, mt)
|
|
|
|
if donk != nil {
|
|
|
|
xonk.Donks = append(xonk.Donks, donk)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-25 19:23:18 +02:00
|
|
|
audience = append(audience, who)
|
2019-04-09 13:59:33 +02:00
|
|
|
|
2019-04-25 19:23:18 +02:00
|
|
|
audience = oneofakind(audience)
|
2019-04-09 13:59:33 +02:00
|
|
|
|
2019-04-26 15:35:22 +02:00
|
|
|
xonk.UserID = user.ID
|
2019-04-25 19:23:18 +02:00
|
|
|
xonk.What = what
|
|
|
|
xonk.Honker = who
|
|
|
|
xonk.XID = xid
|
|
|
|
xonk.RID = rid
|
|
|
|
xonk.Date, _ = time.Parse(time.RFC3339, dt)
|
|
|
|
xonk.URL = url
|
|
|
|
xonk.Noise = content
|
|
|
|
xonk.Audience = audience
|
|
|
|
xonk.Convoy = convoy
|
|
|
|
|
|
|
|
return &xonk
|
|
|
|
}
|
2019-04-09 13:59:33 +02:00
|
|
|
|
2019-04-25 19:23:18 +02:00
|
|
|
return xonkxonkfn(item)
|
2019-04-09 13:59:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func rubadubdub(user *WhatAbout, req map[string]interface{}) {
|
|
|
|
xid, _ := jsongetstring(req, "id")
|
|
|
|
reqactor, _ := jsongetstring(req, "actor")
|
|
|
|
j := NewJunk()
|
|
|
|
j["@context"] = itiswhatitis
|
|
|
|
j["id"] = user.URL + "/dub/" + xid
|
|
|
|
j["type"] = "Accept"
|
|
|
|
j["actor"] = user.URL
|
|
|
|
j["to"] = reqactor
|
|
|
|
j["published"] = time.Now().UTC().Format(time.RFC3339)
|
|
|
|
j["object"] = req
|
|
|
|
|
|
|
|
WriteJunk(os.Stdout, j)
|
|
|
|
|
|
|
|
actor, _ := jsongetstring(req, "actor")
|
2019-04-16 20:40:23 +02:00
|
|
|
box, err := getboxes(actor)
|
2019-04-09 13:59:33 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("can't get dub box: %s", err)
|
|
|
|
return
|
|
|
|
}
|
2019-04-14 16:06:26 +02:00
|
|
|
keyname, key := ziggy(user.Name)
|
2019-04-16 20:40:23 +02:00
|
|
|
err = PostJunk(keyname, key, box.In, j)
|
2019-04-09 13:59:33 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("can't rub a dub: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
stmtSaveDub.Exec(user.ID, actor, actor, "dub")
|
|
|
|
}
|
|
|
|
|
2019-04-26 21:31:42 +02:00
|
|
|
func itakeitallback(user *WhatAbout, xid string) error {
|
|
|
|
j := NewJunk()
|
|
|
|
j["@context"] = itiswhatitis
|
|
|
|
j["id"] = user.URL + "/unsub/" + xid
|
|
|
|
j["type"] = "Undo"
|
|
|
|
j["actor"] = user.URL
|
|
|
|
j["to"] = xid
|
|
|
|
f := NewJunk()
|
|
|
|
f["id"] = user.URL + "/sub/" + xid
|
|
|
|
f["type"] = "Follow"
|
|
|
|
f["actor"] = user.URL
|
|
|
|
f["to"] = xid
|
|
|
|
j["object"] = f
|
|
|
|
j["published"] = time.Now().UTC().Format(time.RFC3339)
|
|
|
|
|
|
|
|
box, err := getboxes(xid)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
keyname, key := ziggy(user.Name)
|
|
|
|
err = PostJunk(keyname, key, box.In, j)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-09 13:59:33 +02:00
|
|
|
func subsub(user *WhatAbout, xid string) {
|
|
|
|
j := NewJunk()
|
|
|
|
j["@context"] = itiswhatitis
|
|
|
|
j["id"] = user.URL + "/sub/" + xid
|
|
|
|
j["type"] = "Follow"
|
|
|
|
j["actor"] = user.URL
|
|
|
|
j["to"] = xid
|
|
|
|
j["object"] = xid
|
|
|
|
j["published"] = time.Now().UTC().Format(time.RFC3339)
|
|
|
|
|
2019-04-16 20:40:23 +02:00
|
|
|
box, err := getboxes(xid)
|
2019-04-09 13:59:33 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("can't send follow: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
WriteJunk(os.Stdout, j)
|
2019-04-14 16:06:26 +02:00
|
|
|
keyname, key := ziggy(user.Name)
|
2019-04-16 20:40:23 +02:00
|
|
|
err = PostJunk(keyname, key, box.In, j)
|
2019-04-09 13:59:33 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("failed to subsub: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func jonkjonk(user *WhatAbout, h *Honk) (map[string]interface{}, map[string]interface{}) {
|
|
|
|
dt := h.Date.Format(time.RFC3339)
|
|
|
|
var jo map[string]interface{}
|
|
|
|
j := NewJunk()
|
|
|
|
j["id"] = user.URL + "/" + h.What + "/" + h.XID
|
|
|
|
j["actor"] = user.URL
|
|
|
|
j["published"] = dt
|
|
|
|
j["to"] = h.Audience[0]
|
|
|
|
if len(h.Audience) > 1 {
|
|
|
|
j["cc"] = h.Audience[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
switch h.What {
|
2019-04-15 22:18:38 +02:00
|
|
|
case "zonk":
|
|
|
|
fallthrough
|
2019-04-09 13:59:33 +02:00
|
|
|
case "tonk":
|
|
|
|
fallthrough
|
|
|
|
case "honk":
|
|
|
|
j["type"] = "Create"
|
2019-04-15 22:18:38 +02:00
|
|
|
if h.What == "zonk" {
|
|
|
|
j["type"] = "Delete"
|
|
|
|
}
|
|
|
|
|
2019-04-09 13:59:33 +02:00
|
|
|
jo = NewJunk()
|
|
|
|
jo["id"] = user.URL + "/h/" + h.XID
|
|
|
|
jo["type"] = "Note"
|
2019-04-15 22:18:38 +02:00
|
|
|
if h.What == "zonk" {
|
|
|
|
jo["type"] = "Tombstone"
|
|
|
|
}
|
2019-04-09 13:59:33 +02:00
|
|
|
jo["published"] = dt
|
|
|
|
jo["url"] = user.URL + "/h/" + h.XID
|
|
|
|
jo["attributedTo"] = user.URL
|
|
|
|
if h.RID != "" {
|
|
|
|
jo["inReplyTo"] = h.RID
|
|
|
|
}
|
2019-04-19 17:50:35 +02:00
|
|
|
if h.Convoy != "" {
|
2019-04-19 18:48:44 +02:00
|
|
|
jo["context"] = h.Convoy
|
2019-04-19 17:50:35 +02:00
|
|
|
jo["conversation"] = h.Convoy
|
|
|
|
}
|
2019-04-09 13:59:33 +02:00
|
|
|
jo["to"] = h.Audience[0]
|
|
|
|
if len(h.Audience) > 1 {
|
|
|
|
jo["cc"] = h.Audience[1:]
|
|
|
|
}
|
2019-04-20 05:23:22 +02:00
|
|
|
jo["content"] = mentionize(h.Noise)
|
2019-04-15 03:38:47 +02:00
|
|
|
jo["summary"] = nil
|
2019-04-12 21:02:56 +02:00
|
|
|
var tags []interface{}
|
2019-04-09 13:59:33 +02:00
|
|
|
g := bunchofgrapes(h.Noise)
|
2019-04-12 21:02:56 +02:00
|
|
|
for _, m := range g {
|
|
|
|
t := NewJunk()
|
|
|
|
t["type"] = "Mention"
|
|
|
|
t["name"] = m.who
|
|
|
|
t["href"] = m.where
|
|
|
|
tags = append(tags, t)
|
|
|
|
}
|
|
|
|
herd := herdofemus(h.Noise)
|
|
|
|
for _, e := range herd {
|
|
|
|
t := NewJunk()
|
|
|
|
t["id"] = e.ID
|
|
|
|
t["type"] = "Emoji"
|
|
|
|
t["name"] = e.Name
|
|
|
|
i := NewJunk()
|
|
|
|
i["type"] = "Image"
|
|
|
|
i["mediaType"] = "image/png"
|
|
|
|
i["url"] = e.ID
|
|
|
|
t["icon"] = i
|
|
|
|
tags = append(tags, t)
|
|
|
|
}
|
|
|
|
if len(tags) > 0 {
|
2019-04-09 13:59:33 +02:00
|
|
|
jo["tag"] = tags
|
|
|
|
}
|
|
|
|
var atts []interface{}
|
|
|
|
for _, d := range h.Donks {
|
2019-04-13 18:32:24 +02:00
|
|
|
if re_emus.MatchString(d.Name) {
|
|
|
|
continue
|
|
|
|
}
|
2019-04-09 13:59:33 +02:00
|
|
|
jd := NewJunk()
|
|
|
|
jd["mediaType"] = d.Media
|
|
|
|
jd["name"] = d.Name
|
|
|
|
jd["type"] = "Document"
|
|
|
|
jd["url"] = d.URL
|
|
|
|
atts = append(atts, jd)
|
|
|
|
}
|
|
|
|
if len(atts) > 0 {
|
|
|
|
jo["attachment"] = atts
|
|
|
|
}
|
|
|
|
j["object"] = jo
|
|
|
|
case "bonk":
|
|
|
|
j["type"] = "Announce"
|
|
|
|
j["object"] = h.XID
|
|
|
|
}
|
|
|
|
|
|
|
|
return j, jo
|
|
|
|
}
|
|
|
|
|
|
|
|
func honkworldwide(user *WhatAbout, honk *Honk) {
|
|
|
|
jonk, _ := jonkjonk(user, honk)
|
|
|
|
jonk["@context"] = itiswhatitis
|
2019-04-14 16:06:26 +02:00
|
|
|
var buf bytes.Buffer
|
|
|
|
WriteJunk(&buf, jonk)
|
|
|
|
msg := buf.Bytes()
|
2019-04-17 02:14:24 +02:00
|
|
|
|
|
|
|
rcpts := make(map[string]bool)
|
|
|
|
for _, a := range honk.Audience {
|
|
|
|
if a != thewholeworld && a != user.URL && !strings.HasSuffix(a, "/followers") {
|
|
|
|
box, _ := getboxes(a)
|
|
|
|
if box != nil && box.Shared != "" {
|
|
|
|
rcpts["%"+box.Shared] = true
|
|
|
|
} else {
|
|
|
|
rcpts[a] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-09 13:59:33 +02:00
|
|
|
for _, f := range getdubs(user.ID) {
|
2019-04-16 20:40:23 +02:00
|
|
|
box, _ := getboxes(f.XID)
|
|
|
|
if box != nil && box.Shared != "" {
|
2019-04-16 20:47:08 +02:00
|
|
|
rcpts["%"+box.Shared] = true
|
2019-04-16 20:40:23 +02:00
|
|
|
} else {
|
|
|
|
rcpts[f.XID] = true
|
|
|
|
}
|
2019-04-09 13:59:33 +02:00
|
|
|
}
|
2019-04-14 16:06:26 +02:00
|
|
|
for a := range rcpts {
|
2019-04-17 02:22:16 +02:00
|
|
|
go deliverate(0, user.Name, a, msg)
|
2019-04-09 13:59:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func asjonker(user *WhatAbout) map[string]interface{} {
|
2019-04-13 19:58:42 +02:00
|
|
|
about := obfusbreak(user.About)
|
2019-04-09 13:59:33 +02:00
|
|
|
|
|
|
|
j := NewJunk()
|
|
|
|
j["@context"] = itiswhatitis
|
|
|
|
j["id"] = user.URL
|
|
|
|
j["type"] = "Person"
|
|
|
|
j["inbox"] = user.URL + "/inbox"
|
|
|
|
j["outbox"] = user.URL + "/outbox"
|
2019-04-25 06:35:19 +02:00
|
|
|
j["followers"] = user.URL + "/followers"
|
|
|
|
j["following"] = user.URL + "/following"
|
2019-04-09 13:59:33 +02:00
|
|
|
j["name"] = user.Display
|
|
|
|
j["preferredUsername"] = user.Name
|
2019-04-13 19:58:42 +02:00
|
|
|
j["summary"] = about
|
2019-04-09 13:59:33 +02:00
|
|
|
j["url"] = user.URL
|
|
|
|
a := NewJunk()
|
|
|
|
a["type"] = "icon"
|
|
|
|
a["mediaType"] = "image/png"
|
|
|
|
a["url"] = fmt.Sprintf("https://%s/a?a=%s", serverName, url.QueryEscape(user.URL))
|
|
|
|
j["icon"] = a
|
|
|
|
k := NewJunk()
|
|
|
|
k["id"] = user.URL + "#key"
|
|
|
|
k["owner"] = user.URL
|
|
|
|
k["publicKeyPem"] = user.Key
|
|
|
|
j["publicKey"] = k
|
|
|
|
|
|
|
|
return j
|
|
|
|
}
|