'------------------------------------------------------------------
'  smburl3.vbs (C) Walter Doekes 20070426, license: public domain
'------------------------------------------------------------------
' 
' Call with "register" argument to add the smb:// url handler.
' Call with "unregister" argument to undo "register".
' Call with any "smb://..." url to open the windows file:// url
' handler.
'
' Why?
' Because Firefox and Opera try to "download" file:// urls
' instead of "opening" them. Now you can use smb:// urls in
' your LAN search engine and you can open the files.
'
' Where?
' Use it on any Windows that has WScript/VBS.
'
'------------------------------------------------------------------

option explicit
on error resume next ' putting "argc == 1 and" here doesn't work because of "and"
if wscript.arguments(0) <> "register" and wscript.arguments(0) <> "unregister" _
		and left(wscript.arguments(0), 6) <> "smb://" then
	wscript.echo _
		"Usage: smburl3.vbs URL/COMMAND" & vbcrlf & _
		"  URL must be an ""smb://""-style url" & vbcrlf & _
		"  COMMAND must be ""register"" or ""unregister"""
	wscript.quit 1
end if
on error goto 0


dim objWshell, objWnetwork
if wscript.arguments(0) = "register" then
	set objWshell = wscript.createObject("Wscript.shell")
	objWshell.regWrite "HKEY_LOCAL_MACHINE\SOFTWARE\Classes\smb\", "URL:SMB URI"
	objWshell.regWrite "HKEY_LOCAL_MACHINE\SOFTWARE\Classes\smb\URL Protocol", "", "REG_SZ"
	objWshell.regWrite "HKEY_LOCAL_MACHINE\SOFTWARE\Classes\smb\Shell\Open\Command\", _
		"""" & wscript.fullName & """ """ & wscript.scriptFullName & """ ""%L"""
	wscript.echo "Registered the smb:// protocol in the registry."
elseif wscript.arguments(0) = "unregister" then
	set objWshell = wscript.createObject("Wscript.shell")
	on error resume next
	objWshell.regDelete "HKEY_LOCAL_MACHINE\SOFTWARE\Classes\smb\Shell\Open\Command\"
	objWshell.regDelete "HKEY_LOCAL_MACHINE\SOFTWARE\Classes\smb\Shell\Open\"
	objWshell.regDelete "HKEY_LOCAL_MACHINE\SOFTWARE\Classes\smb\Shell\"
	objWshell.regDelete "HKEY_LOCAL_MACHINE\SOFTWARE\Classes\smb\URL Protocol"
	objWshell.regDelete "HKEY_LOCAL_MACHINE\SOFTWARE\Classes\smb\"
	on error goto 0
	wscript.echo "Removed the smb:// protocol from the registry."
else
	dim strFile, strComputer, pos
	set objWshell = wscript.createObject("WScript.Shell")
	strFile = replace(mid(wscript.arguments(0), 5), "/", "\")
	strFile = replace(strFile, """", "")
	' If the computer asks for credentials, the ProtocolHandler will fail.
	' Therefore we map the computer as a guest user first.
	pos = instr(3, strFile, "\")
	if pos = 0 then strComputer = strFile else strComputer = left(strFile, pos - 1)
	set objWnetwork = wscript.CreateObject("WScript.Network")
	' Try to make a connection either way
	on error resume next
	objWnetwork.mapNetworkDrive "", strComputer, false, "guest"
	objWnetwork.mapNetworkDrive "", strComputer, false, "usernamethatdoesntexist"
	on error goto 0
	' Now we can open the share(d file).. hopefully
	objWshell.run "rundll32.exe url.dll,FileProtocolHandler """ & strFile & """"
end if
