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