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