This repository has been archived by the owner on Feb 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsd_parse.c
408 lines (392 loc) · 10.8 KB
/
sd_parse.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
// Copyright (C) 2022 Valentin-Ioan VINTILA (313CA / 2021-2022).
// All rights reserved.
// Include the asscociated header file
#include "sd_parse.h"
// This function counts:
// > uppercase_letters
// > uppercase_letters_h
// > exclamation_marks
// > exclamation_marks_h
// > question_marks_h
// > asterisks
// > dollar_signs
// > dollar_signs_h
// > word_count
// > characters
// > punctuation_chars
// ... while also lowering the uppercase letters of a given email.
// - - - - -
// email_t *email = The email that will be parsed.
void sd_parse_count_and_lower_email(email_t *email)
{
email->uppercase_letters = 0;
email->uppercase_letters_h = 0;
email->exclamation_marks = 0;
email->exclamation_marks_h = 0;
email->question_marks_h = 0;
email->asterisks = 0;
email->dollar_signs = 0;
email->dollar_signs_h = 0;
email->word_count = 0;
email->characters = 0;
email->punctuation_chars = 0;
int i;
for (i = 0; i < email->content_n; ++i) {
if (email->content[i] == '*' &&
i + 1 < email->content_n && email->content[i + 1] == '*') {
++email->asterisks;
} else if (email->content[i] == '!') {
++email->exclamation_marks;
} else if (email->content[i] == '$' && i + 1 < email->content_n &&
email->content[i + 1] >= '0' &&
email->content[i + 1] <= '9') {
++email->dollar_signs;
} else if (email->content[i] >= 'A' && email->content[i] <= 'Z') {
++email->uppercase_letters;
email->content[i] += 32; // to lowercase
}
if (email->content[i] != ' ' && email->content[i] != '\n') {
++email->nonspace_count;
if (!isalnum(email->content[i]))
++email->punctuation_chars;
if (i && isalnum(email->content[i - 1]))
email->word_count++;
}
++email->characters;
}
if (i && isalnum(email->content[i - 1]))
email->word_count++;
for (i = 0; i < email->subject_n; ++i) {
if (email->subject[i] == '*' &&
i + 1 < email->subject_n && email->subject[i + 1] == '*') {
++email->asterisks;
} else if (email->subject[i] == '!') {
++email->exclamation_marks;
++email->exclamation_marks_h;
} else if (email->subject[i] == '?') {
++email->question_marks_h;
} else if (email->subject[i] == '$' && i + 1 < email->subject_n &&
email->subject[i + 1] >= '0' &&
email->subject[i + 1] <= '9') {
++email->dollar_signs;
++email->dollar_signs_h;
} else if (email->subject[i] >= 'A' && email->subject[i] <= 'Z') {
++email->uppercase_letters;
++email->uppercase_letters_h;
email->subject[i] += 32; // to lowercase
}
++email->characters;
}
}
// This function removes the excess spaces. It also makes the whole email just a
// long sentence (a single line). Words are recounted.
// - - - - -
// email_t *email = The email that will be parsed.
void sd_parse_spaces(email_t *email)
{
email->word_count = 0;
int j = 0, i;
bool was_an = false;
for (i = 0; i < email->content_n; ++i) {
if (isalnum(email->content[i])) {
email->content[j] = email->content[i];
++j;
was_an = true;
} else {
if (was_an) {
++email->word_count;
email->content[j] = ' ';
++j;
was_an = false;
}
}
}
email->word_count += was_an;
email->content[j] = '\0';
email->content_n = j;
for (was_an = false, j = 0, i = 0; i < email->subject_n; ++i) {
if (isalnum(email->subject[i])) {
email->subject[j] = email->subject[i];
++j;
was_an = true;
} else {
if (was_an) {
++email->word_count;
email->subject[j] = ' ';
++j;
was_an = false;
}
}
}
email->word_count += was_an;
if (!email->word_count) // defensive programming
email->word_count = 1;
email->subject[j] = '\0';
email->subject_n = j;
}
// This function removes all spaces and punctuation. It isn't currently used.
// - - - - -
// email_t *email = The email that will be parsed.
void sd_keep_nonspaces(email_t *email)
{
int j = 0, i;
for (i = 0; i < email->content_n; ++i) {
if (isalnum(email->content[i])) {
email->content[j] = email->content[i];
++j;
}
}
email->content[j] = '\0';
email->content_n = j;
}
// This function counts are removes all numbers in a given email.
// - - - - -
// email_t *email = The email that will be parsed.
void sd_parse_numbers(email_t *email)
{
email->numbers_long = 0;
email->numbers_short = 0;
email->numbers_money = 0;
static char buffer[BUFFER_SIZE];
bool money = false;
int at = 0, digits = 0;
for (int i = 0; i < email->content_n; ++i) {
buffer[at] = '\0';
if (email->content[i] == '$') {
money = true;
buffer[at] = email->content[i];
++at;
} else if (email->content[i] >= '0' && email->content[i] <= '9') {
buffer[at] = email->content[i];
++at;
++digits;
} else if (isalnum(email->content[i])) {
if (!money) {
if (digits >= 9) {
if (!strstr(buffer, "2002")) {
sd_log(LOG_DEBUG, "Found big: %s", buffer);
email->numbers_long++;
}
for (int j = i - at; j < i; ++j)
email->content[j] = ' ';
} else if (digits >= 3) {
email->numbers_short++;
}
} else if (digits >= 1) {
email->numbers_money++;
if (digits >= 3) {
for (int j = i - at; j < i; ++j)
email->content[j] = ' ';
}
}
at = 0;
money = false;
digits = 0;
} else if (digits >= 1 &&
((buffer[at - 1] < '0' || buffer[at - 1] > '9') ||
email->content[i] == ':')) {
if (!money) {
if (digits >= 9) {
if (buffer[0] != '2' || buffer[1] != '0' ||
buffer[2] != '0' || buffer[3] != '2') {
sd_log(LOG_DEBUG, "Found big: %s", buffer);
email->numbers_long++;
}
for (int j = i - at; j < i; ++j)
email->content[j] = ' ';
} else if (digits >= 3) {
email->numbers_short++;
}
} else if (digits >= 1) {
email->numbers_money++;
if (digits >= 3) {
for (int j = i - at; j < i; ++j)
email->content[j] = ' ';
}
}
at = 0;
money = false;
digits = 0;
} else if (digits >= 1) {
buffer[at] = email->content[i];
++at;
}
}
if (digits >= 9)
email->numbers_long++;
else if (digits >= 3)
email->numbers_short++;
if (digits >= 1 && money)
email->numbers_money++;
sd_log(LOG_DEBUG, "| N: Found %d long, %d short and %d money numbers",
email->numbers_long, email->numbers_short, email->numbers_money);
}
// This function clears a known email found using sd_parse_links(). It shouldn't
// really be used anywhere else, but it is present in .h anyway.
// - - - - -
// email_t *email = The email that will be parsed.
// int i = The current position in email->content.
// int len = The length of the prefix that has to be removed.
void sd_parse_clear_links(email_t *email, int i, int len)
{
while (len > 0) {
email->content[i - len] = ' ';
--len;
}
for (; i < email->content_n; ++i) {
if (!isalnum(email->content[i]) &&
!strchr("/$-_.+!?&*'(),=", email->content[i])) {
// stop here
break;
}
email->content[i] = ' ';
}
}
// I'VE HAD ENOUGH OF CS'S STUPID CONCERNS! YEAH, SOMETIMES A FUNCTION IS LONGER
// THAN 80 LINES OF CODE. THIS IS C, NOT PYTHON, SOMETIMES IT CAN'T JUST BE
// SHORTENED WITHOUT TRANSFORMING 81 LINES OF CODE INTO 881! STOP WITH THIS
// NONSENSE AT ONCE. ALSO, A FEW INDENTATIONS TRIGGER CS FOR NO GOOD REASON!
// ENOUGH! THIS IS WHAT YOU GET:
#define sd_parse_links_stupid_cs \
do { \
if (email->content[i] == '/') { \
if (in_link <= 2) \
++in_link; \
if (in_link == 3) { \
if (buffer[at - 1] != '/') { \
++email->found_links; \
++at; \
sd_parse_clear_links(email, i, at); \
} \
at = 0; \
in_link = 0; \
} \
} \
} while (0)
// This function counts all links and email addresses and removes them.
// - - - - -
// email_t *email = The email that will be parsed.
void sd_parse_links(email_t *email)
{
email->found_emails = 0;
email->found_links = 0;
static char buffer[BUFFER_SIZE];
int at = 0, dots = 0, in_link = 0, in_email = 0;
for (int i = 0; i < email->content_n; ++i) {
buffer[at] = '\0';
if (isalnum(email->content[i]) || strchr("_-.", email->content[i])) {
if (email->content[i] == '.') {
if (buffer[at - 1] == '.') {
buffer[0] = '.';
at = 1;
in_link = 0;
in_email = 0;
dots = 0;
}
if (in_email == 2) {
++email->found_emails;
sd_parse_clear_links(email, i, at);
in_email = 0;
at = 0;
}
++dots;
} else {
if (in_email == 1)
in_email++;
}
if (in_link == 1 || in_link == 2) { // http:a / http:/a
at = 0;
in_link = 0;
}
buffer[at] = email->content[i];
++at;
} else {
if (in_email) {
in_email = 0;
} else if (in_link) {
sd_parse_links_stupid_cs;
} else {
if (email->content[i] == '@') {
if (at && !strchr(".@-_", buffer[at - 1])) {
in_email = 1;
buffer[at] = email->content[i];
++at;
} else {
at = 0;
dots = 0;
}
} else if (email->content[i] == ':') {
if (!strcmp(buffer, "http") || !strcmp(buffer, "https")) {
in_link = 1;
buffer[at] = email->content[i];
++at;
}
} else if (email->content[i] == ' ' ||
email->content[i] == '\n' ||
email->content[i] == '/' ||
email->content[i] == '\"') {
if (dots >= 1 && at >= 5 &&
(!strcmp(buffer + at - 4, ".com") ||
!strcmp(buffer + at - 4, ".org") ||
!strcmp(buffer + at - 3, ".ru") ||
!strcmp(buffer + at - 3, ".ie") ||
!strcmp(buffer + at - 4, ".net") ||
!strcmp(buffer + at - 4, "o.uk"))) {
++email->found_links;
++at;
sd_parse_clear_links(email, i, at);
}
at = 0;
dots = 0;
} else {
at = 0;
in_email = 0;
in_link = 0;
dots = 0;
}
}
}
}
}
// YEAH, THAT'S RIGHT YOU DON'T GET TO USE IT - HAHAHA!
// WRITE YOUR OWN USELESS MACROS!
#undef sd_parse_links_stupid_cs
// This function tries to find hidden letters in an email's content
// - - - - -
// email_t *email = The email that will be parsed.
void sd_parse_hidden_letters_email(email_t *email)
{
int i;
for (i = 0; i < email->content_n; ++i) {
if (email->content[i] == '@' || email->content[i] == '4')
email->content[i] = 'a';
else if (email->content[i] == '8')
email->content[i] = 'b';
else if (email->content[i] == '3')
email->content[i] = 'e';
else if (email->content[i] == '1')
email->content[i] = 'i';
else if (email->content[i] == '0')
email->content[i] = 'o';
else if (email->content[i] == '5' || email->content[i] == '$')
email->content[i] = 's';
if (!isalnum(email->content[i]))
email->content[i] = ' ';
}
for (i = 0; i < email->subject_n; ++i) {
if (email->subject[i] == '@' || email->subject[i] == '4')
email->subject[i] = 'a';
else if (email->subject[i] == '8')
email->subject[i] = 'b';
else if (email->subject[i] == '3')
email->subject[i] = 'e';
else if (email->subject[i] == '1')
email->subject[i] = 'i';
else if (email->subject[i] == '0')
email->subject[i] = 'o';
else if (email->subject[i] == '5' || email->subject[i] == '$')
email->subject[i] = 's';
if (!isalnum(email->subject[i]))
email->subject[i] = ' ';
}
}