diff options
Diffstat (limited to 'plugins/New_GPG/src/messages.cpp')
-rwxr-xr-x | plugins/New_GPG/src/messages.cpp | 361 |
1 files changed, 159 insertions, 202 deletions
diff --git a/plugins/New_GPG/src/messages.cpp b/plugins/New_GPG/src/messages.cpp index 4e8d4926e1..fc16bf8d73 100755 --- a/plugins/New_GPG/src/messages.cpp +++ b/plugins/New_GPG/src/messages.cpp @@ -20,50 +20,60 @@ int returnNoError(MCONTACT hContact); std::list<HANDLE> sent_msgs; -void RecvMsgSvc_func(MCONTACT hContact, std::wstring str, char *msg, DWORD, DWORD timestamp) +struct RecvParams +{ + RecvParams(MCONTACT _p1, std::wstring _p2, char *_p3, DWORD _p4) : + hContact(_p1), + str(_p2), + msg(_p3), + timestamp(_p4) + {} + + MCONTACT hContact; + std::wstring str; + char *msg; + DWORD timestamp; +}; + +static void RecvMsgSvc_func(RecvParams *param) { DWORD dbflags = DBEF_UTF; + MCONTACT hContact = param->hContact; { // check for gpg related data - wstring::size_type s1 = str.find(L"-----BEGIN PGP MESSAGE-----"); - wstring::size_type s2 = str.find(L"-----END PGP MESSAGE-----"); - if (s2 != wstring::npos && s1 != wstring::npos) - { //this is generic encrypted data block - if (!isContactSecured(hContact)) - { + wstring::size_type s1 = param->str.find(L"-----BEGIN PGP MESSAGE-----"); + wstring::size_type s2 = param->str.find(L"-----END PGP MESSAGE-----"); + if (s2 != wstring::npos && s1 != wstring::npos) { //this is generic encrypted data block + if (!isContactSecured(hContact)) { if (globals.bDebugLog) globals.debuglog << std::string(time_str() + ": info: received encrypted message from: " + toUTF8(Clist_GetContactDisplayName(hContact)) + " with turned off encryption"); - if (MessageBox(nullptr, TranslateT("We received encrypted message from contact with encryption turned off.\nDo you want to turn on encryption for this contact?"), TranslateT("Warning"), MB_YESNO) == IDYES) - { - if (!isContactHaveKey(hContact)) - { + if (MessageBox(nullptr, TranslateT("We received encrypted message from contact with encryption turned off.\nDo you want to turn on encryption for this contact?"), TranslateT("Warning"), MB_YESNO) == IDYES) { + if (!isContactHaveKey(hContact)) { void ShowLoadPublicKeyDialog(bool = false); globals.item_num = 0; //black magic here globals.user_data[1] = hContact; ShowLoadPublicKeyDialog(true); } - else - { + else { db_set_b(db_mc_isMeta(hContact) ? metaGetMostOnline(hContact) : hContact, MODULENAME, "GPGEncryption", 1); setSrmmIcon(hContact); setClistIcon(hContact); } - if (isContactHaveKey(hContact)) - { + if (isContactHaveKey(hContact)) { db_set_b(db_mc_isMeta(hContact) ? metaGetMostOnline(hContact) : hContact, MODULENAME, "GPGEncryption", 1); setSrmmIcon(hContact); setClistIcon(hContact); } } - else if (MessageBox(nullptr, TranslateT("Do you want to try to decrypt encrypted message?"), TranslateT("Warning"), MB_YESNO) == IDNO) - { - HistoryLog(hContact, db_event(msg, timestamp, 0, dbflags)); + else if (MessageBox(nullptr, TranslateT("Do you want to try to decrypt encrypted message?"), TranslateT("Warning"), MB_YESNO) == IDNO) { + HistoryLog(hContact, db_event(param->msg, param->timestamp, 0, dbflags)); + delete param; return; } } else if (globals.bDebugLog) globals.debuglog << std::string(time_str() + ": info: received encrypted message from: " + toUTF8(Clist_GetContactDisplayName(hContact))); - boost::algorithm::erase_all(str, "\r"); + boost::algorithm::erase_all(param->str, "\r"); s2 += mir_wstrlen(L"-----END PGP MESSAGE-----"); ptrW ptszHomePath(UniGetContactSettingUtf(NULL, MODULENAME, "szHomePath", L"")); @@ -71,8 +81,7 @@ void RecvMsgSvc_func(MCONTACT hContact, std::wstring str, char *msg, DWORD, DWOR wstring decfile = toUTF16(get_random(10)); { wstring path = wstring(ptszHomePath) + L"\\tmp\\" + encfile; - if(!globals.bDebugLog) - { + if (!globals.bDebugLog) { boost::system::error_code e; boost::filesystem::remove(path, e); } @@ -82,21 +91,20 @@ void RecvMsgSvc_func(MCONTACT hContact, std::wstring str, char *msg, DWORD, DWOR int count = 0; fstream f(path.c_str(), std::ios::out); - while (!f.is_open()) - { - boost::this_thread::sleep(boost::posix_time::milliseconds(step)); + while (!f.is_open()) { + ::Sleep(step); count += step; - if(count >= timeout) - { + if (count >= timeout) { db_set_b(hContact, MODULENAME, "GPGEncryption", 0); setSrmmIcon(hContact); setClistIcon(hContact); - globals.debuglog<<std::string(time_str() + "info: failed to create temporary file for decryption, disabling gpg for contact to avoid deadlock"); + globals.debuglog << std::string(time_str() + "info: failed to create temporary file for decryption, disabling gpg for contact to avoid deadlock"); + delete param; return; } f.open(path.c_str(), std::ios::out); } - char *tmp = mir_u2a(str.substr(s1, s2 - s1).c_str()); + char *tmp = mir_u2a(param->str.substr(s1, s2 - s1).c_str()); f << tmp; mir_free(tmp); f.close(); @@ -116,8 +124,7 @@ void RecvMsgSvc_func(MCONTACT hContact, std::wstring str, char *msg, DWORD, DWOR if (pass[0] && globals.bDebugLog) globals.debuglog << std::string(time_str() + ": info: found password in database for key ID: " + inkeyid + ", trying to decrypt message from " + toUTF8(Clist_GetContactDisplayName(hContact)) + " with password"); } - else - { + else { pass = UniGetContactSettingUtf(NULL, MODULENAME, "szKeyPassword", L""); if (pass[0] && globals.bDebugLog) globals.debuglog << std::string(time_str() + ": info: found password for all keys in database, trying to decrypt message from " + toUTF8(Clist_GetContactDisplayName(hContact)) + " with password"); @@ -126,8 +133,7 @@ void RecvMsgSvc_func(MCONTACT hContact, std::wstring str, char *msg, DWORD, DWOR cmd.push_back(L"--passphrase"); cmd.push_back(pass); } - else if (globals.password && globals.password[0]) - { + else if (globals.password && globals.password[0]) { if (globals.bDebugLog) globals.debuglog << std::string(time_str() + ": info: found password in memory, trying to decrypt message from " + toUTF8(Clist_GetContactDisplayName(hContact)) + " with password"); cmd.push_back(L"--passphrase"); @@ -139,8 +145,7 @@ void RecvMsgSvc_func(MCONTACT hContact, std::wstring str, char *msg, DWORD, DWOR mir_free(inkeyid); } - if(!globals.bDebugLog) - { + if (!globals.bDebugLog) { boost::system::error_code e; boost::filesystem::remove(wstring(ptszHomePath) + L"\\tmp\\" + decfile, e); } @@ -150,54 +155,40 @@ void RecvMsgSvc_func(MCONTACT hContact, std::wstring str, char *msg, DWORD, DWOR cmd.push_back(L"-d"); cmd.push_back(L"-a"); cmd.push_back(path); + gpg_execution_params params(cmd); pxResult result; params.out = &out; params.code = &code; params.result = &result; - if (!gpg_launcher(params)) - { - if(!globals.bDebugLog) - { + if (!gpg_launcher(params)) { + if (!globals.bDebugLog) { boost::system::error_code e; boost::filesystem::remove(path, e); } - HistoryLog(hContact, db_event(msg, timestamp, 0, dbflags)); + HistoryLog(hContact, db_event(param->msg, param->timestamp, 0, dbflags)); BYTE enc = db_get_b(hContact, MODULENAME, "GPGEncryption", 0); db_set_b(hContact, MODULENAME, "GPGEncryption", 0); ProtoChainSend(hContact, PSS_MESSAGE, 0, (LPARAM)"Unable to decrypt PGP encrypted message"); HistoryLog(hContact, db_event("Error message sent", 0, 0, DBEF_SENT)); db_set_b(hContact, MODULENAME, "GPGEncryption", enc); + delete param; return; } - if (result == pxNotFound) - { - if(!globals.bDebugLog) - { + if (result == pxNotFound) { + if (!globals.bDebugLog) { boost::system::error_code e; boost::filesystem::remove(path, e); } - HistoryLog(hContact, db_event(msg, timestamp, 0, dbflags)); + HistoryLog(hContact, db_event(param->msg, param->timestamp, 0, dbflags)); + delete param; return; } -/* if (result == pxSuccessExitCodeInvalid) //sometime we have invalid return code after succesful decryption, this should be non-fatal at least - { - if(!bDebugLog) - { - boost::system::error_code e; - boost::filesystem::remove(path, e); - } - - HistoryLog(hContact, db_event(msg, timestamp, 0, dbflags)); - HistoryLog(hContact, db_event(Translate("failed to decrypt message, GPG returned error, turn on debug log for more details"), timestamp, 0, 0)); - return; - } */ - //TODO: check gpg output for errors + // TODO: check gpg output for errors globals._terminate = false; - while (out.find("public key decryption failed: bad passphrase") != string::npos) - { + while (out.find("public key decryption failed: bad passphrase") != string::npos) { if (globals.bDebugLog) globals.debuglog << std::string(time_str() + ": info: failed to decrypt messaage from " + toUTF8(Clist_GetContactDisplayName(hContact)) + " password needed, trying to get one"); if (globals._terminate) { @@ -218,8 +209,7 @@ void RecvMsgSvc_func(MCONTACT hContact, std::wstring str, char *msg, DWORD, DWOR CDlgKeyPasswordMsgBox *d = new CDlgKeyPasswordMsgBox(hContact); d->DoModal(); std::vector<wstring> cmd2 = cmd; - if (globals.password) - { + if (globals.password) { if (globals.bDebugLog) globals.debuglog << std::string(time_str() + ": info: found password in memory, trying to decrypt message from " + toUTF8(Clist_GetContactDisplayName(hContact))); std::vector<wstring> tmp3; @@ -234,147 +224,142 @@ void RecvMsgSvc_func(MCONTACT hContact, std::wstring str, char *msg, DWORD, DWOR params2.code = &code; params2.result = &result2; if (!gpg_launcher(params2)) { - if(!globals.bDebugLog) - { + if (!globals.bDebugLog) { boost::system::error_code e; boost::filesystem::remove(path, e); } - HistoryLog(hContact, db_event(msg, timestamp, 0, dbflags)); + HistoryLog(hContact, db_event(param->msg, param->timestamp, 0, dbflags)); BYTE enc = db_get_b(hContact, MODULENAME, "GPGEncryption", 0); db_set_b(hContact, MODULENAME, "GPGEncryption", 0); ProtoChainSend(hContact, PSS_MESSAGE, 0, (LPARAM)"Unable to decrypt PGP encrypted message"); HistoryLog(hContact, db_event("Error message sent", 0, 0, DBEF_SENT)); db_set_b(hContact, MODULENAME, "GPGEncryption", enc); + delete param; return; } - if (result2 == pxNotFound) - { - if(!globals.bDebugLog) - { + if (result2 == pxNotFound) { + if (!globals.bDebugLog) { boost::system::error_code e; boost::filesystem::remove(path, e); } - HistoryLog(hContact, db_event(msg, timestamp, 0, dbflags)); + HistoryLog(hContact, db_event(param->msg, param->timestamp, 0, dbflags)); + delete param; return; } } out.clear(); - if (!gpg_launcher(params)) - { - if(!globals.bDebugLog) - { + if (!gpg_launcher(params)) { + if (!globals.bDebugLog) { boost::system::error_code e; boost::filesystem::remove(path, e); } - HistoryLog(hContact, db_event(msg, timestamp, 0, dbflags)); + HistoryLog(hContact, db_event(param->msg, param->timestamp, 0, dbflags)); BYTE enc = db_get_b(hContact, MODULENAME, "GPGEncryption", 0); db_set_b(hContact, MODULENAME, "GPGEncryption", 0); ProtoChainSend(hContact, PSS_MESSAGE, 0, (LPARAM)"Unable to decrypt PGP encrypted message"); HistoryLog(hContact, db_event("Error message sent", 0, 0, DBEF_SENT)); db_set_b(hContact, MODULENAME, "GPGEncryption", enc); + delete param; return; } - if (result == pxNotFound) - { - if(!globals.bDebugLog) - { + + if (result == pxNotFound) { + if (!globals.bDebugLog) { boost::system::error_code e; boost::filesystem::remove(path, e); } - HistoryLog(hContact, db_event(msg, timestamp, 0, dbflags)); + HistoryLog(hContact, db_event(param->msg, param->timestamp, 0, dbflags)); } - if(!globals.bDebugLog) - { + + if (!globals.bDebugLog) { boost::system::error_code e; boost::filesystem::remove(wstring(ptszHomePath) + L"\\tmp\\" + encfile, e); } - if (!boost::filesystem::exists(wstring(ptszHomePath) + L"\\tmp\\" + decfile)) - { - string str1 = msg; + if (!boost::filesystem::exists(wstring(ptszHomePath) + L"\\tmp\\" + decfile)) { + string str1 = param->msg; str1.insert(0, "Received unencrypted message:\n"); if (globals.bDebugLog) globals.debuglog << std::string(time_str() + ": info: Failed to decrypt GPG encrypted message."); ptrA tmp4((char*)mir_alloc(sizeof(char)*(str1.length() + 1))); mir_strcpy(tmp4, str1.c_str()); - HistoryLog(hContact, db_event(msg, timestamp, 0, dbflags)); + HistoryLog(hContact, db_event(param->msg, param->timestamp, 0, dbflags)); BYTE enc = db_get_b(hContact, MODULENAME, "GPGEncryption", 0); db_set_b(hContact, MODULENAME, "GPGEncryption", 0); ProtoChainSend(hContact, PSS_MESSAGE, 0, (LPARAM)"Unable to decrypt PGP encrypted message"); HistoryLog(hContact, db_event("Error message sent", 0, 0, DBEF_SENT)); db_set_b(hContact, MODULENAME, "GPGEncryption", enc); + delete param; return; } - str.clear(); + param->str.clear(); wstring tszDecPath = wstring(ptszHomePath) + L"\\tmp\\" + decfile; { fstream f(tszDecPath.c_str(), std::ios::in | std::ios::ate | std::ios::binary); - if (f.is_open()) - { + if (f.is_open()) { size_t size = f.tellg(); char *tmp = new char[size + 1]; f.seekg(0, std::ios::beg); f.read(tmp, size); tmp[size] = '\0'; toUTF16(tmp); - str.append(toUTF16(tmp)); + param->str.append(toUTF16(tmp)); delete[] tmp; f.close(); - if(!globals.bDebugLog) - { + if (!globals.bDebugLog) { boost::system::error_code ec; boost::filesystem::remove(tszDecPath, ec); - if(ec) - { + if (ec) { //TODO: handle error } } } } - if (str.empty()) - { - string szMsg = msg; + if (param->str.empty()) { + string szMsg = param->msg; szMsg.insert(0, "Failed to decrypt GPG encrypted message.\nMessage body for manual decryption:\n"); if (globals.bDebugLog) globals.debuglog << std::string(time_str() + ": info: Failed to decrypt GPG encrypted message."); - HistoryLog(hContact, db_event(msg, timestamp, 0, dbflags)); + HistoryLog(hContact, db_event(param->msg, param->timestamp, 0, dbflags)); BYTE enc = db_get_b(hContact, MODULENAME, "GPGEncryption", 0); db_set_b(hContact, MODULENAME, "GPGEncryption", 0); ProtoChainSend(hContact, PSS_MESSAGE, 0, (LPARAM)"Unable to decrypt PGP encrypted message"); HistoryLog(hContact, db_event("Error message sent", 0, 0, DBEF_SENT)); db_set_b(hContact, MODULENAME, "GPGEncryption", enc); + delete param; return; } - fix_line_term(str); - if (globals.bAppendTags) - { - str.insert(0, globals.inopentag); - str.append(globals.inclosetag); + fix_line_term(param->str); + if (globals.bAppendTags) { + param->str.insert(0, globals.inopentag); + param->str.append(globals.inclosetag); } - char *tmp = mir_strdup(toUTF8(str).c_str()); - HistoryLog(hContact, db_event(tmp, timestamp, 0, dbflags)); + char *tmp = mir_strdup(toUTF8(param->str).c_str()); + HistoryLog(hContact, db_event(tmp, param->timestamp, 0, dbflags)); mir_free(tmp); + delete param; return; } } } - if (db_get_b(db_mc_isMeta(hContact) ? metaGetMostOnline(hContact) : hContact, MODULENAME, "GPGEncryption", 0)) - { - HistoryLog(hContact, db_event(msg, timestamp, 0, dbflags | DBEF_READ)); + if (db_get_b(db_mc_isMeta(hContact) ? metaGetMostOnline(hContact) : hContact, MODULENAME, "GPGEncryption", 0)) { + HistoryLog(hContact, db_event(param->msg, param->timestamp, 0, dbflags | DBEF_READ)); + delete param; return; } - HistoryLog(hContact, db_event(msg, timestamp, 0, dbflags)); - return; + + HistoryLog(hContact, db_event(param->msg, param->timestamp, 0, dbflags)); + delete param; } INT_PTR RecvMsgSvc(WPARAM w, LPARAM l) @@ -389,12 +374,10 @@ INT_PTR RecvMsgSvc(WPARAM w, LPARAM l) if (!msg) return Proto_ChainRecv(w, ccs); DWORD dbflags = DBEF_UTF; - if (db_mc_isMeta(ccs->hContact)) - { + if (db_mc_isMeta(ccs->hContact)) { if (!strstr(msg, "-----BEGIN PGP MESSAGE-----")) return Proto_ChainRecv(w, ccs); - else - { + else { if (globals.bDebugLog) globals.debuglog << std::string(time_str() + ": info: blocked pgp message to metacontact:" + toUTF8(Clist_GetContactDisplayName(ccs->hContact))); return 0; @@ -402,14 +385,12 @@ INT_PTR RecvMsgSvc(WPARAM w, LPARAM l) } wstring str = toUTF16(msg); size_t s1, s2; - if (globals.bAutoExchange && (str.find(L"-----PGP KEY RESPONSE-----") != wstring::npos)) - { + if (globals.bAutoExchange && (str.find(L"-----PGP KEY RESPONSE-----") != wstring::npos)) { if (globals.bDebugLog) globals.debuglog << std::string(time_str() + ": info(autoexchange): parsing key response:" + toUTF8(Clist_GetContactDisplayName(ccs->hContact))); s2 = str.find(L"-----END PGP PUBLIC KEY BLOCK-----"); s1 = str.find(L"-----BEGIN PGP PUBLIC KEY BLOCK-----"); - if (s1 != wstring::npos && s2 != wstring::npos) - { + if (s1 != wstring::npos && s2 != wstring::npos) { if (globals.bDebugLog) globals.debuglog << std::string(time_str() + ": info(autoexchange): found pubkey block:" + toUTF8(Clist_GetContactDisplayName(ccs->hContact))); s2 += mir_wstrlen(L"-----END PGP PUBLIC KEY BLOCK-----"); @@ -429,8 +410,7 @@ INT_PTR RecvMsgSvc(WPARAM w, LPARAM l) mir_wstrcat(tmp2, L".asc"); mir_free(tmp3); //mir_wstrcat(tmp2, L"temporary_exported.asc"); - if(!globals.bDebugLog) - { + if (!globals.bDebugLog) { boost::system::error_code e; boost::filesystem::remove(tmp2, e); } @@ -438,16 +418,14 @@ INT_PTR RecvMsgSvc(WPARAM w, LPARAM l) { const int timeout = 5000, step = 100; int count = 0; - while (!f.is_open()) - { - boost::this_thread::sleep(boost::posix_time::milliseconds(step)); + while (!f.is_open()) { + ::Sleep(step); count += step; - if(count >= timeout) - { + if (count >= timeout) { db_set_b(ccs->hContact, MODULENAME, "GPGEncryption", 0); setSrmmIcon(ccs->hContact); setClistIcon(ccs->hContact); - globals.debuglog<<std::string(time_str() + "info: failed to create temporary file for decryption, disabling gpg for contact to avoid deadlock"); + globals.debuglog << std::string(time_str() + "info: failed to create temporary file for decryption, disabling gpg for contact to avoid deadlock"); return 1; } f.open(tmp2, std::ios::out); @@ -467,18 +445,17 @@ INT_PTR RecvMsgSvc(WPARAM w, LPARAM l) params.result = &result; if (!gpg_launcher(params)) return 1; - if(!globals.bDebugLog) - { + if (!globals.bDebugLog) { boost::system::error_code e; boost::filesystem::remove(tmp2, e); } if (result == pxNotFound) return 1; -/* if (result == pxSuccessExitCodeInvalid) //sometime we have invalid return code after succesful decryption, this should be non-fatal at least - { - HistoryLog(ccs->hContact, db_event(Translate("failed to decrypt message, GPG returned error, turn on debug log for more details"))); - return 1; - } */ + /* if (result == pxSuccessExitCodeInvalid) //sometime we have invalid return code after succesful decryption, this should be non-fatal at least + { + HistoryLog(ccs->hContact, db_event(Translate("failed to decrypt message, GPG returned error, turn on debug log for more details"))); + return 1; + } */ { char *tmp = nullptr; s1 = output.find("gpg: key ") + mir_strlen("gpg: key "); @@ -486,8 +463,7 @@ INT_PTR RecvMsgSvc(WPARAM w, LPARAM l) db_set_s(ccs->hContact, MODULENAME, "KeyID", output.substr(s1, s2 - s1).c_str()); s2 += 2; s1 = output.find("“", s2); - if (s1 == string::npos) - { + if (s1 == string::npos) { s1 = output.find("\"", s2); s1 += 1; } @@ -507,8 +483,7 @@ INT_PTR RecvMsgSvc(WPARAM w, LPARAM l) else if (s1 > output.find(">", s2)) s1 = output.find(">", s2); s2++; - if (output[s1] == ')') - { + if (output[s1] == ')') { tmp = (char*)mir_alloc(output.substr(s2, s1 - s2).length() + 1); mir_strcpy(tmp, output.substr(s2, s1 - s2).c_str()); mir_utf8decode(tmp, nullptr); @@ -522,8 +497,7 @@ INT_PTR RecvMsgSvc(WPARAM w, LPARAM l) db_set_s(ccs->hContact, MODULENAME, "KeyMainEmail", tmp); mir_free(tmp); } - else - { + else { tmp = (char*)mir_alloc(output.substr(s2, s1 - s2).length() + 1); mir_strcpy(tmp, output.substr(s2, s1 - s2).c_str()); mir_utf8decode(tmp, nullptr); @@ -534,8 +508,7 @@ INT_PTR RecvMsgSvc(WPARAM w, LPARAM l) db_set_b(ccs->hContact, MODULENAME, "bAlwatsTrust", 1); setSrmmIcon(ccs->hContact); setClistIcon(ccs->hContact); - if (db_mc_isSub(ccs->hContact)) - { + if (db_mc_isSub(ccs->hContact)) { setSrmmIcon(db_mc_getMeta(ccs->hContact)); setClistIcon(db_mc_getMeta(ccs->hContact)); } @@ -545,13 +518,11 @@ INT_PTR RecvMsgSvc(WPARAM w, LPARAM l) return 1; } } - if (((s2 = str.find(L"-----END PGP PUBLIC KEY BLOCK-----")) == wstring::npos) || ((s1 = str.find(L"-----BEGIN PGP PUBLIC KEY BLOCK-----")) == wstring::npos)) - { + if (((s2 = str.find(L"-----END PGP PUBLIC KEY BLOCK-----")) == wstring::npos) || ((s1 = str.find(L"-----BEGIN PGP PUBLIC KEY BLOCK-----")) == wstring::npos)) { s2 = str.find(L"-----END PGP PRIVATE KEY BLOCK-----"); s1 = str.find(L"-----BEGIN PGP PRIVATE KEY BLOCK-----"); } - if ((s2 != wstring::npos) && (s1 != wstring::npos)) - { //this is public key + if ((s2 != wstring::npos) && (s1 != wstring::npos)) { //this is public key if (globals.bDebugLog) globals.debuglog << std::string(time_str() + ": info: received key from: " + toUTF8(Clist_GetContactDisplayName(ccs->hContact))); s1 = 0; @@ -566,8 +537,7 @@ INT_PTR RecvMsgSvc(WPARAM w, LPARAM l) HistoryLog(ccs->hContact, db_event(msg, 0, 0, dbflags)); return 0; } - if (globals.bAutoExchange && strstr(msg, "-----PGP KEY REQUEST-----") && globals.gpg_valid && globals.gpg_keyexist) - { + if (globals.bAutoExchange && strstr(msg, "-----PGP KEY REQUEST-----") && globals.gpg_valid && globals.gpg_keyexist) { if (globals.bDebugLog) globals.debuglog << std::string(time_str() + ": info(autoexchange): received key request from: " + toUTF8(Clist_GetContactDisplayName(ccs->hContact))); @@ -585,44 +555,34 @@ INT_PTR RecvMsgSvc(WPARAM w, LPARAM l) } return 0; } - else if (!isContactHaveKey(ccs->hContact) && globals.bAutoExchange && globals.gpg_valid && globals.gpg_keyexist) - { + else if (!isContactHaveKey(ccs->hContact) && globals.bAutoExchange && globals.gpg_valid && globals.gpg_keyexist) { char *proto = GetContactProto(ccs->hContact); DWORD uin = db_get_dw(ccs->hContact, proto, "UIN", 0); - if (uin) - { - if (ProtoServiceExists(proto, PS_ICQ_CHECKCAPABILITY)) - { + if (uin) { + if (ProtoServiceExists(proto, PS_ICQ_CHECKCAPABILITY)) { ICQ_CUSTOMCAP cap = { 0 }; strncpy(cap.caps, "GPGAutoExchange", sizeof(cap.caps)); - if (CallProtoService(proto, PS_ICQ_CHECKCAPABILITY, (WPARAM)ccs->hContact, (LPARAM)&cap)) - { + if (CallProtoService(proto, PS_ICQ_CHECKCAPABILITY, (WPARAM)ccs->hContact, (LPARAM)&cap)) { ProtoChainSend(ccs->hContact, PSS_MESSAGE, 0, (LPARAM)"-----PGP KEY REQUEST-----"); return 0; } } } - else - { + else { wchar_t *jid = UniGetContactSettingUtf(ccs->hContact, proto, "jid", L""); - if (jid[0]) - { - for(auto p : globals.Accounts) - { + if (jid[0]) { + for (auto p : globals.Accounts) { wchar_t *caps = p->getJabberInterface()->GetResourceFeatures(jid); - if (caps) - { + if (caps) { wstring str1; - for (int i = 0;; i++) - { + for (int i = 0;; i++) { str1.push_back(caps[i]); if (caps[i] == '\0') if (caps[i + 1] == '\0') break; } mir_free(caps); - if (str1.find(L"GPG_Key_Auto_Exchange:0") != string::npos) - { + if (str1.find(L"GPG_Key_Auto_Exchange:0") != string::npos) { ProtoChainSend(ccs->hContact, PSS_MESSAGE, 0, (LPARAM)"-----PGP KEY REQUEST-----"); return 0; } @@ -635,7 +595,7 @@ INT_PTR RecvMsgSvc(WPARAM w, LPARAM l) if (!strstr(msg, "-----BEGIN PGP MESSAGE-----")) return Proto_ChainRecv(w, ccs); - new boost::thread(boost::bind(RecvMsgSvc_func, ccs->hContact, str, msg, (DWORD)ccs->wParam, pre->timestamp)); + mir_forkThread<RecvParams>(RecvMsgSvc_func, new RecvParams(ccs->hContact, str, msg, pre->timestamp)); return 0; } @@ -695,10 +655,9 @@ void SendMsgSvc_func(MCONTACT hContact, char *msg, DWORD flags) { fstream f(path.c_str(), std::ios::out); while (!f.is_open()) { - boost::this_thread::sleep(boost::posix_time::milliseconds(step)); + ::Sleep(step); count += step; - if(count >= timeout) - { + if (count >= timeout) { db_set_b(hContact, MODULENAME, "GPGEncryption", 0); //disable encryption setSrmmIcon(hContact); setClistIcon(hContact); @@ -707,8 +666,7 @@ void SendMsgSvc_func(MCONTACT hContact, char *msg, DWORD flags) } f.open(path.c_str(), std::ios::out); } - if(count < timeout) - { + if (count < timeout) { std::string tmp = toUTF8(str); f.write(tmp.c_str(), tmp.size()); f.close(); @@ -756,41 +714,31 @@ void SendMsgSvc_func(MCONTACT hContact, char *msg, DWORD flags) } else return; } -// if (result == pxSuccessExitCodeInvalid) { //sometims gpg return error after succesful operation, this should be non-fatal at least - //mir_free(msg); -// HistoryLog(hContact, db_event(Translate("failed to encrypt message, GPG returned error, turn on debug log for more details"), 0, 0, DBEF_SENT)); -// if(!bDebugLog) -// { -// boost::system::error_code e; -// boost::filesystem::remove(path, e); -// } -// return; -// } + if (out.find("usage: ") != string::npos) { MessageBox(nullptr, TranslateT("Something is wrong, GPG does not understand us, aborting encryption."), TranslateT("Warning"), MB_OK); //mir_free(msg); ProtoChainSend(hContact, PSS_MESSAGE, flags, (LPARAM)msg); - if(!globals.bDebugLog) - { + if (!globals.bDebugLog) { boost::system::error_code e; boost::filesystem::remove(path, e); } return; } - if(!globals.bDebugLog) - { + + if (!globals.bDebugLog) { boost::system::error_code e; boost::filesystem::remove(path, e); } + path.append(L".asc"); wfstream f(path.c_str(), std::ios::in | std::ios::ate | std::ios::binary); count = 0; while (!f.is_open()) { - boost::this_thread::sleep(boost::posix_time::milliseconds(step)); + ::Sleep(step); f.open(path.c_str(), std::ios::in | std::ios::ate | std::ios::binary); count += step; - if(count >= timeout) - { + if (count >= timeout) { db_set_b(hContact, MODULENAME, "GPGEncryption", 0); //disable encryption setSrmmIcon(hContact); setClistIcon(hContact); @@ -798,6 +746,7 @@ void SendMsgSvc_func(MCONTACT hContact, char *msg, DWORD flags) break; } } + str.clear(); if (f.is_open()) { std::wifstream::pos_type size = f.tellg(); @@ -808,12 +757,12 @@ void SendMsgSvc_func(MCONTACT hContact, char *msg, DWORD flags) str.append(tmp); delete[] tmp; f.close(); - if(!globals.bDebugLog) - { + if (!globals.bDebugLog) { boost::system::error_code e; boost::filesystem::remove(path, e); } } + if (str.empty()) { HistoryLog(hContact, db_event("Failed to encrypt message with GPG", 0, 0, DBEF_SENT)); if (globals.bDebugLog) @@ -821,6 +770,7 @@ void SendMsgSvc_func(MCONTACT hContact, char *msg, DWORD flags) ProtoChainSend(hContact, PSS_MESSAGE, flags, (LPARAM)msg); return; } + string str_event = msg; if (globals.bAppendTags) { str_event.insert(0, toUTF8(globals.outopentag)); @@ -839,40 +789,47 @@ INT_PTR SendMsgSvc(WPARAM w, LPARAM l) CCSDATA *ccs = (CCSDATA*)l; if (!ccs) return Proto_ChainSend(w, ccs); + if (!ccs->lParam) return Proto_ChainSend(w, ccs); + char *msg = (char*)ccs->lParam; if (!msg) { if (globals.bDebugLog) globals.debuglog << std::string(time_str() + ": info: failed to get message data, name: " + toUTF8(Clist_GetContactDisplayName(ccs->hContact))); return Proto_ChainSend(w, ccs); } + if (strstr(msg, "-----BEGIN PGP MESSAGE-----")) { if (globals.bDebugLog) globals.debuglog << std::string(time_str() + ": info: encrypted messge, let it go, name: " + toUTF8(Clist_GetContactDisplayName(ccs->hContact))); return Proto_ChainSend(w, ccs); } + if (globals.bDebugLog) globals.debuglog << std::string(time_str() + ": info: contact have key, name: " + toUTF8(Clist_GetContactDisplayName(ccs->hContact))); + if (globals.bDebugLog && db_mc_isMeta(ccs->hContact)) globals.debuglog << std::string(time_str() + ": info: protocol is metacontacts, name: " + toUTF8(Clist_GetContactDisplayName(ccs->hContact))); + if (!isContactSecured(ccs->hContact) || db_mc_isMeta(ccs->hContact)) { if (globals.bDebugLog) globals.debuglog << std::string(time_str() + ": info: contact not secured, name: " + toUTF8(Clist_GetContactDisplayName(ccs->hContact))); return Proto_ChainSend(w, ccs); } + return returnNoError(ccs->hContact); } -boost::mutex event_processing_mutex; - int HookSendMsg(WPARAM w, LPARAM l) { if (!l) return 0; + DBEVENTINFO * dbei = (DBEVENTINFO*)l; if (dbei->eventType != EVENTTYPE_MESSAGE) return 0; + MCONTACT hContact = (MCONTACT)w; if (dbei->flags & DBEF_SENT) { if (isContactSecured(hContact) && strstr((char*)dbei->pBlob, "-----BEGIN PGP MESSAGE-----")) //our service data, can be double added by metacontacts e.w.c. @@ -888,6 +845,7 @@ int HookSendMsg(WPARAM w, LPARAM l) return 1; } } + if (db_mc_isMeta(hContact)) return 0; @@ -897,7 +855,7 @@ int HookSendMsg(WPARAM w, LPARAM l) if (globals.bAutoExchange && !strstr((char*)dbei->pBlob, "-----PGP KEY REQUEST-----") && !strstr((char*)dbei->pBlob, "-----BEGIN PGP PUBLIC KEY BLOCK-----") && globals.gpg_valid) { if (globals.bDebugLog) globals.debuglog << std::string(time_str() + ": info: checking for autoexchange possibility, name: " + toUTF8(Clist_GetContactDisplayName(hContact))); - + LPSTR proto = GetContactProto(hContact); DWORD uin = db_get_dw(hContact, proto, "UIN", 0); if (uin) { @@ -915,7 +873,7 @@ int HookSendMsg(WPARAM w, LPARAM l) globals.debuglog << std::string(time_str() + ": info(autoexchange, icq): sending key requiest, name: " + toUTF8(Clist_GetContactDisplayName(hContact))); ProtoChainSend(hContact, PSS_MESSAGE, 0, (LPARAM)"-----PGP KEY REQUEST-----"); globals.hcontact_data[hContact].msgs_to_send.push_back((char*)dbei->pBlob); - new boost::thread(boost::bind(send_encrypted_msgs_thread, (void*)hContact)); + mir_forkthread(send_encrypted_msgs_thread, (void*)hContact); return 0; } } @@ -925,8 +883,7 @@ int HookSendMsg(WPARAM w, LPARAM l) if (jid[0]) { if (globals.bDebugLog) globals.debuglog << std::string(time_str() + ": info(autoexchange): protocol looks like jabber, name: " + toUTF8(Clist_GetContactDisplayName(hContact))); - for(auto p : globals.Accounts) - { + for (auto p : globals.Accounts) { wchar_t *caps = p->getJabberInterface()->GetResourceFeatures(jid); if (caps) { wstring str; @@ -942,7 +899,7 @@ int HookSendMsg(WPARAM w, LPARAM l) globals.debuglog << std::string(time_str() + ": info(autoexchange, jabber): autoexchange capability found, sending key request, name: " + toUTF8(Clist_GetContactDisplayName(hContact))); ProtoChainSend(hContact, PSS_MESSAGE, 0, (LPARAM)"-----PGP KEY REQUEST-----"); globals.hcontact_data[hContact].msgs_to_send.push_back((char*)dbei->pBlob); - new boost::thread(boost::bind(send_encrypted_msgs_thread, (void*)hContact)); + mir_forkthread(send_encrypted_msgs_thread, (void*)hContact); return 0; } } @@ -951,10 +908,9 @@ int HookSendMsg(WPARAM w, LPARAM l) mir_free(jid); } } - else { - return 0; - } + else return 0; } + if (isContactSecured(hContact) && (dbei->flags & DBEF_SENT)) //aggressive outgoing events filtering { SendMsgSvc_func(hContact, (char*)dbei->pBlob, 0); @@ -970,11 +926,13 @@ int HookSendMsg(WPARAM w, LPARAM l) return 0; } + if (!isContactSecured(hContact)) { if (globals.bDebugLog) globals.debuglog << std::string(time_str() + ": event message: \"" + (char*)dbei->pBlob + "\" passed event filter, contact " + toUTF8(Clist_GetContactDisplayName(hContact)) + " is unsecured"); return 0; } + if (!(dbei->flags & DBEF_SENT) && db_mc_isMeta((MCONTACT)w)) { char tmp[29]; strncpy(tmp, (char*)dbei->pBlob, 27); @@ -987,4 +945,3 @@ int HookSendMsg(WPARAM w, LPARAM l) } return 0; } - |