-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
681 lines (565 loc) · 26.9 KB
/
Form1.cs
File metadata and controls
681 lines (565 loc) · 26.9 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
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
using MailKit;
using MailKit.Net.Imap;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text.RegularExpressions;
namespace imap_extractor
{
public partial class EXTRACTOR : Form
{
Dictionary<string, Tuple<string, int>> ?data = null;
public EXTRACTOR()
{
InitializeComponent();
data = LoadCSVFile("./data.csv");
}
static async Task WriteFileAsync(string dir, string file, string content)
{
if (!Path.Exists(dir))
{
Directory.CreateDirectory(dir);
}
using StreamWriter outputFile = new(Path.Combine(dir, file));
await outputFile.WriteAsync(content);
}
public static string ExtractEmail(string text)
{
const string MatchEmailPattern =
@"(([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))@"
+ @"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\."
+ @"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
+ @"([a-zA-Z]+[\w-]+\.)+[a-zA-Z]{2,4})";
Regex rx = new Regex(MatchEmailPattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
MatchCollection matches = rx.Matches(text);
if (matches.Count > 0)
{
return matches[0].Value.ToString();
}
return string.Empty;
}
public Dictionary<string, Tuple<string, int>>? LoadCSVFile(string filePath)
{
try
{
// Open the file
using var reader = new StreamReader(filePath);
var result = new Dictionary<string, Tuple<string, int>>();
// Loop through the rows
string line;
while ((line = reader.ReadLine()) != null)
{
// Split the line by the semicolon delimiter
var fields = line.Split(';');
if (fields.Length != 3)
{
Console.WriteLine($"Row with incorrect number of columns found, it will be skipped.");
continue;
}
// Get the first field as the key
var key = fields[0];
// Get the second field as the string value
var stringValue = fields[1];
// Get the third field as the int value
int intValue;
if (!int.TryParse(fields[2], out intValue))
{
Console.WriteLine($"Failed to parse integer from third field in row with key '{key}', it will be skipped.");
continue;
}
// Add to dictionary
if (result.ContainsKey(key))
{
// Handle duplicate keys if necessary
Console.WriteLine($"Duplicate key '{key}' found. Only the first occurrence will be used.");
}
else
{
result.Add(key, new Tuple<string, int>(stringValue, intValue));
}
}
return result;
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
return null;
}
}
private void Extract_Click(object sender, EventArgs e)
{
try
{
string dir = "./" + DateTime.Now.ToString("dd_MM_yyyy_HH_mm");
extract.Enabled = false;
extract.Text = "SEARCHING ...";
string search_connection = search.Text;
if (string.IsNullOrEmpty(search_connection))
{
MessageBox.Show("ENTER SEARCH TERM", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
string seach_connection2 = search2.Text;
bool multi_from = from.Checked;
var users = user_pass.Text.Split(Environment.NewLine).Select(x => x.Trim().Split(":")).Where(x => x.Length == 2).ToArray();
if (users.Length < 1)
{
MessageBox.Show("ENTER AT LEAST 1 USER AND PASS", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
int found = 0;
Parallel.ForEach(users, user =>
{
try
{
var config = user[0].Split("@");
if (config.Length != 2)
{
return;
}
var domain = config[1];
if(!data.ContainsKey(domain))
{
return;
}
var imap = data.GetValueOrDefault(domain);
var unique = new List<string>();
using var client = new ImapClient();
client.Connect(imap.Item1, imap.Item2, true);
client.Authenticate(user[0], user[1]);
// The Inbox folder is always available on all IMAP servers...
var inbox = client.Inbox;
inbox.Open(FolderAccess.ReadOnly);
Debug.WriteLine("Total messages: {0} in {1}", inbox.Count, user[0]);
for (int i = 0; i < inbox.Count; i++)
{
var message = inbox.GetMessage(i);
var name = message.From[0].Name.Replace(' ', '_');
var email = message.ToString();
if (email.ToLower().Contains(search_connection.ToLower()))
{
if(!string.IsNullOrEmpty(seach_connection2))
{
if (!email.ToLower().Contains(seach_connection2.ToLower())) continue;
}
if (unique.Contains(name) && !multi_from) continue;
unique.Add(name);
string file = user[0].Split("@")[0] + "_" + name + "_" + Guid.NewGuid().ToString("N") + ".txt";
Task asyncTask = WriteFileAsync(dir, file, email);
Interlocked.Increment(ref found);
}
}
client.Disconnect(true);
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
}
});
MessageBox.Show($"FOUND {found} EMAILS", "ENDED", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
}
finally
{
extract.Text = "EXTRACT EMAILS";
extract.Enabled = true;
}
}
private void Extract_froms_Click(object sender, EventArgs e)
{
try
{
string dir = "./" + DateTime.Now.ToString("dd_MM_yyyy_HH_mm");
string search_connection = search.Text;
if (string.IsNullOrEmpty(search_connection))
{
MessageBox.Show("ENTER SEARCH TERM", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
string seach_connection2 = search2.Text;
bool multi_from = from.Checked;
var users = user_pass.Text.Split(Environment.NewLine).Select(x => x.Trim().Split(":")).Where(x => x.Length == 2).ToArray();
if (users.Length < 1)
{
MessageBox.Show("ENTER AT LEAST 1 USER AND PASS", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
int found = 0;
Parallel.ForEach(users, user =>
{
try
{
var config = user[0].Split("@");
if (config.Length != 2)
{
return;
}
var domain = config[1];
if (!data.ContainsKey(domain))
{
return;
}
var imap = data.GetValueOrDefault(domain);
var addresses = new List<string>();
using var client = new ImapClient();
client.Connect(imap.Item1, imap.Item2, true);
client.Authenticate(user[0], user[1]);
// The Inbox folder is always available on all IMAP servers...
var inbox = client.Inbox;
inbox.Open(FolderAccess.ReadOnly);
Debug.WriteLine("Total messages: {0} in {1}", inbox.Count, user[0]);
for (int i = 0; i < inbox.Count; i++)
{
var message = inbox.GetMessage(i);
var name = message.From[0].Name.Replace(' ', '_');
var address = ExtractEmail(message.From.ToString()).ToLower();
if (string.IsNullOrEmpty(address)) continue;
var email = message.ToString();
if (email.ToLower().Contains(search_connection.ToLower()))
{
if (!string.IsNullOrEmpty(seach_connection2))
{
if (!email.ToLower().Contains(seach_connection2.ToLower())) continue;
}
if (addresses.Contains(address) && !multi_from) continue;
addresses.Add(address);
Interlocked.Increment(ref found);
}
}
string file = user[0].Split("@")[0] + "_FROMS_" + Guid.NewGuid().ToString("N") + ".txt";
Task asyncTask = WriteFileAsync(dir, file, string.Join(Environment.NewLine, addresses.ToArray()));
client.Disconnect(true);
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
}
});
MessageBox.Show($"FOUND {found} FROMS", "ENDED", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
finally
{
extract_froms.Enabled = true;
extract_froms.Text = "EXTRACT FROMS";
}
}
private void extract_listid_Click(object sender, EventArgs e)
{
try
{
string dir = "./" + DateTime.Now.ToString("dd_MM_yyyy_HH_mm");
extract_listid.Enabled = false;
extract_listid.Text = "SEARCHING ...";
string search_connection = search.Text;
string seach_connection2 = search2.Text;
bool multi_from = from.Checked;
var users = user_pass.Text.Split(Environment.NewLine).Select(x => x.Trim().Split(":")).Where(x => x.Length == 2).ToArray();
if (users.Length < 1)
{
MessageBox.Show("ENTER AT LEAST 1 USER AND PASS", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
int found = 0;
Parallel.ForEach(users, user =>
{
try
{
var config = user[0].Split("@");
if (config.Length != 2)
{
return;
}
var domain = config[1];
if (!data.ContainsKey(domain))
{
return;
}
var imap = data.GetValueOrDefault(domain);
var listIds = new List<string>();
using var client = new ImapClient();
client.Connect(imap.Item1, imap.Item2);
client.Authenticate(user[0], user[1]);
// The Inbox folder is always available on all IMAP servers...
var inbox = client.Inbox;
inbox.Open(FolderAccess.ReadOnly);
Debug.WriteLine("Total messages: {0} in {1}", inbox.Count, user[0]);
for (int i = 0; i < inbox.Count; i++)
{
var message = inbox.GetMessage(i);
var listId = message.Headers[MimeKit.HeaderId.ListId];
if (string.IsNullOrEmpty(listId)) continue;
if(!string.IsNullOrEmpty(search_connection))
{
var email = message.ToString();
if (!email.ToLower().Contains(search_connection.ToLower()))
{
continue;
}
if (!string.IsNullOrEmpty(seach_connection2))
{
if (!email.ToLower().Contains(seach_connection2.ToLower())) continue;
}
}
listId = listId.Replace("<", "").Replace(">", "");
listIds.Add(listId);
Interlocked.Increment(ref found);
}
string file = user[0].Split("@")[0] + "_LISTIDS_" + Guid.NewGuid().ToString("N") + ".txt";
Task asyncTask = WriteFileAsync(dir, file, string.Join(Environment.NewLine, listIds.ToArray()));
client.Disconnect(true);
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
}
});
MessageBox.Show($"FOUND {found} LIST ID", "ENDED", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
finally
{
extract_listid.Enabled = true;
extract_listid.Text = "EXTRACT LIST IDS";
}
}
private void extract_links_Click(object sender, EventArgs e)
{
try
{
string dir = "./" + DateTime.Now.ToString("dd_MM_yyyy_HH_mm");
extract_links.Enabled = false;
extract_links.Text = "SEARCHING ...";
string search_connection = search.Text;
string seach_connection2 = search2.Text;
bool multi_from = from.Checked;
var users = user_pass.Text.Split(Environment.NewLine).Select(x => x.Trim().Split(":")).Where(x => x.Length == 2).ToArray();
if (users.Length < 1)
{
MessageBox.Show("ENTER AT LEAST 1 USER AND PASS", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
int found = 0;
Parallel.ForEach(users, user =>
{
try
{
var config = user[0].Split("@");
if (config.Length != 2)
{
return;
}
var domain = config[1];
if (!data.ContainsKey(domain))
{
return;
}
var imap = data.GetValueOrDefault(domain);
var links = new List<string>();
using var client = new ImapClient();
client.Connect(imap.Item1, imap.Item2, true);
client.Authenticate(user[0], user[1]);
// The Inbox folder is always available on all IMAP servers...
var inbox = client.Inbox;
inbox.Open(FolderAccess.ReadOnly);
Debug.WriteLine("Total messages: {0} in {1}", inbox.Count, user[0]);
for (int i = 0; i < inbox.Count; i++)
{
var message = inbox.GetMessage(i);
var email = message.ToString();
var body = message.Body.ToString();
if (!string.IsNullOrEmpty(search_connection))
{
if (!email.ToLower().Contains(search_connection.ToLower()))
{
continue;
}
if (!string.IsNullOrEmpty(seach_connection2))
{
if (!email.ToLower().Contains(seach_connection2.ToLower())) continue;
}
}
MatchCollection collection = Regex.Matches(body, @"(https?:\/\/?[^\s.]+\.[\w][^\s]+)", RegexOptions.Multiline);
foreach (Match link in collection)
{
if (!links.Contains(link.Value))
{
Debug.WriteLine("NEW LINK: {0}", link.Value);
links.Add(link.Value);
Interlocked.Increment(ref found);
}
}
}
string file = user[0].Split("@")[0] + "_LINKS_" + Guid.NewGuid().ToString("N") + ".txt";
Task asyncTask = WriteFileAsync(dir, file, string.Join(Environment.NewLine, links.ToArray()));
client.Disconnect(true);
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
}
});
MessageBox.Show($"FOUND {found} LINKS", "ENDED", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
finally
{
extract_links.Enabled = true;
extract_links.Text = "EXTRACT LINKS";
}
}
private void extract_message_id_Click(object sender, EventArgs e)
{
try
{
string dir = "./" + DateTime.Now.ToString("dd_MM_yyyy_HH_mm");
extract_message_id.Enabled = false;
extract_message_id.Text = "SEARCHING ...";
string search_connection = search.Text;
string seach_connection2 = search2.Text;
bool multi_from = from.Checked;
var users = user_pass.Text.Split(Environment.NewLine).Select(x => x.Trim().Split(":")).Where(x => x.Length == 2).ToArray();
if (users.Length < 1)
{
MessageBox.Show("ENTER AT LEAST 1 USER AND PASS", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
int found = 0;
Parallel.ForEach(users, user =>
{
try
{
var config = user[0].Split("@");
if (config.Length != 2)
{
return;
}
var domain = config[1];
if (!data.ContainsKey(domain))
{
return;
}
var imap = data.GetValueOrDefault(domain);
var messageIds = new List<string>();
using var client = new ImapClient();
client.Connect(imap.Item1, imap.Item2, true);
client.Authenticate(user[0], user[1]);
// The Inbox folder is always available on all IMAP servers...
var inbox = client.Inbox;
inbox.Open(FolderAccess.ReadOnly);
Debug.WriteLine("Total messages: {0} in {1}", inbox.Count, user[0]);
for (int i = 0; i < inbox.Count; i++)
{
var message = inbox.GetMessage(i);
var messageId = message?.MessageId.ToString();
if (string.IsNullOrEmpty(messageId)) continue;
if (!string.IsNullOrEmpty(search_connection))
{
var email = message.ToString();
if (!email.ToLower().Contains(search_connection.ToLower()))
{
continue;
}
if (!string.IsNullOrEmpty(seach_connection2))
{
if (!email.ToLower().Contains(seach_connection2.ToLower())) continue;
}
}
messageIds.Add(messageId);
Interlocked.Increment(ref found);
}
string file = user[0].Split("@")[0] + "_MESSAGEIDS_" + Guid.NewGuid().ToString("N") + ".txt";
Task asyncTask = WriteFileAsync(dir, file, string.Join(Environment.NewLine, messageIds.ToArray()));
client.Disconnect(true);
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
}
});
MessageBox.Show($"FOUND {found} MESSAGE IDS", "ENDED", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
finally
{
extract_message_id.Enabled = true;
extract_message_id.Text = "EXTRACT MESSAGE IDS";
}
}
private void button1_Click(object sender, EventArgs e)
{
try
{
string dir = "./" + DateTime.Now.ToString("dd_MM_yyyy_HH_mm");
button1.Enabled = false;
button1.Text = "SEARCHING ...";
string search_connection = search.Text;
string seach_connection2 = search2.Text;
bool multi_from = from.Checked;
var users = user_pass.Text.Split(Environment.NewLine).Select(x => x.Trim().Split(":")).Where(x => x.Length == 2).ToArray();
if (users.Length < 1)
{
MessageBox.Show("ENTER AT LEAST 1 USER AND PASS", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
string feild = feildTxt.Text;
if(string.IsNullOrEmpty(feild) )
{
MessageBox.Show("ENTER FEILD TO SEARCH", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
int found = 0;
Parallel.ForEach(users, user =>
{
try
{
var config = user[0].Split("@");
if (config.Length != 2)
{
return;
}
var domain = config[1];
if (!data.ContainsKey(domain))
{
return;
}
var imap = data.GetValueOrDefault(domain);
var listIds = new List<string>();
using var client = new ImapClient();
client.Connect(imap.Item1, imap.Item2);
client.Authenticate(user[0], user[1]);
// The Inbox folder is always available on all IMAP servers...
var inbox = client.Inbox;
inbox.Open(FolderAccess.ReadOnly);
Debug.WriteLine("Total messages: {0} in {1}", inbox.Count, user[0]);
for (int i = 0; i < inbox.Count; i++)
{
var message = inbox.GetMessage(i);
var body = message.ToString();
string pattern = @"^.*" + Regex.Escape(feild) + @".*$";
Regex regex = new Regex(pattern, RegexOptions.Multiline);
Match match = regex.Match(body);
if (match.Success)
{
string foundLine = match.Value;
listIds.Add(foundLine);
Interlocked.Increment(ref found);
}
}
string file = user[0].Split("@")[0] + "_"+ feild + "_" + Guid.NewGuid().ToString("N") + ".txt";
Task asyncTask = WriteFileAsync(dir, file, string.Join(Environment.NewLine, listIds.ToArray()));
client.Disconnect(true);
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
}
});
MessageBox.Show($"FOUND {found}", "ENDED", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
finally
{
button1.Enabled = true;
button1.Text = "EXTRACT FEILD";
}
}
}
}