-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemail_filter.c
174 lines (142 loc) · 4.37 KB
/
email_filter.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netdb.h>
#include <regex.h>
#define MAX_EMAIL_LENGTH 256
#define MAX_DOMAIN_LENGTH 256
int validate_email(const char *email) {
if (strchr(email, '@') == NULL) {
return 0;
}
regex_t regex;
const char *pattern = "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}";
if (regcomp(®ex, pattern, REG_EXTENDED) != 0) {
fprintf(stderr, "Failed to compile regex pattern.\n");
return 0;
}
int match = regexec(®ex, email, 0, NULL, 0);
regfree(®ex);
if (match == 0)
return 1; // Valid email format
else
return 0; // Invalid email format
}
int check_email_existence(const char *email) {
char domain[MAX_DOMAIN_LENGTH];
sscanf(email, "%*[^@]@%255s", domain);
struct hostent *host = gethostbyname(domain);
if (host == NULL) {
printf("Faild to resolve MX record for domain: %s\n", domain);
return 0;
}
struct sockaddr_in sa;
memcpy(&sa.sin_addr, host->h_addr_list[0], sizeof(struct in_addr));
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1) {
printf("Faild to create socket.\n");
return 0;
}
sa.sin_family = AF_INET;
sa.sin_port = htons(25);
if (connect(sock, (struct sockaddr *)&sa, sizeof(sa)) != 0) {
printf("Failed to connect to mail server\n");
close(sock);
return 0;
}
char response[512];
if (recv(sock, response, sizeof(response) - 1, 0) == -1) {
printf("failed to receive response from mail server\n");
close(sock);
return 0;
}
char ehlo_command[512];
snprintf(ehlo_command, sizeof(ehlo_command), "EHLO %s\r\n", domain);
if (send(sock, ehlo_command, strlen(ehlo_command), 0) == -1) {
printf("Failed to send EHLO command\n");
close(sock);
return 0;
}
if (recv(sock, response, sizeof(response) - 1, 0) == -1) {
printf("failed to receive response from mail server\n");
close(sock);
return 0;
}
char mail_from_command[512];
snprintf(mail_from_command, sizeof(mail_from_command), "MAIL FROM: <%s>\r\n", email);
if (send(sock, mail_from_command, strlen(mail_from_command), 0) == -1) {
printf("Failed to send MAIL FROM command\n");
close(sock);
return 0;
}
if (recv(sock, response, sizeof(response) - 1, 0) == -1) {
printf("failed to receive response from mail server\n");
close(sock);
return 0;
}
const char *quit_command = "QUIT\r\n";
if (send(sock, quit_command, strlen(quit_command), 0) == -1) {
printf("Failed to send QUIT command\n");
close(sock);
return 0;
}
close(sock);
if (response[0] == '2' && response[1] == '5' && response[2] == '0') {
return 1;
} else {
return 0;
}
return 1;
}
void filter_emails(const char *filename) {
FILE *file = fopen(filename, "r");
if (file == NULL) {
fprintf(stderr,
"Error opening file: %s\n",
filename);
return;
}
FILE *validFile = fopen("valid_emails.txt", "w");
if (validFile == NULL) {
fprintf(stderr,
"Error creating valid_emails.txt\n");
fclose(file);
return;
}
FILE *invalidFile = fopen("invalid_emails.txt", "w");
if (invalidFile == NULL) {
fprintf(stderr,
"Error creating invalid_emails.txt\n");
fclose(file);
fclose(validFile);
return;
}
char email[MAX_EMAIL_LENGTH];
while (fgets(email, MAX_EMAIL_LENGTH, file) != NULL) {
// Remove trailing newline character, if present
email[strcspn(email, "\n")] = '\0';
if (validate_email(email)) {
if (check_email_existence(email)) {
fprintf(validFile, "%s\n", email);
} else {
fprintf(invalidFile, "%s\n", email);
}
} else {
fprintf(invalidFile, "%s\n", email);
}
}
fclose(file);
fclose(validFile);
fclose(invalidFile);
}
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("Usage: %s email_list.txt\n", argv[0]);
return 0;
}
filter_emails(argv[1]);
printf("Email filtering completed successfully.\n");
return 1;
}