summaryrefslogtreecommitdiff
path: root/plugins/ConnectionNotify/src/filter.cpp
blob: 593ef05c2137de6d52e30e92c2d26b1283e0f05a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include "stdafx.h"

HWND filterAddDlg = nullptr;
extern HINSTANCE hInst;
extern struct CONNECTION *connExceptions;
extern HANDLE hFilterOptionsThread;
extern DWORD FilterOptionsThreadId;
extern struct CONNECTION *connCurrentEdit;
extern BOOL settingDefaultAction;
extern HANDLE hExceptionsMutex;
extern BOOL bOptionsOpen;
static unsigned __stdcall filterQueue(void *dummy);
static INT_PTR CALLBACK ConnectionFilterEditProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

HANDLE startFilterThread()
{
	return (HANDLE)mir_forkthreadex(filterQueue, nullptr, (unsigned int*)&FilterOptionsThreadId);
}

static unsigned __stdcall filterQueue(void *)
{
	BOOL bRet;
	MSG msg;
	//while(1)
	while ((bRet = GetMessage(&msg, nullptr, 0, 0)) != 0)
	{
		if (msg.message == WM_ADD_FILTER)
		{
			struct CONNECTION *conn = (struct CONNECTION *)msg.lParam;
			filterAddDlg = CreateDialogParam(hInst, MAKEINTRESOURCE(IDD_FILTER_DIALOG), nullptr, ConnectionFilterEditProc, (LPARAM)conn);
			ShowWindow(filterAddDlg, SW_SHOW);

		}
		if (nullptr == filterAddDlg || !IsDialogMessage(filterAddDlg, &msg)) { /* Wine fix. */
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}
	hFilterOptionsThread = nullptr;
	return TRUE;
}

static INT_PTR CALLBACK ConnectionFilterEditProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
	case WM_INITDIALOG:
	{
		struct CONNECTION *conn = (struct CONNECTION*)lParam;
		TranslateDialogDefault(hWnd);

		SetDlgItemText(hWnd, ID_TEXT_NAME, conn->PName);
		SetDlgItemText(hWnd, ID_TXT_LOCAL_IP, conn->strIntIp);
		SetDlgItemText(hWnd, ID_TXT_REMOTE_IP, conn->strExtIp);
		SetDlgItemInt(hWnd, ID_TXT_LOCAL_PORT, conn->intIntPort, FALSE);
		SetDlgItemInt(hWnd, ID_TXT_REMOTE_PORT, conn->intExtPort, FALSE);
		SendDlgItemMessage(hWnd, ID_CBO_ACTION, CB_ADDSTRING, 0, (LPARAM)TranslateT("Always show popup"));
		SendDlgItemMessage(hWnd, ID_CBO_ACTION, CB_ADDSTRING, 0, (LPARAM)TranslateT("Never show popup"));
		SendDlgItemMessage(hWnd, ID_CBO_ACTION, CB_SETCURSEL, 0, 0);
		mir_free(conn);
		return TRUE;
	}
	case WM_ACTIVATE:
		if (0 == wParam)             // becoming inactive
			filterAddDlg = nullptr;
		else                         // becoming active
			filterAddDlg = hWnd;
		return FALSE;
	case WM_COMMAND:
		switch (LOWORD(wParam))
		{
		case ID_OK:
		{
			wchar_t tmpPort[6];
			if (bOptionsOpen)
			{
				MessageBox(hWnd, TranslateT("First close options window"), L"ConnectionNotify", MB_OK | MB_ICONSTOP);
				break;
			}
			if (WAIT_OBJECT_0 == WaitForSingleObject(hExceptionsMutex, 100))
			{
				if (connCurrentEdit == nullptr)
				{
					connCurrentEdit = (struct CONNECTION*)mir_alloc(sizeof(struct CONNECTION));
					connCurrentEdit->next = connExceptions;
					connExceptions = connCurrentEdit;
				}
				GetDlgItemText(hWnd, ID_TXT_LOCAL_PORT, tmpPort, _countof(tmpPort));
				if (tmpPort[0] == '*')
					connCurrentEdit->intIntPort = -1;
				else
					connCurrentEdit->intIntPort = GetDlgItemInt(hWnd, ID_TXT_LOCAL_PORT, nullptr, FALSE);
				GetDlgItemText(hWnd, ID_TXT_REMOTE_PORT, tmpPort, _countof(tmpPort));
				if (tmpPort[0] == '*')
					connCurrentEdit->intExtPort = -1;
				else
					connCurrentEdit->intExtPort = GetDlgItemInt(hWnd, ID_TXT_REMOTE_PORT, nullptr, FALSE);

				GetDlgItemText(hWnd, ID_TXT_LOCAL_IP, connCurrentEdit->strIntIp, _countof(connCurrentEdit->strIntIp));
				GetDlgItemText(hWnd, ID_TXT_REMOTE_IP, connCurrentEdit->strExtIp, _countof(connCurrentEdit->strExtIp));
				GetDlgItemText(hWnd, ID_TEXT_NAME, connCurrentEdit->PName, _countof(connCurrentEdit->PName));

				connCurrentEdit->Pid = !(BOOL)SendDlgItemMessage(hWnd, ID_CBO_ACTION, CB_GETCURSEL, 0, 0);
				connCurrentEdit = nullptr;
				saveSettingsConnections(connExceptions);
				ReleaseMutex(hExceptionsMutex);
			}
			//EndDialog(hWnd,IDOK);
			DestroyWindow(hWnd);
			return TRUE;
		}
		case ID_CANCEL:
			connCurrentEdit = nullptr;
			DestroyWindow(hWnd);
			//EndDialog(hWnd,IDCANCEL);
			return TRUE;
		}
		return FALSE;

		break;
	case WM_CLOSE:
		DestroyWindow(hWnd);
	case WM_DESTROY:
		filterAddDlg = nullptr;
		connCurrentEdit = nullptr;
		//DestroyWindow(hWnd);
		//PostQuitMessage(0);
		break;
	}
	return FALSE;
}

BOOL checkFilter(struct CONNECTION *head, struct CONNECTION *conn)
{
	for (struct CONNECTION *cur = head; cur != nullptr; cur = cur->next)
		if (wildcmpw(conn->PName, cur->PName) && wildcmpw(conn->strIntIp, cur->strIntIp) && wildcmpw(conn->strExtIp, cur->strExtIp)
			&& (cur->intIntPort == -1 || cur->intIntPort == conn->intIntPort) && (cur->intExtPort == -1 || cur->intExtPort == conn->intExtPort))
			return cur->Pid;

	return settingDefaultAction;
}