From 797eecc4872041817440cca36430a92575c098ea Mon Sep 17 00:00:00 2001 From: mrbesen Date: Wed, 7 Apr 2021 20:07:11 +0200 Subject: [PATCH] parse filename, display username --- src/main.cpp | 2 +- src/search.cpp | 36 ++++++++++++++++++++++++++++++------ src/search.h | 18 +++++++++++------- 3 files changed, 42 insertions(+), 14 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 5d1241c..62c820c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -77,7 +77,7 @@ int main(int argc, const char** argv) { if(c == 'y' || c == 'Y') { //print results for(const Message* m : results) { - Log::info << search->getShortChatname(m->chatid) << ": (" << m->messageid << ") " << m->text; + Log::info << search->getShortChatname(m->senderid) << "@" << search->getShortChatname(m->chatid) << "> (" << m->messageid << ") " << m->text; } } diff --git a/src/search.cpp b/src/search.cpp index f8e697b..226c03d 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -23,6 +23,10 @@ bool Message::operator<(const Message& m) const { return (messageid < m.messageid); } +bool Message::hasFile() const { + return !filename.empty(); +} + Searchflags operator|=(Searchflags& lhs, const Searchflags sf) { lhs = (Searchflags) ((uint32_t) lhs | (uint32_t) sf); return lhs; @@ -103,14 +107,14 @@ void Search::addFile(const std::string& file) { //multi chat export if(j.contains("chats")) { - const json& chatlist = j["chats"]["list"]; + const json& chatlist = j["chats"]["list"]; //asume that list exists if(chatlist.is_null()) { Log::error << "File does not contain a chatlist"; return; } for(const json& chat : chatlist) { - uint64_t id = chat["id"]; + int64_t id = chat["id"]; std::string name = ""; if(chat.contains("name") && !chat["name"].is_null()) name = chat.value("name", ""); @@ -123,6 +127,16 @@ void Search::addFile(const std::string& file) { return; } + //get contact list + if(j.contains("frequent_contacts")) { + const json& contacts = j["frequent_contacts"]["list"];//asume that list exists + for(const json& contact : contacts) { + int64_t id = contact["id"]; + std::string name = contact["name"]; + chatnames.insert({id, name}); + } + } + Log::warn << "File " << file << " could not be parsed! Export as json!"; } catch (nlohmann::detail::parse_error& e) { Log::error << "Could not load File: " << e.what(); @@ -157,7 +171,7 @@ std::list Search::search(std::string text, Searchflags flags) co return out; } -const std::string& Search::getChatname(uint64_t id) const { +const std::string& Search::getChatname(int64_t id) const { static const std::string UNKOWNCHAT = ""; auto it = chatnames.find(id); if(it == chatnames.end()) return UNKOWNCHAT; @@ -165,7 +179,7 @@ const std::string& Search::getChatname(uint64_t id) const { return it->second; } -std::string Search::getShortChatname(uint64_t id) const { +std::string Search::getShortChatname(int64_t id) const { std::string chatname = getChatname(id); if(chatname.size() > 14) { return chatname.substr(0, 14); @@ -213,14 +227,24 @@ static void readText(const json& t, std::string& out) { } } -void Search::loadMessages(const json& j, uint64_t chatid) { +void Search::loadMessages(const json& j, int64_t chatid) { uint32_t failed = 0; for(const json& m : j) { try { if(m.contains("text")) { std::string text; readText(m["text"], text); - deduplicate.insert({text, chatid, m["id"]}); + std::string file = ""; + if(m.contains("file")) + file = m["file"]; + else if(m.contains("photo")) + file = m["photo"]; + + int64_t sender = 0; + if(m.contains("from_id") && m["from_id"].is_number_unsigned()) + sender = m["from_id"]; + + deduplicate.insert({chatid, m["id"], sender, text, file, m.contains("reply_to_message_id")}); } else { Log::warn << "text less message: " << m; } diff --git a/src/search.h b/src/search.h index 564179c..328132d 100644 --- a/src/search.h +++ b/src/search.h @@ -11,13 +11,17 @@ using json = nlohmann::json; struct Message { + int64_t chatid; + int64_t messageid; + int64_t senderid; std::string text; - uint64_t chatid; - uint64_t messageid; + std::string filename = ""; //empty filename = no file + bool isreply = false; bool operator==(const Message& m) const; bool operator!=(const Message& m) const; bool operator<(const Message& m) const; + bool hasFile() const; }; enum class Searchflags { @@ -25,7 +29,7 @@ enum class Searchflags { IGNORECASE = 1, REGEX = 2, - //ideen: nach sender filtern, nur nachrichten mit medien, nur nachrichten ohne medien, medien Dateinamen, nach datum filtern + //ideen: nach sender filtern, nur nachrichten mit medien, nur nachrichten ohne medien, medien Dateinamen, nach datum filtern, service messages? (joined, kicked, invited,...), inverted (not conatain) }; Searchflags operator|=(Searchflags& lhs, const Searchflags sf); @@ -45,8 +49,8 @@ public: void finalize(); //stop adding files and finalize deduplication, could be called twice, but then a deduplication is not guaranteed std::list search(std::string text, Searchflags flags = Searchflags::NONE) const; - const std::string& getChatname(uint64_t id) const; - std::string getShortChatname(uint64_t id) const; + const std::string& getChatname(int64_t id) const; + std::string getShortChatname(int64_t id) const; uint32_t getChatCount() const; uint64_t getMessageCount() const; @@ -56,9 +60,9 @@ private: template void runsearch(T st, bool (*checker)(const std::string& msg, T text), std::list& out) const; - void loadMessages(const json& j, uint64_t chatid); + void loadMessages(const json& j, int64_t chatid); std::vector msgs; std::set deduplicate; //intermediate store, for reading files and deduplicate them - std::map chatnames; + std::map chatnames; }; \ No newline at end of file