;*********************************************************************
;
; MAILON.M68    -       MAIL LOGON Routine
;
;                  Written by: Dave Heyliger - AMUS Staff
;
;       Purpose: To inform system that YOU are on. Records up
;                to 3 logon JOBs you happen to be on (3 JOBIDX's)
;
;          Used: Upon startup via a START.CMD file in CMD:
;                or with FORCE from AMOSL.INI
;
;*********************************************************************

       SEARCH  SYS                             ;grab the regulars
       SEARCH  SYSSYM
       SEARCH  TRM

       ;define a version number
       VMAJOR=1.                               ;original program by
       VMINOR=0.                               ;Dave Heyliger - AMUS
       VEDIT=0

       PHDR    -2,0,PH$REE!PH$REU              ;may be run logged out

       .OFINI
       .OFDEF  IDDB,D.DDB                      ;EMAIL.USR, EMAIL.DAT DDB
       .OFDEF  SCRAP,20.                       ;scrap buffer area workspace
       .OFDEF  MEMPTR,4                        ;EMAIL.SYS memory location
       .OFSIZ  IMPSIZ

       GETIMP  IMPSIZ,A3                       ;A3 points to variables

       ;point to EMAIL.SYS memory in system memory - A4 dedicated
SCHSYS: LEA     A2,SYSFIL                       ;point to ascii file
       LEA     A0,SCRAP(A3)                    ;A0 points to buffer area
       FILNAM  @A0                             ;create filename
       SRCH    @A0,A4                          ;A4 will contain mem pointer
       BEQ     SAVPTR                          ;search found EMAIL.USR
       TYPECR  <?EMAIL.SYS not installed. Contact you SYSOP>
BYEBYE: EXIT

       ;get the users name (CR will prompt them again, ^C cancels prompt)
SAVPTR: MOV     A4,MEMPTR(A3)                   ;save memory pointer
SRCHOK: MOV     MEMPTR(A3),A4                   ;get memory pointer to A4
       MOV     #0,SCRAP(A3)                    ;clear name buffer
       CRLF                                    ;screen junk
       TYPE    <Enter EMAIL user name: >       ;prompt for user name
       KBD                                     ;get it
       CTRLC   BYEBYE                          ;^C quits (no EMAIL access)
       CMPB    @A2,#15                         ;just a cr?
       BEQ     SRCHOK                          ;yup, ask em again
       LEA     A1,SCRAP(A3)                    ;nope, point to scrap buffer
       PACK                                    ;create RAD50 handle
       PACK

       ;get the number of "blocks" to scan
SCAN:   MOV     508.(A4),D4                     ;D4 contains block size
       MOV     #20.,D3                         ;number of users per block

       ;look for this guy
LOOP:   CMM     SCRAP(A3),@A4                   ;found a name?
       BNE     NNF                             ;name not found

       ;found the user, point to the 3 IDX slots in the "bucket"
       PUSH    A4                              ;save location of user
       ADD     #12.,A4                         ;bypass passwrd etc.
       CMP     @A4,#0                          ;empty?
       BNE     IDX2                            ;nope, try next position

       ;slot open, insert JOBIDX
INID:   CALL    GETIDX                          ;yup, get IDX in
       JMP     NEWMSG                          ;and see about new messages

       ;user already on one terminal, try next slot
IDX2:   ADD     #4,A4                           ;bypass IDX-1
       CMP     @A4,#0                          ;empty?
       BNE     IDX3                            ;nope
       BR      INID                            ;yup, place em in there

       ;user on two terminals, try next slot
IDX3:   ADD     #4,A4                           ;try next position
       CMP     @A4,#0                          ;empty?
       JNE     FULIDX                          ;nope, IDX array full
       BR      INID                            ;yup, place in IDX

       ;no name found in this "bucket" of the array, try next bucket
NNF:    DEC     D3                              ;one less user to scan
       BEQ     NXTBLK                          ;0 = get next block
       ADD     #24.,A4                         ;bypass rest of goodies
       BR      LOOP                            ;search again

       ;block has been searched - user not found. Get the next block
NXTBLK: DEC     D4                              ;one less block
       JEQ     ERR                             ;user not found, let em know
       ADD     #56.,A4                         ;weird number
                                               ;(24. for the user, 32. extra)
       MOV     #20.,D3                         ;20 users/block
       BR      LOOP                            ;try searching again

;GETIDX Subroutine - gets user's IDX
;       on entry: A4 points to slot in system memory where user will be placed
;       on exit : user JOBIDX placed in system memory
;-----------------------------------------------------------------------------
GETIDX: JOBIDX  A6
       MOV     A6,@A4
       RTN

;NEW MESSAGE Routine - see's if user has any new mail since last logon
;       on entry: user's IDX was placed in system memory, A4 on stack ^ user
;       on exit : user's new mail status displayed to screen, returned to "."
;--------------------------------------------------------------------------
NEWMSG: LEA     A4,IDDB(A3)                     ;point to
       MOVW    #[EMA],IDDB+D.FIL(A3)           ;setup BOX:EMAIL.DAT
       MOVW    #[IL ],IDDB+D.FIL+2(A3)
       MOVW    #[DAT],IDDB+D.EXT(A3)
       MOVW    #[DSK],IDDB+D.DEV(A3)
       MOVW    #0,IDDB+D.DRV(A3)
       MOVW    #3402,IDDB+D.PPN(A3)
       INIT    @A4                             ;create buffer space
       LOOKUP  @A4                             ;there?
       BEQ     10$                             ;yup

       ;BOX:EMAIL.DAT not found! Bummer!
       CRLF                                    ;nope
       MOV     #7,D1                           ;bell
       TTY                                     ;beep
       TYPECR  <       ?EMAIL.DAT not found. Contact your SYSOP.>
       POP     A4                              ;adjust stack
       EXIT                                    ;and quit

10$:    POP     A1                              ;A1 points to user
       ADD     #8.,A1                          ;point to record number
       CLR     D4                              ;clear these guys
       CLR     D3
       MOVW    IDDB+D.REC+2(A3),D4             ;get first block
       MOVW    @A1,D3                          ;get offset
       ADDW    D3,D4                           ;add to D4
       MOVW    D4,IDDB+D.REC+2(A3)             ;set record number
       READ    @A4                             ;read in the users block

       ;now look 40 times for any new mail (null marks last msg slot)
       MOV     IDDB+D.BUF(A3),A2               ;A2 point to the users block
       MOV     #40,D4                          ;counter of looks
       CLR     D3                              ;counter of new messages
CNM:    CMPW    (A2)+,#1                        ;new message?
       BNE     NNM                             ;nope, don't count it
       INC     D3                              ;yup, count it
NNM:    ADD     #4,A2                           ;bypass message
       DEC     D4                              ;one less look
       BNE     CNM                             ;if not 40. looks, look again

       ;messages counted, report info to user
       SUB     #8.,A1                          ;point back to the name
       TYPE    <You have >
       MOV     D3,D1                           ;get the number
       DCVT    0,OT$TRM                        ;type out the number
       CMP     D3,#1                           ;one new message?
       BEQ     10$                             ;yup
       TYPE    < new EMAIL messages, >         ;nope, more than one
       BR      20$                             ;bypass below
10$:    TYPE    < new EMAIL message, >
20$:    LEA     A2,SCRAP(A3)                    ;get some workspace
       PUSH    A2                              ;save start location
       UNPACK                                  ;create the name
       UNPACK
       CLRB    @A2                             ;end name w/ null
       POP     A2                              ;retrieve start location
       TTYL    @A2                             ;type out the name
       CRLF                                    ;return
       EXIT                                    ;and quit

ERR:    TYPECR  <?User not on file.>            ;cant find this user
       CRLF                                    ;crlf
       JMP     SRCHOK                          ;ask again

       ;user already on at least 3 JOBs
FULIDX: CRLF                                    ;return
       MOV     #7,D1                           ;get a bell
       TTY                                     ;beep!
       TYPECR  <?user recorded on at least 3 JOBs and will not be recorded on this JOB.>
       EXIT

SYSFIL: ASCII   /EMAIL.SYS/
       EVEN
END