handleCodes extracts codes (e.g. OTP digits) from a message using a regex pattern. Default pattern matches 4–8 digit sequences.
(store event.Store)
| 243 | // handleCodes extracts codes (e.g. OTP digits) from a message using a regex pattern. |
| 244 | // Default pattern matches 4–8 digit sequences. |
| 245 | func handleCodes(store event.Store) http.HandlerFunc { |
| 246 | return func(w http.ResponseWriter, r *http.Request) { |
| 247 | pattern := r.URL.Query().Get("pattern") |
| 248 | if pattern == "" { |
| 249 | pattern = `\b\d{4,8}\b` |
| 250 | } |
| 251 | |
| 252 | re, err := regexp.Compile(pattern) |
| 253 | if err != nil { |
| 254 | smtpError(w, "invalid pattern: "+err.Error(), http.StatusBadRequest) |
| 255 | return |
| 256 | } |
| 257 | |
| 258 | uuid := r.PathValue("uuid") |
| 259 | ev, err := store.FindByUUID(r.Context(), uuid) |
| 260 | if err != nil { |
| 261 | smtpError(w, err.Error(), http.StatusInternalServerError) |
| 262 | return |
| 263 | } |
| 264 | if ev == nil || ev.Type != "smtp" { |
| 265 | smtpError(w, "message not found", http.StatusNotFound) |
| 266 | return |
| 267 | } |
| 268 | |
| 269 | var email ParsedEmail |
| 270 | if err := json.Unmarshal(ev.Payload, &email); err != nil { |
| 271 | smtpError(w, "failed to parse message", http.StatusInternalServerError) |
| 272 | return |
| 273 | } |
| 274 | |
| 275 | seen := make(map[string]bool) |
| 276 | var codes []string |
| 277 | for _, s := range []string{email.Text, stripHTMLTags(email.HTML)} { |
| 278 | for _, m := range re.FindAllString(s, -1) { |
| 279 | if !seen[m] { |
| 280 | seen[m] = true |
| 281 | codes = append(codes, m) |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | if codes == nil { |
| 286 | codes = []string{} |
| 287 | } |
| 288 | |
| 289 | smtpJSON(w, map[string]any{"data": codes, "pattern": pattern}) |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | // parseFilter extracts MessageFilter fields from query parameters. |
| 294 | func parseFilter(r *http.Request) MessageFilter { |
no test coverage detected