diff --git a/.gitignore b/.gitignore index 7e44299..e1e35d7 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,4 @@ log.txt tdlib/ thirdparty/td/tdlib +assignment.conf diff --git a/inc/tgclient.h b/inc/tgclient.h index f31cef6..22262a5 100644 --- a/inc/tgclient.h +++ b/inc/tgclient.h @@ -86,6 +86,7 @@ public: void downloadFile(int32_t file_id); + void sendTextMessage(int64_t chatid, const std::string& text); void sendTextMessageToSelf(const std::string& text); void getMessage(int64_t chatid, int64_t messageid, std::function)> f); diff --git a/src/crypt.cpp b/src/crypt.cpp index 601c4dd..d5f9b0d 100644 --- a/src/crypt.cpp +++ b/src/crypt.cpp @@ -46,8 +46,11 @@ std::string Crypt::encryptForChat(int64_t chat, const std::string& message) { proc->waitFor(std::chrono::seconds(4)); - out.resize(out.size() - Footer.size()); - return MyHeader + out.substr(Header.size()); + if(out.size() > Footer.size() + Header.size() ) { + out.resize(out.size() - Footer.size()); + return MyHeader + out.substr(Header.size()); + } + return ""; } std::string Crypt::decryptFromChat(int64_t chat, const std::string& message) { diff --git a/src/tgclient.cpp b/src/tgclient.cpp index 165bc83..edbc0e5 100644 --- a/src/tgclient.cpp +++ b/src/tgclient.cpp @@ -285,9 +285,9 @@ void TGClient::downloadFile(int32_t file_id) { send_staticquery(td::make_tl_object(file_id, /* prio */ 15, 0, 0, false), HANDLER_NULL); } -void TGClient::sendTextMessageToSelf(const std::string& text) { +void TGClient:: sendTextMessage(int64_t chatid, const std::string& text) { auto sendmsg = td::make_tl_object(); - sendmsg->chat_id_ = me; + sendmsg->chat_id_ = chatid; sendmsg->message_thread_id_ = 0; sendmsg->reply_to_ = nullptr; sendmsg->options_ = nullptr; @@ -297,6 +297,10 @@ void TGClient::sendTextMessageToSelf(const std::string& text) { send_staticquery(std::move(sendmsg), HANDLER_NULL); } +void TGClient::sendTextMessageToSelf(const std::string& text) { + sendTextMessage(me, text); +} + void TGClient::getMessage(int64_t chatid, int64_t messageid, std::function)> f) { send_wrappedquery(td::make_tl_object(chatid, messageid), f); } diff --git a/src/tgtui.cpp b/src/tgtui.cpp index 7e92f00..9e5f271 100644 --- a/src/tgtui.cpp +++ b/src/tgtui.cpp @@ -45,9 +45,12 @@ static void clearVec(std::vector v) { v.clear(); } -TgTUI::TgTUI() : tgclient(std::bind(&TgTUI::initDoneCB, this)), crypt(std::make_unique()) {} +TgTUI::TgTUI() : tgclient(std::bind(&TgTUI::initDoneCB, this)), crypt(std::make_unique()) { + crypt->load(); +} TgTUI::~TgTUI() { + crypt->save(); stop(); } @@ -146,6 +149,13 @@ void TgTUI::threadLoop() { if(chatScroll->exitType == vNORMAL) { activateCDKEntry(textEntry, nullptr); + + if(textEntry->exitType == vNORMAL) { + char* value = getCDKEntryValue(textEntry); + const std::string msg(value); + const std::string encMsg = crypt->encryptForChat(chat.chatId, msg); + tgclient.sendTextMessage(chat.chatId, encMsg); + } } tgclient.closeChat(chat.chatId);