summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGluzskiy Alexandr <sss@sss.chaoslab.ru>2013-10-10 12:55:59 +0300
committerGluzskiy Alexandr <sss@sss.chaoslab.ru>2013-10-10 12:55:59 +0300
commit2db81bed3b59b2c48bac5cbd97b511e79a9f23ca (patch)
tree756d44af537dd3463e1f81cecf0cf48213ce3d5f
parent3ad5e6b61974c2d71f4afaefeab7c2ed8804176c (diff)
merged some of critical fixes from miranda ng main reponew_gpg
-rwxr-xr-xmain.cpp1119
-rwxr-xr-xmessages.cpp108
-rwxr-xr-xnew_gpg.vcxproj6
-rwxr-xr-xoptions.cpp20
-rwxr-xr-xutilities.cpp4452
5 files changed, 2823 insertions, 2882 deletions
diff --git a/main.cpp b/main.cpp
index be2fe51..aaf82d3 100755
--- a/main.cpp
+++ b/main.cpp
@@ -27,8 +27,8 @@ int itemnum = 0;
HWND hwndList_g = NULL;
BOOL CheckStateStoreDB(HWND hwndDlg, int idCtrl, const char* szSetting);
-TCHAR key_id_global[17] = {0};
-
+TCHAR key_id_global[17] = {0};
+
static INT_PTR CALLBACK DlgProcFirstRun(HWND hwndDlg,UINT msg,WPARAM wParam,LPARAM lParam)
{
HWND hwndList=GetDlgItem(hwndDlg, IDC_KEY_LIST);
@@ -44,9 +44,9 @@ static INT_PTR CALLBACK DlgProcFirstRun(HWND hwndDlg,UINT msg,WPARAM wParam,LPAR
SetWindowPos(hwndDlg, 0, firstrun_rect.left, firstrun_rect.top, 0, 0, SWP_NOSIZE|SWP_SHOWWINDOW);
TranslateDialogDefault(hwndDlg);
SetWindowText(hwndDlg, TranslateT("Set own key"));
- EnableWindow(GetDlgItem(hwndDlg, IDC_COPY_PUBKEY), 0);
- EnableWindow(GetDlgItem(hwndDlg, IDC_EXPORT_PRIVATE), 0);
- EnableWindow(GetDlgItem(hwndDlg, IDC_CHANGE_PASSWD), 0);
+ EnableWindow(GetDlgItem(hwndDlg, IDC_COPY_PUBKEY), 0);
+ EnableWindow(GetDlgItem(hwndDlg, IDC_EXPORT_PRIVATE), 0);
+ EnableWindow(GetDlgItem(hwndDlg, IDC_CHANGE_PASSWD), 0);
col.pszText = _T("Key ID");
col.mask = LVCF_TEXT | LVCF_WIDTH;
col.fmt = LVCFMT_LEFT;
@@ -71,23 +71,23 @@ static INT_PTR CALLBACK DlgProcFirstRun(HWND hwndDlg,UINT msg,WPARAM wParam,LPAR
col.cx = 30;
ListView_InsertColumn(hwndList, 3, &col);
ZeroMemory(&col,sizeof(col));
- col.pszText = TranslateT("Expire date");
+ col.pszText = TranslateT("Expire date");
col.mask = LVCF_TEXT | LVCF_WIDTH;
col.fmt = LVCFMT_LEFT;
col.cx = 30;
ListView_InsertColumn(hwndList, 4, &col);
- ZeroMemory(&col,sizeof(col));
- col.pszText = TranslateT("Key length");
- col.mask = LVCF_TEXT | LVCF_WIDTH;
- col.fmt = LVCFMT_LEFT;
- col.cx = 30;
- ListView_InsertColumn(hwndList, 5, &col);
- ZeroMemory(&col,sizeof(col));
- col.pszText = TranslateT("Accounts");
- col.mask = LVCF_TEXT | LVCF_WIDTH;
- col.fmt = LVCFMT_LEFT;
- col.cx = 30;
- ListView_InsertColumn(hwndList, 6, &col);
+ ZeroMemory(&col,sizeof(col));
+ col.pszText = TranslateT("Key length");
+ col.mask = LVCF_TEXT | LVCF_WIDTH;
+ col.fmt = LVCFMT_LEFT;
+ col.cx = 30;
+ ListView_InsertColumn(hwndList, 5, &col);
+ ZeroMemory(&col,sizeof(col));
+ col.pszText = TranslateT("Accounts");
+ col.mask = LVCF_TEXT | LVCF_WIDTH;
+ col.fmt = LVCFMT_LEFT;
+ col.cx = 30;
+ ListView_InsertColumn(hwndList, 6, &col);
ListView_SetExtendedListViewStyleEx(hwndList, 0, LVS_EX_FULLROWSELECT);
int i = 1, iRow = 0;
{
@@ -101,14 +101,14 @@ static INT_PTR CALLBACK DlgProcFirstRun(HWND hwndDlg,UINT msg,WPARAM wParam,LPAR
pxResult result;
wstring::size_type p = 0, p2 = 0, stop = 0;
{
- std::vector<wstring> cmd;
- cmd.push_back(L"--batch");
- cmd.push_back(L"--list-secret-keys");
- gpg_execution_params params(cmd);
+ std::vector<wstring> cmd;
+ cmd.push_back(L"--batch");
+ cmd.push_back(L"--list-secret-keys");
+ gpg_execution_params params(cmd);
params.out = &out;
params.code = &code;
params.result = &result;
- if(!gpg_launcher(params))
+ if(!gpg_launcher(params))
{
break;
}
@@ -124,85 +124,85 @@ static INT_PTR CALLBACK DlgProcFirstRun(HWND hwndDlg,UINT msg,WPARAM wParam,LPAR
break;
stop = p;
p2 = out.find("/", p) - 1;
- TCHAR *key_len = mir_wstrdup(toUTF16(out.substr(p,p2-p)).c_str()), *creation_date = NULL, *expire_date = NULL;
+ TCHAR *key_len = mir_wstrdup(toUTF16(out.substr(p,p2-p)).c_str()), *creation_date = NULL, *expire_date = NULL;
p2+=2;
p = out.find(" ", p2);
- std::wstring key_id = toUTF16(out.substr(p2,p-p2));
- p += 1;
- p2 = out.find(" ", p);
- std::string::size_type p3 = out.find("\n", p);
- if((p2 != std::string::npos) && (p3 < p2))
- {
- p2 = p3;
- creation_date = mir_wstrdup(toUTF16(out.substr(p,p2-p-1)).c_str());
- }
- else
- {
- creation_date = mir_wstrdup(toUTF16(out.substr(p,p2-p)).c_str());
- p2 = out.find("[", p2);
- p2 = out.find("expires:", p2);
- p2 += strlen("expires:");
- if(p2 != std::string::npos)
- {
- p2++;
- p = p2;
- p2 = out.find("]", p);
- expire_date = mir_wstrdup(toUTF16(out.substr(p,p2-p)).c_str());
- //check expiration
- bool expired = false;
- {
- boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();
- TCHAR buf[5];
- mir_sntprintf(buf, 5, _T("%s"), expire_date);
- int year = _ttoi(buf);
- if(year < now.date().year())
- expired = true;
- else if(year == now.date().year())
- {
- mir_sntprintf(buf, 3, _T("%s"), expire_date+5);
- int month = _ttoi(buf);
- if(month < now.date().month())
- expired = true;
- else if(month == now.date().month())
- {
- mir_sntprintf(buf, 3, _T("%s"), expire_date+8);
- unsigned day = _ttoi(buf);
- if(day <= now.date().day_number())
- expired = true;
- }
- }
- }
- if(expired)
- {
- mir_free(key_len);
- mir_free(creation_date);
- mir_free(expire_date);
- //mimic normal behaviour
- p = out.find("uid ", p);
- p2 = out.find_first_not_of(" ", p+5);
- p = out.find("<", p2);
- p++;
- p2 = out.find(">", p);
- //
- continue; //does not add to key list
- }
- }
- }
- iRow = ListView_InsertItem(hwndList, &item);
- ListView_SetItemText(hwndList, iRow, 3, creation_date);
- mir_free(creation_date);
- if(expire_date)
- {
- ListView_SetItemText(hwndList, iRow, 4, expire_date);
- mir_free(expire_date);
- }
- ListView_SetItemText(hwndList, iRow, 5, key_len);
- mir_free(key_len);
- ListView_SetItemText(hwndList, iRow, 0, (TCHAR*)key_id.c_str());
+ std::wstring key_id = toUTF16(out.substr(p2,p-p2));
+ p += 1;
+ p2 = out.find(" ", p);
+ std::string::size_type p3 = out.find("\n", p);
+ if((p2 != std::string::npos) && (p3 < p2))
+ {
+ p2 = p3;
+ creation_date = mir_wstrdup(toUTF16(out.substr(p,p2-p-1)).c_str());
+ }
+ else
+ {
+ creation_date = mir_wstrdup(toUTF16(out.substr(p,p2-p)).c_str());
+ p2 = out.find("[", p2);
+ p2 = out.find("expires:", p2);
+ p2 += strlen("expires:");
+ if(p2 != std::string::npos)
+ {
+ p2++;
+ p = p2;
+ p2 = out.find("]", p);
+ expire_date = mir_wstrdup(toUTF16(out.substr(p,p2-p)).c_str());
+ //check expiration
+ bool expired = false;
+ {
+ boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();
+ TCHAR buf[5];
+ mir_sntprintf(buf, 5, _T("%s"), expire_date);
+ int year = _ttoi(buf);
+ if(year < now.date().year())
+ expired = true;
+ else if(year == now.date().year())
+ {
+ mir_sntprintf(buf, 3, _T("%s"), expire_date+5);
+ int month = _ttoi(buf);
+ if(month < now.date().month())
+ expired = true;
+ else if(month == now.date().month())
+ {
+ mir_sntprintf(buf, 3, _T("%s"), expire_date+8);
+ unsigned day = _ttoi(buf);
+ if(day <= now.date().day_number())
+ expired = true;
+ }
+ }
+ }
+ if(expired)
+ {
+ mir_free(key_len);
+ mir_free(creation_date);
+ mir_free(expire_date);
+ //mimic normal behaviour
+ p = out.find("uid ", p);
+ p2 = out.find_first_not_of(" ", p+5);
+ p = out.find("<", p2);
+ p++;
+ p2 = out.find(">", p);
+ //
+ continue; //does not add to key list
+ }
+ }
+ }
+ iRow = ListView_InsertItem(hwndList, &item);
+ ListView_SetItemText(hwndList, iRow, 3, creation_date);
+ mir_free(creation_date);
+ if(expire_date)
+ {
+ ListView_SetItemText(hwndList, iRow, 4, expire_date);
+ mir_free(expire_date);
+ }
+ ListView_SetItemText(hwndList, iRow, 5, key_len);
+ mir_free(key_len);
+ ListView_SetItemText(hwndList, iRow, 0, (TCHAR*)key_id.c_str());
p = out.find("uid ", p);
p2 = out.find_first_not_of(" ", p+5);
p = out.find("<", p2);
- TCHAR *tmp = mir_wstrdup(toUTF16(out.substr(p2,p-p2)).c_str());
+ TCHAR *tmp = mir_wstrdup(toUTF16(out.substr(p2,p-p2)).c_str());
ListView_SetItemText(hwndList, iRow, 2, tmp);
mir_free(tmp);
p++;
@@ -210,41 +210,42 @@ static INT_PTR CALLBACK DlgProcFirstRun(HWND hwndDlg,UINT msg,WPARAM wParam,LPAR
tmp = mir_wstrdup(toUTF16(out.substr(p,p2-p)).c_str());
ListView_SetItemText(hwndList, iRow, 1, tmp);
mir_free(tmp);
- { //get accounts
- int count = 0;
- PROTOACCOUNT **accounts;
- ProtoEnumAccounts(&count, &accounts);
- std::wstring accs;
- for(int i = 0; i < count; i++)
- {
- std::string setting = toUTF8(accounts[i]->tszAccountName);
- setting += "(";
- setting += accounts[i]->szModuleName;
- setting += ")" ;
- setting += "_KeyID";
- TCHAR *str = UniGetContactSettingUtf(NULL, szGPGModuleName, setting.c_str(), _T(""));
- if(key_id == str)
- {
- if(accs.empty())
- accs += accounts[i]->tszAccountName;
- else
- {
- accs += _T(",");
- accs += accounts[i]->tszAccountName;
- }
- }
- }
- ListView_SetItemText(hwndList, iRow, 6, (TCHAR*)accs.c_str());
- }
+ { //get accounts
+ int count = 0;
+ PROTOACCOUNT **accounts;
+ ProtoEnumAccounts(&count, &accounts);
+ std::wstring accs;
+ for(int i = 0; i < count; i++)
+ {
+ std::string setting = toUTF8(accounts[i]->tszAccountName);
+ setting += "(";
+ setting += accounts[i]->szModuleName;
+ setting += ")" ;
+ setting += "_KeyID";
+ TCHAR *str = UniGetContactSettingUtf(NULL, szGPGModuleName, setting.c_str(), _T(""));
+ if(key_id == str)
+ {
+ if(accs.empty())
+ accs += accounts[i]->tszAccountName;
+ else
+ {
+ accs += _T(",");
+ accs += accounts[i]->tszAccountName;
+ }
+ }
+ mir_free(str);
+ }
+ ListView_SetItemText(hwndList, iRow, 6, (TCHAR*)accs.c_str());
+ }
i++;
}
- ListView_SetColumnWidth(hwndList, 0, LVSCW_AUTOSIZE);
- ListView_SetColumnWidth(hwndList, 1, LVSCW_AUTOSIZE);
- ListView_SetColumnWidth(hwndList, 2, LVSCW_AUTOSIZE);
- ListView_SetColumnWidth(hwndList, 3, LVSCW_AUTOSIZE);
- ListView_SetColumnWidth(hwndList, 4, LVSCW_AUTOSIZE);
- ListView_SetColumnWidth(hwndList, 5, LVSCW_AUTOSIZE);
- ListView_SetColumnWidth(hwndList, 6, LVSCW_AUTOSIZE);
+ ListView_SetColumnWidth(hwndList, 0, LVSCW_AUTOSIZE);
+ ListView_SetColumnWidth(hwndList, 1, LVSCW_AUTOSIZE);
+ ListView_SetColumnWidth(hwndList, 2, LVSCW_AUTOSIZE);
+ ListView_SetColumnWidth(hwndList, 3, LVSCW_AUTOSIZE);
+ ListView_SetColumnWidth(hwndList, 4, LVSCW_AUTOSIZE);
+ ListView_SetColumnWidth(hwndList, 5, LVSCW_AUTOSIZE);
+ ListView_SetColumnWidth(hwndList, 6, LVSCW_AUTOSIZE);
}
}
{
@@ -300,24 +301,24 @@ static INT_PTR CALLBACK DlgProcFirstRun(HWND hwndDlg,UINT msg,WPARAM wParam,LPAR
}
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(fp);
- gpg_execution_params params(cmd);
+ std::vector<wstring> cmd;
+ cmd.push_back(L"--batch");
+ cmd.push_back(L"-a");
+ cmd.push_back(L"--export");
+ cmd.push_back(fp);
+ gpg_execution_params params(cmd);
pxResult result;
params.out = &out;
params.code = &code;
params.result = &result;
- if(!gpg_launcher(params))
+ if(!gpg_launcher(params))
{
break;
}
if(result == pxNotFound)
break;
string::size_type s = 0;
- boost::algorithm::erase_all(out, "\r");
+ boost::algorithm::erase_all(out, "\r");
{
char buf[64];
GetDlgItemTextA(hwndDlg, IDC_ACCOUNT, buf, 63);
@@ -386,15 +387,15 @@ static INT_PTR CALLBACK DlgProcFirstRun(HWND hwndDlg,UINT msg,WPARAM wParam,LPAR
DWORD code;
wstring::size_type p = 0, p2 = 0, stop = 0;
{
- std::vector<wstring> cmd;
- cmd.push_back(L"--batch");
- cmd.push_back(L"--list-secret-keys");
- gpg_execution_params params(cmd);
+ std::vector<wstring> cmd;
+ cmd.push_back(L"--batch");
+ cmd.push_back(L"--list-secret-keys");
+ gpg_execution_params params(cmd);
pxResult result;
params.out = &out;
params.code = &code;
params.result = &result;
- if(!gpg_launcher(params))
+ if(!gpg_launcher(params))
{
break;
}
@@ -453,16 +454,16 @@ static INT_PTR CALLBACK DlgProcFirstRun(HWND hwndDlg,UINT msg,WPARAM wParam,LPAR
{
string out;
DWORD code;
- std::vector<wstring> cmd;
- cmd.push_back(L"--batch");
- cmd.push_back(L"--fingerprint");
- cmd.push_back(fp);
- gpg_execution_params params(cmd);
+ std::vector<wstring> cmd;
+ cmd.push_back(L"--batch");
+ cmd.push_back(L"--fingerprint");
+ cmd.push_back(fp);
+ gpg_execution_params params(cmd);
pxResult result;
params.out = &out;
params.code = &code;
params.result = &result;
- if(!gpg_launcher(params))
+ if(!gpg_launcher(params))
{
break;
}
@@ -483,12 +484,12 @@ static INT_PTR CALLBACK DlgProcFirstRun(HWND hwndDlg,UINT msg,WPARAM wParam,LPAR
}
cmd.clear();
out.clear();
- cmd.push_back(L"--batch");
- cmd.push_back(L"--delete-secret-and-public-key");
- cmd.push_back(L"--fingerprint");
- cmd.push_back(fp);
+ cmd.push_back(L"--batch");
+ cmd.push_back(L"--delete-secret-and-public-key");
+ cmd.push_back(L"--fingerprint");
+ cmd.push_back(fp);
mir_free(fp);
- if(!gpg_launcher(params))
+ if(!gpg_launcher(params))
{
break;
}
@@ -566,32 +567,32 @@ static INT_PTR CALLBACK DlgProcFirstRun(HWND hwndDlg,UINT msg,WPARAM wParam,LPAR
{ //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);
+ 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;
- extern HFONT bold_font;
- SendMessage(GetDlgItem(hwndDlg, IDC_GENERATING_KEY), WM_SETFONT, (WPARAM)bold_font, true);
+ extern HFONT bold_font;
+ SendMessage(GetDlgItem(hwndDlg, IDC_GENERATING_KEY), WM_SETFONT, (WPARAM)bold_font, true);
SetWindowTextA(GetDlgItem(hwndDlg, IDC_GENERATING_KEY), Translate("Generating new random key, please wait"));
EnableWindow(GetDlgItem(hwndDlg, IDC_GENERATE_KEY), 0);
EnableWindow(GetDlgItem(hwndDlg, IDC_OTHER), 0);
EnableWindow(GetDlgItem(hwndDlg, IDC_DELETE_KEY), 0);
EnableWindow(GetDlgItem(hwndDlg, IDC_KEY_LIST), 0);
EnableWindow(GetDlgItem(hwndDlg, IDC_GENERATE_RANDOM), 0);
- if(!gpg_launcher(params, boost::posix_time::minutes(10)))
+ if(!gpg_launcher(params, boost::posix_time::minutes(10)))
{
break;
}
if(result == pxNotFound)
break;
- boost::filesystem::remove(path);
+ boost::filesystem::remove(path);
string::size_type p1 = 0;
if((p1 = out.find("key ")) != string::npos)
path = toUTF16(out.substr(p1+4, 8));
@@ -602,17 +603,17 @@ static INT_PTR CALLBACK DlgProcFirstRun(HWND hwndDlg,UINT msg,WPARAM wParam,LPAR
{
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);
+ 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))
+ if(!gpg_launcher(params))
{
break;
}
@@ -673,30 +674,30 @@ static INT_PTR CALLBACK DlgProcFirstRun(HWND hwndDlg,UINT msg,WPARAM wParam,LPAR
}
}
break;
- case IDC_COPY_PUBKEY:
- {
- if(OpenClipboard(hwndDlg))
- {
- ListView_GetItemText(hwndList, itemnum, 0, fp, 16);
- 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(fp);
- gpg_execution_params params(cmd);
- pxResult result;
- params.out = &out;
- params.code = &code;
- params.result = &result;
- if(!gpg_launcher(params))
- {
- break;
- }
- if(result == pxNotFound)
- break;
- boost::algorithm::erase_all(out, "\r");
+ case IDC_COPY_PUBKEY:
+ {
+ if(OpenClipboard(hwndDlg))
+ {
+ ListView_GetItemText(hwndList, itemnum, 0, fp, 16);
+ 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(fp);
+ gpg_execution_params params(cmd);
+ pxResult result;
+ params.out = &out;
+ params.code = &code;
+ params.result = &result;
+ if(!gpg_launcher(params))
+ {
+ break;
+ }
+ if(result == pxNotFound)
+ break;
+ boost::algorithm::erase_all(out, "\r");
HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, out.size() +1);
if(!hMem)
{
@@ -707,7 +708,7 @@ static INT_PTR CALLBACK DlgProcFirstRun(HWND hwndDlg,UINT msg,WPARAM wParam,LPAR
if(!szKey)
{
char msg[64];
- mir_snprintf(msg, 127, "Failed to lock memory with error %d", GetLastError());
+ mir_snprintf(msg, 64, "Failed to lock memory with error %d", GetLastError());
MessageBoxA(0, msg, "Error", MB_OK);
GlobalFree(hMem);
}
@@ -719,87 +720,87 @@ static INT_PTR CALLBACK DlgProcFirstRun(HWND hwndDlg,UINT msg,WPARAM wParam,LPAR
{
GlobalFree(hMem);
char msg[64];
- mir_snprintf(msg, 127, "Failed write to clipboard with error %d", GetLastError());
+ mir_snprintf(msg, 64, "Failed write to clipboard with error %d", GetLastError());
MessageBoxA(0, msg, "Error", MB_OK);
}
CloseClipboard();
- }
- }
- break;
- case IDC_EXPORT_PRIVATE:
- {
- TCHAR *p = GetFilePath(_T("Choose file to export key"), _T("*"), _T("Any file"), true);
- if(!p || !p[0])
- {
- delete [] p;
- //TODO: handle error
- break;
- }
- char *path = mir_t2a(p);
- delete [] p;
- std::ofstream file;
- file.open(path, std::ios::trunc | std::ios::out);
- mir_free(path);
- if(!file.is_open())
- break; //TODO: handle error
- ListView_GetItemText(hwndList, itemnum, 0, fp, 16);
- string out;
- DWORD code;
- std::vector<wstring> cmd;
- cmd.push_back(L"--batch");
- cmd.push_back(L"-a");
- cmd.push_back(L"--export-secret-keys");
- cmd.push_back(fp);
- gpg_execution_params params(cmd);
- pxResult result;
- params.out = &out;
- params.code = &code;
- params.result = &result;
- if(!gpg_launcher(params))
- {
- break;
- }
- if(result == pxNotFound)
- break;
- boost::algorithm::erase_all(out, "\r");
- file<<out;
- if(file.is_open())
- file.close();
- }
- break;
- case IDC_CHANGE_PASSWD:
- ListView_GetItemText(hwndList, itemnum, 0, key_id_global, 16);
-// extern void ShowChangePasswdDlg();
-// ShowChangePasswdDlg();
- //temporary code follows
- std::vector<std::wstring> cmd;
- std::string old_pass, new_pass;
- TCHAR tmp2[MAX_PATH] = {0};
- string output;
- DWORD exitcode;
- cmd.push_back(L"--edit-key");
- cmd.push_back(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, &params));
- if(!gpg_thread.timed_join(boost::posix_time::minutes(10)))
- {
- gpg_thread.~thread();
- if(params.child)
- boost::process::terminate(*(params.child));
- if(bDebugLog)
- debuglog<<std::string(time_str()+": GPG execution timed out, aborted");
- DestroyWindow(hwndDlg);
- break;
- }
- if(result == pxNotFound)
- break;
- //
- break;
+ }
+ }
+ break;
+ case IDC_EXPORT_PRIVATE:
+ {
+ TCHAR *p = GetFilePath(_T("Choose file to export key"), _T("*"), _T("Any file"), true);
+ if(!p || !p[0])
+ {
+ delete [] p;
+ //TODO: handle error
+ break;
+ }
+ char *path = mir_t2a(p);
+ delete [] p;
+ std::ofstream file;
+ file.open(path, std::ios::trunc | std::ios::out);
+ mir_free(path);
+ if(!file.is_open())
+ break; //TODO: handle error
+ ListView_GetItemText(hwndList, itemnum, 0, fp, 16);
+ string out;
+ DWORD code;
+ std::vector<wstring> cmd;
+ cmd.push_back(L"--batch");
+ cmd.push_back(L"-a");
+ cmd.push_back(L"--export-secret-keys");
+ cmd.push_back(fp);
+ gpg_execution_params params(cmd);
+ pxResult result;
+ params.out = &out;
+ params.code = &code;
+ params.result = &result;
+ if(!gpg_launcher(params))
+ {
+ break;
+ }
+ if(result == pxNotFound)
+ break;
+ boost::algorithm::erase_all(out, "\r");
+ file<<out;
+ if(file.is_open())
+ file.close();
+ }
+ break;
+ case IDC_CHANGE_PASSWD:
+ ListView_GetItemText(hwndList, itemnum, 0, key_id_global, 16);
+// extern void ShowChangePasswdDlg();
+// ShowChangePasswdDlg();
+ //temporary code follows
+ std::vector<std::wstring> cmd;
+ std::string old_pass, new_pass;
+ TCHAR tmp2[MAX_PATH] = {0};
+ string output;
+ DWORD exitcode;
+ cmd.push_back(L"--edit-key");
+ cmd.push_back(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, &params));
+ if(!gpg_thread.timed_join(boost::posix_time::minutes(10)))
+ {
+ gpg_thread.~thread();
+ if(params.child)
+ boost::process::terminate(*(params.child));
+ if(bDebugLog)
+ debuglog<<std::string(time_str()+": GPG execution timed out, aborted");
+ DestroyWindow(hwndDlg);
+ break;
+ }
+ if(result == pxNotFound)
+ break;
+ //
+ break;
}
break;
}
@@ -811,9 +812,9 @@ static INT_PTR CALLBACK DlgProcFirstRun(HWND hwndDlg,UINT msg,WPARAM wParam,LPAR
if(hdr->hdr.code == NM_CLICK)
{
EnableWindow(GetDlgItem(hwndDlg, ID_OK), 1);
- EnableWindow(GetDlgItem(hwndDlg, IDC_COPY_PUBKEY), 1);
- EnableWindow(GetDlgItem(hwndDlg, IDC_EXPORT_PRIVATE), 1);
- EnableWindow(GetDlgItem(hwndDlg, IDC_CHANGE_PASSWD), 1);
+ EnableWindow(GetDlgItem(hwndDlg, IDC_COPY_PUBKEY), 1);
+ EnableWindow(GetDlgItem(hwndDlg, IDC_EXPORT_PRIVATE), 1);
+ EnableWindow(GetDlgItem(hwndDlg, IDC_CHANGE_PASSWD), 1);
itemnum = hdr->iItem;
}
}
@@ -856,30 +857,30 @@ static INT_PTR CALLBACK DlgProcGpgBinOpts(HWND hwndDlg, UINT msg, WPARAM wParam,
case WM_INITDIALOG:
{
TranslateDialogDefault(hwndDlg);
- TCHAR *path = (TCHAR*)mir_alloc(sizeof(TCHAR) * MAX_PATH);
+ TCHAR *path = (TCHAR*)mir_alloc(sizeof(TCHAR) * MAX_PATH);
bool gpg_exists = false, lang_exists = false;
{
- char *mir_path = (char*)mir_alloc(sizeof(char) * MAX_PATH);
+ char *mir_path = (char*)mir_alloc(sizeof(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 = (TCHAR*)mir_alloc(sizeof(TCHAR) * MAX_PATH), *gpg_lang_path = (TCHAR*)mir_alloc(sizeof(TCHAR) * MAX_PATH);
+ TCHAR *gpg_path = (TCHAR*)mir_alloc(sizeof(TCHAR) * MAX_PATH), *gpg_lang_path = (TCHAR*)mir_alloc(sizeof(TCHAR) * MAX_PATH);
_tcscpy(gpg_path, tmp);
_tcscat(gpg_path, _T("\\GnuPG\\gpg.exe"));
_tcscpy(gpg_lang_path, tmp);
_tcscat(gpg_lang_path, _T("\\GnuPG\\gnupg.nls\\en@quot.mo"));
mir_free(tmp);
- if(boost::filesystem::exists(gpg_path))
+ if(boost::filesystem::exists(gpg_path))
{
gpg_exists = true;
_tcscpy(path, _T("GnuPG\\gpg.exe"));
}
- if(boost::filesystem::exists(gpg_lang_path))
+ if(boost::filesystem::exists(gpg_lang_path))
lang_exists = true;
- if(gpg_exists && !lang_exists)
- MessageBox(0, TranslateT("gpg binary found in miranda folder, but english locale does not exists.\nit's highly recommended to place \\gnupg.nls\\en@quot.mo in gnupg folder under miranda root.\nwithout this file you may expirense many problem with gpg output on non english systems.\nand plugin may completely do not work.\nyou have beed warned."), TranslateT("Warning"), MB_OK);
+ if(gpg_exists && !lang_exists)
+ MessageBox(0, TranslateT("GPG binary found in Miranda folder, but English locale does not exist.\nIt's highly recommended to place \\gnupg.nls\\en@quot.mo in GnuPG folder under Miranda root.\nWithout this file you may experience many problems with GPG output on non-English systems\nand plugin may completely not work.\nYou have been warned."), TranslateT("Warning"), MB_OK);
mir_free(gpg_path);
mir_free(gpg_lang_path);
}
@@ -888,28 +889,29 @@ static INT_PTR CALLBACK DlgProcGpgBinOpts(HWND hwndDlg, UINT msg, WPARAM wParam,
{
tmp = UniGetContactSettingUtf(NULL, szGPGModuleName, "szGpgBinPath", (SHGetValue(HKEY_CURRENT_USER, _T("Software\\GNU\\GnuPG"), _T("gpgProgram"), 0, path, &len) == ERROR_SUCCESS)?path:_T(""));
if(tmp[0])
- if(!boost::filesystem::exists(tmp))
- MessageBox(0, TranslateT("wrong gpg binary location found in system.\nplease choose another location"), TranslateT("Warning"), MB_OK);
+ if(!boost::filesystem::exists(tmp))
+ MessageBox(0, TranslateT("Wrong GPG binary location found in system.\nPlease choose another location"), TranslateT("Warning"), MB_OK);
}
- else tmp = mir_wstrdup(path);
- mir_free(path);
+ else
+ tmp = mir_wstrdup(path);
+ mir_free(path);
SetDlgItemText(hwndDlg, IDC_BIN_PATH, tmp);
bool bad_version = false;
- if(gpg_exists/* && lang_exists*/)
+ if(gpg_exists/* && lang_exists*/)
{
DBWriteContactSettingTString(NULL, szGPGModuleName, "szGpgBinPath", tmp);
string out;
DWORD code;
- std::vector<wstring> cmd;
- cmd.push_back(L"--version");
- gpg_execution_params params(cmd);
+ std::vector<wstring> cmd;
+ cmd.push_back(L"--version");
+ gpg_execution_params params(cmd);
pxResult result;
params.out = &out;
params.code = &code;
params.result = &result;
gpg_valid = true;
- gpg_launcher(params);
+ gpg_launcher(params);
gpg_valid = false;
DBDeleteContactSetting(NULL, szGPGModuleName, "szGpgBinPath");
string::size_type p1 = out.find("(GnuPG) ");
@@ -922,36 +924,36 @@ static INT_PTR CALLBACK DlgProcGpgBinOpts(HWND hwndDlg, UINT msg, WPARAM wParam,
else
{
bad_version = false;
- MessageBox(0, TranslateT("This is not gnupg binary !\nrecommended to use GnuPG v1.x.x with this plugn."), TranslateT("Error"), MB_OK);
+ MessageBox(0, TranslateT("This is not GnuPG binary!\nIt is recommended to use GnuPG v1.x.x with this plugin."), TranslateT("Error"), MB_OK);
}
if(bad_version)
- MessageBox(0, TranslateT("Unsupported gnupg version found, use at you own risk!\nrecommended to use GnuPG v1.x.x with this plugn."), TranslateT("Warning"), MB_OK);
+ MessageBox(0, TranslateT("Unsupported GnuPG version found, use at you own risk!\nIt is recommended to use GnuPG v1.x.x with this plugin."), TranslateT("Warning"), MB_OK);
}
mir_free(tmp);
{
- tmp = UniGetContactSettingUtf(NULL, szGPGModuleName, "szHomePath", _T(""));
- if(!tmp[0])
- {
- mir_free(tmp);
- char *mir_path = (char*)mir_alloc(sizeof(char) * MAX_PATH);
- CallService(MS_UTILS_PATHTOABSOLUTE, (WPARAM)"\\", (LPARAM)mir_path);
- strcat(mir_path, "\\gpg");
- if(_access(mir_path, 0) != -1)
- {
- tmp = mir_wstrdup(toUTF16(mir_path).c_str());
- MessageBox(0, TranslateT("found \"gpg\" directory in MIranda root.\nassuming it's gpg home directory.\ngpg home directory set."), TranslateT("Info"), MB_OK);
- }
- else
- {
- wstring path_ = _wgetenv(_T("APPDATA"));
- path_ += _T("\\GnuPG");
- tmp = mir_wstrdup(path_.c_str());
- }
- }
- SetDlgItemText(hwndDlg, IDC_HOME_DIR, !gpg_exists?tmp:_T("gpg"));
- mir_free(tmp);
+ tmp = UniGetContactSettingUtf(NULL, szGPGModuleName, "szHomePath", _T(""));
+ if(!tmp[0])
+ {
+ mir_free(tmp);
+ char *mir_path = (char*)mir_alloc(sizeof(char) * MAX_PATH);
+ CallService(MS_UTILS_PATHTOABSOLUTE, (WPARAM)"\\", (LPARAM)mir_path);
+ strcat(mir_path, "\\gpg");
+ if(_access(mir_path, 0) != -1)
+ {
+ tmp = mir_wstrdup(toUTF16(mir_path).c_str());
+ MessageBox(0, TranslateT("\"GPG\" directory found in Miranda root.\nAssuming it's GPG home directory.\nGPG home directory set."), TranslateT("Info"), MB_OK);
+ }
+ else
+ {
+ wstring path_ = _wgetenv(_T("APPDATA"));
+ path_ += _T("\\GnuPG");
+ tmp = mir_wstrdup(path_.c_str());
+ }
+ }
+ SetDlgItemText(hwndDlg, IDC_HOME_DIR, !gpg_exists?tmp:_T("gpg"));
+ mir_free(tmp);
}
- //TODO: additional check for write access
+ //TODO: additional check for write access
if(gpg_exists && lang_exists && !bad_version)
MessageBox(0, TranslateT("Your GPG version is supported. The language file was found.\nGPG plugin should work fine.\nPress OK to continue."), TranslateT("Info"), MB_OK);
extern bool bIsMiranda09;
@@ -1010,10 +1012,10 @@ static INT_PTR CALLBACK DlgProcGpgBinOpts(HWND hwndDlg, UINT msg, WPARAM wParam,
CallService(MS_UTILS_PATHTOABSOLUTE, (WPARAM)"\\", (LPARAM)mir_path);
SetCurrentDirectoryA(mir_path);
delete [] mir_path;
- if(!boost::filesystem::exists(tmp))
+ if(!boost::filesystem::exists(tmp))
{
- MessageBox(0, TranslateT("gpg binary does not exists.\nplease choose another location"), TranslateT("Warning"), MB_OK);
- break;
+ MessageBox(0, TranslateT("gpg binary does not exists.\nplease choose another location"), TranslateT("Warning"), MB_OK);
+ break;
}
}
else
@@ -1026,15 +1028,15 @@ static INT_PTR CALLBACK DlgProcGpgBinOpts(HWND hwndDlg, UINT msg, WPARAM wParam,
DBWriteContactSettingTString(NULL, szGPGModuleName, "szGpgBinPath", tmp);
string out;
DWORD code;
- std::vector<wstring> cmd;
- cmd.push_back(L"--version");
- gpg_execution_params params(cmd);
+ std::vector<wstring> cmd;
+ cmd.push_back(L"--version");
+ gpg_execution_params params(cmd);
pxResult result;
params.out = &out;
params.code = &code;
params.result = &result;
gpg_valid = true;
- gpg_launcher(params);
+ gpg_launcher(params);
gpg_valid = false;
DBDeleteContactSetting(NULL, szGPGModuleName, "szGpgBinPath");
string::size_type p1 = out.find("(GnuPG) ");
@@ -1047,10 +1049,10 @@ static INT_PTR CALLBACK DlgProcGpgBinOpts(HWND hwndDlg, UINT msg, WPARAM wParam,
else
{
bad_version = false;
- MessageBox(0, TranslateT("This is not gnupg binary !\nrecommended to use GnuPG v1.x.x with this plugn."), TranslateT("Warning"), MB_OK);
+ MessageBox(0, TranslateT("This is not GnuPG binary!\nIt is recommended to use GnuPG v1.x.x with this plugin."), TranslateT("Warning"), MB_OK);
}
if(bad_version)
- MessageBox(0, TranslateT("Unsupported gnupg version found, use at you own risk!\nrecommended to use GnuPG v1.x.x with this plugn."), TranslateT("Warning"), MB_OK);
+ MessageBox(0, TranslateT("Unsupported GnuPG version found, use at you own risk!\nIt is recommended to use GnuPG v1.x.x with this plugin."), TranslateT("Warning"), MB_OK);
}
DBWriteContactSettingTString(NULL, szGPGModuleName, "szGpgBinPath", tmp);
GetDlgItemText(hwndDlg, IDC_HOME_DIR, tmp, 512);
@@ -1088,10 +1090,10 @@ static INT_PTR CALLBACK DlgProcGpgBinOpts(HWND hwndDlg, UINT msg, WPARAM wParam,
CallService(MS_UTILS_PATHTOABSOLUTE, (WPARAM)"\\", (LPARAM)mir_path);
SetCurrentDirectoryA(mir_path);
delete [] mir_path;
- if(!boost::filesystem::exists(tmp))
+ if(!boost::filesystem::exists(tmp))
{
- MessageBox(0, TranslateT("gpg binary does not exists.\nplease choose another location"), TranslateT("Warning"), MB_OK);
- break;
+ MessageBox(0, TranslateT("GPG binary does not exist.\nPlease choose another location"), TranslateT("Warning"), MB_OK);
+ break;
}
}
else
@@ -1104,15 +1106,15 @@ static INT_PTR CALLBACK DlgProcGpgBinOpts(HWND hwndDlg, UINT msg, WPARAM wParam,
DBWriteContactSettingTString(NULL, szGPGModuleName, "szGpgBinPath", tmp);
string out;
DWORD code;
- std::vector<wstring> cmd;
- cmd.push_back(L"--version");
- gpg_execution_params params(cmd);
+ std::vector<wstring> cmd;
+ cmd.push_back(L"--version");
+ gpg_execution_params params(cmd);
pxResult result;
params.out = &out;
params.code = &code;
params.result = &result;
gpg_valid = true;
- gpg_launcher(params);
+ gpg_launcher(params);
gpg_valid = false;
DBDeleteContactSetting(NULL, szGPGModuleName, "szGpgBinPath");
string::size_type p1 = out.find("(GnuPG) ");
@@ -1125,10 +1127,10 @@ static INT_PTR CALLBACK DlgProcGpgBinOpts(HWND hwndDlg, UINT msg, WPARAM wParam,
else
{
bad_version = false;
- MessageBox(0, TranslateT("This is not gnupg binary !\nrecommended to use GnuPG v1.x.x with this plugn."), TranslateT("Warning"), MB_OK);
+ MessageBox(0, TranslateT("This is not GnuPG binary!\nIt is recommended to use GnuPG v1.x.x with this plugin."), TranslateT("Warning"), MB_OK);
}
if(bad_version)
- MessageBox(0, TranslateT("Unsupported gnupg version found, use at you own risk!\nrecommended to use GnuPG v1.x.x with this plugn."), TranslateT("Warning"), MB_OK);
+ MessageBox(0, TranslateT("Unsupported GnuPG version found, use at you own risk!\nIt is recommended to use GnuPG v1.x.x with this plugin."), TranslateT("Warning"), MB_OK);
}
DBWriteContactSettingTString(NULL, szGPGModuleName, "szGpgBinPath", tmp);
GetDlgItemText(hwndDlg, IDC_HOME_DIR, tmp, 512);
@@ -1185,18 +1187,18 @@ static INT_PTR CALLBACK DlgProcGpgBinOpts(HWND hwndDlg, UINT msg, WPARAM wParam,
{ //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);
+ 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;
gpg_valid = true;
- if(!gpg_launcher(params, boost::posix_time::minutes(10)))
+ if(!gpg_launcher(params, boost::posix_time::minutes(10)))
{
gpg_valid = false;
break;
@@ -1204,7 +1206,7 @@ static INT_PTR CALLBACK DlgProcGpgBinOpts(HWND hwndDlg, UINT msg, WPARAM wParam,
gpg_valid = false;
if(result == pxNotFound)
break;
- boost::filesystem::remove(path);
+ boost::filesystem::remove(path);
string::size_type p1 = 0;
if((p1 = out.find("key ")) != string::npos)
path = toUTF16(out.substr(p1+4, 8));
@@ -1215,18 +1217,18 @@ static INT_PTR CALLBACK DlgProcGpgBinOpts(HWND hwndDlg, UINT msg, WPARAM wParam,
{
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);
+ 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;
gpg_valid = true;
- if(!gpg_launcher(params))
+ if(!gpg_launcher(params))
{
gpg_valid = false;
break;
@@ -1271,8 +1273,8 @@ static INT_PTR CALLBACK DlgProcGpgBinOpts(HWND hwndDlg, UINT msg, WPARAM wParam,
break;
case WM_DESTROY:
hwndSetDirs = NULL;
- void InitCheck();
- InitCheck();
+ void InitCheck();
+ InitCheck();
break;
}
@@ -1291,7 +1293,7 @@ static INT_PTR CALLBACK DlgProcNewKeyDialog(HWND hwndDlg, UINT msg, WPARAM wPara
case WM_INITDIALOG:
{
hContact = new_key_hcnt;
- //new_key_hcnt_mutex.unlock();
+ //new_key_hcnt_mutex.unlock();
SetWindowPos(hwndDlg, 0, new_key_rect.left, new_key_rect.top, 0, 0, SWP_NOSIZE|SWP_SHOWWINDOW);
TranslateDialogDefault(hwndDlg);
TCHAR *tmp = UniGetContactSettingUtf(hContact, szGPGModuleName, "GPGPubKey", _T(""));
@@ -1393,7 +1395,7 @@ static INT_PTR CALLBACK DlgProcKeyGenDialog(HWND hwndDlg, UINT msg, WPARAM wPara
if(_tcslen(tmp) < 3)
{
mir_free(tmp); tmp = NULL;
- MessageBox(0, TranslateT("You must set encryption algorythm first"), TranslateT("Error"), MB_OK);
+ MessageBox(0, TranslateT("You must set encryption algorithm first"), TranslateT("Error"), MB_OK);
break;
}
if(tmp)
@@ -1532,18 +1534,18 @@ static INT_PTR CALLBACK DlgProcKeyGenDialog(HWND hwndDlg, UINT msg, WPARAM wPara
{ //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);
+ 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;
- extern HFONT bold_font;
- SendMessage(GetDlgItem(hwndDlg, IDC_GENERATING_TEXT), WM_SETFONT, (WPARAM)bold_font, true);
+ extern HFONT bold_font;
+ SendMessage(GetDlgItem(hwndDlg, IDC_GENERATING_TEXT), WM_SETFONT, (WPARAM)bold_font, true);
SetWindowTextA(GetDlgItem(hwndDlg, IDC_GENERATING_TEXT), Translate("Generating new key, please wait..."));
EnableWindow(GetDlgItem(hwndDlg, IDCANCEL), 0);
EnableWindow(GetDlgItem(hwndDlg, IDOK), 0);
@@ -1554,12 +1556,12 @@ static INT_PTR CALLBACK DlgProcKeyGenDialog(HWND hwndDlg, UINT msg, WPARAM wPara
EnableWindow(GetDlgItem(hwndDlg, IDC_KEY_EMAIL), 0);
EnableWindow(GetDlgItem(hwndDlg, IDC_KEY_COMMENT), 0);
EnableWindow(GetDlgItem(hwndDlg, IDC_KEY_EXPIRE_DATE), 0);
- if(!gpg_launcher(params, boost::posix_time::minutes(10)))
+ if(!gpg_launcher(params, boost::posix_time::minutes(10)))
break;
if(result == pxNotFound)
break;
}
- boost::filesystem::remove(path);
+ boost::filesystem::remove(path);
DestroyWindow(hwndDlg);
{//parse gpg output
LVITEM item = {0};
@@ -1572,14 +1574,14 @@ static INT_PTR CALLBACK DlgProcKeyGenDialog(HWND hwndDlg, UINT msg, WPARAM wPara
DWORD code;
string::size_type p = 0, p2 = 0, stop = 0;
{
- std::vector<wstring> cmd;
- cmd.push_back(L"--list-secret-keys");
- gpg_execution_params params(cmd);
+ std::vector<wstring> cmd;
+ cmd.push_back(L"--list-secret-keys");
+ gpg_execution_params params(cmd);
pxResult result;
params.out = &out;
params.code = &code;
params.result = &result;
- if(!gpg_launcher(params))
+ if(!gpg_launcher(params))
break;
if(result == pxNotFound)
break;
@@ -1726,15 +1728,15 @@ static INT_PTR CALLBACK DlgProcLoadExistingKey(HWND hwndDlg,UINT msg,WPARAM wPar
DWORD code;
string::size_type p = 0, p2 = 0, stop = 0;
{
- std::vector<wstring> cmd;
- cmd.push_back(L"--batch");
- cmd.push_back(L"--list-keys");
- gpg_execution_params params(cmd);
+ std::vector<wstring> cmd;
+ cmd.push_back(L"--batch");
+ cmd.push_back(L"--list-keys");
+ gpg_execution_params params(cmd);
pxResult result;
params.out = &out;
params.code = &code;
params.result = &result;
- if(!gpg_launcher(params))
+ if(!gpg_launcher(params))
break;
if(result == pxNotFound)
break;
@@ -1827,17 +1829,17 @@ static INT_PTR CALLBACK DlgProcLoadExistingKey(HWND hwndDlg,UINT msg,WPARAM wPar
extern HWND hPubKeyEdit;
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(id);
- gpg_execution_params params(cmd);
+ std::vector<wstring> cmd;
+ cmd.push_back(L"--batch");
+ cmd.push_back(L"-a");
+ cmd.push_back(L"--export");
+ cmd.push_back(id);
+ gpg_execution_params params(cmd);
pxResult result;
params.out = &out;
params.code = &code;
params.result = &result;
- if(!gpg_launcher(params))
+ if(!gpg_launcher(params))
break;
if(result == pxNotFound)
break;
@@ -1939,24 +1941,24 @@ static INT_PTR CALLBACK DlgProcImportKeyDialog(HWND hwndDlg, UINT msg, WPARAM wP
{
string out;
DWORD code;
- std::vector<wstring> cmd;
- cmd.push_back(L"--keyserver");
+ std::vector<wstring> cmd;
+ cmd.push_back(L"--keyserver");
TCHAR *server= new TCHAR [128];
GetDlgItemText(hwndDlg, IDC_KEYSERVER, server, 128);
- cmd.push_back(server);
+ cmd.push_back(server);
delete [] server;
- cmd.push_back(L"--recv-keys");
+ cmd.push_back(L"--recv-keys");
// char *tmp = UniGetContactSettingUtf(hContact, szGPGModuleName, "KeyID_Prescense", "");
// TCHAR *tmp2 = mir_a2t(tmp);
// mir_free(tmp);
- cmd.push_back(toUTF16(hcontact_data[hContact].key_in_prescense));
+ cmd.push_back(toUTF16(hcontact_data[hContact].key_in_prescense));
// mir_free(tmp2);
- gpg_execution_params params(cmd);
+ gpg_execution_params params(cmd);
pxResult result;
params.out = &out;
params.code = &code;
params.result = &result;
- gpg_launcher(params);
+ gpg_launcher(params);
MessageBoxA(0, out.c_str(), "GPG output", MB_OK);
}
break;
@@ -2071,7 +2073,7 @@ void InitCheck()
if(test_file.good())
home_dir_access = true;
test_file.close();
- boost::filesystem::remove(test_path);
+ boost::filesystem::remove(test_path);
}
home_dir = _tgetenv(_T("TEMP"));
test_path = home_dir;
@@ -2084,16 +2086,16 @@ void InitCheck()
if(test_file.good())
temp_access = true;
test_file.close();
- boost::filesystem::remove(test_path);
+ boost::filesystem::remove(test_path);
}
if(!home_dir_access || !temp_access || !gpg_valid)
{
char buf[4096];
- strcpy(buf, gpg_valid?Translate("GPG binary is set and valid (this is good).\n"):Translate("GPG binary unset or invalid (plugin will not work).\n"));
- strcat(buf, home_dir_access?Translate("Home dir write access granted (this is good).\n"):Translate("Home dir have not write access (plugin most probably will not work).\n"));
- strcat(buf, temp_access?Translate("Temp dir write access granted (this is good).\n"):Translate("Temp dir have not write access (plugin should work, but may have some problems, filetransfers will not work)."));
+ strcpy(buf, gpg_valid?Translate("GPG binary is set and valid (This is good).\n"):Translate("GPG binary unset or invalid (Plugin will not work).\n"));
+ strcat(buf, home_dir_access?Translate("Home dir write access granted (This is good).\n"):Translate("Home dir has no write access (Plugin most probably will not work).\n"));
+ strcat(buf, temp_access?Translate("Temp dir write access granted (This is good).\n"):Translate("Temp dir has no write access (Plugin should work, but may have some problems, file transfers will not work)."));
if(!gpg_valid)
- strcat(buf, Translate("\nGPG will be disabled until you solve this problems"));
+ strcat(buf, Translate("\nGPG will be disabled until you solve these problems"));
MessageBoxA(0, buf, Translate("GPG plugin problems"), MB_OK);
}
if(!gpg_valid)
@@ -2104,14 +2106,14 @@ void InitCheck()
pxResult result;
wstring::size_type p = 0, p2 = 0, stop = 0;
{
- std::vector<wstring> cmd;
- cmd.push_back(L"--batch");
- cmd.push_back(L"--list-secret-keys");
- gpg_execution_params params(cmd);
+ std::vector<wstring> cmd;
+ cmd.push_back(L"--batch");
+ cmd.push_back(L"--list-secret-keys");
+ gpg_execution_params params(cmd);
params.out = &out;
params.code = &code;
params.result = &result;
- if(!gpg_launcher(params))
+ if(!gpg_launcher(params))
return;
if(result == pxNotFound)
return;
@@ -2121,145 +2123,150 @@ void InitCheck()
mir_free(home_dir);
tmp_dir += _T("\\tmp");
_wmkdir(tmp_dir.c_str());
- int count = 0;
- PROTOACCOUNT **accounts;
- ProtoEnumAccounts(&count, &accounts);
- string question;
- char *keyid, *key;
- for(int i = 0; i < count; i++)
- {
- if(StriStr(accounts[i]->szModuleName, "metacontacts"))
- continue;
- if(StriStr(accounts[i]->szModuleName, "weather"))
- continue;
- std::string acc = toUTF8(accounts[i]->tszAccountName);
- acc += "(";
- acc += accounts[i]->szModuleName;
- acc += ")";
- acc += "_KeyID";
- keyid = UniGetContactSettingUtf(NULL, szGPGModuleName, acc.c_str(), "");
- if(keyid[0])
- {
- question = Translate("Your secret key whith id: ");
- keyid = UniGetContactSettingUtf(NULL, szGPGModuleName, "KeyID", "");
- key = UniGetContactSettingUtf(NULL, szGPGModuleName, "GPGPubKey", "");
- void ShowFirstRunDialog();
- if((p = out.find(keyid)) == string::npos)
- {
- question += keyid;
- question += Translate(" for account ");
- question += toUTF8(accounts[i]->tszAccountName);
- question += Translate(" deleted from gpg secret keyring\nDo you want to set another key ?");
- if(MessageBoxA(0, question.c_str(), Translate("Own secret key warning"), MB_YESNO) == IDYES)
- ShowFirstRunDialog();
- }
- p2 = p;
- p = out.find("[", p);
- p2 = out.find("\n", p2);
- if((p != std::string::npos) && (p < p2))
- {
- p = out.find("expires:", p);
- p += strlen("expires:");
- p++;
- p2 = out.find("]", p);
- TCHAR *expire_date = mir_wstrdup(toUTF16(out.substr(p,p2-p)).c_str());
- bool expired = false;
- {
- boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();
- TCHAR buf[5];
- mir_sntprintf(buf, 5, _T("%s"), expire_date);
- int year = _ttoi(buf);
- if(year < now.date().year())
- expired = true;
- else if(year == now.date().year())
- {
- mir_sntprintf(buf, 3, _T("%s"), expire_date+5);
- int month = _ttoi(buf);
- if(month < now.date().month())
- expired = true;
- else if(month == now.date().month())
- {
- mir_sntprintf(buf, 3, _T("%s"), expire_date+8);
- unsigned day = _ttoi(buf);
- if(day <= now.date().day_number())
- expired = true;
- }
- }
- }
- if(expired)
- {
- question += keyid;
- question += Translate(" for account ");
- question += toUTF8(accounts[i]->tszAccountName);
- question += Translate(" expired and will not work\nDo you want to set another key ?");
- if(MessageBoxA(0, question.c_str(), Translate("Own secret key warning"), MB_YESNO) == IDYES)
- ShowFirstRunDialog();
- }
- mir_free(expire_date);
- }
- }
- mir_free(keyid);
- }
- question = Translate("Your secret key whith id: ");
- keyid = UniGetContactSettingUtf(NULL, szGPGModuleName, "KeyID", "");
- key = UniGetContactSettingUtf(NULL, szGPGModuleName, "GPGPubKey", "");
+ int count = 0;
+ PROTOACCOUNT **accounts;
+ ProtoEnumAccounts(&count, &accounts);
+ string question;
+ char *keyid = nullptr, *key = nullptr;
+ for(int i = 0; i < count; i++)
+ {
+ if(StriStr(accounts[i]->szModuleName, "metacontacts"))
+ continue;
+ if(StriStr(accounts[i]->szModuleName, "weather"))
+ continue;
+ std::string acc = toUTF8(accounts[i]->tszAccountName);
+ acc += "(";
+ acc += accounts[i]->szModuleName;
+ acc += ")";
+ acc += "_KeyID";
+ keyid = UniGetContactSettingUtf(NULL, szGPGModuleName, acc.c_str(), "");
+ if(keyid[0])
+ {
+ question = Translate("Your secret key with id: ");
+ mir_free(keyid);
+ keyid = UniGetContactSettingUtf(NULL, szGPGModuleName, "KeyID", "");
+ key = UniGetContactSettingUtf(NULL, szGPGModuleName, "GPGPubKey", "");
+ void ShowFirstRunDialog();
+ if((p = out.find(keyid)) == string::npos)
+ {
+ question += keyid;
+ question += Translate(" for account ");
+ question += toUTF8(accounts[i]->tszAccountName);
+ question += Translate(" deleted from GPG secret keyring.\nDo you want to set another key?");
+ if(MessageBoxA(0, question.c_str(), Translate("Own secret key warning"), MB_YESNO) == IDYES)
+ ShowFirstRunDialog();
+ }
+ p2 = p;
+ p = out.find("[", p);
+ p2 = out.find("\n", p2);
+ if((p != std::string::npos) && (p < p2))
+ {
+ p = out.find("expires:", p);
+ p += strlen("expires:");
+ p++;
+ p2 = out.find("]", p);
+ TCHAR *expire_date = mir_wstrdup(toUTF16(out.substr(p,p2-p)).c_str());
+ bool expired = false;
+ {
+ boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();
+ TCHAR buf[5];
+ mir_sntprintf(buf, 5, _T("%s"), expire_date);
+ int year = _ttoi(buf);
+ if(year < now.date().year())
+ expired = true;
+ else if(year == now.date().year())
+ {
+ mir_sntprintf(buf, 3, _T("%s"), expire_date+5);
+ int month = _ttoi(buf);
+ if(month < now.date().month())
+ expired = true;
+ else if(month == now.date().month())
+ {
+ mir_sntprintf(buf, 3, _T("%s"), expire_date+8);
+ unsigned day = _ttoi(buf);
+ if(day <= now.date().day_number())
+ expired = true;
+ }
+ }
+ }
+ if(expired)
+ {
+ question += keyid;
+ question += Translate(" for account ");
+ question += toUTF8(accounts[i]->tszAccountName);
+ question += Translate(" expired and will not work.\nDo you want to set another key?");
+ if(MessageBoxA(0, question.c_str(), Translate("Own secret key warning"), MB_YESNO) == IDYES)
+ ShowFirstRunDialog();
+ }
+ mir_free(expire_date);
+ }
+ }
+ if(keyid)
+ {
+ mir_free(keyid);
+ keyid = nullptr;
+ }
+ }
+ question = Translate("Your secret key with id: ");
+ keyid = UniGetContactSettingUtf(NULL, szGPGModuleName, "KeyID", "");
+ key = UniGetContactSettingUtf(NULL, szGPGModuleName, "GPGPubKey", "");
void ShowFirstRunDialog();
if(!DBGetContactSettingByte(NULL, szGPGModuleName, "FirstRun", 1) && (!keyid[0] || !key[0]))
{
- question = Translate("You didn't set a private key.\nWould you like to set it now?");
- if(MessageBoxA(0, question.c_str(), Translate("Own private key warning"), MB_YESNO) == IDYES)
+ question = Translate("You didn't set a private key.\nWould you like to set it now?");
+ if(MessageBoxA(0, question.c_str(), Translate("Own private key warning"), MB_YESNO) == IDYES)
ShowFirstRunDialog();
}
- if((p = out.find(keyid)) == string::npos)
+ if((p = out.find(keyid)) == string::npos)
{
question += keyid;
- question += Translate(" deleted from gpg secret keyring\nDo you want to set another key ?");
+ question += Translate(" deleted from GPG secret keyring.\nDo you want to set another key?");
if(MessageBoxA(0, question.c_str(), Translate("Own secret key warning"), MB_YESNO) == IDYES)
ShowFirstRunDialog();
}
- p2 = p;
- p = out.find("[", p);
- p2 = out.find("\n", p2);
- if((p != std::string::npos) && (p < p2))
- {
- p = out.find("expires:", p);
- p += strlen("expires:");
- p++;
- p2 = out.find("]", p);
- TCHAR *expire_date = mir_wstrdup(toUTF16(out.substr(p,p2-p)).c_str());
- bool expired = false;
- {
- boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();
- TCHAR buf[5];
- mir_sntprintf(buf, 5, _T("%s"), expire_date);
- int year = _ttoi(buf);
- if(year < now.date().year())
- expired = true;
- else if(year == now.date().year())
- {
- mir_sntprintf(buf, 3, _T("%s"), expire_date+5);
- int month = _ttoi(buf);
- if(month < now.date().month())
- expired = true;
- else if(month == now.date().month())
- {
- mir_sntprintf(buf, 3, _T("%s"), expire_date+8);
- unsigned day = _ttoi(buf);
- if(day <= now.date().day_number())
- expired = true;
- }
- }
- }
- if(expired)
- {
- question += keyid;
- question += Translate(" expired and will not work\nDo you want to set another key ?");
- if(MessageBoxA(0, question.c_str(), Translate("Own secret key warning"), MB_YESNO) == IDYES)
- ShowFirstRunDialog();
- }
- mir_free(expire_date);
- }
- //TODO: check for expired key
+ p2 = p;
+ p = out.find("[", p);
+ p2 = out.find("\n", p2);
+ if((p != std::string::npos) && (p < p2))
+ {
+ p = out.find("expires:", p);
+ p += strlen("expires:");
+ p++;
+ p2 = out.find("]", p);
+ TCHAR *expire_date = mir_wstrdup(toUTF16(out.substr(p,p2-p)).c_str());
+ bool expired = false;
+ {
+ boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();
+ TCHAR buf[5];
+ mir_sntprintf(buf, 5, _T("%s"), expire_date);
+ int year = _ttoi(buf);
+ if(year < now.date().year())
+ expired = true;
+ else if(year == now.date().year())
+ {
+ mir_sntprintf(buf, 3, _T("%s"), expire_date+5);
+ int month = _ttoi(buf);
+ if(month < now.date().month())
+ expired = true;
+ else if(month == now.date().month())
+ {
+ mir_sntprintf(buf, 3, _T("%s"), expire_date+8);
+ unsigned day = _ttoi(buf);
+ if(day <= now.date().day_number())
+ expired = true;
+ }
+ }
+ }
+ if(expired)
+ {
+ question += keyid;
+ question += Translate(" expired and will not work.\nDo you want to set another key?");
+ if(MessageBoxA(0, question.c_str(), Translate("Own secret key warning"), MB_YESNO) == IDYES)
+ ShowFirstRunDialog();
+ }
+ mir_free(expire_date);
+ }
+ //TODO: check for expired key
mir_free(keyid);
mir_free(key);
}
@@ -2294,26 +2301,26 @@ void InitCheck()
CallService(svc, 0, (LPARAM)&cap);
}
}
- if(bFileTransfers)
- {
- int count = 0;
- PROTOACCOUNT **accounts;
- ProtoEnumAccounts(&count, &accounts);
- ICQ_CUSTOMCAP cap;
- cap.cbSize = sizeof(ICQ_CUSTOMCAP);
- cap.hIcon = 0;
- strcpy(cap.name, "GPG Encrypted FileTransfers");
- strcpy(cap.caps, "GPG FileTransfer");
-
- for(int i = 0; i < count; i++)
- {
- char svc[64];
- strcpy(svc, accounts[i]->szProtoName);
- strcat(svc, PS_ICQ_ADDCAPABILITY);
- if(ServiceExists(svc))
- CallService(svc, 0, (LPARAM)&cap);
- }
- }
+ if(bFileTransfers)
+ {
+ int count = 0;
+ PROTOACCOUNT **accounts;
+ ProtoEnumAccounts(&count, &accounts);
+ ICQ_CUSTOMCAP cap;
+ cap.cbSize = sizeof(ICQ_CUSTOMCAP);
+ cap.hIcon = 0;
+ strcpy(cap.name, "GPG Encrypted FileTransfers");
+ strcpy(cap.caps, "GPG FileTransfer");
+
+ for(int i = 0; i < count; i++)
+ {
+ char svc[64];
+ strcpy(svc, accounts[i]->szProtoName);
+ strcat(svc, PS_ICQ_ADDCAPABILITY);
+ if(ServiceExists(svc))
+ CallService(svc, 0, (LPARAM)&cap);
+ }
+ }
}
void ImportKey()
@@ -2325,7 +2332,7 @@ void ImportKey()
new_key_hcnt_mutex.unlock();
bool for_all_sub = false;
if(metaIsProtoMetaContacts(hContact))
- if(MessageBox(0, TranslateT("Do you want load key for all subcontacts ?"), TranslateT("Metacontact detected"), MB_YESNO) == IDYES)
+ if(MessageBox(0, TranslateT("Do you want to load key for all subcontacts?"), TranslateT("Metacontact detected"), MB_YESNO) == IDYES)
for_all_sub = true;
if(metaIsProtoMetaContacts(hContact))
{
@@ -2347,7 +2354,7 @@ void ImportKey()
DBWriteContactSettingTString(hContact, szGPGModuleName, "GPGPubKey", new_key.c_str());
new_key.clear();
{ //gpg execute block
- std::vector<wstring> cmd;
+ std::vector<wstring> cmd;
TCHAR tmp2[MAX_PATH] = {0};
TCHAR *ptmp;
string output;
@@ -2358,7 +2365,7 @@ void ImportKey()
mir_free(ptmp);
_tcscat(tmp2, _T("\\"));
_tcscat(tmp2, _T("temporary_exported.asc"));
- boost::filesystem::remove(tmp2);
+ boost::filesystem::remove(tmp2);
wfstream f(tmp2, std::ios::out);
if(metaIsProtoMetaContacts(hContact))
ptmp = UniGetContactSettingUtf(metaGetMostOnline(hContact), szGPGModuleName, "GPGPubKey", _T(""));
@@ -2368,16 +2375,16 @@ void ImportKey()
mir_free(ptmp);
f<<new_key.c_str();
f.close();
- cmd.push_back(L"--batch");
- cmd.push_back(L"--import");
- cmd.push_back(tmp2);
+ cmd.push_back(L"--batch");
+ cmd.push_back(L"--import");
+ cmd.push_back(tmp2);
}
- gpg_execution_params params(cmd);
+ gpg_execution_params params(cmd);
pxResult result;
params.out = &output;
params.code = &exitcode;
params.result = &result;
- if(!gpg_launcher(params))
+ if(!gpg_launcher(params))
return;
if(result == pxNotFound)
return;
@@ -2606,6 +2613,6 @@ void ImportKey()
ptmp = mir_wstrdup(toUTF16(output).c_str());
MessageBox(0, ptmp, _T(""), MB_OK);
mir_free(ptmp);
- boost::filesystem::remove(tmp2);
+ boost::filesystem::remove(tmp2);
}
} \ No newline at end of file
diff --git a/messages.cpp b/messages.cpp
index 7a4667f..4b82cc2 100755
--- a/messages.cpp
+++ b/messages.cpp
@@ -42,7 +42,7 @@ void RecvMsgSvc_func(HANDLE hContact, std::wstring str, char *msg, DWORD flags,
{
if(bDebugLog)
debuglog<<std::string(time_str()+": info: received encrypted message from: "+toUTF8((TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)hContact, GCDNF_TCHAR))+" with turned off encryption");
- if(MessageBox(0, TranslateT("We received encrypted message from contact with encryption turned off.\nDo you want turn on encryption for this contact ?"), TranslateT("Warning"), MB_YESNO) == IDYES)
+ if(MessageBox(0, 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))
{
@@ -66,7 +66,7 @@ void RecvMsgSvc_func(HANDLE hContact, std::wstring str, char *msg, DWORD flags,
setClistIcon(hContact);
}
}
- else if(MessageBox(0, TranslateT("Do you want try to decrypt encrypted message ?"), TranslateT("Warning"), MB_YESNO) == IDNO)
+ else if(MessageBox(0, TranslateT("Do you want to try to decrypt encrypted message?"), TranslateT("Warning"), MB_YESNO) == IDNO)
{
HistoryLog(hContact, db_event(msg, timestamp, 0, dbflags));
@@ -268,7 +268,7 @@ void RecvMsgSvc_func(HANDLE hContact, std::wstring str, char *msg, DWORD flags,
str.insert(0, "Received unencrypted message:\n");
if(bDebugLog)
debuglog<<std::string(time_str()+": info: Failed to decrypt GPG encrypted message.");
- char *tmp = new char [str.length()+1];
+ char *tmp = (char*)mir_alloc(sizeof(char)*(str.length()+1));
strcpy(tmp, str.c_str());
HistoryLog(hContact, db_event(msg, timestamp, 0, dbflags));
BYTE enc = DBGetContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", 0);
@@ -603,6 +603,7 @@ int RecvMsgSvc(WPARAM w, LPARAM l)
}
}
}
+ mir_free(jid);
}
}
if(!strstr(msg, "-----BEGIN PGP MESSAGE-----"))
@@ -710,7 +711,7 @@ void SendMsgSvc_func(HANDLE hContact, char *msg, DWORD flags)
if(out.find("There is no assurance this key belongs to the named user") != string::npos)
{
out.clear();
- if(MessageBox(0, TranslateT("We 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)
{
DBWriteContactSettingByte(hContact, szGPGModuleName, "bAlwaysTrust", 1);
std::vector<std::wstring> tmp;
@@ -745,13 +746,13 @@ void SendMsgSvc_func(HANDLE hContact, char *msg, DWORD flags)
if(result == pxSuccessExitCodeInvalid)
{
//mir_free(msg);
- HistoryLog(hContact, db_event(Translate("failed o encrypt message, GPG returned error, turn on debug log for more details"), 0,0, DBEF_SENT));
+ HistoryLog(hContact, db_event(Translate("failed to encrypt message, GPG returned error, turn on debug log for more details"), 0,0, DBEF_SENT));
boost::filesystem::remove(path);
return;
}
if(out.find("usage: ") != string::npos)
{
- MessageBox(0, TranslateT("Something wrong, gpg does not understand us, aborting encryption."), TranslateT("Warning"), MB_OK);
+ MessageBox(0, TranslateT("Something is wrong, GPG does not understand us, aborting encryption."), TranslateT("Warning"), MB_OK);
//mir_free(msg);
CallContactService(hContact, PSS_MESSAGE, (WPARAM)flags, (LPARAM)msg);
boost::filesystem::remove(path);
@@ -821,6 +822,8 @@ int SendMsgSvc(WPARAM w, LPARAM l)
CCSDATA *ccs = (CCSDATA*)l;
if (!ccs)
return CallService(MS_PROTO_CHAINSEND, w, l);
+ if(!ccs->lParam)
+ return CallService(MS_PROTO_CHAINSEND, w, l);
char *msg = nullptr;
if((ccs->wParam & PREF_UTF) == PREF_UTF)
msg = mir_strdup((char*)(ccs->lParam));
@@ -839,89 +842,7 @@ int SendMsgSvc(WPARAM w, LPARAM l)
debuglog<<std::string(time_str()+": info: encrypted messge, let it go, name: "+toUTF8((TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)ccs->hContact, GCDNF_TCHAR)));
return CallService(MS_PROTO_CHAINSEND, w, l);
}
- /*if(!isContactHaveKey(ccs->hContact))
- {
- if(bDebugLog)
- debuglog<<std::string(time_str()+": info: contact have not key, name: "+toUTF8((TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)ccs->hContact, GCDNF_TCHAR)));
- if(bAutoExchange && !strstr(msg, "-----PGP KEY REQUEST-----") && !strstr(msg, "-----BEGIN PGP PUBLIC KEY BLOCK-----") && gpg_valid)
- {
- if(bDebugLog)
- debuglog<<std::string(time_str()+": info: checking for autoexchange possibility, name: "+toUTF8((TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)ccs->hContact, GCDNF_TCHAR)));
- void send_encrypted_msgs_thread(HANDLE hContact);
- LPSTR proto = (LPSTR)CallService(MS_PROTO_GETCONTACTBASEPROTO, (WPARAM)ccs->hContact, 0);
- DWORD uin = DBGetContactSettingDword(ccs->hContact, proto, "UIN", 0);
- if(uin)
- {
- if(bDebugLog)
- debuglog<<std::string(time_str()+": info(autoexchange): protocol looks like icq, name: "+toUTF8((TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)ccs->hContact, GCDNF_TCHAR)));
- char *proto = (char*)CallService(MS_PROTO_GETCONTACTBASEPROTO, (WPARAM)ccs->hContact, 0);
- char svc[64];
- strcpy(svc, proto);
- strcat(svc, PS_ICQ_CHECKCAPABILITY);
-
- if(ServiceExists(svc))
- {
- if(bDebugLog)
- debuglog<<std::string(time_str()+": info(autoexchange, icq): checking for autoexchange icq capability, name: "+toUTF8((TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)ccs->hContact, GCDNF_TCHAR)));
- ICQ_CUSTOMCAP cap = {0};
- strcpy(cap.caps, "GPG AutoExchange");
- if(CallService(svc, (WPARAM)ccs->hContact, (LPARAM)&cap))
- {
- if(bDebugLog)
- debuglog<<std::string(time_str()+": info(autoexchange, icq): sending key requiest, name: "+toUTF8((TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)ccs->hContact, GCDNF_TCHAR)));
- CallContactService(ccs->hContact, PSS_MESSAGE, (WPARAM)ccs->wParam, (LPARAM)"-----PGP KEY REQUEST-----");
- hcontact_data[ccs->hContact].msgs_to_send.push_back(msg);
- boost::thread *thr = new boost::thread(boost::bind(send_encrypted_msgs_thread, ccs->hContact));
- mir_free(msg);
- return returnNoError(ccs->hContact);
- }
- }
- }
- else
- {
- TCHAR *jid = UniGetContactSettingUtf(ccs->hContact, proto, "jid", _T(""));
- if(jid[0])
- {
- if(bDebugLog)
- debuglog<<std::string(time_str()+": info(autoexchange): protocol looks like jabber, name: "+toUTF8((TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)ccs->hContact, GCDNF_TCHAR)));
- extern list <JabberAccount*> Accounts;
- list<JabberAccount*>::iterator end = Accounts.end();
- for(list<JabberAccount*>::iterator p = Accounts.begin(); p != end; p++)
- {
- TCHAR *caps = (*p)->getJabberInterface()->Net()->GetResourceFeatures(jid);
- if(caps)
- {
- wstring str;
- for(int i=0;;i++)
- {
- str.push_back(caps[i]);
- if(caps[i] == '\0')
- if(caps[i+1] == '\0')
- break;
- }
- mir_free(caps);
- if(str.find(_T("GPG_Key_Auto_Exchange:0")) != string::npos)
- {
- if(bDebugLog)
- debuglog<<std::string(time_str()+": info(autoexchange, jabber): autoexchange capability found, sending key request, name: "+toUTF8((TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)ccs->hContact, GCDNF_TCHAR)));
- CallContactService(ccs->hContact, PSS_MESSAGE, (WPARAM)ccs->wParam, (LPARAM)"-----PGP KEY REQUEST-----");
- hcontact_data[ccs->hContact].msgs_to_send.push_back(msg);
- boost::thread *thr = new boost::thread(boost::bind(send_encrypted_msgs_thread, ccs->hContact));
- mir_free(msg);
- return returnNoError(ccs->hContact);
- }
- }
- }
- }
- }
- }
- else
- {
- mir_free(msg);
- return CallService(MS_PROTO_CHAINSEND, w, l);
- }
- } */
- else if(bDebugLog)
+ if(bDebugLog)
debuglog<<std::string(time_str()+": info: contact have key, name: "+toUTF8((TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)ccs->hContact, GCDNF_TCHAR)));
if(bDebugLog && metaIsProtoMetaContacts(ccs->hContact))
debuglog<<std::string(time_str()+": info: protocol is metacontacts, name: "+toUTF8((TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)ccs->hContact, GCDNF_TCHAR)));
@@ -1057,11 +978,11 @@ int HookSendMsg(WPARAM w, LPARAM l)
if(bAppendTags)
{
string str_event = (char*)dbei->pBlob;
- mir_free(dbei->pBlob);
+ //mir_free(dbei->pBlob);
str_event.insert(0, toUTF8(outopentag));
str_event.append(toUTF8(outclosetag));
dbei->pBlob = (PBYTE)mir_strdup(str_event.c_str());
- dbei->cbBlob = str_event.length() + 1;
+ dbei->cbBlob = (DWORD)str_event.length() + 1;
}
return 0;
@@ -1130,6 +1051,7 @@ static INT_PTR CALLBACK DlgProcKeyPassword(HWND hwndDlg, UINT msg, WPARAM wParam
TranslateDialogDefault(hwndDlg);
string questionstr = "Please enter password for key with ID: ";
questionstr += inkeyid;
+ mir_free(inkeyid);
SetDlgItemTextA(hwndDlg, IDC_KEYID, questionstr.c_str());
EnableWindow(GetDlgItem(hwndDlg, IDC_DEFAULT_PASSWORD), 0);
return TRUE;
@@ -1160,8 +1082,8 @@ static INT_PTR CALLBACK DlgProcKeyPassword(HWND hwndDlg, UINT msg, WPARAM wParam
DBWriteContactSettingTString(NULL, szGPGModuleName, "szKeyPassword", tmp);
}
if(password)
- delete [] password;
- password = new TCHAR [_tcslen(tmp)+1];
+ mir_free(password);
+ password = (TCHAR*)mir_alloc(sizeof(TCHAR)*(_tcslen(tmp)+1));
_tcscpy(password, tmp);
}
mir_free(tmp);
diff --git a/new_gpg.vcxproj b/new_gpg.vcxproj
index c6de2ed..f49de43 100755
--- a/new_gpg.vcxproj
+++ b/new_gpg.vcxproj
@@ -477,7 +477,7 @@
</Midl>
<ClCompile>
<Optimization>Disabled</Optimization>
- <AdditionalIncludeDirectories>x:\temp\windows\libs\utf8cpp\include;x:\temp\windows\libs\Boost\include;x:\install\git\miranda\miranda-im\miranda\include;x:\install\git\miranda\mim_plugs;../../include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
+ <AdditionalIncludeDirectories>X:\temp\windows\libs\boost_process;x:\temp\windows\libs\utf8cpp\include;x:\temp\windows\libs\Boost\include;x:\install\git\miranda\miranda-im\miranda\include;x:\install\git\miranda\mim_plugs;../../include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;_USRDLL;TESTPLUG_EXPORTS;_CRT_SECURE_NO_WARNINGS;_UNICODE;UNICODE;_CRT_SECURE_NO_DEPRECATE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
@@ -531,7 +531,7 @@
</Midl>
<ClCompile>
<Optimization>Disabled</Optimization>
- <AdditionalIncludeDirectories>x:\temp\windows\libs\utf8cpp\include;X:\temp\windows\libs\Boost\include;x:\install\git\miranda\miranda-im\miranda\include;x:\install\git\miranda\mim_plugs;../../include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
+ <AdditionalIncludeDirectories>X:\temp\windows\libs\boost_process;x:\temp\windows\libs\utf8cpp\include;X:\temp\windows\libs\Boost\include;x:\install\git\miranda\miranda-im\miranda\include;x:\install\git\miranda\mim_plugs;../../include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;_USRDLL;TESTPLUG_EXPORTS;_CRT_SECURE_NO_WARNINGS;_UNICODE;UNICODE;_CRT_SECURE_NO_DEPRECATE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
@@ -690,7 +690,7 @@
<FavorSizeOrSpeed>Size</FavorSizeOrSpeed>
<OmitFramePointers>true</OmitFramePointers>
<WholeProgramOptimization>true</WholeProgramOptimization>
- <AdditionalIncludeDirectories>x:\temp\windows\libs\utf8cpp\include;X:\temp\windows\libs\Boost\include;X:\install\git\miranda\miranda-im\miranda\include;x:\install\git\miranda\mim_plugs</AdditionalIncludeDirectories>
+ <AdditionalIncludeDirectories>x:\temp\windows\libs\utf8cpp\include;X:\temp\windows\libs\boost_process;X:\temp\windows\libs\Boost\include;X:\install\git\miranda\miranda-im\miranda\include;x:\install\git\miranda\mim_plugs</AdditionalIncludeDirectories>
<PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;_USRDLL;_CRT_SECURE_NO_WARNINGS;_UNICODE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<StringPooling>true</StringPooling>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
diff --git a/options.cpp b/options.cpp
index c7b89f5..a9bf91e 100755
--- a/options.cpp
+++ b/options.cpp
@@ -256,7 +256,7 @@ static INT_PTR CALLBACK DlgProcGpgOpts(HWND hwndDlg, UINT msg, WPARAM wParam, LP
}
}
if(!keep)
- if(MessageBox(0, TranslateT("This key not used by any contact, do you want to remove it from public keyring ?"), TranslateT("Key info"), MB_YESNO) == IDYES)
+ if(MessageBox(0, TranslateT("This key is not used by any contact. Do you want to remove it from public keyring?"), TranslateT("Key info"), MB_YESNO) == IDYES)
{
std::vector<wstring> cmd;
string output;
@@ -290,7 +290,7 @@ static INT_PTR CALLBACK DlgProcGpgOpts(HWND hwndDlg, UINT msg, WPARAM wParam, LP
mir_free(tmp);
if(ismetacontact)
{
- if(MessageBox(0, TranslateT("Do you want to remove key from entire metacontact (all subcontacts) ?"), TranslateT("Metacontact detected"), MB_YESNO) == IDYES)
+ if(MessageBox(0, TranslateT("Do you want to remove key from entire metacontact (all subcontacts)?"), TranslateT("Metacontact detected"), MB_YESNO) == IDYES)
{
HANDLE hcnt = NULL;
int count = metaGetContactsNum(meta);
@@ -373,7 +373,7 @@ static INT_PTR CALLBACK DlgProcGpgOpts(HWND hwndDlg, UINT msg, WPARAM wParam, LP
if(OpenClipboard(hwndDlg))
{
char *szKey = UniGetContactSettingUtf(NULL, szGPGModuleName, "GPGPubKey", "");
- std::string str = szKey;;
+ std::string str = szKey;
mir_free(szKey);
boost::algorithm::replace_all(str, "\n", "\r\n");
HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, str.size() +1);
@@ -386,7 +386,7 @@ static INT_PTR CALLBACK DlgProcGpgOpts(HWND hwndDlg, UINT msg, WPARAM wParam, LP
if(!szKey)
{
char msg[64];
- mir_snprintf(msg, 127, "Failed to lock memory with error %d", GetLastError());
+ mir_snprintf(msg, 64, "Failed to lock memory with error %d", GetLastError());
MessageBoxA(0, msg, "Error", MB_OK);
GlobalFree(hMem);
}
@@ -399,7 +399,7 @@ static INT_PTR CALLBACK DlgProcGpgOpts(HWND hwndDlg, UINT msg, WPARAM wParam, LP
{
GlobalFree(hMem);
char msg[64];
- mir_snprintf(msg, 127, "Failed write to clipboard with error %d", GetLastError());
+ mir_snprintf(msg, 64, "Failed write to clipboard with error %d", GetLastError());
MessageBoxA(0, msg, "Error", MB_OK);
}
CloseClipboard();
@@ -407,7 +407,7 @@ static INT_PTR CALLBACK DlgProcGpgOpts(HWND hwndDlg, UINT msg, WPARAM wParam, LP
else
{
char msg[64];
- mir_snprintf(msg, 127, "Failed to open clipboard with error %d", GetLastError());
+ mir_snprintf(msg, 64, "Failed to open clipboard with error %d", GetLastError());
MessageBoxA(0, msg, "Error", MB_OK);
}
}
@@ -526,7 +526,10 @@ static INT_PTR CALLBACK DlgProcGpgBinOpts(HWND hwndDlg, UINT msg, WPARAM wParam,
params.out = &out;
params.code = &code;
params.result = &result;
+ auto old_gpg_state = gpg_valid;
+ gpg_valid = true;
gpg_launcher(params);
+ gpg_valid = old_gpg_state;
DBWriteContactSettingTString(NULL, szGPGModuleName, "szGpgBinPath", tmp_path);
mir_free(tmp_path);
string::size_type p1 = out.find("(GnuPG) ");
@@ -539,10 +542,10 @@ static INT_PTR CALLBACK DlgProcGpgBinOpts(HWND hwndDlg, UINT msg, WPARAM wParam,
else
{
bad_version = false;
- MessageBox(0, TranslateT("This is not gnupg binary !\nrecommended to use GnuPG v1.x.x with this plugn."), TranslateT("Warning"), MB_OK);
+ MessageBox(0, TranslateT("This is not GnuPG binary!\nIt is recommended to use GnuPG v1.x.x with this plugin."), TranslateT("Warning"), MB_OK);
}
/* if(bad_version) //looks like working fine with gpg2
- MessageBox(0, TranslateT("Unsupported gnupg version found, use at you own risk!\nrecommended to use GnuPG v1.x.x with this plugn."), _T("Warning"), MB_OK); */
+ MessageBox(0, TranslateT("Unsupported GnuPG version found, use at you own risk!\nIt is recommended to use GnuPG v1.x.x with this plugin."), _T("Warning"), MB_OK); */
}
}
char mir_path[MAX_PATH];
@@ -574,6 +577,7 @@ static INT_PTR CALLBACK DlgProcGpgBinOpts(HWND hwndDlg, UINT msg, WPARAM wParam,
tmp = mir_a2t(p_path);
SetDlgItemText(hwndDlg, IDC_HOME_DIR, tmp);
}
+ mir_free(atmp);
}
break;
default:
diff --git a/utilities.cpp b/utilities.cpp
index 68f01a2..8f74dfb 100755
--- a/utilities.cpp
+++ b/utilities.cpp
@@ -1,598 +1,599 @@
-// Copyright © 2010-2012 sss
-//
-// This program is free software; you can redistribute it and/or
-// modify it under the terms of the GNU General Public License
-// as published by the Free Software Foundation; either version 2
-// of the License, or (at your option) any later version.
-//
-// This program is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License
-// along with this program; if not, write to the Free Software
-// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-
-
-#include "commonheaders.h"
-
-
-TCHAR* __stdcall UniGetContactSettingUtf(HANDLE hContact, const char *szModule,const char* szSetting, TCHAR* szDef)
-{
- DBVARIANT dbv = {DBVT_DELETED};
- TCHAR* szRes;
- if (DBGetContactSettingTString(hContact, szModule, szSetting, &dbv))
- return mir_tstrdup(szDef);
- if(dbv.pszVal)
- szRes = mir_tstrdup(dbv.ptszVal);
- DBFreeVariant(&dbv);
- return szRes;
-}
-
-char* __stdcall UniGetContactSettingUtf(HANDLE hContact, const char *szModule,const char* szSetting, char* szDef)
-{
- DBVARIANT dbv = {DBVT_DELETED};
- char* szRes;
- if (DBGetContactSettingString(hContact, szModule, szSetting, &dbv))
- return mir_strdup(szDef);
- if(dbv.pszVal)
- szRes = mir_strdup(dbv.pszVal);
- DBFreeVariant(&dbv);
- return szRes;
-}
-
-
-char *date()
-{
- setlocale( LC_ALL, "C" );
- static char d[11];
- char *tmp = __DATE__, m[4], mn[3] = "01";
- m[0]=tmp[0];
- m[1]=tmp[1];
- m[2]=tmp[2];
- if(strstr(m,"Jan"))
- strcpy(mn,"01");
- else if(strstr(m,"Feb"))
- strcpy(mn,"02");
- else if(strstr(m,"Mar"))
- strcpy(mn,"03");
- else if(strstr(m,"Apr"))
- strcpy(mn,"04");
- else if(strstr(m,"May"))
- strcpy(mn,"05");
- else if(strstr(m,"Jun"))
- strcpy(mn,"06");
- else if(strstr(m,"Jul"))
- strcpy(mn,"07");
- else if(strstr(m,"Aug"))
- strcpy(mn,"08");
- else if(strstr(m,"Sep"))
- strcpy(mn,"09");
- else if(strstr(m,"Oct"))
- strcpy(mn,"10");
- else if(strstr(m,"Nov"))
- strcpy(mn,"11");
- else if(strstr(m,"Dec"))
- strcpy(mn,"12");
- d[0]=tmp[7];
- d[1]=tmp[8];
- d[2]=tmp[9];
- d[3]=tmp[10];
- d[4]='.';
- d[5]=mn[0];
- d[6]=mn[1];
- d[7]='.';
- if (tmp[4] == ' ')
- d[8] = '0';
- else
- d[8]=tmp[4];
- d[9]=tmp[5];
- return d;
-}
-
-void GetFilePath(TCHAR *WindowTittle, char *szSetting, TCHAR *szExt, TCHAR *szExtDesc)
-{
- 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);
- _tcscpy(filter,TranslateW(szExtDesc));
- pfilter=filter+_tcslen(filter)+1;
- _tcscpy(pfilter, szExt);
- pfilter[_tcslen(pfilter)+1] = '\0';
- pfilter[_tcslen(pfilter)+2] = '\0';
- ofn.lpstrFilter=filter;
- tmp = UniGetContactSettingUtf(0, szGPGModuleName, szSetting, _T(""));
- _tcscpy(str, tmp);
- mir_free(tmp);
- if(_tcslen(str)< 2)
- str[0] = '\0';
- ofn.lpstrFile=str;
- ofn.nMaxFile=_MAX_PATH;
- ofn.nMaxFileTitle=MAX_PATH;
- if(!GetOpenFileName(&ofn))
- return;
- DBWriteContactSettingTString(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 filter[512], *pfilter;
- ofn.lStructSize=CDSIZEOF_STRUCT(OPENFILENAME,lpTemplateName);
- ofn.Flags=OFN_EXPLORER;
- ofn.lpstrTitle=TranslateW(WindowTittle);
- _tcscpy(filter,TranslateW(szExtDesc));
- pfilter=filter+_tcslen(filter)+1;
- _tcscpy(pfilter, szExt);
- pfilter[_tcslen(pfilter)+1] = '\0';
- pfilter[_tcslen(pfilter)+2] = '\0';
- ofn.lpstrFilter=filter;
- _tcscpy(str, _T(""));
- if(_tcslen(str)< 2)
- str[0] = '\0';
- 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;
- return NULL;
- }
- }
- return str;
-}
-
-void GetFolderPath(TCHAR *WindowTittle, char *szSetting)
-{
- BROWSEINFO pbi = {0};
- pbi.lpszTitle = WindowTittle;
- pbi.ulFlags = BIF_EDITBOX|BIF_NEWDIALOGSTYLE|BIF_SHAREABLE;
- LPITEMIDLIST pidl = SHBrowseForFolder(&pbi);
- if (pidl != 0)
- {
- TCHAR path[MAX_PATH];
- if (SHGetPathFromIDList(pidl, path))
- {
- DBWriteContactSettingTString(NULL, szGPGModuleName, "szHomePath", path);
- }
- IMalloc * imalloc = 0;
- if (SUCCEEDED(SHGetMalloc(&imalloc)))
- {
- imalloc->Free(pidl);
- imalloc->Release();
- }
- }
-}
-
-INT_PTR LoadKey(WPARAM w, LPARAM l)
-{
- void ShowLoadPublicKeyDialog();
- extern map<int, HANDLE> user_data;
- user_data[1] = (HANDLE)w;
- ShowLoadPublicKeyDialog();
- return 0;
-}
-
-INT_PTR SendKey(WPARAM w, LPARAM l)
-{
- HANDLE hContact = (HANDLE)w;
- if(metaIsProtoMetaContacts(hContact))
- hContact = metaGetMostOnline(hContact);
- char *szMessage;
- std::string key_id_str;
- {
- LPSTR proto = (LPSTR)CallService(MS_PROTO_GETCONTACTBASEPROTO, (WPARAM)hContact, 0);
- PROTOACCOUNT *acc = (PROTOACCOUNT*)CallService(MS_PROTO_GETACCOUNT, 0, (LPARAM)proto);
- std::string acc_str;
- if(acc)
- {
- acc_str = toUTF8(acc->tszAccountName);
- acc_str += "(";
- acc_str += acc->szModuleName;
- 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])
- {
- mir_free(szMessage);
- szMessage = UniGetContactSettingUtf(NULL, szGPGModuleName, "GPGPubKey", ""); //try to get default key as fallback in any way
- }
- }
- if(szMessage[0])
- {
- BYTE enc = DBGetContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", 0);
- DBWriteContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", 0);
- CallContactService(hContact, PSS_MESSAGE, (WPARAM)PREF_UTF, (LPARAM)szMessage);
- std::string msg = "Public key ";
- char *keyid = UniGetContactSettingUtf(NULL, szGPGModuleName, key_id_str.c_str(), "");
- if(!keyid[0])
- {
- mir_free(keyid);
- keyid = UniGetContactSettingUtf(NULL, szGPGModuleName, "KeyID", "");
- }
- msg += keyid;
- mir_free(keyid);
- msg += " sent";
- mir_free(szMessage);
- szMessage = mir_strdup(msg.c_str());
- HistoryLog(hContact, db_event(szMessage, 0, 0, DBEF_SENT));
- DBWriteContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", enc);
- }
- else
- mir_free(szMessage);
- return 0;
-}
-
-extern HANDLE hLoadPublicKey, hToggleEncryption, hSendKey;
-
-INT_PTR ToggleEncryption(WPARAM w, LPARAM l)
-{
- HANDLE hContact = (HANDLE)w;
- BYTE enc = 0;
- if(metaIsProtoMetaContacts(hContact))
- enc = DBGetContactSettingByte(metaGetMostOnline(hContact), szGPGModuleName, "GPGEncryption", 0);
- else
- enc = DBGetContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", 0);
- if(metaIsProtoMetaContacts(hContact))
- {
- HANDLE hcnt = NULL;
- if(MessageBox(0, TranslateT("Do you want to toggle encryption for all subcontacts ?"), TranslateT("Metacontact detected"), MB_YESNO) == IDYES)
- {
- int count = metaGetContactsNum(hContact);
- for(int i = 0; i < count; i++)
- {
- hcnt = metaGetSubcontact(hContact, i);
- if(hcnt)
- DBWriteContactSettingByte(hcnt, szGPGModuleName, "GPGEncryption", enc?0:1);
- }
- DBWriteContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", enc?0:1);
- }
- }
- else
- DBWriteContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", enc?0:1);
- void setSrmmIcon(HANDLE hContact);
- void setClistIcon(HANDLE hContact);
- setSrmmIcon(hContact);
- setClistIcon(hContact);
- enc = enc?0:1;
- CLISTMENUITEM mi = {0};
- mi.cbSize=sizeof(mi);
- mi.flags = CMIM_NAME;
- enc?mi.pszName="Turn off GPG encryption":mi.pszName="Turn on GPG encryption";
- CallService(MS_CLIST_MODIFYMENUITEM, (WPARAM)hToggleEncryption, (LPARAM)&mi);
- return 0;
-}
-
-int OnPreBuildContactMenu(WPARAM w, LPARAM l)
-{
- HANDLE hContact = (HANDLE)w;
- if(metaIsProtoMetaContacts(hContact))
- hContact = metaGetMostOnline(hContact);
-
- {
- CLISTMENUITEM mi2 = { sizeof(mi2) };
- LPSTR proto = (LPSTR)CallService(MS_PROTO_GETCONTACTBASEPROTO, (WPARAM)hContact, 0);
- PROTOACCOUNT *acc = (PROTOACCOUNT*)CallService(MS_PROTO_GETACCOUNT, 0, (LPARAM)proto);
- std::string setting;
- if(acc)
- {
- setting = toUTF8(acc->tszAccountName);
- setting += "(";
- setting += acc->szModuleName;
- setting += ")" ;
- setting += "_KeyID";
- }
- char *keyid = keyid = UniGetContactSettingUtf(NULL, szGPGModuleName, setting.c_str(), "");
- if(!keyid[0])
- {
- mir_free(keyid);
- keyid = UniGetContactSettingUtf(NULL, szGPGModuleName, "KeyID", "");
- }
- TCHAR buf[128] = {0};
- mir_sntprintf(buf, 127 * sizeof(TCHAR), _T("%s: %s"), TranslateT("Send publick key"), toUTF16(keyid).c_str());
- mi2.ptszName = buf;
- mi2.flags = CMIM_NAME | CMIF_TCHAR;
- CallService(MS_CLIST_MODIFYMENUITEM, (WPARAM)hSendKey, (LPARAM)&mi2);
- }
-
- CLISTMENUITEM mi = {0};
- mi.cbSize=sizeof(mi);
- mi.flags = CMIM_NAME;
- TCHAR *tmp = UniGetContactSettingUtf(hContact, szGPGModuleName, "GPGPubKey", _T(""));
- if(_tcslen(tmp) < 1)
- {
- DBDeleteContactSetting(hContact, szGPGModuleName, "GPGEncryption");
- mi.flags += CMIM_FLAGS | CMIF_GRAYED;
- }
- else
- mi.flags = CMIM_NAME | CMIM_FLAGS;
- mi.pszName = DBGetContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", 0)?"Turn off GPG encryption":"Turn on GPG encryption";
- CallService(MS_CLIST_MODIFYMENUITEM, (WPARAM)hToggleEncryption, (LPARAM)&mi);
- return 0;
-}
-
-
-list<wstring> transfers;
-
-DWORD file_msg_state = -1;
-
-int onProtoAck(WPARAM w, LPARAM l)
-{
- ACKDATA *ack=(ACKDATA*)l;
- CCSDATA *ccs=(CCSDATA*)ack->lParam;
-
- 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)
- {
- TCHAR *filename = NULL;
- if(f->flags & PFTS_UNICODE)
- {
- if(f->tszCurrentFile && f->tszCurrentFile[0])
- filename = mir_wstrdup(f->tszCurrentFile);
- if(!filename)
- return 0;
- }
- else
- {
- if(f->szCurrentFile && f->szCurrentFile[0])
- filename = mir_utf8decodeT(f->szCurrentFile);
- if(!filename)
- return 0;
- }
- if(_tcsstr(filename, _T(".gpg"))) //decrypt it
- { //process encrypted file
- if(!bFileTransfers && !bSameAction)
- {
- void ShowEncryptedFileMsgBox();
- ShowEncryptedFileMsgBox();
- }
- if(!bFileTransfers && bSameAction)
- return 0;
- if(file_msg_state < 1)
- return 0;
- HistoryLog(ack->hContact, db_event("Recieved encrypted file, trying to decrypt", 0,0, 0));
- if(!boost::filesystem::exists(f->tszCurrentFile))
- return 0;
- string out;
- DWORD code;
- pxResult result;
- std::vector<wstring> cmd;
- cmd.push_back(L"-o");
- wstring file = filename;
- wstring::size_type p1 = file.rfind(_T(".gpg"));
- file.erase(p1, _tcslen(_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)
- return 0;
- }
- cmd.push_back(file);
- boost::filesystem::remove(file);
- extern TCHAR *password;
- { // password
- TCHAR *pass = NULL;
- char *keyid = UniGetContactSettingUtf(ack->hContact, szGPGModuleName, "KeyID", "");
- if(strlen(keyid) > 0)
- {
- string dbsetting = "szKey_";
- dbsetting += keyid;
- dbsetting += "_Password";
- pass = UniGetContactSettingUtf(NULL, szGPGModuleName, dbsetting.c_str(), _T(""));
- if(_tcslen(pass) > 0 && bDebugLog)
- debuglog<<std::string(time_str()+": info: found password in database for key id: "+keyid+", trying to decrypt message from "+toUTF8((TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)ack->hContact, GCDNF_TCHAR))+" with password");
- }
- else
- {
- pass = UniGetContactSettingUtf(NULL, szGPGModuleName, "szKeyPassword", _T(""));
- if(_tcslen(pass) > 0 && bDebugLog)
- debuglog<<std::string(time_str()+": info: found password for all keys in database, trying to decrypt message from "+toUTF8((TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)ack->hContact, GCDNF_TCHAR))+" with password");
- }
- if(_tcslen(pass) > 0)
- {
- cmd.push_back(L"--passphrase");
- cmd.push_back(pass);
- }
- else if(password)
- {
- if(bDebugLog)
- debuglog<<std::string(time_str()+": info: found password in memory, trying to decrypt message from "+toUTF8((TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)ack->hContact, GCDNF_TCHAR))+" with password");
- cmd.push_back(L"--passphrase");
- cmd.push_back(password);
- }
- else if (bDebugLog)
- debuglog<<std::string(time_str()+": info: passwords not found in database or memory, trying to decrypt message from "+toUTF8((TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)ack->hContact, GCDNF_TCHAR))+" with out password");
- mir_free(pass);
- mir_free(keyid);
- }
- cmd.push_back(L"-d");
- cmd.push_back(filename);
- gpg_execution_params params(cmd);
- params.out = &out;
- params.code = &code;
- params.result = &result;
- if(!gpg_launcher(params, boost::posix_time::minutes(15)))
- return 0;
- while(out.find("public key decryption failed: bad passphrase") != string::npos)
- {
- extern bool _terminate;
- extern HANDLE new_key_hcnt;
- extern boost::mutex new_key_hcnt_mutex;
- if(bDebugLog)
- debuglog<<std::string(time_str()+": info: failed to decrypt messaage from "+toUTF8((TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)ack->hContact, GCDNF_TCHAR))+" 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 += strlen(" ID ");
- string::size_type s2 = out.find(",",s);
- if(metaIsProtoMetaContacts(ack->hContact))
- DBWriteContactSettingString(metaGetMostOnline(ack->hContact), szGPGModuleName, "InKeyID", out.substr(s, s2-s).c_str());
- else
- DBWriteContactSettingString(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<wstring> cmd2 = cmd;
- if(password)
- {
- if(bDebugLog)
- debuglog<<std::string(time_str()+": info: found password in memory, trying to decrypt message from "+toUTF8((TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)ack->hContact, GCDNF_TCHAR)));
- std::vector<wstring> 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);
- return 0;
- }
- if(result == pxNotFound)
- {
- //boost::filesystem::remove(filename);
- return 0;
- }
- }
- if(result == pxSuccess)
- boost::filesystem::remove(filename);
- mir_free(filename);
- }
- }
- }
- break;
- }
- }
- else if(ack->type == ACKTYPE_MESSAGE)
- {
- extern std::list<HANDLE> sent_msgs;
- if(!sent_msgs.empty())
- {
- if(ack->result == ACKRESULT_FAILED)
- {
- std::list<HANDLE>::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));
-
- }
- }
- else if(ack->result == ACKRESULT_SUCCESS)
- {
- std::list<HANDLE>::iterator it = std::find(sent_msgs.begin(), sent_msgs.end(), ack->hProcess);
- if(it != sent_msgs.end())
- sent_msgs.erase(it);
- }
- }
- }
- return 0;
-}
-
-std::wstring encrypt_file(HANDLE hContact, TCHAR *filename)
-{
- string out;
- DWORD code;
- pxResult result;
- HANDLE hcnt = metaIsProtoMetaContacts(hContact)?metaGetMostOnline(hContact):hContact;
- std::vector<wstring> 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 )
- name = filename;
- else
- name++;
- TCHAR *file_out = new TCHAR [_tcslen(filename)+4];
- mir_sntprintf(file_out, _tcslen(name)+7, _T("%s.gpg"), name);
- cmd.push_back(szKeyid);
- if(DBGetContactSettingByte(hcnt, szGPGModuleName, "bAlwaysTrust", 0))
- {
- cmd.push_back(L"--trust-model");
- cmd.push_back(L"always");
- }
- mir_free(szKeyid);
- mir_free(keyid);
- cmd.push_back(L"-o");
- TCHAR *temp = _tgetenv(_T("TEMP"));
- cmd.push_back(wstring(temp) + L"\\" + file_out);
- wstring path_out = temp;
- path_out += _T("\\");
- path_out += file_out;
- boost::filesystem::remove(path_out);
- cmd.push_back(L"-e");
- cmd.push_back(filename);
- gpg_execution_params params(cmd);
- params.out = &out;
- params.code = &code;
- params.result = &result;
- 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)
- {
- out.clear();
- if(MessageBox(0, TranslateT("We trying to encrypt with untrusted key, do you want to trust this key permanently ?"), TranslateT("Warning"), MB_YESNO) == IDYES)
- {
- DBWriteContactSettingByte(hcnt, szGPGModuleName, "bAlwaysTrust", 1);
- std::vector<std::wstring> 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)))
- return 0;
- }
- else
- return 0;
- }
- return path_out;
-}
-
-//from secureim partially
-int onSendFile(WPARAM w, LPARAM l)
-{
- if(!bFileTransfers)
- return CallService(MS_PROTO_CHAINSEND, w, l);
- CCSDATA *ccs=(CCSDATA*)l;
- if(isContactSecured(ccs->hContact))
- {
- LPSTR proto = (LPSTR)CallService(MS_PROTO_GETCONTACTBASEPROTO, (WPARAM)ccs->hContact, 0);
+// Copyright © 2010-2012 sss
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License
+// as published by the Free Software Foundation; either version 2
+// of the License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+
+#include "commonheaders.h"
+
+
+TCHAR* __stdcall UniGetContactSettingUtf(HANDLE hContact, const char *szModule,const char* szSetting, TCHAR* szDef)
+{
+ DBVARIANT dbv = {DBVT_DELETED};
+ TCHAR* szRes;
+ if (DBGetContactSettingTString(hContact, szModule, szSetting, &dbv))
+ return mir_tstrdup(szDef);
+ if(dbv.pszVal)
+ szRes = mir_tstrdup(dbv.ptszVal);
+ DBFreeVariant(&dbv);
+ return szRes;
+}
+
+char* __stdcall UniGetContactSettingUtf(HANDLE hContact, const char *szModule,const char* szSetting, char* szDef)
+{
+ DBVARIANT dbv = {DBVT_DELETED};
+ char* szRes;
+ if (DBGetContactSettingString(hContact, szModule, szSetting, &dbv))
+ return mir_strdup(szDef);
+ if(dbv.pszVal)
+ szRes = mir_strdup(dbv.pszVal);
+ DBFreeVariant(&dbv);
+ return szRes;
+}
+
+
+char *date()
+{
+ setlocale( LC_ALL, "C" );
+ static char d[11];
+ char *tmp = __DATE__, m[4], mn[3] = "01";
+ m[0]=tmp[0];
+ m[1]=tmp[1];
+ m[2]=tmp[2];
+ if(strstr(m,"Jan"))
+ strcpy(mn,"01");
+ else if(strstr(m,"Feb"))
+ strcpy(mn,"02");
+ else if(strstr(m,"Mar"))
+ strcpy(mn,"03");
+ else if(strstr(m,"Apr"))
+ strcpy(mn,"04");
+ else if(strstr(m,"May"))
+ strcpy(mn,"05");
+ else if(strstr(m,"Jun"))
+ strcpy(mn,"06");
+ else if(strstr(m,"Jul"))
+ strcpy(mn,"07");
+ else if(strstr(m,"Aug"))
+ strcpy(mn,"08");
+ else if(strstr(m,"Sep"))
+ strcpy(mn,"09");
+ else if(strstr(m,"Oct"))
+ strcpy(mn,"10");
+ else if(strstr(m,"Nov"))
+ strcpy(mn,"11");
+ else if(strstr(m,"Dec"))
+ strcpy(mn,"12");
+ d[0]=tmp[7];
+ d[1]=tmp[8];
+ d[2]=tmp[9];
+ d[3]=tmp[10];
+ d[4]='.';
+ d[5]=mn[0];
+ d[6]=mn[1];
+ d[7]='.';
+ if (tmp[4] == ' ')
+ d[8] = '0';
+ else
+ d[8]=tmp[4];
+ d[9]=tmp[5];
+ return d;
+}
+
+void GetFilePath(TCHAR *WindowTittle, char *szSetting, TCHAR *szExt, TCHAR *szExtDesc)
+{
+ 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);
+ _tcscpy(filter,TranslateW(szExtDesc));
+ pfilter=filter+_tcslen(filter)+1;
+ _tcscpy(pfilter, szExt);
+ pfilter[_tcslen(pfilter)+1] = '\0';
+ pfilter[_tcslen(pfilter)+2] = '\0';
+ ofn.lpstrFilter=filter;
+ tmp = UniGetContactSettingUtf(0, szGPGModuleName, szSetting, _T(""));
+ _tcscpy(str, tmp);
+ mir_free(tmp);
+ if(_tcslen(str)< 2)
+ str[0] = '\0';
+ ofn.lpstrFile=str;
+ ofn.nMaxFile=_MAX_PATH;
+ ofn.nMaxFileTitle=MAX_PATH;
+ if(!GetOpenFileName(&ofn))
+ return;
+ DBWriteContactSettingTString(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 filter[512], *pfilter;
+ ofn.lStructSize=CDSIZEOF_STRUCT(OPENFILENAME,lpTemplateName);
+ ofn.Flags=OFN_EXPLORER;
+ ofn.lpstrTitle=TranslateW(WindowTittle);
+ _tcscpy(filter,TranslateW(szExtDesc));
+ pfilter=filter+_tcslen(filter)+1;
+ _tcscpy(pfilter, szExt);
+ pfilter[_tcslen(pfilter)+1] = '\0';
+ pfilter[_tcslen(pfilter)+2] = '\0';
+ ofn.lpstrFilter=filter;
+ _tcscpy(str, _T(""));
+ if(_tcslen(str)< 2)
+ str[0] = '\0';
+ 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;
+ return NULL;
+ }
+ }
+ return str;
+}
+
+void GetFolderPath(TCHAR *WindowTittle, char *szSetting)
+{
+ BROWSEINFO pbi = {0};
+ pbi.lpszTitle = WindowTittle;
+ pbi.ulFlags = BIF_EDITBOX|BIF_NEWDIALOGSTYLE|BIF_SHAREABLE;
+ LPITEMIDLIST pidl = SHBrowseForFolder(&pbi);
+ if (pidl != 0)
+ {
+ TCHAR path[MAX_PATH];
+ if (SHGetPathFromIDList(pidl, path))
+ {
+ DBWriteContactSettingTString(NULL, szGPGModuleName, "szHomePath", path);
+ }
+ IMalloc * imalloc = 0;
+ if (SUCCEEDED(SHGetMalloc(&imalloc)))
+ {
+ imalloc->Free(pidl);
+ imalloc->Release();
+ }
+ }
+}
+
+INT_PTR LoadKey(WPARAM w, LPARAM l)
+{
+ void ShowLoadPublicKeyDialog();
+ extern map<int, HANDLE> user_data;
+ user_data[1] = (HANDLE)w;
+ ShowLoadPublicKeyDialog();
+ return 0;
+}
+
+INT_PTR SendKey(WPARAM w, LPARAM l)
+{
+ HANDLE hContact = (HANDLE)w;
+ if(metaIsProtoMetaContacts(hContact))
+ hContact = metaGetMostOnline(hContact);
+ char *szMessage;
+ std::string key_id_str;
+ {
+ LPSTR proto = (LPSTR)CallService(MS_PROTO_GETCONTACTBASEPROTO, (WPARAM)hContact, 0);
+ PROTOACCOUNT *acc = (PROTOACCOUNT*)CallService(MS_PROTO_GETACCOUNT, 0, (LPARAM)proto);
+ std::string acc_str;
+ if(acc)
+ {
+ acc_str = toUTF8(acc->tszAccountName);
+ acc_str += "(";
+ acc_str += acc->szModuleName;
+ 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])
+ {
+ mir_free(szMessage);
+ szMessage = UniGetContactSettingUtf(NULL, szGPGModuleName, "GPGPubKey", ""); //try to get default key as fallback in any way
+ }
+ }
+ if(szMessage[0])
+ {
+ BYTE enc = DBGetContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", 0);
+ DBWriteContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", 0);
+ CallContactService(hContact, PSS_MESSAGE, (WPARAM)PREF_UTF, (LPARAM)szMessage);
+ std::string msg = "Public key ";
+ char *keyid = UniGetContactSettingUtf(NULL, szGPGModuleName, key_id_str.c_str(), "");
+ if(!keyid[0])
+ {
+ mir_free(keyid);
+ keyid = UniGetContactSettingUtf(NULL, szGPGModuleName, "KeyID", "");
+ }
+ msg += keyid;
+ mir_free(keyid);
+ msg += " sent";
+ mir_free(szMessage);
+ szMessage = mir_strdup(msg.c_str());
+ HistoryLog(hContact, db_event(szMessage, 0, 0, DBEF_SENT));
+ DBWriteContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", enc);
+ }
+ else
+ mir_free(szMessage);
+ return 0;
+}
+
+extern HANDLE hLoadPublicKey, hToggleEncryption, hSendKey;
+
+INT_PTR ToggleEncryption(WPARAM w, LPARAM l)
+{
+ HANDLE hContact = (HANDLE)w;
+ BYTE enc = 0;
+ if(metaIsProtoMetaContacts(hContact))
+ enc = DBGetContactSettingByte(metaGetMostOnline(hContact), szGPGModuleName, "GPGEncryption", 0);
+ else
+ enc = DBGetContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", 0);
+ if(metaIsProtoMetaContacts(hContact))
+ {
+ HANDLE hcnt = NULL;
+ if(MessageBox(0, TranslateT("Do you want to toggle encryption for all subcontacts ?"), TranslateT("Metacontact detected"), MB_YESNO) == IDYES)
+ {
+ int count = metaGetContactsNum(hContact);
+ for(int i = 0; i < count; i++)
+ {
+ hcnt = metaGetSubcontact(hContact, i);
+ if(hcnt)
+ DBWriteContactSettingByte(hcnt, szGPGModuleName, "GPGEncryption", enc?0:1);
+ }
+ DBWriteContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", enc?0:1);
+ }
+ }
+ else
+ DBWriteContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", enc?0:1);
+ void setSrmmIcon(HANDLE hContact);
+ void setClistIcon(HANDLE hContact);
+ setSrmmIcon(hContact);
+ setClistIcon(hContact);
+ enc = enc?0:1;
+ CLISTMENUITEM mi = {0};
+ mi.cbSize=sizeof(mi);
+ mi.flags = CMIM_NAME;
+ enc?mi.pszName="Turn off GPG encryption":mi.pszName="Turn on GPG encryption";
+ CallService(MS_CLIST_MODIFYMENUITEM, (WPARAM)hToggleEncryption, (LPARAM)&mi);
+ return 0;
+}
+
+int OnPreBuildContactMenu(WPARAM w, LPARAM l)
+{
+ HANDLE hContact = (HANDLE)w;
+ if(metaIsProtoMetaContacts(hContact))
+ hContact = metaGetMostOnline(hContact);
+
+ {
+ CLISTMENUITEM mi2 = { sizeof(mi2) };
+ LPSTR proto = (LPSTR)CallService(MS_PROTO_GETCONTACTBASEPROTO, (WPARAM)hContact, 0);
+ PROTOACCOUNT *acc = (PROTOACCOUNT*)CallService(MS_PROTO_GETACCOUNT, 0, (LPARAM)proto);
+ std::string setting;
+ if(acc)
+ {
+ setting = toUTF8(acc->tszAccountName);
+ setting += "(";
+ setting += acc->szModuleName;
+ setting += ")" ;
+ setting += "_KeyID";
+ }
+ char *keyid = UniGetContactSettingUtf(NULL, szGPGModuleName, setting.c_str(), "");
+ if(!keyid[0])
+ {
+ mir_free(keyid);
+ keyid = UniGetContactSettingUtf(NULL, szGPGModuleName, "KeyID", "");
+ }
+ TCHAR buf[128] = {0};
+ mir_sntprintf(buf, sizeof(buf), _T("%s: %s"), TranslateT("Send public key"), toUTF16(keyid).c_str());
+ mir_free(keyid);
+ mi2.ptszName = buf;
+ mi2.flags = CMIM_NAME | CMIF_TCHAR;
+ CallService(MS_CLIST_MODIFYMENUITEM, (WPARAM)hSendKey, (LPARAM)&mi2);
+ }
+
+ CLISTMENUITEM mi = {0};
+ mi.cbSize=sizeof(mi);
+ mi.flags = CMIM_NAME;
+ TCHAR *tmp = UniGetContactSettingUtf(hContact, szGPGModuleName, "GPGPubKey", _T(""));
+ if(_tcslen(tmp) < 1)
+ {
+ DBDeleteContactSetting(hContact, szGPGModuleName, "GPGEncryption");
+ mi.flags += CMIM_FLAGS | CMIF_GRAYED;
+ }
+ else
+ mi.flags = CMIM_NAME | CMIM_FLAGS;
+ mi.pszName = DBGetContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", 0)?"Turn off GPG encryption":"Turn on GPG encryption";
+ CallService(MS_CLIST_MODIFYMENUITEM, (WPARAM)hToggleEncryption, (LPARAM)&mi);
+ return 0;
+}
+
+
+list<wstring> transfers;
+
+DWORD file_msg_state = -1;
+
+int onProtoAck(WPARAM w, LPARAM l)
+{
+ ACKDATA *ack=(ACKDATA*)l;
+ CCSDATA *ccs=(CCSDATA*)ack->lParam;
+
+ 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)
+ {
+ TCHAR *filename = NULL;
+ if(f->flags & PFTS_UNICODE)
+ {
+ if(f->tszCurrentFile && f->tszCurrentFile[0])
+ filename = mir_wstrdup(f->tszCurrentFile);
+ if(!filename)
+ return 0;
+ }
+ else
+ {
+ if(f->szCurrentFile && f->szCurrentFile[0])
+ filename = mir_utf8decodeT(f->szCurrentFile);
+ if(!filename)
+ return 0;
+ }
+ if(_tcsstr(filename, _T(".gpg"))) //decrypt it
+ { //process encrypted file
+ if(!bFileTransfers && !bSameAction)
+ {
+ void ShowEncryptedFileMsgBox();
+ ShowEncryptedFileMsgBox();
+ }
+ if(!bFileTransfers && bSameAction)
+ return 0;
+ 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))
+ return 0;
+ string out;
+ DWORD code;
+ pxResult result;
+ std::vector<wstring> cmd;
+ cmd.push_back(L"-o");
+ wstring file = filename;
+ wstring::size_type p1 = file.rfind(_T(".gpg"));
+ file.erase(p1, _tcslen(_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)
+ return 0;
+ }
+ cmd.push_back(file);
+ boost::filesystem::remove(file);
+ extern TCHAR *password;
+ { // password
+ TCHAR *pass = NULL;
+ char *keyid = UniGetContactSettingUtf(ack->hContact, szGPGModuleName, "KeyID", "");
+ if(strlen(keyid) > 0)
+ {
+ string dbsetting = "szKey_";
+ dbsetting += keyid;
+ dbsetting += "_Password";
+ pass = UniGetContactSettingUtf(NULL, szGPGModuleName, dbsetting.c_str(), _T(""));
+ if(_tcslen(pass) > 0 && bDebugLog)
+ debuglog<<std::string(time_str()+": info: found password in database for key id: "+keyid+", trying to decrypt message from "+toUTF8((TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)ack->hContact, GCDNF_TCHAR))+" with password");
+ }
+ else
+ {
+ pass = UniGetContactSettingUtf(NULL, szGPGModuleName, "szKeyPassword", _T(""));
+ if(_tcslen(pass) > 0 && bDebugLog)
+ debuglog<<std::string(time_str()+": info: found password for all keys in database, trying to decrypt message from "+toUTF8((TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)ack->hContact, GCDNF_TCHAR))+" with password");
+ }
+ if(_tcslen(pass) > 0)
+ {
+ cmd.push_back(L"--passphrase");
+ cmd.push_back(pass);
+ }
+ else if(password)
+ {
+ if(bDebugLog)
+ debuglog<<std::string(time_str()+": info: found password in memory, trying to decrypt message from "+toUTF8((TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)ack->hContact, GCDNF_TCHAR))+" with password");
+ cmd.push_back(L"--passphrase");
+ cmd.push_back(password);
+ }
+ else if (bDebugLog)
+ debuglog<<std::string(time_str()+": info: passwords not found in database or memory, trying to decrypt message from "+toUTF8((TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)ack->hContact, GCDNF_TCHAR))+" with out password");
+ mir_free(pass);
+ mir_free(keyid);
+ }
+ cmd.push_back(L"-d");
+ cmd.push_back(filename);
+ gpg_execution_params params(cmd);
+ params.out = &out;
+ params.code = &code;
+ params.result = &result;
+ if(!gpg_launcher(params, boost::posix_time::minutes(15)))
+ return 0;
+ while(out.find("public key decryption failed: bad passphrase") != string::npos)
+ {
+ extern bool _terminate;
+ extern HANDLE new_key_hcnt;
+ extern boost::mutex new_key_hcnt_mutex;
+ if(bDebugLog)
+ debuglog<<std::string(time_str()+": info: failed to decrypt messaage from "+toUTF8((TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)ack->hContact, GCDNF_TCHAR))+" 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 += strlen(" ID ");
+ string::size_type s2 = out.find(",",s);
+ if(metaIsProtoMetaContacts(ack->hContact))
+ DBWriteContactSettingString(metaGetMostOnline(ack->hContact), szGPGModuleName, "InKeyID", out.substr(s, s2-s).c_str());
+ else
+ DBWriteContactSettingString(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<wstring> cmd2 = cmd;
+ if(password)
+ {
+ if(bDebugLog)
+ debuglog<<std::string(time_str()+": info: found password in memory, trying to decrypt message from "+toUTF8((TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)ack->hContact, GCDNF_TCHAR)));
+ std::vector<wstring> 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);
+ return 0;
+ }
+ if(result == pxNotFound)
+ {
+ //boost::filesystem::remove(filename);
+ return 0;
+ }
+ }
+ if(result == pxSuccess)
+ boost::filesystem::remove(filename);
+ mir_free(filename);
+ }
+ }
+ }
+ break;
+ }
+ }
+ else if(ack->type == ACKTYPE_MESSAGE)
+ {
+ extern std::list<HANDLE> sent_msgs;
+ if(!sent_msgs.empty())
+ {
+ if(ack->result == ACKRESULT_FAILED)
+ {
+ std::list<HANDLE>::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));
+
+ }
+ }
+ else if(ack->result == ACKRESULT_SUCCESS)
+ {
+ std::list<HANDLE>::iterator it = std::find(sent_msgs.begin(), sent_msgs.end(), ack->hProcess);
+ if(it != sent_msgs.end())
+ sent_msgs.erase(it);
+ }
+ }
+ }
+ return 0;
+}
+
+std::wstring encrypt_file(HANDLE hContact, TCHAR *filename)
+{
+ string out;
+ DWORD code;
+ pxResult result;
+ HANDLE hcnt = metaIsProtoMetaContacts(hContact)?metaGetMostOnline(hContact):hContact;
+ std::vector<wstring> 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 )
+ name = filename;
+ else
+ name++;
+ TCHAR *file_out = new TCHAR [_tcslen(name) + _tcslen(_T(".gpg")) + 1];
+ mir_sntprintf(file_out, _tcslen(name) + _tcslen(_T(".gpg")) + 1, _T("%s.gpg"), name);
+ cmd.push_back(szKeyid);
+ if(DBGetContactSettingByte(hcnt, szGPGModuleName, "bAlwaysTrust", 0))
+ {
+ cmd.push_back(L"--trust-model");
+ cmd.push_back(L"always");
+ }
+ mir_free(szKeyid);
+ mir_free(keyid);
+ cmd.push_back(L"-o");
+ TCHAR *temp = _tgetenv(_T("TEMP"));
+ cmd.push_back(wstring(temp) + L"\\" + file_out);
+ wstring path_out = temp;
+ path_out += _T("\\");
+ path_out += file_out;
+ boost::filesystem::remove(path_out);
+ cmd.push_back(L"-e");
+ cmd.push_back(filename);
+ gpg_execution_params params(cmd);
+ params.out = &out;
+ params.code = &code;
+ params.result = &result;
+ 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)
+ {
+ 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)
+ {
+ DBWriteContactSettingByte(hcnt, szGPGModuleName, "bAlwaysTrust", 1);
+ std::vector<std::wstring> 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)))
+ return 0;
+ }
+ else
+ return 0;
+ }
+ return path_out;
+}
+
+//from secureim partially
+int onSendFile(WPARAM w, LPARAM l)
+{
+ if(!bFileTransfers)
+ return CallService(MS_PROTO_CHAINSEND, w, l);
+ CCSDATA *ccs=(CCSDATA*)l;
+ if(isContactSecured(ccs->hContact))
+ {
+ LPSTR proto = (LPSTR)CallService(MS_PROTO_GETCONTACTBASEPROTO, (WPARAM)ccs->hContact, 0);
DWORD uin = DBGetContactSettingDword(ccs->hContact, proto, "UIN", 0);
bool cap_found = false, supported_proto = false;
if(uin)
@@ -636,1549 +637,1556 @@ int onSendFile(WPARAM w, LPARAM l)
}
}
}
+ 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("Filetransfer warning"), MB_YESNO) == IDNO)
+ 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("Filetransfer warning"), MB_YESNO) == IDNO)
return CallService(MS_PROTO_CHAINSEND, w, l);
}
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 used on other side.\nDo you want to encrypt file(s) anyway?"), TranslateT("Filetransfer warning"), MB_YESNO) == IDNO)
+ 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("Filetransfer warning"), MB_YESNO) == IDNO)
return CallService(MS_PROTO_CHAINSEND, w, l);
}
- HistoryLog(ccs->hContact, db_event(Translate("encrypting file for transfer"), 0, 0, DBEF_SENT));
- DWORD flags = (DWORD)ccs->wParam; //check for PFTS_UNICODE here
- int i;
-// if(flags & PFTS_UNICODE) //this does not work ....
- if(StriStr(ccs->szProtoService, "/sendfilew"))
- {
- TCHAR **file=(TCHAR **)ccs->lParam;
- for(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")))
- continue;
- std::wstring path_out = encrypt_file(ccs->hContact, file[i]);
- mir_free(file[i]);
- file[i] = mir_tstrdup(path_out.c_str());
- transfers.push_back(path_out);
- }
- }
- else
- {
- char **file = (char**) ccs->lParam;
- for(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"))
- continue;
- TCHAR *tmp = mir_utf8decodeT(file[i]);
- std::wstring path_out = encrypt_file(ccs->hContact, tmp);
- mir_free(tmp);
- char* tmp2 = mir_utf8encodeW(path_out.c_str());
- mir_free(file[i]);
- file[i] = tmp2;
- transfers.push_back(path_out);
-
- }
- }
- }
- return CallService(MS_PROTO_CHAINSEND, w, l);
-}
-
-
-void HistoryLog(HANDLE hContact, db_event evt)
-{
- DBEVENTINFO Event = {0};
- Event.cbSize = sizeof(Event);
- Event.szModule = szGPGModuleName;
- Event.eventType = evt.eventType;
- Event.flags = evt.flags;
- if(!evt.timestamp)
- Event.timestamp = (DWORD)time(NULL);
- else
- Event.timestamp = evt.timestamp;
- Event.cbBlob = strlen((char*)evt.pBlob)+1;
- Event.pBlob = (PBYTE)_strdup((char*)evt.pBlob);
- CallService(MS_DB_EVENT_ADD, (WPARAM)(HANDLE)hContact,(LPARAM)&Event);
-}
-
-static int ControlAddStringUtf(HWND ctrl, DWORD msg, const TCHAR *szString)
-{
- int item = -1;
- item = SendMessage(ctrl, msg, 0, (LPARAM)szString);
- return item;
-}
-
-int ComboBoxAddStringUtf(HWND hCombo, const TCHAR *szString, DWORD data)
-{
- int item = ControlAddStringUtf(hCombo, CB_ADDSTRING, szString);
- SendMessage(hCombo, CB_SETITEMDATA, item, data);
-
- return item;
-}
-
-
-int GetJabberInterface(WPARAM w, LPARAM l) //get interface for all jabber accounts, options later
-{
- extern list <JabberAccount*> Accounts;
- void AddHandlers();
- int count = 0;
- PROTOACCOUNT **accounts;
- ProtoEnumAccounts(&count, &accounts);
- list <JabberAccount*>::iterator p;
- Accounts.clear();
- 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
- {
- IJabberInterface *JIftmp = getJabberApi(accounts[i]->szModuleName);
- int a = 0;
- if(JIftmp)
- {
- (*p)->setJabberInterface(JIftmp);
- if(accounts[i]->tszAccountName)
- {
- TCHAR* tmp = mir_tstrdup(accounts[i]->tszAccountName);
- (*p)->setAccountName(tmp);
- }
- else
- {
- TCHAR *tmp = mir_a2t(accounts[i]->szModuleName);
- (*p)->setAccountName(tmp);
- }
- (*p)->setAccountNumber(a);
- a++;
- Accounts.push_back(new JabberAccount);
- p++;
- }
- }
- Accounts.pop_back();
- AddHandlers();
- return 0;
-}
-
-static JABBER_HANDLER_FUNC SendHandler(IJabberInterface *ji, HXML node, void *pUserData)
-{
- HXML local_node = node;
- for(int n = 0; n <= xi.getChildCount(node); n++)
- {
- LPCTSTR str = xi.getText(local_node);
- LPCTSTR nodename = xi.getName(local_node);
- LPCTSTR attr = xi.getAttrValue(local_node, _T("to"));
- if(attr)
- {
- HANDLE hContact = ji->Sys()->ContactFromJID(attr);
- if(hContact)
- if(!isContactSecured(hContact))
- return FALSE;
- }
- if(str)
- {
- if(_tcsstr(str, _T("-----BEGIN PGP MESSAGE-----")) && _tcsstr(str, _T("-----END PGP MESSAGE-----")))
- {
- wstring data = str;
- xi.setText(local_node, _T("This message is encrypted."));
- wstring::size_type p1 = data.find(_T("-----BEGIN PGP MESSAGE-----")) + _tcslen(_T("-----BEGIN PGP MESSAGE-----"));
- 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)
- {
- p1 = data.find(_T("Comment: "), p1);
- p1 = data.find(_T("\n"), p1);
- }
- while(data.find(_T("Encoding: "), p1) != wstring::npos)
- {
- p1 = data.find(_T("Encoding: "), p1);
- p1 = data.find(_T("\n"), p1);
- }
- p1+=3;
- wstring::size_type p2 = data.find(_T("-----END PGP MESSAGE-----"));
- wstring data2 = data.substr(p1, p2-p1-2);
- strip_line_term(data2);
- HXML encrypted_data = xi.addChild(node, _T("x"), data2.c_str());
- xi.addAttr(encrypted_data, _T("xmlns"), _T("jabber:x:encrypted"));
- return FALSE;
- }
- }
- 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));
- mir_free(path_c);
- path_out += _T("\\tmp\\");
- path_out += file;
- boost::filesystem::remove(path_out);
- wfstream f(path_out.c_str(), std::ios::out);
- f<<toUTF8(str).c_str();
- f.close();
- if(!boost::filesystem::exists(path_out))
- {
- if(bDebugLog)
- debuglog<<std::string(time_str()+": info: Failed to write prescense in file");
- return FALSE;
- }
- {
- extern TCHAR *password;
- string out;
- DWORD code;
- std::vector<wstring> cmd;
- {
- char *inkeyid;
- {
- char *proto = ji->Sys()->GetModuleName();
- char setting[64];
- strcpy(setting, proto);
- strcat(setting, "_KeyID");
- inkeyid = UniGetContactSettingUtf(NULL, szGPGModuleName, setting, "");
- if(!inkeyid[0])
- {
- mir_free(inkeyid);
- inkeyid = UniGetContactSettingUtf(NULL, szGPGModuleName, "KeyID", "");
- }
- }
- TCHAR *pass = NULL;
- if(inkeyid[0])
- {
- string dbsetting = "szKey_";
- dbsetting += inkeyid;
- dbsetting += "_Password";
- pass = UniGetContactSettingUtf(NULL, szGPGModuleName, dbsetting.c_str(), _T(""));
- if(pass[0] && bDebugLog)
- debuglog<<std::string(time_str()+": info: found password in database for key id: "+inkeyid+", trying to encrypt message from self with password");
- }
- else
- {
- pass = UniGetContactSettingUtf(NULL, szGPGModuleName, "szKeyPassword", _T(""));
- if(pass[0] && bDebugLog)
- debuglog<<std::string(time_str()+": info: found password for all keys in database, trying to encrypt message from self with password");
- }
- if(pass[0])
- {
- cmd.push_back(L"--passphrase");
- cmd.push_back(pass);
- }
- else if(password)
- {
- if(bDebugLog)
- debuglog<<std::string(time_str()+": info: found password in memory, trying to encrypt message from self with password");
- cmd.push_back(L"--passphrase");
- cmd.push_back(password);
- }
- else if (bDebugLog)
- debuglog<<std::string(time_str()+": info: passwords not found in database or memory, trying to encrypt message from self with out password");
- mir_free(pass);
- mir_free(inkeyid);
- }
- cmd.push_back(L"--local-user");
- path_c = UniGetContactSettingUtf(NULL, szGPGModuleName, "KeyID", _T(""));
- cmd.push_back(path_c);
- cmd.push_back(L"--default-key");
- cmd.push_back(path_c);
- mir_free(path_c);
- cmd.push_back(L"--batch");
- cmd.push_back(L"--yes");
- cmd.push_back(L"-abs");
- cmd.push_back(path_out);
- gpg_execution_params params(cmd);
- pxResult result;
- params.out = &out;
- params.code = &code;
- params.result = &result;
- gpg_launcher(params, boost::posix_time::seconds(15)); // TODO: handle errors
- boost::filesystem::remove(path_out);
- path_out += _T(".asc");
- f.open(path_out.c_str(), std::ios::in | std::ios::ate | std::ios::binary);
- wstring data;
- if(f.is_open())
- {
- std::wifstream::pos_type size = f.tellg();
- TCHAR *tmp = new TCHAR [(std::ifstream::pos_type)size+(std::ifstream::pos_type)1];
- f.seekg(0, std::ios::beg);
- f.read(tmp, size);
- tmp[size]= '\0';
- data.append(tmp);
- delete [] tmp;
- f.close();
- boost::filesystem::remove(path_out);
- }
- if(data.empty())
- {
- if(bDebugLog)
- debuglog<<std::string(time_str()+": info: Failed to read prescense sign from file");
- return FALSE;
- }
- if(data.find(_T("-----BEGIN PGP SIGNATURE-----")) != wstring::npos && data.find(_T("-----END PGP SIGNATURE-----")) != wstring::npos)
- {
- wstring::size_type p1 = data.find(_T("-----BEGIN PGP SIGNATURE-----")) + _tcslen(_T("-----BEGIN PGP SIGNATURE-----"));
- if(data.find(_T("Version: "), p1) != wstring::npos)
- {
- p1 = data.find(_T("Version: "), p1);
- p1 = data.find(_T("\n"), p1);
- if(data.find(_T("Version: "), p1) != wstring::npos)
- {
- p1 = data.find(_T("Version: "), p1);
- p1 = data.find(_T("\n"), p1)+1;
- }
- else
- p1 += 1;
- }
- if(data.find(_T("Comment: "), p1) != wstring::npos)
- {
- p1 = data.find(_T("Comment: "), p1);
- p1 = data.find(_T("\n"), p1);
- if(data.find(_T("Comment: "), p1) != wstring::npos)
- {
- p1 = data.find(_T("Comment: "), p1);
- p1 = data.find(_T("\n"), p1)+1;
- }
- else
- p1 += 1;
- }
- else
- p1+=1;
- p1++;
- wstring::size_type p2 = data.find(_T("-----END PGP SIGNATURE-----"));
- {
- std::wstring tmp = data.substr(p1, p2-p1);
- strip_line_term(tmp);
- HXML encrypted_data = xi.addChild(node, _T("x"), tmp.c_str());
- xi.addAttr(encrypted_data, _T("xmlns"), _T("jabber:x:signed"));
- }
- }
- return FALSE;
- }
- }
- }
- local_node = xi.getChild(node, n);
- }
- return FALSE;
-}
-
-//boost::mutex sign_file_mutex;
-
-static JABBER_HANDLER_FUNC PrescenseHandler(IJabberInterface *ji, HXML node, void *pUserData)
-{
- HXML local_node = node;
- for(int n = 0; n <= xi.getChildCount(node); n++)
- {
- LPCTSTR str = xi.getText(local_node);
- LPCTSTR nodename = xi.getName(local_node);
- if(nodename)
- {
- if(_tcsstr(nodename, _T("x")))
- {
- for(int n = 0; n < xi.getAttrCount(local_node); n++)
- {
- LPCTSTR name = xi.getAttrName(local_node, n);
- LPCTSTR value = xi.getAttrValue(local_node, name);
- if(_tcsstr(value, _T("jabber:x:signed")))
- {
- std::wstring status_str;
- HXML local_node2 = node;
- for(int n = 0; n <= xi.getChildCount(node); n++)
- {
- LPCTSTR nodename2 = xi.getName(local_node2);
- if(_tcsstr(nodename2, _T("status")))
- {
- LPCTSTR status = xi.getText(local_node2);
- if(status)
- status_str = status;
- break;
- }
- local_node2 = xi.getChild(node, n);
- }
- LPCTSTR data = xi.getText(local_node);
- wstring sign = _T("-----BEGIN PGP SIGNATURE-----\n\n");
- wstring file = toUTF16(get_random(10)), status_file = toUTF16(get_random(10));
- sign += data;
- sign += _T("\n-----END PGP SIGNATURE-----\n");
- TCHAR *path_c = UniGetContactSettingUtf(NULL, szGPGModuleName, "szHomePath", _T(""));
- wstring path_out = path_c, status_file_out = path_c;
- mir_free(path_c);
- path_out += L"\\tmp\\";
- path_out += file;
- path_out += L".sig";
- status_file_out += L"\\tmp\\";
- status_file_out += status_file;
- status_file_out += L".status";
-// sign_file_mutex.lock();
- boost::filesystem::remove(path_out);
- boost::filesystem::remove(status_file_out);
- wfstream f(path_out.c_str(), std::ios::out);
- while(!f.is_open())
- f.open(path_out.c_str(), std::ios::out);
- f<<toUTF8(sign).c_str();
- f.close();
- f.open(status_file_out.c_str(), std::ios::out);
- while(!f.is_open())
- f.open(status_file_out.c_str(), std::ios::out);
- f<<toUTF8(status_str).c_str();
- f.close();
- if(!boost::filesystem::exists(path_out))
- {
-// sign_file_mutex.unlock();
- if(bDebugLog)
- debuglog<<std::string(time_str()+": info: Failed to write sign in file");
- return FALSE;
- }
- { //gpg
- string out;
- DWORD code;
- std::vector<wstring> cmd;
- cmd.push_back(L"--verify");
- cmd.push_back(L"-a");
- cmd.push_back(path_out);
- cmd.push_back(status_file_out);
- gpg_execution_params params(cmd);
- pxResult result;
- params.out = &out;
- params.code = &code;
- params.result = &result;
- if(!gpg_launcher(params, boost::posix_time::seconds(15)))
- {
- return FALSE;
- }
- if(result == pxNotFound)
- {
- return FALSE;
- }
- boost::filesystem::remove(path_out);
- boost::filesystem::remove(status_file_out);
- if(out.find("key ID ") != string::npos)
- {
- //need to get hcontact here, i can get jid from hxml, and get handle from jid, maybe exists better way ?
- string::size_type p1 = out.find("key ID ") + strlen("key ID ");
- string::size_type p2 = out.find("\n", p1);
- if(p1 != string::npos && p2 != string::npos)
- {
- HANDLE hContact = NULL;
- {
- extern list <JabberAccount*> Accounts;
- list <JabberAccount*>::iterator p = Accounts.begin();
- for(unsigned int i = 0; i < Accounts.size(); i++, p++)
- {
- if(!(*p))
- break;
- hContact = (*p)->getJabberInterface()->Sys()->ContactFromJID(xi.getAttrValue(node, _T("from")));
- if(hContact)
- hcontact_data[hContact].key_in_prescense = out.substr(p1, p2-p1-1).c_str();
- }
- }
- }
- }
- }
- return FALSE;
- }
- }
- }
- }
- local_node = xi.getChild(node, n);
- }
- return FALSE;
-}
-
-static JABBER_HANDLER_FUNC MessageHandler(IJabberInterface *ji, HXML node, void *pUserData)
-{
- return FALSE;
-}
-
-
-
-
-void AddHandlers()
-{
- extern list<JabberAccount*> Accounts;
- list<JabberAccount*>::iterator end = Accounts.end();
- for(list<JabberAccount*>::iterator p = Accounts.begin(); p != end; p++)
- {
- if(!(*p))
- break;
- if((*p)->getSendHandler() == INVALID_HANDLE_VALUE)
- (*p)->setSendHandler((*p)->getJabberInterface()->Net()->AddSendHandler((JABBER_HANDLER_FUNC)SendHandler));
- if((*p)->getPrescenseHandler() == INVALID_HANDLE_VALUE)
- (*p)->setPrescenseHandler((*p)->getJabberInterface()->Net()->AddPresenceHandler((JABBER_HANDLER_FUNC)PrescenseHandler));
-// if((*p)->getMessageHandler() == INVALID_HANDLE_VALUE)
-// (*p)->setMessageHandler((*p)->getJabberInterface()->Net()->AddMessageHandler((JABBER_HANDLER_FUNC)MessageHandler, JABBER_MESSAGE_TYPE_ANY ,NULL,NULL));
- if(bAutoExchange)
- {
- (*p)->getJabberInterface()->Net()->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()->Net()->AddFeatures(_T("GPG_Key_Auto_Exchange:0\0\0"));
- }
- if(bFileTransfers)
- {
- (*p)->getJabberInterface()->Net()->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()->Net()->AddFeatures(_T("GPG_Encrypted_FileTransfers:0\0\0"));
- }
- }
-}
-
-bool isContactSecured(HANDLE hContact)
-{
- BYTE gpg_enc = DBGetContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", 0);
- if(!gpg_enc)
- {
- if(bDebugLog)
- debuglog<<std::string(time_str()+": encryption is turned off for "+toUTF8((TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)hContact, GCDNF_TCHAR)));
- return false;
- }
- if(!metaIsProtoMetaContacts(hContact))
- {
- TCHAR *key = UniGetContactSettingUtf(hContact, szGPGModuleName, "GPGPubKey", _T(""));
- if(!key[0])
- {
- mir_free(key);
- if(bDebugLog)
- debuglog<<std::string(time_str()+": encryption is turned off for "+toUTF8((TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)hContact, GCDNF_TCHAR)));
- return false;
- }
- mir_free(key);
- }
- if(bDebugLog)
- debuglog<<std::string(time_str()+": encryption is turned on for "+toUTF8((TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)hContact, GCDNF_TCHAR)));
- return true;
-}
-
-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;
-}
-
-bool isGPGKeyExist()
-{
- TCHAR *id = UniGetContactSettingUtf(NULL, szGPGModuleName, "KeyID", _T(""));
- char *key = UniGetContactSettingUtf(NULL, szGPGModuleName, "GPGPubKey", "");
- if(id[0] && key[0])
- {
- mir_free(id);
- mir_free(key);
- return true;
- }
- mir_free(id);
- 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 = (TCHAR*)mir_alloc(sizeof(TCHAR)*MAX_PATH);
- char *mir_path = (char*)mir_alloc(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)+64)*sizeof(TCHAR));
- TCHAR *gpg_path = (TCHAR*)mir_alloc(sizeof(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_tstrdup(path);
- mir_free(path);
- }
- DWORD len = MAX_PATH;
- if(gpg_exists)
- {
- DBWriteContactSettingTString(NULL, szGPGModuleName, "szGpgBinPath", tmp);
- string out;
- DWORD code;
- std::vector<wstring> cmd;
- cmd.push_back(L"--version");
- gpg_execution_params params(cmd);
- pxResult result;
- params.out = &out;
- params.code = &code;
- params.result = &result;
- gpg_valid = true;
- gpg_launcher(params);
- gpg_valid = false;
- string::size_type p1 = out.find("(GnuPG) ");
- if(p1 == string::npos)
- is_valid = false;
- }
- 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);
- return is_valid;
-}
-
-#define NEWTSTR_MALLOC(A) (A==NULL)?NULL:strcpy((char*)mir_alloc(sizeof(char)*(strlen(A)+1)),A)
-
-const bool StriStr(const char *str, const char *substr)
-{
- bool i = false;
- char *str_up = NEWTSTR_MALLOC(str);
- char *substr_up = NEWTSTR_MALLOC(substr);
-
- CharUpperBuffA(str_up, strlen(str_up));
- CharUpperBuffA(substr_up, strlen(substr_up));
-
- if(strstr (str_up, substr_up))
- i = true;
-
- mir_free(str_up);
- mir_free(substr_up);
-
- return i;
-}
-
-bool IsOnline(HANDLE hContact)
-{
- if(DBGetContactSettingByte(hContact, szGPGModuleName, "Status", 0) == ID_STATUS_OFFLINE)
- return false;
- return true;
-}
-
-//from secureim
-#include <process.h>
-
-struct TFakeAckParams {
- inline TFakeAckParams( HANDLE p1, HANDLE p2, LONG p3, LPCSTR p4 ) :
- hEvent( p1 ),
- hContact( p2 ),
- id( p3 ),
- msg( p4 )
- {}
-
- HANDLE hEvent;
- HANDLE hContact;
- LONG id;
- LPCSTR msg;
-};
-
-int SendBroadcast( HANDLE hContact, int type, int result, HANDLE hProcess, LPARAM lParam ) {
- ACKDATA ack;
- memset(&ack,0,sizeof(ack));
- ack.cbSize = sizeof( ACKDATA );
- ack.szModule = szGPGModuleName;// (char*)CallService(MS_PROTO_GETCONTACTBASEPROTO, (WPARAM)hContact, 0);
- ack.hContact = hContact;
- ack.type = type;
- ack.result = result;
- ack.hProcess = (HANDLE)777;//hProcess;
- ack.lParam = lParam;
- return CallService( MS_PROTO_BROADCASTACK, 0, ( LPARAM )&ack );
-}
-
-
-
-unsigned __stdcall sttFakeAck( LPVOID param ) {
-
- 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 );
- else
- SendBroadcast( tParam->hContact, ACKTYPE_MESSAGE, ACKRESULT_FAILED, ( HANDLE )tParam->id, LPARAM( tParam->msg ));
-
- CloseHandle( tParam->hEvent );
- delete tParam;
-
- return 0;
-}
-
-
-int returnNoError(HANDLE 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 );
- return 777;
-}
-// end from secureim
-
-
-
-string toUTF8(wstring str)
-{
- string ustr;
- try{
- utf8::utf16to8(str.begin(), str.end(), back_inserter(ustr));
- }
- catch(const utf8::exception& e)
- {
- if(bDebugLog)
- debuglog<<std::string("utf8cpp encoding exception: ")+(char*)e.what();
- //TODO
- }
- return ustr;
-}
-
-
-
-wstring toUTF16(string str) //convert as much as possible
-{
- wstring ustr;
- string tmpstr;
- try{
- utf8::replace_invalid(str.begin(), str.end(), back_inserter(tmpstr));
- utf8::utf8to16(tmpstr.begin(), tmpstr.end(), back_inserter(ustr));
- }
- catch(const utf8::exception& e)
- {
- if(bDebugLog)
- debuglog<<std::string("utf8cpp decoding exception: ")+(char*)e.what();
- //TODO
- }
- return ustr;
-}
-
-string get_random(int length)
-{
- string chars("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890");
- string data;
- boost::random_device rng;
- boost::variate_generator<boost::random_device&, boost::uniform_int<>> gen(rng, boost::uniform_int<>(0, chars.length()-1));
- for(int i = 0; i < length; ++i)
- data += chars[gen()];
- return data;
-}
-
-void send_encrypted_msgs_thread(HANDLE hContact)
-{
- while(true)
- {
- char *key = UniGetContactSettingUtf(hContact, szGPGModuleName, "GPGPubKey", "");
- while(!isContactSecured(hContact))
- boost::this_thread::sleep(boost::posix_time::seconds(1));
- if(!hcontact_data[hContact].msgs_to_send.empty())
- {
- boost::this_thread::sleep(boost::posix_time::seconds(1));
- list<string>::iterator end = hcontact_data[hContact].msgs_to_send.end();
- extern std::list<HANDLE> sent_msgs;
- for(list<string>::iterator p = hcontact_data[hContact].msgs_to_send.begin(); p != end; ++p)
- {
- sent_msgs.push_back((HANDLE)CallContactService(hContact, PSS_MESSAGE, (WPARAM)PREF_UTF, (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));
- }
- hcontact_data[hContact].msgs_to_send.clear();
- return;
- }
- else
- return;
- }
-}
-
-string time_str()
-{
- boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();
- return (string)boost::posix_time::to_simple_string(now);
-}
-
-int handleEnum(const char *szSetting, LPARAM lParam)
-{
- if(!*(bool*)lParam && szSetting[0] && StriStr(szSetting, "tabsrmm"))
- {
- bool f = false, *found = (bool*)lParam;
- f = !DBGetContactSettingByte(NULL, "PluginDisable", szSetting, 0);
- if(f)
- *found = f;
- }
- return 0;
-}
-
-bool isTabsrmmUsed()
-{
- DBCONTACTENUMSETTINGS enm = {0};
- bool found = false;
- enm.lParam = (LPARAM)&found;
- enm.pfnEnumProc = (DBSETTINGENUMPROC)&handleEnum;
- enm.szModule = "PluginDisable";
- if(CallService(MS_DB_CONTACT_ENUMSETTINGS, (WPARAM)NULL, (LPARAM)&enm) == -1)
- return false;
-
- return found;
-}
-
-void ExportGpGKeysFunc(int type)
-{
- 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;
- std::ofstream file;
- file.open(path, std::ios::trunc | std::ios::out);
- mir_free(path);
- int exported_keys = 0;
- if(!file.is_open())
- return; //TODO: handle error
- if(!type || type == 2)
- {
- for(HANDLE hContact = (HANDLE)CallService(MS_DB_CONTACT_FINDFIRST, 0, 0); hContact; hContact = (HANDLE)CallService(MS_DB_CONTACT_FINDNEXT, (WPARAM)hContact, (LPARAM)0))
- {
- char *k = UniGetContactSettingUtf(hContact, szGPGModuleName, "GPGPubKey", "");
- if(!k[0])
- {
- mir_free(k);
- continue;
- }
- std::string key = k;
- mir_free(k);
- char *proto = (char*)CallService(MS_PROTO_GETCONTACTBASEPROTO, (WPARAM)hContact, 0);
- std::string id = "Comment: login ";
- const char * uid = (const char*)CallProtoService(proto, PS_GETCAPS, (WPARAM)PFLAG_UNIQUEIDSETTING, 0);
- DBVARIANT dbv = {0};
- DBCONTACTGETSETTING dbcgs = {0};
- dbcgs.pValue = &dbv;
- dbcgs.szModule = proto;
- dbcgs.szSetting = uid;
- CallService(MS_DB_CONTACT_GETSETTING, 0, (LPARAM)&dbcgs);
- switch(dbcgs.pValue->type)
- {
- case DBVT_DELETED:
- continue;
- break;
- case DBVT_BYTE:
- {
- char _id[64];
- mir_snprintf(_id, 63, "%d", dbcgs.pValue->bVal);
- id += _id;
- }
- break;
- case DBVT_WORD:
- {
- char _id[64];
- mir_snprintf(_id, 63, "%d", dbcgs.pValue->wVal);
- id += _id;
- }
- break;
- case DBVT_DWORD:
- {
- char _id[64];
- mir_snprintf(_id, 63, "%d", dbcgs.pValue->dVal);
- id += _id;
- }
- break;
- case DBVT_ASCIIZ:
- {
- id += dbcgs.pValue->pszVal;
- CallService(MS_DB_CONTACT_FREEVARIANT, 0, (LPARAM)&dbv);
- }
- break;
- case DBVT_UTF8:
- {
- char *tmp = mir_utf8decodeA(dbcgs.pValue->pszVal);
- if(tmp[0])
- id += tmp;
- mir_free(tmp);
- CallService(MS_DB_CONTACT_FREEVARIANT, 0, (LPARAM)&dbv);
- }
- break;
- case DBVT_BLOB:
- //TODO
- CallService(MS_DB_CONTACT_FREEVARIANT, 0, (LPARAM)&dbv);
- break;
- case DBVT_WCHAR:
- //TODO
- CallService(MS_DB_CONTACT_FREEVARIANT, 0, (LPARAM)&dbv);
- break;
- }
- id += " contact_id ";
- ZeroMemory(&dbv, sizeof(dbv));
- ZeroMemory(&dbcgs, sizeof(dbcgs));
- dbcgs.pValue = &dbv;
- dbcgs.szModule = proto;
- dbcgs.szSetting = uid;
- CallService(MS_DB_CONTACT_GETSETTING, (WPARAM)hContact, (LPARAM)&dbcgs);
- switch(dbcgs.pValue->type)
- {
- case DBVT_DELETED:
- continue;
- break;
- case DBVT_BYTE:
- {
- char _id[64];
- mir_snprintf(_id, 63, "%d", dbcgs.pValue->bVal);
- id += _id;
- }
- break;
- case DBVT_WORD:
- {
- char _id[64];
- mir_snprintf(_id, 63, "%d", dbcgs.pValue->wVal);
- id += _id;
- }
- break;
- case DBVT_DWORD:
- {
- char _id[64];
- mir_snprintf(_id, 63, "%d", dbcgs.pValue->dVal);
- id += _id;
- }
- break;
- case DBVT_ASCIIZ:
- {
- id += dbcgs.pValue->pszVal;
- CallService(MS_DB_CONTACT_FREEVARIANT, 0, (LPARAM)&dbv);
- }
- break;
- case DBVT_UTF8:
- {
- char *tmp = mir_utf8decodeA(dbcgs.pValue->pszVal);
- if(tmp[0])
- id += tmp;
- mir_free(tmp);
- CallService(MS_DB_CONTACT_FREEVARIANT, 0, (LPARAM)&dbv);
- }
- break;
- case DBVT_BLOB:
- //TODO
- CallService(MS_DB_CONTACT_FREEVARIANT, 0, (LPARAM)&dbv);
- break;
- case DBVT_WCHAR:
- //TODO
- CallService(MS_DB_CONTACT_FREEVARIANT, 0, (LPARAM)&dbv);
- break;
- }
- std::string::size_type p1 = key.find("-----BEGIN PGP PUBLIC KEY BLOCK-----");
- if(p1 == std::string::npos)
- continue;
- p1 += strlen("-----BEGIN PGP PUBLIC KEY BLOCK-----");
- p1 ++;
- id += '\n';
- key.insert(p1, id);
- file<<key;
- file<<std::endl;
- exported_keys++;
- }
- }
- if(type == 1 || type == 2)
- {
- string out;
- DWORD code;
- pxResult result;
- std::vector<wstring> cmd;
- cmd.push_back(L"--batch");
- cmd.push_back(L"-export-secret-keys");
- cmd.push_back(L"-a");
- gpg_execution_params params(cmd);
- params.out = &out;
- params.code = &code;
- params.result = &result;
- gpg_launcher(params); //TODO: handle errors
- {
- file<<out;
- file<<std::endl;
- }
- }
- if(file.is_open())
- file.close();
- char msg[512];
- if(type == 2)
- mir_snprintf(msg, 511, "%s %d %s %s %s", Translate("we have succesfully exported"), exported_keys, Translate("public keys"), Translate("and"), Translate("all private keys"));
- else if(type == 1)
- mir_snprintf(msg, 511, "%s %s", Translate("we have succesfully exported"), Translate("all private keys"));
- else if(!type)
- mir_snprintf(msg, 511, "%s %d %s",Translate("we have succesfully exported"), exported_keys, Translate("public keys"));
- MessageBoxA(NULL, msg, Translate("Keys export result"), MB_OK);
-}
-
-INT_PTR ExportGpGKeys(WPARAM w, LPARAM l)
-{
- void ShowExportKeysDlg();
- ShowExportKeysDlg();
- return 0;
-}
-
-INT_PTR ImportGpGKeys(WPARAM w, LPARAM l)
-{
- TCHAR *p = GetFilePath(_T("Choose file to import keys from"), _T("*"), _T("Any file"));
- if(!p || !p[0])
- {
- delete [] p;
- //TODO: handle error
- return 1;
- }
- char *path = mir_t2a(p);
- delete [] p;
- std::ifstream file;
- file.open(path, std::ios::in);
- mir_free(path);
- if(!file.is_open())
- return 1; //TODO: handle error
- PROTOACCOUNT **accs;
- int acc_count = 0, processed_keys = 0, processed_private_keys = 0;
- ProtoEnumAccounts(&acc_count, &accs);
- char line[256];
- file.getline(line, 255);
- if(!strstr(line, "-----BEGIN PGP PUBLIC KEY BLOCK-----") && !strstr(line, "-----BEGIN PGP PRIVATE KEY BLOCK-----"))
- return 1; //TODO: handle error
- std::string key, login, contact_id;
- key += line;
- key += '\n';
- while(file.is_open() && !file.eof())
- {
- file.getline(line, 255);
- key += line;
- key += '\n';
- if(strstr(line, "-----END PGP PUBLIC KEY BLOCK-----"))
- {
- std::string::size_type p1 = 0, p2 = 0;
- p1 = key.find("Comment: login ");
- p1 += strlen("Comment: login ");
- p2 = key.find(" contact_id ");
- login = key.substr(p1, p2-p1);
- p2 += strlen(" contact_id ");
- p1 = key.find("\n", p2);
- contact_id = key.substr(p2, p1-p2);
- p1 = key.find("Comment: login ");
- p2 = key.find("\n", p1);
- p2++;
- key.erase(p1, p2-p1);
- std::string acc;
- for(int i = 0; i < acc_count; i++)
- {
- if(acc.length())
- break;
- const char * uid = (const char*)CallProtoService(accs[i]->szModuleName, PS_GETCAPS, (WPARAM)PFLAG_UNIQUEIDSETTING, 0);
- DBVARIANT dbv = {0};
- DBCONTACTGETSETTING dbcgs = {0};
- dbcgs.pValue = &dbv;
- dbcgs.szModule = accs[i]->szModuleName;
- dbcgs.szSetting = uid;
- CallService(MS_DB_CONTACT_GETSETTING, 0, (LPARAM)&dbcgs);
- std::string id;
- switch(dbcgs.pValue->type)
- {
- case DBVT_DELETED:
- continue;
- break;
- case DBVT_BYTE:
- {
- char _id[64];
- mir_snprintf(_id, 63, "%d", dbcgs.pValue->bVal);
- id += _id;
- if(id == login)
- acc = accs[i]->szModuleName;
- }
- break;
- case DBVT_WORD:
- {
- char _id[64];
- mir_snprintf(_id, 63, "%d", dbcgs.pValue->wVal);
- id += _id;
- if(id == login)
- acc = accs[i]->szModuleName;
- }
- break;
- case DBVT_DWORD:
- {
- char _id[64];
- mir_snprintf(_id, 63, "%d", dbcgs.pValue->dVal);
- id += _id;
- if(id == login)
- acc = accs[i]->szModuleName;
- }
- break;
- case DBVT_ASCIIZ:
- {
- id += dbcgs.pValue->pszVal;
- CallService(MS_DB_CONTACT_FREEVARIANT, 0, (LPARAM)&dbv);
- if(id == login)
- acc = accs[i]->szModuleName;
- }
- break;
- case DBVT_UTF8:
- {
- char *tmp = mir_utf8decodeA(dbcgs.pValue->pszVal);
- if(tmp[0])
- id += tmp;
- mir_free(tmp);
- CallService(MS_DB_CONTACT_FREEVARIANT, 0, (LPARAM)&dbv);
- if(id == login)
- acc = accs[i]->szModuleName;
- }
- break;
- case DBVT_BLOB:
- //TODO
- CallService(MS_DB_CONTACT_FREEVARIANT, 0, (LPARAM)&dbv);
- break;
- case DBVT_WCHAR:
- //TODO
- CallService(MS_DB_CONTACT_FREEVARIANT, 0, (LPARAM)&dbv);
- break;
- }
- }
- if(acc.length())
- {
- const char * uid = (const char*)CallProtoService(acc.c_str(), PS_GETCAPS, (WPARAM)PFLAG_UNIQUEIDSETTING, 0);
- for(HANDLE hContact = (HANDLE)CallService(MS_DB_CONTACT_FINDFIRST, 0, (LPARAM)acc.c_str()); hContact; hContact = (HANDLE)CallService(MS_DB_CONTACT_FINDNEXT, (WPARAM)hContact, (LPARAM)acc.c_str()))
- {
- DBVARIANT dbv = {0};
- DBCONTACTGETSETTING dbcgs = {0};
- dbcgs.pValue = &dbv;
- dbcgs.szModule = acc.c_str();
- dbcgs.szSetting = uid;
- CallService(MS_DB_CONTACT_GETSETTING, (WPARAM)hContact, (LPARAM)&dbcgs);
- std::string id;
- bool found = false;
- switch(dbcgs.pValue->type)
- {
- case DBVT_DELETED:
- continue;
- break;
- case DBVT_BYTE:
- {
- char _id[64];
- mir_snprintf(_id, 63, "%d", dbcgs.pValue->bVal);
- id += _id;
- if(id == contact_id)
- found = true;
- }
- break;
- case DBVT_WORD:
- {
- char _id[64];
- mir_snprintf(_id, 63, "%d", dbcgs.pValue->wVal);
- id += _id;
- if(id == contact_id)
- found = true;
- }
- break;
- case DBVT_DWORD:
- {
- char _id[64];
- mir_snprintf(_id, 63, "%d", dbcgs.pValue->dVal);
- id += _id;
- if(id == contact_id)
- found = true;
- }
- break;
- case DBVT_ASCIIZ:
- {
- id += dbcgs.pValue->pszVal;
- CallService(MS_DB_CONTACT_FREEVARIANT, 0, (LPARAM)&dbv);
- if(id == contact_id)
- found = true;
- }
- break;
- case DBVT_UTF8:
- {
- char *tmp = mir_utf8decodeA(dbcgs.pValue->pszVal);
- if(tmp[0])
- id += tmp;
- mir_free(tmp);
- CallService(MS_DB_CONTACT_FREEVARIANT, 0, (LPARAM)&dbv);
- if(id == contact_id)
- found = true;
- }
- break;
- case DBVT_BLOB:
- //TODO
- CallService(MS_DB_CONTACT_FREEVARIANT, 0, (LPARAM)&dbv);
- break;
- case DBVT_WCHAR:
- //TODO
- CallService(MS_DB_CONTACT_FREEVARIANT, 0, (LPARAM)&dbv);
- break;
- }
- if(found)
- {
- wstring path;
- std::vector<std::wstring> cmd;
- TCHAR *ptmp;
- string output;
- DWORD exitcode;
- {
- HANDLE hcnt = hContact;
- ptmp = UniGetContactSettingUtf(NULL, szGPGModuleName, "szHomePath", _T(""));
- path = ptmp;
- mir_free(ptmp);
- mir_free(ptmp);
- wstring rand = toUTF16(get_random(10));
- path += L"\\";
- path += rand;
- boost::filesystem::remove(path);
- wfstream f(path, std::ios::out);
- f<<toUTF16(key).c_str();
- f.close();
- cmd.push_back(L"--batch");
- cmd.push_back(L"--import");
- cmd.push_back(path);
- }
- gpg_execution_params params(cmd);
- pxResult result;
- params.out = &output;
- params.code = &exitcode;
- params.result = &result;
- if(!gpg_launcher(params))
- break;
- if(result == pxNotFound)
- break;
- if(result == pxSuccess)
- processed_keys++;
- {
- if(output.find("already in secret keyring") != string::npos)
- {
- MessageBox(0, TranslateT("Key already in scret key ring."), TranslateT("Info"), MB_OK);
- boost::filesystem::remove(path);
- break;
- }
- char *tmp2;
- string::size_type s = output.find("gpg: key ") + strlen("gpg: key ");
- string::size_type s2 = output.find(":", s);
- tmp2 = (char*)mir_alloc((output.substr(s,s2-s).length()+1) * sizeof(char));
- strcpy(tmp2, output.substr(s,s2-s).c_str());
- mir_utf8decode(tmp2, 0);
- DBWriteContactSettingString(hContact, szGPGModuleName, "KeyID", tmp2);
- mir_free(tmp2);
- s = output.find("“", s2);
- if(s == string::npos)
- {
- s = output.find("\"", s2);
- s += 1;
- }
- else
- s += 3;
- if((s2 = output.find("(", s)) == string::npos)
- s2 = 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));
- strcpy(tmp2, output.substr(s,s2-s-1).c_str());
- mir_utf8decode(tmp2, 0);
- if(hContact)
- {
- DBWriteContactSettingString(hContact, szGPGModuleName, "KeyMainName", output.substr(s,s2-s-1).c_str());
- }
- mir_free(tmp2);
- if((s = output.find(")", s2)) == string::npos)
- 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));
- strcpy(tmp2, output.substr(s2,s-s2).c_str());
- mir_utf8decode(tmp2, 0);
- if(hContact)
- DBWriteContactSettingString(hContact, szGPGModuleName, "KeyComment", output.substr(s2,s-s2).c_str());
- mir_free(tmp2);
- s+=3;
- s2 = output.find(">", s);
- tmp2 = (char*)mir_alloc((output.substr(s,s2-s).length()+1) * sizeof(char));
- strcpy(tmp2, output.substr(s,s2-s).c_str());
- mir_utf8decode(tmp2, 0);
- if(hContact)
- DBWriteContactSettingString(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));
- strcpy(tmp2, output.substr(s2,s-s2).c_str());
- mir_utf8decode(tmp2, 0);
- if(hContact)
- DBWriteContactSettingString(hContact, szGPGModuleName, "KeyMainEmail", output.substr(s2,s-s2).c_str());
- mir_free(tmp2);
- }
- }
- DBWriteContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", 1);
- DBWriteContactSettingTString(hContact, szGPGModuleName, "GPGPubKey", toUTF16(key).c_str());
- }
- boost::filesystem::remove(path);
- break;
- }
- }
- }
- key.clear();
- }
- if(strstr(line, "-----END PGP PRIVATE KEY BLOCK-----"))
- {
- std::vector<wstring> cmd;
- TCHAR tmp2[MAX_PATH] = {0};
- TCHAR *ptmp;
- string output;
- DWORD exitcode;
- {
- ptmp = UniGetContactSettingUtf(NULL, szGPGModuleName, "szHomePath", _T(""));
- _tcscpy(tmp2, ptmp);
- mir_free(ptmp);
- _tcscat(tmp2, _T("\\"));
- _tcscat(tmp2, _T("temporary_exported.asc"));
- boost::filesystem::remove(tmp2);
- wfstream f(tmp2, std::ios::out);
- f<<toUTF16(key).c_str();
- f.close();
- cmd.push_back(L"--batch");
- cmd.push_back(L"--import");
- cmd.push_back(tmp2);
- }
- gpg_execution_params params(cmd);
- pxResult result;
- params.out = &output;
- params.code = &exitcode;
- params.result = &result;
- if(!gpg_launcher(params))
- break;
- if(result == pxNotFound)
- break;
- if(result == pxSuccess)
- processed_private_keys++;
- key.clear();
- }
- }
- if(file.is_open())
- file.close();
- char msg[512];
- if(processed_private_keys)
- mir_snprintf(msg, 511, "we have succesfully processed %d public keys and some private keys", processed_keys);
- else
- mir_snprintf(msg, 511, "we have succesfully processed %d public keys", processed_keys);
- MessageBoxA(NULL, msg, Translate("Keys import result"), MB_OK);
- return 0;
-}
-
-void fix_line_term(std::string &s)
-{
- if(s.empty())
- return;
- boost::algorithm::erase_all(s, "\r\r");
-}
-
-void fix_line_term(std::wstring &s)
-{
- if(s.empty())
- return;
- boost::algorithm::erase_all(s, _T("\r\r"));
-}
-
-void strip_line_term(std::wstring &s)
-{
- if(s.empty())
- return;
- boost::algorithm::erase_all(s, _T("\r"));
- boost::algorithm::erase_all(s, _T("\n"));
-}
-
-void strip_line_term(std::string &s)
-{
- if(s.empty())
- return;
- boost::algorithm::erase_all(s, "\r");
- boost::algorithm::erase_all(s, "\n");
-}
-
-void strip_tags(std::wstring &str)
-{
- if(str.empty())
- return;
- boost::algorithm::erase_all(str, inopentag);
- boost::algorithm::erase_all(str, inclosetag);
- boost::algorithm::erase_all(str, outopentag);
- boost::algorithm::erase_all(str, outclosetag);
-}
-
-
-static INT_PTR CALLBACK DlgProcEncryptedFileMsgBox(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
-{
- char *inkeyid = NULL;
- switch (msg)
- {
- case WM_INITDIALOG:
- {
- TranslateDialogDefault(hwndDlg);
- file_msg_state = -1;
- return TRUE;
- }
-
-
- case WM_COMMAND:
- {
- switch (LOWORD(wParam))
- {
- case IDC_IGNORE:
- if(IsDlgButtonChecked(hwndDlg, IDC_REMEMBER))
- {
- DBWriteContactSettingByte(NULL, szGPGModuleName, "bSameAction", 1);
- bSameAction = true;
- }
- DestroyWindow(hwndDlg);
- break;
-
- case IDC_DECRYPT:
- file_msg_state = 1;
- if(IsDlgButtonChecked(hwndDlg, IDC_REMEMBER))
- {
- DBWriteContactSettingByte(NULL, szGPGModuleName, "bFileTransfers", 1);
- bFileTransfers = true;
- DBWriteContactSettingByte(NULL, szGPGModuleName, "bSameAction", 0);
- bSameAction = false;
- }
-
- DestroyWindow(hwndDlg);
- break;
-
- default:
- break;
- }
-
- break;
- }
-
- case WM_NOTIFY:
- {
- }
- break;
- case WM_CLOSE:
- DestroyWindow(hwndDlg);
- break;
- case WM_DESTROY:
- {
- }
- break;
- }
- return FALSE;
-}
-
-
-void ShowEncryptedFileMsgBox()
-{
- extern HINSTANCE hInst;
- DialogBox(hInst, MAKEINTRESOURCE(IDD_ENCRYPTED_FILE_MSG_BOX), NULL, DlgProcEncryptedFileMsgBox);
-}
-
-
-static INT_PTR CALLBACK DlgProcExportKeys(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
-{
- switch (msg)
- {
- case WM_INITDIALOG:
- {
- TranslateDialogDefault(hwndDlg);
- return TRUE;
- }
-
-
- case WM_COMMAND:
- {
- switch (LOWORD(wParam))
- {
- case IDC_OK:
- if(IsDlgButtonChecked(hwndDlg, IDC_PUBLIC))
- ExportGpGKeysFunc(0);
- else if(IsDlgButtonChecked(hwndDlg, IDC_PRIVATE))
- ExportGpGKeysFunc(1);
- else if(IsDlgButtonChecked(hwndDlg, IDC_ALL))
- ExportGpGKeysFunc(2);
- DestroyWindow(hwndDlg);
- break;
-
- case IDC_CANCEL:
- DestroyWindow(hwndDlg);
- break;
-
- default:
- break;
- }
-
- break;
- }
-
- case WM_NOTIFY:
- {
- }
- break;
- case WM_CLOSE:
- DestroyWindow(hwndDlg);
- break;
- case WM_DESTROY:
- {
- }
- break;
- }
- return FALSE;
-}
-
-void ShowExportKeysDlg()
-{
- extern HINSTANCE hInst;
- DialogBox(hInst, MAKEINTRESOURCE(IDD_EXPORT_TYPE), NULL, DlgProcExportKeys);
-}
-
-static INT_PTR CALLBACK DlgProcChangePasswd(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
-{
- switch (msg)
- {
- case WM_INITDIALOG:
- {
- TranslateDialogDefault(hwndDlg);
- return TRUE;
- }
-
-
- case WM_COMMAND:
- {
- switch (LOWORD(wParam))
- {
- case IDC_OK:
- //TODO: show some prgress
- {
- std::string old_pass, new_pass;
- extern TCHAR key_id_global[17];
- TCHAR buf[256] = {0};
- GetDlgItemText(hwndDlg, IDC_NEW_PASSWD1, buf, 255);
- new_pass = toUTF8(buf);
- GetDlgItemText(hwndDlg, IDC_NEW_PASSWD2, buf, 255);
- if(new_pass != toUTF8(buf))
- {
- MessageBox(hwndDlg, TranslateT("New passwords not match"), TranslateT("Error"), MB_OK);
- //key_id_global[0] = 0;
- break;
- }
- GetDlgItemText(hwndDlg, IDC_OLD_PASSWD, buf, 255);
- old_pass = toUTF8(buf);
- bool old_pass_match = false;
- TCHAR *pass = UniGetContactSettingUtf(NULL, szGPGModuleName, "szKeyPassword", _T(""));
- if(!_tcscmp(pass,buf))
- old_pass_match = true;
- mir_free(pass);
- if(!old_pass_match)
+ HistoryLog(ccs->hContact, db_event(Translate("encrypting file for transfer"), 0, 0, DBEF_SENT));
+ DWORD flags = (DWORD)ccs->wParam; //check for PFTS_UNICODE here
+ int i;
+// if(flags & PFTS_UNICODE) //this does not work ....
+ if(StriStr(ccs->szProtoService, "/sendfilew"))
+ {
+ TCHAR **file=(TCHAR **)ccs->lParam;
+ for(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")))
+ continue;
+ std::wstring path_out = encrypt_file(ccs->hContact, file[i]);
+ mir_free(file[i]);
+ file[i] = mir_tstrdup(path_out.c_str());
+ transfers.push_back(path_out);
+ }
+ }
+ else
+ {
+ char **file = (char**) ccs->lParam;
+ for(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"))
+ continue;
+ TCHAR *tmp = mir_utf8decodeT(file[i]);
+ std::wstring path_out = encrypt_file(ccs->hContact, tmp);
+ mir_free(tmp);
+ char* tmp2 = mir_utf8encodeW(path_out.c_str());
+ mir_free(file[i]);
+ file[i] = tmp2;
+ transfers.push_back(path_out);
+
+ }
+ }
+ }
+ return CallService(MS_PROTO_CHAINSEND, w, l);
+}
+
+
+void HistoryLog(HANDLE hContact, db_event evt)
+{
+ DBEVENTINFO Event = {0};
+ Event.cbSize = sizeof(Event);
+ Event.szModule = szGPGModuleName;
+ Event.eventType = evt.eventType;
+ Event.flags = evt.flags;
+ if(!evt.timestamp)
+ Event.timestamp = (DWORD)time(NULL);
+ else
+ Event.timestamp = evt.timestamp;
+ Event.cbBlob = strlen((char*)evt.pBlob)+1;
+ Event.pBlob = (PBYTE)_strdup((char*)evt.pBlob);
+ CallService(MS_DB_EVENT_ADD, (WPARAM)(HANDLE)hContact,(LPARAM)&Event);
+}
+
+static int ControlAddStringUtf(HWND ctrl, DWORD msg, const TCHAR *szString)
+{
+ int item = -1;
+ item = SendMessage(ctrl, msg, 0, (LPARAM)szString);
+ return item;
+}
+
+int ComboBoxAddStringUtf(HWND hCombo, const TCHAR *szString, DWORD data)
+{
+ int item = ControlAddStringUtf(hCombo, CB_ADDSTRING, szString);
+ SendMessage(hCombo, CB_SETITEMDATA, item, data);
+
+ return item;
+}
+
+
+int GetJabberInterface(WPARAM w, LPARAM l) //get interface for all jabber accounts, options later
+{
+ extern list <JabberAccount*> Accounts;
+ void AddHandlers();
+ int count = 0;
+ PROTOACCOUNT **accounts;
+ ProtoEnumAccounts(&count, &accounts);
+ list <JabberAccount*>::iterator p;
+ Accounts.clear();
+ 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
+ {
+ IJabberInterface *JIftmp = getJabberApi(accounts[i]->szModuleName);
+ int a = 0;
+ if(JIftmp)
+ {
+ (*p)->setJabberInterface(JIftmp);
+ if(accounts[i]->tszAccountName)
+ {
+ TCHAR* tmp = mir_tstrdup(accounts[i]->tszAccountName);
+ (*p)->setAccountName(tmp);
+ }
+ else
+ {
+ TCHAR *tmp = mir_a2t(accounts[i]->szModuleName);
+ (*p)->setAccountName(tmp);
+ }
+ (*p)->setAccountNumber(a);
+ a++;
+ Accounts.push_back(new JabberAccount);
+ p++;
+ }
+ }
+ Accounts.pop_back();
+ AddHandlers();
+ return 0;
+}
+
+static JABBER_HANDLER_FUNC SendHandler(IJabberInterface *ji, HXML node, void *pUserData)
+{
+ HXML local_node = node;
+ for(int n = 0; n <= xi.getChildCount(node); n++)
+ {
+ LPCTSTR str = xi.getText(local_node);
+ LPCTSTR nodename = xi.getName(local_node);
+ LPCTSTR attr = xi.getAttrValue(local_node, _T("to"));
+ if(attr)
+ {
+ HANDLE hContact = ji->Sys()->ContactFromJID(attr);
+ if(hContact)
+ if(!isContactSecured(hContact))
+ return FALSE;
+ }
+ if(str)
+ {
+ if(_tcsstr(str, _T("-----BEGIN PGP MESSAGE-----")) && _tcsstr(str, _T("-----END PGP MESSAGE-----")))
+ {
+ wstring data = str;
+ xi.setText(local_node, _T("This message is encrypted."));
+ wstring::size_type p1 = data.find(_T("-----BEGIN PGP MESSAGE-----")) + _tcslen(_T("-----BEGIN PGP MESSAGE-----"));
+ 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)
+ {
+ p1 = data.find(_T("Comment: "), p1);
+ p1 = data.find(_T("\n"), p1);
+ }
+ while(data.find(_T("Encoding: "), p1) != wstring::npos)
+ {
+ p1 = data.find(_T("Encoding: "), p1);
+ p1 = data.find(_T("\n"), p1);
+ }
+ p1+=3;
+ wstring::size_type p2 = data.find(_T("-----END PGP MESSAGE-----"));
+ wstring data2 = data.substr(p1, p2-p1-2);
+ strip_line_term(data2);
+ HXML encrypted_data = xi.addChild(node, _T("x"), data2.c_str());
+ xi.addAttr(encrypted_data, _T("xmlns"), _T("jabber:x:encrypted"));
+ return FALSE;
+ }
+ }
+ 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));
+ mir_free(path_c);
+ path_out += _T("\\tmp\\");
+ path_out += file;
+ boost::filesystem::remove(path_out);
+ wfstream f(path_out.c_str(), std::ios::out);
+ f<<toUTF8(str).c_str();
+ f.close();
+ if(!boost::filesystem::exists(path_out))
+ {
+ if(bDebugLog)
+ debuglog<<std::string(time_str()+": info: Failed to write prescense in file");
+ return FALSE;
+ }
+ {
+ extern TCHAR *password;
+ string out;
+ DWORD code;
+ std::vector<wstring> cmd;
+ {
+ char *inkeyid;
+ {
+ char *proto = ji->Sys()->GetModuleName();
+ char setting[64];
+ strcpy(setting, proto);
+ strcat(setting, "_KeyID");
+ inkeyid = UniGetContactSettingUtf(NULL, szGPGModuleName, setting, "");
+ if(!inkeyid[0])
+ {
+ mir_free(inkeyid);
+ inkeyid = UniGetContactSettingUtf(NULL, szGPGModuleName, "KeyID", "");
+ }
+ }
+ TCHAR *pass = NULL;
+ if(inkeyid[0])
+ {
+ string dbsetting = "szKey_";
+ dbsetting += inkeyid;
+ dbsetting += "_Password";
+ pass = UniGetContactSettingUtf(NULL, szGPGModuleName, dbsetting.c_str(), _T(""));
+ if(pass[0] && bDebugLog)
+ debuglog<<std::string(time_str()+": info: found password in database for key id: "+inkeyid+", trying to encrypt message from self with password");
+ }
+ else
+ {
+ pass = UniGetContactSettingUtf(NULL, szGPGModuleName, "szKeyPassword", _T(""));
+ if(pass[0] && bDebugLog)
+ debuglog<<std::string(time_str()+": info: found password for all keys in database, trying to encrypt message from self with password");
+ }
+ if(pass[0])
+ {
+ cmd.push_back(L"--passphrase");
+ cmd.push_back(pass);
+ }
+ else if(password)
+ {
+ if(bDebugLog)
+ debuglog<<std::string(time_str()+": info: found password in memory, trying to encrypt message from self with password");
+ cmd.push_back(L"--passphrase");
+ cmd.push_back(password);
+ }
+ else if (bDebugLog)
+ debuglog<<std::string(time_str()+": info: passwords not found in database or memory, trying to encrypt message from self with out password");
+ mir_free(pass);
+ mir_free(inkeyid);
+ }
+ cmd.push_back(L"--local-user");
+ path_c = UniGetContactSettingUtf(NULL, szGPGModuleName, "KeyID", _T(""));
+ cmd.push_back(path_c);
+ cmd.push_back(L"--default-key");
+ cmd.push_back(path_c);
+ mir_free(path_c);
+ cmd.push_back(L"--batch");
+ cmd.push_back(L"--yes");
+ cmd.push_back(L"-abs");
+ cmd.push_back(path_out);
+ gpg_execution_params params(cmd);
+ pxResult result;
+ params.out = &out;
+ params.code = &code;
+ params.result = &result;
+ gpg_launcher(params, boost::posix_time::seconds(15)); // TODO: handle errors
+ boost::filesystem::remove(path_out);
+ path_out += _T(".asc");
+ f.open(path_out.c_str(), std::ios::in | std::ios::ate | std::ios::binary);
+ wstring data;
+ if(f.is_open())
+ {
+ std::wifstream::pos_type size = f.tellg();
+ TCHAR *tmp = new TCHAR [(std::ifstream::pos_type)size+(std::ifstream::pos_type)1];
+ f.seekg(0, std::ios::beg);
+ f.read(tmp, size);
+ tmp[size]= '\0';
+ data.append(tmp);
+ delete [] tmp;
+ f.close();
+ boost::filesystem::remove(path_out);
+ }
+ if(data.empty())
+ {
+ if(bDebugLog)
+ debuglog<<std::string(time_str()+": info: Failed to read prescense sign from file");
+ return FALSE;
+ }
+ if(data.find(_T("-----BEGIN PGP SIGNATURE-----")) != wstring::npos && data.find(_T("-----END PGP SIGNATURE-----")) != wstring::npos)
+ {
+ wstring::size_type p1 = data.find(_T("-----BEGIN PGP SIGNATURE-----")) + _tcslen(_T("-----BEGIN PGP SIGNATURE-----"));
+ if(data.find(_T("Version: "), p1) != wstring::npos)
+ {
+ p1 = data.find(_T("Version: "), p1);
+ p1 = data.find(_T("\n"), p1);
+ if(data.find(_T("Version: "), p1) != wstring::npos)
+ {
+ p1 = data.find(_T("Version: "), p1);
+ p1 = data.find(_T("\n"), p1)+1;
+ }
+ else
+ p1 += 1;
+ }
+ if(data.find(_T("Comment: "), p1) != wstring::npos)
+ {
+ p1 = data.find(_T("Comment: "), p1);
+ p1 = data.find(_T("\n"), p1);
+ if(data.find(_T("Comment: "), p1) != wstring::npos)
+ {
+ p1 = data.find(_T("Comment: "), p1);
+ p1 = data.find(_T("\n"), p1)+1;
+ }
+ else
+ p1 += 1;
+ }
+ else
+ p1+=1;
+ p1++;
+ wstring::size_type p2 = data.find(_T("-----END PGP SIGNATURE-----"));
+ {
+ std::wstring tmp = data.substr(p1, p2-p1);
+ strip_line_term(tmp);
+ HXML encrypted_data = xi.addChild(node, _T("x"), tmp.c_str());
+ xi.addAttr(encrypted_data, _T("xmlns"), _T("jabber:x:signed"));
+ }
+ }
+ return FALSE;
+ }
+ }
+ }
+ local_node = xi.getChild(node, n);
+ }
+ return FALSE;
+}
+
+//boost::mutex sign_file_mutex;
+
+static JABBER_HANDLER_FUNC PrescenseHandler(IJabberInterface *ji, HXML node, void *pUserData)
+{
+ HXML local_node = node;
+ for(int n = 0; n <= xi.getChildCount(node); n++)
+ {
+ LPCTSTR str = xi.getText(local_node);
+ LPCTSTR nodename = xi.getName(local_node);
+ if(nodename)
+ {
+ if(_tcsstr(nodename, _T("x")))
+ {
+ for(int n = 0; n < xi.getAttrCount(local_node); n++)
+ {
+ LPCTSTR name = xi.getAttrName(local_node, n);
+ LPCTSTR value = xi.getAttrValue(local_node, name);
+ if(_tcsstr(value, _T("jabber:x:signed")))
+ {
+ std::wstring status_str;
+ HXML local_node2 = node;
+ for(int n = 0; n <= xi.getChildCount(node); n++)
+ {
+ LPCTSTR nodename2 = xi.getName(local_node2);
+ if(_tcsstr(nodename2, _T("status")))
+ {
+ LPCTSTR status = xi.getText(local_node2);
+ if(status)
+ status_str = status;
+ break;
+ }
+ local_node2 = xi.getChild(node, n);
+ }
+ LPCTSTR data = xi.getText(local_node);
+ wstring sign = _T("-----BEGIN PGP SIGNATURE-----\n\n");
+ wstring file = toUTF16(get_random(10)), status_file = toUTF16(get_random(10));
+ sign += data;
+ sign += _T("\n-----END PGP SIGNATURE-----\n");
+ TCHAR *path_c = UniGetContactSettingUtf(NULL, szGPGModuleName, "szHomePath", _T(""));
+ wstring path_out = path_c, status_file_out = path_c;
+ mir_free(path_c);
+ path_out += L"\\tmp\\";
+ path_out += file;
+ path_out += L".sig";
+ status_file_out += L"\\tmp\\";
+ status_file_out += status_file;
+ status_file_out += L".status";
+// sign_file_mutex.lock();
+ boost::filesystem::remove(path_out);
+ boost::filesystem::remove(status_file_out);
+ wfstream f(path_out.c_str(), std::ios::out);
+ while(!f.is_open())
+ f.open(path_out.c_str(), std::ios::out);
+ f<<toUTF8(sign).c_str();
+ f.close();
+ f.open(status_file_out.c_str(), std::ios::out);
+ while(!f.is_open())
+ f.open(status_file_out.c_str(), std::ios::out);
+ f<<toUTF8(status_str).c_str();
+ f.close();
+ if(!boost::filesystem::exists(path_out))
+ {
+// sign_file_mutex.unlock();
+ if(bDebugLog)
+ debuglog<<std::string(time_str()+": info: Failed to write sign in file");
+ return FALSE;
+ }
+ { //gpg
+ string out;
+ DWORD code;
+ std::vector<wstring> cmd;
+ cmd.push_back(L"--verify");
+ cmd.push_back(L"-a");
+ cmd.push_back(path_out);
+ cmd.push_back(status_file_out);
+ gpg_execution_params params(cmd);
+ pxResult result;
+ params.out = &out;
+ params.code = &code;
+ params.result = &result;
+ if(!gpg_launcher(params, boost::posix_time::seconds(15)))
+ {
+ return FALSE;
+ }
+ if(result == pxNotFound)
+ {
+ return FALSE;
+ }
+ boost::filesystem::remove(path_out);
+ boost::filesystem::remove(status_file_out);
+ if(out.find("key ID ") != string::npos)
+ {
+ //need to get hcontact here, i can get jid from hxml, and get handle from jid, maybe exists better way ?
+ string::size_type p1 = out.find("key ID ") + strlen("key ID ");
+ string::size_type p2 = out.find("\n", p1);
+ if(p1 != string::npos && p2 != string::npos)
+ {
+ HANDLE hContact = NULL;
+ {
+ extern list <JabberAccount*> Accounts;
+ list <JabberAccount*>::iterator p = Accounts.begin();
+ for(unsigned int i = 0; i < Accounts.size(); i++, p++)
+ {
+ if(!(*p))
+ break;
+ hContact = (*p)->getJabberInterface()->Sys()->ContactFromJID(xi.getAttrValue(node, _T("from")));
+ if(hContact)
+ hcontact_data[hContact].key_in_prescense = out.substr(p1, p2-p1-1).c_str();
+ }
+ }
+ }
+ }
+ }
+ return FALSE;
+ }
+ }
+ }
+ }
+ local_node = xi.getChild(node, n);
+ }
+ return FALSE;
+}
+
+static JABBER_HANDLER_FUNC MessageHandler(IJabberInterface *ji, HXML node, void *pUserData)
+{
+ return FALSE;
+}
+
+
+
+
+void AddHandlers()
+{
+ extern list<JabberAccount*> Accounts;
+ list<JabberAccount*>::iterator end = Accounts.end();
+ for(list<JabberAccount*>::iterator p = Accounts.begin(); p != end; p++)
+ {
+ if(!(*p))
+ break;
+ if((*p)->getSendHandler() == INVALID_HANDLE_VALUE)
+ (*p)->setSendHandler((*p)->getJabberInterface()->Net()->AddSendHandler((JABBER_HANDLER_FUNC)SendHandler));
+ if((*p)->getPrescenseHandler() == INVALID_HANDLE_VALUE)
+ (*p)->setPrescenseHandler((*p)->getJabberInterface()->Net()->AddPresenceHandler((JABBER_HANDLER_FUNC)PrescenseHandler));
+// if((*p)->getMessageHandler() == INVALID_HANDLE_VALUE)
+// (*p)->setMessageHandler((*p)->getJabberInterface()->Net()->AddMessageHandler((JABBER_HANDLER_FUNC)MessageHandler, JABBER_MESSAGE_TYPE_ANY ,NULL,NULL));
+ if(bAutoExchange)
+ {
+ (*p)->getJabberInterface()->Net()->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()->Net()->AddFeatures(_T("GPG_Key_Auto_Exchange:0\0\0"));
+ }
+ if(bFileTransfers)
+ {
+ (*p)->getJabberInterface()->Net()->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()->Net()->AddFeatures(_T("GPG_Encrypted_FileTransfers:0\0\0"));
+ }
+ }
+}
+
+bool isContactSecured(HANDLE hContact)
+{
+ BYTE gpg_enc = DBGetContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", 0);
+ if(!gpg_enc)
+ {
+ if(bDebugLog)
+ debuglog<<std::string(time_str()+": encryption is turned off for "+toUTF8((TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)hContact, GCDNF_TCHAR)));
+ return false;
+ }
+ if(!metaIsProtoMetaContacts(hContact))
+ {
+ TCHAR *key = UniGetContactSettingUtf(hContact, szGPGModuleName, "GPGPubKey", _T(""));
+ if(!key[0])
+ {
+ mir_free(key);
+ if(bDebugLog)
+ debuglog<<std::string(time_str()+": encryption is turned off for "+toUTF8((TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)hContact, GCDNF_TCHAR)));
+ return false;
+ }
+ mir_free(key);
+ }
+ if(bDebugLog)
+ debuglog<<std::string(time_str()+": encryption is turned on for "+toUTF8((TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)hContact, GCDNF_TCHAR)));
+ return true;
+}
+
+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;
+}
+
+bool isGPGKeyExist()
+{
+ TCHAR *id = UniGetContactSettingUtf(NULL, szGPGModuleName, "KeyID", _T(""));
+ char *key = UniGetContactSettingUtf(NULL, szGPGModuleName, "GPGPubKey", "");
+ if(id[0] && key[0])
+ {
+ mir_free(id);
+ mir_free(key);
+ return true;
+ }
+ mir_free(id);
+ mir_free(key);
+ return false;
+}
+bool isGPGValid()
+{
+ TCHAR *tmp = NULL;
+ 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);
+ tmp = NULL;
+ TCHAR *path = (TCHAR*)mir_alloc(sizeof(TCHAR)*MAX_PATH);
+ char *mir_path = (char*)mir_alloc(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)+64)*sizeof(TCHAR));
+ TCHAR *gpg_path = (TCHAR*)mir_alloc(sizeof(TCHAR)*MAX_PATH);
+ _tcscpy(gpg_path, tmp);
+ _tcscat(gpg_path, _T("\\GnuPG\\gpg.exe"));
+ mir_free(tmp);
+ tmp = NULL;
+ if(_waccess(gpg_path, 0) != -1)
+ {
+ gpg_exists = true;
+ _tcscpy(path, _T("GnuPG\\gpg.exe"));
+ }
+ mir_free(gpg_path);
+ tmp = mir_tstrdup(path);
+ mir_free(path);
+ }
+ DWORD len = MAX_PATH;
+ if(gpg_exists)
+ {
+ DBWriteContactSettingTString(NULL, szGPGModuleName, "szGpgBinPath", tmp);
+ mir_free(tmp);
+ tmp = NULL;
+ string out;
+ DWORD code;
+ std::vector<wstring> cmd;
+ cmd.push_back(L"--version");
+ gpg_execution_params params(cmd);
+ pxResult result;
+ params.out = &out;
+ params.code = &code;
+ params.result = &result;
+ gpg_valid = true;
+ gpg_launcher(params);
+ gpg_valid = false;
+ string::size_type p1 = out.find("(GnuPG) ");
+ if(p1 == string::npos)
+ is_valid = false;
+ }
+ 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); */
+ return is_valid;
+}
+
+#define NEWTSTR_MALLOC(A) (A==NULL)?NULL:strcpy((char*)mir_alloc(sizeof(char)*(strlen(A)+1)),A)
+
+const bool StriStr(const char *str, const char *substr)
+{
+ bool i = false;
+ char *str_up = NEWTSTR_MALLOC(str);
+ char *substr_up = NEWTSTR_MALLOC(substr);
+
+ CharUpperBuffA(str_up, strlen(str_up));
+ CharUpperBuffA(substr_up, strlen(substr_up));
+
+ if(strstr (str_up, substr_up))
+ i = true;
+
+ mir_free(str_up);
+ mir_free(substr_up);
+
+ return i;
+}
+
+bool IsOnline(HANDLE hContact)
+{
+ if(DBGetContactSettingByte(hContact, szGPGModuleName, "Status", 0) == ID_STATUS_OFFLINE)
+ return false;
+ return true;
+}
+
+//from secureim
+#include <process.h>
+
+struct TFakeAckParams {
+ inline TFakeAckParams( HANDLE p1, HANDLE p2, LONG p3, LPCSTR p4 ) :
+ hEvent( p1 ),
+ hContact( p2 ),
+ id( p3 ),
+ msg( p4 )
+ {}
+
+ HANDLE hEvent;
+ HANDLE hContact;
+ LONG id;
+ LPCSTR msg;
+};
+
+int SendBroadcast( HANDLE hContact, int type, int result, HANDLE hProcess, LPARAM lParam ) {
+ ACKDATA ack;
+ memset(&ack,0,sizeof(ack));
+ ack.cbSize = sizeof( ACKDATA );
+ ack.szModule = szGPGModuleName;// (char*)CallService(MS_PROTO_GETCONTACTBASEPROTO, (WPARAM)hContact, 0);
+ ack.hContact = hContact;
+ ack.type = type;
+ ack.result = result;
+ ack.hProcess = (HANDLE)777;//hProcess;
+ ack.lParam = lParam;
+ return CallService( MS_PROTO_BROADCASTACK, 0, ( LPARAM )&ack );
+}
+
+
+
+unsigned __stdcall sttFakeAck( LPVOID param ) {
+
+ 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 );
+ else
+ SendBroadcast( tParam->hContact, ACKTYPE_MESSAGE, ACKRESULT_FAILED, ( HANDLE )tParam->id, LPARAM( tParam->msg ));
+
+ CloseHandle( tParam->hEvent );
+ delete tParam;
+
+ return 0;
+}
+
+
+int returnNoError(HANDLE 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 );
+ return 777;
+}
+// end from secureim
+
+
+
+string toUTF8(wstring str)
+{
+ string ustr;
+ try{
+ utf8::utf16to8(str.begin(), str.end(), back_inserter(ustr));
+ }
+ catch(const utf8::exception& e)
+ {
+ if(bDebugLog)
+ debuglog<<std::string("utf8cpp encoding exception: ")+(char*)e.what();
+ //TODO
+ }
+ return ustr;
+}
+
+
+
+wstring toUTF16(string str) //convert as much as possible
+{
+ wstring ustr;
+ string tmpstr;
+ try{
+ utf8::replace_invalid(str.begin(), str.end(), back_inserter(tmpstr));
+ utf8::utf8to16(tmpstr.begin(), tmpstr.end(), back_inserter(ustr));
+ }
+ catch(const utf8::exception& e)
+ {
+ if(bDebugLog)
+ debuglog<<std::string("utf8cpp decoding exception: ")+(char*)e.what();
+ //TODO
+ }
+ return ustr;
+}
+
+string get_random(int length)
+{
+ string chars("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890");
+ string data;
+ boost::random_device rng;
+ boost::variate_generator<boost::random_device&, boost::uniform_int<>> 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(HANDLE hContact)
+{
+ while(true)
+ {
+ //char *key = UniGetContactSettingUtf(hContact, szGPGModuleName, "GPGPubKey", "");
+ while(!isContactSecured(hContact))
+ boost::this_thread::sleep(boost::posix_time::seconds(1));
+ if(!hcontact_data[hContact].msgs_to_send.empty())
+ {
+ boost::this_thread::sleep(boost::posix_time::seconds(1));
+ list<string>::iterator end = hcontact_data[hContact].msgs_to_send.end();
+ extern std::list<HANDLE> sent_msgs;
+ for(list<string>::iterator p = hcontact_data[hContact].msgs_to_send.begin(); p != end; ++p)
+ {
+ sent_msgs.push_back((HANDLE)CallContactService(hContact, PSS_MESSAGE, PREF_UTF, (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));
+ }
+ hcontact_data[hContact].msgs_to_send.clear();
+ return;
+ }
+ else
+ return;
+ }
+}
+
+string time_str()
+{
+ boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();
+ return (string)boost::posix_time::to_simple_string(now);
+}
+
+int handleEnum(const char *szSetting, LPARAM lParam)
+{
+ if(!*(bool*)lParam && szSetting[0] && StriStr(szSetting, "tabsrmm"))
+ {
+ bool f = false, *found = (bool*)lParam;
+ f = !DBGetContactSettingByte(NULL, "PluginDisable", szSetting, 0);
+ if(f)
+ *found = f;
+ }
+ return 0;
+}
+
+bool isTabsrmmUsed()
+{
+ DBCONTACTENUMSETTINGS enm = {0};
+ bool found = false;
+ enm.lParam = (LPARAM)&found;
+ enm.pfnEnumProc = (DBSETTINGENUMPROC)&handleEnum;
+ enm.szModule = "PluginDisable";
+ if(CallService(MS_DB_CONTACT_ENUMSETTINGS, (WPARAM)NULL, (LPARAM)&enm) == -1)
+ return false;
+
+ return found;
+}
+
+void ExportGpGKeysFunc(int type)
+{
+ 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;
+ std::ofstream file;
+ file.open(path, std::ios::trunc | std::ios::out);
+ mir_free(path);
+ int exported_keys = 0;
+ if(!file.is_open())
+ return; //TODO: handle error
+ if(!type || type == 2)
+ {
+ for(HANDLE hContact = (HANDLE)CallService(MS_DB_CONTACT_FINDFIRST, 0, 0); hContact; hContact = (HANDLE)CallService(MS_DB_CONTACT_FINDNEXT, (WPARAM)hContact, (LPARAM)0))
+ {
+ char *k = UniGetContactSettingUtf(hContact, szGPGModuleName, "GPGPubKey", "");
+ if(!k[0])
+ {
+ mir_free(k);
+ continue;
+ }
+ std::string key = k;
+ mir_free(k);
+ char *proto = (char*)CallService(MS_PROTO_GETCONTACTBASEPROTO, (WPARAM)hContact, 0);
+ std::string id = "Comment: login ";
+ const char * uid = (const char*)CallProtoService(proto, PS_GETCAPS, (WPARAM)PFLAG_UNIQUEIDSETTING, 0);
+ DBVARIANT dbv = {0};
+ DBCONTACTGETSETTING dbcgs = {0};
+ dbcgs.pValue = &dbv;
+ dbcgs.szModule = proto;
+ dbcgs.szSetting = uid;
+ CallService(MS_DB_CONTACT_GETSETTING, 0, (LPARAM)&dbcgs);
+ switch(dbcgs.pValue->type)
+ {
+ case DBVT_DELETED:
+ continue;
+
+ case DBVT_BYTE:
+ {
+ char _id[64];
+ mir_snprintf(_id, 64, "%d", dbv.bVal);
+ id += _id;
+ }
+ break;
+ case DBVT_WORD:
+ {
+ char _id[64];
+ mir_snprintf(_id, 64, "%d", dbv.wVal);
+ id += _id;
+ }
+ break;
+ case DBVT_DWORD:
+ {
+ char _id[64];
+ mir_snprintf(_id, 64, "%d", dbv.dVal);
+ id += _id;
+ }
+ break;
+ case DBVT_ASCIIZ:
+ {
+ id += dbcgs.pValue->pszVal;
+ CallService(MS_DB_CONTACT_FREEVARIANT, 0, (LPARAM)&dbv);
+ }
+ break;
+ case DBVT_UTF8:
+ {
+ char *tmp = mir_utf8decodeA(dbcgs.pValue->pszVal);
+ if(tmp[0])
+ id += tmp;
+ mir_free(tmp);
+ CallService(MS_DB_CONTACT_FREEVARIANT, 0, (LPARAM)&dbv);
+ }
+ break;
+ case DBVT_BLOB:
+ //TODO
+ CallService(MS_DB_CONTACT_FREEVARIANT, 0, (LPARAM)&dbv);
+ break;
+ case DBVT_WCHAR:
+ //TODO
+ CallService(MS_DB_CONTACT_FREEVARIANT, 0, (LPARAM)&dbv);
+ break;
+ }
+ id += " contact_id ";
+ ZeroMemory(&dbv, sizeof(dbv));
+ ZeroMemory(&dbcgs, sizeof(dbcgs));
+ dbcgs.pValue = &dbv;
+ dbcgs.szModule = proto;
+ dbcgs.szSetting = uid;
+ CallService(MS_DB_CONTACT_GETSETTING, (WPARAM)hContact, (LPARAM)&dbcgs);
+ switch(dbcgs.pValue->type)
+ {
+ case DBVT_DELETED:
+ continue;
+ case DBVT_BYTE:
+ {
+ char _id[64];
+ mir_snprintf(_id, 64, "%d", dbv.bVal);
+ id += _id;
+ }
+ break;
+ case DBVT_WORD:
+ {
+ char _id[64];
+ mir_snprintf(_id, 64, "%d", dbv.wVal);
+ id += _id;
+ }
+ break;
+ case DBVT_DWORD:
+ {
+ char _id[64];
+ mir_snprintf(_id, 64, "%d", dbv.dVal);
+ id += _id;
+ }
+ break;
+ case DBVT_ASCIIZ:
+ {
+ id += dbcgs.pValue->pszVal;
+ CallService(MS_DB_CONTACT_FREEVARIANT, 0, (LPARAM)&dbv);
+ }
+ break;
+ case DBVT_UTF8:
+ {
+ char *tmp = mir_utf8decodeA(dbcgs.pValue->pszVal);
+ if(tmp[0])
+ id += tmp;
+ mir_free(tmp);
+ CallService(MS_DB_CONTACT_FREEVARIANT, 0, (LPARAM)&dbv);
+ }
+ break;
+ case DBVT_BLOB:
+ //TODO
+ CallService(MS_DB_CONTACT_FREEVARIANT, 0, (LPARAM)&dbv);
+ break;
+ case DBVT_WCHAR:
+ //TODO
+ CallService(MS_DB_CONTACT_FREEVARIANT, 0, (LPARAM)&dbv);
+ break;
+ }
+ std::string::size_type p1 = key.find("-----BEGIN PGP PUBLIC KEY BLOCK-----");
+ if(p1 == std::string::npos)
+ continue;
+ p1 += strlen("-----BEGIN PGP PUBLIC KEY BLOCK-----");
+ p1 ++;
+ id += '\n';
+ key.insert(p1, id);
+ file<<key;
+ file<<std::endl;
+ exported_keys++;
+ }
+ }
+ if(type == 1 || type == 2)
+ {
+ string out;
+ DWORD code;
+ pxResult result;
+ std::vector<wstring> cmd;
+ cmd.push_back(L"--batch");
+ cmd.push_back(L"-export-secret-keys");
+ cmd.push_back(L"-a");
+ gpg_execution_params params(cmd);
+ params.out = &out;
+ params.code = &code;
+ params.result = &result;
+ gpg_launcher(params); //TODO: handle errors
+ {
+ file<<out;
+ file<<std::endl;
+ }
+ }
+ if(file.is_open())
+ file.close();
+ char msg[512];
+ if(type == 2)
+ mir_snprintf(msg, 512, "%s %d %s %s %s", Translate("we have succesfully exported"), exported_keys, Translate("public keys"), Translate("and"), Translate("all private keys"));
+ else if(type == 1)
+ mir_snprintf(msg, 512, "%s %s", Translate("we have succesfully exported"), Translate("all private keys"));
+ else if(!type)
+ mir_snprintf(msg, 512, "%s %d %s",Translate("we have succesfully exported"), exported_keys, Translate("public keys"));
+ MessageBoxA(NULL, msg, Translate("Keys export result"), MB_OK);
+}
+
+INT_PTR ExportGpGKeys(WPARAM w, LPARAM l)
+{
+ void ShowExportKeysDlg();
+ ShowExportKeysDlg();
+ return 0;
+}
+
+INT_PTR ImportGpGKeys(WPARAM w, LPARAM l)
+{
+ TCHAR *p = GetFilePath(_T("Choose file to import keys from"), _T("*"), _T("Any file"));
+ if(!p || !p[0])
+ {
+ delete [] p;
+ //TODO: handle error
+ return 1;
+ }
+ char *path = mir_t2a(p);
+ delete [] p;
+ std::ifstream file;
+ file.open(path, std::ios::in);
+ mir_free(path);
+ if(!file.is_open())
+ return 1; //TODO: handle error
+ PROTOACCOUNT **accs;
+ int acc_count = 0, processed_keys = 0, processed_private_keys = 0;
+ ProtoEnumAccounts(&acc_count, &accs);
+ char line[256];
+ file.getline(line, 255);
+ if(!strstr(line, "-----BEGIN PGP PUBLIC KEY BLOCK-----") && !strstr(line, "-----BEGIN PGP PRIVATE KEY BLOCK-----"))
+ return 1; //TODO: handle error
+ std::string key, login, contact_id;
+ key += line;
+ key += '\n';
+ while(file.is_open() && !file.eof())
+ {
+ file.getline(line, 255);
+ key += line;
+ key += '\n';
+ if(strstr(line, "-----END PGP PUBLIC KEY BLOCK-----"))
+ {
+ std::string::size_type p1 = 0, p2 = 0;
+ p1 = key.find("Comment: login ");
+ p1 += strlen("Comment: login ");
+ p2 = key.find(" contact_id ");
+ login = key.substr(p1, p2-p1);
+ p2 += strlen(" contact_id ");
+ p1 = key.find("\n", p2);
+ contact_id = key.substr(p2, p1-p2);
+ p1 = key.find("Comment: login ");
+ p2 = key.find("\n", p1);
+ p2++;
+ key.erase(p1, p2-p1);
+ std::string acc;
+ for(int i = 0; i < acc_count; i++)
+ {
+ if(acc.length())
+ break;
+ const char * uid = (const char*)CallProtoService(accs[i]->szModuleName, PS_GETCAPS, (WPARAM)PFLAG_UNIQUEIDSETTING, 0);
+ DBVARIANT dbv = {0};
+ DBCONTACTGETSETTING dbcgs = {0};
+ dbcgs.pValue = &dbv;
+ dbcgs.szModule = accs[i]->szModuleName;
+ dbcgs.szSetting = uid;
+ CallService(MS_DB_CONTACT_GETSETTING, 0, (LPARAM)&dbcgs);
+ std::string id;
+ switch(dbcgs.pValue->type)
+ {
+ case DBVT_DELETED:
+ continue;
+ break;
+ case DBVT_BYTE:
+ {
+ char _id[64];
+ mir_snprintf(_id, 64, "%d", dbv.bVal);
+ id += _id;
+ if(id == login)
+ acc = accs[i]->szModuleName;
+ }
+ break;
+ case DBVT_WORD:
+ {
+ char _id[64];
+ mir_snprintf(_id, 64, "%d", dbv.wVal);
+ id += _id;
+ if(id == login)
+ acc = accs[i]->szModuleName;
+ }
+ break;
+ case DBVT_DWORD:
+ {
+ char _id[64];
+ mir_snprintf(_id, 64, "%d", dbv.dVal);
+ id += _id;
+ if(id == login)
+ acc = accs[i]->szModuleName;
+ }
+ break;
+ case DBVT_ASCIIZ:
+ {
+ id += dbcgs.pValue->pszVal;
+ CallService(MS_DB_CONTACT_FREEVARIANT, 0, (LPARAM)&dbv);
+ if(id == login)
+ acc = accs[i]->szModuleName;
+ }
+ break;
+ case DBVT_UTF8:
+ {
+ char *tmp = mir_utf8decodeA(dbcgs.pValue->pszVal);
+ if(tmp[0])
+ id += tmp;
+ mir_free(tmp);
+ CallService(MS_DB_CONTACT_FREEVARIANT, 0, (LPARAM)&dbv);
+ if(id == login)
+ acc = accs[i]->szModuleName;
+ }
+ break;
+ case DBVT_BLOB:
+ //TODO
+ CallService(MS_DB_CONTACT_FREEVARIANT, 0, (LPARAM)&dbv);
+ break;
+ case DBVT_WCHAR:
+ //TODO
+ CallService(MS_DB_CONTACT_FREEVARIANT, 0, (LPARAM)&dbv);
+ break;
+ }
+ }
+ if(acc.length())
+ {
+ const char * uid = (const char*)CallProtoService(acc.c_str(), PS_GETCAPS, (WPARAM)PFLAG_UNIQUEIDSETTING, 0);
+ for(HANDLE hContact = (HANDLE)CallService(MS_DB_CONTACT_FINDFIRST, 0, (LPARAM)acc.c_str()); hContact; hContact = (HANDLE)CallService(MS_DB_CONTACT_FINDNEXT, (WPARAM)hContact, (LPARAM)acc.c_str()))
+ {
+ DBVARIANT dbv = {0};
+ DBCONTACTGETSETTING dbcgs = {0};
+ dbcgs.pValue = &dbv;
+ dbcgs.szModule = acc.c_str();
+ dbcgs.szSetting = uid;
+ CallService(MS_DB_CONTACT_GETSETTING, (WPARAM)hContact, (LPARAM)&dbcgs);
+ std::string id;
+ bool found = false;
+ switch(dbcgs.pValue->type)
+ {
+ case DBVT_DELETED:
+ continue;
+ break;
+ case DBVT_BYTE:
+ {
+ char _id[64];
+ mir_snprintf(_id, 64, "%d", dbv.bVal);
+ id += _id;
+ if(id == contact_id)
+ found = true;
+ }
+ break;
+ case DBVT_WORD:
+ {
+ char _id[64];
+ mir_snprintf(_id, 64, "%d", dbv.wVal);
+ id += _id;
+ if(id == contact_id)
+ found = true;
+ }
+ break;
+ case DBVT_DWORD:
+ {
+ char _id[64];
+ mir_snprintf(_id, 64, "%d", dbv.dVal);
+ id += _id;
+ if(id == contact_id)
+ found = true;
+ }
+ break;
+ case DBVT_ASCIIZ:
+ {
+ id += dbcgs.pValue->pszVal;
+ CallService(MS_DB_CONTACT_FREEVARIANT, 0, (LPARAM)&dbv);
+ if(id == contact_id)
+ found = true;
+ }
+ break;
+ case DBVT_UTF8:
+ {
+ char *tmp = mir_utf8decodeA(dbcgs.pValue->pszVal);
+ if(tmp[0])
+ id += tmp;
+ mir_free(tmp);
+ CallService(MS_DB_CONTACT_FREEVARIANT, 0, (LPARAM)&dbv);
+ if(id == contact_id)
+ found = true;
+ }
+ break;
+ case DBVT_BLOB:
+ //TODO
+ CallService(MS_DB_CONTACT_FREEVARIANT, 0, (LPARAM)&dbv);
+ break;
+ case DBVT_WCHAR:
+ //TODO
+ CallService(MS_DB_CONTACT_FREEVARIANT, 0, (LPARAM)&dbv);
+ break;
+ }
+ if(found)
+ {
+ wstring path;
+ std::vector<std::wstring> cmd;
+ TCHAR *ptmp;
+ string output;
+ DWORD exitcode;
+ {
+ HANDLE hcnt = hContact;
+ ptmp = UniGetContactSettingUtf(NULL, szGPGModuleName, "szHomePath", _T(""));
+ path = ptmp;
+ mir_free(ptmp);
+ wstring rand = toUTF16(get_random(10));
+ path += L"\\";
+ path += rand;
+ boost::filesystem::remove(path);
+ wfstream f(path, std::ios::out);
+ f<<toUTF16(key).c_str();
+ f.close();
+ cmd.push_back(L"--batch");
+ cmd.push_back(L"--import");
+ cmd.push_back(path);
+ }
+ gpg_execution_params params(cmd);
+ pxResult result;
+ params.out = &output;
+ params.code = &exitcode;
+ params.result = &result;
+ if(!gpg_launcher(params))
+ break;
+ if(result == pxNotFound)
+ break;
+ if(result == pxSuccess)
+ processed_keys++;
+ {
+ if(output.find("already in secret keyring") != string::npos)
+ {
+ MessageBox(0, TranslateT("Key already in secret keyring."), TranslateT("Info"), MB_OK);
+ boost::filesystem::remove(path);
+ break;
+ }
+ char *tmp2;
+ string::size_type s = output.find("gpg: key ") + strlen("gpg: key ");
+ string::size_type s2 = output.find(":", s);
+ tmp2 = (char*)mir_alloc((output.substr(s,s2-s).length()+1) * sizeof(char));
+ strcpy(tmp2, output.substr(s,s2-s).c_str());
+ mir_utf8decode(tmp2, 0);
+ DBWriteContactSettingString(hContact, szGPGModuleName, "KeyID", tmp2);
+ mir_free(tmp2);
+ s = output.find("“", s2);
+ if(s == string::npos)
+ {
+ s = output.find("\"", s2);
+ s += 1;
+ }
+ else
+ s += 3;
+ if((s2 = output.find("(", s)) == string::npos)
+ s2 = 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));
+ strcpy(tmp2, output.substr(s,s2-s-1).c_str());
+ mir_utf8decode(tmp2, 0);
+ if(hContact)
+ {
+ DBWriteContactSettingString(hContact, szGPGModuleName, "KeyMainName", output.substr(s,s2-s-1).c_str());
+ }
+ mir_free(tmp2);
+ if((s = output.find(")", s2)) == string::npos)
+ 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));
+ strcpy(tmp2, output.substr(s2,s-s2).c_str());
+ mir_utf8decode(tmp2, 0);
+ if(hContact)
+ DBWriteContactSettingString(hContact, szGPGModuleName, "KeyComment", output.substr(s2,s-s2).c_str());
+ mir_free(tmp2);
+ s+=3;
+ s2 = output.find(">", s);
+ tmp2 = (char*)mir_alloc((output.substr(s,s2-s).length()+1) * sizeof(char));
+ strcpy(tmp2, output.substr(s,s2-s).c_str());
+ mir_utf8decode(tmp2, 0);
+ if(hContact)
+ DBWriteContactSettingString(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));
+ strcpy(tmp2, output.substr(s2,s-s2).c_str());
+ mir_utf8decode(tmp2, 0);
+ if(hContact)
+ DBWriteContactSettingString(hContact, szGPGModuleName, "KeyMainEmail", output.substr(s2,s-s2).c_str());
+ mir_free(tmp2);
+ }
+ }
+ DBWriteContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", 1);
+ DBWriteContactSettingTString(hContact, szGPGModuleName, "GPGPubKey", toUTF16(key).c_str());
+ }
+ boost::filesystem::remove(path);
+ break;
+ }
+ }
+ }
+ key.clear();
+ }
+ if(strstr(line, "-----END PGP PRIVATE KEY BLOCK-----"))
+ {
+ std::vector<wstring> cmd;
+ TCHAR tmp2[MAX_PATH] = {0};
+ TCHAR *ptmp;
+ string output;
+ DWORD exitcode;
+ {
+ ptmp = UniGetContactSettingUtf(NULL, szGPGModuleName, "szHomePath", _T(""));
+ _tcscpy(tmp2, ptmp);
+ mir_free(ptmp);
+ _tcscat(tmp2, _T("\\"));
+ _tcscat(tmp2, _T("temporary_exported.asc"));
+ boost::filesystem::remove(tmp2);
+ wfstream f(tmp2, std::ios::out);
+ f<<toUTF16(key).c_str();
+ f.close();
+ cmd.push_back(L"--batch");
+ cmd.push_back(L"--import");
+ cmd.push_back(tmp2);
+ }
+ gpg_execution_params params(cmd);
+ pxResult result;
+ params.out = &output;
+ params.code = &exitcode;
+ params.result = &result;
+ if(!gpg_launcher(params))
+ break;
+ if(result == pxNotFound)
+ break;
+ if(result == pxSuccess)
+ processed_private_keys++;
+ key.clear();
+ }
+ }
+ if(file.is_open())
+ file.close();
+ char msg[512];
+ if(processed_private_keys)
+ mir_snprintf(msg, 512, "we have succesfully processed %d public keys and some private keys", processed_keys);
+ else
+ mir_snprintf(msg, 512, "we have succesfully processed %d public keys", processed_keys);
+ MessageBoxA(NULL, msg, Translate("Keys import result"), MB_OK);
+ return 0;
+}
+
+void fix_line_term(std::string &s)
+{
+ if(s.empty())
+ return;
+ boost::algorithm::erase_all(s, "\r\r");
+}
+
+void fix_line_term(std::wstring &s)
+{
+ if(s.empty())
+ return;
+ boost::algorithm::erase_all(s, _T("\r\r"));
+}
+
+void strip_line_term(std::wstring &s)
+{
+ if(s.empty())
+ return;
+ boost::algorithm::erase_all(s, _T("\r"));
+ boost::algorithm::erase_all(s, _T("\n"));
+}
+
+void strip_line_term(std::string &s)
+{
+ if(s.empty())
+ return;
+ boost::algorithm::erase_all(s, "\r");
+ boost::algorithm::erase_all(s, "\n");
+}
+
+void strip_tags(std::wstring &str)
+{
+ if(str.empty())
+ return;
+ boost::algorithm::erase_all(str, inopentag);
+ boost::algorithm::erase_all(str, inclosetag);
+ boost::algorithm::erase_all(str, outopentag);
+ boost::algorithm::erase_all(str, outclosetag);
+}
+
+
+static INT_PTR CALLBACK DlgProcEncryptedFileMsgBox(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
+{
+ char *inkeyid = NULL;
+ switch (msg)
+ {
+ case WM_INITDIALOG:
+ {
+ TranslateDialogDefault(hwndDlg);
+ file_msg_state = -1;
+ return TRUE;
+ }
+
+
+ case WM_COMMAND:
+ {
+ switch (LOWORD(wParam))
+ {
+ case IDC_IGNORE:
+ if(IsDlgButtonChecked(hwndDlg, IDC_REMEMBER))
+ {
+ DBWriteContactSettingByte(NULL, szGPGModuleName, "bSameAction", 1);
+ bSameAction = true;
+ }
+ DestroyWindow(hwndDlg);
+ break;
+
+ case IDC_DECRYPT:
+ file_msg_state = 1;
+ if(IsDlgButtonChecked(hwndDlg, IDC_REMEMBER))
+ {
+ DBWriteContactSettingByte(NULL, szGPGModuleName, "bFileTransfers", 1);
+ bFileTransfers = true;
+ DBWriteContactSettingByte(NULL, szGPGModuleName, "bSameAction", 0);
+ bSameAction = false;
+ }
+
+ DestroyWindow(hwndDlg);
+ break;
+
+ default:
+ break;
+ }
+
+ break;
+ }
+
+ case WM_NOTIFY:
+ {
+ }
+ break;
+ case WM_CLOSE:
+ DestroyWindow(hwndDlg);
+ break;
+ case WM_DESTROY:
+ {
+ }
+ break;
+ }
+ return FALSE;
+}
+
+
+void ShowEncryptedFileMsgBox()
+{
+ extern HINSTANCE hInst;
+ DialogBox(hInst, MAKEINTRESOURCE(IDD_ENCRYPTED_FILE_MSG_BOX), NULL, DlgProcEncryptedFileMsgBox);
+}
+
+
+static INT_PTR CALLBACK DlgProcExportKeys(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
+{
+ switch (msg)
+ {
+ case WM_INITDIALOG:
+ {
+ TranslateDialogDefault(hwndDlg);
+ return TRUE;
+ }
+
+
+ case WM_COMMAND:
+ {
+ switch (LOWORD(wParam))
+ {
+ case IDC_OK:
+ if(IsDlgButtonChecked(hwndDlg, IDC_PUBLIC))
+ ExportGpGKeysFunc(0);
+ else if(IsDlgButtonChecked(hwndDlg, IDC_PRIVATE))
+ ExportGpGKeysFunc(1);
+ else if(IsDlgButtonChecked(hwndDlg, IDC_ALL))
+ ExportGpGKeysFunc(2);
+ DestroyWindow(hwndDlg);
+ break;
+
+ case IDC_CANCEL:
+ DestroyWindow(hwndDlg);
+ break;
+
+ default:
+ break;
+ }
+
+ break;
+ }
+
+ case WM_NOTIFY:
+ {
+ }
+ break;
+ case WM_CLOSE:
+ DestroyWindow(hwndDlg);
+ break;
+ case WM_DESTROY:
+ {
+ }
+ break;
+ }
+ return FALSE;
+}
+
+void ShowExportKeysDlg()
+{
+ extern HINSTANCE hInst;
+ DialogBox(hInst, MAKEINTRESOURCE(IDD_EXPORT_TYPE), NULL, DlgProcExportKeys);
+}
+
+static INT_PTR CALLBACK DlgProcChangePasswd(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
+{
+ switch (msg)
+ {
+ case WM_INITDIALOG:
+ {
+ TranslateDialogDefault(hwndDlg);
+ return TRUE;
+ }
+
+
+ case WM_COMMAND:
+ {
+ switch (LOWORD(wParam))
+ {
+ case IDC_OK:
+ //TODO: show some prgress
+ {
+ std::string old_pass, new_pass;
+ extern TCHAR key_id_global[17];
+ TCHAR buf[256] = {0};
+ GetDlgItemText(hwndDlg, IDC_NEW_PASSWD1, buf, 255);
+ new_pass = toUTF8(buf);
+ GetDlgItemText(hwndDlg, IDC_NEW_PASSWD2, buf, 255);
+ if(new_pass != toUTF8(buf))
+ {
+ MessageBox(hwndDlg, TranslateT("New passwords do not match"), TranslateT("Error"), MB_OK);
+ //key_id_global[0] = 0;
+ break;
+ }
+ GetDlgItemText(hwndDlg, IDC_OLD_PASSWD, buf, 255);
+ old_pass = toUTF8(buf);
+ bool old_pass_match = false;
+ TCHAR *pass = UniGetContactSettingUtf(NULL, szGPGModuleName, "szKeyPassword", _T(""));
+ if(!_tcscmp(pass,buf))
+ old_pass_match = true;
+ mir_free(pass);
+ if(!old_pass_match)
{
if(key_id_global[0])
{
@@ -2190,103 +2198,103 @@ static INT_PTR CALLBACK DlgProcChangePasswd(HWND hwndDlg, UINT msg, WPARAM wPara
old_pass_match = true;
mir_free(pass);
}
- }
- if(!old_pass_match)
- {
- if(MessageBox(hwndDlg, TranslateT("Old password not match, you can continue, but gpg will reject wrong password.\nDo you want to continue?"), TranslateT("Error"), MB_YESNO) == IDNO)
- {
- //key_id_global[0] = 0;
- break;
- }
- }
- std::vector<std::wstring> cmd;
- TCHAR tmp2[MAX_PATH] = {0};
- string output;
- DWORD exitcode;
- cmd.push_back(L"--edit-key");
- cmd.push_back(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, &params));
- if(!gpg_thread.timed_join(boost::posix_time::minutes(10)))
- {
- gpg_thread.~thread();
- if(params.child)
- boost::process::terminate(*(params.child));
- if(bDebugLog)
- debuglog<<std::string(time_str()+": GPG execution timed out, aborted");
- DestroyWindow(hwndDlg);
- break;
- }
- if(result == pxNotFound)
- break;
- //if(result == pxSuccess)
- //TODO: save to db
-
-
- }
- DestroyWindow(hwndDlg);
- break;
- default:
- break;
- }
-
- break;
- }
-
- case WM_NOTIFY:
- {
- }
- break;
- case WM_CLOSE:
- DestroyWindow(hwndDlg);
- break;
- case WM_DESTROY:
- {
- extern TCHAR key_id_global[17];
- key_id_global[0] = 0;
- }
- break;
- }
- return FALSE;
-}
-
-void ShowChangePasswdDlg()
-{
- extern HINSTANCE hInst;
- HWND hwndPaaswdDlg = NULL;
- hwndPaaswdDlg = CreateDialog(hInst, MAKEINTRESOURCE(IDD_CHANGE_PASSWD), NULL, DlgProcChangePasswd);
- SetForegroundWindow(hwndPaaswdDlg);
-}
-
-
-void clean_temp_dir()
-{
+ }
+ if(!old_pass_match)
+ {
+ if(MessageBox(hwndDlg, 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)
+ {
+ //key_id_global[0] = 0;
+ break;
+ }
+ }
+ std::vector<std::wstring> cmd;
+ TCHAR tmp2[MAX_PATH] = {0};
+ string output;
+ DWORD exitcode;
+ cmd.push_back(L"--edit-key");
+ cmd.push_back(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, &params));
+ if(!gpg_thread.timed_join(boost::posix_time::minutes(10)))
+ {
+ gpg_thread.~thread();
+ if(params.child)
+ boost::process::terminate(*(params.child));
+ if(bDebugLog)
+ debuglog<<std::string(time_str()+": GPG execution timed out, aborted");
+ DestroyWindow(hwndDlg);
+ break;
+ }
+ if(result == pxNotFound)
+ break;
+ //if(result == pxSuccess)
+ //TODO: save to db
+
+
+ }
+ DestroyWindow(hwndDlg);
+ break;
+ default:
+ break;
+ }
+
+ break;
+ }
+
+ case WM_NOTIFY:
+ {
+ }
+ break;
+ case WM_CLOSE:
+ DestroyWindow(hwndDlg);
+ break;
+ case WM_DESTROY:
+ {
+ extern TCHAR key_id_global[17];
+ key_id_global[0] = 0;
+ }
+ break;
+ }
+ return FALSE;
+}
+
+void ShowChangePasswdDlg()
+{
+ extern HINSTANCE hInst;
+ HWND hwndPaaswdDlg = NULL;
+ hwndPaaswdDlg = CreateDialog(hInst, MAKEINTRESOURCE(IDD_CHANGE_PASSWD), NULL, DlgProcChangePasswd);
+ SetForegroundWindow(hwndPaaswdDlg);
+}
+
+
+void clean_temp_dir()
+{
using namespace boost::filesystem;
char *mir_path = new char [MAX_PATH];
CallService(MS_UTILS_PATHTOABSOLUTE, (WPARAM)"\\", (LPARAM)mir_path);
wstring path = toUTF16(mir_path);
SetCurrentDirectoryA(mir_path);
delete [] mir_path;
- TCHAR *tmp = UniGetContactSettingUtf(NULL, szGPGModuleName, "szHomePath", _T(""));
- path += tmp;
- mir_free(tmp);
- path += L"\\tmp";
- if(exists(path) && is_directory(path))
- {
- boost::filesystem::path p(path);
- 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());
- }
- }
- }
+ TCHAR *tmp = UniGetContactSettingUtf(NULL, szGPGModuleName, "szHomePath", _T(""));
+ path += tmp;
+ mir_free(tmp);
+ path += L"\\tmp";
+ if(exists(path) && is_directory(path))
+ {
+ boost::filesystem::path p(path);
+ 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