matchesFilter returns true if ev satisfies all criteria in f.
(ev event.Event, f MessageFilter)
| 364 | |
| 365 | // matchesFilter returns true if ev satisfies all criteria in f. |
| 366 | func matchesFilter(ev event.Event, f MessageFilter) bool { |
| 367 | if f.Since > 0 && ev.Timestamp < f.Since { |
| 368 | return false |
| 369 | } |
| 370 | if f.Until > 0 && ev.Timestamp > f.Until { |
| 371 | return false |
| 372 | } |
| 373 | |
| 374 | var email ParsedEmail |
| 375 | if err := json.Unmarshal(ev.Payload, &email); err != nil { |
| 376 | return false |
| 377 | } |
| 378 | |
| 379 | if f.To != "" && !matchAddresses(email.To, f.To) { |
| 380 | return false |
| 381 | } |
| 382 | if f.From != "" && !matchAddresses(email.From, f.From) { |
| 383 | return false |
| 384 | } |
| 385 | if f.Cc != "" && !matchAddresses(email.Cc, f.Cc) { |
| 386 | return false |
| 387 | } |
| 388 | if f.Subject != "" && email.Subject != f.Subject { |
| 389 | return false |
| 390 | } |
| 391 | if f.SubjectContains != "" && !strings.Contains(email.Subject, f.SubjectContains) { |
| 392 | return false |
| 393 | } |
| 394 | if f.SubjectRegex != "" { |
| 395 | re, err := regexp.Compile(f.SubjectRegex) |
| 396 | if err != nil || !re.MatchString(email.Subject) { |
| 397 | return false |
| 398 | } |
| 399 | } |
| 400 | if f.BodyContains != "" && |
| 401 | !strings.Contains(email.Text, f.BodyContains) && |
| 402 | !strings.Contains(email.HTML, f.BodyContains) { |
| 403 | return false |
| 404 | } |
| 405 | |
| 406 | return true |
| 407 | } |
| 408 | |
| 409 | func matchAddresses(addrs []EmailAddress, query string) bool { |
| 410 | lq := strings.ToLower(query) |