diff options
Diffstat (limited to 'plugins/New_GPG')
-rw-r--r-- | plugins/New_GPG/src/clist.cpp | 4 | ||||
-rwxr-xr-x | plugins/New_GPG/src/gpg_wrapper.cpp | 105 | ||||
-rw-r--r-- | plugins/New_GPG/src/gpg_wrapper.h | 2 | ||||
-rwxr-xr-x | plugins/New_GPG/src/icons.cpp | 11 | ||||
-rwxr-xr-x | plugins/New_GPG/src/init.cpp | 74 | ||||
-rw-r--r-- | plugins/New_GPG/src/jabber_account.cpp | 35 | ||||
-rw-r--r-- | plugins/New_GPG/src/jabber_account.h | 20 | ||||
-rwxr-xr-x | plugins/New_GPG/src/log.cpp | 60 | ||||
-rw-r--r-- | plugins/New_GPG/src/log.h | 6 | ||||
-rwxr-xr-x | plugins/New_GPG/src/main.cpp | 16 | ||||
-rwxr-xr-x | plugins/New_GPG/src/messages.cpp | 361 | ||||
-rw-r--r-- | plugins/New_GPG/src/metacontacts.cpp | 6 | ||||
-rwxr-xr-x | plugins/New_GPG/src/options.cpp | 2 | ||||
-rwxr-xr-x | plugins/New_GPG/src/srmm.cpp | 37 | ||||
-rwxr-xr-x | plugins/New_GPG/src/stdafx.h | 17 | ||||
-rwxr-xr-x | plugins/New_GPG/src/ui.cpp | 136 | ||||
-rwxr-xr-x | plugins/New_GPG/src/utilities.cpp | 250 | ||||
-rwxr-xr-x | plugins/New_GPG/src/utilities.h | 4 | ||||
-rwxr-xr-x | plugins/New_GPG/src/version.h | 4 |
19 files changed, 534 insertions, 616 deletions
diff --git a/plugins/New_GPG/src/clist.cpp b/plugins/New_GPG/src/clist.cpp index 7dcbaf6a42..3960de9150 100644 --- a/plugins/New_GPG/src/clist.cpp +++ b/plugins/New_GPG/src/clist.cpp @@ -18,13 +18,13 @@ void RefreshContactListIcons(void); -int onExtraImageListRebuilding(WPARAM, LPARAM) +int onExtraImageListRebuilding(WPARAM, LPARAM) { RefreshContactListIcons(); return 0; } -int onExtraImageApplying(WPARAM wParam, LPARAM) +int onExtraImageApplying(WPARAM wParam, LPARAM) { setClistIcon(wParam); return 0; diff --git a/plugins/New_GPG/src/gpg_wrapper.cpp b/plugins/New_GPG/src/gpg_wrapper.cpp index 939c0bb6cc..f0dbbbf704 100755 --- a/plugins/New_GPG/src/gpg_wrapper.cpp +++ b/plugins/New_GPG/src/gpg_wrapper.cpp @@ -15,22 +15,18 @@ // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "stdafx.h" -//thx gpg module from Harald Treder, Zakhar V. Bardymov - pxResult pxExecute(std::vector<std::wstring> &aargv, string *aoutput, LPDWORD aexitcode, pxResult *result, boost::process::child *_child) { - if(!globals.gpg_valid) + if (!globals.gpg_valid) return pxNotConfigured; - wchar_t *bin_path = UniGetContactSettingUtf(NULL, MODULENAME, "szGpgBinPath", L""); { - if(!boost::filesystem::exists(bin_path)) - { + if (!boost::filesystem::exists(bin_path)) { mir_free(bin_path); - if(globals.bDebugLog) - globals.debuglog<<std::string(time_str()+": GPG executable not found"); + if (globals.bDebugLog) + globals.debuglog << std::string(time_str() + ": GPG executable not found"); *result = pxNotFound; return pxNotFound; } @@ -40,7 +36,6 @@ pxResult pxExecute(std::vector<std::wstring> &aargv, string *aoutput, LPDWORD ae using namespace boost::process::initializers; using namespace boost::iostreams; - std::vector<std::wstring> argv; std::vector<std::wstring> env; env.push_back(L"LANGUAGE=en@quot"); @@ -48,7 +43,7 @@ pxResult pxExecute(std::vector<std::wstring> &aargv, string *aoutput, LPDWORD ae env.push_back(L"LANG=C"); argv.push_back(bin_path); wchar_t *home_dir = UniGetContactSettingUtf(NULL, MODULENAME, "szHomePath", L""); - if(mir_wstrlen(home_dir)) //this check are required for first run gpg binary validation + if (mir_wstrlen(home_dir)) //this check are required for first run gpg binary validation { argv.push_back(L"--homedir"); argv.push_back(home_dir); @@ -59,19 +54,16 @@ pxResult pxExecute(std::vector<std::wstring> &aargv, string *aoutput, LPDWORD ae argv.push_back(L"-z9"); argv.insert(argv.end(), aargv.begin(), aargv.end()); - if(globals.bDebugLog) - { + if (globals.bDebugLog) { std::wstring args; - for(unsigned int i = 0; i < argv.size(); ++i) - { + for (unsigned int i = 0; i < argv.size(); ++i) { args += argv[i]; args += L" "; } - args.erase(args.size()-1, 1); - globals.debuglog<<std::string(time_str()+": gpg in: "+toUTF8(args)); + args.erase(args.size() - 1, 1); + globals.debuglog << std::string(time_str() + ": gpg in: " + toUTF8(args)); } - pipe pout = create_pipe(); pipe perr = create_pipe(); child *c = nullptr; @@ -79,107 +71,97 @@ pxResult pxExecute(std::vector<std::wstring> &aargv, string *aoutput, LPDWORD ae file_descriptor_sink sout(pout.sink, close_handle); file_descriptor_sink serr(perr.sink, close_handle); - wchar_t *mir_path = new wchar_t [MAX_PATH]; + wchar_t *mir_path = new wchar_t[MAX_PATH]; PathToAbsoluteW(L"\\", mir_path); c = new child(execute(set_args(argv), bind_stdout(sout), bind_stderr(serr), close_stdin(),/*bind_stdin(sin),*/ show_window(SW_HIDE), hide_console(), inherit_env(), set_env(env), start_in_dir(mir_path))); _child = c; - delete [] mir_path; + delete[] mir_path; } - file_descriptor_source source(pout.source, close_handle); stream<file_descriptor_source> is(source); - try{ + try { std::string s; - while(std::getline(is, s)) - { + while (std::getline(is, s)) { aoutput->append(s); aoutput->append("\n"); } } - catch(const std::exception &e) - { - if(globals.bDebugLog) - globals.debuglog<<std::string(time_str()+": failed to read from stream with error: " + e.what() + "\n\tSuccesfully read : " + *aoutput); + catch (const std::exception &e) { + if (globals.bDebugLog) + globals.debuglog << std::string(time_str() + ": failed to read from stream with error: " + e.what() + "\n\tSuccesfully read : " + *aoutput); } file_descriptor_source source2(perr.source, close_handle); stream<file_descriptor_source> is2(source2); - try{ + try { std::string s; - while(std::getline(is2, s)) - { + while (std::getline(is2, s)) { aoutput->append(s); aoutput->append("\n"); } } - catch(const std::exception &e) - { - if(globals.bDebugLog) - globals.debuglog<<std::string(time_str()+": failed to read from stream with error: " + e.what() + "\n\tSuccesfully read : " + *aoutput); + catch (const std::exception &e) { + if (globals.bDebugLog) + globals.debuglog << std::string(time_str() + ": failed to read from stream with error: " + e.what() + "\n\tSuccesfully read : " + *aoutput); } fix_line_term(*aoutput); - if(globals.bDebugLog) - globals.debuglog<<std::string(time_str()+": gpg out: "+*aoutput); + if (globals.bDebugLog) + globals.debuglog << std::string(time_str() + ": gpg out: " + *aoutput); auto ec = wait_for_exit(*c); delete c; *aexitcode = ec; _child = nullptr; - - if(*aexitcode) - { - if(globals.bDebugLog) - globals.debuglog<<std::string(time_str()+": warning: wrong gpg exit status, gpg output: "+*aoutput); + if (*aexitcode) { + if (globals.bDebugLog) + globals.debuglog << std::string(time_str() + ": warning: wrong gpg exit status, gpg output: " + *aoutput); return pxSuccessExitCodeInvalid; } - return pxSuccess; } -void pxEexcute_thread(gpg_execution_params ¶ms) +void pxEexcute_thread(gpg_execution_params *params) { - pxExecute(params.aargv, params.out, params.code, params.result, params.child); + pxExecute(params->aargv, params->out, params->code, params->result, params->child); + delete params; } bool gpg_launcher(gpg_execution_params ¶ms, boost::posix_time::time_duration t) { bool ret = true; - boost::thread *gpg_thread = new boost::thread(boost::bind(&pxEexcute_thread, params)); - if(!gpg_thread->timed_join(t)) - { + HANDLE hThread = mir_forkThread<gpg_execution_params>(pxEexcute_thread, new gpg_execution_params(params)); + if (WaitForSingleObject(hThread, t.total_milliseconds()) == WAIT_TIMEOUT) { ret = false; - delete gpg_thread; - if(params.child) + if (params.child) boost::process::terminate(*(params.child)); - if(globals.bDebugLog) - globals.debuglog<<std::string(time_str()+": GPG execution timed out, aborted"); + if (globals.bDebugLog) + globals.debuglog << std::string(time_str() + ": GPG execution timed out, aborted"); } return ret; } pxResult pxExecute_passwd_change(std::vector<std::wstring> &aargv, pxResult *result, boost::process::child *_child) { - if(!globals.gpg_valid) + if (!globals.gpg_valid) return pxNotConfigured; wchar_t *bin_path = UniGetContactSettingUtf(NULL, MODULENAME, "szGpgBinPath", L""); { - if(!boost::filesystem::exists(bin_path)) - { + if (!boost::filesystem::exists(bin_path)) { mir_free(bin_path); - if(globals.bDebugLog) - globals.debuglog<<std::string(time_str()+": GPG executable not found"); + if (globals.bDebugLog) + globals.debuglog << std::string(time_str() + ": GPG executable not found"); *result = pxNotFound; return pxNotFound; } @@ -265,7 +247,7 @@ pxResult pxExecute_passwd_change(std::vector<std::wstring> &aargv, pxResult *res // boost::process::pistream &_stdout = proc.get_stdout(); boost::process::postream &_stdin = proc.get_stdin(); - boost::this_thread::sleep(boost::posix_time::milliseconds(50)); + ::Sleep(50); std::string buf; */ @@ -276,7 +258,7 @@ pxResult pxExecute_passwd_change(std::vector<std::wstring> &aargv, pxResult *res *aoutput += buf; if(aoutput->find("Enter passphrase") != std::string::npos) break; - boost::this_thread::sleep(boost::posix_time::milliseconds(50)); + ::Sleep(50); std::getline(_stdout, buf); } *aoutput += buf; */ @@ -289,7 +271,7 @@ pxResult pxExecute_passwd_change(std::vector<std::wstring> &aargv, pxResult *res *aoutput += buf; if(aoutput->find("Enter the new passphrase for this secret key.") != std::string::npos) break; - boost::this_thread::sleep(boost::posix_time::milliseconds(50)); + ::Sleep(50); std::getline(_stdout, buf); } @@ -304,7 +286,7 @@ pxResult pxExecute_passwd_change(std::vector<std::wstring> &aargv, pxResult *res *aoutput += buf; if(aoutput->find("Repeat passphrase") != std::string::npos) break; - boost::this_thread::sleep(boost::posix_time::milliseconds(50)); + ::Sleep(50); std::getline(_stdout, buf); } *aoutput += buf; */ @@ -317,7 +299,7 @@ pxResult pxExecute_passwd_change(std::vector<std::wstring> &aargv, pxResult *res *aoutput += buf; if(aoutput->find("Command") != std::string::npos) break; - boost::this_thread::sleep(boost::posix_time::milliseconds(50)); + ::Sleep(50); std::getline(_stdout, buf); } *aoutput += buf; */ @@ -331,7 +313,6 @@ pxResult pxExecute_passwd_change(std::vector<std::wstring> &aargv, pxResult *res return pxSuccess; } - void pxEexcute_passwd_change_thread(void *param) { gpg_execution_params_pass *params = (gpg_execution_params_pass*)param; diff --git a/plugins/New_GPG/src/gpg_wrapper.h b/plugins/New_GPG/src/gpg_wrapper.h index 673ada398e..36e1ac4a20 100644 --- a/plugins/New_GPG/src/gpg_wrapper.h +++ b/plugins/New_GPG/src/gpg_wrapper.h @@ -69,6 +69,6 @@ struct gpg_execution_params_pass void pxEexcute_thread(gpg_execution_params ¶ms); bool gpg_launcher(gpg_execution_params ¶ms, boost::posix_time::time_duration t = boost::posix_time::seconds(10)); -void pxEexcute_passwd_change_thread(void *param); +void __cdecl pxEexcute_passwd_change_thread(void *param); #endif
\ No newline at end of file diff --git a/plugins/New_GPG/src/icons.cpp b/plugins/New_GPG/src/icons.cpp index 8d6cc66f60..82c08cd45a 100755 --- a/plugins/New_GPG/src/icons.cpp +++ b/plugins/New_GPG/src/icons.cpp @@ -16,7 +16,6 @@ #include "stdafx.h" - static IconItem iconList[] = { { "Secured", "secured", IDI_SECURED }, @@ -39,14 +38,14 @@ void setClistIcon(MCONTACT hContact) MCONTACT hMC = db_mc_tryMeta(hContact); const char *szIconId = (enabled) ? "secured" : nullptr; ExtraIcon_SetIconByName(globals.g_hCLIcon, hContact, szIconId); - if(hMC != hContact) + if (hMC != hContact) ExtraIcon_SetIconByName(globals.g_hCLIcon, hMC, szIconId); } void setSrmmIcon(MCONTACT h) { MCONTACT hContact = db_mc_isMeta(h) ? metaGetMostOnline(h) : h; - bool enabled = isContactSecured(hContact); + bool enabled = isContactSecured(hContact); MCONTACT hMC = db_mc_tryMeta(hContact); StatusIconData sid = {}; @@ -55,18 +54,18 @@ void setSrmmIcon(MCONTACT h) sid.dwId = 1; sid.flags = enabled ? 0 : MBF_HIDDEN; Srmm_ModifyIcon(hContact, &sid); - if(hMC != hContact) + if (hMC != hContact) Srmm_ModifyIcon(hMC, &sid); sid.hIcon = IcoLib_GetIcon("unsecured"); sid.dwId = 2; sid.flags = enabled ? MBF_HIDDEN : 0; Srmm_ModifyIcon(hContact, &sid); - if(hMC != hContact) + if (hMC != hContact) Srmm_ModifyIcon(hMC, &sid); } -void RefreshContactListIcons() +void RefreshContactListIcons() { for (auto &hContact : Contacts()) setClistIcon(hContact); diff --git a/plugins/New_GPG/src/init.cpp b/plugins/New_GPG/src/init.cpp index d2e50a0ffb..8407d2bb96 100755 --- a/plugins/New_GPG/src/init.cpp +++ b/plugins/New_GPG/src/init.cpp @@ -16,7 +16,7 @@ #include "stdafx.h" -//global variables +// global variables CMPlugin g_plugin; ///////////////////////////////////////////////////////////////////////////////////////// @@ -36,17 +36,20 @@ PLUGININFOEX pluginInfoEx = { CMPlugin::CMPlugin() : PLUGIN<CMPlugin>(MODULENAME, pluginInfoEx) -{} +{ +} ///////////////////////////////////////////////////////////////////////////////////////// -INT_PTR LoadKey(WPARAM w, LPARAM l); -INT_PTR ToggleEncryption(WPARAM w, LPARAM l); -INT_PTR SendKey(WPARAM w, LPARAM l); -INT_PTR ExportGpGKeys(WPARAM w, LPARAM l); -INT_PTR ImportGpGKeys(WPARAM w, LPARAM l); +INT_PTR LoadKey(WPARAM, LPARAM); +INT_PTR SendKey(WPARAM, LPARAM); +INT_PTR ExportGpGKeys(WPARAM, LPARAM); +INT_PTR ImportGpGKeys(WPARAM, LPARAM); +INT_PTR ToggleEncryption(WPARAM, LPARAM); + +int onExtraImageApplying(WPARAM, LPARAM); int onExtraImageListRebuilding(WPARAM, LPARAM); -int onExtraImageApplying(WPARAM wParam, LPARAM); + void InitIconLib(); void init_vars() @@ -85,22 +88,23 @@ void init_vars() static int OnModulesLoaded(WPARAM, LPARAM) { - int GpgOptInit(WPARAM wParam,LPARAM lParam); - int OnPreBuildContactMenu(WPARAM w, LPARAM l); - INT_PTR RecvMsgSvc(WPARAM w, LPARAM l); - INT_PTR SendMsgSvc(WPARAM w, LPARAM l); - int HookSendMsg(WPARAM w, LPARAM l); -// int TestHook(WPARAM w, LPARAM l); - int GetJabberInterface(WPARAM w, LPARAM l); - int onWindowEvent(WPARAM wParam, LPARAM lParam); - int onIconPressed(WPARAM wParam, LPARAM lParam); - int onProtoAck(WPARAM, LPARAM); + int GpgOptInit(WPARAM, LPARAM); + int OnPreBuildContactMenu(WPARAM, LPARAM); + + INT_PTR RecvMsgSvc(WPARAM, LPARAM); + INT_PTR SendMsgSvc(WPARAM, LPARAM); INT_PTR onSendFile(WPARAM, LPARAM); + int HookSendMsg(WPARAM, LPARAM); + int GetJabberInterface(WPARAM, LPARAM); + int onProtoAck(WPARAM, LPARAM); + int onWindowEvent(WPARAM, LPARAM); + int onIconPressed(WPARAM, LPARAM); + void InitCheck(); void FirstRun(); FirstRun(); - if(!db_get_b(NULL, MODULENAME, "FirstRun", 1)) + if (!db_get_b(NULL, MODULENAME, "FirstRun", 1)) InitCheck(); StatusIconData sid = {}; @@ -116,12 +120,12 @@ static int OnModulesLoaded(WPARAM, LPARAM) sid.szTooltip = LPGEN("GPG Turn on encryption"); Srmm_AddIcon(&sid, &g_plugin); - if(globals.bJabberAPI) - GetJabberInterface(0,0); + if (globals.bJabberAPI) + GetJabberInterface(0, 0); HookEvent(ME_OPT_INITIALISE, GpgOptInit); HookEvent(ME_DB_EVENT_FILTER_ADD, HookSendMsg); - if(globals.bJabberAPI) + if (globals.bJabberAPI) HookEvent(ME_PROTO_ACCLISTCHANGED, GetJabberInterface); HookEvent(ME_PROTO_ACK, onProtoAck); @@ -132,10 +136,10 @@ static int OnModulesLoaded(WPARAM, LPARAM) HookEvent(ME_MSG_ICONPRESSED, onIconPressed); Proto_RegisterModule(PROTOTYPE_ENCRYPTION, MODULENAME); - + CreateProtoServiceFunction(MODULENAME, PSR_MESSAGE, RecvMsgSvc); CreateProtoServiceFunction(MODULENAME, PSS_MESSAGE, SendMsgSvc); - CreateProtoServiceFunction(MODULENAME, PSS_FILE, onSendFile); + CreateProtoServiceFunction(MODULENAME, PSS_FILE, onSendFile); clean_temp_dir(); return 0; } @@ -145,11 +149,11 @@ int CMPlugin::Load() HookEvent(ME_SYSTEM_MODULESLOADED, OnModulesLoaded); init_vars(); - CreateServiceFunction("/LoadPubKey",LoadKey); - CreateServiceFunction("/ToggleEncryption",ToggleEncryption); - CreateServiceFunction("/SendKey",SendKey); - CreateServiceFunction("/ExportGPGKeys",ExportGpGKeys); - CreateServiceFunction("/ImportGPGKeys",ImportGpGKeys); + CreateServiceFunction("/LoadPubKey", LoadKey); + CreateServiceFunction("/ToggleEncryption", ToggleEncryption); + CreateServiceFunction("/SendKey", SendKey); + CreateServiceFunction("/ExportGPGKeys", ExportGpGKeys); + CreateServiceFunction("/ImportGPGKeys", ImportGpGKeys); CMenuItem mi(&g_plugin); @@ -200,17 +204,15 @@ int CMPlugin::Load() extern list<wstring> transfers; int CMPlugin::Unload() { - if(!transfers.empty()) - { - for(auto p : transfers) - if(!p.empty()) - boost::filesystem::remove(p); - } + for (auto p : transfers) + if (!p.empty()) + boost::filesystem::remove(p); + mir_free(globals.inopentag); mir_free(globals.inclosetag); mir_free(globals.outopentag); mir_free(globals.outclosetag); - if(globals.password) + if (globals.password) mir_free(globals.password); clean_temp_dir(); return 0; diff --git a/plugins/New_GPG/src/jabber_account.cpp b/plugins/New_GPG/src/jabber_account.cpp index 1d06dd4ccd..e40a0e0a83 100644 --- a/plugins/New_GPG/src/jabber_account.cpp +++ b/plugins/New_GPG/src/jabber_account.cpp @@ -20,47 +20,55 @@ void JabberAccount::setAccountName(wchar_t *Name) { AccountName = Name; } + void JabberAccount::setAccountNumber(int Number) { AccountNumber = Number; } + void JabberAccount::setJabberInterface(IJabberInterface *JIf) { JabberInterface = JIf; } + void JabberAccount::setSendHandler(HJHANDLER hHandler) { hSendHandler = hHandler; } -void JabberAccount::setPrescenseHandler(HJHANDLER hHandler) + +void JabberAccount::setPresenceHandler(HJHANDLER hHandler) { - hPrescenseHandler = hHandler; + hPresenceHandler = hHandler; } + void JabberAccount::setMessageHandler(HJHANDLER hHandler) { hMessageHandler = hHandler; } - -wchar_t *JabberAccount::getAccountName() +wchar_t* JabberAccount::getAccountName() { return AccountName; } + int JabberAccount::getAccountNumber() { return AccountNumber; } -IJabberInterface *JabberAccount::getJabberInterface() + +IJabberInterface* JabberAccount::getJabberInterface() { return JabberInterface; } + HJHANDLER JabberAccount::getSendHandler() { return hSendHandler; } -HJHANDLER JabberAccount::getPrescenseHandler() + +HJHANDLER JabberAccount::getPresenceHandler() { - return hPrescenseHandler; + return hPresenceHandler; } HJHANDLER JabberAccount::getMessageHandler() @@ -68,18 +76,11 @@ HJHANDLER JabberAccount::getMessageHandler() return hMessageHandler; } - JabberAccount::JabberAccount() { - AccountName = nullptr; - hSendHandler = INVALID_HANDLE_VALUE; - hPrescenseHandler = INVALID_HANDLE_VALUE; - hMessageHandler = INVALID_HANDLE_VALUE; - AccountNumber = -1; - JabberInterface = nullptr; } + JabberAccount::~JabberAccount() { - if(AccountName) - mir_free(AccountName); -}
\ No newline at end of file + mir_free(AccountName); +} diff --git a/plugins/New_GPG/src/jabber_account.h b/plugins/New_GPG/src/jabber_account.h index ea5a741772..5d9f46a33c 100644 --- a/plugins/New_GPG/src/jabber_account.h +++ b/plugins/New_GPG/src/jabber_account.h @@ -22,22 +22,24 @@ public: void setAccountNumber(int Number); void setJabberInterface(IJabberInterface *JIf); void setSendHandler(HJHANDLER hHandler); - void setPrescenseHandler(HJHANDLER hHandler); + void setPresenceHandler(HJHANDLER hHandler); void setMessageHandler(HJHANDLER hHandler); - wchar_t *getAccountName(); + wchar_t* getAccountName(); int getAccountNumber(); - IJabberInterface *getJabberInterface(); + IJabberInterface* getJabberInterface(); HJHANDLER getSendHandler(); - HJHANDLER getPrescenseHandler(); + HJHANDLER getPresenceHandler(); HJHANDLER getMessageHandler(); - ~JabberAccount(); + JabberAccount(); + ~JabberAccount(); + private: - wchar_t *AccountName; - int AccountNumber; - IJabberInterface *JabberInterface; - HJHANDLER hSendHandler, hPrescenseHandler, hMessageHandler; + wchar_t *AccountName = nullptr; + int AccountNumber = -1; + IJabberInterface *JabberInterface = nullptr; + HJHANDLER hSendHandler = INVALID_HANDLE_VALUE, hPresenceHandler = INVALID_HANDLE_VALUE, hMessageHandler = INVALID_HANDLE_VALUE; }; #endif
\ No newline at end of file diff --git a/plugins/New_GPG/src/log.cpp b/plugins/New_GPG/src/log.cpp index 598047b5c3..c5f704ec57 100755 --- a/plugins/New_GPG/src/log.cpp +++ b/plugins/New_GPG/src/log.cpp @@ -16,71 +16,75 @@ #include "stdafx.h" - logtofile& logtofile::operator<<(wchar_t *buf) { - if(_bDebugLog != globals.bDebugLog) + if (_bDebugLog != globals.bDebugLog) init(); - log_mutex.lock(); - log.open(toUTF8(path).c_str(), std::ios::app |std::ios::ate); - log<<toUTF8(buf); - log<<"\n"; + + mir_cslock l(csLock); + log.open(toUTF8(path).c_str(), std::ios::app | std::ios::ate); + log << toUTF8(buf); + log << "\n"; log.close(); - log_mutex.unlock(); return *this; } + logtofile& logtofile::operator<<(char *buf) { - if(_bDebugLog != globals.bDebugLog) + if (_bDebugLog != globals.bDebugLog) init(); - log_mutex.lock(); - log.open(toUTF8(path).c_str(), std::ios::app |std::ios::ate); - log<<buf; - log<<"\n"; + + mir_cslock l(csLock); + log.open(toUTF8(path).c_str(), std::ios::app | std::ios::ate); + log << buf; + log << "\n"; log.close(); - log_mutex.unlock(); return *this; } + logtofile& logtofile::operator<<(string buf) { - if(_bDebugLog != globals.bDebugLog) + if (_bDebugLog != globals.bDebugLog) init(); - log_mutex.lock(); + + mir_cslock l(csLock); char *tmp = mir_utf8encode(buf.c_str()); - log.open(toUTF8(path).c_str(), std::ios::app |std::ios::ate); - log<<tmp; - log<<"\n"; + log.open(toUTF8(path).c_str(), std::ios::app | std::ios::ate); + log << tmp; + log << "\n"; log.close(); - log_mutex.unlock(); mir_free(tmp); return *this; } + logtofile& logtofile::operator<<(wstring buf) { - if(_bDebugLog != globals.bDebugLog) + if (_bDebugLog != globals.bDebugLog) init(); - log_mutex.lock(); - log.open(toUTF8(path).c_str(), std::ios::app |std::ios::ate); - log<<toUTF8(buf); - log<<"\n"; + + mir_cslock l(csLock); + log.open(toUTF8(path).c_str(), std::ios::app | std::ios::ate); + log << toUTF8(buf); + log << "\n"; log.close(); - log_mutex.unlock(); return *this; } + void logtofile::init() { - if(globals.bDebugLog) - { - if(path) + if (globals.bDebugLog) { + if (path) mir_free(path); path = UniGetContactSettingUtf(NULL, MODULENAME, "szLogFilePath", L"C:\\GPGdebug.log"); } _bDebugLog = globals.bDebugLog; } + logtofile::logtofile() { path = nullptr; } + logtofile::~logtofile() { mir_free(path); diff --git a/plugins/New_GPG/src/log.h b/plugins/New_GPG/src/log.h index 76238918e3..4e448d635c 100644 --- a/plugins/New_GPG/src/log.h +++ b/plugins/New_GPG/src/log.h @@ -27,10 +27,10 @@ public: ~logtofile(); logtofile(); private: - fstream log; + fstream log; wchar_t *path; - boost::mutex log_mutex; - bool _bDebugLog; + bool _bDebugLog; + mir_cs csLock; }; #endif diff --git a/plugins/New_GPG/src/main.cpp b/plugins/New_GPG/src/main.cpp index 8975dcb35a..42d754f6d5 100755 --- a/plugins/New_GPG/src/main.cpp +++ b/plugins/New_GPG/src/main.cpp @@ -18,7 +18,6 @@ #pragma comment(lib, "shlwapi.lib") - void FirstRun() { if (!db_get_b(NULL, MODULENAME, "FirstRun", 1)) @@ -120,8 +119,7 @@ void InitCheck() question += Translate(" for account "); question += toUTF8(pa->tszAccountName); question += Translate(" deleted from GPG secret keyring.\nDo you want to set another key?"); - if (MessageBoxA(nullptr, question.c_str(), Translate("Own secret key warning"), MB_YESNO) == IDYES) - { + if (MessageBoxA(nullptr, question.c_str(), Translate("Own secret key warning"), MB_YESNO) == IDYES) { CDlgFirstRun *d = new CDlgFirstRun; d->DoModal(); } @@ -161,8 +159,7 @@ void InitCheck() question += Translate(" for account "); question += toUTF8(pa->tszAccountName); question += Translate(" expired and will not work.\nDo you want to set another key?"); - if (MessageBoxA(nullptr, question.c_str(), Translate("Own secret key warning"), MB_YESNO) == IDYES) - { + if (MessageBoxA(nullptr, question.c_str(), Translate("Own secret key warning"), MB_YESNO) == IDYES) { CDlgFirstRun *d = new CDlgFirstRun; d->DoModal(); } @@ -180,8 +177,7 @@ void InitCheck() char *key = UniGetContactSettingUtf(NULL, MODULENAME, "GPGPubKey", ""); if (!db_get_b(NULL, MODULENAME, "FirstRun", 1) && (!keyid[0] || !key[0])) { question = Translate("You didn't set a private key.\nWould you like to set it now?"); - if (MessageBoxA(nullptr, question.c_str(), Translate("Own private key warning"), MB_YESNO) == IDYES) - { + if (MessageBoxA(nullptr, question.c_str(), Translate("Own private key warning"), MB_YESNO) == IDYES) { CDlgFirstRun *d = new CDlgFirstRun; d->DoModal(); } @@ -189,8 +185,7 @@ void InitCheck() if ((p = out.find(keyid)) == string::npos) { question += keyid; question += Translate(" deleted from GPG secret keyring.\nDo you want to set another key?"); - if (MessageBoxA(nullptr, question.c_str(), Translate("Own secret key warning"), MB_YESNO) == IDYES) - { + if (MessageBoxA(nullptr, question.c_str(), Translate("Own secret key warning"), MB_YESNO) == IDYES) { CDlgFirstRun *d = new CDlgFirstRun; d->DoModal(); } @@ -228,8 +223,7 @@ void InitCheck() if (expired) { question += keyid; question += Translate(" expired and will not work.\nDo you want to set another key?"); - if (MessageBoxA(nullptr, question.c_str(), Translate("Own secret key warning"), MB_YESNO) == IDYES) - { + if (MessageBoxA(nullptr, question.c_str(), Translate("Own secret key warning"), MB_YESNO) == IDYES) { CDlgFirstRun *d = new CDlgFirstRun; d->DoModal(); } 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; } - diff --git a/plugins/New_GPG/src/metacontacts.cpp b/plugins/New_GPG/src/metacontacts.cpp index d9936c792a..3de739fae1 100644 --- a/plugins/New_GPG/src/metacontacts.cpp +++ b/plugins/New_GPG/src/metacontacts.cpp @@ -16,14 +16,14 @@ #include "stdafx.h" -bool metaIsDefaultSubContact(MCONTACT hContact) +bool metaIsDefaultSubContact(MCONTACT hContact) { return db_mc_getDefault(db_mc_getMeta(hContact)) == hContact; } -MCONTACT metaGetMostOnline(MCONTACT hContact) +MCONTACT metaGetMostOnline(MCONTACT hContact) { - if(db_mc_isMeta(hContact)) + if (db_mc_isMeta(hContact)) return db_mc_getMostOnline(hContact); return NULL; } diff --git a/plugins/New_GPG/src/options.cpp b/plugins/New_GPG/src/options.cpp index cc8f137211..dbf212737c 100755 --- a/plugins/New_GPG/src/options.cpp +++ b/plugins/New_GPG/src/options.cpp @@ -454,7 +454,7 @@ public: void onClick_SET_HOME_DIR(CCtrlButton*) { - GetFolderPath(TranslateT("Set home directory"), "szHomePath"); + GetFolderPath(TranslateT("Set home directory")); CMStringW tmp(ptrW(UniGetContactSettingUtf(NULL, MODULENAME, "szHomePath", L""))); edit_HOME_DIR.SetText(tmp); wchar_t mir_path[MAX_PATH]; diff --git a/plugins/New_GPG/src/srmm.cpp b/plugins/New_GPG/src/srmm.cpp index d9e8187c7a..129cb29570 100755 --- a/plugins/New_GPG/src/srmm.cpp +++ b/plugins/New_GPG/src/srmm.cpp @@ -14,66 +14,57 @@ // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - - #include "stdafx.h" -void ShowStatusIcon(MCONTACT hContact); void setSrmmIcon(MCONTACT hContact); int __cdecl onWindowEvent(WPARAM, LPARAM lParam) { MessageWindowEventData *mwd = (MessageWindowEventData *)lParam; - if(mwd->uType == MSG_WINDOW_EVT_OPEN || mwd->uType == MSG_WINDOW_EVT_OPENING) + if (mwd->uType == MSG_WINDOW_EVT_OPEN || mwd->uType == MSG_WINDOW_EVT_OPENING) setSrmmIcon(mwd->hContact); return 0; } - int __cdecl onIconPressed(WPARAM wParam, LPARAM lParam) { MCONTACT hContact = wParam; MCONTACT hMeta = NULL; - if(db_mc_isMeta(hContact)) - { + if (db_mc_isMeta(hContact)) { hMeta = hContact; hContact = metaGetMostOnline(hContact); // возьмем тот, через который пойдет сообщение } - else if(db_mc_isSub(hContact)) + else if (db_mc_isSub(hContact)) hMeta = db_mc_getMeta(hContact); + StatusIconClickData *sicd = (StatusIconClickData *)lParam; - if(mir_strcmp(sicd->szModule, MODULENAME)) + if (mir_strcmp(sicd->szModule, MODULENAME)) return 0; // not our event - + BYTE enc = db_get_b(hContact, MODULENAME, "GPGEncryption", 0); - if(enc) - { + if (enc) { db_set_b(hContact, MODULENAME, "GPGEncryption", 0); - hMeta?db_set_b(hMeta, MODULENAME, "GPGEncryption", 0):0; + hMeta ? db_set_b(hMeta, MODULENAME, "GPGEncryption", 0) : 0; setSrmmIcon(hContact); setClistIcon(hContact); } - else if(!enc) - { - if(!isContactHaveKey(hContact)) - { + else if (!enc) { + if (!isContactHaveKey(hContact)) { void ShowLoadPublicKeyDialog(bool = false); globals.item_num = 0; //black magic here globals.user_data[1] = hContact; ShowLoadPublicKeyDialog(); } - else - { + else { db_set_b(hContact, MODULENAME, "GPGEncryption", 1); - hMeta?db_set_b(hMeta, MODULENAME, "GPGEncryption", 1):0; + hMeta ? db_set_b(hMeta, MODULENAME, "GPGEncryption", 1) : 0; setSrmmIcon(hContact); setClistIcon(hContact); return 0; } - if(isContactHaveKey(hContact)) - { + if (isContactHaveKey(hContact)) { db_set_b(hContact, MODULENAME, "GPGEncryption", 1); - hMeta?db_set_b(hMeta, MODULENAME, "GPGEncryption", 1):0; + hMeta ? db_set_b(hMeta, MODULENAME, "GPGEncryption", 1) : 0; setSrmmIcon(hContact); setClistIcon(hContact); } diff --git a/plugins/New_GPG/src/stdafx.h b/plugins/New_GPG/src/stdafx.h index c23c23e7c1..38be73fc33 100755 --- a/plugins/New_GPG/src/stdafx.h +++ b/plugins/New_GPG/src/stdafx.h @@ -21,13 +21,13 @@ #define _SCL_SECURE_NO_WARNINGS -//windows +// windows #include <windows.h> #include <shlobj.h> #include <io.h> #include <shlwapi.h> -//c++ +// c++ #include <map> using std::map; #include <list> @@ -39,22 +39,17 @@ using std::wstring; using std::wfstream; using std::fstream; -//boost -#include <boost/thread/thread.hpp> -#include <boost/thread/mutex.hpp> +// boost #include <boost/nondet_random.hpp> #include <boost/random/variate_generator.hpp> #include <boost/random/uniform_int.hpp> #include <boost/date_time.hpp> #include <boost/iostreams/stream.hpp> -//boost process +// boost process #include <boost/process.hpp> -//utf8cpp -#include <utf8.h> - -//miranda +// miranda #include <newpluginapi.h> #include <m_database.h> #include <m_options.h> @@ -81,7 +76,7 @@ struct CMPlugin : public PLUGIN<CMPlugin> int Unload() override; }; -//internal +// internal #include "resource.h" #include "version.h" #include "constants.h" diff --git a/plugins/New_GPG/src/ui.cpp b/plugins/New_GPG/src/ui.cpp index 1ff5613792..8ec4ca30b8 100755 --- a/plugins/New_GPG/src/ui.cpp +++ b/plugins/New_GPG/src/ui.cpp @@ -14,7 +14,6 @@ // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - #include "stdafx.h" ///////////////////////////////////////////////////////////////////////////////////////// @@ -106,62 +105,62 @@ CDlgChangePasswdMsgBox::CDlgChangePasswdMsgBox() : void CDlgChangePasswdMsgBox::onClick_OK(CCtrlButton*) { //TODO: show some prgress - { - if (mir_wstrcmp(edit_NEW_PASSWD1.GetText(), edit_NEW_PASSWD2.GetText())) { - MessageBox(m_hwnd, TranslateT("New passwords do not match"), TranslateT("Error"), MB_OK); - return; - } - std::string old_pass, new_pass; - // wchar_t buf[256] = { 0 }; - new_pass = toUTF8(edit_NEW_PASSWD1.GetText()); - old_pass = toUTF8(edit_OLD_PASSWD.GetText()); - bool old_pass_match = false; - wchar_t *pass = UniGetContactSettingUtf(NULL, MODULENAME, "szKeyPassword", L""); - if (!mir_wstrcmp(pass, edit_OLD_PASSWD.GetText())) - old_pass_match = true; - mir_free(pass); - - if (!old_pass_match) { - if (globals.key_id_global[0]) { - string dbsetting = "szKey_"; - dbsetting += toUTF8(globals.key_id_global); - dbsetting += "_Password"; - pass = UniGetContactSettingUtf(NULL, MODULENAME, dbsetting.c_str(), L""); - if (!mir_wstrcmp(pass, edit_OLD_PASSWD.GetText())) - old_pass_match = true; - mir_free(pass); - } + if (mir_wstrcmp(edit_NEW_PASSWD1.GetText(), edit_NEW_PASSWD2.GetText())) { + MessageBox(m_hwnd, TranslateT("New passwords do not match"), TranslateT("Error"), MB_OK); + return; + } + std::string old_pass, new_pass; + // wchar_t buf[256] = { 0 }; + new_pass = toUTF8(edit_NEW_PASSWD1.GetText()); + old_pass = toUTF8(edit_OLD_PASSWD.GetText()); + bool old_pass_match = false; + wchar_t *pass = UniGetContactSettingUtf(NULL, MODULENAME, "szKeyPassword", L""); + if (!mir_wstrcmp(pass, edit_OLD_PASSWD.GetText())) + old_pass_match = true; + mir_free(pass); + + if (!old_pass_match) { + if (globals.key_id_global[0]) { + string dbsetting = "szKey_"; + dbsetting += toUTF8(globals.key_id_global); + dbsetting += "_Password"; + pass = UniGetContactSettingUtf(NULL, MODULENAME, dbsetting.c_str(), L""); + if (!mir_wstrcmp(pass, edit_OLD_PASSWD.GetText())) + old_pass_match = true; + mir_free(pass); } + } - if (!old_pass_match) - if (MessageBox(m_hwnd, TranslateT("Old password does not match, you can continue, but GPG will reject wrong password.\nDo you want to continue?"), TranslateT("Error"), MB_YESNO) == IDNO) - return; - - std::vector<std::wstring> cmd; - string output; - DWORD exitcode; - cmd.push_back(L"--edit-key"); - cmd.push_back(globals.key_id_global); - cmd.push_back(L"passwd"); - gpg_execution_params_pass params(cmd, old_pass, new_pass); - pxResult result; - params.out = &output; - params.code = &exitcode; - params.result = &result; - boost::thread gpg_thread(boost::bind(&pxEexcute_passwd_change_thread, ¶ms)); - if (!gpg_thread.timed_join(boost::posix_time::minutes(10))) { - gpg_thread.~thread(); - if (params.child) - boost::process::terminate(*(params.child)); - if (globals.bDebugLog) - globals.debuglog << std::string(time_str() + ": GPG execution timed out, aborted"); - this->Close(); - return; - } - if (result == pxNotFound) + if (!old_pass_match) + if (MessageBox(m_hwnd, TranslateT("Old password does not match, you can continue, but GPG will reject wrong password.\nDo you want to continue?"), TranslateT("Error"), MB_YESNO) == IDNO) return; + + string output; + DWORD exitcode; + pxResult result; + + std::vector<std::wstring> cmd; + cmd.push_back(L"--edit-key"); + cmd.push_back(globals.key_id_global); + cmd.push_back(L"passwd"); + + gpg_execution_params_pass *params = new gpg_execution_params_pass(cmd, old_pass, new_pass); + params->out = &output; + params->code = &exitcode; + params->result = &result; + + HANDLE hThread = mir_forkthread(&pxEexcute_passwd_change_thread, params); + if (WaitForSingleObject(hThread, 600000) != WAIT_OBJECT_0) { + if (params->child) + boost::process::terminate(*(params->child)); + if (globals.bDebugLog) + globals.debuglog << std::string(time_str() + ": GPG execution timed out, aborted"); + this->Close(); + return; } - this->Close(); + + if (result != pxNotFound) + this->Close(); } ///////////////////////////////////////////////////////////////////////////////////////// @@ -342,29 +341,30 @@ void CDlgFirstRun::onClick_CHANGE_PASSWD(CCtrlButton*) return; list_KEY_LIST.GetItemText(i, 0, globals.key_id_global, _countof(globals.key_id_global)); - //temporary code follows - std::vector<std::wstring> cmd; + // temporary code follows std::string old_pass, new_pass; string output; DWORD exitcode; + pxResult result; + + std::vector<std::wstring> cmd; cmd.push_back(L"--edit-key"); cmd.push_back(globals.key_id_global); cmd.push_back(L"passwd"); - gpg_execution_params_pass params(cmd, old_pass, new_pass); - pxResult result; - params.out = &output; - params.code = &exitcode; - params.result = &result; - boost::thread gpg_thread(boost::bind(&pxEexcute_passwd_change_thread, ¶ms)); - if (!gpg_thread.timed_join(boost::posix_time::minutes(10))) { - gpg_thread.~thread(); - if (params.child) - boost::process::terminate(*(params.child)); + + gpg_execution_params_pass *params = new gpg_execution_params_pass(cmd, old_pass, new_pass); + params->out = &output; + params->code = &exitcode; + params->result = &result; + + HANDLE hThread = mir_forkthread(pxEexcute_passwd_change_thread, params); + if (WaitForSingleObject(hThread, 600000) != WAIT_OBJECT_0) { + if (params->child) + boost::process::terminate(*(params->child)); if (globals.bDebugLog) globals.debuglog << std::string(time_str() + ": GPG execution timed out, aborted"); this->Close(); } - } void CDlgFirstRun::onClick_GENERATE_RANDOM(CCtrlButton*) @@ -876,7 +876,7 @@ void CDlgGpgBinOpts::onClick_SET_BIN_PATH(CCtrlButton*) void CDlgGpgBinOpts::onClick_SET_HOME_DIR(CCtrlButton*) { - GetFolderPath(L"Set home directory", "szHomePath"); + GetFolderPath(L"Set home directory"); CMStringW tmp(ptrW(UniGetContactSettingUtf(NULL, MODULENAME, "szHomePath", L""))); edit_HOME_DIR.SetText(tmp); wchar_t mir_path[MAX_PATH]; @@ -1330,7 +1330,7 @@ void CDlgLoadExistingKey::onClick_OK(CCtrlButton*) while ((s = out.find("\r", s)) != string::npos) { out.erase(s, 1); } - + std::string::size_type p1 = 0, p2 = 0; p1 = out.find("-----BEGIN PGP PUBLIC KEY BLOCK-----"); if (p1 != std::string::npos) { diff --git a/plugins/New_GPG/src/utilities.cpp b/plugins/New_GPG/src/utilities.cpp index fc042ff105..fed328235e 100755 --- a/plugins/New_GPG/src/utilities.cpp +++ b/plugins/New_GPG/src/utilities.cpp @@ -14,13 +14,14 @@ // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - #include "stdafx.h" +#include "utf8.h" + void ShowExportKeysDlg(); void ShowLoadPublicKeyDialog(bool = false); -wchar_t* __stdcall UniGetContactSettingUtf(MCONTACT hContact, const char *szModule, const char* szSetting, wchar_t* szDef) +wchar_t* __stdcall UniGetContactSettingUtf(MCONTACT hContact, const char *szModule, const char *szSetting, wchar_t *szDef) { DBVARIANT dbv = { DBVT_DELETED }; wchar_t* szRes = nullptr; @@ -35,7 +36,7 @@ wchar_t* __stdcall UniGetContactSettingUtf(MCONTACT hContact, const char *szModu return szRes; } -char* __stdcall UniGetContactSettingUtf(MCONTACT hContact, const char *szModule, const char* szSetting, char* szDef) +char* __stdcall UniGetContactSettingUtf(MCONTACT hContact, const char *szModule, const char *szSetting, char *szDef) { DBVARIANT dbv = { DBVT_DELETED }; char* szRes = nullptr; @@ -76,7 +77,7 @@ void GetFilePath(wchar_t *WindowTittle, char *szSetting, wchar_t *szExt, wchar_t db_set_ws(0, MODULENAME, szSetting, str); } -wchar_t *GetFilePath(wchar_t *WindowTittle, wchar_t *szExt, wchar_t *szExtDesc, bool save_file) +wchar_t* GetFilePath(wchar_t *WindowTittle, wchar_t *szExt, wchar_t *szExtDesc, bool save_file) { wchar_t *str = new wchar_t[MAX_PATH + 2]; OPENFILENAME ofn = { 0 }; @@ -111,7 +112,7 @@ wchar_t *GetFilePath(wchar_t *WindowTittle, wchar_t *szExt, wchar_t *szExtDesc, return str; } -void GetFolderPath(wchar_t *WindowTittle, char*) +void GetFolderPath(wchar_t *WindowTittle) { BROWSEINFO pbi = {}; pbi.lpszTitle = WindowTittle; @@ -179,8 +180,8 @@ INT_PTR SendKey(WPARAM w, LPARAM) HistoryLog(hContact, db_event(szMessage, 0, 0, DBEF_SENT)); db_set_b(hContact, MODULENAME, "GPGEncryption", enc); } - else - mir_free(szMessage); + else mir_free(szMessage); + return 0; } @@ -495,15 +496,12 @@ INT_PTR onSendFile(WPARAM w, LPARAM l) else { wchar_t *jid = UniGetContactSettingUtf(ccs->hContact, proto, "jid", L""); if (jid[0]) { - for(auto p : globals.Accounts) - { + for (auto p : globals.Accounts) { wchar_t *caps = p->getJabberInterface()->GetResourceFeatures(jid); - if (caps) - { + if (caps) { supported_proto = true; wstring str; - for (int i = 0;; i++) - { + for (int i = 0;; i++) { str.push_back(caps[i]); if (caps[i] == '\0') if (caps[i + 1] == '\0') @@ -595,7 +593,7 @@ int ComboBoxAddStringUtf(HWND hCombo, const wchar_t *szString, DWORD data) int GetJabberInterface(WPARAM, LPARAM) //get interface for all jabber accounts, options later { void AddHandlers(); - + list <JabberAccount*>::iterator p; globals.Accounts.clear(); globals.Accounts.push_back(new JabberAccount); @@ -638,7 +636,7 @@ static JABBER_HANDLER_FUNC SendHandler(IJabberInterface *ji, HXML node, void*) if (!isContactSecured(hContact)) break; } - + if (str == nullptr) continue; @@ -663,8 +661,8 @@ static JABBER_HANDLER_FUNC SendHandler(IJabberInterface *ji, HXML node, void*) wstring::size_type p2 = data.find(L"-----END PGP MESSAGE-----"); wstring data2 = data.substr(p1, p2 - p1 - 2); strip_line_term(data2); - if(globals.bDebugLog) - globals.debuglog<<std::string(time_str() + ": jabber_api: attaching:\r\n\r\n" + toUTF8(data2) + "\n\n\t to outgoing xml"); + if (globals.bDebugLog) + globals.debuglog << std::string(time_str() + ": jabber_api: attaching:\r\n\r\n" + toUTF8(data2) + "\n\n\t to outgoing xml"); HXML encrypted_data = xmlAddChild(node, L"x", data2.c_str()); xmlAddAttr(encrypted_data, L"xmlns", L"jabber:x:encrypted"); break; @@ -806,7 +804,7 @@ static JABBER_HANDLER_FUNC SendHandler(IJabberInterface *ji, HXML node, void*) return FALSE; } -static JABBER_HANDLER_FUNC PrescenseHandler(IJabberInterface*, HXML node, void*) +static JABBER_HANDLER_FUNC PresenceHandler(IJabberInterface*, HXML node, void*) { HXML local_node = node; for (int n = 0; n <= xmlGetChildCount(node); n++) { @@ -890,8 +888,7 @@ static JABBER_HANDLER_FUNC PrescenseHandler(IJabberInterface*, HXML node, void*) if (p1 != string::npos && p2 != string::npos) { MCONTACT hContact = NULL; { - for(auto p : globals.Accounts) - { + for (auto p : globals.Accounts) { /*if (!p) break;*/ hContact = p->getJabberInterface()->ContactFromJID(xmlGetAttrValue(node, L"from")); @@ -919,16 +916,15 @@ static JABBER_HANDLER_FUNC MessageHandler(IJabberInterface*, HXML, void*) void AddHandlers() { - for(auto p : globals.Accounts) - { + for (auto p : globals.Accounts) { /*if (p) break;*/ if (p->getSendHandler() == INVALID_HANDLE_VALUE) p->setSendHandler(p->getJabberInterface()->AddSendHandler((JABBER_HANDLER_FUNC)SendHandler)); - if (p->getPrescenseHandler() == INVALID_HANDLE_VALUE) - p->setPrescenseHandler(p->getJabberInterface()->AddPresenceHandler((JABBER_HANDLER_FUNC)PrescenseHandler)); - // if((*p)->getMessageHandler() == INVALID_HANDLE_VALUE) - // (*p)->setMessageHandler((*p)->getJabberInterface()->AddMessageHandler((JABBER_HANDLER_FUNC)MessageHandler, JABBER_MESSAGE_TYPE_ANY ,NULL,NULL)); + + if (p->getPresenceHandler() == INVALID_HANDLE_VALUE) + p->setPresenceHandler(p->getJabberInterface()->AddPresenceHandler((JABBER_HANDLER_FUNC)PresenceHandler)); + if (globals.bAutoExchange) { p->getJabberInterface()->RegisterFeature(L"GPG_Key_Auto_Exchange:0", L"Indicates that gpg installed and configured to public key auto exchange (currently implemented in new_gpg plugin for Miranda IM and Miranda NG)"); p->getJabberInterface()->AddFeatures(L"GPG_Key_Auto_Exchange:0\0\0"); @@ -1187,15 +1183,16 @@ void send_encrypted_msgs_thread(void *param) while (true) { //char *key = UniGetContactSettingUtf(hContact, MODULENAME, "GPGPubKey", ""); while (!isContactSecured(hContact)) - boost::this_thread::sleep(boost::posix_time::seconds(1)); + Sleep(1000); + if (!globals.hcontact_data[hContact].msgs_to_send.empty()) { - boost::this_thread::sleep(boost::posix_time::seconds(1)); + Sleep(1000); list<string>::iterator end = globals.hcontact_data[hContact].msgs_to_send.end(); extern std::list<HANDLE> sent_msgs; for (list<string>::iterator p = globals.hcontact_data[hContact].msgs_to_send.begin(); p != end; ++p) { sent_msgs.push_back((HANDLE)ProtoChainSend(hContact, PSS_MESSAGE, 0, (LPARAM)p->c_str())); HistoryLog(hContact, db_event((char*)p->c_str(), 0, 0, DBEF_SENT)); - boost::this_thread::sleep(boost::posix_time::seconds(1)); + Sleep(1000); } globals.hcontact_data[hContact].msgs_to_send.clear(); return; @@ -1512,19 +1509,19 @@ INT_PTR ImportGpGKeys(WPARAM, LPARAM) acc = pa->szModuleName; } break; - + case DBVT_BLOB: //TODO db_free(&dbv); break; - + case DBVT_WCHAR: //TODO db_free(&dbv); break; } } - + if (acc.length()) { const char *uid = Proto_GetUniqueId(acc.c_str()); for (auto &hContact : Contacts(acc.c_str())) { @@ -1908,105 +1905,102 @@ bool gpg_use_new_random_key(char *account_name, wchar_t *gpg_bin_path, wchar_t * { if (gpg_bin_path && gpg_home_dir) gpg_save_paths(gpg_bin_path, gpg_home_dir); + + wstring path; { - wstring path; - { - // generating key file - wchar_t *tmp = nullptr; - if (gpg_home_dir) - tmp = gpg_home_dir; - else - tmp = UniGetContactSettingUtf(NULL, MODULENAME, "szHomePath", L""); - path = tmp; - if (!gpg_home_dir) - mir_free(tmp); - path.append(L"\\new_key"); - wfstream f(path.c_str(), std::ios::out); - if (!f.is_open()) { - MessageBox(nullptr, TranslateT("Failed to open file"), TranslateT("Error"), MB_OK); - return false; - } - f << "Key-Type: RSA"; - f << "\n"; - f << "Key-Length: 4096"; - f << "\n"; - f << "Subkey-Type: RSA"; - f << "\n"; - f << "Name-Real: "; - f << get_random(6).c_str(); - f << "\n"; - f << "Name-Email: "; - f << get_random(5).c_str(); - f << "@"; - f << get_random(5).c_str(); - f << "."; - f << get_random(3).c_str(); - f << "\n"; - f.close(); + // generating key file + wchar_t *tmp = nullptr; + if (gpg_home_dir) + tmp = gpg_home_dir; + else + tmp = UniGetContactSettingUtf(NULL, MODULENAME, "szHomePath", L""); + path = tmp; + if (!gpg_home_dir) + mir_free(tmp); + path.append(L"\\new_key"); + wfstream f(path.c_str(), std::ios::out); + if (!f.is_open()) { + MessageBox(nullptr, TranslateT("Failed to open file"), TranslateT("Error"), MB_OK); + return false; } - { // gpg execution - DWORD code; - string out; - std::vector<wstring> cmd; - cmd.push_back(L"--batch"); - cmd.push_back(L"--yes"); - cmd.push_back(L"--gen-key"); - cmd.push_back(path); - gpg_execution_params params(cmd); - pxResult result; - params.out = &out; - params.code = &code; - params.result = &result; - if (!gpg_launcher(params, boost::posix_time::minutes(10))) - return false; - if (result == pxNotFound) - return false; - - boost::filesystem::remove(path); - string::size_type p1 = 0; - if ((p1 = out.find("key ")) != string::npos) - path = toUTF16(out.substr(p1 + 4, 8)); - else - path.clear(); + f << "Key-Type: RSA"; + f << "\n"; + f << "Key-Length: 4096"; + f << "\n"; + f << "Subkey-Type: RSA"; + f << "\n"; + f << "Name-Real: "; + f << get_random(6).c_str(); + f << "\n"; + f << "Name-Email: "; + f << get_random(5).c_str(); + f << "@"; + f << get_random(5).c_str(); + f << "."; + f << get_random(3).c_str(); + f << "\n"; + f.close(); + } + { // gpg execution + DWORD code; + string out; + std::vector<wstring> cmd; + cmd.push_back(L"--batch"); + cmd.push_back(L"--yes"); + cmd.push_back(L"--gen-key"); + cmd.push_back(path); + gpg_execution_params params(cmd); + pxResult result; + params.out = &out; + params.code = &code; + params.result = &result; + if (!gpg_launcher(params, boost::posix_time::minutes(10))) + return false; + if (result == pxNotFound) + return false; + + boost::filesystem::remove(path); + string::size_type p1 = 0; + if ((p1 = out.find("key ")) != string::npos) + path = toUTF16(out.substr(p1 + 4, 8)); + else + path.clear(); + } + + if (!path.empty()) { + string out; + DWORD code; + std::vector<wstring> cmd; + cmd.push_back(L"--batch"); + cmd.push_back(L"-a"); + cmd.push_back(L"--export"); + cmd.push_back(path); + gpg_execution_params params(cmd); + pxResult result; + params.out = &out; + params.code = &code; + params.result = &result; + if (!gpg_launcher(params)) + return false; + + if (result == pxNotFound) + return false; + + string::size_type s = 0; + while ((s = out.find("\r", s)) != string::npos) + out.erase(s, 1); + + if (!mir_strcmp(account_name, Translate("Default"))) { + db_set_s(NULL, MODULENAME, "GPGPubKey", out.c_str()); + db_set_ws(NULL, MODULENAME, "KeyID", path.c_str()); } - if (!path.empty()) { - string out; - DWORD code; - std::vector<wstring> cmd; - cmd.push_back(L"--batch"); - cmd.push_back(L"-a"); - cmd.push_back(L"--export"); - cmd.push_back(path); - gpg_execution_params params(cmd); - pxResult result; - params.out = &out; - params.code = &code; - params.result = &result; - if (!gpg_launcher(params)) { - return false; - } - if (result == pxNotFound) - return false; - string::size_type s = 0; - while ((s = out.find("\r", s)) != string::npos) { - out.erase(s, 1); - } - { - if (!mir_strcmp(account_name, Translate("Default"))) - { - db_set_s(NULL, MODULENAME, "GPGPubKey", out.c_str()); - db_set_ws(NULL, MODULENAME, "KeyID", path.c_str()); - } - else - { - std::string acc_str = account_name; - acc_str += "_GPGPubKey"; - db_set_s(NULL, MODULENAME, acc_str.c_str(), out.c_str()); - acc_str = account_name; - acc_str += "_KeyID"; - db_set_ws(NULL, MODULENAME, acc_str.c_str(), path.c_str()); - } - } + else { + std::string acc_str = account_name; + acc_str += "_GPGPubKey"; + db_set_s(NULL, MODULENAME, acc_str.c_str(), out.c_str()); + acc_str = account_name; + acc_str += "_KeyID"; + db_set_ws(NULL, MODULENAME, acc_str.c_str(), path.c_str()); } } return true; diff --git a/plugins/New_GPG/src/utilities.h b/plugins/New_GPG/src/utilities.h index 984970fbaf..ffcf265b44 100755 --- a/plugins/New_GPG/src/utilities.h +++ b/plugins/New_GPG/src/utilities.h @@ -21,9 +21,7 @@ wchar_t* __stdcall UniGetContactSettingUtf(MCONTACT hContact, const char *szModu char* __stdcall UniGetContactSettingUtf(MCONTACT hContact, const char *szModule,const char* szSetting, char* szDef); void GetFilePath(wchar_t *WindowTittle, char *szSetting, wchar_t *szExt, wchar_t *szExtDesc); wchar_t *GetFilePath(wchar_t *WindowTittle, wchar_t *szExt, wchar_t *szExtDesc, bool save_file = false); -void GetFolderPath(wchar_t *WindowTittle, char *szSetting); - -void storeOutput(HANDLE ahandle, string *output); +void GetFolderPath(wchar_t *WindowTittle); void setSrmmIcon(MCONTACT); void setClistIcon(MCONTACT); diff --git a/plugins/New_GPG/src/version.h b/plugins/New_GPG/src/version.h index 2343966394..29760db317 100755 --- a/plugins/New_GPG/src/version.h +++ b/plugins/New_GPG/src/version.h @@ -1,7 +1,7 @@ #define __MAJOR_VERSION 0 -#define __MINOR_VERSION 0 +#define __MINOR_VERSION 1 #define __RELEASE_NUM 0 -#define __BUILD_NUM 14 +#define __BUILD_NUM 1 #include <stdver.h> |