 Rem File: Cmd203.bas
 Rem Sub: |Toggle
 Rem $Include: 'dndbbs.inc'
 On Error Goto Main.Error
 If Chained=0 Then ' local mode
    Call Get.Node
    Call Open.Arrays
    Call Get.Config
    Call Reset.Cursor
    Call Detect.Share
    Call Open.Files
    Call Save.Screen2(0)
    Color 14,0
    Print "DNDBBS CMD203 - |Toggle"
    Timeon=Timeit!
    Time.Left=3600!
 Endif
 Do While Lost.Carrier=False
    Graphics.Off=False
    Strng="Enter codename(?=list,*=search,<enter>=Quit): "
    Call IO.I
    Var$=Out2
    If Var$=Nul Then
       Exit Do
    Endif
    If Var$="?" Then
       Temp2=False
       Var2=False
       For Temp1=0 To MaxNodes
          Call Read.Message.Record(MessWorkFile1,Temp1+1)
          If MessWorkRecord1.ActiveNode Then
             Out3=MessWorkRecord1.UserName
             Out3=Lcase$(Rtrim$(Out3))
             Select Case Out3
             Case "login", "logoff", "relogin"
                Eat$=Nul
             Case Deleted$, Offline$
                Eat$=Nul
             Case Else
                Gosub Display.User
                Temp2=Temp2+1
                Var2=True
                If Temp2>=User.PageLength-2 Then
                   Temp2=False
                   Call More.Prompt
                   If No Or Quit Then
                      Exit For
                   Endif
                Endif
             End Select
          Endif
       Next
       If Temp2 Then
          Call More.Prompt
       Endif
       If Var2=False Then
          Strng="No online users found."
          Call IO.O
       Endif
    Else
       If Var$="*" Then
          Graphics.Off=False
          Strng="Enter codename search(?,* allowed): "
          Call IO.I
          Out2=Lcase$(Out2)
          Var2$=Out2
          Temp2=False
          Var2=False
          For Temp1=0 To MaxNodes
             Call Read.Message.Record(MessWorkFile1,Temp1+1)
             If MessWorkRecord1.ActiveNode Then
                Out3=MessWorkRecord1.UserName
                Out3=Lcase$(Rtrim$(Out3))
                Select Case Out3
                Case "login", "logoff", "relogin"
                   Eat$=Nul
                Case Deleted$, Offline$
                   Eat$=Nul
                Case Else
                   If Len(Out3) Then
                      Out2=Var2$
                      Call InstrSUB2(VarX)
                      If VarX Then
                         Var2=True
                         Gosub Display.User
                         Temp2=Temp2+1
                         If Temp2>=User.PageLength-2 Then
                            Temp2=False
                            Call More.Prompt
                            If No Or Quit Then
                               Exit For
                            Endif
                         Endif
                      Endif
                   Endif
                End Select
             Endif
          Next
          If Var2=False Then
             Strng="Online user not found."
             Call IO.O
          Endif
          If Temp2 Then
             Call More.Prompt
          Endif
       Else
          Var$=Lcase$(Var$)
          Call Search.Mess.File(Var$,Var1)
          If Var1 Then
             Call Edit.Node.List
          Else
             Graphics.Off=True
             Strng="Online user not found."
             Call IO.O
          Endif
       Endif
    Endif
 Loop
Main.Resume:
 If Chained=-2 Then
    Chain DND.Path+"DNDBBS.EXE"
 Endif
 Call Clear.Status.Line
 Call ScreenANSI(1,37,7)
 Color 7, 0
 Cls
 Call Save.Screen2(-1)
' Locate Max.Row,1,1
 If TempX=0 Then TempX=1
 Locate TempX,1,1
 Color 15,0
 Print
 Print "Now exiting |Toggle.."
 Color 7,0
 End
Display.User:
 If Len(Out3) Then
    Mid$(Out3,1,1)=Ucase$(Mid$(Out3,1,1))
 Else
    Out3="<none>"
 Endif
 Select Case Temp1
 Case 0 ' <nul>
    Strng=" -"
 Case 1 To 10 ' 0-9
    Strng=Str$(Temp1-1)
 Case 11 To 36 ' A-Z
    Strng=" "+Chr$(Temp1+54) ' 11-14-2018
 Case 37 To 126 ' 10-99
    Strng=Str$(Temp1-27)
 End Select
 Strng="Node"+Strng+" : "+Out3
 Graphics.Off=True
 Call IO.O
 Return
Main.Error:
 Resume Main.Resume

SUB Edit.Node.List
 On Local Error Goto NodeListError2
 Call Toggle.Status.Line
 Do While Lost.Carrier=False
    Call Read.Message.Record(MessWorkFile1,TempA+1)
    If MessWorkRecord1.ActiveNode=False Then
       Strng="User is offline."
       Call IO.O
       Exit Do
    Endif
    Call Display.Node.Chart
    Graphics.Off=False
    Strng="Enter (T)oggle item, (Q)uit? "
    No.Input.Out="Q"
    Call IO.I
    If Quit Then
       Exit Do
    Endif
    If Lcase$(Out2)="t" Then
       Graphics.Off=False
       Strng="Node(-,0-9,A-Z,10-99)? "
       Call IO.I
       VarQ=False
       VarZ=False
       Select Case Len(Out2)
       Case 1
          Select Case Ucase$(Out2)
          Case "-" ' nul
             VarZ=True
          Case "0" To "9"
             VarQ=Val(Out2)+1
          Case "A" To "Z"
             VarQ=Asc(Ucase$(Out2))-54
          End Select
       Case 2
          Select Case Int(Val(Out2))
          Case 10 To 99
             VarX=Int(Val(Out2))
             VarQ=VarX+27 ' 37-126
          End Select
       End Select
       If VarQ Or VarZ Then
          Graphics.Off=False
          Strng="Toggle (S)end/(R)eceive flag? "
          Call IO.I
          If Lcase$(Out2)="s" Then
             Call Read.Message.Record(MessWorkFile1,TempA+1)
             If Mid$(MessWorkRecord1.SendFlags,VarQ+1,1)="1" Then
                Mid$(MessWorkRecord1.SendFlags,VarQ+1,1)="0"
             Else
                Mid$(MessWorkRecord1.SendFlags,VarQ+1,1)="1"
             Endif
             Call Write.Message.Record(MessWorkFile1,TempA+1)
          Else
             If Lcase$(Out2)="r" Then
                Call Read.Message.Record(MessWorkFile1,TempA+1)
                If Mid$(MessWorkRecord1.ReceiveFlags,VarQ+1,1)="1" Then
                   Mid$(MessWorkRecord1.ReceiveFlags,VarQ+1,1)="0"
                Else
                   Mid$(MessWorkRecord1.ReceiveFlags,VarQ+1,1)="1"
                Endif
                Call Write.Message.Record(MessWorkFile1,TempA+1)
             Endif
          Endif
       Endif
    Endif
 Loop
 Call More.Prompt
NodeListResume2:
 Exit Sub
NodeListError2:
 Resume NodeListResume2
END SUB

' display node chart
SUB Display.Node.Chart
On Local Error Goto NodeListError1

Call Read.Message.Record(MessWorkFile1,TempA+1)

Var$ = MessWorkRecord1.UserName
Var$ = Ltrim$(Rtrim$(Var$))
Var$ = Lcase$(Var$)
If Len(Var$) Then
   Mid$(Var$,1,1) = Ucase$(Left$(Var$,1))
Endif

Strng = "Dndbbs nodelist editor: " + Var$
CALL IO.O

Var1$ = MessWorkRecord1.SendFlags
Var2$ = MessWorkRecord1.ReceiveFlags

Strng="Node <nul> is: "
If Mid$(Var1$,1,1)="1" Then
   Strng=Strng+"S"
Else
   Strng=Strng+"-"
Endif
If Mid$(Var2$,1,1)="1" Then
   Strng=Strng+"R"
Else
   Strng=Strng+"-"
Endif
Call IO.O

' 9 items * 14 lines = 126 nodes. (<nul> not listed).

Graphics.Off=False
Strng = "N# SR  N# SR  N# SR  N# SR  N# SR  N# SR  N# SR  N# SR  N# SR"
CALL IO.O

Strng = "=====  =====  =====  =====  =====  =====  =====  =====  ====="
CALL IO.O

Graphics.Off=True
Strng = "00 --  01 --  02 --  03 --  04 --  05 --  06 --  07 --  08 --"
Var3 = 0
FOR Var = 2 TO 10
   GOSUB Make.Node.String
NEXT
CALL IO.O

Strng = "09 --  0A --  0B --  0C --  0D --  0E --  0F --  0G --  0H --"
Var3 = 0
FOR Var = 11 TO 19
   GOSUB Make.Node.String
NEXT
CALL IO.O

Strng = "0I --  0J --  0K --  0L --  0M --  0N --  0O --  0P --  0Q --"
Var3 = 0
FOR Var = 20 TO 28
   GOSUB Make.Node.String
NEXT
CALL IO.O

Strng = "0R --  0S --  0T --  0U --  0V --  0W --  0X --  0Y --  0Z --"
Var3 = 0
FOR Var = 29 TO 37
   GOSUB Make.Node.String
NEXT
CALL IO.O

Strng = "10 --  11 --  12 --  13 --  14 --  15 --  16 --  17 --  18 --"
Var3 = 0
FOR Var = 38 TO 46
   GOSUB Make.Node.String
NEXT
CALL IO.O

Strng = "19 --  20 --  21 --  22 --  23 --  24 --  25 --  26 --  27 --"
Var3 = 0
FOR Var = 47 TO 55
   GOSUB Make.Node.String
NEXT
CALL IO.O

Strng = "28 --  29 --  30 --  31 --  32 --  33 --  34 --  35 --  36 --"
Var3 = 0
FOR Var = 56 TO 64
   GOSUB Make.Node.String
NEXT
CALL IO.O

Strng = "37 --  38 --  39 --  40 --  41 --  42 --  43 --  44 --  45 --"
Var3 = 0
FOR Var = 65 TO 73
   GOSUB Make.Node.String
NEXT
CALL IO.O

Strng = "46 --  47 --  48 --  49 --  50 --  51 --  52 --  53 --  54 --"
Var3 = 0
FOR Var = 74 TO 82
   GOSUB Make.Node.String
NEXT
CALL IO.O

Strng = "55 --  56 --  57 --  58 --  59 --  60 --  61 --  62 --  63 --"
Var3 = 0
FOR Var = 83 TO 91
   GOSUB Make.Node.String
NEXT
CALL IO.O

Strng = "64 --  65 --  66 --  67 --  68 --  69 --  70 --  71 --  72 --"
Var3 = 0
FOR Var = 92 TO 100
   GOSUB Make.Node.String
NEXT
CALL IO.O

Strng = "73 --  74 --  75 --  76 --  77 --  78 --  79 --  80 --  81 --"
Var3 = 0
FOR Var = 101 TO 109
   GOSUB Make.Node.String
NEXT
CALL IO.O

Strng = "82 --  83 --  84 --  85 --  86 --  87 --  88 --  89 --  90 --"
Var3 = 0
FOR Var = 110 TO 118
   GOSUB Make.Node.String
NEXT
CALL IO.O

Strng = "91 --  92 --  93 --  94 --  95 --  96 --  97 --  98 --  99 --"
Var3 = 0
FOR Var = 119 TO 127
   GOSUB Make.Node.String
NEXT
CALL IO.O
EXIT SUB

Make.Node.String:
 IF MID$(Var1$, Var, 1) = "1" THEN ' send flag
    Tmp1$ = "S"
 ELSE
    Tmp1$ = "-"
 END IF
 IF MID$(Var2$, Var, 1) = "1" THEN ' receive flag
    Tmp1$ = Tmp1$ + "R"
 ELSE
    Tmp1$ = Tmp1$ + "-"
 END IF

 ' construct string
 Var3 = Var3 + 1
 SELECT CASE Var3
 CASE 1
    MID$(Strng, 4, 2) = Tmp1$
 CASE 2
    MID$(Strng, 11, 2) = Tmp1$
 CASE 3
    MID$(Strng, 18, 2) = Tmp1$
 CASE 4
    MID$(Strng, 25, 2) = Tmp1$
 CASE 5
    MID$(Strng, 32, 2) = Tmp1$
 CASE 6
    MID$(Strng, 39, 2) = Tmp1$
 CASE 7
    MID$(Strng, 46, 2) = Tmp1$
 CASE 8
    MID$(Strng, 53, 2) = Tmp1$
 CASE 9
    MID$(Strng, 60, 2) = Tmp1$
 END SELECT
 RETURN
NodeListResume1:
 Exit Sub
NodeListError1:
 Resume NodeListResume1
END SUB

Sub Toggle.Status.Line
 On Local Error Goto Error.Trap58
 d1$=format$(now,"mm-dd-yyyy")
 Temp$="Node List Editor: "+d1$+" "+Format$(Now,"hh:mm:ss")
 Temp$=Left$(Temp$,79)
 Temp$=Temp$+Space$(79-Len(Temp$))
 Call Display.Status.Line(True)
Error.Resume58:
 Exit Sub
Error.Trap58:
 Resume Error.Resume58
End Sub

Sub Clear.Status.Line
 On Local Error Goto Error.Trap58a
 Temp$=Space$(79)
 Call Display.Status.Line(False)
Error.Resume58a:
 Exit Sub
Error.Trap58a:
 Resume Error.Resume58a
End Sub

Sub Get.Node
 On Local Error Goto Node.Error
 Var$=Command$
 Var$=Ltrim$(Rtrim$(Var$))
 If Instr(Var$,"/N:") Then
    Var$=Mid$(Var$,Instr(Var$,"/N:")+3)
    Select Case Len(Var$)
    Case 0
       Node=0
       NodeX=Nul
       Exit Sub
    Case 1 ' 0-9,A-Z
       Select Case Var$
       Case "0" To "9"
          Node=Val(Var$)+1
          NodeX=Var$
       Case "A" To "Z"
          Node=Asc(Var$)-54
          NodeX=Var$
       Case Else
          Goto Node.Error
       End Select
    Case 2 ' 10-99
       VarX=Int(Val(Var$))
       If VarX>=10 And VarX<=99 Then
          Node=VarX+27 ' 37-126
          NodeX=Mid$(Str$(VarX),2) ' 10-99
       Else
          Goto Node.Error
       Endif
    Case Else
       Goto Node.Error
    End Select
 Endif
 Exit Sub
Node.Error:
 Color 14,0
 Out2="CMD203 Module - |Toggle"+Chr$(13)+Chr$(10)
 Out3="Error reading node. Node must be <nul>,0-9,A-Z,10-99."
 Call Terminate.ProgramX
End Sub

Sub Open.Arrays
 On Local Error Goto Array.Error
 Call Plex.Function(26)
 If (OutregsX.AX And &HFF)=&HFF Then
    Local.ANSI.Color=True
 Else
    Local.ANSI.Color=False
 Endif
 Local.Avatar.Color=False
 Call Plex.Function(27)
 If (OutregsX.AX And &HFF)=&HFF Then
    If (OutregsX.DX And &HFF)=&H16 Then
       Local.Avatar.Color=True
    Endif
 Endif
 Local.Mode=True
 Lost.Carrier=False
 Call Read.Rows(Max.Row)
 Locate Max.Row,1,1
 Call VideoInt(15,0)
 Video.Page=OutregsX.BX
 Exit Sub
Array.Error:
 Color 14,0
 Out2="CMD203 Module - |Toggle"+Chr$(13)+Chr$(10)
 Out3="Error opening arrays. Free more RAM."
 Call Terminate.ProgramX
End Sub

Sub Get.Config
 On Local Error Goto Config.Error
 DND.Path=Environ$("DNDBBS")
 If DND.Path<>Nul Then
    If Right$(DND.Path,1)<>"\" Then
       DND.Path=DND.Path+"\"
    Endif
 Endif
 FileName=DND.Path+"DND"+NodeX+".CFG"
 Close #TempFile1
 Open FileName For Input Shared As #TempFile1
 Line Input #TempFile1,Var$
 For Temp1=1 To MaxConfigArray1
    Input #TempFile1,Var!
 Next
 For Temp1=1 To MaxConfigArray2
    Input #TempFile1,Var%
    Select Case Temp1
    Case 48
       Allow.High.Class=Var%
    Case 81
       Allow.Remote.Edit=Var%
    Case 86
       User.Echo=Var%
    Case 87
       User.Linefeeds=Var%
    Case 88
       User.Linelength=Var%
    Case 89
       User.Pagelength=Var%
    Case 90
       User.Wordwrap=Var%
    Case 111
       Disable.DM=Var%
    Case 112
       Disable.Nodes=Var%
    Case 113
       Disable.Sysop=Var%
    End Select
 Next
 For Temp1=1 To MaxConfigArray3
    Line Input #TempFile1,Var$
    Select Case Temp1
    Case 15
       File.Extension1=Var$
    Case 53
       Data.Path3=Var$
    Case 68
       MessWork.FileName=Var$
    Case 85
       ANSIPrefixChar=Var$
    End Select
 Next
 Exit Sub
Config.Error:
 Color 14,0
 Out2="CMD203 Module - |Toggle"+Chr$(13)+Chr$(10)
 Out3="Error reading "+Lcase$(FileName)+" (error:"+Str$(Err)+"). Run Dndcnfg."
 Call Terminate.ProgramX
End Sub

Sub Open.Files
 On Local Error Goto Open.Error
 Close
 MessWorkRecLen1=Len(MessWorkRecord1)
 MessWork.FileName=Data.Path3+MessWork.FileName
 FileName=MessWork.FileName+"1"+File.Extension1
 Open FileName For Random Shared As #MessWorkFile1 Len=MessWorkRecLen1
 Exit Sub
Open.Error:
 Color 14,0
 Out2="CMD203 Module - |Toggle"+Chr$(13)+Chr$(10)
 Out3="Error opening "+Lcase$(FileName)+" (error:"+Str$(Err)+"). Increase files= in config.sys or run Dndcnfg."+Chr$(13)+Chr$(10)
 Call Terminate.ProgramX
End Sub

Sub Reset.Cursor
 On Local Error Goto ErrMain13
 Call ScreenANSI(1,37,7)
 Locate Max.Row-1,1,1
 Graphics.Off=False
 Line.Length=False
MainExit13:
 Exit Sub
ErrMain13:
 Resume MainExit13
End Sub

Sub Detect.Share
 On Local Error Goto ErrResume149b
 Call Plex.Function(16)
 If (OutregsX.AX And &HFF)=&HFF Then
    Share.Installed=True
 Else
    Share.Installed=False
 Endif
 Windows.Detected = False
 Call Plex.Function(22)
 If OutregsX.BX>0 Then
    Windows.Detected = True
    ' store windows version
    Win.Minor=OutregsX.BX And &HFF
    Win.Major=(OutregsX.BX And &HFF00)/256
 Endif
ErrExit149b:
 Exit Sub
ErrResume149b:
 Resume ErrExit149b
End Sub

Sub Read.Rows(Var)
 On Local Error Goto Error.Trap62x
 Temp1=50
 Locate 50,1,0
 Var=50
 Exit Sub
Next.Trap1:
 Temp1=43
 Locate 43,1,0
 Var=43
 Exit Sub
Next.Trap2:
 Temp1=25
 Locate 25,1,0
 Var=25
 Exit Sub
Next.Trap3:
 Var=12
Error.Resume62x:
 Exit Sub
Error.Trap62x:
 If Temp1=50 Then
    Resume Next.Trap1
 Endif
 If Temp1=43 Then
    Resume Next.Trap2
 Endif
 If Temp1=25 Then
    Resume Next.Trap3
 Endif
 Resume Error.Resume62x
End Sub

' search for online user
Sub Search.Mess.File(Var1$,Var2)
 On Local Error Goto ErrMain41
 Var2=False
 For TempA=False To MaxNodes
    Call Read.Message.Record(MessWorkFile1,TempA+1)
    If MessWorkRecord1.ActiveNode Then
       Var$=Lcase$(Rtrim$(MessWorkRecord1.UserName))
       Select Case Rtrim$(Lcase$(Var$))
       Case "login", "logoff", "relogin"
          Eat$=Nul
       Case Deleted$, Offline$
          Eat$=Nul
       Case Else
          If Var$=Var1$ Then
             Var2=True
             Exit For
          Endif
       End Select
    Endif
 Next
MainExit41:
 Exit Sub
ErrMain41:
 Resume MainExit41
End Sub

Sub Terminate.ProgramX
 On Local Error Goto Error.Trap1
 Call ScreenANSI(4,37,15)
 Call ScreenANSI(2,Max.Row,1)
 If Len(Out2) Then
    Strng=Out2
    Call Scroll.Row(1)
 Endif
 If Len(Out3) Then
    Strng=Out3
    Call Scroll.Row(1)
 Endif
 Print
 Call Exit.To.DOSX
Error.Resume1:
 End
Error.Trap1:
 Resume Error.Resume1
End Sub

Sub Exit.To.DOSX
 On Local Error Goto Error.Trap2
 Call Extended.Driver(29)
 Call Modem.ANSI(1,37,7)
 Call ScreenANSI(4,33,7)
 Call ScreenANSI(2,Max.Row,1)
 If BBS.Mode Then
    Strng="Returning you to: "
    If Rtrim$(BBS.Name)=Nul Then
       Strng=Strng+"<n/a>"
    Else
       Strng=Strng+BBS.Name
    Endif
 Else
    If Windows.Detected Then
       Strng="Returning you to Windows Dos."
    Else
       Strng="Returning you to Dos."
    Endif
 Endif
 Call Scroll.Row(1)
 Call ScreenANSI(1,37,7)
 Color 7,0
Error.Resume2:
 Exit Sub
Error.Trap2:
 Resume Error.Resume2
End Sub

Sub Save.Screen2(Var)
 On Local Error Goto Error.Trap62
 If Var=False Then
    VarX3=(Max.Row-1)*80+80
    Redim Temp.ArrayY(VarX3) As Long
    Redim Temp.ArrayZ(VarX3) As Double
    For Var1=1 To Max.Row
       For Var2=1 To 80
          TempZ1=Screen(Var1,Var2)
          TempZ2=Screen(Var1,Var2,1)
          VarX2=(Var1-1)*80+Var2
          Temp.ArrayY(VarX2)=TempZ1
          Temp.ArrayZ(VarX2)=TempZ2
       Next
    Next
 Else
    Cls
    For VarX=Max.Row To 1 Step -1
       For VarY=1 To 80
          VarX2=(VarX-1)*80+VarY
          If Temp.ArrayY(VarX2)<>32 Then
             Goto Next.Line
          Endif
       Next
    Next
Next.Line:
    TempX=VarX
    For Var1=1 To VarX ' Max.Row
       For Var2=1 To 80
          VarX2=(Var1-1)*80+Var2
          VarB=Int(Temp.ArrayZ(VarX2)/16)
          VarF=Temp.ArrayZ(VarX2) Mod 16
          TempZ1=Temp.ArrayY(VarX2)
          Locate Var1,Var2,0
          Color VarF,VarB
          Print Chr$(TempZ1);
       Next
    Next
 Endif
Error.Resume62:
 Exit Sub
Error.Trap62:
 Resume Error.Resume62
End Sub


