From b9548a4f0c0c9315928814fb629f4e52fbebe66c Mon Sep 17 00:00:00 2001 From: Gluzskiy Alexandr Date: Tue, 24 Aug 2010 00:30:00 +0300 Subject: modified: init.cpp modified: messages.cpp modified: new_gpg.rc modified: new_gpg.vcproj modified: options.cpp modified: srmm.cpp modified: utilities.cpp --- init.cpp | 2 +- messages.cpp | 39 +++++++++++++++++++- new_gpg.rc | 5 +-- new_gpg.vcproj | 114 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- options.cpp | 103 +++++++++++++++++++++++++++++++-------------------- srmm.cpp | 9 ++--- utilities.cpp | 12 ++++++ 7 files changed, 232 insertions(+), 52 deletions(-) diff --git a/init.cpp b/init.cpp index f8fee40..7a39f1e 100644 --- a/init.cpp +++ b/init.cpp @@ -39,7 +39,7 @@ fstream debuglog; PLUGININFOEX pluginInfo={ sizeof(PLUGININFOEX), 0, - PLUGIN_MAKE_VERSION(0,0,0,3), + PLUGIN_MAKE_VERSION(0,0,0,4), "new GPG encryption support plugin, used code from http://addons.miranda-im.org/details.php?action=viewfile&id=3485", "sss", "sss123next@list.ru", diff --git a/messages.cpp b/messages.cpp index ad87255..f7e0d9c 100644 --- a/messages.cpp +++ b/messages.cpp @@ -80,10 +80,36 @@ int RecvMsgSvc(WPARAM w, LPARAM l) s2 = str.find(_T("-----END PGP MESSAGE-----")); if((s2 != wstring::npos) && (s1 != wstring::npos)) { //this is generic encrypted data block + void setSrmmIcon(HANDLE); + void setClistIcon(HANDLE); + bool isContactHaveKey(HANDLE hContact); if(!DBGetContactSettingByte(ccs->hContact, szGPGModuleName, "GPGEncryption", 0)) { if(MessageBox(0, _T("We received encrypted message from contact with encryption turned off.\nDo you want turn on encryption for this contact ?"), _T("Warning"), MB_YESNO) == IDYES) - DBWriteContactSettingByte(ccs->hContact, szGPGModuleName, "GPGEncryption", 1); + { + if(!isContactHaveKey(ccs->hContact)) + { + void ShowLoadPublicKeyDialog(); + extern map user_data; + extern int item_num; + item_num = 0; //black magic here + user_data[1] = ccs->hContact; + ShowLoadPublicKeyDialog(); + } + else + { + DBWriteContactSettingByte(ccs->hContact, szGPGModuleName, "GPGEncryption", 1); + setSrmmIcon(ccs->hContact); + setClistIcon(ccs->hContact); + return 0; + } + if(isContactHaveKey(ccs->hContact)) + { + DBWriteContactSettingByte(ccs->hContact, szGPGModuleName, "GPGEncryption", 1); + setSrmmIcon(ccs->hContact); + setClistIcon(ccs->hContact); + } + } else if(MessageBox(0, _T("Do you want try to decrypt encrypted message ?"), _T("Warning"), MB_YESNO) == IDNO) return CallService(MS_PROTO_CHAINRECV, w, l); } @@ -222,6 +248,17 @@ int RecvMsgSvc(WPARAM w, LPARAM l) } } } + if(DBGetContactSettingByte(ccs->hContact, szGPGModuleName, "GPGEncryption", 0)) + { + wchar_t *tmp = mir_utf8decodeW(msg); + wstring str = tmp; + mir_free(tmp); + mir_free((void**)pre->szMessage); + str.insert(0, _T("Received unencrypted message:\n")); + char *utf = mir_utf8encodeW(str.c_str()); + pre->szMessage = utf; + return CallService(MS_PROTO_CHAINRECV, w, (LPARAM)ccs); + } return CallService(MS_PROTO_CHAINRECV, w, l); } diff --git a/new_gpg.rc b/new_gpg.rc index 842eaf0..47eef95 100644 --- a/new_gpg.rc +++ b/new_gpg.rc @@ -255,9 +255,8 @@ FONT 8, "MS Shell Dlg", 400, 0, 0x0 BEGIN CONTROL "",IDC_USERLIST,"SysListView32",LVS_REPORT | LVS_SINGLESEL | LVS_SHOWSELALWAYS | LVS_NOSORTHEADER | WS_BORDER | WS_TABSTOP,7,17,272,112 CTEXT "Userlist:",IDC_STATIC,25,7,201,8 - PUSHBUTTON "Import PubKey",IDC_LOAD_KEY_BUTTON,7,135,54,14 - PUSHBUTTON "Export PubKey",IDC_SAVE_KEY_BUTTON,64,135,54,14 - PUSHBUTTON "Delete key",IDC_DELETE_KEY_BUTTON,121,135,50,14 + PUSHBUTTON "Export PubKey",IDC_SAVE_KEY_BUTTON,8,135,54,14 + PUSHBUTTON "Delete key",IDC_DELETE_KEY_BUTTON,65,135,50,14 PUSHBUTTON "Select own key",IDC_SELECT_KEY,222,193,57,14 CONTROL "Turn on debug log",IDC_DEBUG_LOG,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,181,159,89,10 EDITTEXT IDC_LOG_FILE_EDIT,11,157,98,14,ES_AUTOHSCROLL diff --git a/new_gpg.vcproj b/new_gpg.vcproj index f195b1a..f1fc7fa 100644 --- a/new_gpg.vcproj +++ b/new_gpg.vcproj @@ -258,7 +258,9 @@ EnableFunctionLevelLinking="true" EnableEnhancedInstructionSet="1" FloatingPointModel="0" - PrecompiledHeaderFile=".\Release Unicode (static)/testplug.pch" + UsePrecompiledHeader="2" + PrecompiledHeaderThrough="commonheaders.h" + PrecompiledHeaderFile="c:\temp\new_gpg.pch" AssemblerListingLocation=".\Release Unicode (static)/" ObjectFile=".\Release Unicode (static)/" ProgramDataBaseFileName=".\Release Unicode (static)/" @@ -627,6 +629,16 @@ UsePrecompiledHeader="2" /> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1)?tmp:_T("not set")); - mir_free(tmp); - tmp2 = UniGetContactSettingUtf(hContact, szGPGModuleName, "KeyMainName", ""); - tmp = mir_a2t(tmp2); - mir_free(tmp2); - ListView_SetItemText(hwndList, iRow, 2, (_tcslen(tmp) > 1)?tmp:_T("not set")); - mir_free(tmp); - tmp2 = UniGetContactSettingUtf(hContact, szGPGModuleName, "KeyMainEmail", ""); - tmp = mir_a2t(tmp2); - mir_free(tmp2); - ListView_SetItemText(hwndList, iRow, 3, (_tcslen(tmp) > 1)?tmp:_T("not set")); - mir_free(tmp); - if(DBGetContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", 0)) - ListView_SetItemState(hwndList, iRow, 0x2000, 0xF000); - user_data[i] = hContact; - ZeroMemory(&item,sizeof(item)); - ListView_SetColumnWidth(hwndList, 0, LVSCW_AUTOSIZE);// not sure about this - ListView_SetColumnWidth(hwndList, 1, LVSCW_AUTOSIZE); - ListView_SetColumnWidth(hwndList, 2, LVSCW_AUTOSIZE); - ListView_SetColumnWidth(hwndList, 3, LVSCW_AUTOSIZE); - ListView_SetColumnWidth(hwndList, 4, LVSCW_AUTOSIZE); + if(isContactHaveKey(hContact)) + { + TCHAR *name = (TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)hContact, GCDNF_TCHAR); + item.mask = LVIF_TEXT; + item.iItem = i; + item.iSubItem = 0; + item.pszText = name; + iRow = ListView_InsertItem(hwndList, &item); + ListView_SetItemText(hwndList, iRow, 0, name); + TCHAR *tmp = mir_a2t((char*)CallService(MS_PROTO_GETCONTACTBASEPROTO, (WPARAM)hContact, 0)); + ListView_SetItemText(hwndList, iRow, 4, tmp); + mir_free(tmp); + tmp2 = UniGetContactSettingUtf(hContact, szGPGModuleName, "KeyID", ""); + tmp = mir_a2t(tmp2); + mir_free(tmp2); + ListView_SetItemText(hwndList, iRow, 1, (_tcslen(tmp) > 1)?tmp:_T("not set")); + mir_free(tmp); + tmp2 = UniGetContactSettingUtf(hContact, szGPGModuleName, "KeyMainName", ""); + tmp = mir_a2t(tmp2); + mir_free(tmp2); + ListView_SetItemText(hwndList, iRow, 2, (_tcslen(tmp) > 1)?tmp:_T("not set")); + mir_free(tmp); + tmp2 = UniGetContactSettingUtf(hContact, szGPGModuleName, "KeyMainEmail", ""); + tmp = mir_a2t(tmp2); + mir_free(tmp2); + ListView_SetItemText(hwndList, iRow, 3, (_tcslen(tmp) > 1)?tmp:_T("not set")); + mir_free(tmp); + if(DBGetContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", 0)) + ListView_SetItemState(hwndList, iRow, 0x2000, 0xF000); + user_data[i] = hContact; + ZeroMemory(&item,sizeof(item)); + ListView_SetColumnWidth(hwndList, 0, LVSCW_AUTOSIZE);// not sure about this + ListView_SetColumnWidth(hwndList, 1, LVSCW_AUTOSIZE); + ListView_SetColumnWidth(hwndList, 2, LVSCW_AUTOSIZE); + ListView_SetColumnWidth(hwndList, 3, LVSCW_AUTOSIZE); + ListView_SetColumnWidth(hwndList, 4, LVSCW_AUTOSIZE); + } i++; } tmp = UniGetContactSettingUtf(NULL, szGPGModuleName, "szLogFilePath", _T("")); @@ -182,9 +186,6 @@ static BOOL CALLBACK DlgProcGpgOpts(HWND hwndDlg, UINT msg, WPARAM wParam, LPARA { switch (LOWORD(wParam)) { - case IDC_LOAD_KEY_BUTTON: - ShowLoadPublicKeyDialog(); - return TRUE; case IDC_DELETE_KEY_BUTTON: { //gpg execute block TCHAR tmp2[MAX_PATH] = {0}; @@ -481,6 +482,11 @@ static BOOL CALLBACK DlgProcLoadPublicKey(HWND hwndDlg,UINT msg,WPARAM wParam,LP { TranslateDialogDefault(hwndDlg); tmp = UniGetContactSettingUtf(hContact, szGPGModuleName, "GPGPubKey", _T("")); + if(!hContact) + { + EnableWindow(GetDlgItem(hwndDlg, IDC_SELECT_EXISTING), 0); + EnableWindow(GetDlgItem(hwndDlg, IDC_ENABLE_ENCRYPTION), 0); + } wstring str = tmp; mir_free(tmp); wstring::size_type p = 0, stop = 0; @@ -512,6 +518,7 @@ static BOOL CALLBACK DlgProcLoadPublicKey(HWND hwndDlg,UINT msg,WPARAM wParam,LP { case ID_OK: tmp = new TCHAR [40960]; + TCHAR *begin, *end; GetDlgItemText(hwndDlg, IDC_PUBLIC_KEY_EDIT, tmp, 40960); key_buf.append(tmp); key_buf.append(_T("\n")); //no new line at end of file ) @@ -521,12 +528,26 @@ static BOOL CALLBACK DlgProcLoadPublicKey(HWND hwndDlg,UINT msg,WPARAM wParam,LP key_buf.erase(ws1, 1); //remove windows specific trash } ws1 = 0; - if(((ws2 = key_buf.find(_T("-----END PGP PUBLIC KEY BLOCK-----"))) == wstring::npos) || ((ws1 = key_buf.find(_T("-----BEGIN PGP PUBLIC KEY BLOCK-----"))) == wstring::npos)) + if(((ws2 = key_buf.find(_T("-----END PGP PUBLIC KEY BLOCK-----"))) != wstring::npos) && ((ws1 = key_buf.find(_T("-----BEGIN PGP PUBLIC KEY BLOCK-----"))) != wstring::npos)) + { + begin = new TCHAR [_tcslen(_T("-----BEGIN PGP PUBLIC KEY BLOCK-----")) + 1]; + _tcscpy(begin, _T("-----BEGIN PGP PUBLIC KEY BLOCK-----")); + end = new TCHAR [_tcslen(_T("-----END PGP PUBLIC KEY BLOCK-----")) + 1]; + _tcscpy(end, _T("-----END PGP PUBLIC KEY BLOCK-----")); + } + else if(((ws2 = key_buf.find(_T("-----END PGP PRIVATE KEY BLOCK-----"))) != wstring::npos) && ((ws1 = key_buf.find(_T("-----BEGIN PGP PRIVATE KEY BLOCK-----"))) != wstring::npos)) + { + begin = new TCHAR [_tcslen(_T("-----BEGIN PGP PRIVATE KEY BLOCK-----")) + 1]; + _tcscpy(begin, _T("-----BEGIN PGP PRIVATE KEY BLOCK-----")); + end = new TCHAR [_tcslen(_T("-----END PGP PRIVATE KEY BLOCK-----")) + 1]; + _tcscpy(end, _T("-----END PGP PRIVATE KEY BLOCK-----")); + } + else { - MessageBox(0, _T("This is not public key"), _T("INFO"), MB_OK); + MessageBox(0, _T("This is not public or private key"), _T("INFO"), MB_OK); break; } - ws2 += _tcslen(_T("-----END PGP PUBLIC KEY BLOCK-----")); + ws2 += _tcslen(end); DBWriteContactSettingTString(hContact, szGPGModuleName, "GPGPubKey", key_buf.substr(ws1,ws2-ws1).c_str()); tmp = new TCHAR [key_buf.length()+1]; _tcscpy(tmp, key_buf.substr(ws1,ws2-ws1).c_str()); @@ -564,6 +585,8 @@ static BOOL CALLBACK DlgProcLoadPublicKey(HWND hwndDlg,UINT msg,WPARAM wParam,LP MessageBox(0, _T("Set path to gpg.exe first!"), _T("Warning"), MB_OK); break; } + mir_free(begin); + mir_free(end); cp866_to_cp1251(&output); DBDeleteContactSetting(user_data[item_num+1], szGPGModuleName, "bAlwatsTrust"); { diff --git a/srmm.cpp b/srmm.cpp index d5f6061..b9f8ea1 100644 --- a/srmm.cpp +++ b/srmm.cpp @@ -45,7 +45,7 @@ int __cdecl onIconPressed(WPARAM wParam, LPARAM lParam) { void setSrmmIcon(HANDLE); void setClistIcon(HANDLE); - TCHAR *key = UniGetContactSettingUtf(hContact, szGPGModuleName, "GPGPubKey", _T("")); + bool isContactHaveKey(HANDLE hContact); BYTE enc = DBGetContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", 0); if(enc) { @@ -55,7 +55,7 @@ int __cdecl onIconPressed(WPARAM wParam, LPARAM lParam) { } else if(!enc) { - if(_tcslen(key) < 1) + if(!isContactHaveKey(hContact)) { void ShowLoadPublicKeyDialog(); extern map user_data; @@ -69,12 +69,9 @@ int __cdecl onIconPressed(WPARAM wParam, LPARAM lParam) { DBWriteContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", 1); setSrmmIcon(hContact); setClistIcon(hContact); - mir_free(key); return 0; } - mir_free(key); - key = UniGetContactSettingUtf(hContact, szGPGModuleName, "GPGPubKey", _T("")); - if(_tcslen(key) > 0) + if(isContactHaveKey(hContact)) { DBWriteContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", 1); setSrmmIcon(hContact); diff --git a/utilities.cpp b/utilities.cpp index 6988fed..07623e1 100644 --- a/utilities.cpp +++ b/utilities.cpp @@ -500,3 +500,15 @@ bool isContactSecured(HANDLE hContact) return false; } +bool isContactHaveKey(HANDLE hContact) +{ + TCHAR *key = UniGetContactSettingUtf(hContact, szGPGModuleName, "GPGPubKey", _T("")); + if(_tcslen(key) > 0) + { + mir_free(key); + return true; + } + mir_free(key); + return false; +} + -- cgit v1.2.3