-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmailAnalyser.cpp
More file actions
142 lines (128 loc) · 3.99 KB
/
Copy pathEmailAnalyser.cpp
File metadata and controls
142 lines (128 loc) · 3.99 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
#include "EmailAnalyser.h"
#include "Base64.h"
#include <iostream>
#include <fstream>
#include <regex>
#include <thread>
#include <boost/algorithm/string.hpp>
#include <boost/filesystem.hpp>
#include <boost/range/iterator_range.hpp>
#define SCRIPT_EXTENSION ".sh"
// On receiving an email
void CEmailAnalyser::NewEmail (const std::string& aNewEmail)
{
std::string NewEmailWithoutAttachments;
std::list<std::string> Attachments;
GetAttachments(aNewEmail, NewEmailWithoutAttachments, Attachments);
std::cout << "Email received :" << std::endl;
std::cout << NewEmailWithoutAttachments << std::endl;
if (Attachments.size())
{
std::cout << "Attachments:" << std::endl;
for (std::string Attachment : Attachments)
{
std::cout << Attachment << std::endl;
}
}
// The large email string must be converted in a istringstream to be read line by line
// (regex on multiple line is not working)
std::istringstream EmailLines (aNewEmail);
// Search for scripts in scripts directory
boost::filesystem::path p = "./scripts/";
for (boost::filesystem::directory_entry& entry : boost::make_iterator_range(boost::filesystem::directory_iterator(p), {}))
{
if (SCRIPT_EXTENSION == entry.path().extension())
{
std::cout << "Opening " << entry.path().string() << std::endl;
std::ifstream ScriptFile(entry.path().string());
std::string RegexLine;
// Read the two first lines
if (std::getline(ScriptFile, RegexLine) && std::getline(ScriptFile, RegexLine))
{
RegexLine.erase(0, 1); // Remove the first char (it should be a '#')
std::cout << "The regex of this script is: '" << RegexLine << "'" << std::endl;
// Catch exception (if regex is not valid)
try
{
std::regex RegexFromScript(RegexLine);
std::string EmailLine;
// Try regex on each line from email
while (std::getline(EmailLines, EmailLine))
{
if (std::regex_search(EmailLine, RegexFromScript))
{
// Start script
std::cout << "Regex from script match." << std::endl;
std::string Command = entry.path().string();
for (std::string Attachment : Attachments)
{
Command.append(" \"" + Attachment + "\"");
}
std::thread ([Command](){
std::cout << "start : " << Command << std::endl;
system(Command.c_str());
std::cout << "end : " << Command << std::endl;
}).detach();
}
break; // no need to read more
}
}
catch (std::exception& e)
{
// Most commonly a regex error.
std::cerr << e.what() << std::endl;
}
}
}
}
}
// Search for attachment
void CEmailAnalyser::GetAttachments (const std::string& aNewEmail, std::string& aNewEmailWithoutAttachments, std::list<std::string>& aAttachments)
{
// The large email string must be converted in a istringstream to be read line by line
std::istringstream EmailLines (aNewEmail);
std::string EmailLine;
std::ofstream AttachmentFile;
// Try regex on each line from email
while (std::getline(EmailLines, EmailLine))
{
if (AttachmentFile.is_open())
{
if (std::string::npos != EmailLine.find(' '))
{
// Drop blank lines
}
else if (std::string::npos != EmailLine.find('-'))
{
AttachmentFile.close();
}
else
{
AttachmentFile << base64_decode(EmailLine);
}
}
else
{
if (std::string::npos != EmailLine.find("Content-Disposition: attachment;"))
{
std::getline(EmailLines, EmailLine);
std::regex FilenameRegex("filename=\"([^/\\:*?\"<>|]*)\""); // chars / \ : * ? " < > | are forbids in filename
std::smatch matches;
if (std::regex_search(EmailLine, matches, FilenameRegex) && 1 < matches.size() )
{
std::string Filename = std::string("attachments/") + std::string(matches[1]);
AttachmentFile.open (Filename, std::ofstream::binary);
if (AttachmentFile.is_open())
{
aAttachments.push_back(matches[1]);
}
}
}
aNewEmailWithoutAttachments.append(EmailLine + "\n");
}
}
if (AttachmentFile.is_open())
{
AttachmentFile.close();
}
}