From af58213e6005cbf4b9ce8be2ad66eb6eae3df4a9 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Sun, 16 Aug 2015 22:18:54 +0000 Subject: code cleaning git-svn-id: http://svn.miranda-ng.org/main/trunk@14974 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- plugins/New_GPG/src/utilities.cpp | 1130 ++++++++++++++++--------------------- 1 file changed, 496 insertions(+), 634 deletions(-) (limited to 'plugins/New_GPG/src/utilities.cpp') diff --git a/plugins/New_GPG/src/utilities.cpp b/plugins/New_GPG/src/utilities.cpp index 891a8b16cc..66d9d473e4 100755 --- a/plugins/New_GPG/src/utilities.cpp +++ b/plugins/New_GPG/src/utilities.cpp @@ -24,95 +24,91 @@ extern HINSTANCE hInst; extern HANDLE hLoadPublicKey; extern HGENMENU hToggleEncryption, hSendKey; -TCHAR* __stdcall UniGetContactSettingUtf(MCONTACT hContact, const char *szModule,const char* szSetting, TCHAR* szDef) +TCHAR* __stdcall UniGetContactSettingUtf(MCONTACT hContact, const char *szModule, const char* szSetting, TCHAR* szDef) { - DBVARIANT dbv = {DBVT_DELETED}; - TCHAR* szRes = NULL; - if (db_get_ts(hContact, szModule, szSetting, &dbv)) - return mir_tstrdup(szDef); - else if(dbv.pszVal) - szRes = mir_tstrdup(dbv.ptszVal); - else - szRes = mir_tstrdup(szDef); - - db_free(&dbv); - return szRes; + DBVARIANT dbv = { DBVT_DELETED }; + TCHAR* szRes = NULL; + if (db_get_ts(hContact, szModule, szSetting, &dbv)) + return mir_tstrdup(szDef); + else if (dbv.pszVal) + szRes = mir_tstrdup(dbv.ptszVal); + else + szRes = mir_tstrdup(szDef); + + db_free(&dbv); + 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 = NULL; - if (db_get_s(hContact, szModule, szSetting, &dbv)) - return mir_strdup(szDef); - else if(dbv.pszVal) - szRes = mir_strdup(dbv.pszVal); - else - szRes = mir_strdup(szDef); - db_free(&dbv); - return szRes; + DBVARIANT dbv = { DBVT_DELETED }; + char* szRes = NULL; + if (db_get_s(hContact, szModule, szSetting, &dbv)) + return mir_strdup(szDef); + else if (dbv.pszVal) + szRes = mir_strdup(dbv.pszVal); + else + szRes = mir_strdup(szDef); + db_free(&dbv); + return szRes; } void GetFilePath(TCHAR *WindowTittle, char *szSetting, TCHAR *szExt, TCHAR *szExtDesc) { - TCHAR str[MAX_PATH+2] = {0}, *tmp; - OPENFILENAME ofn={0}; + TCHAR str[MAX_PATH + 2] = { 0 }, *tmp; + OPENFILENAME ofn = { 0 }; TCHAR filter[512], *pfilter; - ofn.lStructSize=CDSIZEOF_STRUCT(OPENFILENAME,lpTemplateName); - ofn.Flags=OFN_EXPLORER; - ofn.lpstrTitle=TranslateW(WindowTittle); - _tcsncpy(filter,TranslateW(szExtDesc), _countof(filter)-1); - pfilter=filter+mir_tstrlen(filter)+1; + ofn.lStructSize = CDSIZEOF_STRUCT(OPENFILENAME, lpTemplateName); + ofn.Flags = OFN_EXPLORER; + ofn.lpstrTitle = TranslateW(WindowTittle); + _tcsncpy(filter, TranslateW(szExtDesc), _countof(filter) - 1); + pfilter = filter + mir_tstrlen(filter) + 1; mir_tstrcpy(pfilter, szExt); - pfilter[mir_tstrlen(pfilter)+1] = '\0'; - pfilter[mir_tstrlen(pfilter)+2] = '\0'; - ofn.lpstrFilter=filter; + pfilter[mir_tstrlen(pfilter) + 1] = '\0'; + pfilter[mir_tstrlen(pfilter) + 2] = '\0'; + ofn.lpstrFilter = filter; tmp = UniGetContactSettingUtf(0, szGPGModuleName, szSetting, _T("")); - _tcsncpy(str, tmp, _countof(str)-1); + _tcsncpy(str, tmp, _countof(str) - 1); mir_free(tmp); - if(mir_tstrlen(str)< 2) + if (mir_tstrlen(str) < 2) str[0] = '\0'; - ofn.lpstrFile=str; - ofn.nMaxFile=_MAX_PATH; - ofn.nMaxFileTitle=MAX_PATH; - if(!GetOpenFileName(&ofn)) + ofn.lpstrFile = str; + ofn.nMaxFile = _MAX_PATH; + ofn.nMaxFileTitle = MAX_PATH; + if (!GetOpenFileName(&ofn)) return; db_set_ts(0, szGPGModuleName, szSetting, str); } TCHAR *GetFilePath(TCHAR *WindowTittle, TCHAR *szExt, TCHAR *szExtDesc, bool save_file) { - TCHAR *str = new TCHAR [MAX_PATH+2]; - OPENFILENAME ofn={0}; + TCHAR *str = new TCHAR[MAX_PATH + 2]; + OPENFILENAME ofn = { 0 }; TCHAR filter[512], *pfilter; - ofn.lStructSize=CDSIZEOF_STRUCT(OPENFILENAME,lpTemplateName); - ofn.Flags=OFN_EXPLORER; - ofn.lpstrTitle=TranslateW(WindowTittle); - mir_tstrcpy(filter,TranslateW(szExtDesc)); - pfilter=filter+mir_tstrlen(filter)+1; + ofn.lStructSize = CDSIZEOF_STRUCT(OPENFILENAME, lpTemplateName); + ofn.Flags = OFN_EXPLORER; + ofn.lpstrTitle = TranslateW(WindowTittle); + mir_tstrcpy(filter, TranslateW(szExtDesc)); + pfilter = filter + mir_tstrlen(filter) + 1; mir_tstrcpy(pfilter, szExt); - pfilter[mir_tstrlen(pfilter)+1] = '\0'; - pfilter[mir_tstrlen(pfilter)+2] = '\0'; - ofn.lpstrFilter=filter; + pfilter[mir_tstrlen(pfilter) + 1] = '\0'; + pfilter[mir_tstrlen(pfilter) + 2] = '\0'; + ofn.lpstrFilter = filter; mir_tstrcpy(str, _T("")); - if(mir_tstrlen(str)< 2) + if (mir_tstrlen(str) < 2) str[0] = '\0'; - ofn.lpstrFile=str; - ofn.nMaxFile=_MAX_PATH; - ofn.nMaxFileTitle=MAX_PATH; - if(!save_file) - { - if(!GetOpenFileName(&ofn)) - { - delete [] str; + ofn.lpstrFile = str; + ofn.nMaxFile = _MAX_PATH; + ofn.nMaxFileTitle = MAX_PATH; + if (!save_file) { + if (!GetOpenFileName(&ofn)) { + delete[] str; return NULL; } } - else - { - if(!GetSaveFileName(&ofn)) - { - delete [] str; + else { + if (!GetSaveFileName(&ofn)) { + delete[] str; return NULL; } } @@ -121,20 +117,17 @@ TCHAR *GetFilePath(TCHAR *WindowTittle, TCHAR *szExt, TCHAR *szExtDesc, bool sav void GetFolderPath(TCHAR *WindowTittle, char*) { - BROWSEINFO pbi = {0}; + BROWSEINFO pbi = { 0 }; pbi.lpszTitle = WindowTittle; - pbi.ulFlags = BIF_EDITBOX|BIF_NEWDIALOGSTYLE|BIF_SHAREABLE; + pbi.ulFlags = BIF_EDITBOX | BIF_NEWDIALOGSTYLE | BIF_SHAREABLE; LPITEMIDLIST pidl = SHBrowseForFolder(&pbi); - if (pidl != 0) - { + if (pidl != 0) { TCHAR path[MAX_PATH]; - if (SHGetPathFromIDList(pidl, path)) - { + if (SHGetPathFromIDList(pidl, path)) { db_set_ts(NULL, szGPGModuleName, "szHomePath", path); } IMalloc * imalloc = 0; - if (SUCCEEDED(SHGetMalloc(&imalloc))) - { + if (SUCCEEDED(SHGetMalloc(&imalloc))) { imalloc->Free(pidl); imalloc->Release(); } @@ -157,32 +150,28 @@ INT_PTR SendKey(WPARAM w, LPARAM) LPSTR proto = GetContactProto(hContact); PROTOACCOUNT *acc = Proto_GetAccount(proto); std::string acc_str; - if(acc) - { + if (acc) { acc_str = toUTF8(acc->tszAccountName); acc_str += "("; acc_str += acc->szModuleName; - acc_str += ")" ; + acc_str += ")"; key_id_str = acc_str; key_id_str += "_KeyID"; acc_str += "_GPGPubKey"; } - szMessage = UniGetContactSettingUtf(NULL, szGPGModuleName, acc_str.empty()?"GPGPubKey":acc_str.c_str(), ""); - if(!szMessage[0]) - { + szMessage = UniGetContactSettingUtf(NULL, szGPGModuleName, acc_str.empty() ? "GPGPubKey" : acc_str.c_str(), ""); + if (!szMessage[0]) { mir_free(szMessage); szMessage = UniGetContactSettingUtf(NULL, szGPGModuleName, "GPGPubKey", ""); //try to get default key as fallback in any way } } - if(szMessage[0]) - { + if (szMessage[0]) { BYTE enc = db_get_b(hContact, szGPGModuleName, "GPGEncryption", 0); db_set_b(hContact, szGPGModuleName, "GPGEncryption", 0); CallContactService(hContact, PSS_MESSAGE, 0, (LPARAM)szMessage); std::string msg = "Public key "; char *keyid = UniGetContactSettingUtf(NULL, szGPGModuleName, key_id_str.c_str(), ""); - if(!keyid[0]) - { + if (!keyid[0]) { mir_free(keyid); keyid = UniGetContactSettingUtf(NULL, szGPGModuleName, "KeyID", ""); } @@ -203,24 +192,21 @@ INT_PTR ToggleEncryption(WPARAM w, LPARAM) { MCONTACT hContact = (MCONTACT)w; BYTE enc; - if(db_mc_isMeta(hContact)) - { + if (db_mc_isMeta(hContact)) { enc = db_get_b(metaGetMostOnline(hContact), szGPGModuleName, "GPGEncryption", 0); - if(MessageBox(0, TranslateT("Do you want to toggle encryption for all subcontacts?"), TranslateT("Metacontact detected"), MB_YESNO) == IDYES) - { + if (MessageBox(0, TranslateT("Do you want to toggle encryption for all subcontacts?"), TranslateT("Metacontact detected"), MB_YESNO) == IDYES) { int count = db_mc_getSubCount(hContact); - for(int i = 0; i < count; i++) - { + for (int i = 0; i < count; i++) { MCONTACT hcnt = db_mc_getSub(hContact, i); - if(hcnt) - db_set_b(hcnt, szGPGModuleName, "GPGEncryption", enc?0:1); + if (hcnt) + db_set_b(hcnt, szGPGModuleName, "GPGEncryption", enc ? 0 : 1); } - db_set_b(hContact, szGPGModuleName, "GPGEncryption", enc?0:1); + db_set_b(hContact, szGPGModuleName, "GPGEncryption", enc ? 0 : 1); } } else { enc = db_get_b(hContact, szGPGModuleName, "GPGEncryption", 0); - db_set_b(hContact, szGPGModuleName, "GPGEncryption", enc?0:1); + db_set_b(hContact, szGPGModuleName, "GPGEncryption", enc ? 0 : 1); } void setSrmmIcon(MCONTACT hContact); void setClistIcon(MCONTACT hContact); @@ -239,21 +225,19 @@ int OnPreBuildContactMenu(WPARAM w, LPARAM) LPSTR proto = GetContactProto(hContact); PROTOACCOUNT *acc = Proto_GetAccount(proto); std::string setting; - if(acc) - { + if (acc) { setting = toUTF8(acc->tszAccountName); setting += "("; setting += acc->szModuleName; - setting += ")" ; + setting += ")"; setting += "_KeyID"; } char *keyid = UniGetContactSettingUtf(NULL, szGPGModuleName, setting.c_str(), ""); - if(!keyid[0]) - { + if (!keyid[0]) { mir_free(keyid); keyid = UniGetContactSettingUtf(NULL, szGPGModuleName, "KeyID", ""); } - TCHAR buf[128] = {0}; + TCHAR buf[128] = { 0 }; mir_sntprintf(buf, _T("%s: %s"), TranslateT("Send public key"), toUTF16(keyid).c_str()); mir_free(keyid); Menu_ModifyItem(hSendKey, buf); @@ -261,14 +245,13 @@ int OnPreBuildContactMenu(WPARAM w, LPARAM) int flags; TCHAR *tmp = UniGetContactSettingUtf(hContact, szGPGModuleName, "GPGPubKey", _T("")); - if(!tmp[0]) - { + if (!tmp[0]) { db_unset(hContact, szGPGModuleName, "GPGEncryption"); flags = CMIF_GRAYED; } else flags = 0; - Menu_ModifyItem(hToggleEncryption, + Menu_ModifyItem(hToggleEncryption, db_get_b(hContact, szGPGModuleName, "GPGEncryption", 0) ? _T("Turn off GPG encryption") : _T("Turn on GPG encryption"), INVALID_HANDLE_VALUE, flags); mir_free(tmp); @@ -282,46 +265,40 @@ DWORD file_msg_state = -1; int onProtoAck(WPARAM, LPARAM l) { - ACKDATA *ack=(ACKDATA*)l; - if(ack->type == ACKTYPE_FILE) - { - switch(ack->result) - { - case ACKRESULT_DENIED: case ACKRESULT_FAILED: + ACKDATA *ack = (ACKDATA*)l; + if (ack->type == ACKTYPE_FILE) { + switch (ack->result) { + case ACKRESULT_DENIED: case ACKRESULT_FAILED: break; case ACKRESULT_SUCCESS: { - PROTOFILETRANSFERSTATUS *f = (PROTOFILETRANSFERSTATUS*) ack->hProcess; - if((f->flags & PFTS_SENDING) != PFTS_SENDING) - { + PROTOFILETRANSFERSTATUS *f = (PROTOFILETRANSFERSTATUS*)ack->hProcess; + if ((f->flags & PFTS_SENDING) != PFTS_SENDING) { TCHAR *filename = NULL; - if(f->flags & PFTS_UNICODE) - { - if(f->tszCurrentFile && f->tszCurrentFile[0]) + if (f->flags & PFTS_UNICODE) { + if (f->tszCurrentFile && f->tszCurrentFile[0]) filename = mir_wstrdup(f->tszCurrentFile); - if(!filename) + if (!filename) return 0; } - else - { - if(f->szCurrentFile && f->szCurrentFile[0]) + else { + if (f->szCurrentFile && f->szCurrentFile[0]) filename = mir_utf8decodeT(f->szCurrentFile); - if(!filename) + if (!filename) return 0; } - if(_tcsstr(filename, _T(".gpg"))) //decrypt it + if (_tcsstr(filename, _T(".gpg"))) //decrypt it { //process encrypted file - if(!bFileTransfers && !bSameAction) - { + if (!bFileTransfers && !bSameAction) { void ShowEncryptedFileMsgBox(); ShowEncryptedFileMsgBox(); } - if(!bFileTransfers && bSameAction) + if (!bFileTransfers && bSameAction) return 0; - if(file_msg_state < 1) + if (file_msg_state < 1) return 0; - HistoryLog(ack->hContact, db_event("Received encrypted file, trying to decrypt", 0,0, 0)); - if(!boost::filesystem::exists(f->tszCurrentFile)) + HistoryLog(ack->hContact, db_event("Received encrypted file, trying to decrypt", 0, 0, 0)); + if (!boost::filesystem::exists(f->tszCurrentFile)) return 0; string out; DWORD code; @@ -331,9 +308,8 @@ int onProtoAck(WPARAM, LPARAM l) wstring file = filename; wstring::size_type p1 = file.rfind(_T(".gpg")); file.erase(p1, mir_tstrlen(_T(".gpg"))); - if(boost::filesystem::exists(file)) - { - if(MessageBox(0, TranslateT("Target file exists, do you want to replace it?"), TranslateT("Warning"), MB_YESNO) == IDNO) + if (boost::filesystem::exists(file)) { + if (MessageBox(0, TranslateT("Target file exists, do you want to replace it?"), TranslateT("Warning"), MB_YESNO) == IDNO) return 0; } cmd.push_back(file); @@ -342,35 +318,31 @@ int onProtoAck(WPARAM, LPARAM l) { // password TCHAR *pass = NULL; char *keyid = UniGetContactSettingUtf(ack->hContact, szGPGModuleName, "KeyID", ""); - if(mir_strlen(keyid) > 0) - { + if (mir_strlen(keyid) > 0) { string dbsetting = "szKey_"; dbsetting += keyid; dbsetting += "_Password"; pass = UniGetContactSettingUtf(NULL, szGPGModuleName, dbsetting.c_str(), _T("")); - if(mir_tstrlen(pass) > 0 && bDebugLog) - debuglog<pfnGetContactDisplayName(ack->hContact, 0))+" with password"); + if (mir_tstrlen(pass) > 0 && bDebugLog) + debuglog << std::string(time_str() + ": info: found password in database for key ID: " + keyid + ", trying to decrypt message from " + toUTF8(pcli->pfnGetContactDisplayName(ack->hContact, 0)) + " with password"); } - else - { + else { pass = UniGetContactSettingUtf(NULL, szGPGModuleName, "szKeyPassword", _T("")); - if(mir_tstrlen(pass) > 0 && bDebugLog) - debuglog<pfnGetContactDisplayName(ack->hContact, 0))+" with password"); + if (mir_tstrlen(pass) > 0 && bDebugLog) + debuglog << std::string(time_str() + ": info: found password for all keys in database, trying to decrypt message from " + toUTF8(pcli->pfnGetContactDisplayName(ack->hContact, 0)) + " with password"); } - if(mir_tstrlen(pass) > 0) - { + if (mir_tstrlen(pass) > 0) { cmd.push_back(L"--passphrase"); cmd.push_back(pass); } - else if(password) - { - if(bDebugLog) - debuglog<pfnGetContactDisplayName(ack->hContact, 0))+" with password"); + else if (password) { + if (bDebugLog) + debuglog << std::string(time_str() + ": info: found password in memory, trying to decrypt message from " + toUTF8(pcli->pfnGetContactDisplayName(ack->hContact, 0)) + " with password"); cmd.push_back(L"--passphrase"); cmd.push_back(password); } else if (bDebugLog) - debuglog<pfnGetContactDisplayName(ack->hContact, 0))+" with out password"); + debuglog << std::string(time_str() + ": info: passwords not found in database or memory, trying to decrypt message from " + toUTF8(pcli->pfnGetContactDisplayName(ack->hContact, 0)) + " with out password"); mir_free(pass); mir_free(keyid); } @@ -380,79 +352,66 @@ int onProtoAck(WPARAM, LPARAM l) params.out = &out; params.code = &code; params.result = &result; - if(!gpg_launcher(params, boost::posix_time::minutes(15))) + if (!gpg_launcher(params, boost::posix_time::minutes(15))) return 0; - while(out.find("public key decryption failed: bad passphrase") != string::npos) - { - if(bDebugLog) - debuglog<pfnGetContactDisplayName(ack->hContact, 0))+" password needed, trying to get one"); - if(_terminate) + while (out.find("public key decryption failed: bad passphrase") != string::npos) { + if (bDebugLog) + debuglog << std::string(time_str() + ": info: failed to decrypt messaage from " + toUTF8(pcli->pfnGetContactDisplayName(ack->hContact, 0)) + " password needed, trying to get one"); + if (_terminate) break; { //save inkey id string::size_type s = out.find(" encrypted with "); s = out.find(" ID ", s); s += mir_strlen(" ID "); - string::size_type s2 = out.find(",",s); - if(db_mc_isMeta(ack->hContact)) - db_set_s(metaGetMostOnline(ack->hContact), szGPGModuleName, "InKeyID", out.substr(s, s2-s).c_str()); + string::size_type s2 = out.find(",", s); + if (db_mc_isMeta(ack->hContact)) + db_set_s(metaGetMostOnline(ack->hContact), szGPGModuleName, "InKeyID", out.substr(s, s2 - s).c_str()); else - db_set_s(ack->hContact, szGPGModuleName, "InKeyID", out.substr(s, s2-s).c_str()); + db_set_s(ack->hContact, szGPGModuleName, "InKeyID", out.substr(s, s2 - s).c_str()); } void ShowLoadKeyPasswordWindow(); new_key_hcnt_mutex.lock(); new_key_hcnt = ack->hContact; ShowLoadKeyPasswordWindow(); std::vector cmd2 = cmd; - if(password) - { - if(bDebugLog) - debuglog<pfnGetContactDisplayName(ack->hContact, 0))); + if (password) { + if (bDebugLog) + debuglog << std::string(time_str() + ": info: found password in memory, trying to decrypt message from " + toUTF8(pcli->pfnGetContactDisplayName(ack->hContact, 0))); std::vector tmp; tmp.push_back(L"--passphrase"); tmp.push_back(password); cmd2.insert(cmd2.begin(), tmp.begin(), tmp.end()); } out.clear(); - gpg_execution_params params(cmd2); - //pxResult result; - params.out = &out; - params.code = &code; - params.result = &result; - if(!gpg_launcher(params, boost::posix_time::seconds(15))) - { - //boost::filesystem::remove(filename); + gpg_execution_params params2(cmd2); + params2.out = &out; + params2.code = &code; + params2.result = &result; + if (!gpg_launcher(params2, boost::posix_time::seconds(15))) return 0; - } - if(result == pxNotFound) - { - //boost::filesystem::remove(filename); + if (result == pxNotFound) return 0; - } } - if(result == pxSuccess) + if (result == pxSuccess) boost::filesystem::remove(filename); + } + mir_free(filename); } - mir_free(filename); } + break; } - break; } - } - else if(ack->type == ACKTYPE_MESSAGE) - { + else if (ack->type == ACKTYPE_MESSAGE) { extern std::list sent_msgs; - if(!sent_msgs.empty()) - { - if(ack->result == ACKRESULT_FAILED) - { + if (!sent_msgs.empty()) { + if (ack->result == ACKRESULT_FAILED) { std::list::iterator it = std::find(sent_msgs.begin(), sent_msgs.end(), ack->hProcess); - if(it != sent_msgs.end()) - HistoryLog(ack->hContact, db_event("Failed to send encrypted message", 0,0, 0)); + if (it != sent_msgs.end()) + HistoryLog(ack->hContact, db_event("Failed to send encrypted message", 0, 0, 0)); } - else if(ack->result == ACKRESULT_SUCCESS) - { + else if (ack->result == ACKRESULT_SUCCESS) { std::list::iterator it = std::find(sent_msgs.begin(), sent_msgs.end(), ack->hProcess); - if(it != sent_msgs.end()) + if (it != sent_msgs.end()) sent_msgs.erase(it); } } @@ -465,23 +424,22 @@ std::wstring encrypt_file(MCONTACT hContact, TCHAR *filename) string out; DWORD code; pxResult result; - MCONTACT hcnt = db_mc_isMeta(hContact)?metaGetMostOnline(hContact):hContact; + MCONTACT hcnt = db_mc_isMeta(hContact) ? metaGetMostOnline(hContact) : hContact; std::vector cmd; cmd.push_back(L"--batch"); cmd.push_back(L"--tes"); cmd.push_back(L"-r"); char *keyid = UniGetContactSettingUtf(hcnt, szGPGModuleName, "KeyID", ""); TCHAR *szKeyid = mir_a2t(keyid); - TCHAR *name = _tcsrchr(filename,_T('\\')); - if( !name ) + TCHAR *name = _tcsrchr(filename, _T('\\')); + if (!name) name = filename; else name++; - TCHAR *file_out = new TCHAR [mir_tstrlen(name) + mir_tstrlen(_T(".gpg")) + 1]; + TCHAR *file_out = new TCHAR[mir_tstrlen(name) + mir_tstrlen(_T(".gpg")) + 1]; mir_sntprintf(file_out, mir_tstrlen(name) + mir_tstrlen(_T(".gpg")) + 1, _T("%s.gpg"), name); cmd.push_back(szKeyid); - if(db_get_b(hcnt, szGPGModuleName, "bAlwaysTrust", 0)) - { + if (db_get_b(hcnt, szGPGModuleName, "bAlwaysTrust", 0)) { cmd.push_back(L"--trust-model"); cmd.push_back(L"always"); } @@ -500,20 +458,18 @@ std::wstring encrypt_file(MCONTACT hContact, TCHAR *filename) params.out = &out; params.code = &code; params.result = &result; - delete [] file_out; - if(!gpg_launcher(params, boost::posix_time::minutes(3))) + delete[] file_out; + if (!gpg_launcher(params, boost::posix_time::minutes(3))) return 0; - if(out.find("There is no assurance this key belongs to the named user") != string::npos) - { + if (out.find("There is no assurance this key belongs to the named user") != string::npos) { out.clear(); - if(MessageBox(0, TranslateT("We're trying to encrypt with untrusted key. Do you want to trust this key permanently?"), TranslateT("Warning"), MB_YESNO) == IDYES) - { + if (MessageBox(0, TranslateT("We're trying to encrypt with untrusted key. Do you want to trust this key permanently?"), TranslateT("Warning"), MB_YESNO) == IDYES) { db_set_b(hcnt, szGPGModuleName, "bAlwaysTrust", 1); std::vector tmp; tmp.push_back(L"--trust-model"); tmp.push_back(L"always"); cmd.insert(cmd.begin(), tmp.begin(), tmp.end()); - if(!gpg_launcher(params, boost::posix_time::minutes(3))) + if (!gpg_launcher(params, boost::posix_time::minutes(3))) return 0; } else @@ -525,73 +481,62 @@ std::wstring encrypt_file(MCONTACT hContact, TCHAR *filename) //from secureim partially INT_PTR onSendFile(WPARAM w, LPARAM l) { - CCSDATA *ccs=(CCSDATA*)l; - if(!bFileTransfers) + CCSDATA *ccs = (CCSDATA*)l; + if (!bFileTransfers) return Proto_ChainSend(w, ccs); - if(isContactSecured(ccs->hContact)) - { + if (isContactSecured(ccs->hContact)) { char *proto = GetContactProto(ccs->hContact); DWORD uin = db_get_dw(ccs->hContact, proto, "UIN", 0); bool cap_found = false, supported_proto = false; - if(uin) - { - if( ProtoServiceExists(proto, PS_ICQ_CHECKCAPABILITY)) { + if (uin) { + if (ProtoServiceExists(proto, PS_ICQ_CHECKCAPABILITY)) { supported_proto = true; - ICQ_CUSTOMCAP cap = {0}; - strncpy(cap.caps, "GPGFileTransfer",sizeof(cap.caps)); + ICQ_CUSTOMCAP cap = { 0 }; + strncpy(cap.caps, "GPGFileTransfer", sizeof(cap.caps)); if (CallProtoService(proto, PS_ICQ_CHECKCAPABILITY, (WPARAM)ccs->hContact, (LPARAM)&cap)) cap_found = true; } } - else - { + else { TCHAR *jid = UniGetContactSettingUtf(ccs->hContact, proto, "jid", _T("")); - if(jid[0]) - { + if (jid[0]) { extern list Accounts; list::iterator end = Accounts.end(); - for(list::iterator p = Accounts.begin(); p != end; p++) - { + for (list::iterator p = Accounts.begin(); p != end; p++) { TCHAR *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') + if (caps[i] == '\0') + if (caps[i + 1] == '\0') break; } mir_free(caps); - if(str.find(_T("GPG_Encrypted_FileTransfers:0")) != string::npos) + if (str.find(_T("GPG_Encrypted_FileTransfers:0")) != string::npos) cap_found = true; } } } mir_free(jid); } - if(supported_proto && !cap_found) - { - if(MessageBox(0, TranslateT("Capability to decrypt file not found on other side.\nRecipient may be unable to decrypt file(s).\nDo you want to encrypt file(s) anyway?"), TranslateT("File transfer warning"), MB_YESNO) == IDNO) + if (supported_proto && !cap_found) { + if (MessageBox(0, TranslateT("Capability to decrypt file not found on other side.\nRecipient may be unable to decrypt file(s).\nDo you want to encrypt file(s) anyway?"), TranslateT("File transfer warning"), MB_YESNO) == IDNO) return Proto_ChainSend(w, ccs); } - if(!supported_proto) - { - if(MessageBox(0, TranslateT("Unable to check encryption support on other side.\nRecipient may be unable to decrypt file(s).\nCurrently capability check supported only for ICQ and Jabber protocols.\nIt will work for any other proto if Miranda with New_GPG is used on other side.\nDo you want to encrypt file(s) anyway?"), TranslateT("File transfer warning"), MB_YESNO) == IDNO) + if (!supported_proto) { + if (MessageBox(0, TranslateT("Unable to check encryption support on other side.\nRecipient may be unable to decrypt file(s).\nCurrently capability check supported only for ICQ and Jabber protocols.\nIt will work for any other proto if Miranda with New_GPG is used on other side.\nDo you want to encrypt file(s) anyway?"), TranslateT("File transfer warning"), MB_YESNO) == IDNO) return Proto_ChainSend(w, ccs); } HistoryLog(ccs->hContact, db_event(Translate("encrypting file for transfer"), 0, 0, DBEF_SENT)); - if(StriStr(ccs->szProtoService, "/sendfilew")) - { - TCHAR **file=(TCHAR **)ccs->lParam; - for(int i = 0; file[i]; i++) - { - if(!boost::filesystem::exists(file[i])) + if (StriStr(ccs->szProtoService, "/sendfilew")) { + TCHAR **file = (TCHAR **)ccs->lParam; + for (int i = 0; file[i]; i++) { + if (!boost::filesystem::exists(file[i])) return 0; //we do not want to send file unencrypted (sometimes ack have wrong info) - if (_tcsstr(file[i],_T(".gpg"))) + if (_tcsstr(file[i], _T(".gpg"))) continue; std::wstring path_out = encrypt_file(ccs->hContact, file[i]); mir_free(file[i]); @@ -599,14 +544,12 @@ INT_PTR onSendFile(WPARAM w, LPARAM l) transfers.push_back(path_out); } } - else - { - char **file = (char**) ccs->lParam; - for(int i = 0; file[i]; i++) - { - if(!boost::filesystem::exists(file[i])) + else { + char **file = (char**)ccs->lParam; + for (int i = 0; file[i]; i++) { + if (!boost::filesystem::exists(file[i])) return 0; //we do not want to send file unencrypted (sometimes ack have wrong info) - if (strstr(file[i],".gpg")) + if (strstr(file[i], ".gpg")) continue; TCHAR *tmp = mir_utf8decodeT(file[i]); std::wstring path_out = encrypt_file(ccs->hContact, tmp); @@ -629,11 +572,11 @@ void HistoryLog(MCONTACT hContact, db_event evt) Event.szModule = szGPGModuleName; Event.eventType = evt.eventType; Event.flags = evt.flags; - if(!evt.timestamp) + if (!evt.timestamp) Event.timestamp = (DWORD)time(NULL); else Event.timestamp = evt.timestamp; - Event.cbBlob = (DWORD)mir_strlen((char*)evt.pBlob)+1; + Event.cbBlob = (DWORD)mir_strlen((char*)evt.pBlob) + 1; Event.pBlob = (PBYTE)_strdup((char*)evt.pBlob); db_event_add(hContact, &Event); } @@ -666,20 +609,17 @@ int GetJabberInterface(WPARAM, LPARAM) //get interface for all jabber accounts, Accounts.push_back(new JabberAccount); p = Accounts.begin(); (*p)->setAccountNumber(0); - for(int i = 0; i < count; i++) //get only jabber accounts from all accounts + for (int i = 0; i < count; i++) //get only jabber accounts from all accounts { IJabberInterface *JIftmp = getJabberApi(accounts[i]->szModuleName); int a = 0; - if(JIftmp) - { + if (JIftmp) { (*p)->setJabberInterface(JIftmp); - if(accounts[i]->tszAccountName) - { + if (accounts[i]->tszAccountName) { TCHAR* tmp = mir_tstrdup(accounts[i]->tszAccountName); (*p)->setAccountName(tmp); } - else - { + else { TCHAR *tmp = mir_a2t(accounts[i]->szModuleName); (*p)->setAccountName(tmp); } @@ -697,53 +637,44 @@ int GetJabberInterface(WPARAM, LPARAM) //get interface for all jabber accounts, static JABBER_HANDLER_FUNC SendHandler(IJabberInterface *ji, HXML node, void*) { HXML local_node = node; - for(int n = 0; n <= xmlGetChildCount(node); n++) - { - LPCTSTR str = xmlGetText(local_node); + for (int n = 0; n <= xmlGetChildCount(node); n++) { + LPCTSTR str = xmlGetText(local_node); LPCTSTR nodename = xmlGetName(local_node); LPCTSTR attr = xmlGetAttrValue(local_node, _T("to")); - if(attr) - { + if (attr) { MCONTACT hContact = ji->ContactFromJID(attr); - if(hContact) - if(!isContactSecured(hContact)) + if (hContact) + if (!isContactSecured(hContact)) return FALSE; } - if(str) - { - if(_tcsstr(str, _T("-----BEGIN PGP MESSAGE-----")) && _tcsstr(str, _T("-----END PGP MESSAGE-----"))) - { + if (str) { + if (_tcsstr(str, _T("-----BEGIN PGP MESSAGE-----")) && _tcsstr(str, _T("-----END PGP MESSAGE-----"))) { wstring data = str; xmlSetText(local_node, _T("This message is encrypted.")); wstring::size_type p1 = data.find(_T("-----BEGIN PGP MESSAGE-----")) + mir_tstrlen(_T("-----BEGIN PGP MESSAGE-----")); - while(data.find(_T("Version: "), p1) != wstring::npos) - { + while (data.find(_T("Version: "), p1) != wstring::npos) { p1 = data.find(_T("Version: "), p1); p1 = data.find(_T("\n"), p1); } - while(data.find(_T("Comment: "), p1) != wstring::npos) - { + while (data.find(_T("Comment: "), p1) != wstring::npos) { p1 = data.find(_T("Comment: "), p1); p1 = data.find(_T("\n"), p1); } - while(data.find(_T("Encoding: "), p1) != wstring::npos) - { + while (data.find(_T("Encoding: "), p1) != wstring::npos) { p1 = data.find(_T("Encoding: "), p1); p1 = data.find(_T("\n"), p1); } - p1+=3; + p1 += 3; wstring::size_type p2 = data.find(_T("-----END PGP MESSAGE-----")); - wstring data2 = data.substr(p1, p2-p1-2); + wstring data2 = data.substr(p1, p2 - p1 - 2); strip_line_term(data2); HXML encrypted_data = xmlAddChild(node, _T("x"), data2.c_str()); xmlAddAttr(encrypted_data, _T("xmlns"), _T("jabber:x:encrypted")); return FALSE; } } - if(bPresenceSigning && nodename) - { - if(_tcsstr(nodename, _T("status"))) - { + if (bPresenceSigning && nodename) { + if (_tcsstr(nodename, _T("status"))) { TCHAR *path_c = UniGetContactSettingUtf(NULL, szGPGModuleName, "szHomePath", _T("")); wstring path_out = path_c; wstring file = toUTF16(get_random(10)); @@ -752,12 +683,11 @@ static JABBER_HANDLER_FUNC SendHandler(IJabberInterface *ji, HXML node, void*) path_out += file; boost::filesystem::remove(path_out); wfstream f(path_out.c_str(), std::ios::out); - f<GetModuleName(); char setting[64]; - mir_snprintf(setting, sizeof(setting)-1,"%s_KeyID",proto); + mir_snprintf(setting, sizeof(setting) - 1, "%s_KeyID", proto); inkeyid = UniGetContactSettingUtf(NULL, szGPGModuleName, setting, ""); - if(!inkeyid[0]) - { + if (!inkeyid[0]) { mir_free(inkeyid); inkeyid = UniGetContactSettingUtf(NULL, szGPGModuleName, "KeyID", ""); } } TCHAR *pass = NULL; - if(inkeyid[0]) - { + if (inkeyid[0]) { string dbsetting = "szKey_"; dbsetting += inkeyid; dbsetting += "_Password"; pass = UniGetContactSettingUtf(NULL, szGPGModuleName, dbsetting.c_str(), _T("")); - if(pass[0] && bDebugLog) - debuglog< Accounts; - list ::iterator p = Accounts.begin(); - for(unsigned int i = 0; i < Accounts.size(); i++, p++) - { - if(!(*p)) + list ::iterator p = Accounts.begin(); + for (unsigned int m = 0; m < Accounts.size(); m++, p++) { + if (!(*p)) break; hContact = (*p)->getJabberInterface()->ContactFromJID(xmlGetAttrValue(node, _T("from"))); - if(hContact) - hcontact_data[hContact].key_in_prescense = out.substr(p1, p2-p1-1).c_str(); + if (hContact) + hcontact_data[hContact].key_in_prescense = out.substr(p1, p2 - p1 - 1).c_str(); } } } @@ -1023,30 +928,24 @@ static JABBER_HANDLER_FUNC MessageHandler(IJabberInterface*, HXML, void*) return FALSE; } - - - void AddHandlers() { extern list Accounts; list::iterator end = Accounts.end(); - for(list::iterator p = Accounts.begin(); p != end; p++) - { - if(!(*p)) + for (list::iterator p = Accounts.begin(); p != end; p++) { + if (!(*p)) break; - if((*p)->getSendHandler() == INVALID_HANDLE_VALUE) + if ((*p)->getSendHandler() == INVALID_HANDLE_VALUE) (*p)->setSendHandler((*p)->getJabberInterface()->AddSendHandler((JABBER_HANDLER_FUNC)SendHandler)); - if((*p)->getPrescenseHandler() == INVALID_HANDLE_VALUE) + 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(bAutoExchange) - { + // if((*p)->getMessageHandler() == INVALID_HANDLE_VALUE) + // (*p)->setMessageHandler((*p)->getJabberInterface()->AddMessageHandler((JABBER_HANDLER_FUNC)MessageHandler, JABBER_MESSAGE_TYPE_ANY ,NULL,NULL)); + if (bAutoExchange) { (*p)->getJabberInterface()->RegisterFeature(_T("GPG_Key_Auto_Exchange:0"), _T("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(_T("GPG_Key_Auto_Exchange:0\0\0")); } - if(bFileTransfers) - { + if (bFileTransfers) { (*p)->getJabberInterface()->RegisterFeature(_T("GPG_Encrypted_FileTransfers:0"), _T("Indicates that gpg installed and configured to encrypt files (currently implemented in new_gpg plugin for Miranda IM and Miranda NG)")); (*p)->getJabberInterface()->AddFeatures(_T("GPG_Encrypted_FileTransfers:0\0\0")); } @@ -1056,34 +955,30 @@ void AddHandlers() bool isContactSecured(MCONTACT hContact) { BYTE gpg_enc = db_get_b(hContact, szGPGModuleName, "GPGEncryption", 0); - if(!gpg_enc) - { - if(bDebugLog) - debuglog<pfnGetContactDisplayName(hContact, 0))); + if (!gpg_enc) { + if (bDebugLog) + debuglog << std::string(time_str() + ": encryption is turned off for " + toUTF8(pcli->pfnGetContactDisplayName(hContact, 0))); return false; } - if(!db_mc_isMeta(hContact)) - { + if (!db_mc_isMeta(hContact)) { TCHAR *key = UniGetContactSettingUtf(hContact, szGPGModuleName, "GPGPubKey", _T("")); - if(!key[0]) - { + if (!key[0]) { mir_free(key); - if(bDebugLog) - debuglog<pfnGetContactDisplayName(hContact, 0))); + if (bDebugLog) + debuglog << std::string(time_str() + ": encryption is turned off for " + toUTF8(pcli->pfnGetContactDisplayName(hContact, 0))); return false; } mir_free(key); } - if(bDebugLog) - debuglog<pfnGetContactDisplayName(hContact, 0))); + if (bDebugLog) + debuglog << std::string(time_str() + ": encryption is turned on for " + toUTF8(pcli->pfnGetContactDisplayName(hContact, 0))); return true; } bool isContactHaveKey(MCONTACT hContact) { TCHAR *key = UniGetContactSettingUtf(hContact, szGPGModuleName, "GPGPubKey", _T("")); - if(mir_tstrlen(key) > 0) - { + if (mir_tstrlen(key) > 0) { mir_free(key); return true; } @@ -1095,8 +990,7 @@ bool isGPGKeyExist() { TCHAR *id = UniGetContactSettingUtf(NULL, szGPGModuleName, "KeyID", _T("")); char *key = UniGetContactSettingUtf(NULL, szGPGModuleName, "GPGPubKey", ""); - if(id[0] && key[0]) - { + if (id[0] && key[0]) { mir_free(id); mir_free(key); return true; @@ -1112,10 +1006,9 @@ bool isGPGValid() tmp = UniGetContactSettingUtf(NULL, szGPGModuleName, "szGpgBinPath", _T("")); boost::filesystem::path p(tmp); - if(boost::filesystem::exists(p) && boost::filesystem::is_regular_file(p)) + if (boost::filesystem::exists(p) && boost::filesystem::is_regular_file(p)) gpg_exists = true; - else - { + else { mir_free(tmp); tmp = NULL; TCHAR *path = (TCHAR*)mir_alloc(sizeof(TCHAR)*MAX_PATH); @@ -1131,8 +1024,7 @@ bool isGPGValid() mir_free(tmp); tmp = NULL; p = boost::filesystem::path(gpg_path); - if(boost::filesystem::exists(p) && boost::filesystem::is_regular_file(p)) - { + if (boost::filesystem::exists(p) && boost::filesystem::is_regular_file(p)) { gpg_exists = true; mir_tstrcpy(path, _T("GnuPG\\gpg.exe")); } @@ -1141,8 +1033,7 @@ bool isGPGValid() mir_free(path); } - if(gpg_exists) - { + if (gpg_exists) { db_set_ts(NULL, szGPGModuleName, "szGpgBinPath", tmp); mir_free(tmp); tmp = NULL; @@ -1159,22 +1050,21 @@ bool isGPGValid() gpg_launcher(params); gpg_valid = false; string::size_type p1 = out.find("(GnuPG) "); - if(p1 == string::npos) + if (p1 == string::npos) is_valid = false; } - if(tmp) - { + if (tmp) { mir_free(tmp); tmp = NULL; } -/* if(!gpg_exists) - { - wstring path_ = _wgetenv(_T("APPDATA")); - path_ += _T("\\GnuPG"); - tmp = UniGetContactSettingUtf(NULL, szGPGModuleName, "szHomePath", (TCHAR*)path_.c_str()); - } - if(tmp) - mir_free(tmp); */ + /* if(!gpg_exists) + { + wstring path_ = _wgetenv(_T("APPDATA")); + path_ += _T("\\GnuPG"); + tmp = UniGetContactSettingUtf(NULL, szGPGModuleName, "szHomePath", (TCHAR*)path_.c_str()); + } + if(tmp) + mir_free(tmp); */ return is_valid && gpg_exists; } @@ -1189,18 +1079,18 @@ const bool StriStr(const char *str, const char *substr) CharUpperBuffA(str_up, (DWORD)mir_strlen(str_up)); CharUpperBuffA(substr_up, (DWORD)mir_strlen(substr_up)); - if(strstr (str_up, substr_up)) + if (strstr(str_up, substr_up)) i = true; mir_free(str_up); mir_free(substr_up); - + return i; } bool IsOnline(MCONTACT hContact) { - if(db_get_b(hContact, szGPGModuleName, "Status", 0) == ID_STATUS_OFFLINE) + if (db_get_b(hContact, szGPGModuleName, "Status", 0) == ID_STATUS_OFFLINE) return false; return true; } @@ -1208,13 +1098,15 @@ bool IsOnline(MCONTACT hContact) //from secureim #include -struct TFakeAckParams { - inline TFakeAckParams( HANDLE p1, MCONTACT p2, LONG p3, LPCSTR p4 ) : - hEvent( p1 ), - hContact( p2 ), - id( p3 ), - msg( p4 ) - {} +struct TFakeAckParams +{ + inline TFakeAckParams(HANDLE p1, MCONTACT p2, LONG p3, LPCSTR p4) : + hEvent(p1), + hContact(p2), + id(p3), + msg(p4) + { + } HANDLE hEvent; MCONTACT hContact; @@ -1224,32 +1116,33 @@ struct TFakeAckParams { __forceinline int SendBroadcast(MCONTACT hContact, int type, int result, HANDLE hProcess, LPARAM lParam) { - return ProtoBroadcastAck( GetContactProto(hContact), hContact, type, result, hProcess, lParam); + return ProtoBroadcastAck(GetContactProto(hContact), hContact, type, result, hProcess, lParam); } unsigned __stdcall sttFakeAck(void *param) { - TFakeAckParams *tParam = ( TFakeAckParams* )param; - WaitForSingleObject( tParam->hEvent, INFINITE ); + TFakeAckParams *tParam = (TFakeAckParams*)param; + WaitForSingleObject(tParam->hEvent, INFINITE); - Sleep( 100 ); - if ( tParam->msg == NULL ) - SendBroadcast(tParam->hContact, ACKTYPE_MESSAGE, ACKRESULT_SUCCESS, (HANDLE)tParam->id, 0 ); + Sleep(100); + if (tParam->msg == NULL) + SendBroadcast(tParam->hContact, ACKTYPE_MESSAGE, ACKRESULT_SUCCESS, (HANDLE)tParam->id, 0); else SendBroadcast(tParam->hContact, ACKTYPE_MESSAGE, ACKRESULT_FAILED, (HANDLE)tParam->id, LPARAM(tParam->msg)); - CloseHandle( tParam->hEvent ); + CloseHandle(tParam->hEvent); delete tParam; return 0; } -int returnNoError(MCONTACT hContact) { - HANDLE hEvent = CreateEvent( NULL, TRUE, FALSE, NULL ); +int returnNoError(MCONTACT hContact) +{ + HANDLE hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); unsigned int tID; - CloseHandle( (HANDLE) _beginthreadex(NULL, 0, sttFakeAck, new TFakeAckParams(hEvent,hContact,777,0), 0, &tID) ); - SetEvent( hEvent ); + CloseHandle((HANDLE)_beginthreadex(NULL, 0, sttFakeAck, new TFakeAckParams(hEvent, hContact, 777, 0), 0, &tID)); + SetEvent(hEvent); return 777; } // end from secureim @@ -1259,14 +1152,13 @@ int returnNoError(MCONTACT hContact) { string toUTF8(wstring str) { string ustr; - try{ - utf8::utf16to8(str.begin(), str.end(), back_inserter(ustr)); + try { + utf8::utf16to8(str.begin(), str.end(), back_inserter(ustr)); } - catch(const utf8::exception& e) - { - if(bDebugLog) - debuglog<> gen(rng, boost::uniform_int<>(0, (int)chars.length()-1)); - for(int i = 0; i < length; ++i) - data += chars[gen()]; + boost::variate_generator> gen(rng, boost::uniform_int<>(0, (int)chars.length() - 1)); + for (int i = 0; i < length; ++i) + data += chars[gen()]; return data; } void send_encrypted_msgs_thread(void *param) { - MCONTACT hContact = (MCONTACT)param; - while(true) - { + MCONTACT hContact = (MCONTACT)(DWORD_PTR)param; + while (true) { //char *key = UniGetContactSettingUtf(hContact, szGPGModuleName, "GPGPubKey", ""); - while(!isContactSecured(hContact)) + while (!isContactSecured(hContact)) boost::this_thread::sleep(boost::posix_time::seconds(1)); - if(!hcontact_data[hContact].msgs_to_send.empty()) - { + if (!hcontact_data[hContact].msgs_to_send.empty()) { boost::this_thread::sleep(boost::posix_time::seconds(1)); list::iterator end = hcontact_data[hContact].msgs_to_send.end(); extern std::list sent_msgs; - for(list::iterator p = hcontact_data[hContact].msgs_to_send.begin(); p != end; ++p) - { + for (list::iterator p = hcontact_data[hContact].msgs_to_send.begin(); p != end; ++p) { sent_msgs.push_back((HANDLE)CallContactService(hContact, PSS_MESSAGE, 0, (LPARAM)p->c_str())); - HistoryLog(hContact, db_event((char*)p->c_str(),0,0, DBEF_SENT)); + HistoryLog(hContact, db_event((char*)p->c_str(), 0, 0, DBEF_SENT)); boost::this_thread::sleep(boost::posix_time::seconds(1)); } hcontact_data[hContact].msgs_to_send.clear(); @@ -1336,11 +1224,10 @@ string time_str() int handleEnum(const char *szSetting, LPARAM lParam) { - if(!*(bool*)lParam && szSetting[0] && StriStr(szSetting, "tabsrmm")) - { + if (!*(bool*)lParam && szSetting[0] && StriStr(szSetting, "tabsrmm")) { bool f = false, *found = (bool*)lParam; - f = !db_get_b(NULL, "PluginDisable", szSetting, 0); - if(f) + f = !db_get_b(NULL, "PluginDisable", szSetting, 0); + if (f) *found = f; } return 0; @@ -1348,12 +1235,12 @@ int handleEnum(const char *szSetting, LPARAM lParam) bool isTabsrmmUsed() { - DBCONTACTENUMSETTINGS enm = {0}; + DBCONTACTENUMSETTINGS enm = { 0 }; bool found = false; enm.lParam = (LPARAM)&found; enm.pfnEnumProc = handleEnum; enm.szModule = "PluginDisable"; - if(CallService(MS_DB_CONTACT_ENUMSETTINGS, 0, (LPARAM)&enm) == -1) + if (CallService(MS_DB_CONTACT_ENUMSETTINGS, 0, (LPARAM)&enm) == -1) return false; return found; @@ -1361,42 +1248,39 @@ bool isTabsrmmUsed() void ExportGpGKeysFunc(int type) { - TCHAR *p = GetFilePath(_T("Choose file to export keys"), _T("*"), _T("Any file"), true); - if(!p || !p[0]) - { - delete [] p; + TCHAR *p = GetFilePath(_T("Choose file to export keys"), _T("*"), _T("Any file"), true); + if (!p || !p[0]) { + delete[] p; //TODO: handle error return; } char *path = mir_t2a(p); - delete [] p; + delete[] p; std::ofstream file; file.open(path, std::ios::trunc | std::ios::out); mir_free(path); int exported_keys = 0; - if(!file.is_open()) + if (!file.is_open()) return; //TODO: handle error - if(!type || type == 2) { - for(MCONTACT hContact = db_find_first(); hContact; hContact = db_find_next(hContact)) { + if (!type || type == 2) { + for (MCONTACT hContact = db_find_first(); hContact; hContact = db_find_next(hContact)) { char *k = UniGetContactSettingUtf(hContact, szGPGModuleName, "GPGPubKey", ""); std::string key; - if(!k[0]) - { + if (!k[0]) { mir_free(k); continue; } - else - { + else { key = k; mir_free(k); } - + const char* proto = (const char*)GetContactProto(hContact); std::string id = "Comment: login "; - const char * uid = (const char*)CallProtoService(proto, PS_GETCAPS, (WPARAM)PFLAG_UNIQUEIDSETTING, 0); - DBVARIANT dbv = {0}; + const char * uid = (const char*)CallProtoService(proto, PS_GETCAPS, (WPARAM)PFLAG_UNIQUEIDSETTING, 0); + DBVARIANT dbv = { 0 }; db_get(0, proto, uid, &dbv); - switch(dbv.type) { + switch (dbv.type) { case DBVT_DELETED: continue; @@ -1430,7 +1314,7 @@ void ExportGpGKeysFunc(int type) case DBVT_UTF8: { char *tmp = mir_utf8decodeA(dbv.pszVal); - if(tmp[0]) + if (tmp[0]) id += tmp; mir_free(tmp); db_free(&dbv); @@ -1448,7 +1332,7 @@ void ExportGpGKeysFunc(int type) id += " contact_id "; memset(&dbv, 0, sizeof(dbv)); db_get(hContact, proto, uid, &dbv); - switch(dbv.type) { + switch (dbv.type) { case DBVT_DELETED: continue; case DBVT_BYTE: @@ -1469,7 +1353,7 @@ void ExportGpGKeysFunc(int type) { char _id[64]; mir_snprintf(_id, "%d", dbv.dVal); - id += _id; + id += _id; } break; case DBVT_ASCIIZ: @@ -1481,7 +1365,7 @@ void ExportGpGKeysFunc(int type) case DBVT_UTF8: { char *tmp = mir_utf8decodeA(dbv.pszVal); - if(tmp[0]) + if (tmp[0]) id += tmp; mir_free(tmp); db_free(&dbv); @@ -1497,19 +1381,18 @@ void ExportGpGKeysFunc(int type) break; } std::string::size_type p1 = key.find("-----BEGIN PGP PUBLIC KEY BLOCK-----"); - if(p1 == std::string::npos) + if (p1 == std::string::npos) continue; p1 += mir_strlen("-----BEGIN PGP PUBLIC KEY BLOCK-----"); - p1 ++; + p1++; id += '\n'; key.insert(p1, id); - file<szModuleName, PS_GETCAPS, (WPARAM)PFLAG_UNIQUEIDSETTING, 0); - DBVARIANT dbv = {0}; + const char * uid = (const char*)CallProtoService(accs[i]->szModuleName, PS_GETCAPS, (WPARAM)PFLAG_UNIQUEIDSETTING, 0); + DBVARIANT dbv = { 0 }; db_get(0, accs[i]->szModuleName, uid, &dbv); std::string id; - switch(dbv.type) - { + switch (dbv.type) { case DBVT_DELETED: continue; break; @@ -1609,7 +1492,7 @@ INT_PTR ImportGpGKeys(WPARAM, LPARAM) char _id[64]; mir_snprintf(_id, "%d", dbv.bVal); id += _id; - if(id == login) + if (id == login) acc = accs[i]->szModuleName; } break; @@ -1618,7 +1501,7 @@ INT_PTR ImportGpGKeys(WPARAM, LPARAM) char _id[64]; mir_snprintf(_id, "%d", dbv.wVal); id += _id; - if(id == login) + if (id == login) acc = accs[i]->szModuleName; } break; @@ -1627,7 +1510,7 @@ INT_PTR ImportGpGKeys(WPARAM, LPARAM) char _id[64]; mir_snprintf(_id, "%d", dbv.dVal); id += _id; - if(id == login) + if (id == login) acc = accs[i]->szModuleName; } break; @@ -1635,18 +1518,18 @@ INT_PTR ImportGpGKeys(WPARAM, LPARAM) { id += dbv.pszVal; db_free(&dbv); - if(id == login) + if (id == login) acc = accs[i]->szModuleName; } break; case DBVT_UTF8: { char *tmp = mir_utf8decodeA(dbv.pszVal); - if(tmp[0]) + if (tmp[0]) id += tmp; mir_free(tmp); db_free(&dbv); - if(id == login) + if (id == login) acc = accs[i]->szModuleName; } break; @@ -1660,16 +1543,14 @@ INT_PTR ImportGpGKeys(WPARAM, LPARAM) break; } } - if(acc.length()) - { - const char * uid = (const char*)CallProtoService(acc.c_str(), PS_GETCAPS, (WPARAM)PFLAG_UNIQUEIDSETTING, 0); - for(MCONTACT hContact = db_find_first(acc.c_str()); hContact; hContact = db_find_next(hContact, acc.c_str())) { - DBVARIANT dbv = {0}; + if (acc.length()) { + const char * uid = (const char*)CallProtoService(acc.c_str(), PS_GETCAPS, (WPARAM)PFLAG_UNIQUEIDSETTING, 0); + for (MCONTACT hContact = db_find_first(acc.c_str()); hContact; hContact = db_find_next(hContact, acc.c_str())) { + DBVARIANT dbv = { 0 }; db_get(hContact, acc.c_str(), uid, &dbv); std::string id; bool found = false; - switch(dbv.type) - { + switch (dbv.type) { case DBVT_DELETED: continue; break; @@ -1678,7 +1559,7 @@ INT_PTR ImportGpGKeys(WPARAM, LPARAM) char _id[64]; mir_snprintf(_id, "%d", dbv.bVal); id += _id; - if(id == contact_id) + if (id == contact_id) found = true; } break; @@ -1687,7 +1568,7 @@ INT_PTR ImportGpGKeys(WPARAM, LPARAM) char _id[64]; mir_snprintf(_id, "%d", dbv.wVal); id += _id; - if(id == contact_id) + if (id == contact_id) found = true; } break; @@ -1696,7 +1577,7 @@ INT_PTR ImportGpGKeys(WPARAM, LPARAM) char _id[64]; mir_snprintf(_id, "%d", dbv.dVal); id += _id; - if(id == contact_id) + if (id == contact_id) found = true; } break; @@ -1704,18 +1585,18 @@ INT_PTR ImportGpGKeys(WPARAM, LPARAM) { id += dbv.pszVal; db_free(&dbv); - if(id == contact_id) + if (id == contact_id) found = true; } break; case DBVT_UTF8: { char *tmp = mir_utf8decodeA(dbv.pszVal); - if(tmp[0]) + if (tmp[0]) id += tmp; mir_free(tmp); db_free(&dbv); - if(id == contact_id) + if (id == contact_id) found = true; } break; @@ -1728,8 +1609,7 @@ INT_PTR ImportGpGKeys(WPARAM, LPARAM) db_free(&dbv); break; } - if(found) - { + if (found) { wstring path; std::vector cmd; TCHAR *ptmp; @@ -1744,7 +1624,7 @@ INT_PTR ImportGpGKeys(WPARAM, LPARAM) path += rand; boost::filesystem::remove(path); wfstream f(path, std::ios::out); - f< output.find("<", s)) + else if (s2 > output.find("<", s)) s2 = output.find("<", s); - if(s2 != string::npos) - { - tmp2 = (char*)mir_alloc((output.substr(s,s2-s-1).length()+1) * sizeof(char)); - mir_strcpy(tmp2, output.substr(s,s2-s-1).c_str()); + if (s2 != string::npos) { + tmp2 = (char*)mir_alloc((output.substr(s, s2 - s - 1).length() + 1) * sizeof(char)); + mir_strcpy(tmp2, output.substr(s, s2 - s - 1).c_str()); mir_utf8decode(tmp2, 0); - if(hContact) - { - db_set_s(hContact, szGPGModuleName, "KeyMainName", output.substr(s,s2-s-1).c_str()); + if (hContact) { + db_set_s(hContact, szGPGModuleName, "KeyMainName", output.substr(s, s2 - s - 1).c_str()); } mir_free(tmp2); - if((s = output.find(")", s2)) == string::npos) + if ((s = output.find(")", s2)) == string::npos) s = output.find(">", s2); - else if(s > output.find(">", s2)) + else if (s > output.find(">", s2)) s = output.find(">", s2); s2++; - if(output[s] == ')') - { - tmp2 = (char*)mir_alloc((output.substr(s2,s-s2).length()+1) * sizeof(char)); - mir_strcpy(tmp2, output.substr(s2,s-s2).c_str()); + if (output[s] == ')') { + tmp2 = (char*)mir_alloc((output.substr(s2, s - s2).length() + 1) * sizeof(char)); + mir_strcpy(tmp2, output.substr(s2, s - s2).c_str()); mir_utf8decode(tmp2, 0); - if(hContact) - db_set_s(hContact, szGPGModuleName, "KeyComment", output.substr(s2,s-s2).c_str()); + if (hContact) + db_set_s(hContact, szGPGModuleName, "KeyComment", output.substr(s2, s - s2).c_str()); mir_free(tmp2); - s+=3; + s += 3; s2 = output.find(">", s); - tmp2 = (char*)mir_alloc((output.substr(s,s2-s).length()+1) * sizeof(char)); - mir_strcpy(tmp2, output.substr(s,s2-s).c_str()); + tmp2 = (char*)mir_alloc((output.substr(s, s2 - s).length() + 1) * sizeof(char)); + mir_strcpy(tmp2, output.substr(s, s2 - s).c_str()); mir_utf8decode(tmp2, 0); - if(hContact) - db_set_s(hContact, szGPGModuleName, "KeyMainEmail", output.substr(s,s2-s).c_str()); + if (hContact) + db_set_s(hContact, szGPGModuleName, "KeyMainEmail", output.substr(s, s2 - s).c_str()); mir_free(tmp2); } - else - { - tmp2 = (char*)mir_alloc((output.substr(s2,s-s2).length()+1) * sizeof(char)); - mir_strcpy(tmp2, output.substr(s2,s-s2).c_str()); + else { + tmp2 = (char*)mir_alloc((output.substr(s2, s - s2).length() + 1) * sizeof(char)); + mir_strcpy(tmp2, output.substr(s2, s - s2).c_str()); mir_utf8decode(tmp2, 0); - if(hContact) - db_set_s(hContact, szGPGModuleName, "KeyMainEmail", output.substr(s2,s-s2).c_str()); + if (hContact) + db_set_s(hContact, szGPGModuleName, "KeyMainEmail", output.substr(s2, s - s2).c_str()); mir_free(tmp2); } } @@ -1840,22 +1714,21 @@ INT_PTR ImportGpGKeys(WPARAM, LPARAM) } key.clear(); } - if(strstr(line, "-----END PGP PRIVATE KEY BLOCK-----")) - { + if (strstr(line, "-----END PGP PRIVATE KEY BLOCK-----")) { std::vector cmd; - TCHAR tmp2[MAX_PATH] = {0}; + TCHAR tmp2[MAX_PATH] = { 0 }; TCHAR *ptmp; string output; DWORD exitcode; { ptmp = UniGetContactSettingUtf(NULL, szGPGModuleName, "szHomePath", _T("")); - _tcsncpy(tmp2, ptmp, MAX_PATH-1); + _tcsncpy(tmp2, ptmp, MAX_PATH - 1); mir_free(ptmp); mir_tstrncat(tmp2, _T("\\"), _countof(tmp2) - mir_tstrlen(tmp2)); mir_tstrncat(tmp2, _T("temporary_exported.asc"), _countof(tmp2) - mir_tstrlen(tmp2)); boost::filesystem::remove(tmp2); wfstream f(tmp2, std::ios::out); - f< cmd; string output; DWORD exitcode; @@ -2076,17 +1942,16 @@ static INT_PTR CALLBACK DlgProcChangePasswd(HWND hwndDlg, UINT msg, WPARAM wPara 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))) - { + if (!gpg_thread.timed_join(boost::posix_time::minutes(10))) { gpg_thread.~thread(); - if(params.child) + if (params.child) boost::process::terminate(*(params.child)); - if(bDebugLog) - debuglog<path())) - { - if((i->path().filename().generic_string().length() == 10 && (i->path().filename().generic_string().find(".") == std::string::npos)) || + for (directory_iterator i = directory_iterator(p), end = directory_iterator(); i != end; ++i) { + if (boost::filesystem::is_regular_file(i->path())) { + if ((i->path().filename().generic_string().length() == 10 && (i->path().filename().generic_string().find(".") == std::string::npos)) || i->path().extension() == ".sig" || i->path().extension() == ".asc" || i->path().extension() == ".status") boost::filesystem::remove(i->path()); } } } -} \ No newline at end of file +} -- cgit v1.2.3