1 |
werner |
36 |
/* wptProxySettingsDlg.cpp - Dialog for the proxy settings |
2 |
twoaday |
225 |
* Copyright (C) 2002-2006 Timo Schulz |
3 |
werner |
36 |
* |
4 |
|
|
* This file is part of WinPT. |
5 |
|
|
* |
6 |
|
|
* WinPT is free software; you can redistribute it and/or modify |
7 |
|
|
* it under the terms of the GNU General Public License as published by |
8 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
9 |
|
|
* (at your option) any later version. |
10 |
|
|
* |
11 |
|
|
* WinPT is distributed in the hope that it will be useful, |
12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
|
* GNU General Public License for more details. |
15 |
|
|
* |
16 |
|
|
* You should have received a copy of the GNU General Public License |
17 |
|
|
* along with WinPT; if not, write to the Free Software Foundation, |
18 |
|
|
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
19 |
|
|
*/ |
20 |
|
|
#ifdef HAVE_CONFIG_H |
21 |
|
|
#include <config.h> |
22 |
|
|
#endif |
23 |
|
|
|
24 |
|
|
#include <windows.h> |
25 |
werner |
47 |
#include <ctype.h> |
26 |
twoaday |
270 |
#include <stdio.h> |
27 |
werner |
36 |
|
28 |
werner |
47 |
#include "resource.h" |
29 |
werner |
36 |
#include "wptTypes.h" |
30 |
twoaday |
271 |
#include "wptGPG.h" |
31 |
werner |
36 |
#include "wptKeyserver.h" |
32 |
|
|
#include "wptW32API.h" |
33 |
|
|
#include "wptErrors.h" |
34 |
|
|
#include "wptNLS.h" |
35 |
twoaday |
180 |
#include "wptRegistry.h" |
36 |
werner |
36 |
|
37 |
twoaday |
185 |
#define combo_get_sel(dlg, id) \ |
38 |
|
|
SendDlgItemMessage ((dlg), (id), CB_GETCURSEL, 0, 0) |
39 |
werner |
36 |
|
40 |
twoaday |
225 |
|
41 |
twoaday |
270 |
static int |
42 |
|
|
set_reg_entry_keyserver_int (const char *key, int val) |
43 |
|
|
{ |
44 |
|
|
char buf[32]; |
45 |
|
|
|
46 |
|
|
_snprintf (buf, DIM (buf)-1, "%d", val); |
47 |
|
|
return set_reg_entry_keyserver (key, buf); |
48 |
|
|
} |
49 |
|
|
|
50 |
|
|
|
51 |
|
|
/* Save the proxy settings in the registry. */ |
52 |
|
|
int |
53 |
|
|
set_reg_proxy_prefs (keyserver_proxy_t prox) |
54 |
|
|
{ |
55 |
|
|
int rc; |
56 |
|
|
|
57 |
|
|
rc = set_reg_entry_keyserver_int ("Proto", prox->proto); |
58 |
|
|
if (!rc) |
59 |
|
|
rc = set_reg_entry_keyserver ("Host", prox->host? prox->host : ""); |
60 |
|
|
if (!rc) |
61 |
|
|
rc = set_reg_entry_keyserver_int ("Port", prox->port); |
62 |
|
|
if (!rc) |
63 |
|
|
rc = set_reg_entry_keyserver ("User", prox->user? prox->user : ""); |
64 |
|
|
if (!rc) |
65 |
|
|
rc = set_reg_entry_keyserver ("Pass", prox->pass? prox->pass : ""); |
66 |
|
|
return rc; |
67 |
|
|
} |
68 |
|
|
|
69 |
|
|
|
70 |
twoaday |
225 |
/* Check if the edit control (@id) contains a valid number. */ |
71 |
werner |
36 |
static int |
72 |
|
|
check_number (HWND dlg, int id) |
73 |
|
|
{ |
74 |
|
|
char buf[32]; |
75 |
|
|
size_t i; |
76 |
|
|
|
77 |
twoaday |
270 |
i = GetDlgItemText (dlg, id, buf, DIM (buf)-1); |
78 |
twoaday |
225 |
if (!i) |
79 |
|
|
return -1; |
80 |
twoaday |
270 |
for (i = 0; i < strlen (buf); i++) { |
81 |
twoaday |
225 |
if (!isdigit (buf[i])) |
82 |
werner |
36 |
return -1; |
83 |
|
|
} |
84 |
|
|
return 0; |
85 |
|
|
} |
86 |
|
|
|
87 |
|
|
|
88 |
|
|
static int |
89 |
twoaday |
180 |
read_proxy (HWND dlg, keyserver_proxy_t ctx) |
90 |
werner |
36 |
{ |
91 |
|
|
char t[512]; |
92 |
|
|
int ncount = 0, pos = 0; |
93 |
|
|
|
94 |
twoaday |
180 |
ncount = GetDlgItemText (dlg, IDC_PROXY_PWD, t, DIM (t)-1); |
95 |
|
|
if (ncount > 0) |
96 |
twoaday |
225 |
ctx->pass = m_strdup (t); |
97 |
twoaday |
180 |
ncount = GetDlgItemText (dlg, IDC_PROXY_USER, t, DIM (t)-1); |
98 |
|
|
if (ncount > 0) |
99 |
twoaday |
225 |
ctx->user = m_strdup (t); |
100 |
twoaday |
180 |
ncount = GetDlgItemText (dlg, IDC_PROXY_HOST, t, DIM (t)-1); |
101 |
|
|
if (ncount > 0) { |
102 |
werner |
36 |
if (check_IP_or_hostname (t)) { |
103 |
|
|
msg_box (dlg, _("Invalid host/IP address."), _("Proxy Settings"), MB_ERR); |
104 |
|
|
return -1; |
105 |
|
|
} |
106 |
|
|
/* XXX: check prefix */ |
107 |
|
|
if (!strncmp (t, "http://", 7)) |
108 |
|
|
pos = 7; |
109 |
twoaday |
225 |
ctx->host = m_strdup (t + pos); |
110 |
werner |
36 |
} |
111 |
twoaday |
185 |
else { |
112 |
|
|
msg_box (dlg, _("Please enter the proxy hostname."), |
113 |
|
|
_("Proxy Settings"), MB_ERR); |
114 |
|
|
return -1; |
115 |
|
|
} |
116 |
|
|
|
117 |
werner |
36 |
if (check_number (dlg, IDC_PROXY_PORT)) { |
118 |
|
|
msg_box (dlg, _("Invalid port number."), _("Proxy Settings"), MB_ERR); |
119 |
|
|
return -1; |
120 |
|
|
} |
121 |
|
|
|
122 |
twoaday |
180 |
ctx->port = GetDlgItemInt (dlg, IDC_PROXY_PORT, NULL, FALSE); |
123 |
|
|
if (ctx->port < 0 || ctx->port > 65535) { |
124 |
werner |
36 |
msg_box (dlg, _("Please select a value from 0-65535 for the port"), |
125 |
|
|
_("Proxy Settings"), MB_INFO); |
126 |
|
|
return -1; |
127 |
|
|
} |
128 |
twoaday |
180 |
if (ctx->user && !ctx->pass || !ctx->user && ctx->pass) { |
129 |
werner |
36 |
msg_box (dlg, _("When you want to use authentication, " |
130 |
|
|
"please fill out both fields."), _("Proxy Settings"), MB_ERR); |
131 |
|
|
return -1; |
132 |
|
|
} |
133 |
|
|
if (ctx->host && !ctx->port || !ctx->host && ctx->port) { |
134 |
|
|
msg_box (dlg, _("Please enter a host name and a port."), |
135 |
|
|
_("Proxy Settings"), MB_INFO); |
136 |
|
|
return -1; |
137 |
|
|
} |
138 |
twoaday |
185 |
ctx->proto = combo_get_sel (dlg, IDC_PROXY_PROTO); |
139 |
werner |
36 |
return 0; |
140 |
|
|
} |
141 |
|
|
|
142 |
|
|
|
143 |
twoaday |
225 |
/* Enable or disable the authentication elements based on |
144 |
|
|
the value @val. */ |
145 |
werner |
36 |
static void |
146 |
|
|
enable_proxy_auth (HWND dlg, int val) |
147 |
|
|
{ |
148 |
|
|
int mode = val? TRUE : FALSE; |
149 |
|
|
EnableWindow (GetDlgItem (dlg, IDC_PROXY_PWD), mode); |
150 |
|
|
EnableWindow (GetDlgItem (dlg, IDC_PROXY_USER), mode); |
151 |
|
|
} |
152 |
|
|
|
153 |
|
|
|
154 |
twoaday |
225 |
/* Fill in all valid proxy protocols. */ |
155 |
twoaday |
180 |
static void |
156 |
|
|
init_proxy_protocols (HWND dlg) |
157 |
|
|
{ |
158 |
|
|
HWND cb = GetDlgItem (dlg, IDC_PROXY_PROTO); |
159 |
twoaday |
185 |
combox_add_string (cb, (char *)"NONE"); |
160 |
twoaday |
180 |
combox_add_string (cb, (char *)"HTTP"); |
161 |
twoaday |
225 |
/*XXX: combox_add_string (cb, (char *)"SOCKS5");*/ |
162 |
twoaday |
180 |
SendMessage (cb, CB_SETCURSEL, (WPARAM)0, 0); |
163 |
|
|
} |
164 |
|
|
|
165 |
|
|
|
166 |
werner |
36 |
/* Dialog box procedure for proxy authentication. */ |
167 |
|
|
BOOL CALLBACK |
168 |
|
|
keyserver_proxy_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
169 |
twoaday |
180 |
{ |
170 |
|
|
int auth = 0; |
171 |
|
|
|
172 |
werner |
36 |
switch (msg) { |
173 |
|
|
case WM_INITDIALOG: |
174 |
twoaday |
180 |
if (proxy.host != NULL) { |
175 |
|
|
SetDlgItemText (dlg, IDC_PROXY_HOST, proxy.host); |
176 |
|
|
SetDlgItemInt( dlg, IDC_PROXY_PORT, proxy.port, FALSE ); |
177 |
|
|
if (proxy.user != NULL) { |
178 |
|
|
SetDlgItemText (dlg, IDC_PROXY_USER, proxy.user); |
179 |
werner |
36 |
CheckDlgButton (dlg, IDC_PROXY_AUTH, BST_CHECKED); |
180 |
|
|
auth++; |
181 |
|
|
} |
182 |
twoaday |
180 |
if (proxy.pass != NULL) { |
183 |
|
|
SetDlgItemText (dlg, IDC_PROXY_PWD, proxy.pass); |
184 |
werner |
36 |
auth++; |
185 |
|
|
} |
186 |
|
|
} |
187 |
twoaday |
180 |
enable_proxy_auth (dlg, auth); |
188 |
twoaday |
78 |
SetDlgItemText (dlg, IDC_PROXY_HOSTINF, _("Proxy host name or IP address")); |
189 |
werner |
36 |
SetDlgItemText (dlg, IDC_PROXY_AUTH, _("Server requires &authentication")); |
190 |
twoaday |
78 |
SetDlgItemText (dlg, IDC_PROXY_USERINF, _("User name")); |
191 |
|
|
SetDlgItemText (dlg, IDC_PROXY_PWDINF, _("Password")); |
192 |
twoaday |
180 |
SetDlgItemText (dlg, IDC_PROXY_PROTOINF, _("Proxy type")); |
193 |
|
|
SetDlgItemText (dlg, IDC_PROXY_AUTHINF, _("Authentication")); |
194 |
werner |
36 |
SetWindowText (dlg, _("Proxy Settings")); |
195 |
twoaday |
180 |
init_proxy_protocols (dlg); |
196 |
twoaday |
185 |
SendDlgItemMessage (dlg, IDC_PROXY_PROTO, CB_SETCURSEL, |
197 |
|
|
(WPARAM)proxy.proto, 0); |
198 |
twoaday |
78 |
center_window (dlg, NULL); |
199 |
werner |
36 |
SetForegroundWindow (dlg); |
200 |
|
|
break; |
201 |
|
|
|
202 |
|
|
case WM_COMMAND: |
203 |
|
|
switch (HIWORD (wparam)) { |
204 |
twoaday |
185 |
case CBN_SELCHANGE: |
205 |
|
|
if (combo_get_sel (dlg, (int)LOWORD (wparam))) { |
206 |
|
|
} |
207 |
|
|
else { |
208 |
|
|
} |
209 |
|
|
break; |
210 |
|
|
|
211 |
werner |
36 |
case BN_CLICKED: |
212 |
twoaday |
180 |
switch ((int)LOWORD (wparam)) { |
213 |
werner |
36 |
case IDC_PROXY_AUTH: |
214 |
|
|
if (IsDlgButtonChecked (dlg, IDC_PROXY_AUTH)) |
215 |
|
|
enable_proxy_auth (dlg, 1); |
216 |
|
|
else |
217 |
|
|
enable_proxy_auth (dlg, 0); |
218 |
|
|
break; |
219 |
|
|
} |
220 |
|
|
break; |
221 |
|
|
} |
222 |
|
|
|
223 |
twoaday |
180 |
switch (LOWORD (wparam)) { |
224 |
werner |
36 |
case IDOK: |
225 |
|
|
if (IsDlgButtonChecked (dlg, IDC_PROXY_AUTH) && |
226 |
|
|
item_get_text_length (dlg, IDC_PROXY_USER) == 0 && |
227 |
|
|
item_get_text_length (dlg, IDC_PROXY_PWD) == 0) { |
228 |
|
|
msg_box (dlg, _("Please fill out all required fields for authentication."), |
229 |
|
|
_("Proxy Settings"), MB_WARN); |
230 |
|
|
return TRUE; |
231 |
|
|
} |
232 |
twoaday |
180 |
kserver_proxy_release (&proxy); |
233 |
twoaday |
185 |
if (combo_get_sel (dlg, IDC_PROXY_PROTO) != PROXY_PROTO_NONE) { |
234 |
|
|
if (read_proxy (dlg, &proxy)) |
235 |
|
|
return FALSE; |
236 |
|
|
} |
237 |
twoaday |
180 |
set_reg_proxy_prefs (&proxy); |
238 |
werner |
36 |
EndDialog (dlg, TRUE); |
239 |
|
|
return TRUE; |
240 |
|
|
|
241 |
|
|
case IDCANCEL: |
242 |
|
|
EndDialog (dlg, FALSE); |
243 |
|
|
return FALSE; |
244 |
|
|
} |
245 |
|
|
break; |
246 |
|
|
} |
247 |
|
|
|
248 |
|
|
return FALSE; |
249 |
|
|
} |