From dd4c07b6f71dc074dabef6b27e5dd4c49d882bb2 Mon Sep 17 00:00:00 2001 From: Gluzskiy Alexandr Date: Fri, 8 Feb 2013 22:52:02 +0200 Subject: merged with miranda_ng main repo "boosted" process management evnt_to_db/history>actual_sending logic to avoid srmm problem --- messages.cpp | 388 ++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 224 insertions(+), 164 deletions(-) (limited to 'messages.cpp') diff --git a/messages.cpp b/messages.cpp index 1821035..7a4667f 100755 --- a/messages.cpp +++ b/messages.cpp @@ -86,7 +86,7 @@ void RecvMsgSvc_func(HANDLE hContact, std::wstring str, char *msg, DWORD flags, wstring decfile = toUTF16(get_random(10)); path.append(_T("\\tmp\\")); path.append(encfile); - DeleteFile(path.c_str()); + boost::filesystem::remove(path); fstream f(path.c_str(), std::ios::out); while(!f.is_open()) f.open(path.c_str(), std::ios::out); @@ -97,8 +97,8 @@ void RecvMsgSvc_func(HANDLE hContact, std::wstring str, char *msg, DWORD flags, extern TCHAR *password; string out; DWORD code; - wstring cmd; - cmd += _T("--batch "); + std::vector cmd; + cmd.push_back(L"--batch"); { char *inkeyid = UniGetContactSettingUtf(metaIsProtoMetaContacts(hContact)?metaGetMostOnline(hContact):hContact, szGPGModuleName, "InKeyID", ""); TCHAR *pass = NULL; @@ -119,17 +119,15 @@ void RecvMsgSvc_func(HANDLE hContact, std::wstring str, char *msg, DWORD flags, } if(pass && pass[0]) { - cmd += _T("--passphrase \""); - cmd += pass; - cmd += _T("\" "); + cmd.push_back(L"--passphrase"); + cmd.push_back(pass); } else if(password && password[0]) { if(bDebugLog) debuglog<timed_join(boost::posix_time::seconds(10))) + if(!gpg_launcher(params)) { - delete gpg_thread; - TerminateProcess(params.hProcess, 1); - params.hProcess = NULL; - if(bDebugLog) - debuglog< cmd2 = cmd; if(password) { if(bDebugLog) debuglog< tmp; + tmp.push_back(L"--passphrase"); + tmp.push_back(password); + cmd2.insert(cmd2.begin(), tmp.begin(), tmp.end()); } out.clear(); - gpg_execution_params params; + gpg_execution_params params(cmd2); pxResult result; - params.cmd = &cmd2; - params.useless = ""; params.out = &out; params.code = &code; params.result = &result; - gpg_thread = gpg_thread = new boost::thread(boost::bind(&pxEexcute_thread, ¶ms)); - if(!gpg_thread->timed_join(boost::posix_time::seconds(10))) + if(!gpg_launcher(params)) { - delete gpg_thread; - TerminateProcess(params.hProcess, 1); - params.hProcess = NULL; - if(bDebugLog) - debuglog<timed_join(boost::posix_time::seconds(10))) + if(!gpg_launcher(params)) { - delete gpg_thread; - TerminateProcess(params.hProcess, 1); - params.hProcess = NULL; - if(bDebugLog) - debuglog<hContact, szGPGModuleName, "GPGPubKey", str.substr(s1,s2-s1).c_str()); { //gpg execute block - wstring cmd; + std::vector cmd; TCHAR tmp2[MAX_PATH] = {0}; TCHAR *ptmp; string output; @@ -439,7 +409,7 @@ int RecvMsgSvc(WPARAM w, LPARAM l) _tcscat(tmp2, _T(".asc")); mir_free(tmp3); //_tcscat(tmp2, _T("temporary_exported.asc")); - DeleteFile(tmp2); + boost::filesystem::remove(tmp2); wfstream f(tmp2, std::ios::out); while(!f.is_open()) f.open(tmp2, std::ios::out); @@ -448,29 +418,18 @@ int RecvMsgSvc(WPARAM w, LPARAM l) mir_free(ptmp); f< cmd; extern bool bJabberAPI, bIsMiranda09; char *tmp = UniGetContactSettingUtf(hContact, szGPGModuleName, "KeyID", ""); if(!tmp[0]) { mir_free(tmp); HistoryLog(hContact, db_event("Failed to encrypt message with GPG (not found key for encryption in db)", 0,0, DBEF_SENT)); - hcontact_data[hContact].msgs_to_pass.push_back("Failed to encrypt message with GPG (not found key for encryption in db)"); - mir_free(msg); +// hcontact_data[hContact].msgs_to_pass.push_back("Failed to encrypt message with GPG (not found key for encryption in db)"); + //mir_free(msg); CallContactService(hContact, PSS_MESSAGE, (WPARAM)flags, (LPARAM)msg); return; } if(!bJabberAPI || !bIsMiranda09) //force jabber to handle encrypted message by itself - cmd += _T("--comment \"\" --no-version "); + { + cmd.push_back(L"--comment"); + cmd.push_back(L"\"\""); + cmd.push_back(L"--no-version"); + } if(DBGetContactSettingByte(hContact, szGPGModuleName, "bAlwaysTrust", 0)) - cmd += _T("--trust-model always "); - cmd += _T("--batch --yes -e -a -t -r "); + { + cmd.push_back(L"--trust-model"); + cmd.push_back(L"always"); + } + cmd.push_back(L"--batch"); + cmd.push_back(L"--yes"); + cmd.push_back(L"-eatr"); TCHAR *tmp2 = mir_a2t(tmp); mir_free(tmp); - cmd += tmp2; + cmd.push_back(tmp2); mir_free(tmp2); - cmd += _T(" \""); tmp2 = UniGetContactSettingUtf(NULL, szGPGModuleName, "szHomePath", _T("")); - path.append(tmp2); - cmd += tmp2; + path = tmp2; + cmd.push_back(std::wstring(tmp2) + L"\\tmp\\" + file); mir_free(tmp2); - cmd += _T("\\tmp\\"); - cmd += file; - path.append(_T("\\tmp\\")); + path += L"\\tmp\\"; path += file; - cmd += _T("\""); { fstream f(path.c_str(), std::ios::out); while(!f.is_open()) @@ -727,28 +690,20 @@ void SendMsgSvc_func(HANDLE hContact, char *msg, DWORD flags) f.write(tmp.c_str(), tmp.size()); f.close(); } - gpg_execution_params params; + gpg_execution_params params(cmd); pxResult result; - params.cmd = &cmd; - params.useless = ""; params.out = &out; params.code = &code; params.result = &result; - boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms)); - if(!gpg_thread.timed_join(boost::posix_time::seconds(10))) + if(!gpg_launcher(params)) { - gpg_thread.~thread(); - TerminateProcess(params.hProcess, 1); - params.hProcess = NULL; - if(bDebugLog) - debuglog< tmp; + tmp.push_back(L"--trust-model"); + tmp.push_back(L"always"); + cmd.insert(cmd.begin(), tmp.begin(), tmp.end()); + gpg_execution_params params(cmd); pxResult result; - params.cmd = &cmd; - params.useless = ""; params.out = &out; params.code = &code; params.result = &result; - boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms)); - if(!gpg_thread.timed_join(boost::posix_time::seconds(10))) + if(!gpg_launcher(params)) { - gpg_thread.~thread(); - TerminateProcess(params.hProcess, 1); - params.hProcess = NULL; - if(bDebugLog) - debuglog<hContact, GCDNF_TCHAR))); return CallService(MS_PROTO_CHAINSEND, w, l); } - if(!isContactHaveKey(ccs->hContact)) + /*if(!isContactHaveKey(ccs->hContact)) { if(bDebugLog) debuglog<hContact, GCDNF_TCHAR))); @@ -969,7 +920,7 @@ int SendMsgSvc(WPARAM w, LPARAM l) mir_free(msg); return CallService(MS_PROTO_CHAINSEND, w, l); } - } + } */ else if(bDebugLog) debuglog<hContact, GCDNF_TCHAR))); if(bDebugLog && metaIsProtoMetaContacts(ccs->hContact)) @@ -981,7 +932,8 @@ int SendMsgSvc(WPARAM w, LPARAM l) mir_free(msg); return CallService(MS_PROTO_CHAINSEND, w, l); } - boost::thread *thr = new boost::thread(boost::bind(SendMsgSvc_func, ccs->hContact, msg, (DWORD)ccs->wParam)); + mir_free(msg); + //boost::thread *thr = new boost::thread(boost::bind(SendMsgSvc_func, ccs->hContact, msg, (DWORD)ccs->wParam)); return returnNoError(ccs->hContact); } @@ -1010,28 +962,136 @@ int HookSendMsg(WPARAM w, LPARAM l) return 1; } } - if(isContactSecured(hContact) && (dbei->flags & DBEF_SENT)) //aggressive outgoing events filtering + if(metaIsProtoMetaContacts(hContact)) + return 0; + + if(!isContactHaveKey(hContact)) { - if(!hcontact_data[hContact].msgs_to_pass.empty()) + if(bDebugLog) + debuglog<pBlob, "-----PGP KEY REQUEST-----") && !strstr((char*)dbei->pBlob, "-----BEGIN PGP PUBLIC KEY BLOCK-----") && gpg_valid) { - event_processing_mutex.lock(); - std::list::iterator end = hcontact_data[hContact].msgs_to_pass.end(); - for(std::list::iterator i = hcontact_data[hContact].msgs_to_pass.begin(); i != end; ++i) + if(bDebugLog) + debuglog<flags & DBEF_UTF) == DBEF_UTF)?PREF_UTF:0, (LPARAM)"-----PGP KEY REQUEST-----"); + hcontact_data[hContact].msgs_to_send.push_back((char*)dbei->pBlob); + boost::thread *thr = new boost::thread(boost::bind(send_encrypted_msgs_thread, hContact)); + //TODO: wait for message + return 0; + } + } + } + else { - if(!strcmp((*i).c_str(), (char*)dbei->pBlob)) + TCHAR *jid = UniGetContactSettingUtf(hContact, proto, "jid", _T("")); + if(jid[0]) { - hcontact_data[hContact].msgs_to_pass.erase(i); if(bDebugLog) - debuglog<pBlob+"\" passed event filter, contact "+toUTF8((TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)hContact, GCDNF_TCHAR))+", message is in allowed list"); - event_processing_mutex.unlock(); - return 0; + debuglog< Accounts; + list::iterator end = Accounts.end(); + for(list::iterator p = Accounts.begin(); p != end; p++) + { + TCHAR *caps = (*p)->getJabberInterface()->Net()->GetResourceFeatures(jid); + if(caps) + { + wstring str; + for(int i=0;;i++) + { + str.push_back(caps[i]); + if(caps[i] == '\0') + if(caps[i+1] == '\0') + break; + } + mir_free(caps); + if(str.find(_T("GPG_Key_Auto_Exchange:0")) != string::npos) + { + if(bDebugLog) + debuglog<flags & DBEF_UTF) == DBEF_UTF)?PREF_UTF:0, (LPARAM)"-----PGP KEY REQUEST-----"); + hcontact_data[hContact].msgs_to_send.push_back((char*)dbei->pBlob); + boost::thread *thr = new boost::thread(boost::bind(send_encrypted_msgs_thread, hContact)); + mir_free((char*)dbei->pBlob); + //TODO: wait for message + return 0; + } + } + } } } - event_processing_mutex.unlock(); } - if(metaIsProtoMetaContacts(hContact) && !isContactSecured(metaGetMostOnline(hContact))) + else + { return 0; - return 1; + } + } + if(isContactSecured(hContact) && (dbei->flags & DBEF_SENT)) //aggressive outgoing events filtering + { + DWORD flags; + if((dbei->flags & DBEF_UTF) == DBEF_UTF) + flags |= PREF_UTF; + SendMsgSvc_func(hContact, (char*)dbei->pBlob, flags); + //TODO: handle errors somehow ... + if(bAppendTags) + { + string str_event = (char*)dbei->pBlob; + mir_free(dbei->pBlob); + str_event.insert(0, toUTF8(outopentag)); + str_event.append(toUTF8(outclosetag)); + dbei->pBlob = (PBYTE)mir_strdup(str_event.c_str()); + dbei->cbBlob = str_event.length() + 1; + } + + return 0; +/* bool stop = false; + int count = 0; */ +/* while(!stop) + { + if(count >= 300) + stop = true; + if(!hcontact_data[hContact].msgs_to_pass.empty()) + { + event_processing_mutex.lock(); + std::list::iterator end = hcontact_data[hContact].msgs_to_pass.end(); + for(std::list::iterator i = hcontact_data[hContact].msgs_to_pass.begin(); i != end; ++i) + { + if(!strcmp((*i).c_str(), (char*)dbei->pBlob)) + { + hcontact_data[hContact].msgs_to_pass.erase(i); + if(bDebugLog) + debuglog<pBlob+"\" passed event filter, contact "+toUTF8((TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)hContact, GCDNF_TCHAR))+", message is in allowed list"); + event_processing_mutex.unlock(); + return 0; + } + } + event_processing_mutex.unlock(); + } + boost::this_thread::sleep(boost::posix_time::milliseconds(100)); + count++; + } */ + //return 1; } if(!isContactSecured(hContact)) { -- cgit v1.2.3