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:].]+`)
96
97func grapevine(s string) []string {
98 m := re_mentions.FindAllString(s, -1)
99 var mentions []string
100 for i := range m {
101 where := gofish(m[i])
102 if where != "" {
103 mentions = append(mentions, where)
104 }
105 }
106 return mentions
107}
108
109func bunchofgrapes(s string) []Mention {
110 m := re_mentions.FindAllString(s, -1)
111 var mentions []Mention
112 for i := range m {
113 where := gofish(m[i])
114 if where != "" {
115 mentions = append(mentions, Mention{who: m[i], where: where})
116 }
117 }
118 return mentions
119}
120
121type Emu struct {
122 ID string
123 Name string
124}
125
126var re_link = regexp.MustCompile(`https?://[^\s"]+[\w/)]`)
127var re_emus = regexp.MustCompile(`:[[:alnum:]_]+:`)
128
129func herdofemus(noise string) []Emu {
130 m := re_emus.FindAllString(noise, -1)
131 m = oneofakind(m)
132 var emus []Emu
133 for _, e := range m {
134 fname := e[1 : len(e)-1]
135 url := fmt.Sprintf("https://%s/emu/%s.png", serverName, fname)
136 emus = append(emus, Emu{ID: url, Name: e})
137 }
138 return emus
139}
140
141func obfusbreak(s string) string {
142 s = strings.TrimSpace(s)
143 s = strings.Replace(s, "\r", "", -1)
144 s = html.EscapeString(s)
145 linkfn := func(url string) string {
146 addparen := false
147 adddot := false
148 if strings.HasSuffix(url, ")") && strings.IndexByte(url, '(') == -1 {
149 url = url[:len(url)-1]
150 addparen = true
151 }
152 if strings.HasSuffix(url, ".") {
153 url = url[:len(url)-1]
154 adddot = true
155 }
156 url = fmt.Sprintf(`<a href="%s">%s</a>`, url, url)
157 if adddot {
158 url += "."
159 }
160 if addparen {
161 url += ")"
162 }
163 return url
164 }
165 s = re_link.ReplaceAllStringFunc(s, linkfn)
166
167 s = strings.Replace(s, "\n", "<br>", -1)
168 s = re_mentions.ReplaceAllStringFunc(s, func(m string) string {
169 where := gofish(m)
170 if where == "" {
171 return m
172 }
173 who := m[0 : 1+strings.IndexByte(m[1:], '@')]
174 return fmt.Sprintf(`<span class="h-card"><a class="u-url mention" href="%s">%s</a></span>`,
175 html.EscapeString(where), html.EscapeString(who))
176 })
177 return s
178}
179
180var re_unurl = regexp.MustCompile("https://([^/]+).*/([^/]+)")
181
182func honkerhandle(h string) string {
183 m := re_unurl.FindStringSubmatch(h)
184 if len(m) > 2 {
185 return fmt.Sprintf("%s@%s", m[2], m[1])
186 }
187 return h
188}
189
190func prepend(s string, x []string) []string {
191 return append([]string{s}, x...)
192}
193
194func oneofakind(a []string) []string {
195 var x []string
196 for n, s := range a {
197 if s != "" {
198 x = append(x, s)
199 for i := n + 1; i < len(a); i++ {
200 if a[i] == s {
201 a[i] = ""
202 }
203 }
204 }
205 }
206 return x
207}
208
209var ziggies = make(map[string]*rsa.PrivateKey)
210var zaggies = make(map[string]*rsa.PublicKey)
211var ziggylock sync.Mutex
212
213func ziggy(username string) (keyname string, key *rsa.PrivateKey) {
214 ziggylock.Lock()
215 key = ziggies[username]
216 ziggylock.Unlock()
217 if key == nil {
218 db := opendatabase()
219 row := db.QueryRow("select seckey from users where username = ?", username)
220 var data string
221 row.Scan(&data)
222 var err error
223 key, _, err = pez(data)
224 if err != nil {
225 log.Printf("error decoding %s seckey: %s", username, err)
226 return
227 }
228 ziggylock.Lock()
229 ziggies[username] = key
230 ziggylock.Unlock()
231 }
232 keyname = fmt.Sprintf("https://%s/u/%s#key", serverName, username)
233 return
234}
235
236func zaggy(keyname string) (key *rsa.PublicKey) {
237 ziggylock.Lock()
238 key = zaggies[keyname]
239 ziggylock.Unlock()
240 if key != nil {
241 return
242 }
243 db := opendatabase()
244 row := db.QueryRow("select pubkey from xonkers where xid = ?", keyname)
245 var data string
246 err := row.Scan(&data)
247 savekey := false
248 if err != nil {
249 log.Printf("hitting the webs for missing pubkey: %s", keyname)
250 j, err := GetJunk(keyname)
251 if err != nil {
252 log.Printf("error getting %s pubkey: %s", keyname, err)
253 return
254 }
255 var ok bool
256 data, ok = jsonfindstring(j, []string{"publicKey", "publicKeyPem"})
257 if !ok {
258 log.Printf("error getting %s pubkey", keyname)
259 return
260 }
261 _, ok = jsonfindstring(j, []string{"publicKey", "owner"})
262 if !ok {
263 log.Printf("error getting %s pubkey owner", keyname)
264 return
265 }
266 savekey = true
267 }
268 _, key, err = pez(data)
269 if err != nil {
270 log.Printf("error decoding %s pubkey: %s", keyname, err)
271 return
272 }
273 ziggylock.Lock()
274 zaggies[keyname] = key
275 ziggylock.Unlock()
276 if savekey {
277 db.Exec("insert into xonkers (xid, ibox, obox, sbox, pubkey) values (?, ?, ?, ?, ?)",
278 keyname, "", "", "", data)
279 }
280 return
281}
282
283func keymatch(keyname string, actor string) bool {
284 return strings.HasPrefix(keyname, actor)
285}