'*******************************************************************************
'* Source Code         : NeedHelpFromChris.vbs                                 *
'* Description         : A simple script that gets the IP address currently    *
'*                       used by the Internet connection.  It then tries to    *
'*                       launch TightVNC if it's installed on the computer     *
'*                       (which it should be because I'll be installing it     *
'*                       myself).  If it can't find TightVNC in the default    *
'*                       location, it attempts to download it.                 *
'* Version             : 1.0.1                                                 *
'* Programmer          : Christopher V. Bellini                                *
'*                                                                             *
'* ----------------                                                            *
'* | REVISION LOG |                                                            *
'*-----------------------------------------------------------------------------*
'* Programmers Notes:                                                          *
'*     - cvb (12/17/2003): V1.0.0 completed in time to use in Crystal Falls so *
'*                         I can remotely control their computers with TightVNC*
'*                         when neccessary later on.  I used VBS intead of a   *
'*                         compiled language like C++, VB or Delphi so that if *
'*                         anything changes in the future, they can make the   *
'*                         changes themselves (with my guideance).  If worse   *
'*                         comes to worse, I can whip up a EXE that works off  *
'*                         of an INI file.                                     *
'*                                                                             *
'*     - cvb (01/17/2004): V1.0.1 Added GetExternalIP() to handle situations   *
'*                         where the user is behind a NAT router.              *
'*                                                                             *
'* ////////////////////////////////////////////////////////////////////////////*
'*  This program is free software; you can redistribute it and/or modify       *
'*  it under the terms of the GNU General Public License as published by       *
'*  the Free Software Foundation; either version 2 of the License, or          *
'*  (at your option) any later version.                                        *
'*                                                                             *
'*  This program is distributed in the hope that it will be useful, but        *
'*  WITHOUT ANY WARRANTY; without even the implied warranty of                 *
'*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General  *
'*  Public License for more details.                                           *
'*                                                                             *
'*  You should have received a copy of the GNU General Public License along    *
'*  with this program; if not, visit:                                          *
'*  http://www.gnu.org/licenses/gpl.txt or write to the Free Software          *
'*  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,                 *
'*  MA  02110-1301, USA                                                        *
'* ////////////////////////////////////////////////////////////////////////////*
'*-----------------------------------------------------------------------------*
'*******************************************************************************

Option Explicit

' run the main function
Call Main()

' Purpose: the Main subroutine
Sub Main()
    Const DEF_TIGHTVNC_PATH = "C:\Program Files\TightVNC\WinVNC.exe" 'TightVNC path
    Dim strIP        ' the IP address
    Dim nRet         ' return value from launch question


    ' Get the external IP of this computer.  If it's "valid", prompt the user to let me
    ' know what their IP is.  Then prompt the user to launch the TightVNC server
    ' so that I may connect to them with the TightVNC client.

    ' get the current IP
    strIP = GetExternalIP()

    If (strIP = "0.0.0.0") Or (strIP = "") Then  ' no IP found
        MsgBox "No IP Address found.  Connect to the Internet first and then run this script again."
    Else                                         ' found an IP
        MsgBox "Tell Chris that your IP address is:  " & strIP

        ' ask if we should run TightVNC
        nRet = MsgBox ("Do you want to run TightVNC now?", 4)

        If (nRet = 7) Then ' no
            MsgBox "We're done, but for Chris to be able to control your computer" & _
                   ", you need to have TightVNC running."
            Exit Sub
        Else                                                       ' yes
            ' try to launch Tight VNC
            If (LaunchTightVNC(DEF_TIGHTVNC_PATH) = True) Then ' life is good
                MsgBox "The little V should be in your system tray now." & vbLf & _
                       "Let Chris know that you're all set."
            End If
        End If
    End If
End Sub


' Purpose: Retrieve the currently assigned internal IP address
'       I: (none)
'       O: IP address
Function GetInternalIP()
    Const DEF_ADDRESS_TEXT = "Address"               ' address text
    Const DEF_9X_CMDLINE = "winipcfg /batch"         ' Windows 9X/Me command line
    Const DEF_NT_CMDLINE = "%comspec% /c ipconfig >" ' Windows NT/2000/XP command line
    Dim objWSHShell                                  ' WSH
    Dim objFSO                                       ' FSO
    Dim strTempFile                                  ' path to temp file
    Dim strTempLine                                  ' line of text read from temp file
    Dim strIP                                        ' IP address


    ' create the WSH object
    Set objWSHShell = CreateObject("WScript.Shell")

    ' create the FSO object
    Set objFSO = CreateObject("Scripting.FileSystemObject")

    ' assemble the path to our temp file in the Windows' TEMP directory
    strTempFile = objFSO.GetSpecialFolder(2) & "\ip.txt"

    If objWSHShell.Environment("SYSTEM")("OS") = "" Then  ' Windows 9x/ME
        objWSHShell.Run DEF_9X_CMDLINE & " " & strTempFile, 0, True
    Else                                                  ' Windows NT4/2000/XP
        objWSHShell.Run DEF_NT_CMDLINE & " " & strTempFile, 0, True
    End If

    ' open the temp file
    With objFSO.GetFile(strTempFile).OpenAsTextStream
        Do While NOT .AtEndOfStream
            ' read a line from our temp file
            strTempLine = .ReadLine
            If InStr(strTempLine, DEF_ADDRESS_TEXT) <> 0 Then ' this line has the IP
                ' strip out the IP address
                strIP = Trim(Mid(strTempLine, InStr(strTempLine, ":") + 1))
            End If
        Loop
        ' close the temp file
        .Close
    End With

    ' return the IP address
    GetInternalIP = strIP

    ' delete our temp file
    objFSO.GetFile(strTempFile).Delete

    ' cleanup
    Set objFSO = Nothing
    Set objWSHShell = Nothing
End Function


' Purpose: Retrieve the currently assigned external IP address using
'          "checkip" on Dyndns.org.
'       I: (none)
'       O: IP address
Function GetExternalIP()
    Const DEF_IP_URL = "http://checkip.dyndns.org"
    Dim objInet    ' Inet Control object
    Dim objRE      ' RegExp object
    Dim objMatches ' Matches object
    Dim strHTML    ' HTML returned from Dyndns.org
    Dim Match      ' match item in Matches collection


    ' Create Inet Control object
    Set objInet = CreateObject("InetCtls.Inet.1")

    ' Set the timeout property
    objInet.RequestTimeOut = 20

    ' Set the URL property of the control
    objInet.Url = DEF_IP_URL

    ' Go to URL and get data
    strHTML = objInet.OpenURL()

    ' Parse out the IP address from this:
    ' <html><head><title>Current IP Check</title></head><body>Current IP Address: xxx.xxx.xxx.xxx</body></html>
    Set objRE = New RegExp
    objRE.Pattern = "(\d{1,3}.){3}\d{1,3}"
    objRE.IgnoreCase = True
    objRE.Global = True
    Set objMatches = objRE.Execute(strHTML)

    ' If there weren't any matches, return a "bad" IP (0.0.0.0).  Otherwise
    ' grab the last IP from the collection of matches, even though there
    ' really should be only one.
    If (objMatches.Count = 0) Then
        GetExternalIP = "0.0.0.0"
    Else
        For Each Match in objMatches
            GetExternalIP = Match.Value
        Next
    End If

    ' Cleanup
    Set objInet = Nothing
    Set objRE = Nothing
    Set objMatches = Nothing
End Function


' Purpose: Launch TightVNC
'       I: path to WinVNC EXE
'       O: True=success, False=fail
Function LaunchTightVNC(strTightVNCPath)
    Const DEF_TIGHTVNC_129_URL = "http://umn.dl.sourceforge.net/sourceforge/vnc-tight/tightvnc-1.2.9-setup.exe" ' TightVNC 1.2.9 URL
    Dim objWSHShell  ' WSH object
    Dim objFSO       ' FSO object


    ' Create the WSH object
    Set objWSHShell = CreateObject("WScript.Shell")

    ' Create the FSO object
    Set objFSO = CreateObject("Scripting.FileSystemObject")

    ' Make sure the path to TightVNC is right.  If it isn't, try to download it
    ' from SourceForge.  If it is, launch TightVNC server.
    If (objFSO.FileExists(strTightVNCPath) = False) Then ' can't find TightVNC
        MsgBox "Couldn't find TightVNC.  It should be in:" & vbLf & vbLf & _
               strTightVNCPath & vbLf & vbLf & _
               "Looks like we'll need to download/install it again."

        objWSHShell.Run DEF_TIGHTVNC_129_URL
        LaunchTightVNC = False
    Else
        objWSHShell.Exec strTightVNCPath
        LaunchTightVNC = True
    End If
End Function
