all repos — honk @ 4383bba0663db4b587d2af9f4f49d1c742bf94bb

my fork of honk

fun.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	"crypto/rand"
 20	"crypto/rsa"
 21	"fmt"
 22	"html"
 23	"html/template"
 24	"log"
 25	"regexp"
 26	"strings"
 27	"sync"
 28)
 29
 30func reverbolate(honks []*Honk) {
 31	for _, h := range honks {
 32		h.What += "ed"
 33		if h.Honker == "" {
 34			h.Honker = "https://" + serverName + "/u/" + h.Username
 35			if strings.IndexByte(h.XID, '/') == -1 {
 36				h.URL = h.Honker + "/h/" + h.XID
 37			} else {
 38				h.URL = h.XID
 39			}
 40			if h.RID != "" && strings.IndexByte(h.RID, '/') == -1 {
 41				h.RID = h.Honker + "/h/" + h.RID
 42			}
 43		} else {
 44			idx := strings.LastIndexByte(h.Honker, '/')
 45			if idx != -1 {
 46				h.Username = honkerhandle(h.Honker)
 47			} else {
 48				h.Username = h.Honker
 49			}
 50			if h.URL == "" {
 51				h.URL = h.XID
 52			}
 53		}
 54		zap := make(map[*Donk]bool)
 55		h.HTML = cleanstring(h.Noise)
 56		emuxifier := func(e string) string {
 57			for _, d := range h.Donks {
 58				if d.Name == e {
 59					zap[d] = true
 60					return fmt.Sprintf(`<img class="emu" title="%s" src="/d/%s">`, d.Name, d.XID)
 61				}
 62			}
 63			return e
 64		}
 65		h.HTML = template.HTML(re_emus.ReplaceAllStringFunc(string(h.HTML), emuxifier))
 66		j := 0
 67		for i := 0; i < len(h.Donks); i++ {
 68			if !zap[h.Donks[i]] {
 69				h.Donks[j] = h.Donks[i]
 70				j++
 71			}
 72		}
 73		h.Donks = h.Donks[:j]
 74	}
 75}
 76
 77func xfiltrate() string {
 78	letters := "BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz1234567891234567891234"
 79	for {
 80		var b [18]byte
 81		rand.Read(b[:])
 82		for i, c := range b {
 83			b[i] = letters[c&63]
 84		}
 85		s := string(b[:])
 86		return s
 87	}
 88}
 89
 90type Mention struct {
 91	who   string
 92	where string
 93}
 94
 95var re_mentions = regexp.MustCompile(`@[[:alnum:]]+@[[:alnum:].]+`)
 96var re_urltions = regexp.MustCompile(`@https://\S+`)
 97
 98func grapevine(s string) []string {
 99	var mentions []string
100	m := re_mentions.FindAllString(s, -1)
101	for i := range m {
102		where := gofish(m[i])
103		if where != "" {
104			mentions = append(mentions, where)
105		}
106	}
107	m = re_urltions.FindAllString(s, -1)
108	for i := range m {
109		mentions = append(mentions, m[i][1:])
110	}
111	return mentions
112}
113
114func bunchofgrapes(s string) []Mention {
115	m := re_mentions.FindAllString(s, -1)
116	var mentions []Mention
117	for i := range m {
118		where := gofish(m[i])
119		if where != "" {
120			mentions = append(mentions, Mention{who: m[i], where: where})
121		}
122	}
123	m = re_urltions.FindAllString(s, -1)
124	for i := range m {
125		mentions = append(mentions, Mention{who: m[i][1:], where: m[i][1:]})
126	}
127	return mentions
128}
129
130type Emu struct {
131	ID   string
132	Name string
133}
134
135var re_link = regexp.MustCompile(`@?https?://[^\s"]+[\w/)]`)
136var re_emus = regexp.MustCompile(`:[[:alnum:]_]+:`)
137
138func herdofemus(noise string) []Emu {
139	m := re_emus.FindAllString(noise, -1)
140	m = oneofakind(m)
141	var emus []Emu
142	for _, e := range m {
143		fname := e[1 : len(e)-1]
144		url := fmt.Sprintf("https://%s/emu/%s.png", serverName, fname)
145		emus = append(emus, Emu{ID: url, Name: e})
146	}
147	return emus
148}
149
150func obfusbreak(s string) string {
151	s = strings.TrimSpace(s)
152	s = strings.Replace(s, "\r", "", -1)
153	s = html.EscapeString(s)
154	linkfn := func(url string) string {
155		if url[0] == '@' {
156			return url
157		}
158		addparen := false
159		adddot := false
160		if strings.HasSuffix(url, ")") && strings.IndexByte(url, '(') == -1 {
161			url = url[:len(url)-1]
162			addparen = true
163		}
164		if strings.HasSuffix(url, ".") {
165			url = url[:len(url)-1]
166			adddot = true
167		}
168		url = fmt.Sprintf(`<a href="%s">%s</a>`, url, url)
169		if adddot {
170			url += "."
171		}
172		if addparen {
173			url += ")"
174		}
175		return url
176	}
177	s = re_link.ReplaceAllStringFunc(s, linkfn)
178
179	s = strings.Replace(s, "\n", "<br>", -1)
180	s = re_mentions.ReplaceAllStringFunc(s, func(m string) string {
181		where := gofish(m)
182		if where == "" {
183			return m
184		}
185		who := m[0 : 1+strings.IndexByte(m[1:], '@')]
186		return fmt.Sprintf(`<span class="h-card"><a class="u-url mention" href="%s">%s</a></span>`,
187			html.EscapeString(where), html.EscapeString(who))
188	})
189	s = re_urltions.ReplaceAllStringFunc(s, func(m string) string {
190		return fmt.Sprintf(`<span class="h-card"><a class="u-url mention" href="%s">%s</a></span>`,
191			html.EscapeString(m[1:]), html.EscapeString(m))
192	})
193	return s
194}
195
196var re_unurl = regexp.MustCompile("https://([^/]+).*/([^/]+)")
197
198func honkerhandle(h string) string {
199	m := re_unurl.FindStringSubmatch(h)
200	if len(m) > 2 {
201		return fmt.Sprintf("%s@%s", m[2], m[1])
202	}
203	return h
204}
205
206func prepend(s string, x []string) []string {
207	return append([]string{s}, x...)
208}
209
210func oneofakind(a []string) []string {
211	var x []string
212	for n, s := range a {
213		if s != "" {
214			x = append(x, s)
215			for i := n + 1; i < len(a); i++ {
216				if a[i] == s {
217					a[i] = ""
218				}
219			}
220		}
221	}
222	return x
223}
224
225var ziggies = make(map[string]*rsa.PrivateKey)
226var zaggies = make(map[string]*rsa.PublicKey)
227var ziggylock sync.Mutex
228
229func ziggy(username string) (keyname string, key *rsa.PrivateKey) {
230	ziggylock.Lock()
231	key = ziggies[username]
232	ziggylock.Unlock()
233	if key == nil {
234		db := opendatabase()
235		row := db.QueryRow("select seckey from users where username = ?", username)
236		var data string
237		row.Scan(&data)
238		var err error
239		key, _, err = pez(data)
240		if err != nil {
241			log.Printf("error decoding %s seckey: %s", username, err)
242			return
243		}
244		ziggylock.Lock()
245		ziggies[username] = key
246		ziggylock.Unlock()
247	}
248	keyname = fmt.Sprintf("https://%s/u/%s#key", serverName, username)
249	return
250}
251
252func zaggy(keyname string) (key *rsa.PublicKey) {
253	ziggylock.Lock()
254	key = zaggies[keyname]
255	ziggylock.Unlock()
256	if key != nil {
257		return
258	}
259	db := opendatabase()
260	row := db.QueryRow("select pubkey from xonkers where xid = ?", keyname)
261	var data string
262	err := row.Scan(&data)
263	savekey := false
264	if err != nil {
265		log.Printf("hitting the webs for missing pubkey: %s", keyname)
266		j, err := GetJunk(keyname)
267		if err != nil {
268			log.Printf("error getting %s pubkey: %s", keyname, err)
269			return
270		}
271		var ok bool
272		data, ok = jsonfindstring(j, []string{"publicKey", "publicKeyPem"})
273		if !ok {
274			log.Printf("error getting %s pubkey", keyname)
275			return
276		}
277		_, ok = jsonfindstring(j, []string{"publicKey", "owner"})
278		if !ok {
279			log.Printf("error getting %s pubkey owner", keyname)
280			return
281		}
282		savekey = true
283	}
284	_, key, err = pez(data)
285	if err != nil {
286		log.Printf("error decoding %s pubkey: %s", keyname, err)
287		return
288	}
289	ziggylock.Lock()
290	zaggies[keyname] = key
291	ziggylock.Unlock()
292	if savekey {
293		db.Exec("insert into xonkers (xid, ibox, obox, sbox, pubkey) values (?, ?, ?, ?, ?)",
294			keyname, "", "", "", data)
295	}
296	return
297}
298
299func keymatch(keyname string, actor string) bool {
300	return strings.HasPrefix(keyname, actor)
301}