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