;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 "utility/utility_lib.i"
                        include "/global.i"
                                
_Nil                    moveq #0,d0
                        rts
                        dc.b "ProfCmds"
                
                
                        dc.l SMAIL_Name
                        dc.l 1
                        dc.l SMAIL_Init_1
                        dc.l SMAIL_Head_2       ;Next
                        dc.l 512                ;Subtype (9=Mailing)

                
SMAIL_Head_2            dc.l SMAIL_Name_2
                        dc.l 1
                        dc.l SMAIL_Init_2
                        dc.l SMAIL_Head_3       ;Next
                        dc.l 512                ;Subtype (9=Mailing)
                        
                
SMAIL_Head_3            dc.l SMAIL_Name_3
                        dc.l 1
                        dc.l SMAIL_Init_3
                        dc.l SMAIL_Head_4       ;Next
                        dc.l 512                ;Subtype (9=Mailing)
SMAIL_Head_4            dc.l SMAIL_Name_4
                        dc.l 1
                        dc.l SMAIL_Init_4
                        dc.l SMAIL_Head_5
                        dc.l 512
                        
SMAIL_Head_5            dc.l SMAIL_Name_5
                        dc.l 1
                        dc.l SMAIL_Init_5
                        dc.l RMAIL_Head
                        dc.l 512

                        
SMAIL_Name              dc.b "SM",$01,"AIL",0
SMAIL_Name_2            dc.b "bbs_SP",0
SMAIL_Name_3            dc.b "bbs_SB",0
SMAIL_Name_4            dc.b "bbs_FBBBatch",0
SMAIL_Name_5            dc.b "bbs_SA",0
                        even
                        
SMAIL_Init_1            moveq #0,d1
                        bra SMAIL_Init
SMAIL_Init_2            moveq #1,d1
                        bra SMAIL_Init
SMAIL_Init_3            moveq #2,d1
                        bra SMAIL_Init
SMAIL_Init_4            moveq #3,d1
                        bra SMAIL_Init
SMAIL_Init_5            moveq #4,d1
                        bra SMAIL_Init

SMAIL_Init              movem.l a6/a2-a4/d5-d7,-(sp)
                        move.l a0,a4
                        move.l a1,d7
                        move.l d0,d6
                        move.l a3,a6
                        move.l #SMAIL_Functions-SMAIL_FunctionsBegin,d0
                        move.l d1,-(Sp)
                        move.l #smail_Size,d1
                        lea SMAIL_Functions,a0
                        move.l ppb_Global(a6),a5
                        jsr _QVOCreateServerBase(A6)
                        move.l (sp)+,d1
                        tst.l d0
                        beq .Cannot_Serve
                        move.l d0,a6
                        move.l a3,a5
                        move.l d1,smail_MailMode(a6)
                        move.l a3,smail_QSOBase(a6)
                        move.l d7,smail_OldServer(A6)
                        move.l d6,smail_Channel(a6)
                        move.l a2,smail_Parameter(a6)
                        move.l a4,smail_ThisQSO(a6)
                        move.l a6,qso_Server(a4)
                        clr.l smail_MailHandle(a6)
                        clr.l smail_InLine(a6)
                        clr.l smail_Boxmode(a6)
                        clr.l smail_BatchMode(a6)

                        tst.l smail_MailMode(a6)
                        beq .No_BOXMODE
                        move.l smail_ThisQSO(a6),a0
                        tst.l qso_ForwardFlag(a0)
                        beq .No_BOXMODE
                        move.l #1,smail_Boxmode(a6)
.No_BOXMODE             cmp.l #3,smail_MailMode(a6)
                        bne .No_FBBBatch
                        move.l #1,smail_BatchMode(a6)
                        move.l smail_Parameter(a6),smail_BatchBase(a6)
                        clr.l smail_Parameter(a6)
                        move.l smail_BatchBase(a6),a0

                        move.l (a0),smail_Personal(a6)
                        move.l 4(a0),smail_FromCall(a6)
                        move.l 8(a0),smail_AtCall(a6)
                        move.l 12(a0),smail_ToCall(a6)
                        move.l 16(a0),smail_Bid(a6)
                        clr.l 28(a0)
                        move.l #1,smail_Boxmode(a6)
                        bra .Boxmode_Init


.No_FBBBatch

                        move.l a2,a0
                        GOQSO ParseComLine
                        tst.l d0
                        ble .Invalid_Parameters
                        move.l d0,d7
                        move.l smail_ThisQSO(a6),a0
                        lea qso_Call(a0),a0
                        move.l a0,smail_FromCall(a6)
                        clr.l smail_ToCall(a6)
                        clr.l smail_Bid(a6)
                        clr.l smail_AtCall(a6)
                        move.l #2,smail_Personal(a6)
                        cmp.l #4,smail_MailMode(a6)
                        beq .Do_Persmail
                        move.l #1,smail_Personal(a6)
                        cmp.l #2,smail_MailMode(a6)
                        bne .Do_Persmail
                        clr.l smail_Personal(a6)
.Do_Persmail            move.l smail_Parameter(a6),a3
                        move.l a3,smail_ToCall(a6)
.Next_Parameter_TC      tst.b (a3)+
                        bne .Next_Parameter_TC
                        subq.l #1,d7
.Parsing_Loop           tst.l d7
                        beq .Parameter_Parsed
                        subq.l #1,d7
                        cmp.b #"@",(a3)
                        beq .Parameter_At
                        cmp.b #"$",(a3)
                        beq .Parameter_Bid
                        cmp.b #"<",(a3)
                        beq .Parameter_From
                        cmp.b #"#",(a3)
                        beq .Parameter_Livetime
                        cmp.b #$1a,(a3)
                        beq .Parameter_Parsed_Z
                        tst.l smail_Boxmode(a6)
                        beq .Parameter_Error
                        move.l smail_ToCall(a6),a0
                        tst.b 1(a0)
                        bne .Parameter_Error
                        cmp.b #"M",(a0)
                        beq .Skip_Parameter
                        cmp.b #"E",(a0)
                        beq .Skip_Parameter
                        bra .Parameter_Error
.Parameter_At           addq.l #1,a3
                        tst.b (a3)
                        bne .Param_AtGood
                        tst.l d7
                        beq .Parameter_Error
                        subq.l #1,d7
                        addq.l #1,a3
.Param_AtGood           move.l a3,smail_AtCall(A6)
.Skip_Parameter         tst.b (a3)+
                        bne .Skip_Parameter
                        bra .Parsing_Loop
.Parameter_Livetime     addq.l #1,a3
                        tst.b (a3)
                        bne .Param_LtGood
                        tst.l d7
                        beq .Parameter_Error
                        subq.l #1,d7
                        addq.l #1,a3
.Param_LtGood           bra .Skip_Parameter

.Parameter_Bid          addq.l #1,a3
                        tst.b (a3)
                        bne .Param_BidGood
                        tst.l d7
                        beq .Parameter_Error
                        subq.l #1,d7
                        addq.l #1,a3
.Param_BidGood          move.l a3,smail_Bid(A6)
                        tst.l smail_Boxmode(a6)
                        bne .Skip_Parameter
                        move.l a3,a0
                        GOQSO SearchMailBid
                        tst.l d0
                        beq .Skip_Parameter
                        moveq #11,d0
                        bra .Abort_With_Text
.Parameter_From         addq.l #1,a3
                        tst.b (a3)
                        bne .Param_FromGood
                        tst.l d7
                        beq .Parameter_Error
                        subq.l #1,d7
                        addq.l #1,a3
.Param_FromGood         move.l a3,smail_FromCall(A6)
                        bra .Skip_Parameter
                        
.Parameter_Error        bra .Invalid_Parameters

.Parameter_Parsed_Z     tst.l smail_Boxmode(a6)
                        beq .Parameter_Error
                        move.l smail_ToCall(a6),a0
                        tst.b 1(a0)
                        bne .Parameter_Error
                        cmp.b #"E",(a0)
                        beq .IssueNODouble
                        cmp.b #"M",(a0)
                        beq .IssueNODouble
                        bra .Parameter_Error
.Parameter_Parsed

                        tst.l smail_Boxmode(a6)
                        beq .No_Boxmode_Init
                        tst.l (a0)
                        bne .Dont_Skip_Bull
                        move.l smail_ThisQSO(a6),a0
                        lea qso_Call(a0),a0
                        GOQSO TestPrivateMail
                        tst.l d0
                        beq .Dont_Skip_Bull
                        bra .IssueNO
.Dont_Skip_Bull         tst.l smail_Bid(a6)
                        beq .No_BID_Test
                        move.l smail_Bid(a6),a0
.TestBidIlli            tst.b (a0)
                        beq .BIDLegal
                        move.b (a0)+,d0
                        cmp.b #":",d0
                        beq .IssueNO
                        cmp.b #"/",d0
                        beq .IssueNO
                        bra .TestBidIlli
.BIDLegal
                        move.l smail_Bid(a6),a0
                        GOQSO SearchMailBid
                        tst.l d0
                        beq .IssueYes
.IssueNODouble          lea IssueNO_Double,a0
                        bra .IssueNO

.No_BID_Test            move.l smail_ThisQSO(a6),a0
                        lea qso_StationID(a0),a0
                        cmp.b #-1,sid_DollarVersion(a0)
                        beq .IssueYes
                        tst.l smail_Personal(a6)
                        beq .IssueNOBid
                        cmp.l #-1,sid_MVersion(a0)
                        beq .IssueYes
                        bra .IssueNOBid
.IssueNOBid             lea IssueNO_BID,a0
.IssueNO                move.l smail_Channel(a6),d0
                        moveq #0,d1
                        moveq #0,d2
                        GOQSO TransmitText
                        bra .AbortMe
.IssueYes               lea IssueYes,a0
                        move.l smail_Channel(a6),d0
                        moveq #0,d1
                        moveq #0,d2
                        GOQSO TransmitText
                        move.l smail_Channel(a6),d0
                        GOQSO ForceTransmit
                        bra .Boxmode_Init
.No_Boxmode_Init        move.l smail_FromCall(a6),a0
                        move.l smail_ToCall(a6),a1
                        move.l smail_AtCall(a6),a2
                        move.l smail_Personal(a6),d0
                        move.l smail_Bid(a6),d1
                        GOQSO SendMail
                        tst.l d0
                        beq .Cannot_SendMail
                        move.l d0,smail_MailHandle(a6)
                        sub.l #128,a7
                        moveq #12,d0
                        bsr Get_Text
                        move.l d0,a0
                        move.l a7,a4
.Copy_Routing_1         move.b (a0)+,(a4)+
                        tst.b -1(a0)
                        bne .Copy_Routing_1
                        subq.l #1,a4
                        move.l smail_MailHandle(A6),a0
                        GOQSO GetMailTo
                        tst.l d0
                        beq .NoIssue_Routing
                        move.l d0,a0
                        tst.b (a0)
                        beq .NoIssue_Routing
.Copy_Routing_2         move.b (a0)+,(a4)+
                        tst.b -1(a0)
                        bne .Copy_Routing_2
                        subq.l #1,a4
                        move.l smail_MailHandle(a6),a0
                        GOQSO GetMailAt
                        tst.l d0
                        beq .NoIssue_Routing
                        move.l d0,a0
                        tst.b (a0)
                        beq .NoIssue_Routing
                        move.b #" ",(a4)+
                        move.b #"@",(a4)+
                        move.b #" ",(a4)+
.Copy_Routing_3         move.b (a0)+,(a4)+
                        tst.b -1(a0)
                        bne .Copy_Routing_3
                        subq.l #1,a4
                        move.l smail_MailHandle(a6),a0
                        GOQSO GetMailHRoute
                        tst.l d0
                        beq .Issue_Routing
                        move.l d0,a0
                        tst.b (a0)
                        beq .Issue_Routing
                        move.b #".",(a4)+
.Copy_Routing_4         move.b (a0)+,(a4)+
                        tst.b -1(a0)
                        bne .Copy_Routing_4
                        subq.l #1,a4
.Issue_Routing          move.b #$0d,(a4)+
                        clr.b (a4)
                        move.l a7,a0
                        move.l smail_Channel(a6),d0
                        moveq #0,d1
                        moveq #0,d2
                        GOQSO TransmitText
.NoIssue_Routing        add.l #128,a7

                        moveq #2,d0
                        bsr Get_Text
                        move.l d0,a0
                        move.l smail_Channel(a6),d0
                        moveq #1,d1
                        moveq #0,d2
                        GOQSO TransmitText
                        bra .Server_Success
.Boxmode_Init           move.l smail_FromCall(a6),a0
                        move.l smail_ToCall(a6),a1
                        move.l smail_AtCall(a6),a2
                        move.l smail_Personal(a6),d0
                        move.l smail_Bid(a6),d1
                        GOQSO SendMail
                        tst.l d0
                        beq .Cannot_SendMail_BB
                        move.l d0,smail_MailHandle(a6)
                        move.l smail_ThisQSO(a6),a0
                        lea qso_Call(a0),a1
                        move.l d0,a0
                        GOQSO SetNotForward
.Server_Success         bsr SMAIL_ReceivePacket
                        movem.l (sp)+,a6/a2-a4/d5-d7
                        moveq #0,d0
                        rts


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

.No_User_Possible       moveq #1,d0
                        bra .Abort_With_Text

.Error_InBatch          move.l smail_BatchBase(a6),a0
                        move.l #1,28(a0)
                        bra .Exit_InBatch
                        
.Cannot_SendMail                moveq #1,d0
                        bra .Abort_With_Text
.Cannot_SendMail_BB     tst.l smail_BatchMode(a6)
                        bne .Error_InBatch
                        bsr SMAIL_BulkError
                        bra .AbortMe
.Invalid_Parameters     tst.l smail_Boxmode(a6)
                        beq .Standard_Error
                        bsr SMAIL_BulkError
.Exit_InBatch           movem.l (sp)+,a6/a2-a4/d5-d7
                        rts

.Standard_Error         moveq #0,d0
.Abort_With_Text        bsr Get_Text
                        move.l d0,a0
                        move.l smail_Channel(a6),d0
                        moveq #1,d1
                        moveq #0,d2
                        GOQSO TransmitText
.AbortMe                bsr SMAIL_Cleanup
                        movem.l (Sp)+,a6/a2-a4/d5-d7
                        moveq #0,d0
                        rts

SMAIL_BulkError         lea .Error(pc),a0
                        moveq #9,d0
                        move.l smail_Channel(a6),d1
                        GOQSO TransmitData
                        move.l smail_Channel(a6),d0
                        GOQSO ForceTransmit
                        move.l #"DDDD",-(sp)
                        move.l sp,a0
                        moveq #1,d0
                        move.l smail_Channel(a6),d1
                        GOQSO TransmitCommand
                        move.l (sp)+,d0
                        rts
.Error                  dc.b "***error",$0d
                        even

SMAIL_Cleanup           tst.l smail_MailHandle(a6)
                        beq .No_MailOpen
                        move.l smail_MailHandle(a6),a0
                        GOQSO MarkAsIncomp
                        move.l smail_MailHandle(a6),a0
                        clr.l smail_MailHandle(a6)
                        GOQSO CloseMail                 
.No_MailOpen            move.l smail_OldServer(a6),d0
                        move.l smail_ThisQSO(a6),a0
                        move.l d0,qso_Server(a0)
                        move.l a6,a1
                        move.l ppb_ExecBase(a5),a6
                        sub.l #SMAIL_Functions-SMAIL_FunctionsBegin,a1
                        jsr _LVOFreeVec(A6)
                        moveq #0,d0
                        rts

SMAIL_ConnectProced     rts

SMAIL_ReceivePacket     movem.l d2-d7/a2-a6,-(sp)
.Redo_Receive           tst.l smail_Status(a6)
                        beq .Enter_Title
                        cmp.l #4,smail_Status(a6)
                        beq .Enter_Title
                        cmp.l #1,smail_Status(a6)
                        beq .In_Mail
                        cmp.l #2,smail_Status(a6)
                        beq .In_Bin
                        bra .Exit
        
.Enter_Title            bsr .Get_Line
                        tst.l d0
                        bne .Exit
                        lea smail_LineBuffer(a6),a0
.Enter_Title_CutIt      tst.b (a0)
                        beq .Enter_Title_Cutted
                        cmp.b #$d,(a0)
                        beq .Enter_Title_Cutted
                        addq.l #1,a0
                        bra .Enter_Title_CutIt
.Enter_Title_Cutted     clr.b (a0)
                        lea smail_LineBuffer(a6),a1
                        tst.l smail_Boxmode(a6)
                        bne .Accept_EmptyTitle
                        tst.b (a1)
                        beq .Leave_Serve_UC
.Accept_EmptyTitle      move.l smail_MailHandle(a6),a0
                        GOQSO SetMailTitle
                        move.l #1,smail_Status(a6)
                        tst.l smail_Boxmode(a6)
                        bne .Redo_Receive
                        moveq #3,d0
                        bsr Get_Text
                        move.l d0,a0
                        move.l smail_Channel(a6),d0
                        moveq #1,d1
                        moveq #0,d2
                        GOQSO TransmitText
                        bra .Redo_Receive
.Cannot_SendMail_FWD    bra .MailWrite_Error
                        



.In_Bin                 tst.l smail_RemainBytes(a6)
                        beq .Deep_InMail_EndMail
                        move.l smail_ThisQSO(a6),a0
                        GOQSO TestServBuffer
                        tst.l d0
                        beq .Exit
                        cmp.l #256,d0
                        ble .In_Bin_LittleCh
                        move.l #256,d0
.In_Bin_LittleCh        cmp.l smail_RemainBytes(A6),d0
                        ble .In_Bin_MidCh
                        move.l smail_RemainBytes(a6),d0
.In_Bin_MidCh           move.l d0,d7
                        move.l smail_ThisQSO(A6),a1
                        lea smail_LineBuffer(a6),a0
                        GOQSO ReadServBuffer
                        tst.l d0
                        beq .Exit
                        sub.l d0,smail_RemainBytes(a6)
                        lea smail_LineBuffer(a6),a1
                        move.l smail_MailHandle(a6),a0
                        GOQSO WriteMail
                        tst.l d0
                        beq .Redo_Receive
                        tst.l smail_BatchMode(a6)
                        bne .Write_BatchError
                        move.l smail_Channel(a6),d0
                        GOQSO ForceTransmit
                        lea AB_AbortString,a0
                        move.l smail_Channel(a6),d0
                        moveq #1,d1
                        moveq #0,d2
                        GOQSO TransmitText
                        move.l smail_Channel(a6),d0
                        GOQSO ForceTransmit
                        bra .MailWrite_Error                    
.Write_BatchError       move.l smail_BatchBase(a6),a0
                        move.l #1,(a0)
                        bra .MailWrite_Error
                        
.In_Mail                bsr .Get_Line
                        tst.l d0
                        bne .Exit
                        lea smail_LineBuffer(a6),a0
                        cmp.b #"#",(a0)
                        bne .Standard_Line
                        cmp.b #"B",1(a0)
                        bne .Standard_Line
                        cmp.b #"I",2(a0)
                        bne .Standard_Line
                        cmp.b #"N",3(a0)
                        bne .Standard_Line
                        cmp.b #"#",4(a0)
                        bne .Standard_Line
                        move.l #2,smail_Status(a6)
                        move.l a0,a1
                        moveq #-1,d0
.In_FrontBinGetS        addq.l #1,d0
                        tst.b (a1)+
                        bne .In_FrontBinGetS
                        move.l d0,smail_BinHeadSize(a6)
                        move.l d0,d7
                        lea smail_LineBuffer(a6),a4
                        addq.l #5,a4
                        subq.l #6,d7
                        tst.l d7
                        beq .MailWrite_Error_AB
                        moveq #0,d6
.Get_FileSize           tst.l d7
                        beq .Get_FileSize_OK
                        cmp.b #"#",(a4)
                        beq .Get_FileSize_OK
                        move.l d6,d0
                        move.l a6,-(sp)
                        move.l ppb_UtilityBase(a5),a6
                        moveq #10,d1
                        jsr _LVOUMult32(a6)
                        move.l (sp)+,a6
                        move.l d0,d6
                        move.b (a4)+,d0
                        subq.l #1,d7
                        cmp.b #"0",d0
                        blt .MailWrite_Error_AB
                        cmp.b #"9",d0
                        bgt .MailWrite_Error_AB
                        and.l #$f,d0
                        add.l d0,d6
                        bra .Get_FileSize
.MailWrite_Error_AB     tst.l smail_Boxmode(a6)
                        bne .BBS_GeneralError
                        tst.l smail_BatchMode(a6)
                        bne .Write_BatchError
                        move.l smail_Channel(a6),d0
                        GOQSO ForceTransmit
                        lea AB_NoString,a0
                        move.l smail_Channel(a6),d0
                        moveq #1,d1
                        moveq #0,d2
                        GOQSO TransmitText
                        move.l smail_Channel(a6),d0
                        GOQSO ForceTransmit
                        bra .MailWrite_Error                    
                        
.Get_FileSize_OK        move.l d6,smail_RemainBytes(a6)
                        move.l smail_BinHeadSize(a6),d0
                        lea smail_LineBuffer(a6),a1
                        move.l smail_MailHandle(a6),a0
                        GOQSO WriteMail
                        tst.l d0
                        bne .MailWrite_Error_AB

                        tst.l smail_Boxmode(a6)
                        bne .Redo_Receive
                        move.l smail_Channel(a6),d0
                        GOQSO ForceTransmit
                        lea AB_OkString,a0
                        move.l smail_Channel(a6),d0
                        moveq #1,d1
                        moveq #0,d2
                        GOQSO TransmitText
                        move.l smail_Channel(a6),d0
                        GOQSO ForceTransmit
                        bra .Redo_Receive
.Standard_Line          lea smail_LineBuffer(a6),a0
                        cmp.l #" go_",(a0)
                        beq .7plus_line
                        cmp.l #" sto",(a0)
                        bne .No_7plus_line
                        cmp.w #"p_",4(a0)
                        bne .No_7plus_line
.7plus_line             move.l smail_MailHandle(a6),a0
                        GOQSO MarkAs7Plus
.No_7plus_line
                        lea smail_LineBuffer(a6),a0
                        cmp.b #26,(a0)
                        bne .Deep_IMNCZ
                        cmp.b #$d,1(a0)
                        beq .Deep_InMail_EndMail
.Deep_IMNCZ             tst.l smail_Boxmode(a6)
                        bne .Deep_IMNEX
                        cmp.b #"N",(a0)
                        bne .Deep_IMN4N
                        cmp.b #"N",1(a0)
                        bne .Deep_IMN4N
                        cmp.b #"N",2(a0)
                        bne .Deep_IMN4N
                        cmp.b #"N",3(a0)
                        bne .Deep_IMN4N
                        cmp.b #$0d,4(a0)
                        bne .Deep_IMN4N
                        bra .Deep_InMail_EndMail
.Deep_IMN4N
                        cmp.b #"/",(a0)
                        bne .Deep_IMNEX
                        cmp.b #"E",1(a0)
                        beq .Deep_IMEXP1
                        cmp.b #"e",1(a0)
                        bne .Deep_IMNEX
.Deep_IMEXP1            cmp.b #"X",2(a0)
                        beq .Deep_IMEXP2
                        cmp.b #"x",2(a0)
                        bne .Deep_IMNEX
.Deep_IMEXP2            cmp.b #$d,3(a0)
                        beq .Deep_InMail_EndMail
.Deep_IMNEX             lea smail_LineBuffer(A6),a0
                        cmp.l #"/ACK",(a0)
                        beq .Is_Aki
                        cmp.l #"/ack",(a0)
                        bne .Not_Aki
.Is_Aki                 cmp.b #$0d,4(a0)
                        bne .Not_Aki
                        move.l smail_MailHandle(a6),a0
                        GOQSO MarkAsWantAck
.Not_Aki                lea smail_LineBuffer(a6),a0
                        move.l a0,a1
                        moveq #-1,d0
.In_DeepMailGetS        addq.l #1,d0
                        tst.b (a1)+
                        bne .In_DeepMailGetS
                        move.l a0,a1
                        move.l smail_MailHandle(a6),a0
                        GOQSO WriteMail
                        tst.l d0
                        bne .MailWrite_Error
                        bra .Redo_Receive

.Deep_InMail_EndMail    tst.l smail_Boxmode(a6)
                        bne .Leave_Serve

                        move.l smail_MailHandle(a6),a0
                        GOQSO GetMailBytes
                        move.l d0,-(sp)
                        move.l smail_MailHandle(a6),a0
                        GOQSO GetMailBid
                        move.l d0,-(sp)
                        move.l smail_MailHandle(a6),a0
                        GOQSO GetNumberMail
                        move.l d0,-(sp)

                        moveq #13,d0
                        bsr Get_Text
                        move.l d0,a0
                        move.l a7,a1
                        move.l smail_Channel(a6),d0
                        moveq #0,d1
                        GOQSO OutputFormat
                        add.l #12,a7
                        move.l smail_MailHandle(a6),a0
                        lea smail_LineBuffer(a6),a1
                        GOQSO GetForwardDest
                        tst.l d0
                        beq .No_Forwarding
                        moveq #14,d0
                        bsr Get_Text
                        move.l d0,a0
                        sub.l a1,a1
                        move.l smail_Channel(a6),d0
                        moveq #0,d1
                        GOQSO OutputFormat
                        lea smail_LineBuffer(a6),a0
                        sub.l a1,a1
                        move.l smail_Channel(a6),d0
                        moveq #0,d1
                        GOQSO OutputFormat
.No_Forwarding          bra .Leave_Serve

.MailWrite_Error        tst.l smail_Boxmode(a6)
                        bne .BBS_GeneralError
                        moveq #4,d0
                        bsr Get_Text
                        move.l d0,a0
                        move.l smail_Channel(a6),d0
                        moveq #1,d1
                        moveq #0,d2
                        GOQSO TransmitText
                        bra .Leave_Serve_UC
.BBS_GeneralError       bsr SMAIL_BulkError
                        bra .Exit

.Exit                   movem.l (Sp)+,d2-d7/a2-a6
                        rts
.Get_Line               move.l smail_ThisQSO(a6),a0
                        GOQSO TestServBufferLin
                        tst.l d0
                        beq .Get_Line_No
                        move.l d0,d1
                        add.l smail_InLine(a6),d1
                        lea smail_LineBuffer(a6),a0
                        add.l d1,a0
                        clr.b (a0)
                        lea smail_LineBuffer(a6),a0
                        add.l smail_InLine(a6),a0
                        move.l smail_ThisQSO(a6),a1
                        GOQSO ReadServBuffer
                        add.l d0,smail_InLine(a6)
                        tst.l smail_InLine(a6)
                        beq .Get_Line_No
                        cmp.l #256,smail_InLine(a6)
                        beq .Get_Line_Yes
                        move.l smail_InLine(a6),d0
                        lea smail_LineBuffer(a6),a1
                        subq.l #1,d0
                        add.l d0,a1
                        cmp.b #$d,(a1)
                        bne .Get_Line_No
.Get_Line_Yes           clr.l smail_InLine(a6)
                        moveq #0,d0
                        rts                     
.Get_Line_No            moveq #1,d0
                        rts
.Leave_Serve            move.l smail_MailHandle(a6),a0
                        clr.l smail_MailHandle(a6)
                        GOQSO CloseMail
.Leave_Serve_UC         movem.l (sp)+,d2-d7/a2-a6
                        move.l smail_ThisQSO(a6),a0
                        GOQSO TestServBuffer
                        move.l d0,-(sp)
                        move.l smail_OldServer(a6),-(sp)
                        bsr SMAIL_Cleanup
                        move.l (sp)+,a6
                        move.l (Sp)+,d0
                        cmpa.l #0,a6
                        beq .Exit_2
                        jsr _SVOReceivePacket(A6)
.Exit_2                 rts

SMAIL_TerminalInput     rts     

SMAIL_AbortServer       move.l smail_ThisQSO(a6),a0
                        GOQSO TestServBuffer
                        move.l d0,-(sp)
                        move.l smail_OldServer(a6),-(sp)
                        bsr SMAIL_Cleanup
                        move.l (sp)+,a6
                        move.l (Sp)+,d0
                        cmpa.l #0,a6
                        beq .Exit_2
                        jsr _SVOReceivePacket(A6)
.Exit_2                 rts


SMAIL_GetServerType     move.l #512,d0
                        rts

SMAIL_Nop                       rts

                        
SMAIL_FunctionsBegin    
                        AJUMP GEN_AskAudioPass                  ;-54
                        AJUMP SMAIL_AbortServer                 ;-48
                        AJUMP SMAIL_GetServerType               ;-42
                        AJUMP SMAIL_TerminalInput               ;-36
                        AJUMP SMAIL_ReceivePacket               ;-30
                        AJUMP SMAIL_Nop                         ;-24
                        AJUMP SMAIL_ConnectProced               ;-18
                        AJUMP SMAIL_Cleanup                     ;-12
                        AJUMP SMAIL_Init                        ;-6
SMAIL_Functions         
                        
                        even
                        
                        
                        rsreset
smail_ThisQSO           rs.l 1
smail_Channel           rs.l 1
smail_OldServer         rs.l 1
smail_QSOBase           rs.l 1
smail_Parameter         rs.l 1
smail_FromCall          rs.l 1
smail_ToCall            rs.l 1
smail_AtCall            rs.l 1
smail_Personal          rs.l 1
smail_Bid               rs.l 1
smail_MailHandle        rs.l 1
smail_Status            rs.l 1          ;0=Enter Title 1=In Mail 2=In Bin 3=Front
smail_BinHeadSize       rs.l 1
smail_RemainBytes       rs.l 1
smail_InLine            rs.l 1
smail_MailMode          rs.l 1          ;0=//SM 1=SP 2=SB
smail_Boxmode           rs.l 1
smail_BatchMode         rs.l 1
smail_BatchBase         rs.l 1          ;0  Personal
                                        ;4  *Fm
                                        ;8  *At
                                        ;12 *To
                                        ;16 *BID
                                        ;20 Bytes
                                        ;24 Lifetime
                                        ;28 ReportCode
smail_LineBuffer        rs.b 260
smail_Size              rs.b 0



RMAIL_Head              dc.l RMAIL_Name
                        dc.l 1
                        dc.l RMAIL_Init_Chat
                        dc.l RMAIL_Head_2       ;Next
                        dc.l 512                ;Subtype (9=Mailing)
RMAIL_Head_2            dc.l RMAIL_Name_2
                        dc.l 1
                        dc.l RMAIL_Init_R
                        dc.l RMAIL_Head_3       ;Next
                        dc.l 512                ;Subtype (9=Mailing)
RMAIL_Head_3            dc.l RMAIL_Name_3
                        dc.l 1
                        dc.l RMAIL_Init_RM
                        dc.l RMAIL_Head_4       ;Next
                        dc.l 512                ;Subtype (9=Mailing)
RMAIL_Head_4            dc.l RMAIL_Name_4
                        dc.l 1
                        dc.l RMAIL_Init_RN
                        dc.l RMAIL_Head_5       ;Next
                        dc.l 512                ;Subtype (9=Mailing)
RMAIL_Head_5            dc.l RMAIL_Name_5
                        dc.l 1
                        dc.l RMAIL_Init_RFm
                        dc.l RMAIL_Head_6       ;Next
                        dc.l 512                ;Subtype (9=Mailing)
RMAIL_Head_6            dc.l RMAIL_Name_6
                        dc.l 1
                        dc.l RMAIL_Init_RTo
                        dc.l KMAIL_Head         ;Next
                        dc.l 512                ;Subtype (9=Mailing)
                        
RMAIL_Name              dc.b "RM",$01,"AIL",0
RMAIL_Name_2            dc.b "bbs_R",0
RMAIL_Name_3            dc.b "bbs_RM",0
RMAIL_Name_4            dc.b "bbs_RN",0
RMAIL_Name_5            dc.b "bbs_R<",0
RMAIL_Name_6            dc.b "bbs_R>",0
                        even
                        
RMAIL_Init_Chat         move.l #-1,d1
                        bra RMAIL_Init_Gen
RMAIL_Init_R            moveq #2,d1
                        bra RMAIL_Init_Gen
RMAIL_Init_RM           moveq #1,d1
                        bra RMAIL_Init_Gen
RMAIL_Init_RN           moveq #0,d1
                        bra RMAIL_Init_Gen
RMAIL_Init_RFm          moveq #4,d1
                        bra RMAIL_Init_Gen
RMAIL_Init_RTo          moveq #5,d1
                        bra RMAIL_Init_Gen
                                                
RMAIL_Init_Gen          movem.l a6/a2-a4/d5-d7,-(sp)
                        move.l a0,a4
                        move.l a1,d7
                        move.l d0,d6
                        move.l a3,a6
                        move.l #RMAIL_Functions-RMAIL_FunctionsBegin,d0
                        move.l d1,-(sp)
                        move.l #rmail_Size,d1
                        lea RMAIL_Functions,a0
                        move.l ppb_Global(a6),a5
                        jsr _QVOCreateServerBase(A6)
                        move.l (sp)+,d1
                        tst.l d0
                        beq .Cannot_Serve
                        move.l d0,a6
                        move.l a3,a5
                        move.l d1,rmail_ListMode(a6)
                        move.l a3,rmail_QSOBase(a6)
                        move.l d7,rmail_OldServer(A6)
                        move.l d6,rmail_Channel(a6)
                        move.l a2,rmail_Parameter(a6)
                        move.l a4,rmail_ThisQSO(a6)
                        move.l a6,qso_Server(a4)
                        clr.l rmail_MailHandle(a6)
                        move.l #1,rmail_InHeader(a6)
                        clr.l rmail_ChatMode(a6)

                        move.l a2,a0
                        GOQSO ParseComLine
                        tst.l d0
                        blt .Invalid_Parameters
                        move.l d0,d7

                        move.l rmail_ListMode(a6),d0
                        cmp.l #-1,d0
                        bne .RLine_NoChat
                        move.l #1,rmail_ChatMode(a6)
                        tst.l d7
                        bne .RLine_ChatPar
                        clr.l rmail_ListMode(a6)
                        bra .First_POK
                        
.RLine_NoChat           tst.l d0
                        beq .First_POKNO
                        cmp.l #1,d0
                        beq .First_POKNO
                        cmp.l #2,d0
                        beq .RLine_ChatPar
                        cmp.l #3,d0
                        beq .RLine_ChatPar
                        cmp.l #4,d0
                        beq .RLine_FmPar
                        cmp.l #5,d0
                        beq .RLine_ToPar
                        bra .Invalid_Parameters
.First_POKNO            tst.l d7
                        bne .Invalid_Parameters
                        bra .First_POK
.RLine_ChatPar          tst.l d7
                        beq .Invalid_Parameters
                        cmp.l #1,d7
                        bgt .CC_RVM
                        move.l #2,rmail_ListMode(a6)
                        lea rmail_LowerMail(a6),a0
                        move.l rmail_Parameter(a6),a1
                        move.l d7,d0
                        bsr MPAR_Area
                        tst.l d0
                        bne .CC_RVM
                        bra .First_POK
.CC_RVM         move.l #3,rmail_ListMode(a6)
                        lea rmail_MailList(a6),a0
                        move.l rmail_Parameter(a6),a1
                        move.l d7,d0
                        bsr MPAR_Multi
                        tst.l d0
                        bne .Invalid_Parameters
                        bra .First_POK
.RLine_FmPar            move.l #4,rmail_ListMode(a6)
                        lea rmail_ListCall(a6),a0
                        move.l rmail_Parameter(A6),a1
                        move.l d7,d0
                        bsr MPAR_Call
                        tst.l d0
                        bne .Invalid_Parameters
                        bra .First_POK
.RLine_ToPar            move.l #5,rmail_ListMode(a6)
                        lea rmail_ListCall(a6),a0
                        move.l rmail_Parameter(A6),a1
                        move.l d7,d0
                        bsr MPAR_Call
                        tst.l d0
                        bne .Invalid_Parameters
.First_POK              
                
                
                        clr.l rmail_ListProgress(a6)
                                                        
.Server_Success         bsr RMAIL_DoPoll
                        movem.l (sp)+,a6/a2-a4/d5-d7
                        moveq #0,d0
                        rts


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

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

RMAIL_Cleanup           tst.l rmail_MailHandle(a6)
                        beq .No_MailOpen
                        move.l rmail_MailHandle(a6),a0
                        clr.l rmail_MailHandle(a6)
                        GOQSO CloseMail
.No_MailOpen            move.l rmail_OldServer(a6),d0
                        move.l rmail_ThisQSO(a6),a0
                        move.l d0,qso_Server(a0)
                        move.l a6,a1
                        move.l ppb_ExecBase(a5),a6
                        sub.l #RMAIL_Functions-RMAIL_FunctionsBegin,a1
                        jsr _LVOFreeVec(A6)
                        moveq #0,d0
                        rts

RMAIL_ConnectProced     rts

RMAIL_IssueCR           move.l #$0d0d0d0d,-(a7)
                        move.l a7,a0
                        moveq #1,d0
                        move.l rmail_Channel(A6),d1
                        GOQSO TransmitData
                        move.l (sp)+,d0
                        rts


RMAIL_ReceivePacket     movem.l d2-d7/a2-a6,-(sp)
.Loop                   move.l rmail_ThisQSO(a6),a0
                        GOQSO TestServBufferLin
                        tst.l d0
                        beq .Exit
                        move.l d0,d1
                        add.l rmail_InLine(a6),d1
                        cmp.l #1024,d1
                        ble .Good_Line
                        move.l #1024,d1
.Good_Line              lea rmail_LineBuffer2(a6),a0
                        add.l rmail_InLine(a6),d0
                        move.l rmail_ThisQSO(A6),a1
                        GOQSO ReadServBuffer
                        add.l d0,rmail_InLine(a6)
                        move.l rmail_InLine(a6),d0
                        tst.l d0
                        beq .Exit
                        lea rmail_LineBuffer2(a6),a0
                        subq.l #1,d0
                        add.l d0,a0
                        cmp.b #$0d,(a0)
                        bne .Exit
                        lea rmail_LineBuffer2(a6),a0
                        cmp.l #2,rmail_WaitBinStatus(a6)
                        blt .No_Automessages
                        cmp.b #"#",(a0)
                        bne .No_Automessages
                        cmp.l #4,d0
                        blt .No_Automessages
                        cmp.l #"#OK#",(a0)
                        beq .Autobin_OK
                        cmp.l #"#NO#",(a0)
                        beq .Autobin_NO
                        cmp.l #7,d0
                        blt .No_Automessages
                        cmp.l #"#ABO",(a0)
                        bne .No_Automessages
                        cmp.w #"RT",4(a0)
                        bne .No_Automessages
                        cmp.b #"#",6(a0)
                        bne .No_Automessages
.Autobin_NO             move.l #1,rmail_ForceEnd(a6)
                        bsr RMAIL_DoPoll
                        bra .Exit
.Autobin_OK             move.l #3,rmail_WaitBinStatus(A6)
                        bsr RMAIL_DoPoll
                        clr.l rmail_InLine(a6)
                        bra .Loop
.No_Automessages        tst.l rmail_ChatMode(a6)
                        beq .No_Output
                        move.l rmail_InLine(a6),d0
                        move.l rmail_Channel(a6),d1
                        moveq #3,d2
                        lea rmail_LineBuffer2(a6),a0
                        GOQSO WriteTerminal
.No_Output              tst.l rmail_WaitBinStatus(a6)
                        bne .No_TextAborter
                        cmp.l #2,rmail_InLine(a6)
                        blt .No_TextAborter
                        lea rmail_LineBuffer2(a6),a0
                        move.b (a0),d0
                        and.b #$df,d0
                        cmp.b #$41,d0
                        bne .No_TextAborter
                        cmp.b #$0d,1(a0)
                        bne .No_TextAborter
                        moveq #19,d0
                        bsr Get_Text
                        move.l d0,a0
                        move.l rmail_Channel(a6),d0
                        moveq #1,d1
                        moveq #0,d2
                        GOQSO TransmitText
                        bsr RMAIL_AbortServer
                        bra .Exit
.No_TextAborter         clr.l rmail_InLine(a6)
                        bra .Loop
.Exit                   movem.l (Sp)+,d2-d7/a2-a6
                        rts










RMAIL_TerminalInput     rts     

RMAIL_DoPoll            movem.l d2-d7/a2-a6,-(sp)
.Do_RePoll              moveq #0,d5
                        move.l rmail_Channel(a6),d0
                        GOQSO GetOutstanding
                        cmp.l #5,d0
                        bge .Exit
                        
                        tst.l rmail_MailHandle(a6)
                        bne .InMail
                        tst.l rmail_ListMode(a6)
                        beq .Do_RList
                        cmp.l #1,rmail_ListMode(A6)
                        beq .Do_RList
                        cmp.l #2,rmail_ListMode(a6)
                        beq .Do_RArea                   
                        cmp.l #3,rmail_ListMode(a6)
                        beq .Do_RMList
                        cmp.l #4,rmail_ListMode(a6)
                        beq .Do_FmList
                        cmp.l #5,rmail_ListMode(a6)
                        beq .Do_ToList
                        bra .Terminate_Server

.Do_RMList              cmp.l #6,rmail_ListProgress(a6)
                        beq .Terminate_Server
                        move.l rmail_ListProgress(a6),d0
                        lea rmail_MailList(a6),a0
                        move.l d0,d1
                        asl.l #2,d1
                        addq.l #1,d0
                        move.l d0,rmail_ListProgress(a6)
                        add.l d1,a0
                        move.l (a0),d0
                        tst.l d0
                        beq .Terminate_Server
                        bra .Open_NewMail

.Do_RArea               sub.l #20,a7
                                move.l rmail_ThisQSO(a6),a0
                                lea qso_Call(a0),a0
                                move.l (a0),(a7)
                                move.l 4(a0),4(a7)
                                move.l 8(a0),8(a7)
                                move.l 12(a0),12(a7)
                                lea rmail_LowerMail(a6),a1
                                move.l a1,16(a7)
                                move.l a7,a0
                                moveq #11,d0
                                move.l rmail_ListProgress(a6),d1
                                GOQSO SearchMail
                                add.l #20,a7
                                tst.l d0
                                beq .Terminate_Server
                                move.l d0,rmail_ListProgress(a6)
                                bra .Open_NewMail
                        
                                                                
.Do_RList               move.l rmail_ListProgress(a6),d1
                        move.l rmail_ThisQSO(a6),a0
                        lea qso_Call(a0),a0
                        moveq #1,d0
                        tst.l rmail_ListMode(a6)
                        bne .Do_OldList
                        moveq #3,d0
.Do_OldList             GOQSO SearchMail
                        tst.l d0
                        beq .Terminate_Server
                        move.l d0,rmail_ListProgress(a6)
                        bra .Open_NewMail
.Do_FmList              move.l rmail_ListProgress(a6),d1
                        move.l rmail_ThisQSO(a6),a0
                        lea qso_Call(a0),a0
                        sub.l #32,a7
                        move.l a7,a1
                        move.l (a0),(a1)
                        move.l 4(a0),4(a1)
                        move.l 8(a0),8(A1)
                        move.l 12(a0),12(a1)
                        lea rmail_ListCall(a6),a0
                        move.l (a0),16(a1)
                        move.l 4(a0),20(a1)
                        move.l 8(a0),24(a1)
                        move.l 12(a0),28(a1)
                        clr.b 31(a1)
                        move.l a1,a0
                        moveq #8,d0
                        GOQSO SearchMail
                        add.l #32,a7
                        tst.l d0
                        beq .Terminate_Server
                        move.l d0,rmail_ListProgress(a6)
                        bra .Open_NewMail
.Do_ToList              move.l rmail_ListProgress(a6),d1
                        move.l rmail_ThisQSO(a6),a0
                        lea qso_Call(a0),a0
                        sub.l #32,a7
                        move.l a7,a1
                        move.l (a0),(a1)
                        move.l 4(a0),4(a1)
                        move.l 8(a0),8(A1)
                        move.l 12(a0),12(a1)
                        lea rmail_ListCall(a6),a0
                        move.l (a0),16(a1)
                        move.l 4(a0),20(a1)
                        move.l 8(a0),24(a1)
                        move.l 12(a0),28(a1)
                        clr.b 31(a1)
                        move.l a1,a0
                        moveq #9,d0
                        GOQSO SearchMail
                        add.l #32,a7
                        tst.l d0
                        beq .Terminate_Server
                        move.l d0,rmail_ListProgress(a6)
                        bra .Open_NewMail

.Open_NewMail           move.l rmail_ThisQSO(a6),a0
                        lea qso_Call(a0),a0
                        GOQSO OpenMail
                        tst.l d0
                        beq .Do_RePoll          ;*** TO DO ERROR NO MAIL
                        cmp.l #-1,d0
                        beq .Do_RePoll          ;*** TO DO ERROR NO ACCESS
                        move.l d0,rmail_MailHandle(a6)

                        bsr RMAIL_IssueCR

                        move.l #1,rmail_InHeader(a6)
                        move.l rmail_MailHandle(a6),a4
                        move.l a4,a0
                        sub.l #7*4,a7
                        GOQSO GetMailFm
                        move.l d0,(a7)
                        move.l a4,a0
                        GOQSO GetMailTo
                        move.l d0,4(a7)
                        move.l a4,a0
                        lea rmail_DateBuffer(a6),a1
                        move.l a1,12(a7)
                        GOQSO GetMailDateTxt
                        move.l a4,a0
                        lea rmail_StatusBuffer(a6),a1
                        move.l a1,8(a7)
                        GOQSO GetMailStatusTxt  
                        move.l a4,a0
                        GOQSO GetMailBid
                        move.l d0,16(a7)
                        move.l a4,a0
                        GOQSO GetNumberMail
                        move.l d0,20(a7)
                        move.l a4,a0
                        GOQSO GetMailTitle
                        move.l d0,24(a7)
                        moveq #5,d0
                        bsr Get_Text
                        move.l d0,a0
                        move.l a7,a1
                        moveq #0,d1
                        move.l rmail_Channel(a6),d0
                        GOQSO OutputFormat
                        add.l #7*4,a7
                        bsr RMAIL_IssueCR
                        move.l rmail_Channel(a6),d0
                        GOQSO ForceTransmit
                        clr.l rmail_WaitBinStatus(a6)
                        clr.l rmail_ForceEnd(a6)
                        move.l rmail_MailHandle(a6),a0
                        GOQSO GetMailStatus
                        btst #30,d0
                        beq .Do_RePoll
                        move.l #1,rmail_WaitBinStatus(a6)                        
                        bra .Do_RePoll
.InMail                 move.l rmail_Channel(a6),d0
                        GOQSO GetPacLen
                        move.l d0,d5
                        tst.l rmail_ForceEnd(a6)
                        bne .InMail_EOF_2
                        cmp.l #3,rmail_WaitBinStatus(a6)
                        beq .InMail_PushAB
                        tst.l rmail_WaitBinStatus(a6)
                        bne .InMail_AutoBin
                        tst.l rmail_InHeader(a6)
                        bne .InMail_SkipHeader
.InMail_PushAB          ;move.l rmail_Channel(a6),d0
                        ;GOQSO ForceTransmit
                        lea rmail_LineBuffer(a6),a1
                        move.l rmail_MailHandle(a6),a0
                        move.l d5,d0
                        GOQSO ReadMail
                        cmp.l #-1,d0
                        beq .Error_Read
                        tst.l d0
                        beq .InMail_EOF
                        move.l d0,d7
                        move.l d7,d0
                        move.l rmail_Channel(a6),d1
                        lea rmail_LineBuffer(a6),a0
                        GOQSO TransmitData
                        ;move.l rmail_Channel(a6),d0
                        ;GOQSO ForceTransmit
                        cmp.l d5,d7
                        bne .InMail_EOF
                        bra .Do_RePoll
.InMail_AutoBin         cmp.l #1,rmail_WaitBinStatus(a6)
                        bne .Exit

.InMail_SkipHeader      move.l rmail_MailHandle(a6),a0
                        move.l d5,d0
                        lea rmail_LineBuffer(a6),a1
                        GOQSO ReadMailLine
                        cmp.l #-1,d0
                        beq .Error_Read
                        tst.l d0
                        beq .InMail_EOF
                        move.l d0,d7
                        
                        tst.l rmail_InHeader(a6)
                        beq .InMail_NoRHead
                        lea rmail_LineBuffer(a6),a0
                        cmp.l #2,d7
                        blt .InMail_NoRHead
                        cmp.w #"R:",(a0)
                        beq .InMail_SkipHeader
                        clr.l rmail_InHeader(a6)
                        move.l d7,d0
                        move.l rmail_Channel(a6),d1
                        lea rmail_LineBuffer(a6),a0
                        GOQSO TransmitData
                        bra .Do_RePoll
.InMail_NoRHead         lea rmail_LineBuffer(a6),a0
                        cmp.l #5,d7
                        blt .InMail_NAB
                        cmp.l #"#BIN",(a0)
                        bne .InMail_NAB
                        cmp.b #"#",4(a0)
                        bne .InMail_NAB
                        bra .InMail_YAB
.InMail_NAB             move.l d7,d0
                        move.l rmail_Channel(a6),d1
                        lea rmail_LineBuffer(a6),a0
                        GOQSO TransmitData
                        bra .Do_RePoll
.InMail_YAB             move.l rmail_Channel(a6),d0
                        GOQSO ForceTransmit
                        move.l d7,d0
                        move.l rmail_Channel(a6),d1
                        lea rmail_LineBuffer(a6),a0
                        GOQSO TransmitData
                        move.l rmail_Channel(a6),d0
                        GOQSO ForceTransmit
                        move.l #2,rmail_WaitBinStatus(a6)
                        bra .Exit
.InMail_EOF             move.l rmail_MailHandle(a6),a0
                        GOQSO MarkAsRead
.InMail_EOF_2

                        move.l rmail_MailHandle(a6),a4
                        sub.l #5*4,a7
                        move.l a4,a0
                        GOQSO GetNumberMail
                        move.l d0,(A7)
                        move.l a4,a0
                        GOQSO GetMailTo
                        move.l d0,4(a7)
                        move.l a4,a0
                        GOQSO GetMailAt
                        move.l d0,8(a7)
                        move.l a4,a0
                        GOQSO GetMailFm
                        move.l d0,12(A7)
                        move.l a4,a0
                        GOQSO GetMailBid
                        move.l d0,16(a7)
                        moveq #6,d0
                        bsr Get_Text
                        move.l d0,a0
                        move.l a7,a1
                        move.l rmail_Channel(a6),d0
                        moveq #0,d1
                        GOQSO OutputFormat
                        add.l #5*4,a7
                        move.l rmail_MailHandle(a6),a0
                        clr.l rmail_MailHandle(a6)
                        GOQSO CloseMail
                        bra .Do_RePoll
                        

.Error_Read             ;*** TO DO DISPLAY ERROR
.Terminate_Server       move.l rmail_ThisQSO(a6),a0
                        GOQSO TestServBuffer
                        move.l d0,-(sp)
                        move.l rmail_OldServer(a6),-(sp)
                        bsr RMAIL_Cleanup
                        move.l (sp)+,a6
                        move.l (Sp)+,d0
                        cmpa.l #0,a6
                        beq .Exit
                        jsr _SVOReceivePacket(A6)
.Exit                   movem.l (sp)+,d2-d7/a2-a6
                        rts
                

RMAIL_AbortServer       move.l rmail_ThisQSO(a6),a0
                        GOQSO TestServBuffer
                        move.l d0,-(sp)
                        move.l rmail_OldServer(a6),-(sp)
                        bsr RMAIL_Cleanup
                        move.l (sp)+,a6
                        move.l (Sp)+,d0
                        cmpa.l #0,a6
                        beq .Exit_2
                        jsr _SVOReceivePacket(A6)
.Exit_2                 rts


RMAIL_GetServerType     move.l #512,d0
                        rts

RMAIL_Nop               rts

                        
RMAIL_FunctionsBegin    
                        AJUMP GEN_AskAudioPass                  ;-54
                        AJUMP RMAIL_AbortServer                 ;-48
                        AJUMP RMAIL_GetServerType               ;-42
                        AJUMP RMAIL_TerminalInput               ;-36
                        AJUMP RMAIL_ReceivePacket               ;-30
                        AJUMP RMAIL_DoPoll                      ;-24
                        AJUMP RMAIL_ConnectProced               ;-18
                        AJUMP RMAIL_Cleanup                     ;-12
                        AJUMP RMAIL_Init_Chat                   ;-6
RMAIL_Functions         
                        
                        even
                        
                        
                        rsreset
rmail_ThisQSO           rs.l 1
rmail_Channel           rs.l 1
rmail_OldServer         rs.l 1
rmail_QSOBase           rs.l 1
rmail_Parameter         rs.l 1
rmail_MailHandle        rs.l 1
rmail_ListMode          rs.l 1          ;(0=RN 1=RM 2=Rx-y 3=R n n n n n n n
                                        ; 4=R< 5=R> )
rmail_LowerMail         rs.l 1
rmail_UpperMail         rs.l 1
rmail_MailList          rs.l 6
rmail_ListCall          rs.b 16
rmail_ListProgress      rs.l 1
rmail_WaitBinStatus     rs.l 1
rmail_ForceEnd          rs.l 1
rmail_InHeader          rs.l 1
rmail_LineBuffer        rs.b 260
rmail_LineBuffer2       rs.b 1024
rmail_InLine            rs.l 1
rmail_ChatMode          rs.l 1
rmail_DateBuffer        rs.b 16
rmail_StatusBuffer      rs.b 16
rmail_Size              rs.b 0


KMAIL_Head              dc.l KMAIL_Name
                        dc.l 1
                        dc.l KMAIL_Init
                        dc.l KMAIL_Head2        ;Next
                        dc.l 512                ;Subtype (9=Mailing)
KMAIL_Head2             dc.l KMAIL_Name_2
                        dc.l 1
                        dc.l KMAIL_Init_BBS
                        dc.l KMAIL_Head3
                        dc.l 512
KMAIL_Head3             dc.l KMAIL_Name_3
                        dc.l 1
                        dc.l KMAIL_Init_Oldmail
                        dc.l KMAIL_Head4
                        dc.l 512
KMAIL_Head4             dc.l KMAIL_Name_4
                        dc.l 1
                        dc.l KMAIL_Init_Allmail
                        dc.l KMAIL_Head5
                        dc.l 512
KMAIL_Head5             dc.l KMAIL_Name_5
                        dc.l 1
                        dc.l KMAIL_Init_To
                        dc.l KMAIL_Head6
                        dc.l 512
KMAIL_Head6             dc.l KMAIL_Name_6
                        dc.l 1
                        dc.l KMAIL_Init_From
                        dc.l LMAIL_Head
                        dc.l 512


KMAIL_Name              dc.b "KM",$01,"AIL",0
KMAIL_Name_2            dc.b "bbs_K",0
KMAIL_Name_3            dc.b "bbs_KM",0
KMAIL_Name_4            dc.b "bbs_KN",0
KMAIL_Name_5            dc.b "bbs_K>",0
KMAIL_Name_6            dc.b "bbs_K<",0
            even


KMAIL_Init              moveq #-1,d1
                        bra KMAIL_Init_Gen
KMAIL_Init_BBS          moveq #2,d1
                        bra KMAIL_Init_Gen
KMAIL_Init_Oldmail      moveq #1,d1
                        bra KMAIL_Init_Gen
KMAIL_Init_Allmail      moveq #0,d1
                        bra KMAIL_Init_Gen
KMAIL_Init_To           moveq #5,d1
                        bra KMAIL_Init_Gen
KMAIL_Init_From         moveq #4,d1
                        bra KMAIL_Init_Gen

KMAIL_Init_Gen          movem.l a6/a2-a4/d5-d7,-(sp)
                        move.l a0,a4
                        move.l a1,d7
                        move.l d0,d6
                        move.l a3,a6
                        move.l #KMAIL_Functions-KMAIL_FunctionsBegin,d0
                        move.l d1,-(sp)
                        move.l #kmail_Size,d1
                        lea KMAIL_Functions,a0
                        move.l ppb_Global(a6),a5
                        jsr _QVOCreateServerBase(A6)
                        move.l (sp)+,d1
                        tst.l d0
                        beq .Cannot_Serve
                        move.l d0,a6
                        move.l a3,a5
                        move.l d1,kmail_ListMode(a6)
                        clr.l kmail_ListProgress(a6)
                        move.l a3,kmail_QSOBase(a6)
                        move.l d7,kmail_OldServer(A6)
                        move.l d6,kmail_Channel(a6)
                        move.l a2,kmail_Parameter(a6)
                        move.l a4,kmail_ThisQSO(a6)
                        clr.l kmail_DelayKill(a6)
                        move.l a6,qso_Server(a4)
                        
                        move.l a2,a0
                        GOQSO ParseComLine
                        tst.l d0
                        blt .Invalid_Parameters
                        move.l d0,d7
                        move.l kmail_ThisQSO(a6),a0
                        lea qso_Call(a0),a0
                        move.l a0,kmail_FromCall(a6)
                        
                        
                        move.l kmail_ListMode(a6),d0
                        cmp.l #-1,d0
                        bne .RLine_NoChat
                        tst.l d7
                        bne .RLine_ChatPar
                        move.l #1,kmail_ListMode(a6)
                        bra .First_POK
                        
.RLine_NoChat           tst.l d0
                        beq .First_POKNO
                        cmp.l #1,d0
                        beq .First_POKNO
                        cmp.l #2,d0
                        beq .RLine_ChatPar
                        cmp.l #3,d0
                        beq .RLine_ChatPar
                        cmp.l #4,d0
                        beq .RLine_FmPar
                        cmp.l #5,d0
                        beq .RLine_ToPar
                        bra .Invalid_Parameters
.First_POKNO            tst.l d7
                        bne .Invalid_Parameters
                        bra .First_POK
.RLine_ChatPar          tst.l d7
                        beq .Invalid_Parameters
                        cmp.l #1,d7
                        bgt .CC_RVM
                        move.l #2,kmail_ListMode(a6)
                        lea kmail_LowerMail(a6),a0
                        move.l kmail_Parameter(a6),a1
                        move.l d7,d0
                        bsr MPAR_Area
                        tst.l d0
                        bne .CC_RVM
                        bra .First_POK
.CC_RVM         move.l #3,kmail_ListMode(a6)
                        lea kmail_MailList(a6),a0
                        move.l kmail_Parameter(a6),a1
                        move.l d7,d0
                        bsr MPAR_Multi
                        tst.l d0
                        bne .Invalid_Parameters
                        bra .First_POK
.RLine_FmPar            move.l #4,kmail_ListMode(a6)
                        lea kmail_ListCall(a6),a0
                        move.l kmail_Parameter(A6),a1
                        move.l d7,d0
                        bsr MPAR_Call
                        tst.l d0
                        bne .Invalid_Parameters
                        bra .First_POK
.RLine_ToPar            move.l #5,kmail_ListMode(a6)
                        lea kmail_ListCall(a6),a0
                        move.l lmail_Parameter(A6),a1
                        move.l d7,d0
                        bsr MPAR_Call
                        tst.l d0
                        bne .Invalid_Parameters
.First_POK
                        tst.l kmail_ListMode(a6)
                        beq .Do_RList
                        cmp.l #1,kmail_ListMode(A6)
                        beq .Do_RList
                        cmp.l #2,kmail_ListMode(a6)
                        beq .Do_RArea
                        cmp.l #3,kmail_ListMode(a6)
                        beq .Do_RMList
                        cmp.l #4,kmail_ListMode(a6)
                        beq .Do_FmList
                        cmp.l #5,kmail_ListMode(a6)
                        beq .Do_ToList
                        bra .Server_Success

.Do_RMList              cmp.l #6,kmail_ListProgress(a6)
                        beq .Server_Success
                        move.l kmail_ListProgress(a6),d0
                        lea kmail_MailList(a6),a0
                        move.l d0,d1
                        asl.l #2,d1
                        addq.l #1,d0
                        move.l d0,kmail_ListProgress(a6)
                        add.l d1,a0
                        move.l (a0),d0
                        tst.l d0
                        beq .Server_Success
                        bra .Open_NewMail

.Do_RArea               sub.l #20,a7
                        move.l kmail_ThisQSO(a6),a0
                        lea qso_Call(a0),a0
                        move.l (a0),(a7)
                        move.l 4(a0),4(a7)
                        move.l 8(a0),8(a7)
                        move.l 12(a0),12(a7)
                        lea kmail_LowerMail(a6),a1
                        move.l a1,16(a7)
                        move.l a7,a0
                        moveq #11,d0
                        move.l kmail_ListProgress(a6),d1
                        GOQSO SearchMail
                        add.l #20,a7
                        tst.l d0
                        beq .Server_Success
                        move.l d0,kmail_ListProgress(a6)
                        bra .Open_NewMail


.Do_RList               move.l kmail_ListProgress(a6),d1
                        move.l kmail_ThisQSO(a6),a0
                        lea qso_Call(a0),a0
                        moveq #1,d0
                        tst.l kmail_ListMode(a6)
                        beq .Do_OldList
                        moveq #4,d0
.Do_OldList             GOQSO SearchMail
                        tst.l d0
                        beq .Server_Success
                        move.l d0,kmail_ListProgress(a6)
                        bra .Open_NewMail
.Do_FmList              move.l kmail_ListProgress(a6),d1
                        move.l kmail_ThisQSO(a6),a0
                        lea qso_Call(a0),a0
                        sub.l #32,a7
                        move.l a7,a1
                        move.l (a0),(a1)
                        move.l 4(a0),4(a1)
                        move.l 8(a0),8(A1)
                        move.l 12(a0),12(a1)
                        lea kmail_ListCall(a6),a0
                        move.l (a0),16(a1)
                        move.l 4(a0),20(a1)
                        move.l 8(a0),24(a1)
                        move.l 12(a0),28(a1)
                        clr.b 31(a1)
                        move.l a1,a0
                        moveq #8,d0
                        GOQSO SearchMail
                        add.l #32,a7
                        tst.l d0
                        beq .Server_Success
                        move.l d0,kmail_ListProgress(a6)
                        bra .Open_NewMail
.Do_ToList              move.l kmail_ListProgress(a6),d1
                        move.l kmail_ThisQSO(a6),a0
                        lea qso_Call(a0),a0
                        sub.l #32,a7
                        move.l a7,a1
                        move.l (a0),(a1)
                        move.l 4(a0),4(a1)
                        move.l 8(a0),8(A1)
                        move.l 12(a0),12(a1)
                        lea kmail_ListCall(a6),a0
                        move.l (a0),16(a1)
                        move.l 4(a0),20(a1)
                        move.l 8(a0),24(a1)
                        move.l 12(a0),28(a1)
                        clr.b 31(a1)
                        move.l a1,a0
                        moveq #9,d0
                        GOQSO SearchMail
                        add.l #32,a7
                        tst.l d0
                        beq .Server_Success
                        move.l d0,kmail_ListProgress(a6)
                        bra .Open_NewMail

.Open_NewMail           tst.l kmail_DelayKill(a6)
                        beq .No_OpenKill
                        move.l d0,-(sp)
                        move.l kmail_DelayKill(a6),d0
                        bsr .Kill_MailSub
                        move.l (sp)+,d0
.No_OpenKill            move.l d0,kmail_DelayKill(a6)
                        bra .First_POK


                
                        
.Kill_MailSub           movem.l d2-d7/a2-a6,-(sp)
                        move.l d0,d7
                        move.l kmail_FromCall(a6),a0
                        move.l d7,d0
                        GOQSO KillMail
                        neg.l d0
                        add.l #7,d0
                        bsr Get_Text
                        move.l d7,-(sp)
                        move.l a7,a1
                        move.l d0,a0
                        move.l kmail_Channel(a6),d0
                        moveq #0,d1
                        GOQSO OutputFormat
                        move.l (sp)+,d0
                        movem.l (sp)+,d2-d7/a2-a6
                        rts
                        
.Server_Success         tst.l kmail_DelayKill(A6)
                        beq .NoShutKill
                        move.l kmail_DelayKill(A6),d0
                        bsr .Kill_MailSub
.NoShutKill             bsr KMAIL_Cleanup
                        movem.l (sp)+,a6/a2-a4/d5-d7
                        moveq #0,d0
                        rts

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

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

KMAIL_Cleanup           move.l kmail_OldServer(a6),d0
                        move.l kmail_ThisQSO(a6),a0
                        move.l d0,qso_Server(a0)
                        move.l a6,a1
                        move.l ppb_ExecBase(a5),a6
                        sub.l #KMAIL_Functions-KMAIL_FunctionsBegin,a1
                        jsr _LVOFreeVec(A6)
                        moveq #0,d0
                        rts

KMAIL_ConnectProced     rts

KMAIL_ReceivePacket     rts

KMAIL_TerminalInput     rts     

KMAIL_AbortServer       rts

KMAIL_GetServerType     move.l #512,d0
                        rts

KMAIL_Nop               rts

KMAIL_FunctionsBegin    
                        AJUMP GEN_AskAudioPass                  ;-54
                        AJUMP KMAIL_AbortServer                 ;-48
                        AJUMP KMAIL_GetServerType               ;-42
                        AJUMP KMAIL_TerminalInput               ;-36
                        AJUMP KMAIL_ReceivePacket               ;-30
                        AJUMP KMAIL_Nop                         ;-24
                        AJUMP KMAIL_ConnectProced               ;-18
                        AJUMP KMAIL_Cleanup                     ;-12
                        AJUMP KMAIL_Init                        ;-6
KMAIL_Functions         
                        
                        even
                        
                        
                        rsreset
kmail_ThisQSO           rs.l 1
kmail_Channel           rs.l 1
kmail_OldServer         rs.l 1
kmail_QSOBase           rs.l 1
kmail_Parameter         rs.l 1
kmail_FromCall          rs.l 1
kmail_ListMode          rs.l 1          ;(0=RN 1=RM 2=Rx-y 3=R n n n n n n n
                                        ; 4=R< 5=R> )
kmail_LowerMail         rs.l 1
kmail_UpperMail         rs.l 1
kmail_MailList          rs.l 6
kmail_ListCall          rs.b 16
kmail_ListProgress      rs.l 1
kmail_DelayKill         rs.l 1
kmail_Size              rs.b 0





LMAIL_Head              dc.l LMAIL_Name
                        dc.l 1
                        dc.l LMAIL_Init_Chat
                        dc.l LMAIL_Head_2       ;Next
                        dc.l 512                ;Subtype (9=Mailing)
LMAIL_Head_2            dc.l LMAIL_Name_2
                        dc.l 1
                        dc.l LMAIL_Init_R
                        dc.l LMAIL_Head_3       ;Next
                        dc.l 512                ;Subtype (9=Mailing)
LMAIL_Head_3            dc.l LMAIL_Name_3
                        dc.l 1
                        dc.l LMAIL_Init_RM
                        dc.l LMAIL_Head_4       ;Next
                        dc.l 512                ;Subtype (9=Mailing)
LMAIL_Head_4            dc.l LMAIL_Name_4
                        dc.l 1
                        dc.l LMAIL_Init_RN
                        dc.l LMAIL_Head_5       ;Next
                        dc.l 512                ;Subtype (9=Mailing)
LMAIL_Head_5            dc.l LMAIL_Name_5
                        dc.l 1
                        dc.l LMAIL_Init_RFm
                        dc.l LMAIL_Head_6       ;Next
                        dc.l 512                ;Subtype (9=Mailing)
LMAIL_Head_6            dc.l LMAIL_Name_6
                        dc.l 1
                        dc.l LMAIL_Init_RTo
                        dc.l 0                  ;Next
                        dc.l 512                ;Subtype (9=Mailing)

                        
LMAIL_Name              dc.b "LM",$01,"AIL",0
LMAIL_Name_2            dc.b "bbs_L",0
LMAIL_Name_3            dc.b "bbs_LM",0
LMAIL_Name_4            dc.b "bbs_LN",0
LMAIL_Name_5            dc.b "bbs_L<",0
LMAIL_Name_6            dc.b "bbs_L>",0
                        even
                        
LMAIL_Init_Chat         move.l #-1,d1
                        bra LMAIL_Init_Gen
LMAIL_Init_R            moveq #2,d1
                        bra LMAIL_Init_Gen
LMAIL_Init_RM           moveq #1,d1
                        bra LMAIL_Init_Gen
LMAIL_Init_RN           moveq #0,d1
                        bra LMAIL_Init_Gen
LMAIL_Init_RFm          moveq #4,d1
                        bra LMAIL_Init_Gen
LMAIL_Init_RTo          moveq #5,d1
                        bra LMAIL_Init_Gen

LMAIL_Init_Gen          movem.l a6/a2-a4/d5-d7,-(sp)
                        move.l a0,a4
                        move.l a1,d7
                        move.l d0,d6
                        move.l a3,a6
                        move.l #LMAIL_Functions-LMAIL_FunctionsBegin,d0
                        move.l d1,-(sp)
                        move.l #lmail_Size,d1
                        lea LMAIL_Functions,a0
                        move.l ppb_Global(a6),a5
                        jsr _QVOCreateServerBase(A6)
                        move.l (sp)+,d1
                        tst.l d0
                        beq .Cannot_Serve
                        move.l d0,a6
                        move.l a3,a5
                        move.l d1,lmail_ListMode(a6)
                        move.l a3,lmail_QSOBase(a6)
                        move.l d7,lmail_OldServer(A6)
                        move.l d6,lmail_Channel(a6)
                        move.l a2,lmail_Parameter(a6)
                        move.l a4,lmail_ThisQSO(a6)
                        move.l a6,qso_Server(a4)
                        clr.l lmail_ChatMode(a6)
                        clr.l lmail_InLine(a6)

                        move.l a2,a0
                        GOQSO ParseComLine
                        tst.l d0
                        blt .Invalid_Parameters
                        move.l d0,d7

                        move.l lmail_ListMode(a6),d0
                        cmp.l #-1,d0
                        bne .RLine_NoChat
                        move.l #1,lmail_ChatMode(a6)
                        tst.l d7
                        bne .RLine_ChatPar
                        clr.l lmail_ListMode(a6)
                        bra .First_POK
                        
.RLine_NoChat           tst.l d0
                        beq .First_POKNO
                        cmp.l #1,d0
                        beq .First_POKNO
                        cmp.l #2,d0
                        beq .RLine_ChatPar
                        cmp.l #3,d0
                        beq .RLine_ChatPar
                        cmp.l #4,d0
                        beq .RLine_FmPar
                        cmp.l #5,d0
                        beq .RLine_ToPar
                        bra .Invalid_Parameters
.First_POKNO            tst.l d7
                        bne .Invalid_Parameters
                        bra .First_POK
.RLine_ChatPar          tst.l d7
                        beq .Line_Boxcheck
                        cmp.l #1,d7
                        bgt .CC_RVM
                        move.l #2,lmail_ListMode(a6)
                        lea lmail_LowerMail(a6),a0
                        move.l lmail_Parameter(a6),a1
                        move.l d7,d0
                        bsr MPAR_Area
                        tst.l d0
                        bne .CC_RVM
                        bra .First_POK
.CC_RVM         move.l #3,lmail_ListMode(a6)
                        lea lmail_MailList(a6),a0
                        move.l lmail_Parameter(a6),a1
                        move.l d7,d0
                        bsr MPAR_Multi
                        tst.l d0
                        bne .Invalid_Parameters
                        bra .First_POK
.RLine_FmPar            move.l #4,lmail_ListMode(a6)
                        lea lmail_ListCall(a6),a0
                        move.l lmail_Parameter(A6),a1
                        move.l d7,d0
                        bsr MPAR_Call
                        tst.l d0
                        bne .Invalid_Parameters
                        bra .First_POK
.RLine_ToPar            move.l #5,lmail_ListMode(a6)
                        lea lmail_ListCall(a6),a0
                        move.l lmail_Parameter(A6),a1
                        move.l d7,d0
                        bsr MPAR_Call
                        tst.l d0
                        bne .Invalid_Parameters
                        bra .First_POK
.Line_Boxcheck          move.l lmail_ThisQSO(a6),a0
                        move.l qso_User(a0),a0
                        cmpa.l #0,a0
                        bne .Good_UserBCheck
.Boxcheck_Abo           bsr LMAIL_Cleanup
                        bra .Exit
.Good_UserBCheck        move.l user_LastBoxcheck(a0),d0
                        addq.l #1,d0
                        move.l d0,lmail_BoxcheckFm(a6)
                        move.l a0,-(sp)
                        GOQSO GetLatestMail
                        move.l (sp)+,a0
                        tst.l d0
                        beq .Boxcheck_Abo
                        move.l d0,lmail_BoxcheckTo(a6)
                        move.l d0,user_LastBoxcheck(a0)
                        addq.l #1,d0
                        cmp.l lmail_BoxcheckFm(A6),d0
                        beq .Boxcheck_Abo
                        GOQSO ChangeUser
                        move.l #6,lmail_ListMode(a6)
.First_POK              
                        clr.l lmail_ListProgress(a6)
                                                        
.Server_Success         bsr LMAIL_DoPoll
.Exit                   movem.l (sp)+,a6/a2-a4/d5-d7
                        moveq #0,d0
                        rts


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

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

LMAIL_Cleanup           move.l lmail_OldServer(a6),d0
                        move.l lmail_ThisQSO(a6),a0
                        move.l d0,qso_Server(a0)
                        move.l a6,a1
                        move.l ppb_ExecBase(a5),a6
                        sub.l #LMAIL_Functions-LMAIL_FunctionsBegin,a1
                        jsr _LVOFreeVec(A6)
                        moveq #0,d0
                        rts

LMAIL_ConnectProced     rts

LMAIL_ReceivePacket     movem.l d2-d7/a2-a6,-(sp)
.Loop                   move.l lmail_ThisQSO(a6),a0
                        GOQSO TestServBufferLin
                        tst.l d0
                        beq .Exit
                        move.l d0,d1
                        add.l lmail_InLine(a6),d1
                        cmp.l #1024,d1
                        ble .Good_Line
                        move.l #1024,d1
.Good_Line              lea lmail_LineBuffer2(a6),a0
                        add.l lmail_InLine(a6),d0
                        move.l lmail_ThisQSO(A6),a1
                        GOQSO ReadServBuffer
                        add.l d0,lmail_InLine(a6)
                        move.l lmail_InLine(a6),d0
                        tst.l d0
                        beq .Exit
                        lea lmail_LineBuffer2(a6),a0
                        subq.l #1,d0
                        add.l d0,a0
                        cmp.b #$0d,(a0)
                        bne .Exit
                        tst.l lmail_ChatMode(a6)
                        beq .No_Output
                        move.l lmail_InLine(a6),d0
                        move.l lmail_Channel(a6),d1
                        moveq #3,d2
                        lea lmail_LineBuffer2(a6),a0
                        GOQSO WriteTerminal
.No_Output              cmp.l #2,lmail_InLine(a6)
                        blt .No_TextAborter
                        lea lmail_LineBuffer2(a6),a0
                        move.b (a0),d0
                        and.b #$df,d0
                        cmp.b #$41,d0
                        bne .No_TextAborter
                        cmp.b #$0d,1(a0)
                        bne .No_TextAborter
                        moveq #20,d0
                        bsr Get_Text
                        move.l d0,a0
                        move.l lmail_Channel(a6),d0
                        moveq #1,d1
                        moveq #0,d2
                        GOQSO TransmitText
                        bsr LMAIL_AbortServer
                        bra .Exit
.No_TextAborter         clr.l lmail_InLine(a6)
                        bra .Loop
.Exit                   movem.l (Sp)+,d2-d7/a2-a6
                        rts




LMAIL_TerminalInput     rts     

LMAIL_DoPoll            movem.l d2-d7/a2-a6,-(sp)
.Do_RePoll              moveq #0,d5
                        move.l lmail_Channel(a6),d0
                        GOQSO GetOutstanding
                        cmp.l #5,d0
                        bge .Exit
                        
                        tst.l lmail_ListMode(a6)
                        beq .Do_RList
                        cmp.l #1,lmail_ListMode(A6)
                        beq .Do_RList
                        cmp.l #2,lmail_ListMode(a6)
                        beq .Do_RArea                   
                        cmp.l #3,lmail_ListMode(a6)
                        beq .Do_RMList
                        cmp.l #4,lmail_ListMode(a6)
                        beq .Do_FmList
                        cmp.l #5,lmail_ListMode(a6)
                        beq .Do_ToList
                        cmp.l #6,lmail_ListMode(a6)
                        beq .Do_Boxcheck
                        bra .Terminate_Server

.Do_RMList              sub.l #20,a7
                                move.l lmail_ThisQSO(a6),a0
                                lea qso_Call(a0),a0
                                move.l (a0),(a7)
                                move.l 4(a0),4(a7)
                                move.l 8(a0),8(a7)
                                move.l 12(a0),12(a7)
                                lea lmail_MailList(a6),a1
                                move.l a1,16(a7)
                                move.l a7,a0
                                lea lmail_LineBuffer(a6),a1
                                moveq #10,d0
                                move.l lmail_ListProgress(a6),d1
                                GOQSO GetMailLines
                                add.l #20,a7
                                tst.l d0
                                beq .Terminate_Server
                                move.l d0,lmail_ListProgress(a6)
                                bra .Open_NewMail

.Do_Boxcheck            move.l lmail_BoxcheckFm(A6),d0
                        cmp.l lmail_BoxcheckTo(a6),d0
                        bgt .Do_Boxcheck_Overflow
                        sub.l #28,a7
                        move.l lmail_ThisQSO(A6),a0
                        lea qso_Call(a0),a0
                        move.l (a0),(a7)
                        move.l 4(a0),4(a7)
                        move.l 8(a0),8(a7)
                        move.l 12(a0),12(a7)
                        lea 20(a7),a1
                        move.l a1,16(a7)
                        move.l lmail_BoxcheckFm(A6),d0
                        move.l d0,20(a7)
                        move.l lmail_BoxcheckTo(A6),d0
                        move.l d0,24(a7)
                        move.l a7,a0
                        lea lmail_LineBuffer(A6),a1
                        moveq #11,d0
                        move.l lmail_ListProgress(A6),d1
                        GOQSO GetMailLines
                        add.l #28,a7
                        tst.l d0
                        beq .Terminate_Server
                        move.l d0,lmail_ListProgress(A6)
                        bra .Open_NewMail

.Do_Boxcheck_Overflow   sub.l #28,a7
                        move.l lmail_ThisQSO(A6),a0
                        lea qso_Call(a0),a0
                        move.l (a0),(A7)
                        move.l 4(a0),4(a7)
                        move.l 8(a0),8(a7)
                        move.l 12(a0),12(a7)
                        lea 20(a7),a1
                        move.l a1,16(a7)
                        move.l lmail_BoxcheckFm(a6),d0
                        move.l d0,20(A7)
                        move.l #9999999,24(a7)
                        move.l a7,a0
                        lea lmail_LineBuffer(a6),a1
                        moveq #11,d0
                        move.l lmail_ListProgress(a6),d1
                        GOQSO GetMailLines
                        add.l #28,a7
                        tst.l d0
                        beq .Do_Boxcheck_Overend
                        move.l d0,lmail_ListProgress(a6)
                        bra .Open_NewMail
.Do_Boxcheck_Overend    move.l #1,lmail_BoxcheckFm(a6)
                        move.l #-2,lmail_ListProgress(a6)
                        bra .Do_Boxcheck
                        
.Do_RArea               sub.l #20,a7
                                move.l lmail_ThisQSO(a6),a0
                                lea qso_Call(a0),a0
                                move.l (a0),(a7)
                                move.l 4(a0),4(a7)
                                move.l 8(a0),8(a7)
                                move.l 12(a0),12(a7)
                                lea lmail_LowerMail(a6),a1
                                move.l a1,16(a7)
                                move.l a7,a0
                                lea lmail_LineBuffer(a6),a1
                                moveq #11,d0
                                move.l lmail_ListProgress(a6),d1
                                GOQSO GetMailLines
                                add.l #20,a7
                                tst.l d0
                                beq .Terminate_Server
                                move.l d0,lmail_ListProgress(a6)
                                bra .Open_NewMail
.Do_RList               move.l lmail_ListProgress(a6),d1
                        move.l lmail_ThisQSO(a6),a0
                        lea qso_Call(a0),a0
                        lea lmail_LineBuffer(a6),a1
                        moveq #1,d0
                        tst.l lmail_ListMode(a6)
                        bne .Do_OldList
                        moveq #3,d0
.Do_OldList             GOQSO GetMailLines
                        tst.l d0
                        beq .Terminate_Server
                        move.l d0,lmail_ListProgress(a6)
                        bra .Open_NewMail
.Do_FmList              move.l lmail_ListProgress(a6),d1
                        move.l lmail_ThisQSO(a6),a0
                        lea qso_Call(a0),a0
                        sub.l #32,a7
                        move.l a7,a1
                        move.l (a0),(a1)
                        move.l 4(a0),4(a1)
                        move.l 8(a0),8(A1)
                        move.l 12(a0),12(a1)
                        lea lmail_ListCall(a6),a0
                        move.l (a0),16(a1)
                        move.l 4(a0),20(a1)
                        move.l 8(a0),24(a1)
                        move.l 12(a0),28(a1)
                        clr.b 31(a1)
                        move.l a1,a0
                        moveq #8,d0
                        lea lmail_LineBuffer(a6),a1
                        GOQSO GetMailLines
                        add.l #32,a7
                        tst.l d0
                        beq .Terminate_Server
                        move.l d0,lmail_ListProgress(a6)
                        bra .Open_NewMail
.Do_ToList              move.l lmail_ListProgress(a6),d1
                        move.l lmail_ThisQSO(a6),a0
                        lea qso_Call(a0),a0
                        sub.l #32,a7
                        move.l a7,a1
                        move.l (a0),(a1)
                        move.l 4(a0),4(a1)
                        move.l 8(a0),8(A1)
                        move.l 12(a0),12(a1)
                        lea lmail_ListCall(a6),a0
                        move.l (a0),16(a1)
                        move.l 4(a0),20(a1)
                        move.l 8(a0),24(a1)
                        move.l 12(a0),28(a1)
                        clr.b 31(a1)
                        move.l a1,a0
                        moveq #9,d0
                        lea lmail_LineBuffer(a6),a1
                        GOQSO GetMailLines
                        add.l #32,a7
                        tst.l d0
                        beq .Terminate_Server
                        move.l d0,lmail_ListProgress(a6)
                        bra .Open_NewMail

.Open_NewMail   lea lmail_LineBuffer(a6),a0
                        move.l lmail_Channel(a6),d0
                        moveq #0,d1
                        moveq #0,d2
                        GOQSO TransmitText                      
                        bra .Do_RePoll
.Terminate_Server       move.l lmail_ThisQSO(a6),a0
                        GOQSO TestServBuffer
                        move.l d0,-(sp)
                        move.l lmail_OldServer(a6),-(sp)
                        bsr LMAIL_Cleanup
                        move.l (sp)+,a6
                        move.l (Sp)+,d0
                        cmpa.l #0,a6
                        beq .Exit
                        jsr _SVOReceivePacket(A6)
.Exit                   movem.l (sp)+,d2-d7/a2-a6
                        rts
                

LMAIL_AbortServer       move.l lmail_ThisQSO(a6),a0
                        GOQSO TestServBuffer
                        move.l d0,-(sp)
                        move.l lmail_OldServer(a6),-(sp)
                        bsr LMAIL_Cleanup
                        move.l (sp)+,a6
                        move.l (Sp)+,d0
                        cmpa.l #0,a6
                        beq .Exit_2
                        jsr _SVOReceivePacket(A6)
.Exit_2                 rts


LMAIL_GetServerType     move.l #512,d0
                        rts

LMAIL_Nop               rts

                        
LMAIL_FunctionsBegin    
                        AJUMP GEN_AskAudioPass                  ;-54
                        AJUMP LMAIL_AbortServer                 ;-48
                        AJUMP LMAIL_GetServerType               ;-42
                        AJUMP LMAIL_TerminalInput               ;-36
                        AJUMP LMAIL_ReceivePacket               ;-30
                        AJUMP LMAIL_DoPoll                      ;-24
                        AJUMP LMAIL_ConnectProced               ;-18
                        AJUMP LMAIL_Cleanup                     ;-12
                        AJUMP LMAIL_Init_Chat                   ;-6
LMAIL_Functions         
                        
                        even
                        
                        
                        rsreset
lmail_ThisQSO           rs.l 1
lmail_Channel           rs.l 1
lmail_OldServer         rs.l 1
lmail_QSOBase           rs.l 1
lmail_Parameter         rs.l 1
lmail_ListMode          rs.l 1          ;(0=RN 1=RM 2=Rx-y 3=R n n n n n n n
                                        ; 4=R< 5=R> )
lmail_LowerMail         rs.l 1
lmail_UpperMail         rs.l 1
lmail_MailList          rs.l 6
lmail_ListCall          rs.b 16
lmail_ListProgress      rs.l 1
lmail_ForceEnd          rs.l 1
lmail_InLine            rs.l 1
lmail_ChatMode          rs.l 1
lmail_LineBuffer        rs.b 260
lmail_LineBuffer2       rs.b 1024
lmail_DateBuffer        rs.b 16
lmail_StatusBuffer      rs.b 16
lmail_BoxcheckFm        rs.l 1
lmail_BoxcheckTo        rs.l 1
lmail_Size              rs.b 0




MPAR_Area       cmp.l #1,d0
                        bne .BadPar
                        move.l #1,(a0)
                        move.l #99999,4(a0)
                        cmp.b #"-",(a1)
                        beq .AreaStix
                        bsr MPAR_ReadDe
                        cmp.l #-1,d0
                        beq .BadPar
                        move.l d0,(a0)
                        cmp.b #"-",(a1)
                        bne .BadPar
.AreaStix       addq.l #1,a1
                        tst.b (a1)
                        beq .GoodPar
                        bsr MPAR_ReadDe
                        cmp.l #-1,d0
                        beq .BadPar
                        move.l d0,4(a0)
.GoodPar        move.l (a0),d0
                        cmp.l 4(a0),d0
                        ble .NoSwap
                        move.l 4(a0),d1
                        move.l d0,4(a0)
                        move.l d1,(a0)
.NoSwap         moveq #0,d0
                        rts
.BadPar         moveq #1,d0
                        rts
MPAR_Multi      move.l d0,d3
                        tst.l d3
                        ble .BadPar
                        cmp.l #7,d3
                        bge .BadPar
.Loop           tst.l d3
                        beq .ExitPar
                        subq.l #1,d3
                        bsr MPAR_ReadDe
                        tst.b (a1)+
                        bne .BadPar
                        move.l d0,(a0)+
                        bra .Loop                       
.ExitPar        moveq #0,d0
                        rts             
.BadPar moveq #1,d0
                        rts
MPAR_Call       cmp.l #1,d0
                        bne .Invalid
                        moveq #8,d1
.Looping                move.b (a1)+,(a0)+
                        tst.b -1(a0)
                        beq .GoodCall
                        dbf d1,.Looping                 
.Invalid                moveq #1,d0
                        rts
.GoodCall               moveq #0,d0
                        rts

MPAR_ReadDe moveq #0,d0
                        moveq #1,d1
.Looping        tst.b (a1)
                        beq .ExitSign
                        cmp.b #"-",(a1)
                        beq .ExitSign
                        move.b (a1)+,d2
                        cmp.b #$30,d2
                        blt .BadSign
                        cmp.b #$39,d2
                        bgt .BadSign
                        and.l #$f,d2
                        mulu #10,d0
                        add.l d2,d0
                        mulu #10,d1
                        bra .Looping
.BadSign        moveq #-1,d0
                        rts
.ExitSign       cmp.l #1,d1
                        beq .BadSign
                        rts
                        
                                
                
GEN_AskAudioPass move.l a6,-(sp)
                        move.l smail_OldServer(a6),a6
                        cmpa.l #0,a6
                        beq .NoServer
                        jsr _SVOAskAudio(a6)
.NoServer       move.l (sp)+,a6
                        rts


Get_Text                lea _MailCatalog(pc),a0
                        move.l smail_ThisQSO(a6),a1
                        move.l a2,-(sp)
                        lea _MailCatalogName(pc),a2
                        GOQSO GetText
                        move.l (sp)+,a2
                        rts

        
AB_NoString             dc.b "#NO#",$0d,0
AB_OkString             dc.b "#OK#",$0d,0
AB_AbortString          dc.b "#ABORT#",$0d,0

IssueNO_BID             dc.b "NO - no bid",$0d,0
IssueNO_Double          dc.b "NO - double",$0d,0
IssueYes                dc.b "OK",$0d,0
                        even

                        include "/Catalogs/Sources/Mail.s"
                        
                        
                        
                        
                        
                        
                        
                        
                        
                        
                        
                        
                        
