;ProfiPacket - packet radio terminal program
;Copyright (C) 1999  Alexander Feigl
;
;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, write to the Free Software
;Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
;
;Author:
;
;Alexander Feigl
;Burachstraße 51
;
;D-88250 Weingarten
;
;EMail : Alexander.Feigl@gmx.de

                CPU M68000
                SNMAOPT Q+,P+,A+,B-,S+,M+,E+
                nolist
                
AJUMP                   MACRO
                        dc.w $4ef9
                        dc.l \1
                        ENDM

                        incdir "sys:include/"
                        include "exec/exec_lib.i"
                        include "/global.i"
                        include "utility/utility_lib.i"

                                
_Nil                    moveq #0,d0
                        rts
                        dc.b "ProfCmds"
                
                
DCOL_Head               dc.l DCOL_Name
                        dc.l 1
                        dc.l DCOL_Init
                        dc.l 0
                        dc.l 8                ;Subtype (3=Miscellaneous)
                        
DCOL_Name               dc.b "Datacollector",0
                        even
DCOL_Init               movem.l a6/a2-a4/d2-d7,-(sp)
                        move.l a0,a4
                        move.l a1,d7
                        move.l d0,d6
                        move.l a3,a6
                        move.l #DCOL_Functions-DCOL_FunctionsBegin,d0
                        move.l #dcol_Size,d1
                        lea DCOL_Functions,a0
                        move.l ppb_Global(a6),a5
                        jsr _QVOCreateServerBase(A6)
                        tst.l d0
                        beq .Cannot_Serve
                        move.l d0,a6
                        move.l a3,a5
                        move.l a3,dcol_QSOBase(a6)
                        move.l d7,dcol_OldServer(A6)
                        move.l d6,dcol_Channel(a6)
                        move.l a2,dcol_Parameter(a6)
                        move.l a4,dcol_ThisQSO(a6)
                        move.l a6,qso_Server(a4)
                        
                        clr.l dcol_InLine(a6)
                        move.l #1,dcol_FirstInvoke(a6)
                        clr.l dcol_Requestings(a6)

                        clr.l dcol_BBSMode(a6)
                        lea qso_Call(a4),a0
                        lea dcol_Call(a6),a1
.Copy_FromCall          move.b (a0)+,d0
                        tst.b d0
                        beq .CFCE
                        cmp.b #"-",d0
                        beq .CFCE
                        move.b d0,(a1)+
                        bra .Copy_FromCall
.CFCE                   clr.b (a1)+

                        clr.b dcol_Name(a6)
                        clr.b dcol_QTH(a6)
                        clr.b dcol_Locator(a6)
                        clr.b dcol_PLZ(a6)
                        clr.b dcol_Homebbs(a6)


                        move.l a2,a0
                        GOQSO ParseComLine
                        tst.l d0
                        blt .Invalid_Parameters
                        cmp.l #1,d0
                        bgt .Invalid_Parameters

                        move.l a2,a0
                        cmp.l #0,a0
                        beq .No_BBSMode
                        cmp.b #"B",(a0)
                        bne .No_BBSMode
                        cmp.b #"B",1(a0)
                        bne .No_BBSMode
                        cmp.b #"S",2(a0)
                        bne .No_BBSMode
                        move.l #1,dcol_BBSMode(a6)
.No_BBSMode
                        movem.l a2/a3/d2,-(sp)
                        move.l dcol_ThisQSO(a6),a0
                        lea dcol_Name(a6),a1
                        lea dcol_QTH(a6),a2
                        lea dcol_Locator(a6),a3
                        move.l a3,d0
                        lea dcol_PLZ(a6),a3
                        move.l a3,d1
                        lea dcol_Homebbs(a6),a3
                        move.l a3,d2
                        GOQSO FillUserdata
                        movem.l (sp)+,a2/a3/d2

                        ;Verify Settings
                        tst.b dcol_Name(a6)
                        beq .Need_UserInput
                        tst.b dcol_QTH(a6)
                        beq .Need_UserInput
                        tst.l dcol_BBSMode(a6)
                        beq .All_Data_Here
                        tst.b dcol_Homebbs(a6)
                        beq .Need_UserInput


.All_Data_Here          movem.l a2/a3/d2,-(sp)
                        move.l dcol_ThisQSO(A6),a0
                        lea dcol_Name(a6),a1
                        lea dcol_QTH(a6),a2
                        lea dcol_Locator(a6),a3
                        move.l a3,d0
                        lea dcol_PLZ(a6),a3
                        move.l a3,d1
                        lea dcol_Homebbs(a6),a3
                        move.l a3,d2
                        GOQSO WriteUserdata
                        movem.l (sp)+,a2/a3/d2
                        bsr DCOL_Cleanup
                        bra .Exit

.Need_UserInput         move.l #-1,dcol_Step(a6)
                        bsr DCOL_NextStep
                        tst.l d0
                        bne .All_Data_Here

.Server_Success         bsr DCOL_ReceivePacket
.Exit                   movem.l (sp)+,a6/a2-a4/d2-d7
                        moveq #0,d0
                        rts


.Cannot_Serve           movem.l (sp)+,a6/a2-a4/d2-d7
                        moveq #-1,d0
                        rts

.No_User_Possible       moveq #1,d0
                        bra .Abort_With_Text
                        
.Invalid_Parameters     moveq #0,d0
.Abort_With_Text        bsr Get_Text
                        move.l d0,a0
                        move.l dcol_Channel(a6),d0
                        moveq #1,d1
                        moveq #0,d2
                        GOQSO TransmitText
                        bsr DCOL_Cleanup
                        movem.l (Sp)+,a6/a2-a4/d2-d7
                        moveq #0,d0
                        rts
                        

DCOL_Cleanup
                        move.l dcol_OldServer(a6),d0
                        move.l dcol_ThisQSO(a6),a0
                        move.l d0,qso_Server(a0)
                        move.l a6,a1
                        move.l ppb_ExecBase(a5),a6
                        sub.l #DCOL_Functions-DCOL_FunctionsBegin,a1
                        jsr _LVOFreeVec(A6)
                        moveq #0,d0
                        rts

DCOL_ConnectProced      rts

DCOL_ReceivePacket      movem.l d2-d7/a2-a6,-(sp)
.Loop                   move.l dcol_ThisQSO(a6),a0
                        GOQSO TestServBufferLin
                        tst.l d0
                        beq .Exit
                        move.l d0,d1
                        add.l dcol_InLine(a6),d1
                        cmp.l #1024,d1
                        ble .Good_Line
                        move.l #1024,d1
.Good_Line              lea dcol_ServerLine(a6),a0
                        add.l dcol_InLine(a6),d0
                        move.l dcol_ThisQSO(A6),a1
                        GOQSO ReadServBuffer
                        add.l d0,dcol_InLine(a6)
                        move.l dcol_InLine(a6),d0
                        tst.l d0
                        beq .Exit
                        move.l dcol_InLine(a6),d0
                        lea dcol_ServerLine(a6),a0
                        subq.l #1,d0
                        add.l d0,a0
                        cmp.b #$0d,(a0)
                        bne .Exit
                        move.l a0,-(sp)
                        sub.l a1,a1
                        lea dcol_ServerLine(a6),a0
                        cmp.b #"[",(a0)
                        bne .No_Surpress
.Loop_Surpress          cmp.b #"]",(a0)
                        beq .Surpress_EndBracket
                        cmp.b #" ",(a0)
                        beq .No_Surpress
                        cmp.b #"-",(a0)+
                        bne .Loop_Surpress
                        move.l a0,a1
                        bra .Loop_Surpress
.Surpress_EndBracket    cmp.b #$d,1(a0)
                        bne .No_Surpress
                        cmpa.l #0,a1
                        beq .No_Surpress
                        clr.b 1(a0)
                        lea dcol_ServerLine(a6),a0
                        lea dcol_ReceivedSID(a6),a1
                        GOQSO ParseSID
                        tst.l d0
                        beq .No_GoodSID

                        move.l dcol_ThisQSO(a6),a0
                        lea dcol_ReceivedSID(a6),a1
                        GOQSO HandleSID



                        lea dcol_ReceivedSID(a6),a0
                        cmp.l #2,sid_Program(a0)
                        beq .XP_SID
                        cmp.l #10,sid_Program(a0)
                        beq .XP_SID
                        cmp.l #1,sid_Program(a0)
                        bne .No_Trans_SID

                        ;Valid PP Signature / XP Signature

.XP_SID                 move.l dcol_Channel(a6),d0
                        GOQSO TestNameRequest
                        move.l d0,dcol_Requestings(a6)

.No_Trans_SID           move.l (sp)+,a0
                        clr.l dcol_InLine(a6)
                        bra .Loop
.No_GoodSID
.No_Surpress            move.l dcol_InLine(a6),d0
                        move.l dcol_Channel(a6),d1
                        lea dcol_ServerLine(a6),a0
                        move.l d2,-(Sp)
                        moveq #2,d2
                        GOQSO WriteTerminalUni
                        move.l (sp)+,d2
                        move.l (sp)+,a0
                        clr.b (a0)

                        lea dcol_ServerLine(a6),a0
                        tst.b (a0)
                        beq .Abort_Step
                        cmp.b #"#",(a0)
                        bne .No_XPLine

                        move.l dcol_ThisQSO(A6),a0
                        lea dcol_Name(a6),a1
                        lea dcol_QTH(a6),a2
                        lea dcol_Locator(a6),a3
                        move.l a3,d0
                        lea dcol_PLZ(a6),a3
                        move.l a3,d1
                        lea dcol_Homebbs(a6),a3
                        move.l a3,d2
                        GOQSO WriteUserdata

                        lea dcol_ServerLine(a6),a0
                        move.l dcol_Channel(a6),d0
                        GOQSO HandleXPLine
                        cmp.l #2,d0
                        bne .Ignore_Line

                        move.l dcol_ThisQSO(a6),a0
                        lea dcol_Name(a6),a1
                        lea dcol_QTH(a6),a2
                        lea dcol_Locator(a6),a3
                        move.l a3,d0
                        lea dcol_PLZ(a6),a3
                        move.l a3,d1
                        lea dcol_Homebbs(a6),a3
                        move.l a3,d2
                        GOQSO FillUserdata


                        ;Verify Settings
                        tst.b dcol_Name(a6)
                        beq .Restart_AfterXP
                        tst.b dcol_QTH(a6)
                        beq .Restart_AfterXP
                        tst.b dcol_Locator(a6)
                        beq .Restart_AfterXP
                        tst.l dcol_BBSMode(a6)
                        beq .XP_Succeed
                        tst.b dcol_Homebbs(a6)
                        beq .Restart_AfterXP
                        bra .XP_Succeed
.Restart_AfterXP        move.l #-1,dcol_Step(a6)
                        bsr DCOL_NextStep
                        tst.l d0
                        bne .XP_Succeed
.Ignore_Line            clr.l dcol_InLine(a6)
                        bra .Loop

.No_XPLine

                        move.l #64,d1
                        move.l dcol_Step(a6),d0
                        cmp.l #2,d0
                        blt .Long_Step
                        move.l #16,d1
.Long_Step              bsr DCOL_GetStringPos
                        move.l a0,a1
                        subq.l #1,d1
                        lea dcol_ServerLine(a6),a0
.Copy_SLine             move.b (a0)+,(a1)+
                        subq.l #1,d1
                        tst.l d1
                        beq .Copy_SLineEnd
                        tst.b -1(a0)
                        bne .Copy_SLine
.Copy_SLineEnd          clr.b (A1)+


                        cmp.l #2,dcol_Step(a6)
                        bne .No_Locator
                        lea dcol_Locator(a6),a0
                        GOQSO ValidateLocator
                        tst.l d0
                        beq .Every_Good
                        clr.b dcol_Locator(a6)
                        sub.l #1,dcol_Step(a6)
                        bra .Go_NextStep

.No_Locator             cmp.l #4,dcol_Step(a6)
                        bne .Every_Good
                        lea dcol_Homebbs(a6),a0
.Parse_HRoute           move.b (a0)+,d0
                        tst.b d0
                        beq .No_HRoute
                        cmp.b #".",d0
                        bne .Parse_HRoute
                        clr.b -1(a0)
.No_HRoute              lea dcol_Homebbs(a6),a0
                        moveq #0,d1
.Homebbs_Upper          move.b (a0),d0
                        tst.b d0
                        beq .Every_Good
                        cmp.l #6,d1
                        beq .Bad_BBS
                        addq.l #1,d1
                        cmp.b #$61,d0
                        blt .BBS_IsLower
                        cmp.b #$7a,d0
                        bgt .BBS_IsLower
                        sub.b #$20,d0
.BBS_IsLower            move.b d0,(a0)+
                        bra .Homebbs_Upper
.Bad_BBS                clr.b dcol_Homebbs(a6)
.Abort_Step             sub.l #1,dcol_Step(a6)
                        bra .Go_NextStep
.Every_Good             
.Go_NextStep            clr.l dcol_InLine(a6)
                        bsr DCOL_NextStep
                        tst.l d0
                        beq .Loop
                        movem.l a2/a3/d2,-(sp)
                        move.l dcol_ThisQSO(A6),a0
                        lea dcol_Name(a6),a1
                        lea dcol_QTH(a6),a2
                        lea dcol_Locator(a6),a3
                        move.l a3,d0
                        lea dcol_PLZ(a6),a3
                        move.l a3,d1
                        lea dcol_Homebbs(a6),a3
                        move.l a3,d2
                        GOQSO WriteUserdata
                        movem.l (sp)+,a2/a3/d2
.XP_Succeed             moveq #7,d0
                        bsr Get_Text
                        move.l d0,a0
                        lea dcol_Name(a6),a1
                        move.l a1,-(sp)
                        move.l a7,a1
                        move.l dcol_Channel(a6),d0
                        moveq #1,d1
                        GOQSO OutputFormat
                        move.l (sp)+,d0
                        bsr DCOL_AbortServer
.Exit                   movem.l (Sp)+,d2-d7/a2-a6
                        rts

DCOL_TerminalInput      rts
DCOL_WriteTerminal      move.l dcol_Channel(a6),d1
                        moveq #2,d2
                        GOQSO WriteTerminalUni
                        rts

DCOL_AbortServer        move.l a6,-(sp)
                        move.l dcol_ThisQSO(a6),a0
                        GOQSO TestServBuffer
                        move.l d0,-(sp)
                        move.l dcol_OldServer(a6),-(sp)
                        bsr DCOL_Cleanup
                        move.l (sp)+,a6
                        move.l (Sp)+,d0
                        cmpa.l #0,a6
                        beq .Exit
                        jsr _SVOReceivePacket(A6)
.Exit                   move.l (sp)+,a6
                        rts

DCOL_GetServerType      moveq #8,d0
                        rts




DCOL_Nop                rts

                        
DCOL_AskAudio           move.l a6,-(sp)
                        move.l dcol_OldServer(a6),a6
                        cmpa.l #0,a6
                        beq .NoServer
                        jsr _SVOAskAudio(a6)
                        move.l (sp)+,a6
                        rts
.NoServer               moveq #0,d0
                        move.l (sp)+,a6
                        rts



DCOL_GetStringPos       move.l dcol_Step(a6),d0
                        lea dcol_Name(a6),a0
                        tst.l d0
                        beq .Exit
                        lea dcol_QTH(a6),a0
                        subq.l #1,d0
                        tst.l d0
                        beq .Exit
                        lea dcol_Locator(a6),a0
                        subq.l #1,d0
                        tst.l d0
                        beq .Exit
                        lea dcol_PLZ(a6),a0
                        subq.l #1,d0
                        tst.l d0
                        beq .Exit
                        lea dcol_Homebbs(a6),a0
                        subq.l #1,d0
                        tst.l d0
                        beq .Exit
.Exit                   rts

DCOL_NextStep           move.l dcol_Step(a6),d0
                        addq.l #1,d0
                        move.l d0,dcol_Step(a6)
                        cmp.l #5,d0
                        beq .Dcol_End
                        tst.l d0
                        bne .Not_First
                        tst.l dcol_FirstInvoke(a6)
                        beq .Not_First
                        clr.l dcol_FirstInvoke(a6)
                        moveq #1,d0
                        bsr Get_Text
                        move.l d0,a0
                        move.l dcol_Channel(a6),d0
                        moveq #1,d1
                        moveq #0,d2
                        move.l d2,-(sp)
                        GOQSO TransmitText
                        move.l (sp)+,d2
.Not_First              bsr DCOL_GetStringPos
                        tst.b (a0)
                        bne DCOL_NextStep
                        tst.l dcol_BBSMode(a6)
                        bne .Next_BBSMode
                        move.l dcol_Step(a6),d0
                        cmp.l #3,d0
                        beq DCOL_NextStep
                        cmp.l #4,d0
                        beq DCOL_NextStep
                        bra .Good_Step
.Next_BBSMode
.Good_Step              move.l dcol_Step(a6),d0
                        move.l dcol_Requestings(a6),d1
                        btst d0,d1
                        beq .Printout_Request
                        moveq #0,d0
                        rts
.Printout_Request       move.l dcol_Step(a6),d0
                        addq.l #2,d0
                        bsr Get_Text
                        move.l d0,a0
                        move.l dcol_Channel(a6),d0
                        moveq #1,d1
                        move.l d2,-(sp)
                        moveq #0,d2
                        GOQSO TransmitText
                        move.l (sp)+,d2
                        moveq #0,d0
                        rts

.Dcol_End               moveq #1,d0
                        rts
                        
DCOL_FunctionsBegin

                        AJUMP DCOL_AskAudio
                        AJUMP DCOL_AbortServer              ;-48
                        AJUMP DCOL_GetServerType                ;-42
                        AJUMP DCOL_TerminalInput                ;-36
                        AJUMP DCOL_ReceivePacket                ;-30
                        AJUMP DCOL_Nop                  ;-24
                        AJUMP DCOL_ConnectProced                ;-18
                        AJUMP DCOL_Cleanup              ;-12
                        AJUMP DCOL_Init                 ;-6
DCOL_Functions
                        
                        even
                        
                        
                        rsreset
dcol_ThisQSO            rs.l 1
dcol_Channel            rs.l 1
dcol_OldServer          rs.l 1
dcol_QSOBase            rs.l 1
dcol_Parameter          rs.l 1
dcol_BBSMode            rs.l 1
dcol_Step               rs.l 1  ;0=Name 1=QTH 2=Locator 3=PLZ 4=Homebbs
dcol_InLine             rs.l 1
dcol_FirstInvoke        rs.l 1
dcol_Requestings        rs.l 1
dcol_ServerLine         rs.b 1024
dcol_Call               rs.b 16
dcol_Name               rs.b 64
dcol_QTH                rs.b 64
dcol_Locator            rs.b 16
dcol_PLZ                rs.b 16
dcol_Homebbs            rs.b 16
dcol_ReceivedSID        rs.b sid_Size
dcol_Size               rs.b 0






Get_Text                lea _DatacollectorCatalog(pc),a0
                        move.l dcol_ThisQSO(a6),a1
                        move.l a2,-(sp)
                        lea _DatacollectorCatalogName(pc),a2
                        GOQSO GetText
                        move.l (sp)+,a2
                        rts

                        include "/Catalogs/Sources/Datacollector.s"
 
