 Rem File: Cmd021.bas
 Rem Sub: Bulletins
 Rem $Include: 'dndbbs.inc'
 On Error Goto Main.Error
 TempX=False
 If Chained<>-2 Then
    User.Linelength=80
    Call Read.Rows(Var)
    User.Pagelength=Var
    Max.Row=Var
    Local.Mode=True
    CALL Save.Screen2(0)
    Locate Max.Row-1,1,1
    Call Get.ANSI
    Call Get.Node
    Call Get.Config(Var1$,Var2$,Var3$,Var4$)
    Call Force.ANSI
    Graphics.Off=True
    Strng = "DNDBBS Command 021 -- Bulletins."
    Call IO.O
    Call Bulletins(Var1$,Var2$,Var3$,Var4$)
    Out2=Nul : Out3=Nul
    Call Terminate.ProgramX
    End
 Endif
 Var1$=Config3(1) ' prelog
 Var2$=Config3(3) ' welcome
 Var3$=Config3(4) ' notice
 Var4$=Config3(28) ' logoff
 Call Bulletins(Var1$,Var2$,Var3$,Var4$)
Main.Resume:
 If Chained=-2 Then
    Call Exit.To.DOSX
    Chain DND.Path+"DNDBBS.EXE"
 Endif
 End
Main.Error:
 Resume Main.Resume

Sub Bulletins(Var1$,Var2$,Var3$,Var4$)
 On Local Error Goto Error.Trap26
 Var5$=DND.Path
 If Var5$=Nul Then
    Var5$=Curdir$
 Endif
 If Right$(Var5$,1)<>"\" Then
    Var5$=Var5$+"\"
 Endif

 If TempX Then ' /w
    FileName=Var5$+"DNDUTIL.EXE"
    If Dir$(FileName)<>Nul Then
       FileName=FileName+" /L"
       Shell FileName
       Out3="Call to Dndutil success."
    Else
       Out3="Dndutil not found."
    Endif
    Call Clear.Status.Line
    Exit Sub
 Endif

 VarX$=".txt"
 If Local.Mode Then
    If Local.ANSI.Color Or Local.Avatar.Color Then
       VarX$=".ans"
    Endif
 Else
    If Remote.ANSI.Color Or Remote.Avatar.Color Then
       VarX$=".ans"
    Endif
 Endif
 Call Edit.Status.Line
 Color 15,0
 Do While Lost.Carrier=False
    Carriage.Return=False
    Graphics.Off=False
    Strng="Bulletin menu:" : Call IO.O
    Graphics.Off=True
    Strng="[1]Prelog" : Call IO.O
    Strng="[2]Welcome" : Call IO.O
    Strng="[3]Notice" : Call IO.O
    Strng="[4]Logoff" : Call IO.O
    Strng="[5]User list" : Call IO.O
    Strng="[6]Top ten users" : Call IO.O
    Strng="[7]Team list" : Call IO.O
    Strng="[8]Top ten teams" : Call IO.O
    Strng="[9]Last users" : Call IO.O
    Strng="[10]Write reports" : Call IO.O
    Strng="[11]Display filenames" : Call IO.O
    If Local.Mode Then
       Strng="[12]Print reports" : Call IO.O
    Endif
    Graphics.Off=False
    If Local.Mode Then
       Strng="Enter bulletin selection(1-12, Q to quit)? "
    Else
       Strng="Enter bulletin selection(1-11, Q to quit)? "
    Endif
    No.Input.Out="Q"
    Call IO.I
    If Ucase$(Out2)="Q" Then
       Exit Do
    Endif
    VarQ=Int(Val(Out2))
    Select Case VarQ
    Case 1
       Var$=Data.Path1+Var1$+File.Extension1
    Case 2
       Var$=Data.Path1+Var2$+File.Extension1
    Case 3
       Var$=Data.Path1+Var3$+File.Extension1
    Case 4
       Var$=Data.Path1+Var4$+File.Extension1
    Case 5
       Var$=Var5$+"userlist"+VarX$
    Case 6
       Var$=Var5$+"ranklist"+VarX$
    Case 7
       Var$=Var5$+"teamlist"+VarX$
    Case 8
       Var$=Var5$+"teamrank"+VarX$
    Case 9
       Var$=Var5$+"lastlist"+VarX$
    Case 10
       FileName=Var5$+"DNDUTIL.EXE"
       If Dir$(FileName)<>Nul Then
          FileName=FileName+" /L"
          Shell FileName
          If Local.Mode=False Then
             Strng="Call to Dndutil success." : Call IO.O
          Endif
       Else
          Strng="Dndutil not found." : Call IO.O
       Endif
    Case 11
       Graphics.Off=True
       Strng="Prelog: "+Data.Path1+Var1$+File.Extension1 : Call IO.O
       Strng="Welcome: "+Data.Path1+Var2$+File.Extension1 : Call IO.O
       Strng="Notice: "+Data.Path1+Var3$+File.Extension1 : Call IO.O
       Strng="Logoff: "+Data.Path1+Var4$+File.Extension1 : Call IO.O
       Call More.Prompt
       Graphics.Off=True
       Strng="Userlist: "+Var5$+Ucase$("userlist"+VarX$) : Call IO.O
       Strng="Ranklist: "+Var5$+Ucase$("ranklist"+VarX$) : Call IO.O
       Strng="Teamlist: "+Var5$+Ucase$("teamlist"+VarX$) : Call IO.O
       Strng="Teamrank: "+Var5$+Ucase$("teamrank"+VarX$) : Call IO.O
       Strng="Lastlist: "+Var5$+Ucase$("lastlist"+VarX$) : Call IO.O
       Call More.Prompt
    Case 12
       If Local.Mode Then
          FileName=Var5$+"DNDUTIL.EXE"
          If Dir$(FileName)<>Nul Then
             FileName=FileName+" /P"
             Shell FileName
          Else
             Strng = "Dndutil not found." : Call IO.O
          Endif
       Endif
    End Select
    Select Case VarQ
    Case 1 To 4
       If Chained=-2 Then
          Call Out.File(Var$,"")
       Else
          Call Out.File(Var$,"-2")
       Endif
    Case 5 To 9
       If Chained=-2 Then
          Call Out.File(Var$,VarX$)
       Else
          Call Out.File(Var$,"-2")
       Endif
    End Select
 Loop
Error.Resume26:
 Call Clear.Status.Line
 Exit Sub
Error.Trap26:
 Resume Error.Resume26
End Sub

Sub Get.Node
 On Local Error Goto Node.Error
 Var$=Command$
 Var$=Ltrim$(Rtrim$(Var$))
 If Var$="/?" Then Goto Boot.Usage
 If Var$="-?" Then Goto Boot.Usage
 If Var$="/HELP" Then Goto Boot.Usage
 Var=Instr(Var$,"/W")
 If Var Then
    TempX=True
    Var$=Left$(Var$,Var-1)+Mid$(Var$,Var+2)
 Endif

 Var=Instr(Var$,"/A")
 If Var Then
    Local.ANSI.Color=True
    Var$=Left$(Var$,Var-1)+Mid$(Var$,Var+2)
 Endif

 Var=Instr(Var$,"/V")
 If Var Then
    Local.Avatar.Color=True
    Var$=Left$(Var$,Var-1)+Mid$(Var$,Var+2)
 Endif

 Var$=Ltrim$(Rtrim$(Var$))
 If Left$(Var$,3)="/N:" Then
    Var$=Mid$(Var$,4)
    Select Case Len(Var$)
    Case 0 ' /N:
       Node=0
       NodeX=Nul
       Var$=Nul
    Case 1 ' 0-9,A-Z
       Select Case Var$
       Case "0" To "9" 
          Node=Val(Var$)+1
          NodeX=Var$
          Var$=Nul
       Case "A" To "Z"
          Node=Asc(Var$)-54
          NodeX=Var$
          Var$=Nul
       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
          Var$=Nul
       Else
          Goto Node.Error
       Endif
    Case Else
       Goto Node.Error
    End Select
 Endif
 Var$=Ltrim$(Rtrim$(Var$))
 If Var$=Nul Then
    Exit Sub
 Endif
Node.Error:
 Color 14,0
 Out2="CMD021 Module - Bulletins"
 Out3="Error reading node. Node must be <nul>,0-9,A-Z,10-99."
 Call Terminate.ProgramX
 Exit Sub
Boot.Usage:
 Color 15
 Print "Cmd021 Module - Bulletins."
 Color 14
 Print "Usage:"
 Color 15
 Print " Cmd021 /W/N:<node>"
 Color 14
 Print "Where:"
 Color 15
 Print " /W - write bulletin files."
 Print " <node> = node <nul>,0-9,A-Z,10-99."
 Color 7
 End
End Sub

Sub Get.Config(Var1$,Var2$,Var3$,Var4$)
 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
 If DND.Path=Nul Then
    DND.Path=Curdir$
 Endif
 If Right$(DND.Path,1)<>"\" Then
    DND.Path=DND.Path+"\"
 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%
 Next
 For Temp1=1 To MaxConfigArray3
    Line Input #TempFile1,Var$
    Select Case Temp1
    Case 1
       Var1$=Var$ ' prelog
    Case 3
       Var2$=Var$ ' welcome
    Case 4
       Var3$=Var$ ' notice
    Case 28
       Var4$=Var$ ' logoff
    Case 11
       Data.Path1=Var$ ' \dnddat
    Case 15
       File.Extension1=Var$ ' .dat
    Case 85
       ANSIPrefixChar=Var$ ' most likely a {
    End Select
 Next
 Exit Sub
Config.Error:
 Color 14,0
 Out2="CMD021 Module - Bulletins"
 Out3="Error reading "+Lcase$(FileName)+" (error:"+Str$(Err)+"). Run Dndcnfg."
 Call Terminate.ProgramX
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

Sub Terminate.ProgramX
 On Local Error Goto Error.Trap1
 If Chained=-2 Then
    Call Exit.To.DOSX
    Chain DND.Path+"DNDBBS.EXE"
 Endif
 Graphics.Off=True
 Strng = Nul
 Call IO.O
 Call ScreenANSI(1,37,7)
 Color 7,0
 Cls
 CALL Save.Screen2(-1)
 If TempX=0 Then TempX=1
 LOCATE TempX, 1, 1
 Color 15
 Print
 If Len(Out2) Then
    Print Out2
 Endif
 If Len(Out3) Then
    Print Out3
 Endif
 Color 15
 Print "Now exiting Bulletins.."
 Color 7
Error.Resume1:
 End
Error.Trap1:
 Resume Error.Resume1
End Sub

Sub Exit.To.DOSX
 On Local Error Goto Error.Trap2
 Color 15
 Strng="Returning you to Dndbbs."
 If BBS.Mode Then
    If Rtrim$(BBS.Name)<>Nul Then
       Strng="Returning you to: "+BBS.Name
    Endif
 Endif
 Call IO.O
 Color 7,0
Error.Resume2:
 Exit Sub
Error.Trap2:
 Resume Error.Resume2
End Sub

Sub Edit.Status.Line
 On Local Error Goto Error.Trap58
 Var$="DND Editor <timeon: "+Fclock$+">"
 Var$=Left$(Var$,79)
 Var$=Var$+Space$(79-Len(Var$))
 Call Display.Status.LineX(True,Var$)
Error.Resume58:
 Exit Sub
Error.Trap58:
 Resume Error.Resume58
End Sub

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

Sub Display.Status.LineX(Var,Var$)
 On Local Error Goto Error.Trap29
 Call Cursor(False)
 If Var Then
    Color 14,1
 Else
    Color 15,0
 Endif
 Locate Max.Row,1,0
 Print Var$;
 If Var Then
    Color 0, 1
 Else
    Color 7, 0
 Endif
 Locate Max.Row,80
 Print " ";
 Locate Max.Row-1,1,1
 Graphics.Off=False
 Call Restore.Color
 Call Cursor(True)
Error.Resume29:
 Exit Sub
Error.Trap29:
 Resume Error.Resume29
End Sub

Sub Get.ANSI
 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
End Sub

Sub Force.ANSI
 On Local Error Goto Error.Trap62z
 FileName=Data.Path1+"START"+NodeX+".CFG"
 Close #TempFile1
 If Dir$(FileName)<>Nul Then
    Open Filename For Input Shared As #TempFile1
    Do
       If Eof(TempFile1) Then
          Exit Do
       Endif
       Line Input #TempFile1,Var$
       Var$=Ltrim$(Rtrim$(Var$))
       If Var$<>Nul Then
          If Left$(Var$,1)<>";" Then
             Var=Instr(Var$,"=")
             If Var Then
                Var1$=Left$(Var$,Var-1)
                Var2$=Mid$(Var$,Var+1)
                Var1$=Ltrim$(Rtrim$(Var1$))
                Var2$=Ltrim$(Rtrim$(Var2$))
                Var1$=Ucase$(Var1$)
                Var2$=Ucase$(Var2$)
                If Len(Var1$) Then
                   If Len(Var2$) Then
                      Select Case Var1$
                      Case "FORCEANSI"
                         Select Case Var2$
                         Case "-1", "ON", "TRUE"
                            Local.Ansi.Color=-1
                         End Select
                      Case "FORCEAVATAR"
                         Select Case Var2$
                         Case "-1", "ON", "TRUE"
                            Local.Avatar.Color=-1
                         End Select
                      End Select
                   Endif
                Endif
             Endif
          Endif
       Endif
    Loop
 Endif
Error.Resume62z:
 Exit Sub
Error.Trap62z:
 Resume Error.Resume62z
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
