summaryrefslogtreecommitdiff
path: root/plugins/ProxySwitch/src/proxy.cpp
blob: 45ac91f3036ee0e1b7a42c4515872fb8ab73ba14 (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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
proxySwitch

The plugin watches IP address changes, reports them via popups and adjusts
the proxy settings of Miranda and Internet Explorer accordingly.
*/

#include "stdafx.h"

/* ################################################################################ */

int Enum_Settings(const char *szSetting, LPARAM lParam)
{
	PPROXY_SETTINGS ps = (PPROXY_SETTINGS)lParam;

	if (mir_strcmp(szSetting, "NLUseProxy") != 0 && mir_strcmpi(szSetting, "useproxy") != 0)
		return 0;

	if (ps->count >= ps->_alloc) {
		ps->_alloc += 10;
		ps->item = (PPROXY_SETTING)mir_realloc(ps->item, ps->_alloc * sizeof(PROXY_SETTING));
		ZeroMemory(&(ps->item[ps->count]), 10 * sizeof(PROXY_SETTING));
	}
	mir_strncpy(ps->item[ps->count].ModuleName, ps->_current_module, MAXLABELLENGTH - 1);
	mir_strncpy(ps->item[ps->count].SettingName, szSetting, MAXLABELLENGTH - 1);
	ps->count++;

	return 0;
}

int Enum_Modules(const char *szModuleName, DWORD, LPARAM lParam) 
{
	//DBCONTACTENUMSETTINGS e;
	MCONTACT hContact = NULL;

	((PPROXY_SETTINGS)lParam)->_current_module = szModuleName;

	//e.pfnEnumProc = Enum_Settings;
	//e.lParam = lParam;
	//e.szModule = szModuleName;
	db_enum_settings(hContact, (DBSETTINGENUMPROC)Enum_Settings, szModuleName, (void*)lParam);
	//CallService(MS_DB_CONTACT_ENUMSETTINGS, (WPARAM)hContact,(LPARAM)&e);

	return 0;
}

/* ################################################################################ */

void Create_Proxy_Settings_List(PPROXY_SETTINGS ps) 
{
	ZeroMemory(ps, sizeof(PROXY_SETTINGS));
	ps->_alloc = 10;
	ps->item = (PPROXY_SETTING)mir_alloc(ps->_alloc * sizeof(PROXY_SETTING));
	ZeroMemory(ps->item, ps->_alloc * sizeof(PROXY_SETTING));

	db_enum_modules((DBMODULEENUMPROC)Enum_Modules);
	//CallService(MS_DB_MODULES_ENUM, (WPARAM)ps, (LPARAM)Enum_Modules );

	ps->_alloc = ps->count + 1;
	ps->item = (PPROXY_SETTING)mir_realloc(ps->item, ps->_alloc * sizeof(PROXY_SETTING));
	ZeroMemory(&(ps->item[ps->count]), sizeof(PROXY_SETTING));
	ps->_current_module = NULL;
}

/* ################################################################################ */

void Free_Proxy_Settings_List(PPROXY_SETTINGS ps) 
{
	if (ps->item)
		mir_free(ps->item);
	ZeroMemory(ps, sizeof(PROXY_SETTINGS));
}

/* ################################################################################ */

char Get_Miranda_Proxy_Status(void) 
{
	PROXY_SETTINGS ps;
	int i, p;
	char proxy;
	proxy = PROXY_NO_CONFIG;

	Create_Proxy_Settings_List(&ps);
	for (i = 0; i < ps.count; i++) {
		p = db_get_b(0, ps.item[i].ModuleName, ps.item[i].SettingName, FALSE);
		if (proxy == PROXY_NO_CONFIG) {
			proxy = p;
			continue;
		}
		if (proxy != p) {
			proxy = PROXY_MIXED;
			break;
		}
	}
	Free_Proxy_Settings_List(&ps);
	return proxy;
}

/* ################################################################################ */

void Set_Miranda_Proxy_Status(char proxy) 
{
	PROXY_SETTINGS ps;
	NETLIBUSERSETTINGS nlus;
	int i;

	if (proxy < 0)
		return;
	Create_Proxy_Settings_List(&ps);
	for (i = 0; i < ps.count; i++) {
		if (ps.item[i].SettingName[0] != 0)
			db_set_b(0, ps.item[i].ModuleName, ps.item[i].SettingName, proxy);
		ZeroMemory(&nlus, sizeof(nlus));
		nlus.cbSize = sizeof(nlus);
		if (Netlib_GetUserSettingsByName(ps.item[i].ModuleName, &nlus)) {
			nlus.useProxy = proxy;
			nlus.szProxyAuthPassword = NEWSTR_ALLOCA(nlus.szProxyAuthPassword);
			nlus.szProxyAuthUser = NEWSTR_ALLOCA(nlus.szProxyAuthUser);
			nlus.szProxyServer = NEWSTR_ALLOCA(nlus.szProxyServer);
			nlus.szIncomingPorts = NEWSTR_ALLOCA(nlus.szIncomingPorts);
			nlus.szOutgoingPorts = NEWSTR_ALLOCA(nlus.szOutgoingPorts);
			Netlib_SetUserSettingsByName(ps.item[i].ModuleName, &nlus);
		}
	}
	Free_Proxy_Settings_List(&ps);
}

/* ################################################################################ */

char Get_IE_Proxy_Status(void)
{
	INTERNET_PER_CONN_OPTION_LIST    list;
	INTERNET_PER_CONN_OPTION         option[1];
	unsigned long                    nSize = sizeof(INTERNET_PER_CONN_OPTION_LIST);

	option[0].dwOption = INTERNET_PER_CONN_FLAGS;

	list.dwSize = sizeof(INTERNET_PER_CONN_OPTION_LIST);
	list.pszConnection = NULL;
	list.dwOptionCount = 1;
	list.dwOptionError = 0;
	list.pOptions = option;

	if (!InternetQueryOption(NULL, INTERNET_OPTION_PER_CONNECTION_OPTION, &list, &nSize))
		return -1;

	return option[0].Value.dwValue & PROXY_TYPE_PROXY ? 1 : 0;
}

/* ################################################################################ */

void Set_IE_Proxy_Status(char proxy)
{
	INTERNET_PER_CONN_OPTION_LIST    list;
	INTERNET_PER_CONN_OPTION         option[1];
	unsigned long                    nSize = sizeof(INTERNET_PER_CONN_OPTION_LIST);

	if (proxy < 0)
		return;
	option[0].dwOption = INTERNET_PER_CONN_FLAGS;
	option[0].Value.dwValue = proxy ? PROXY_TYPE_PROXY | PROXY_TYPE_DIRECT : PROXY_TYPE_DIRECT;

	list.dwSize = sizeof(INTERNET_PER_CONN_OPTION_LIST);
	list.pszConnection = NULL;
	list.dwOptionCount = 1;
	list.dwOptionError = 0;
	list.pOptions = option;

	if (!InternetSetOption(NULL, INTERNET_OPTION_PER_CONNECTION_OPTION, &list, nSize))
		return;

	InternetQueryOption(NULL, INTERNET_OPTION_SETTINGS_CHANGED, NULL, 0);
	InternetQueryOption(NULL, INTERNET_OPTION_REFRESH, NULL, 0);
}

/* ################################################################################ */

char Get_Firefox_Proxy_Status(void)
{
	wchar_t path[MAX_PATH];
	wchar_t prefs[MAX_PATH];
	char line[500];
	FILE *fP;
	struct _stat info;
	struct _wfinddata_t dir;
	long hFile;
	char *setting;
	int p, proxy;

	ZeroMemory(&info, sizeof(info));
	proxy = PROXY_NO_CONFIG;
	if (!SHGetSpecialFolderPath(NULL, path, CSIDL_APPDATA, 0))
		return proxy;
	mir_wstrcat(path, L"\\Mozilla\\Firefox\\Profiles\\*");
	if ((hFile = _wfindfirst(path, &dir)) != -1L) {
		do {
			if (!(dir.attrib & _A_SUBDIR) || dir.name[0] == '.')
				continue;
			mir_wstrcpy(prefs, path);
			prefs[mir_wstrlen(prefs) - 1] = 0;
			mir_wstrcat(prefs, dir.name);
			mir_wstrcat(prefs, L"\\prefs.js");
			if ((fP = _wfopen(prefs, L"r")) != NULL) {
				p = 0;
				while (fgets(line, 500, fP)) {
					if ((setting = strstr(line, "user_pref(\"network.proxy.type\",")) != NULL) {
						setting += 31;
						p = atoi(setting);
						p = p == 3 ? 0 : p > 0;
						break;
					}
				}
				fclose(fP);
				proxy = proxy == -2 ? p : (proxy == p ? p : -1);
			}
		} while (_wfindnext(hFile, &dir) == 0);
		_findclose(hFile);
	}
	return proxy;
}

/* ################################################################################ */

void Set_Firefox_Proxy_Status(char proxy) 
{
	wchar_t path[MAX_PATH];
	wchar_t prefsR[MAX_PATH];
	wchar_t prefsW[MAX_PATH];
	char line[500];
	FILE *fR, *fW;
	struct _stat info;
	struct _wfinddata_t dir;
	long hFile;
	char done;

	ZeroMemory(&info, sizeof(info));
	if (!SHGetSpecialFolderPath(NULL, path, CSIDL_APPDATA, 0))
		return;
	mir_wstrcat(path, L"\\Mozilla\\Firefox\\Profiles\\*");
	if ((hFile = _wfindfirst(path, &dir)) != -1L) {
		do {
			if (!(dir.attrib & _A_SUBDIR) || dir.name[0] == '.')
				continue;
			mir_wstrcpy(prefsR, path);
			prefsR[mir_wstrlen(prefsR) - 1] = 0;
			mir_wstrcat(prefsR, dir.name);
			mir_wstrcat(prefsR, L"\\prefs.js");
			done = 0;
			if ((fR = _wfopen(prefsR, L"r")) != NULL) {
				mir_wstrcpy(prefsW, prefsR);
				mir_wstrcat(prefsW, L"~");
				if ((fW = _wfopen(prefsW, L"w")) != NULL) {
					while (fgets(line, 500, fR)) {
						if (strstr(line, "\"network.proxy.type\""))
							continue;
						if (strstr(line, "\"network.proxy") && !done) {
							fprintf(fW, "user_pref(\"network.proxy.type\", %d);\n", proxy);
							done = 1;
						}
						fprintf(fW, "%s", line);
					}
					if (!done) {
						fprintf(fW, "user_pref(\"network.proxy.type\", %d);\n", proxy);
						done = 1;
					}
					fclose(fW);
				}
				fclose(fR);
			}
			if (done) {
				_wremove(prefsR);
				_wrename(prefsW, prefsR);
			}
		} while (_wfindnext(hFile, &dir) == 0);
		_findclose(hFile);
	}
}

/* ################################################################################ */

char Firefox_Installed(void) 
{
	wchar_t path[MAX_PATH];
	struct _stat info;
	ZeroMemory(&info, sizeof(info));

	if (SHGetSpecialFolderPath(NULL, path, CSIDL_APPDATA, 0)) {
		mir_wstrcat(path, L"\\Mozilla\\Firefox\\Profiles");
		if (_wstat(path, &info) == 0 && (info.st_mode & _S_IFDIR) == _S_IFDIR)
			return 1;
	}
	return 0;
}

/* ################################################################################ */

void Disconnect_All_Protocols(PPROTO_SETTINGS settings, int disconnect) 
{
	int count = 0, c, i, status;
	PROTOCOLDESCRIPTOR **plist;

	Proto_EnumProtocols(&c, &plist);

	ZeroMemory(settings, sizeof(PROTO_SETTINGS));
	settings->item = (PPROTO_SETTING)mir_alloc(c * sizeof(PROTO_SETTING));
	ZeroMemory(settings->item, c * sizeof(PROTO_SETTING));

	for (i = 0; i < c; i++) {
		if (plist[i]->type != PROTOTYPE_PROTOCOL)
			continue;
		if (CallProtoService(plist[i]->szName, PS_GETCAPS, PFLAGNUM_2, 0) == 0)
			continue;
		status = CallProtoService(plist[i]->szName, PS_GETSTATUS, 0, 0);
		mir_strncpy(settings->item[count].ProtoName, plist[i]->szName, MAXLABELLENGTH - 1);
		if (status != ID_STATUS_OFFLINE && disconnect) {
			CallProtoService(plist[i]->szName, PS_SETSTATUS, ID_STATUS_OFFLINE, 0);
		}
		if (status < MAX_CONNECT_RETRIES) 
			status = ID_STATUS_ONLINE;
		if (status == ID_STATUS_OFFLINE) 
			status = ID_STATUS_ONLINE;
		settings->item[count].Status = status;
		count++;
	}
	settings->count = count;
}

/* ################################################################################ */

void Connect_All_Protocols(PPROTO_SETTINGS settings) 
{
	int i;
	for (i = 0; i < settings->count; i++) {
		CallProtoService(settings->item[i].ProtoName, PS_SETSTATUS, settings->item[i].Status, 0);
	}
	mir_free(settings->item);
	ZeroMemory(settings, sizeof(PROTO_SETTINGS));
}