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