' bearshare.vbs set forceIP; 4/2001

' =================================

'

' Released and copyright by Dun3, 2001

' eMail: Dun3@gmx.de

' Use at your own risk

'

' You have the right to use, distribute and modify the code.

' But it would be nice to write a short eMail to Dun3@gmx.de.

' I'd like to know how many people use it. Maybe you did

' improve it, or found a real bug? Please be so kind and pass

' me a note.

 

' The use is quite simple:

' 1.) Change the path below to your needs.

' 2.) Doubleklick the Script or pass the IP by commandline:

'      c:\> START bearshare.vbs IP[:Port] Port is optional

'           Best used in a BATCH-Skript like:

'           @echo off

'           START bearshare.vbs %1

'

 

'      *******************************

'      *  change path to your needs  *

'      *******************************

 

    bearPATH = "C:\Programme\BearShare\"

  

    bearEXE = bearPATH & "BearShare.exe"

    bearINI = bearPATH & "FreePeers.ini"

    bearTMP = bearPATH & "temp.ini"

 

' ************************************************************

' *

' *  INIT

' *

 

Dim wsh, args, INIfile, TMPfile, fs

Dim oldIP, newIP, oldPort, newPort

 

' -> Object

Set fs = CreateObject("Scripting.FileSystemObject")

Set wsh = WScript.CreateObject("WScript.Shell")

 

' Store Args

Set args = WScript.Arguments

 

' -> check paths

If Not fs.FileExists(bearEXE) Then

  wsh.Popup "Could not find BearShare.exe!" & Chr(13) & _

               "(looked for '" & bearEXE & "')", , "Error", 16

  WScript.Quit

End If

If Not fs.FileExists(bearINI) Then

  wsh.Popup "Could not find FreePeers.ini!" & Chr(13) & _

               "(looked for '" & bearINI & "')", , "Error", 16

  WScript.Quit

End If

 

' ************************************************************

' *

' *  Search for "old" IP and Port

' *

 

' -> Open INI file for reading

 

Set INIfile = fs.OpenTextFile(bearINI, 1)

 

Do

   retstring = INIfile.ReadLine

 

'  -> Is this the right line?

   If InStr(retstring, "forcedIP = " + Chr(34)) <> 0 Then

'     -> Chr(34) equals a "

      IPArray = Split(retstring, Chr(34), -1, 1)

      oldIP = IPArray(1)

   End If

   If InStr(retstring, "listenPort = ") <> 0 Then

      PortArray = Split(retstring, " ", -1, 1)

      oldPort = PortArray(2)

   End If

 

   If INIfile.AtEndOfStream = True Then

      Exit Do

   End If

Loop

 

INIfile.close

 

' ************************************************************

' *

' *  "FRONTEND" ;o)

' *

 

' -> In case no Port is given:

newPort = oldPort

 

' -> started from commandline?

If args.Count = 0 Then

 

' -> Give old values as default

  newString = oldIP & ":" & oldPort

 

  Do

    newString = InputBox("Enter IP of the Router" & vbCrLf & _

           " (xxx.xxx.xxx.xxx[:Port])", "Enter IP", newString)

 

'   -> User pressed Cancel

    If newString = "" Then

       wsh.Popup "User Canceled", , "Error", 16

       WScript.Quit

    End If

 

'   -> Did the User enter a Port?

    If InStr(newString, ":") <> 0 Then

'     -> If so split and pass it on

      SplitIP = Split(newString, ":")

      newIP = SplitIP(0)

      newPort = SplitIP(1)

    Else

      newIP = newString

    End If

  Loop Until CheckIP(newIP) AND isNumeric(newPort)

Else

 

  newString = args(0)

' -> Did the User enter a Port?

  If InStr(newString, ":") <> 0 Then

'   -> If so split and pass it on

    SplitIP = Split(newString, ":")

    newIP = SplitIP(0)

    newPort = SplitIP(1)

  Else

    newIP = newString

  End If

 

  If NOT (CheckIP(newIP) AND isNumeric(newPort)) Then

    wsh.Popup "No valide IP! String was: " & _

           newString, , "Error", 16

'   -> Be agressive against errors

    WScript.Quit

  End If

 

  MsgBox "Args -> " & newIP & ":" & newPort

End If

 

' ************************************************************

' *

' *  ALTER INIfile

' *

 

' -> make Backups... well you never know

 

fs.CopyFile bearINI, bearPATH & "Backup.ini", True

 

fs.CopyFile bearINI, bearTMP, True

 

' -> Open TMP file for reading

 

Set TMPfile = fs.OpenTextFile(bearTMP, 1)

 

' -> Creating empty INI file (overwriting existing)

 

Set INIfile = fs.CreateTextFile(bearINI, True)

 

Do

   retstring = TMPfile.ReadLine

 

'  -> Is this the right line?

   If InStr(retstring, "listenPort = ") <> 0 Then

      INIfile.WriteLine("listenPort = " & newPort & _

                   " ; TCP/IP port number to listen on")

   ElseIf InStr(retstring, "bForcedIP =") <> 0 Then

'     -> Chr(34) equals a "

      INIfile.WriteLine("bForcedIP = Yes ; True if "& _

                   "incoming connections cannot be accepted")

   ElseIf InStr(retstring, "forcedIP = " + Chr(34)) <> 0 Then

'     -> Chr(34) equals a "

      INIfile.WriteLine("forcedIP = " & Chr(34) & _

                newIP & Chr(34) & " ; Local IP " & _

                "reported in query hits if bForcedIP")

   Else

      INIfile.WriteLine(retstring)

   End If

 

   If TMPfile.AtEndOfStream = True Then

      Exit Do

   End If

Loop

 

INIfile.close

TMPfile.close

 

' -> Let the Bear RUN!

 

ret = wsh.Run(bearEXE, 1, True)

 

' ************************************************************

' *

' *  FUNCTIONS

' *

 

Function CheckIP (CheckString)

 

  Dim SplitCheck

 

  CheckIP = False

 

    SplitCheck = Split(CheckString, ".")

    If UBound(SplitCheck, 1) <> 3 Then

      wsh.Popup "No valide IP! 4 Numbers! ", , "Error", 16

      Exit Function

    End If

    If SplitCheck(0) > 255 OR SplitCheck(1) > 255 OR _

            SplitCheck(2) > 255 OR SplitCheck(3) > 255 Then

      wsh.Popup "No valide IP! Values to high!", , "Error", 16

      Exit Function

    End If

    If SplitCheck(0) < 0 OR SplitCheck(1) < 0 _

                         OR SplitCheck(2) < 0 _

                         OR SplitCheck(3) < 0 Then

      wsh.Popup "No valide IP! Values to high!", , "Error", 16

      Exit Function

    End If

 

  CheckIP = True

 

End Function

 

' -> EOF