-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemail_validator.cpp
More file actions
68 lines (52 loc) · 2.17 KB
/
Copy pathemail_validator.cpp
File metadata and controls
68 lines (52 loc) · 2.17 KB
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
#include <iostream>
#include <string>
#include <regex>
#include <algorithm>
class Email_validator {
private:
std::regex name_pattern;
std::regex domain_pattern;
public:
Email_validator() {
name_pattern = std::regex("^[a-zA-Z0-9!#$'*+\\-/=?^_`{|}~.]+$");
domain_pattern = std::regex("^[a-zA-Z0-9.-]+$");
}
bool validate(const std::string& email) {
if (email.empty() || email.length() > 254) return false;
if (std::count(email.begin(), email.end(), '@') != 1) return false;
size_t spliting_the_whole_mail = email.find('@');
std::string name_part = email.substr(0, spliting_the_whole_mail);
std::string domain_part = email.substr(spliting_the_whole_mail + 1);
if (name_part.empty() || name_part.length() > 64) return false;
if (name_part.front() == '.' || name_part.back() == '.') return false;
if (domain_part.front() == '.' || domain_part.back() == '.') return false;
if (name_part.find("..") != std::string::npos) return false;
if (domain_part.find("..") != std::string::npos) return false;
bool is_quoted = false;
if (name_part.length() >= 2 && name_part.front() == '"' && name_part.back() == '"') {
is_quoted = true;
}
std::string name_test = is_quoted ? name_part.substr(1, name_part.length() - 2) : name_part;
if (!is_quoted && !std::regex_match(name_test, name_pattern)) return false;
if (!std::regex_match(domain_part, domain_pattern)) return false;
if (domain_part.find('.') == std::string::npos) return false;
return true;
}
};
int main(int argc, char* argv[]) {
// Check if an email was passed as an argument
if (argc < 2) {
std::cerr << "[-] Usage: ./validator <email_address>\n";
return 1;
}
Email_validator validator;
// Grab the exact string passed in the terminal
std::string email = argv[1];
bool isValid = validator.validate(email);
if (isValid) {
std::cout << "\033[92m[Valid]\033[0m " << email << "\n";
} else {
std::cout << "\033[91m[Invalid]\033[0m " << email << "\n";
}
return 0;
}