hfcs.go (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
// // Copyright (c) 2019 Ted Unangst <tedu@tedunangst.com> // // Permission to use, copy, modify, and distribute this software for any // purpose with or without fee is hereby granted, provided that the above // copyright notice and this permission notice appear in all copies. // // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR // ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. package main import ( "log" "net/http" "regexp" "sort" "time" ) type Filter struct { ID int64 `json:"-"` Actions []filtType `json:"-"` Name string Date time.Time Actor string `json:",omitempty"` IncludeAudience bool `json:",omitempty"` Text string `json:",omitempty"` re_text *regexp.Regexp IsAnnounce bool `json:",omitempty"` AnnounceOf string `json:",omitempty"` Reject bool `json:",omitempty"` SkipMedia bool `json:",omitempty"` Hide bool `json:",omitempty"` Collapse bool `json:",omitempty"` Rewrite string `json:",omitempty"` re_rewrite *regexp.Regexp Replace string `json:",omitempty"` } type filtType uint const ( filtNone filtType = iota filtAny filtReject filtSkipMedia filtHide filtCollapse filtRewrite ) var filtNames = []string{"None", "Any", "Reject", "SkipMedia", "Hide", "Collapse", "Rewrite"} func (ft filtType) String() string { return filtNames[ft] } type afiltermap map[filtType][]*Filter var filtcache = cacheNew(func(userid int64) (afiltermap, bool) { rows, err := stmtGetFilters.Query(userid) if err != nil { log.Printf("error querying filters: %s", err) return nil, false } defer rows.Close() filtmap := make(afiltermap) for rows.Next() { filt := new(Filter) var j string var filterid int64 err = rows.Scan(&filterid, &j) if err == nil { err = unjsonify(j, filt) } if err != nil { log.Printf("error scanning filter: %s", err) continue } if filt.Text != "" { filt.re_text, err = regexp.Compile("\\b(?i:" + filt.Text + ")\\b") if err != nil { log.Printf("error compiling filter text: %s", err) continue } } if filt.Rewrite != "" { filt.re_rewrite, err = regexp.Compile("\\b(?i:" + filt.Rewrite + ")\\b") if err != nil { log.Printf("error compiling filter rewrite: %s", err) continue } } filt.ID = filterid if filt.Reject { filt.Actions = append(filt.Actions, filtReject) filtmap[filtReject] = append(filtmap[filtReject], filt) } if filt.SkipMedia { filt.Actions = append(filt.Actions, filtSkipMedia) filtmap[filtSkipMedia] = append(filtmap[filtSkipMedia], filt) } if filt.Hide { filt.Actions = append(filt.Actions, filtHide) filtmap[filtHide] = append(filtmap[filtHide], filt) } if filt.Collapse { filt.Actions = append(filt.Actions, filtCollapse) filtmap[filtCollapse] = append(filtmap[filtCollapse], filt) } if filt.Rewrite != "" { filt.Actions = append(filt.Actions, filtRewrite) filtmap[filtRewrite] = append(filtmap[filtRewrite], filt) } filtmap[filtAny] = append(filtmap[filtAny], filt) } sorting := filtmap[filtAny] sort.Slice(filtmap[filtAny], func(i, j int) bool { return sorting[i].Name < sorting[j].Name }) return filtmap, true }) func getfilters(userid int64, scope filtType) []*Filter { var filtmap afiltermap ok := filtcache.Get(userid, &filtmap) if ok { return filtmap[scope] } return nil } func rejectorigin(userid int64, origin string) bool { if o := originate(origin); o != "" { origin = o } filts := getfilters(userid, filtReject) for _, f := range filts { if f.Actor == origin { log.Printf("rejecting origin: %s", origin) return true } } return false } func rejectactor(userid int64, actor string) bool { origin := originate(actor) filts := getfilters(userid, filtReject) for _, f := range filts { if f.Actor == actor || (origin != "" && f.Actor == origin) { log.Printf("rejecting actor: %s", actor) return true } } return false } func stealthmode(userid int64, r *http.Request) bool { agent := r.UserAgent() agent = originate(agent) if agent != "" { fake := rejectorigin(userid, agent) if fake { log.Printf("faking 404 for %s", agent) return true } } return false } func matchfilter(h *Honk, f *Filter) bool { match := true if match && f.Actor != "" { match = false if f.Actor == h.Honker || f.Actor == h.Oonker { match = true } if !match && (f.Actor == originate(h.Honker) || f.Actor == originate(h.Oonker) || f.Actor == originate(h.XID)) { match = true } if !match && f.IncludeAudience { for _, a := range h.Audience { if f.Actor == a || f.Actor == originate(a) { match = true break } } } } if match && f.IsAnnounce { match = false if (f.AnnounceOf == "" && h.Oonker != "") || f.AnnounceOf == h.Oonker || f.AnnounceOf == originate(h.Oonker) { match = true } } if match && f.Text != "" { match = false re := f.re_text if re.MatchString(h.Noise) || re.MatchString(h.Precis) { match = true } if !match { for _, d := range h.Donks { if re.MatchString(d.Desc) { match = true } } } } return match } func rejectxonk(xonk *Honk) bool { filts := getfilters(xonk.UserID, filtReject) for _, f := range filts { if matchfilter(xonk, f) { return true } } return false } func skipMedia(xonk *Honk) bool { filts := getfilters(xonk.UserID, filtSkipMedia) for _, f := range filts { if matchfilter(xonk, f) { return true } } return false } func unsee(userid int64, h *Honk) { filts := getfilters(userid, filtCollapse) for _, f := range filts { if matchfilter(h, f) { bad := f.Text if f.Actor != "" { bad = f.Actor } if h.Precis == "" { h.Precis = bad } h.Open = "" break } } filts = getfilters(userid, filtRewrite) for _, f := range filts { if matchfilter(h, f) { h.Noise = f.re_rewrite.ReplaceAllString(h.Noise, f.Replace) } } } func osmosis(honks []*Honk, userid int64) []*Honk { filts := getfilters(userid, filtHide) j := 0 outer: for _, h := range honks { for _, f := range filts { if matchfilter(h, f) { continue outer } } honks[j] = h j++ } honks = honks[0:j] return honks } |