summaryrefslogtreecommitdiff
path: root/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'main.cpp')
-rw-r--r--main.cpp278
1 files changed, 273 insertions, 5 deletions
diff --git a/main.cpp b/main.cpp
index 6a58405..9921b65 100644
--- a/main.cpp
+++ b/main.cpp
@@ -30,13 +30,16 @@ void test()
}
-HWND hwndFirstRun = NULL, hwndSetDirs = NULL, hwndNewKey = NULL;
+HWND hwndFirstRun = NULL, hwndSetDirs = NULL, hwndNewKey = NULL, hwndKeyGen = NULL;
int itemnum = 0;
+HWND hwndList_g = NULL;
+
static BOOL CALLBACK DlgProcFirstRun(HWND hwndDlg,UINT msg,WPARAM wParam,LPARAM lParam)
{
HWND hwndList=GetDlgItem(hwndDlg, IDC_KEY_LIST);
+ hwndList_g = hwndList;
LVCOLUMN col = {0};
LVITEM item = {0};
NMLISTVIEW * hdr = (NMLISTVIEW *) lParam;
@@ -77,12 +80,12 @@ static BOOL CALLBACK DlgProcFirstRun(HWND hwndDlg,UINT msg,WPARAM wParam,LPARAM
ListView_InsertColumn(hwndList, 4, &col);
ListView_SetExtendedListViewStyleEx(hwndList, 0, LVS_EX_FULLROWSELECT);
int i = 1, iRow = 0;
- { //parse gpg output
+ {
item.mask = LVIF_TEXT;
item.iItem = i;
item.iSubItem = 0;
item.pszText = _T("");
- {
+ {//parse gpg output
string out;
DWORD code;
string::size_type p = 0, p2 = 0, stop = 0;
@@ -149,6 +152,10 @@ static BOOL CALLBACK DlgProcFirstRun(HWND hwndDlg,UINT msg,WPARAM wParam,LPARAM
{
switch (LOWORD(wParam))
{
+ case IDC_GENERATE_KEY:
+ void ShowKeyGenDialog();
+ ShowKeyGenDialog();
+ break;
case ID_OK:
ListView_GetItemText(hwndList, itemnum, 0, fp, 16);
{
@@ -275,7 +282,7 @@ static BOOL CALLBACK DlgProcGpgBinOpts(HWND hwndDlg, UINT msg, WPARAM wParam, LP
DestroyWindow(hwndDlg);
break;
case WM_DESTROY:
- hwndFirstRun = NULL;
+ hwndSetDirs = NULL;
break;
}
@@ -333,7 +340,258 @@ static BOOL CALLBACK DlgProcNewKeyDialog(HWND hwndDlg, UINT msg, WPARAM wParam,
DestroyWindow(hwndDlg);
break;
case WM_DESTROY:
- hwndFirstRun = NULL;
+ hwndNewKey = NULL;
+ break;
+
+ }
+ return FALSE;
+}
+static BOOL CALLBACK DlgProcKeyGenDialog(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
+{
+ switch (msg)
+ {
+ case WM_INITDIALOG:
+ {
+ TranslateDialogDefault(hwndDlg);
+ ComboBoxAddStringUtf(GetDlgItem(hwndDlg, IDC_KEY_TYPE), _T("RSA"), 0);
+ ComboBoxAddStringUtf(GetDlgItem(hwndDlg, IDC_KEY_TYPE), _T("DSA"), 0);
+ return TRUE;
+ }
+
+
+ case WM_COMMAND:
+ {
+ switch (LOWORD(wParam))
+ {
+ case IDCANCEL:
+ DestroyWindow(hwndDlg);
+ break;
+ case IDOK:
+ {
+ wstring path;
+ { //data sanity checks
+ TCHAR *tmp = new TCHAR [5];
+ GetDlgItemText(hwndDlg, IDC_KEY_TYPE, tmp, 5);
+ if(_tcslen(tmp) < 3)
+ {
+ mir_free(tmp);
+ MessageBox(0, _T("You must set encryption algorythm first"), _T("Error"), MB_OK);
+ break;
+ }
+ mir_free(tmp);
+ tmp = new TCHAR [5];
+ GetDlgItemText(hwndDlg, IDC_KEY_LENGTH, tmp, 5);
+ int length = _ttoi(tmp);
+ mir_free(tmp);
+ if(length < 1024 || length > 4096)
+ {
+ MessageBox(0, _T("Key length must be of length from 1024 to 4096 bits"), _T("Error"), MB_OK);
+ break;
+ }
+ tmp = new TCHAR [12];
+ GetDlgItemText(hwndDlg, IDC_KEY_EXPIRE_DATE, tmp, 12);
+ if(_tcslen(tmp) != 10 && tmp[0] != '0')
+ {
+ MessageBox(0, _T("Invalid date"), _T("Error"), MB_OK);
+ delete [] tmp;
+ break;
+ }
+ delete [] tmp;
+ tmp = new TCHAR [128];
+ GetDlgItemText(hwndDlg, IDC_KEY_REAL_NAME, tmp, 128);
+ if(_tcslen(tmp) < 5)
+ {
+ MessageBox(0, _T("Name must contain at least 5 characters"), _T("Error"), MB_OK);
+ delete [] tmp;
+ break;
+ }
+ delete [] tmp;
+ tmp = new TCHAR [128];
+ GetDlgItemText(hwndDlg, IDC_KEY_EMAIL, tmp, 128);
+ if((_tcslen(tmp)) < 5 || (!_tcschr(tmp, _T('@'))) || (!_tcschr(tmp, _T('.'))))
+ {
+ MessageBox(0, _T("Invalid Email"), _T("Error"), MB_OK);
+ delete [] tmp;
+ break;
+ }
+ delete [] tmp;
+ }
+ { //generating key file
+ TCHAR *tmp = UniGetContactSettingUtf(NULL, szModuleName, "szHomePath", _T(""));
+ char *tmp2;// = mir_t2a(tmp);
+ path = tmp;
+ mir_free(tmp);
+ // mir_free(tmp2);
+ path.append(_T("\\new_key"));
+ wfstream f(path.c_str(), std::ios::out);
+ if(!f.is_open())
+ {
+ MessageBox(0, _T("Failed to open file"), _T("Error"), MB_OK);
+ break;
+ }
+ f<<"Key-Type: ";
+ tmp = new TCHAR [5];
+ GetDlgItemText(hwndDlg, IDC_KEY_TYPE, tmp, 5);
+ tmp2 = mir_t2a(tmp);
+ char *subkeytype = new char [6];
+ if(strstr(tmp2, "RSA"))
+ strcpy(subkeytype, "RSA");
+ else if(strstr(tmp2, "DSA")) //this is useless check for now, but it will be required if someone add another key types support
+ strcpy(subkeytype, "ELG-E");
+ delete [] tmp;
+ f<<tmp2;
+ mir_free(tmp2);
+ f<<"\n";
+ f<<"Key-Length: ";
+ tmp = new TCHAR [5];
+ GetDlgItemText(hwndDlg, IDC_KEY_LENGTH, tmp, 5);
+ int length = _ttoi(tmp);
+ delete [] tmp;
+ f<<length;
+ f<<"\n";
+ f<<"Subkey-Type: ";
+ f<<subkeytype;
+ delete [] subkeytype;
+ f<<"\n";
+ tmp = new TCHAR [64]; //i hope this is enough for password
+ GetDlgItemText(hwndDlg, IDC_KEY_PASSWD, tmp, 64);
+ if(_tcslen(tmp) > 0)
+ {
+ f<<"Passphrase: ";
+ tmp2 = mir_utf8encodeW(tmp);
+ f<<tmp2;
+ f<<"\n";
+ mir_free(tmp2);
+ }
+ delete [] tmp;
+ f<<"Name-Real: ";
+ tmp = new TCHAR [128];
+ GetDlgItemText(hwndDlg, IDC_KEY_REAL_NAME, tmp, 128);
+ tmp2 = mir_utf8encodeW(tmp);
+ f<<tmp2;
+ mir_free(tmp2);
+ delete [] tmp;
+ f<<"\n";
+ tmp = new TCHAR [512];
+ GetDlgItemText(hwndDlg, IDC_KEY_COMMENT, tmp, 512);
+ if(_tcslen(tmp) > 0)
+ {
+ tmp2 = mir_utf8encodeW(tmp);
+ f<<"Name-Comment: ";
+ f<<tmp2;
+ f<<"\n";
+ }
+ mir_free(tmp2);
+ delete [] tmp;
+ f<<"Name-Email: ";
+ tmp = new TCHAR [128];
+ GetDlgItemText(hwndDlg, IDC_KEY_EMAIL, tmp, 128);
+ tmp2 = mir_utf8encodeW(tmp);
+ f<<tmp2;
+ mir_free(tmp2);
+ delete [] tmp;
+ f<<"\n";
+ f<<"Expire-Date: ";
+ tmp = new TCHAR [12];
+ GetDlgItemText(hwndDlg, IDC_KEY_EXPIRE_DATE, tmp, 12);
+ tmp2 = mir_utf8encodeW(tmp);
+ f<<tmp2;
+ mir_free(tmp2);
+ delete [] tmp;
+ f<<"\n";
+ f.close();
+ }
+ { //gpg execution
+ DWORD code;
+ string out;
+ wstring cmd;
+ cmd += _T("--batch --yes --gen-key \"");
+ cmd += path;
+ cmd += _T("\"");
+ pxExecute(&cmd, "", &out, &code);
+ }
+ DeleteFile(path.c_str());
+ DestroyWindow(hwndDlg);
+ {//parse gpg output
+ LVITEM item = {0};
+ int i = 1, iRow = 0;
+ item.mask = LVIF_TEXT;
+ item.iItem = i;
+ item.iSubItem = 0;
+ item.pszText = _T("");
+ string out;
+ DWORD code;
+ string::size_type p = 0, p2 = 0, stop = 0;
+ {
+ wstring cmd = _T("--list-secret-keys");
+ if(pxExecute(&cmd, "", &out, &code) == pxNotFound)
+ {
+ MessageBox(0, _T("Set path to gpg.exe first!"), _T("Warning"), MB_OK);
+ break;
+ }
+ }
+ cp866_to_cp1251(&out);
+ ListView_DeleteAllItems(hwndList_g);
+ while(p != string::npos)
+ {
+ if((p = out.find("sec ", p)) == string::npos)
+ break;
+ p += 5;
+ if(p < stop)
+ break;
+ stop = p;
+ p2 = out.find("/", p) - 1;
+ TCHAR *tmp = mir_a2t(out.substr(p,p2-p).c_str());
+ item.pszText = tmp;
+ iRow = ListView_InsertItem(hwndList_g, &item);
+ ListView_SetItemText(hwndList_g, iRow, 4, tmp);
+ mir_free(tmp);
+ p2+=2;
+ p = out.find(" ", p2);
+ tmp = mir_a2t(out.substr(p2,p-p2).c_str());
+ ListView_SetItemText(hwndList_g, iRow, 0, tmp);
+ mir_free(tmp);
+ p = out.find("uid ", p);
+ p2 = out.find_first_not_of(" ", p+5);
+ p = out.find("<", p2);
+ tmp = mir_a2t(out.substr(p2,p-p2).c_str());
+ ListView_SetItemText(hwndList_g, iRow, 2, tmp);
+ mir_free(tmp);
+ p++;
+ p2 = out.find(">", p);
+ tmp = mir_a2t(out.substr(p,p2-p).c_str());
+ ListView_SetItemText(hwndList_g, iRow, 1, tmp);
+ mir_free(tmp);
+ p = out.find("ssb ", p2) + 6;
+ p = out.find(" ", p) + 1;
+ p2 = out.find("\n", p);
+ tmp = mir_a2t(out.substr(p,p2-p-1).c_str());
+ ListView_SetItemText(hwndList_g, iRow, 3, tmp);
+ mir_free(tmp);
+ i++;
+ }
+ }
+ }
+ break;
+ default:
+ break;
+ }
+
+ break;
+ }
+
+ case WM_NOTIFY:
+ {
+ switch (((LPNMHDR)lParam)->code)
+ {
+ }
+ }
+ break;
+ case WM_CLOSE:
+ DestroyWindow(hwndDlg);
+ break;
+ case WM_DESTROY:
+ hwndKeyGen = NULL;
break;
}
@@ -369,6 +627,16 @@ void ShowNewKeyDialog()
SetForegroundWindow(hwndNewKey);
}
+void ShowKeyGenDialog()
+{
+ if (hwndKeyGen == NULL)
+ {
+ hwndKeyGen = CreateDialog(hInst, MAKEINTRESOURCE(IDD_KEY_GEN), NULL, DlgProcKeyGenDialog);
+ }
+ SetForegroundWindow(hwndKeyGen);
+}
+
+
void FirstRun()