plans and sketches

This commit is contained in:
leca 2025-04-15 22:02:33 +03:00
parent 5f4a86cee8
commit 6706cbec45
9 changed files with 235 additions and 2 deletions

View File

@ -110,6 +110,11 @@ if (BUILD_OFD_BINARYEYE_SCAN)
list(APPEND PROJECT_SOURCES http_server/http_server.h http_server/http_server.cpp)
endif()
if (BUILD_EMAIL_MODE)
list(APPEND PROJECT_SOURCES email_parser/emailparser.h email_parser/emailparser.cpp)
list(APPEND PROJECT_SOURCES utils/base64.h utils/base64.cpp)
endif()
if (BUILD_TRANSLATIONS)
set(TS_FILES

View File

@ -3,7 +3,22 @@
#include "../goods/goods.h"
#include <vector>
typedef enum OperationTypes {
funds_income, // Приход средств
funds_return, // Возврат прихода
funds_spend, // Расход средств
spends_return // Возврат расхода
} OperationType;
class Check {
std::string fn; // Fiscal Number = Фискальный номер
std::string fd; // Fiscal Document = Фискальный документ
std::string fi; // Fiscal Identifier = Фискальный признак
std::string date;
OperationType operation_type;
double total;
std::vector<Goods> goods;
public:
@ -14,6 +29,20 @@ public:
double calculae_total_price();
std::vector<Goods> &get_goods();
void set_fn(std::string);
void set_fd(std::string);
void set_fi(std::string);
void set_date(std::string);
void set_operation_type(OperationType);
void set_total(double);
std::string get_fn();
std::string get_fd();
std::string get_fi();
std::string get_date();
OperationType get_operationType();
double get_total();
};
#endif // CHECK_H

View File

@ -0,0 +1,70 @@
#include <email_parser/emailparser.h>
#include <utils/base64.h>
#include <check/check.h>
#include <boost/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
std::string EmailParser::get_payload_in_email(std::string &email_content) {
boost::regex content_type_and_transfer_encoding_regex("Content-Type")
// boost::regex body_start_regex("\r\n\r\n"); //boost::regex_constants::egrep
// boost::smatch smatch;
// if (boost::regex_search(email_content, smatch, body_start_regex)) {
// return email_content.substr(smatch.position(), email_content.length());
// }
// return "";
}
// std::vector<int> EmailParser::find_base64_blocks_in_email(std::string &email_content) {
// std::string glued_together;
// for (auto c : email_content) {
// if (c == '\n') continue;
// glued_together.push_back(c);
// }
// boost::regex base64_regex("^[-A-Za-z0-9+/]*={0,3}$");
// }
EmailParser::EmailParser() {
}
Check EmailParser::parse(std::string &email_content) {
//1. Search "Content-Type: image/.*" in the .eml file.
// 1.1 If found 0, go to [2]
// 1.2 If found 1, try decoding it, if it's not a QR code, go to [2]
// 1.3 Loop through every found entry. If not found in any, go to [2]
//2. Try decoding content of the e-mail
//3. Search "t=\d{8}T\d{4,6}&s=\d{1,6}\.\d{1,2}&fn=\d{10,16}&i=\d{6}&fp=\d{10}&n=\d". Note that in some emails = and & signs could be replaced with its code in HTTP requests: %3D, %26
// 3.1 If not found, notify the user that we could not parse the .eml file
// std::string payload = get_payload_in_email(email_content);
// Check c;
// std::cout << payload << std::endl;
// if (payload == "")
// return c;
// return c;
}
Check EmailParser::parse_file(std::string path) {
std::ifstream ifile(path, std::ios::in | std::ios::binary);
const unsigned int size = std::filesystem::file_size(path);
std::string content(size, '\0');
ifile.read(content.data(), size);
return parse(content);
}

View File

@ -0,0 +1,15 @@
#ifndef CHECKS_PARSER_EMAIL_PARSER
#define CHECKS_PARSER_EMAIL_PARSER
#include <check/check.h>
class EmailParser {
std::string get_payload_in_email(std::string &email_content);
// std::vector<int> find_base64_blocks_in_email(std::string &email_content);
public:
EmailParser();
Check parse(std::string &email_content);
Check parse_file(std::string path);
};
#endif // CHECKS_PARSER_EMAIL_PARSER

View File

@ -1,3 +1,4 @@
#include "email_parser/emailparser.h"
#include <mainwindow.h>
#include <net/net.h>
#include <settings/settings.h>
@ -27,7 +28,12 @@
#endif
#include <QPushButton>
#include <utils/base64.h>
int main(int argc, char *argv[]) {
// EmailParser ep;
// ep.parse_file("/home/leca/example_email_receipts/rzd.eml");
// return 0;
curl_global_init(CURL_GLOBAL_ALL);
std::string program_data_path = get_path_relative_to_home(".local/share/checks_parser");

View File

@ -597,7 +597,7 @@
<context>
<name>QObject</name>
<message>
<location filename="../main.cpp" line="60"/>
<location filename="../main.cpp" line="66"/>
<source>Using locale: </source>
<translation>Using locale: </translation>
</message>

View File

@ -593,7 +593,7 @@
<context>
<name>QObject</name>
<message>
<location filename="../main.cpp" line="60"/>
<location filename="../main.cpp" line="66"/>
<source>Using locale: </source>
<translation>Использую локаль: </translation>
</message>

79
utils/base64.cpp Normal file
View File

@ -0,0 +1,79 @@
/*
base64.cpp and base64.h
Copyright (C) 2004-2008 René Nyffenegger
This source code is provided 'as-is', without any express or implied
warranty. In no event will the author be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this source code must not be misrepresented; you must not
claim that you wrote the original source code. If you use this source code
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original source code.
3. This notice may not be removed or altered from any source distribution.
René Nyffenegger rene.nyffenegger@adp-gmbh.ch
*/
#include <utils/base64.h>
#include <string>
static const std::string base64_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
static inline bool is_base64(unsigned char c) {
return (std::isalnum(c) || (c == '+') || (c == '/'));
}
std::string base64_decode(std::string const& encoded_string) {
int in_len = encoded_string.size();
int i = 0;
int j = 0;
int in_ = 0;
unsigned char char_array_4[4], char_array_3[3];
std::string ret;
while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
char_array_4[i++] = encoded_string[in_]; in_++;
if (i ==4) {
for (i = 0; i <4; i++)
char_array_4[i] = base64_chars.find(char_array_4[i]);
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
for (i = 0; (i < 3); i++)
ret += char_array_3[i];
i = 0;
}
}
if (i) {
for (j = i; j <4; j++)
char_array_4[j] = 0;
for (j = 0; j <4; j++)
char_array_4[j] = base64_chars.find(char_array_4[j]);
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
}
return ret;
}

29
utils/base64.h Normal file
View File

@ -0,0 +1,29 @@
/*
base64.cpp and base64.h
Copyright (C) 2004-2008 René Nyffenegger
This source code is provided 'as-is', without any express or implied
warranty. In no event will the author be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this source code must not be misrepresented; you must not
claim that you wrote the original source code. If you use this source code
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original source code.
3. This notice may not be removed or altered from any source distribution.
René Nyffenegger rene.nyffenegger@adp-gmbh.ch
*/
#include <string>
std::string base64_decode(std::string const& encoded_string);