214 lines
8.4 KiB
C++
214 lines
8.4 KiB
C++
#include <email_parser/emailparser.h>
|
|
|
|
#include <opencv2/opencv.hpp>
|
|
#include <opencv2/imgcodecs.hpp>
|
|
#include <opencv2/objdetect.hpp>
|
|
#include <opencv2/core/core.hpp>
|
|
#include <opencv2/videoio.hpp>
|
|
|
|
#include <utils/utils.h>
|
|
#include <utils/base64.h>
|
|
#include <check/check.h>
|
|
#include <boost/regex.hpp>
|
|
#include <boost/algorithm/string/regex.hpp>
|
|
#include <iostream>
|
|
#include <fstream>
|
|
|
|
#if __GNUC__ < 8 && __clang_major__ < 17
|
|
# include <experimental/filesystem>
|
|
using namespace std::experimental::filesystem;
|
|
#else
|
|
# include <filesystem>
|
|
using namespace std::filesystem;
|
|
#endif
|
|
|
|
|
|
EmailParser::EmailParser() {
|
|
|
|
}
|
|
|
|
std::map<std::string, std::string> EmailParser::parse(std::string &email_content) {
|
|
// boost::regex check_data_content("t=\\d+T\\d+&s=\\d+\\.\\d+&fn=\\d{16}&i=\\d{4,5}&fp=\\d{10}&n=\\d");
|
|
boost::regex to_erase("([\\w-]+:\\s*.{2,64}\\r\\n)+");
|
|
boost::regex to_erase_two("--.{5,48}");
|
|
|
|
std::string parameters;
|
|
parameters = search_in_images(email_content);
|
|
|
|
if (parameters != "") {
|
|
std::cout << parameters << std::endl;
|
|
return get_params_from_string(parameters);
|
|
}
|
|
|
|
parameters = search_in_text(email_content);
|
|
if (parameters != "") {
|
|
std::cout << parameters << std::endl;
|
|
return get_params_from_string(parameters);
|
|
}
|
|
|
|
/* If the code has reached this part and found nothing, it's most likely that there are no QR codes at all. */
|
|
|
|
return std::map<std::string, std::string>();
|
|
}
|
|
|
|
std::map<std::string, std::string> EmailParser::parse_file(std::string path) {
|
|
std::string content = read_file(path);
|
|
return parse(content);
|
|
return std::map<std::string, std::string>();
|
|
}
|
|
std::vector<std::pair<int, int>> EmailParser::find_parts(const boost::regex &start_regex, const boost::regex &end_regex, const std::string &content) {
|
|
std::vector<std::pair<int, int>> parts = {};
|
|
|
|
for (boost::sregex_iterator it{content.begin(), content.end(), start_regex}, end{}; it != end; it ++) {
|
|
unsigned int start_position = it->position(), end_position = content.length();
|
|
|
|
for (boost::sregex_iterator it2{content.begin() + start_position, content.end(), end_regex}, end2{}; it2 != end2; it2++) {
|
|
end_position = it2->position();
|
|
break;
|
|
}
|
|
parts.push_back(std::pair<int, int>(start_position, end_position));
|
|
}
|
|
|
|
return parts;
|
|
}
|
|
|
|
std::string EmailParser::find_check_parameters(std::string &part) {
|
|
boost::regex params_regex ("t(=|%3d)\\d+T\\d+(&|%26)s\\1\\d+\\.\\d+\\2fn\\1\\d{16}\\2i\\1\\d{3,6}\\2fp\\1\\d{9,10}\\2n\\1\\d", boost::regex::icase);
|
|
for (boost::sregex_iterator it{part.begin(), part.end(), params_regex}, end{}; it != end; it++) {
|
|
return it->str();
|
|
}
|
|
return "";
|
|
}
|
|
|
|
std::string EmailParser::extract_qr_url_from_img(std::string &part) {
|
|
boost::regex img_tag_regex("<img[^\\n\\r<]*>");
|
|
boost::regex img_url_str("https?:\\/\\/.*(qr(code)?)[^\\n\\r\"]+", boost::regex::icase);
|
|
|
|
for (boost::sregex_iterator it{part.begin(), part.end(), img_tag_regex}, end{}; it != end; it++) {
|
|
std::string img_tag = it->str();
|
|
for (boost::sregex_iterator it2{img_tag.begin(), img_tag.end(), img_url_str}, end2{}; it2 != end2; it2++) {
|
|
return it2->str();
|
|
}
|
|
|
|
}
|
|
return "";
|
|
}
|
|
|
|
std::vector<std::string> EmailParser::extract_qr_embeddings_from_part(std::string &part) {
|
|
std::vector<std::string> embeddings = {};
|
|
boost::regex img_tag_regex("<img[^\\n\\r<]*>");
|
|
boost::regex img_base64_str("data:image\\/(png|jpg);base64,[\\w+\\/=]+", boost::regex::icase);
|
|
|
|
for (boost::sregex_iterator it{part.begin(), part.end(), img_tag_regex}, end{}; it != end; it++) {
|
|
std::string img_tag = it->str();
|
|
for (boost::sregex_iterator it2{img_tag.begin(), img_tag.end(), img_base64_str}, end2{}; it2 != end2; it2++) {
|
|
embeddings.push_back(split(it2->str(), ",")[1]);
|
|
}
|
|
|
|
}
|
|
return embeddings;
|
|
}
|
|
|
|
std::string EmailParser::search_in_images(std::string &content) {
|
|
boost::regex images_content_type_regex("Content-Type: image/(gif|png|jpg)");
|
|
boost::regex part_end_regex("--.{5,48}");
|
|
boost::regex to_erase("([\\w-]+:\\s*.{2,64}\\r\\n)+");
|
|
boost::regex to_erase_two("--.{5,48}");
|
|
|
|
std::vector<std::pair<int, int>> images_content_parts = find_parts(images_content_type_regex, part_end_regex, content);
|
|
|
|
/* iterate through found image content-types and try searching qr codes, decode them and see if it's the needed data */
|
|
|
|
for (unsigned int i = 0; i < images_content_parts.size(); i ++) {
|
|
|
|
std::string part = content.substr(images_content_parts[i].first, images_content_parts[i].second);
|
|
boost::erase_regex(part, to_erase);
|
|
boost::erase_regex(part, to_erase_two);
|
|
part.erase(std::remove(part.begin(), part.end(), '\r'), part.end());
|
|
part.erase(std::remove(part.begin(), part.end(), '\n'), part.end());
|
|
std::string decoded = base64_decode(part);
|
|
return handle_image(decoded);
|
|
}
|
|
return "";
|
|
}
|
|
|
|
std::string EmailParser::search_in_text(std::string &content) {
|
|
boost::regex text_content_types_regex("Content-Type: text\\/(html|plain)");
|
|
boost::regex part_end_regex("--.{5,48}");
|
|
boost::regex to_erase("([\\w-]+:\\s*.{2,64}\\r\\n)+");
|
|
boost::regex to_erase_two("--.{5,48}");
|
|
/* If the E-Mail has no QR code in it as a separate part, there's posibilly a QR code inserted using html's tag <img> with base64-encoded image. Try searching it */
|
|
|
|
std::vector<std::pair<int, int>> texts_content_parts = find_parts(text_content_types_regex, part_end_regex, content);
|
|
|
|
for (unsigned int i = 0; i < texts_content_parts.size(); i ++) {
|
|
|
|
std::string part = content.substr(texts_content_parts[i].first, texts_content_parts[i].second);
|
|
boost::erase_regex(part, to_erase);
|
|
boost::erase_regex(part, to_erase_two);
|
|
|
|
|
|
//If there's '<' character, most likely that the part's content is plain html, otherwise it's most likely a base64 encoded html.
|
|
if (part.find("<") == std::string::npos) {
|
|
part.erase(std::remove(part.begin(), part.end(), '\r'), part.end());
|
|
part.erase(std::remove(part.begin(), part.end(), '\n'), part.end());
|
|
part = base64_decode(part);
|
|
}
|
|
|
|
// Try searching parameters just in plain html. Will help if there's a link to a QR code with it's parameters passed in request.
|
|
std::string parameters = find_check_parameters(part);
|
|
|
|
if (parameters != "") return parameters;
|
|
|
|
// If there's no, try search anything that looks like a link to a qr code.
|
|
std::string url = extract_qr_url_from_img(part);
|
|
if (url != "") {
|
|
Net n;
|
|
std::string path = get_path_relative_to_home(".local/share/checks_parser/tmp");
|
|
n.get_file(url, path);
|
|
|
|
std::string qr_code_contents = read_file(path);
|
|
|
|
std::vector<uchar> data(qr_code_contents.begin(), qr_code_contents.end());
|
|
cv::Mat image = cv::imdecode(cv::Mat(data), 1);
|
|
|
|
cv::QRCodeDetector qrDecoder = cv::QRCodeDetector();
|
|
std::string decoded_qr = qrDecoder.detectAndDecode(image);
|
|
parameters = find_check_parameters(decoded_qr);
|
|
}
|
|
if (parameters != "") return parameters;
|
|
|
|
// if there's no any link that looks like a link to QR code, maybe the qr code is encoded as base64 inside an img tag.
|
|
|
|
std::vector<std::string> embeddings = extract_qr_embeddings_from_part(part);
|
|
for (std::string &embedding : embeddings) {
|
|
std::string decoded = base64_decode(embedding);
|
|
parameters = handle_image(decoded);
|
|
if (parameters != "") return parameters;
|
|
}
|
|
}
|
|
return "";
|
|
}
|
|
|
|
std::string EmailParser::handle_image(std::string &content) {
|
|
cv::Mat image;
|
|
|
|
if (content.substr(1, 3) == "PNG" || content.substr(1, 3) == "JPG") {
|
|
std::vector<uchar> data(content.begin(), content.end());
|
|
image = cv::imdecode(cv::Mat(data), 1);
|
|
} else if (content.substr(0, 3) == "GIF") {
|
|
std::string gif_file_path = get_application_home_path() + "/temp.gif";
|
|
|
|
std::ofstream gif_output(gif_file_path, std::ios::binary);
|
|
gif_output << content;
|
|
gif_output.close();
|
|
cv::VideoCapture gif(gif_file_path, cv::CAP_FFMPEG);
|
|
gif.read(image);
|
|
}
|
|
if (image.empty()) return "";
|
|
|
|
cv::QRCodeDetector qrDecoder = cv::QRCodeDetector();
|
|
std::string decoded_qr = qrDecoder.detectAndDecode(image);
|
|
return find_check_parameters(decoded_qr);
|
|
}
|