-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemail_catcher.lex
292 lines (233 loc) · 8.4 KB
/
email_catcher.lex
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
%{
/******************************************************************************
* FILENAME : *
* email_catcher.lex *
* *
* DESCRIPTION : *
* Gets email addresses from a file and save them in different formats *
* *
* AUTHOR : *
* Copyright Luis Liñán (luislivilla at gmail.com) 2017 *
* *
* REPOSITORY : *
* [email protected]:lulivi/LEX_Email_Catcher.git *
* *
* LICENSE : *
* This program is free software: you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation, version 3. *
* *
* This program is distributed in the hope that it will be *
* useful, but WITHOUT ANY WARRANTY; without even the implied *
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR *
* PURPOSE. See the GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/> *
******************************************************************************/
/* ********************************* */
/* ********** Declarations ********* */
/* ********************************* */
#include <stdio.h>
#include <string.h>
#include <python2.7/Python.h>
#include <time.h>
#include <ctype.h> // for IsNumeric
#include <stdlib.h>
int TAM;
int next;
char ** mails;
FILE * fpy;
int i, j;
time_t current_time;
/*
* Returns true (non-zero) if character-string parameter represents a signed or
* unsigned floating-point number. Otherwise returns false (zero).
*
* See http://rosettacode.org/wiki/Determine_if_a_string_is_numeric#C
*/
int IsNumeric (const char * s);
/*
* Adds and item to "mails"
*/
void AddItem(char * new_element);
/*
* Removes the text before the param "delim" from "prefix_mail"
*/
void RemovePrefix(char * prefix_mail, char delim);
/*
* Replaces one substring for other in a given string
*/
void ReplaceStr(char *str, char const *orig, char const *rep);
/*
* Displays all elements from the array "mails"
*/
void DisplayMails();
/*
* Uses CPython to execute a python file for saving the mails to JSON format
*/
void WriteJSON();
/*
* Deletes "mails" pointer
*/
void FreeMails();
%}
/* local-part */
outspch ["!#$%&'*+-/=?^_`{|}~"]
out {outspch}|[a-zA-Z0-9]
out_dot "."{out}+
out_dot_1 {out}+{out_dot}*
quotspch ["(),:;<>@\[\]."]|"////"|"//\""|" "
quot \"[{quotspch}{outspch}a-zA-Z0-9]*\"
local (({out_dot_1}+".")?{quot}("."{out_dot_1}+)?)|{out_dot_1}+
/* domain */
aA0 [a-zA-Z0-9]
aA0_hyph_dot {aA0}+|"-"|"."
dom_hyph_dot {aA0}+{aA0_hyph_dot}*{aA0}+
n [0-9]
dom_ip "\[IPv6:"({aA0}*|":")+"\]"|"\["{n}+"."{n}+"."{n}+"."{n}+"\]"
domain {dom_hyph_dot}|{dom_ip}
/* address */
address {local}"@"{domain}
address_at {local}" at "{domain}
/* elements */
tag1 a|abbr|address|area|article|aside|audio|b|base|bdi|bdo
tag2 blockquote|body|br|button|canvas|caption|cite|code|col
tag3 colgroup|data|datalist|dd|del|details|dfn|dialog|div|dl
tag4 dt|em|embed|fieldset|figcaption|figure|footer|form|h1|h2
tag5 h3|h4|h5|h6|head|header|hgroup|hr|html|i|iframe|img|input
tag6 ins|kbd|keygen|label|legend|li|link|main|map|mark|menu
tag7 menuitem|meta|meter|nav|noscript|object|ol|optgroup
tag8 option|output|p|param|pre|progress|q|rb|rp|rt|rtc|ruby
tag9 s|samp|script|section|select|small|source|span|strong
tag10 style|sub|summary|sup|table|tbody|td|template|textarea
tag11 tfoot|th|thead|time|title|tr|track|u|ul|var|video|wbr
tag12 acronym|applet|basefont|big|center|dir|font|frame|frameset
tag13 isindex|noframes|strike|tt
tag (.*"<"({tag1}|{tag2}|{tag3}|{tag4}|{tag5}|{tag6}|{tag7}|{tag8}|{tag9}|{tag10}|{tag11}|{tag12}|{tag13})">")
%%
%{
/* ********************************* */
/* ********** Productions ********** */
/* ********************************* */
%}
{tag} {;}
^("mail="){address} {RemovePrefix(yytext, '='); AddItem(yytext);}
{address} {AddItem(yytext);}
{address_at} {ReplaceStr(yytext, " at ", "@"); AddItem(yytext);}
.|\n {;}
%%
/* ********************************* */
/* ******** Additional Code ******** */
/* ********************************* */
int main (int argc, char *argv[]) {
if (argc != 1){
printf("Arguments: %s", argv[0]);
exit(-1);
}
char input[255];
char output[255];
int json_choice = 0;
char json_string[10];
printf("Choose the input stream [ stdin(0) / path_to_file ]: ");
scanf("%s", &input);
if(IsNumeric(input)) strcpy(input, "stdin");
printf("Choose the output stream [ stdout(0) / path_to_file ]: ");
scanf("%s", &output);
if(IsNumeric(output)) strcpy(output, "stdout");
printf("Do you want the output to be in json format? [ no(0) / yes(1) ]: ");
scanf("%d", &json_choice);
if(!strcmp(input, "stdin")) yyin = stdin;
else
if(!(yyin = fopen(input, "r"))){
printf("Error opening input file.\n");
exit(-1);
}
if(strcmp(output, "stdout")){
if(!(yyout = fopen(output, "w"))){
printf("Error opening output file.\n");
exit(-1);
}
}
else yyout = stdout;
next = 0;
TAM = 5;
mails = (char**) malloc(20*sizeof(char*));
for(i=0; i<TAM; ++i) mails[i] = (char*) malloc(255*sizeof(char));
if(strcmp(input, "stdin")==0)
printf("Enter the text (Ctrl+D to finish):\n\n");
yylex ();
if(!strcmp(output, "stdout"))
fprintf(yyout, "\n\n");
if(next > 0){
if(json_choice){
AddItem(output);
WriteJSON();
}
else DisplayMails();
}
else{
printf("ERROR 404: EMAILS NOT FOUND.\n");
}
FreeMails();
return 0;
}
// ====================================
// Returns 0 if string is not a signed or unsigned floating-point number.
int IsNumeric (const char * s) {
if (s == NULL || *s == '\0' || isspace(*s))
return 0;
char * p;
strtod (s, &p);
return *p == '\0';
}
// ====================================
void AddItem(char *new_element){
if(next >= TAM){
TAM = TAM * sizeof * mails;
mails = (char**)realloc(mails, sizeof * mails * TAM);
for(i=next; i<TAM; ++i) mails[i] = (char*) malloc(255*sizeof(char));
}
strcpy(mails[next], new_element);
++next;
}
// ====================================
void RemovePrefix(char * prefix_mail, char delim){
char * substr = strchr(prefix_mail, delim);
strcpy(prefix_mail, substr+1);
}
// ====================================
void ReplaceStr(char *str, char const *orig, char const *rep){
static char buffer[4096];
char *p;
if((p = strstr(str, orig))){
strncpy(buffer, str, p-str); // Copy characters from 'str' start to 'orig' st$
buffer[p-str] = '\0';
sprintf(buffer+(p-str), "%s%s", rep, p+strlen(orig));
strcpy(str, buffer);
}
}
// ====================================
void DisplayMails(){
time_t rawtime;
struct tm * timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
fprintf(yyout, "%s\n\nE-Mails:\n", asctime (timeinfo) );
for(i=0; i<next; ++i) fprintf(yyout, "%s\n", mails[i]);
}
// ====================================
void WriteJSON(){
Py_SetProgramName("pySON");
Py_Initialize();
PySys_SetArgv(next, mails);
fpy = fopen("pySON.py","r");
PyRun_SimpleFile(fpy, "pySON.py");
fclose(fpy);
Py_Finalize();
}
// ====================================
void FreeMails(){
for(i=0; i<TAM; ++i) free(mails[i]);
free(mails);
}