From 365d949d8ec2712fbdec479e73e78cbbe80b3399 Mon Sep 17 00:00:00 2001 From: Gluzskiy Alexandr Date: Fri, 18 May 2012 22:28:05 +0300 Subject: fixed (#33 Settings) --- globals.h | 2 +- gpg_wrapper.cpp | 2 +- init.cpp | 2 +- main.cpp | 54 ++++++++++++++++++++++++++------------------ messages.cpp | 6 ++--- utilities.cpp | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- utilities.h | 3 ++- 7 files changed, 109 insertions(+), 30 deletions(-) diff --git a/globals.h b/globals.h index 334e535..e62ab24 100755 --- a/globals.h +++ b/globals.h @@ -16,7 +16,7 @@ #ifndef GLOBALS_H #define GLOBALS_H -extern bool bAppendTags, gpg_configured; +extern bool bAppendTags, gpg_valid, gpg_keyexist; extern TCHAR *inopentag, *inclosetag, *outopentag, *outclosetag; extern logtofile debuglog; #endif diff --git a/gpg_wrapper.cpp b/gpg_wrapper.cpp index a36926e..410a0d5 100755 --- a/gpg_wrapper.cpp +++ b/gpg_wrapper.cpp @@ -24,7 +24,7 @@ pxResult pxExecute(wstring *acommandline, char *ainput, string *aoutput, LPDWORD aexitcode, pxResult *result, HANDLE hProcess, PROCESS_INFORMATION *pr) { // gpg_mutex.lock(); - if(!gpg_configured) + if(!gpg_valid) return pxNotConfigured; extern logtofile debuglog; BOOL success; diff --git a/init.cpp b/init.cpp index 97bc30c..0d2fb4b 100755 --- a/init.cpp +++ b/init.cpp @@ -33,7 +33,7 @@ UTF8_INTERFACE utfi = {0}; RECT key_from_keyserver_rect = {0}, firstrun_rect = {0}, new_key_rect = {0}, key_gen_rect = {0}, load_key_rect = {0}, import_key_rect = {0}, key_password_rect = {0}, load_existing_key_rect = {0}; XML_API xi = {0}; logtofile debuglog; -bool gpg_configured = false; +bool gpg_valid = false, gpg_keyexist = false; std::map hcontact_data; diff --git a/main.cpp b/main.cpp index abd6982..36a037f 100755 --- a/main.cpp +++ b/main.cpp @@ -226,7 +226,8 @@ static BOOL CALLBACK DlgProcFirstRun(HWND hwndDlg,UINT msg,WPARAM wParam,LPARAM delete [] name; } bAutoExchange = CheckStateStoreDB(hwndDlg, IDC_AUTO_EXCHANGE, "bAutoExchange"); - gpg_configured = isGPGConfigured(); + gpg_valid = isGPGValid(); + gpg_keyexist = isGPGKeyExist(); DestroyWindow(hwndDlg); break; case IDC_OTHER: @@ -570,9 +571,7 @@ static BOOL CALLBACK DlgProcGpgBinOpts(HWND hwndDlg, UINT msg, WPARAM wParam, LP if(_waccess(tmp, 0) == -1) { if(errno == ENOENT) - { MessageBox(0, TranslateT("wrong gpg binary location found in system.\nplease choose another location"), TranslateT("Warning"), MB_OK); - } } } } @@ -594,7 +593,7 @@ static BOOL CALLBACK DlgProcGpgBinOpts(HWND hwndDlg, UINT msg, WPARAM wParam, LP params.out = &out; params.code = &code; params.result = &result; - gpg_configured = true; + gpg_valid = true; boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms)); if(!gpg_thread.timed_join(boost::posix_time::seconds(10))) { @@ -603,7 +602,7 @@ static BOOL CALLBACK DlgProcGpgBinOpts(HWND hwndDlg, UINT msg, WPARAM wParam, LP params.hProcess = NULL; debuglog<hContact, db_event(msg, 0, 0, dbflags)); return 0; } - if(bAutoExchange && strstr(msg, "-----PGP KEY REQUEST-----") && isGPGConfigured()) + if(bAutoExchange && strstr(msg, "-----PGP KEY REQUEST-----") && gpg_valid && gpg_keyexist) { char *tmp = UniGetContactSettingUtf(NULL, szGPGModuleName, "GPGPubKey", ""); if(tmp[0]) @@ -557,7 +557,7 @@ int RecvMsgSvc(WPARAM w, LPARAM l) DBWriteContactSettingByte(ccs->hContact, szGPGModuleName, "GPGEncryption", 1); } mir_free(tmp); - if(!isContactHaveKey(ccs->hContact) && bAutoExchange && isGPGConfigured()) + if(!isContactHaveKey(ccs->hContact) && bAutoExchange && gpg_valid && gpg_keyexist) { LPSTR proto = (LPSTR)CallService(MS_PROTO_GETCONTACTBASEPROTO, (WPARAM)ccs->hContact, 0); DWORD uin = DBGetContactSettingDword(ccs->hContact, proto, "UIN", 0); @@ -802,7 +802,7 @@ int SendMsgSvc(WPARAM w, LPARAM l) return CallService(MS_PROTO_CHAINSEND, w, l); if(!isContactHaveKey(ccs->hContact)) { - if(bAutoExchange && !strstr(msg, "-----PGP KEY REQUEST-----") && !strstr(msg, "-----BEGIN PGP PUBLIC KEY BLOCK-----") && isGPGConfigured()) + if(bAutoExchange && !strstr(msg, "-----PGP KEY REQUEST-----") && !strstr(msg, "-----BEGIN PGP PUBLIC KEY BLOCK-----") && gpg_valid) { void send_encrypted_msgs_thread(HANDLE hContact); LPSTR proto = (LPSTR)CallService(MS_PROTO_GETCONTACTBASEPROTO, (WPARAM)ccs->hContact, 0); diff --git a/utilities.cpp b/utilities.cpp index f6a0290..0648c37 100755 --- a/utilities.cpp +++ b/utilities.cpp @@ -1033,7 +1033,7 @@ bool isContactHaveKey(HANDLE hContact) return false; } -bool isGPGConfigured() +bool isGPGKeyExist() { TCHAR *id = UniGetContactSettingUtf(NULL, szGPGModuleName, "KeyID", _T("")); char *key = UniGetContactSettingUtf(NULL, szGPGModuleName, "GPGPubKey", ""); @@ -1047,6 +1047,74 @@ bool isGPGConfigured() mir_free(key); return false; } +bool isGPGValid() +{ + TCHAR *tmp; + bool gpg_exists = false, is_valid = true; + tmp = UniGetContactSettingUtf(NULL, szGPGModuleName, "szGpgBinPath", _T("")); + if(_waccess(tmp, 0) != -1) + gpg_exists = true; + else + { + mir_free(tmp); + TCHAR *path = new TCHAR [MAX_PATH]; + char *mir_path = new char [MAX_PATH]; + CallService(MS_UTILS_PATHTOABSOLUTE, (WPARAM)"\\", (LPARAM)mir_path); + SetCurrentDirectoryA(mir_path); + tmp = mir_a2t(mir_path); + mir_free(mir_path); + mir_realloc(path, (_tcslen(path)+128)*sizeof(TCHAR)); + TCHAR *gpg_path = new TCHAR [MAX_PATH]; + _tcscpy(gpg_path, tmp); + _tcscat(gpg_path, _T("\\GnuPG\\gpg.exe")); + mir_free(tmp); + if(_waccess(gpg_path, 0) != -1) + { + gpg_exists = true; + _tcscpy(path, _T("GnuPG\\gpg.exe")); + } + mir_free(gpg_path); + tmp = mir_wstrdup(path); + delete [] path; + } + DWORD len = MAX_PATH; + if(gpg_exists) + { + DBWriteContactSettingTString(NULL, szGPGModuleName, "szGpgBinPath", tmp); + string out; + DWORD code; + wstring cmd = _T("--version"); + gpg_execution_params params; + pxResult result; + params.cmd = &cmd; + params.useless = ""; + params.out = &out; + params.code = &code; + params.result = &result; + gpg_valid = true; + boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms)); + if(!gpg_thread.timed_join(boost::posix_time::seconds(10))) + { + gpg_thread.~thread(); + TerminateProcess(params.hProcess, 1); + params.hProcess = NULL; + debuglog<