-
Notifications
You must be signed in to change notification settings - Fork 0
/
providers.clj
60 lines (57 loc) · 2.66 KB
/
providers.clj
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
(ns email-normalisation.providers
(:require [clojure.string :as string]))
(def providers
[{:provider "google"
:mx "^.*google.*\\.com\\.?$"
:domain-processor (fn [domain] (string/replace domain #"(google|gmail|googlemail).*$" "gmail.com"))
:username-processor (fn [username] (first (string/split (string/replace username "." "") #"\+")))}
{:provider "microsoft"
:mx "^.*outlook.*\\.com\\.?$"
:domain-processor (partial identity)
:username-processor (fn [username] (first (string/split username #"\+")))}
{:provider "fastmail"
:mx "^.*messagingengine.*\\.com\\.?$"
:domain-processor (partial identity)
:username-processor (fn [username] (first (string/split username #"\+")))}
{:provider "zoho"
:mx "^.*zohocorp.*\\.com\\.?$"
:domain-processor (partial identity)
:username-processor (fn [username] (first (string/split username #"\+")))}
{:provider "protonmail"
:mx "^.*protonmail.*\\.ch\\.?$"
:domain-processor (partial identity)
:username-processor (fn [username] (first (string/split username #"\+")))}
{:provider "rackspace"
:mx "^.*mailcontrol.*\\.com\\.?$"
:domain-processor (partial identity)
:username-processor (fn [username] (first (string/split username #"\+")))}
{:provider "runbox"
:mx "^.*runbox.*\\.com\\.?$"
:domain-processor (partial identity)
:username-processor (fn [username] (first (string/split username #"\+")))}
{:provider "icloud"
:mx "^.*icloud.*\\.com\\.?$"
:domain-processor (partial identity)
:username-processor (fn [username] (first (string/split username #"\+")))}
{:provider "yahoo"
:mx "^.*yahoodns.*\\.net\\.?$"
:domain-processor (partial identity)
:username-processor (fn [username] (first (string/split username #"\-")))}
{:provider "unknown"
:mx ".*"
:domain-processor (partial identity)
;assume a subset of allow characters as various email provider software (which doesnt have a fixed dns and is self hosted)
;allows any of these characters to be used for aliasing
:username-processor (fn [username] (first (string/split username #"\+|\=|\-|\#")))}])
(defn get-email-provider
"Get an email provider for the mx records associated with the domain"
[mx-records]
(let [mx-providers (map #(get % "data") mx-records)]
(first
(for [provider providers
:let [pattern (re-pattern (:mx provider))]
:when (some #(re-matches pattern %) mx-providers)] provider))))
(defn normalise-for-provider
"Normalise the email based on the matching provider rules"
[{:keys [username-processor domain-processor]} username domain]
(string/join "@" [(username-processor username) (domain-processor domain)]))