summaryrefslogtreecommitdiff
path: root/plugins/WhoUsesMyFiles/src/wumf.cpp
blob: c60a5377160278ef6b692b3effb487181ffb453f (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include "stdafx.h"

#define MAX_CHARS 4096
#define WS_WINDOW_STYLE WS_OVERLAPPED
#define NAME "WUMF"
#define WM_MYCMD 0x0401

PWumf list = nullptr;
PWumf lst = nullptr;

HANDLE hLogger = nullptr;
BOOL wumf();

static int DlgResizer(HWND, LPARAM, UTILRESIZECONTROL *urc)
{
	switch (urc->wId) {
	case IDC_CONNLIST:
		return RD_ANCHORX_WIDTH | RD_ANCHORY_HEIGHT;
	case IDOK:
		return RD_ANCHORX_RIGHT | RD_ANCHORY_BOTTOM;
	}
	return RD_ANCHORX_LEFT | RD_ANCHORY_TOP;
}

void AddToList(HWND hList, PWumf w)
{
	LVITEM lvi = {};
	lvi.iItem = ListView_GetItemCount(hList) + 1;
	lvi.mask = LVIF_PARAM | LVIF_TEXT;
	lvi.pszText = w->szID;
	lvi.lParam = (LPARAM)w;
	ListView_InsertItem(hList, &lvi);
}

void ShowList(PWumf l, HWND hList)
{
	PWumf w = l;
	while (w) {
		AddToList(hList, w);
		w = w->next;
	}
}

VOID OnGetDispInfo(NMLVDISPINFO *plvdi)
{
	PWumf w = (PWumf)(plvdi->item.lParam);
	switch (plvdi->item.iSubItem) {
	case 0:
		plvdi->item.pszText = w->szID;
		break;
	case 1:
		plvdi->item.pszText = w->szUser;
		break;
	case 2:
		plvdi->item.pszText = w->szPath;
		break;
	case 3:
		plvdi->item.pszText = w->szPerm;
		break;
	}
}

INT_PTR CALLBACK ConnDlgProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
	switch (Msg) {
	case WM_INITDIALOG:
		TranslateDialogDefault(hWnd);
		{
			HWND hList = GetDlgItem(hWnd, IDC_CONNLIST);

			ListView_SetExtendedListViewStyle(hList, LVS_EX_FULLROWSELECT);
			LV_COLUMN lvc = {};
			lvc.mask = LVCF_TEXT | LVCF_FMT | LVCF_WIDTH;
			lvc.fmt = LVCFMT_LEFT;
			lvc.cx = 40;
			lvc.pszText = L"ID";
			ListView_InsertColumn(hList, 0, &lvc);
			lvc.cx = 50;
			lvc.pszText = TranslateT("User");
			ListView_InsertColumn(hList, 1, &lvc);
			lvc.cx = 250;
			lvc.pszText = TranslateT("File");
			ListView_InsertColumn(hList, 2, &lvc);
			lvc.cx = 50;
			lvc.pszText = TranslateT("Access");
			ListView_InsertColumn(hList, 3, &lvc);
			KillTimer(nullptr, 777);
			lst = cpy_list(&list);
			if (IsUserAnAdmin())
				SetTimer(nullptr, 777, TIME, TimerProc);
			else
				MessageBox(nullptr, TranslateT("Plugin WhoUsesMyFiles requires admin privileges in order to work."), L"Miranda NG", MB_OK);
			ShowList(lst, hList);
		}
		Utils_RestoreWindowPosition(hWnd, NULL, MODULENAME, "conn");
		return TRUE;

	case WM_CLOSE:
		PostMessage(hWnd, WM_COMMAND, IDCANCEL, 0l);
		break;

	case WM_COMMAND:
		switch (LOWORD(wParam)) {
		case IDOK:
		case IDCANCEL:
			PostMessage(hWnd, WM_DESTROY, 0, 0);
			break;
		}
		break;

	case WM_SIZE:
		Utils_ResizeDialog(hWnd, g_plugin.getInst(), MAKEINTRESOURCEA(IDD_CONNLIST), DlgResizer);
		Utils_SaveWindowPosition(hWnd, NULL, MODULENAME, "conn");
		return TRUE;

	case WM_MOVE:
		Utils_SaveWindowPosition(hWnd, NULL, MODULENAME, "conn");
		break;

	case WM_NOTIFY:
		switch (((LPNMHDR)lParam)->code) {
		case LVN_GETDISPINFO:
			OnGetDispInfo((NMLVDISPINFO *)lParam);
			break;
		}
		break;

	case WM_DESTROY:
		del_all(&lst);
		PostQuitMessage(0);
		break;
	}
	return FALSE;
}

void LogWumf(PWumf w)
{
	if (!WumfOptions.LogFolders && (w->dwAttr & FILE_ATTRIBUTE_DIRECTORY)) return;

	if (hLogger == nullptr) {
		hLogger = mir_createLog("wumf", L"WhoIsUsingMyFiles log file", WumfOptions.LogFile, 0);
		if (hLogger == nullptr) {
			wchar_t str[256];
			mir_snwprintf(str, L"Can't open log file %s", WumfOptions.LogFile);
			MessageBox(nullptr, str, TranslateT("Error opening file"), MB_OK | MB_ICONSTOP);
			WumfOptions.LogToFile = FALSE;
			return;
		}
	}

	SYSTEMTIME time;
	GetLocalTime(&time);

	wchar_t lpDateStr[20], lpTimeStr[20];
	GetDateFormat(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &time, nullptr, lpDateStr, 20);
	GetTimeFormat(LOCALE_USER_DEFAULT, TIME_FORCE24HOURFORMAT | TIME_NOTIMEMARKER, &time, nullptr, lpTimeStr, 20);
	mir_writeLogW(hLogger, L"%s %s %20s\t%s\r\n", lpDateStr, lpTimeStr, w->szUser, w->szPath);
}

BOOL wumf()
{
	LPSESSION_INFO_1 s_info = nullptr;
	DWORD ent_read = 0, ent_total = 0, res_handle = 0;
	NET_API_STATUS res = NERR_Success;
	if ((res = NetSessionEnum(nullptr, nullptr, nullptr, 1, (LPBYTE *)&s_info, MAX_PREFERRED_LENGTH, &ent_read, &ent_total, &res_handle)) == NERR_Success ||
		res == ERROR_MORE_DATA) {
		mark_all(&list, TRUE);
		for (unsigned i = 0; i < ent_read; i++)
			process_session(s_info[i]);

		NetApiBufferFree(s_info);
	}
	else printError(res);

	return del_marked(&list);
}

void process_session(SESSION_INFO_1 s_info)
{
	LPFILE_INFO_3 f_info = nullptr;
	DWORD ent_read = 0, ent_total = 0, res_handle = 0;
	NET_API_STATUS res = NERR_Success;
	if ((res = NetFileEnum(nullptr, nullptr, s_info.sesi1_username, 3, (LPBYTE *)&f_info, MAX_PREFERRED_LENGTH, &ent_read, &ent_total, (PDWORD_PTR)&res_handle)) == NERR_Success ||
		res == ERROR_MORE_DATA) {
		for (unsigned i = 0; i < ent_read; i++)
			process_file(s_info, f_info[i]);

		NetApiBufferFree(f_info);
	}
	else printError(res);
}

void process_file(SESSION_INFO_1 s_info, FILE_INFO_3 f_info)
{
	PWumf w = fnd_cell(&list, f_info.fi3_id);
	if (!w) {
		w = new_wumf(f_info.fi3_id, f_info.fi3_username, f_info.fi3_pathname, s_info.sesi1_cname, nullptr, 0, f_info.fi3_permissions, GetFileAttributes(f_info.fi3_pathname));
		w->mark = FALSE;
		if (!add_cell(&list, w))
			msg(TranslateT("Error memory allocation"));

		if (g_plugin.bPopups) ShowWumfPopup(w);
		if (WumfOptions.LogToFile) LogWumf(w);
	}
	else w->mark = FALSE;
}

void printError(uint32_t res)
{
	LPVOID lpMsgBuf;
	FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, res, 0, (LPTSTR)&lpMsgBuf, 0, nullptr);
	OutputDebugString((LPCTSTR)lpMsgBuf);
	msg((LPCTSTR)lpMsgBuf);
	LocalFree(lpMsgBuf);
}

VOID CALLBACK TimerProc(HWND, UINT, UINT_PTR, DWORD)
{
	if (!wumf())
		KillTimer(nullptr, 777);
};

void FreeAll()
{
	del_all(&list);
}