renamed a few files
[mereo.git] / src / autostart.asm
blobe794fb83b94608c06bd051bb3aa23043785274d9
1 ; Mereo - An HTTP server.
2 ; Copyright (C) 2008 Jelle Geerts
4 ; This program is free software: you can redistribute it and/or modify
5 ; it under the terms of the GNU General Public License as published by
6 ; the Free Software Foundation, either version 3 of the License, or
7 ; (at your option) any later version.
9 ; This program is distributed in the hope that it will be useful,
10 ; but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ; GNU General Public License for more details.
14 ; You should have received a copy of the GNU General Public License
15 ; along with this program. If not, see <http://www.gnu.org/licenses/>.
17 AutoStartDialogProc proc uses ebx esi hwndDlg:DWORD, uMsg:DWORD, wParam:DWORD, lParam:DWORD
18 LOCAL hTempWnd :DWORD
19 LOCAL lbGetInt :DWORD
20 LOCAL lbDirBrowse :DWORD
21 LOCAL lbHidden :DWORD
22 LOCAL lTempArg[8] :BYTE
23 LOCAL lPathBuffer[2048] :BYTE
24 LOCAL ip_bind_buffer[16] :BYTE
26 .if [uMsg] == WM_INITDIALOG
27 invoke SendMessage, [hwndDlg], WM_SETICON, ICON_BIG, [hMainIcon]
29 ; ========================================================================================
30 ; = Parse all auto-start options and set them =
31 ; ========================================================================================
33 ; Parse the port (if any) and set it (or set the default)
34 invoke GetCmdLineArgInt, ADDR autostart_arguments, ADDR CmdArgP
35 .if eax == -1
36 invoke GetDlgItemInt, [ghwndDlg], ID_MAIN_PORT, ADDR lbGetInt, FALSE
37 .if eax == 0 || eax > 0FFFFh
38 mov eax, [HTTP_DEFAULT_PORT]
39 .endif
40 .endif
41 invoke SetDlgItemInt, [hwndDlg], ID_AUTOSTART_PORT, eax, FALSE
43 ; Parse the 'start hidden' option
44 invoke GetCmdLineArgPtr, ADDR autostart_arguments, ADDR CmdArgHide
45 .if eax != -1
46 invoke GetDlgItem, [hwndDlg], IDC_AUTOSTART_HIDDEN
47 invoke SendMessage, eax, BM_SETCHECK, BST_CHECKED, 0
48 .endif
50 ; Parse the 'Directory Browsing' option
51 invoke GetCmdLineArgPtr, ADDR autostart_arguments, ADDR CmdArgDE
52 mov ebx, eax
53 .if eax != -1
54 invoke GetDlgItem, [hwndDlg], IDC_AUTOSTART_DIRBROWSING
55 invoke SendMessage, eax, BM_SETCHECK, BST_CHECKED, 0
56 .elseif
57 invoke GetDlgItem, [ghwndDlg], IDC_DIRBROWSING
58 invoke SendMessage, eax, BM_GETCHECK, 0, 0
59 mov ebx, eax
60 invoke GetDlgItem, [hwndDlg], IDC_AUTOSTART_DIRBROWSING
61 invoke SendMessage, eax, BM_SETCHECK, ebx, 0
62 .endif
64 ; Parse the 'Webroot' option
65 invoke GetCmdLineArgString, ADDR autostart_arguments, ADDR CmdArgH
66 mov ebx, eax
67 .if ebx != -1
68 invoke GetDlgItem, [hwndDlg], ID_AUTOSTART_PATH
69 invoke SendMessage, eax, WM_SETTEXT, 0, ebx
70 invoke GlobalFree, ebx
71 .else
72 invoke GetDlgItemText, [ghwndDlg], IDE_ROOT, ADDR lPathBuffer, SIZEOF lPathBuffer
73 invoke SetDlgItemText, [hwndDlg], ID_AUTOSTART_PATH, ADDR lPathBuffer
74 .endif
76 ; Parse the 'Bind Address' option
77 invoke GetDlgItem, [hwndDlg], IDE_AUTOSTART_BIND_ADDRESS
78 mov ebx, eax
79 invoke GetCmdLineArgString, ADDR autostart_arguments, ADDR CmdArgB
80 mov esi, eax
81 .if esi != -1
82 invoke SendMessage, ebx, WM_SETTEXT, 0, esi
83 invoke GlobalFree, esi
84 .else
85 invoke SendMessage, [hBindAddress], WM_GETTEXT, SIZEOF ip_bind_buffer, ADDR ip_bind_buffer
86 invoke SendMessage, ebx, WM_SETTEXT, 0, ADDR ip_bind_buffer
87 .endif
88 .elseif [uMsg] == WM_COMMAND
89 .if [wParam] == ID_AUTOSTART_FINISH
90 ; Validate path
91 invoke GetDlgItemText, [hwndDlg], ID_AUTOSTART_PATH, ADDR lPathBuffer, SIZEOF lPathBuffer
92 .if eax == 0
93 invoke MessageBox, [hwndDlg], ADDR input_path_error, ADDR app_name, MB_ICONSTOP
94 jmp @@EXIT
95 .endif
97 invoke GetFileAttributes, ADDR lPathBuffer
98 .if eax == -1
99 invoke MessageBox, [hwndDlg], ADDR host_path_invalid, ADDR app_name, MB_ICONSTOP
100 jmp @@EXIT
101 .elseif ! (eax & 010h)
102 invoke MessageBox, [hwndDlg], ADDR host_path_not_a_dir, ADDR app_name, MB_ICONSTOP
103 jmp @@EXIT
104 .endif
106 ; Validate port
107 invoke GetDlgItemInt, [hwndDlg], ID_AUTOSTART_PORT, ADDR lbGetInt, FALSE
108 .if eax == 0 || eax > 0FFFFh
109 invoke MessageBox, [hwndDlg], ADDR invalid_port, ADDR app_name, MB_ICONSTOP
110 jmp @@EXIT
111 .endif
112 mov [lbGetInt], eax ; Save port number
114 ; Check the states of the checkboxes
115 invoke GetDlgItem, [hwndDlg], IDC_AUTOSTART_DIRBROWSING
116 invoke SendMessage, eax, BM_GETCHECK, 0, 0
117 mov [lbDirBrowse], eax
118 invoke GetDlgItem, [hwndDlg], IDC_AUTOSTART_HIDDEN
119 invoke SendMessage, eax, BM_GETCHECK, 0, 0
120 mov [lbHidden], eax
122 ; ========================================================================================
123 ; = Combine all arguments and put them into the argument buffer, then close the dialog =
124 ; ========================================================================================
125 invoke ClearBuffer, ADDR autostart_arguments, SIZEOF autostart_arguments
126 .if [lbDirBrowse] == BST_CHECKED
127 invoke StrCat, ADDR autostart_arguments, ADDR CmdArgDE
128 invoke StrCat, ADDR autostart_arguments, ADDR WhitespacePadding
129 .endif
131 .if [lbHidden] == BST_CHECKED
132 invoke StrCat, ADDR autostart_arguments, ADDR CmdArgHide
133 invoke StrCat, ADDR autostart_arguments, ADDR WhitespacePadding
134 .endif
136 .if [lbGetInt] != HTTP_DEFAULT_PORT
137 invoke StrCat, ADDR autostart_arguments, ADDR CmdArgP
138 invoke StrCat, ADDR autostart_arguments, ADDR WhitespacePadding
139 invoke dwtoa, [lbGetInt], ADDR lTempArg
140 invoke StrCat, ADDR autostart_arguments, ADDR lTempArg
141 invoke StrCat, ADDR autostart_arguments, ADDR WhitespacePadding
142 .endif
144 invoke GetDlgItem, [hwndDlg], IDE_AUTOSTART_BIND_ADDRESS
145 lea edx, [ip_bind_buffer]
146 invoke SendMessage, eax, WM_GETTEXT, SIZEOF ip_bind_buffer, edx
147 or eax, eax
148 jz @F
149 invoke StrCat, ADDR autostart_arguments, ADDR CmdArgB
150 invoke StrCat, ADDR autostart_arguments, ADDR WhitespacePadding
151 invoke StrCat, ADDR autostart_arguments, ADDR ip_bind_buffer
152 invoke StrCat, ADDR autostart_arguments, ADDR WhitespacePadding
155 invoke StrCat, ADDR autostart_arguments, ADDR CmdArgH
156 invoke StrCat, ADDR autostart_arguments, ADDR WhitespacePadding
157 mov byte ptr [lTempArg], '"'
158 mov byte ptr [lTempArg+1], 0
159 invoke StrCat, ADDR autostart_arguments, ADDR lTempArg
160 invoke StrCat, ADDR autostart_arguments, ADDR lPathBuffer
161 mov byte ptr [lTempArg], '"'
162 mov byte ptr [lTempArg+1], 0
163 invoke StrCat, ADDR autostart_arguments, ADDR lTempArg
165 ; Close the dialog
166 invoke EndDialog, [hwndDlg], 0
167 .elseif [wParam] == ID_AUTOSTART_BROWSE
168 invoke SHGetOpenDirName, [hwndDlg], ADDR lPathBuffer
169 or eax, eax
170 jz @@EXIT
171 invoke SetDlgItemText, [hwndDlg], ID_AUTOSTART_PATH, ADDR lPathBuffer
172 .elseif [wParam] == IDC_AUTOSTART_DIRBROWSING || [wParam] == IDC_AUTOSTART_HIDDEN
173 invoke GetDlgItem, [hwndDlg], [wParam]
174 mov [hTempWnd], eax
175 invoke SendMessage, eax, BM_GETCHECK, 0, 0
176 .if eax == BST_CHECKED
177 mov eax, BST_UNCHECKED
178 .elseif eax == BST_UNCHECKED
179 mov eax, BST_CHECKED
180 .endif
181 invoke SendMessage, [hTempWnd], BM_SETCHECK, eax, 0
182 .elseif [wParam] == IDCANCEL
183 invoke SendMessage, [hwndDlg], WM_CLOSE, 0, 0
184 .endif
185 .elseif [uMsg] == WM_CLOSE
186 invoke EndDialog, [hwndDlg], -2
187 .endif
189 @@EXIT:
190 xor eax, eax
192 AutoStartDialogProc endp
194 ; Retrieves the current startup setting for Mereo
195 ; Returns -1 on error (no settings/no access to view the settings)
196 ; Returns 0 if using HKEY_CURRENT_USER (HKCU) as current startup method or using HKCU and HKLM both as current startup method
197 ; Returns 1 if using HKEY_LOCAL_MACHINE (HKLM) as current startup method
198 GetAutoStartSetting proc
199 LOCAL kHandle :DWORD
200 LOCAL kResult :DWORD
201 LOCAL ResultHKCU :DWORD
202 LOCAL ResultHKLM :DWORD
203 LOCAL temp :DWORD
204 LOCAL temp2 :DWORD
206 mov [ResultHKCU], FALSE
207 mov [ResultHKLM], FALSE
208 invoke RegCreateKeyEx, HKEY_CURRENT_USER, ADDR autostart_run, 0, 0, REG_OPTION_NON_VOLATILE, \
209 KEY_ALL_ACCESS, 0, ADDR kHandle, ADDR kResult
210 .if eax == 0
211 mov [temp], SIZEOF autostart_arguments
212 mov [temp2], REG_SZ
213 invoke RegQueryValueEx, [kHandle], ADDR app_name, 0, \
214 ADDR temp2, ADDR autostart_arguments, ADDR temp
215 .if eax == 0
216 mov [ResultHKCU], TRUE
217 .endif
218 invoke RegCloseKey, [kHandle]
219 .endif
221 invoke RegCreateKeyEx, HKEY_LOCAL_MACHINE, ADDR autostart_run, 0, 0, REG_OPTION_NON_VOLATILE, \
222 KEY_ALL_ACCESS, 0, ADDR kHandle, ADDR kResult
223 .if eax == 0
224 mov [temp], SIZEOF autostart_arguments
225 mov [temp2], REG_SZ
226 invoke RegQueryValueEx, [kHandle], ADDR app_name, 0, \
227 ADDR temp2, ADDR autostart_arguments, ADDR temp
228 .if eax == 0
229 mov [ResultHKLM], TRUE
230 .endif
231 invoke RegCloseKey, [kHandle]
232 .endif
234 .if [ResultHKLM] == TRUE
235 mov eax, 1
236 .endif
238 .if [ResultHKCU] == TRUE
239 xor eax, eax
240 .endif
242 .if [ResultHKCU] == FALSE && [ResultHKLM] == FALSE
243 mov eax, -1
244 .endif
246 GetAutoStartSetting endp
248 ; Deletes all auto-start settings for Mereo
249 DeleteAutoStartSettings proc newSetting:DWORD
250 LOCAL kHandle :DWORD
251 LOCAL kResult :DWORD
253 .if [newSetting] == HKEY_LOCAL_MACHINE
254 ; If new setting should be HKLM then old setting HKCU needs to be deleted
255 mov ecx, HKEY_CURRENT_USER
256 .elseif [newSetting] == HKEY_CURRENT_USER
257 ; And vice versa
258 mov ecx, HKEY_LOCAL_MACHINE
259 .endif
261 invoke RegCreateKeyEx, ecx, ADDR autostart_run, 0, 0, REG_OPTION_NON_VOLATILE, \
262 KEY_ALL_ACCESS, 0, ADDR kHandle, ADDR kResult
263 .if eax == 0
264 invoke RegDeleteValue, [kHandle], ADDR app_name
265 invoke RegCloseKey, [kHandle]
266 .endif
268 DeleteAutoStartSettings endp
270 ; Writes the new auto-start setting to the registry
271 ; Returns -1 if an unknown setting value has been specified
272 ; Returns 0 if successful
273 SetAutoStartSetting proc uses edi newSetting:DWORD, delete:DWORD
274 LOCAL temp :DWORD
275 LOCAL temp2 :DWORD
276 LOCAL tempKey :DWORD
277 LOCAL kResult :DWORD
279 mov eax, [newSetting]
280 .if eax == HKEY_CURRENT_USER || eax == HKEY_LOCAL_MACHINE
281 ; Set new settings
282 invoke RegCreateKeyEx, [newSetting], ADDR autostart_run, 0, 0, REG_OPTION_NON_VOLATILE, \
283 KEY_ALL_ACCESS, 0, ADDR hKey, ADDR kResult
284 .if eax == 0
285 .if [newSetting] == HKEY_LOCAL_MACHINE
286 ; If new setting should be HKLM then old setting HKCU should be retrieved (to display the old arguments so user can edit it)
287 mov ecx, HKEY_CURRENT_USER
288 .elseif [newSetting] == HKEY_CURRENT_USER
289 ; And vice versa
290 mov ecx, HKEY_LOCAL_MACHINE
291 .endif
293 invoke RegCreateKeyEx, ecx, ADDR autostart_run, 0, 0, REG_OPTION_NON_VOLATILE, \
294 KEY_ALL_ACCESS, 0, ADDR tempKey, ADDR kResult
295 mov [temp], SIZEOF autostart_arguments
296 mov [temp2], REG_SZ
297 invoke RegQueryValueEx, [tempKey], ADDR app_name, 0, \
298 ADDR temp2, ADDR autostart_arguments, ADDR temp
299 invoke RegCloseKey, [tempKey]
300 ; Parse current arguments
301 cmp byte ptr [autostart_arguments], 0
302 jz @@DIALOG
303 cmp byte ptr [autostart_arguments], '"'
304 jnz @@SEARCH_SPACE
305 invoke StrLen, ADDR autostart_arguments
306 mov ecx, eax
307 mov eax, '"'
308 mov edi, OFFSET autostart_arguments
310 repnz scasb
311 jnz @@CLEAR
312 repnz scasb
313 jnz @@CLEAR
314 inc edi
315 jmp @@FOUND
316 @@CLEAR:
317 xor eax, eax
318 mov ecx, (128/4)
319 mov edi, OFFSET autostart_arguments
320 rep stosd
321 @@SEARCH_SPACE:
322 invoke StrLen, ADDR autostart_arguments
323 mov ecx, eax
324 mov eax, ' '
325 mov edi, OFFSET autostart_arguments
327 repnz scasb
328 @@FOUND:
329 invoke StrCpy, ADDR autostart_arguments, edi
330 @@DIALOG:
331 invoke DialogBoxParam, [hInstance], ID_AUTOSTART_DIALOG, [ghwndDlg], ADDR AutoStartDialogProc, 0
332 .if eax == 0
333 ; Delete current settings
334 invoke DeleteAutoStartSettings, [newSetting]
335 invoke GetModuleFileName, 0, ADDR autostart_tempbuf, 2048
336 invoke wsprintf, ADDR autostart_runline, ADDR autostart_runtemplate, ADDR autostart_tempbuf, ADDR autostart_arguments
337 invoke StrLen, ADDR autostart_runline
338 inc eax
339 ; Set the new settings
340 invoke RegSetValueEx, [hKey], ADDR app_name, 0, \
341 REG_SZ, ADDR autostart_runline, eax
342 push eax
343 invoke RegCloseKey, [hKey]
344 pop eax
345 .if eax != 0
346 mov eax, -1
348 .endif
349 .if eax != 0
350 mov eax, -1
351 .endif
352 .endif
353 .else
354 mov eax, -1
356 .endif
357 .elseif [delete] == TRUE
358 ; No auto-start, delete current settings
359 invoke DeleteAutoStartSettings, HKEY_CURRENT_USER
360 invoke DeleteAutoStartSettings, HKEY_LOCAL_MACHINE
361 mov byte ptr [autostart_arguments], 0
362 .else
363 ; Unknown requested new setting, ignore
364 mov eax, -1
366 .endif
368 SetAutoStartSetting endp