2019-04-13 19:58:42 +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 (
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
|
|
|
"fmt"
|
|
|
|
"html"
|
|
|
|
"html/template"
|
|
|
|
"log"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
2019-04-14 16:06:26 +02:00
|
|
|
"sync"
|
2019-04-13 19:58:42 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func reverbolate(honks []*Honk) {
|
|
|
|
for _, h := range honks {
|
|
|
|
h.What += "ed"
|
|
|
|
if h.Honker == "" {
|
|
|
|
h.Honker = "https://" + serverName + "/u/" + h.Username
|
|
|
|
if strings.IndexByte(h.XID, '/') == -1 {
|
|
|
|
h.URL = h.Honker + "/h/" + h.XID
|
|
|
|
} else {
|
|
|
|
h.URL = h.XID
|
|
|
|
}
|
2019-04-16 23:11:04 +02:00
|
|
|
if h.RID != "" && strings.IndexByte(h.RID, '/') == -1 {
|
|
|
|
h.RID = h.Honker + "/h/" + h.RID
|
|
|
|
}
|
2019-04-13 19:58:42 +02:00
|
|
|
} else {
|
|
|
|
idx := strings.LastIndexByte(h.Honker, '/')
|
|
|
|
if idx != -1 {
|
|
|
|
h.Username = honkerhandle(h.Honker)
|
|
|
|
} else {
|
|
|
|
h.Username = h.Honker
|
|
|
|
}
|
|
|
|
if h.URL == "" {
|
|
|
|
h.URL = h.XID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
zap := make(map[*Donk]bool)
|
|
|
|
h.HTML = cleanstring(h.Noise)
|
|
|
|
emuxifier := func(e string) string {
|
|
|
|
for _, d := range h.Donks {
|
|
|
|
if d.Name == e {
|
|
|
|
zap[d] = true
|
|
|
|
return fmt.Sprintf(`<img class="emu" title="%s" src="/d/%s">`, d.Name, d.XID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
h.HTML = template.HTML(re_emus.ReplaceAllStringFunc(string(h.HTML), emuxifier))
|
2019-04-17 04:36:05 +02:00
|
|
|
j := 0
|
2019-04-13 19:58:42 +02:00
|
|
|
for i := 0; i < len(h.Donks); i++ {
|
2019-04-17 04:36:05 +02:00
|
|
|
if !zap[h.Donks[i]] {
|
|
|
|
h.Donks[j] = h.Donks[i]
|
|
|
|
j++
|
2019-04-13 19:58:42 +02:00
|
|
|
}
|
|
|
|
}
|
2019-04-17 04:36:05 +02:00
|
|
|
h.Donks = h.Donks[:j]
|
2019-04-13 19:58:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func xfiltrate() string {
|
|
|
|
letters := "BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz1234567891234567891234"
|
|
|
|
for {
|
2019-04-19 17:32:01 +02:00
|
|
|
var b [18]byte
|
2019-04-13 19:58:42 +02:00
|
|
|
rand.Read(b[:])
|
|
|
|
for i, c := range b {
|
|
|
|
b[i] = letters[c&63]
|
|
|
|
}
|
|
|
|
s := string(b[:])
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Mention struct {
|
|
|
|
who string
|
|
|
|
where string
|
|
|
|
}
|
|
|
|
|
|
|
|
var re_mentions = regexp.MustCompile(`@[[:alnum:]]+@[[:alnum:].]+`)
|
2019-04-19 18:35:31 +02:00
|
|
|
var re_urltions = regexp.MustCompile(`@https://\S+`)
|
2019-04-13 19:58:42 +02:00
|
|
|
|
|
|
|
func grapevine(s string) []string {
|
|
|
|
var mentions []string
|
2019-04-19 18:35:31 +02:00
|
|
|
m := re_mentions.FindAllString(s, -1)
|
2019-04-13 19:58:42 +02:00
|
|
|
for i := range m {
|
|
|
|
where := gofish(m[i])
|
|
|
|
if where != "" {
|
|
|
|
mentions = append(mentions, where)
|
|
|
|
}
|
|
|
|
}
|
2019-04-19 18:35:31 +02:00
|
|
|
m = re_urltions.FindAllString(s, -1)
|
|
|
|
for i := range m {
|
|
|
|
mentions = append(mentions, m[i][1:])
|
|
|
|
}
|
2019-04-13 19:58:42 +02:00
|
|
|
return mentions
|
|
|
|
}
|
|
|
|
|
|
|
|
func bunchofgrapes(s string) []Mention {
|
|
|
|
m := re_mentions.FindAllString(s, -1)
|
|
|
|
var mentions []Mention
|
|
|
|
for i := range m {
|
|
|
|
where := gofish(m[i])
|
|
|
|
if where != "" {
|
|
|
|
mentions = append(mentions, Mention{who: m[i], where: where})
|
|
|
|
}
|
|
|
|
}
|
2019-04-19 18:35:31 +02:00
|
|
|
m = re_urltions.FindAllString(s, -1)
|
|
|
|
for i := range m {
|
|
|
|
mentions = append(mentions, Mention{who: m[i][1:], where: m[i][1:]})
|
|
|
|
}
|
2019-04-13 19:58:42 +02:00
|
|
|
return mentions
|
|
|
|
}
|
|
|
|
|
|
|
|
type Emu struct {
|
|
|
|
ID string
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
2019-04-19 18:35:31 +02:00
|
|
|
var re_link = regexp.MustCompile(`@?https?://[^\s"]+[\w/)]`)
|
2019-04-13 19:58:42 +02:00
|
|
|
var re_emus = regexp.MustCompile(`:[[:alnum:]_]+:`)
|
|
|
|
|
|
|
|
func herdofemus(noise string) []Emu {
|
|
|
|
m := re_emus.FindAllString(noise, -1)
|
|
|
|
m = oneofakind(m)
|
|
|
|
var emus []Emu
|
|
|
|
for _, e := range m {
|
|
|
|
fname := e[1 : len(e)-1]
|
|
|
|
url := fmt.Sprintf("https://%s/emu/%s.png", serverName, fname)
|
|
|
|
emus = append(emus, Emu{ID: url, Name: e})
|
|
|
|
}
|
|
|
|
return emus
|
|
|
|
}
|
|
|
|
|
|
|
|
func obfusbreak(s string) string {
|
|
|
|
s = strings.TrimSpace(s)
|
|
|
|
s = strings.Replace(s, "\r", "", -1)
|
|
|
|
s = html.EscapeString(s)
|
|
|
|
linkfn := func(url string) string {
|
2019-04-19 18:35:31 +02:00
|
|
|
if url[0] == '@' {
|
|
|
|
return url
|
|
|
|
}
|
2019-04-13 19:58:42 +02:00
|
|
|
addparen := false
|
|
|
|
adddot := false
|
|
|
|
if strings.HasSuffix(url, ")") && strings.IndexByte(url, '(') == -1 {
|
|
|
|
url = url[:len(url)-1]
|
|
|
|
addparen = true
|
|
|
|
}
|
|
|
|
if strings.HasSuffix(url, ".") {
|
|
|
|
url = url[:len(url)-1]
|
|
|
|
adddot = true
|
|
|
|
}
|
|
|
|
url = fmt.Sprintf(`<a href="%s">%s</a>`, url, url)
|
|
|
|
if adddot {
|
|
|
|
url += "."
|
|
|
|
}
|
|
|
|
if addparen {
|
|
|
|
url += ")"
|
|
|
|
}
|
|
|
|
return url
|
|
|
|
}
|
|
|
|
s = re_link.ReplaceAllStringFunc(s, linkfn)
|
|
|
|
|
|
|
|
s = strings.Replace(s, "\n", "<br>", -1)
|
|
|
|
s = re_mentions.ReplaceAllStringFunc(s, func(m string) string {
|
2019-04-16 05:48:01 +02:00
|
|
|
where := gofish(m)
|
|
|
|
if where == "" {
|
|
|
|
return m
|
|
|
|
}
|
2019-04-17 02:33:01 +02:00
|
|
|
who := m[0 : 1+strings.IndexByte(m[1:], '@')]
|
2019-04-16 22:10:51 +02:00
|
|
|
return fmt.Sprintf(`<span class="h-card"><a class="u-url mention" href="%s">%s</a></span>`,
|
|
|
|
html.EscapeString(where), html.EscapeString(who))
|
2019-04-13 19:58:42 +02:00
|
|
|
})
|
2019-04-19 18:35:31 +02:00
|
|
|
s = re_urltions.ReplaceAllStringFunc(s, func(m string) string {
|
|
|
|
return fmt.Sprintf(`<span class="h-card"><a class="u-url mention" href="%s">%s</a></span>`,
|
|
|
|
html.EscapeString(m[1:]), html.EscapeString(m))
|
|
|
|
})
|
2019-04-13 19:58:42 +02:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
var re_unurl = regexp.MustCompile("https://([^/]+).*/([^/]+)")
|
|
|
|
|
|
|
|
func honkerhandle(h string) string {
|
|
|
|
m := re_unurl.FindStringSubmatch(h)
|
|
|
|
if len(m) > 2 {
|
|
|
|
return fmt.Sprintf("%s@%s", m[2], m[1])
|
|
|
|
}
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
|
|
|
func prepend(s string, x []string) []string {
|
|
|
|
return append([]string{s}, x...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func oneofakind(a []string) []string {
|
|
|
|
var x []string
|
|
|
|
for n, s := range a {
|
|
|
|
if s != "" {
|
|
|
|
x = append(x, s)
|
|
|
|
for i := n + 1; i < len(a); i++ {
|
|
|
|
if a[i] == s {
|
|
|
|
a[i] = ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
|
2019-04-14 16:06:26 +02:00
|
|
|
var ziggies = make(map[string]*rsa.PrivateKey)
|
2019-04-14 19:20:56 +02:00
|
|
|
var zaggies = make(map[string]*rsa.PublicKey)
|
2019-04-14 16:06:26 +02:00
|
|
|
var ziggylock sync.Mutex
|
|
|
|
|
|
|
|
func ziggy(username string) (keyname string, key *rsa.PrivateKey) {
|
|
|
|
ziggylock.Lock()
|
|
|
|
key = ziggies[username]
|
2019-04-14 19:20:56 +02:00
|
|
|
ziggylock.Unlock()
|
2019-04-14 16:06:26 +02:00
|
|
|
if key == nil {
|
|
|
|
db := opendatabase()
|
|
|
|
row := db.QueryRow("select seckey from users where username = ?", username)
|
|
|
|
var data string
|
|
|
|
row.Scan(&data)
|
|
|
|
var err error
|
|
|
|
key, _, err = pez(data)
|
|
|
|
if err != nil {
|
2019-04-14 19:20:56 +02:00
|
|
|
log.Printf("error decoding %s seckey: %s", username, err)
|
2019-04-14 16:06:26 +02:00
|
|
|
return
|
|
|
|
}
|
2019-04-14 19:20:56 +02:00
|
|
|
ziggylock.Lock()
|
|
|
|
ziggies[username] = key
|
|
|
|
ziggylock.Unlock()
|
2019-04-13 19:58:42 +02:00
|
|
|
}
|
2019-04-14 16:06:26 +02:00
|
|
|
keyname = fmt.Sprintf("https://%s/u/%s#key", serverName, username)
|
2019-04-13 19:58:42 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func zaggy(keyname string) (key *rsa.PublicKey) {
|
2019-04-14 19:20:56 +02:00
|
|
|
ziggylock.Lock()
|
|
|
|
key = zaggies[keyname]
|
|
|
|
ziggylock.Unlock()
|
|
|
|
if key != nil {
|
|
|
|
return
|
|
|
|
}
|
2019-04-13 19:58:42 +02:00
|
|
|
db := opendatabase()
|
2019-04-19 17:08:22 +02:00
|
|
|
row := db.QueryRow("select pubkey from xonkers where xid = ?", keyname)
|
2019-04-13 19:58:42 +02:00
|
|
|
var data string
|
|
|
|
err := row.Scan(&data)
|
|
|
|
savekey := false
|
|
|
|
if err != nil {
|
2019-04-14 19:20:56 +02:00
|
|
|
log.Printf("hitting the webs for missing pubkey: %s", keyname)
|
2019-04-13 19:58:42 +02:00
|
|
|
j, err := GetJunk(keyname)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error getting %s pubkey: %s", keyname, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var ok bool
|
|
|
|
data, ok = jsonfindstring(j, []string{"publicKey", "publicKeyPem"})
|
|
|
|
if !ok {
|
|
|
|
log.Printf("error getting %s pubkey", keyname)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_, ok = jsonfindstring(j, []string{"publicKey", "owner"})
|
|
|
|
if !ok {
|
|
|
|
log.Printf("error getting %s pubkey owner", keyname)
|
|
|
|
return
|
|
|
|
}
|
2019-04-14 19:20:56 +02:00
|
|
|
savekey = true
|
2019-04-13 19:58:42 +02:00
|
|
|
}
|
|
|
|
_, key, err = pez(data)
|
|
|
|
if err != nil {
|
2019-04-14 19:20:56 +02:00
|
|
|
log.Printf("error decoding %s pubkey: %s", keyname, err)
|
2019-04-13 19:58:42 +02:00
|
|
|
return
|
|
|
|
}
|
2019-04-14 19:20:56 +02:00
|
|
|
ziggylock.Lock()
|
|
|
|
zaggies[keyname] = key
|
|
|
|
ziggylock.Unlock()
|
2019-04-13 19:58:42 +02:00
|
|
|
if savekey {
|
2019-04-19 17:08:22 +02:00
|
|
|
db.Exec("insert into xonkers (xid, ibox, obox, sbox, pubkey) values (?, ?, ?, ?, ?)",
|
|
|
|
keyname, "", "", "", data)
|
2019-04-13 19:58:42 +02:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func keymatch(keyname string, actor string) bool {
|
|
|
|
return strings.HasPrefix(keyname, actor)
|
|
|
|
}
|