 Rem File: Cmd152.bas
 Rem Sub: Remote.Shell/Remote.DOS
 Rem $Include: 'dndbbs.inc'
 On Error Goto Main.Error
 If Chained<>-2 Then
    Color 15,0
    Print "DNDBBS Command 152 -- Remote.Shell/Remote.DOS."
    Color 7,0
    End
 Endif
 Chained=-1
 Shelled=-1
 Remote=Local.Mode
 Shell.Type=-2
 If TempX=False Then
    Call Remote.Shell
 Else
    Call Remote.DOS
 Endif
Main.Resume:
 Chain DND.Path+"DNDBBS.EXE"
 End
Main.Error:
 Resume Main.Resume

Sub Remote.Shell
 On Local Error Goto ErrorTrap37
 Gosub Get.Config1
 Close #TempFile1
 Close #TempFile2
 Temp$=Rtrim$(Config3(46))
 Temp$=Ucase$(Temp$)
 If Config2(66)=False Or Temp$=Nul Then
    Strng="Shell disabled!"
    Call IO.O
    Shell.Type=0
    Exit Sub
 Endif
 Strng="Enter password to enter shell: "
 Hidden=True
 Call IO.I
 Hidden=False
 If Len(Out2)=0 Then
    Exit Sub
 Endif
 Out2=Ucase$(Out2)
 Var$=Nul
 Call Cryptograph(Out2)
 For Var=1 To Len(Out2)
    Var2=Asc(Mid$(Out2,Var,1))
    Var$=Var$+Right$("0"+Hex$(Var2),2)
 Next
 If Temp$<>Var$ Then
    Strng="Illegal shell password!"
    Call IO.O
    Shell.Type=0
    Exit Sub
 Endif
 If Local.Mode Then
    Call Status.Line(-2)
    Graphics.Off=False
    Strng="Type 'EXIT' to return to Dndbbs.."
    Call IO.O
    Close #TempFile1
    Open DOS.Exit For Output Shared As #TempFile1
    Print #TempFile1,"ECHO OFF"
    If Len(option1$) Then
       Comspec$=option1$
    Else
       Comspec$=ENVIRON$("COMSPEC")
       If Comspec$="" Then
          Comspec$="C:\COMMAND.COM"
          If Windows.Detected Or OS2.Detected Then
             Comspec$="CMD.EXE"
          Endif
       Endif
    Endif
    Print #TempFile1,Comspec$
    Close
    Call ScreenANSI(1,37,7)
    Call Modem.ANSI(1,37,7)
    Var2$=Curdir$
    Shell DOS.Exit
    Chdrive Var2$
    Chdir Var2$
    Exit Sub
 Endif
 If Port>3 Then
    Strng="Command shell ports can only be 0 to 3."
    Call IO.O
    Shell.Type=0
    Exit Sub
 Endif
 If optionz=0 Then
    Filename=Dnd.Path+"WATCHCAT.COM"
    If Dir$(Filename)="" Then
       Strng="Watchcat not found."
       Call IO.O
       Shell.Type=0
       Exit Sub
    Endif
 Endif
 Strng="Now entering remote DOS shell.."
 Call IO.O
 Call Status.Line(-2)
 Graphics.Off=True
 Strng="Type 'EXIT' to return to Dndbbs."
 Call IO.O
 Close #TempFile1
 Open DOS.Exit For Output Shared As #TempFile1
 Print #TempFile1,"ECHO OFF"
 If optionz=0 Then
    Print #TempFile1,Filename+Str$(Port+1)+"C"
    Print #TempFile1,Filename+" ON"
 Endif
 Var$="COM"+Mid$(Str$(Port+1),2)+":"
 If Len(option1$) Then
    Comspec$=option1$
 Else
    Comspec$=ENVIRON$("COMSPEC")
    If Comspec$="" Then
       Comspec$="C:\COMMAND.COM"
       If Windows.Detected Or OS2.Detected Then
          Comspec$="CMD.EXE"
       Endif
    Endif
 Endif
 VarX$=Comspec$ + " < " + Var$ + " > " + Var$
 Print #TempFile1,VarX$
 If optionz=0 Then
    Print #TempFile1,Filename+" OFF"
 Endif
 Close
 If optionx Then
    Call Driver.Watchdog(1) ' on
 Endif
 Call ScreenANSI(1,37,7)
 Call Modem.ANSI(1,37,7)
 Var2$=Curdir$
 Shell DOS.Exit
 Chdrive Var2$
 Chdir Var2$
 If optionx Then
    Call Driver.Watchdog(0) ' off
 Endif
 Exit Sub
Get.Config1:
 VarX=0
 Do
    VarX=VarX+1
    If VarX>Options.Lines Then
       Goto Next.Read1
       Exit Do
    Endif
    Var$=Ltrim$(Rtrim$(Options.Array(VarX)))
    If Ucase$(Var$)="[CMD152]" Then
       Do
          VarX=VarX+1
          If VarX>Options.Lines Then
             Goto Next.Read1
             Return
          Endif
          Var$=Ltrim$(Rtrim$(Options.Array(VarX)))
          If Ucase$(Var$)="[END]" Then
             Goto Next.Read1
             Return
          Endif
          Var=Instr(Var$,"=")
          If Var Then
             Var2$=Ltrim$(Rtrim$(Left$(Var$,Var-1)))
             Var3$=Ltrim$(Rtrim$(Mid$(Var$,Var+1)))
             Gosub Parse.Options1
          Endif
       Loop
    Endif
 Loop
 Return
Next.Read1:
 Close #TempFile1
 Filename$=DND.Path+"CMD152.CFG"
 If Dir$(Filename$)="" Then
    Return
 Endif
 Open Filename$ For Input As #TempFile1
 Do
    If Eof(TempFile1) Then
       Exit Do
    Endif
    Line Input #TempFile1, Var$
    Var$=Ltrim$(Rtrim$(Var$))
    If Left$(Var$,1)<>";" Then
       VarX$=UserRecord.CodeName
       VarX$=Rtrim$(VarX$)
       VarX$=Ucase$(VarX$)
       If Ucase$(Var$)="["+VarX$+"]" Then
          VarZ=0
          Option1=-1
          Do
             If Eof(TempFile1) Then
                Exit Do
             Endif
             Line Input #TempFile1, Var$
             If Ucase$(Var$)="[END]" Then
                Return
             Endif
             Var=Instr(Var$,"=")
             If Var Then
                Var2$=Ltrim$(Rtrim$(Left$(Var$,Var-1)))
                Var3$=Ltrim$(Rtrim$(Mid$(Var$,Var+1)))
                Gosub Parse.Options1
             Endif
          Loop
       Endif
    Endif
 Loop
 Return
Parse.Options1:
 Select Case Lcase$(Var2$)
 Case "force.comspec"
    If Left$(Var3$,1)=Chr$(34) Then
       If Right$(Var3$,1)=Chr$(34) Then
          option1$=Mid$(Var3$,2)
          option1$=Left$(option1$,Len(option1$)-1)
       Endif
    Endif
 Case "comspec.switches"
    If Left$(Var3$,1)=Chr$(34) Then
       If Right$(Var3$,1)=Chr$(34) Then
          option2$=Mid$(Var3$,2)
          option2$=Left$(option2$,Len(option2$)-1)
       Endif
    Endif
 Case "also.use.watchdog"
    Select Case Ucase$(Var3$)
    Case "-1", "TRUE", "ON"
       optionx=-1
    End Select
 Case "disable.watchcat"
    Select Case Ucase$(Var3$)
    Case "-1", "TRUE", "ON"
       optionz=-1
    End Select
 End Select
 Return
ErrorResume37:
 Exit Sub
ErrorTrap37:
 Resume ErrorResume37
End Sub

Sub Remote.DOS
 On Local Error Goto ErrorTrap38
 Gosub Get.Config2
 Close #TempFile1
 Close #TempFile2
 Temp$=Rtrim$(Config3(46))
 Temp$=Ucase$(Temp$)
 If Config2(66)=False Or Temp$=Nul Then
    Strng="DOS command disabled!"
    Call IO.O
    Shell.Type=0
    Exit Sub
 Endif
 Strng="Enter password to enter DOS command: "
 Hidden=True
 Call IO.I
 Hidden=False
 Out2=Ucase$(Out2)
 If Len(Out2)=0 Then
    Exit Sub
 Endif
 Out2=Ucase$(Out2)
 Var$=Nul
 Call Cryptograph(Out2)
 For Var=1 To Len(Out2)
    Var2=Asc(Mid$(Out2,Var,1))
    Var$=Var$+Right$("0"+Hex$(Var2),2)
 Next
 If Temp$<>Var$ Then
    Strng="Illegal DOS command password!"
    Call IO.O
    Shell.Type=0
    Exit Sub
 Endif
 If Local.Mode Then
    Graphics.Off=True
    Strng="Enter DOS command: "
    Call IO.I
    Var$=Out2
    Close #TempFile1
    Open DOS.Exit For Output Shared As #TempFile1
    If Len(option1$) Then
       Comspec$=option1$
    Else
       Comspec$=ENVIRON$("COMSPEC")
       If Comspec$="" Then
          Comspec$="C:\COMMAND.COM"
          If Windows.Detected Or OS2.Detected Then
             Comspec$="CMD.EXE"
          Endif
       Endif
    Endif
    If Len(option2$) Then
       VarY$=option2$
    Else
       VarY$="/C"
    Endif
    VarX$=Comspec$
    VarX$ = VarX$ + " " + VarY$ + " " + Var$
    Print #TempFile1,VarX$
    Print #TempFile1,"PAUSE"
    Print #TempFile1,"EXIT"
    Close #TempFile1
    Close
    Call Status.Line(-2)
    Var2$=Curdir$
    Graphics.Off=False
    Strng="Starting shell.."
    Call IO.O
    Call ScreenANSI(1,37,7)
    Call Modem.ANSI(1,37,7)
    Shell DOS.Exit
    Chdrive Var2$
    Chdir Var2$
    Exit Sub
 Endif
 If Port>3 Then
    Strng="Command shell ports can only be 0 to 3."
    Call IO.O
    Shell.Type=0
    Exit Sub
 Endif
 If optionz=0 Then
    Filename=Dnd.Path+"WATCHCAT.COM"
    If Dir$(Filename)="" Then
       Strng="Watchcat not found."
       Call IO.O
       Shell.Type=0
       Exit Sub
    Endif
 Endif
 Strng="Enter DOS command: "
 Call IO.I
 If Instr(Out2,">") Or Instr(Out2,"<") Or Instr(Out2,"|") Then
    Strng="Use |Shell for DOS command with <, >, or |."
    Call IO.O
    Shell.Type=0
    Exit Sub
 Endif
 Call Status.Line(-2)
 Graphics.Off=True
 Strng="Now entering remote DOS shell.."
 Call IO.O
 Close #TempFile1
 Open DOS.Exit For Output Shared As #TempFile1
 If optionz=0 Then
    Print #TempFile1,Filename+Str$(Port+1)+"C"
    Print #TempFile1,Filename+" ON"
 Endif
 Var$="COM"+Mid$(Str$(Port+1),2)+":"
 If Len(option1$) Then
    Comspec$=option1$
 Else
    Comspec$=ENVIRON$("COMSPEC")
    If Comspec$="" Then
       Comspec$="C:\COMMAND.COM"
       If Windows.Detected Or OS2.Detected Then
          Comspec$="CMD.EXE"
       Endif
    Endif
 Endif
 If Len(option2$) Then
    VarY$ = option2$
 Else
    VarY$ = "/C"
 Endif
 VarX$=Comspec$ + " " + VarY$ + " " + Out2 + " < " + Var$ + " | TEE"
 If Config2(83) Then
    VarX$=VarX$+"2" ' lptx:
 Endif
 VarX$=VarX$+" > "+Var$
 Print #TempFile1,VarX$
 If optionz=0 Then
    Print #TempFile1,Filename+" OFF"
 Endif
 Print #TempFile1,"EXIT"
 Close
 If optionx Then
    Call Driver.Watchdog(1) ' on
 Endif
 Call ScreenANSI(1,37,7)
 Call Modem.ANSI(1,37,7)
 Var2$=Curdir$
 Shell DOS.Exit
 Chdrive Var2$
 Chdir Var2$
 If optionx Then
    Call Driver.Watchdog(0) ' off
 Endif
 Exit Sub
Get.Config2:
 VarX=0
 Do
    VarX=VarX+1
    If VarX>Options.Lines Then
       Goto Next.Read2
       Exit Do
    Endif
    Var$=Ltrim$(Rtrim$(Options.Array(VarX)))
    If Ucase$(Var$)="[CMD152]" Then
       Do
          VarX=VarX+1
          If VarX>Options.Lines Then
             Goto Next.Read2
             Return
          Endif
          Var$=Ltrim$(Rtrim$(Options.Array(VarX)))
          If Ucase$(Var$)="[END]" Then
             Goto Next.Read2
             Return
          Endif
          Var=Instr(Var$,"=")
          If Var Then
             Var2$=Ltrim$(Rtrim$(Left$(Var$,Var-1)))
             Var3$=Ltrim$(Rtrim$(Mid$(Var$,Var+1)))
             Gosub Parse.Options2
          Endif
       Loop
    Endif
 Loop
 Return
Next.Read2:
 Close #TempFile1
 Filename$=DND.Path+"CMD152.CFG"
 If Dir$(Filename$)="" Then
    Return
 Endif
 Open Filename$ For Input As #TempFile1
 Do
    If Eof(TempFile1) Then
       Exit Do
    Endif
    Line Input #TempFile1, Var$
    Var$=Ltrim$(Rtrim$(Var$))
    If Left$(Var$,1)<>";" Then
       VarX$=UserRecord.CodeName
       VarX$=Rtrim$(VarX$)
       VarX$=Ucase$(VarX$)
       If Ucase$(Var$)="["+VarX$+"]" Then
          VarZ=0
          Option1=-1
          Do
             If Eof(TempFile1) Then
                Exit Do
             Endif
             Line Input #TempFile1, Var$
             If Ucase$(Var$)="[END]" Then
                Return
             Endif
             Var=Instr(Var$,"=")
             If Var Then
                Var2$=Ltrim$(Rtrim$(Left$(Var$,Var-1)))
                Var3$=Ltrim$(Rtrim$(Mid$(Var$,Var+1)))
                Gosub Parse.Options1
             Endif
          Loop
       Endif
    Endif
 Loop
 Return
Parse.Options2:
 Select Case Lcase$(Var2$)
 Case "force.comspec"
    If Left$(Var3$,1)=Chr$(34) Then
       If Right$(Var3$,1)=Chr$(34) Then
          option1$=Mid$(Var3$,2)
          option1$=Left$(option1$,Len(option1$)-1)
       Endif
    Endif
 Case "comspec.switches"
    If Left$(Var3$,1)=Chr$(34) Then
       If Right$(Var3$,1)=Chr$(34) Then
          option2$=Mid$(Var3$,2)
          option2$=Left$(option2$,Len(option2$)-1)
       Endif
    Endif
 Case "also.use.watchdog"
    Select Case Ucase$(Var3$)
    Case "-1", "TRUE", "ON"
       optionx=-1
    End Select
 Case "disable.watchcat"
    Select Case Ucase$(Var3$)
    Case "-1", "TRUE", "ON"
       optionz=-1
    End Select
 End Select
 Return
ErrorResume38:
 Exit Sub
ErrorTrap38:
 Resume ErrorResume38
End Sub

' fossil reboot if carrier lost during shell.
Sub Driver.Watchdog(Var)
 On Local Error Goto ErrorTrap36
 If Var Then
    Call Extended.Driver(13) ' activate
 Else
    Call Extended.Driver(14) ' deactivate
 Endif
ErrorResume36:
 Exit Sub
ErrorTrap36:
 Resume ErrorResume36
End Sub
