.;MAIL Reader Command file	By David Mitton, Distributed Systems
.;	.DISABLE DISPLAY
	.DISABLE QUIET
	.DISABLE LOWERCASE
	.ENABLE SUBSTITUTION
	.ENABLE GLOBAL
	.IFDF <ERRCTL> .SETN <ERRCTL> 203
	.SETS $TERM ""
	.SETS $NODE ""
	.SETS $EDIT "EDT"
	.SETS $MEMO "LB:"+<LIBUIC>+"MEMO.TXT"
	.TESTFILE MAIL.INI
	.IF <FILERR> EQ 1 @MAIL.INI
	.GOSUB ESCSEQ
	.SETF PASS2
	.SETS CURFIL "MAIL"
	.SETN CURMSG 1.
	.SETN LIMIT 80.
	.SETN GAPLIM 8.
	.SETF MCR
	.SETF FNEX
	.SETF CHRFLG
	.SETS BLANKS "                                            "
	.SETS CMD1 "READIRSENDELHELCOPFILFORPUREXIPRINEXBACUNDREPLAS."
	.SETS CMD2 "CHECHRSTASQUOPECATNODVERCONTERACTNTDEXTMCRUSE."
	.GOTO MAIN

.ESCSEQ: .SETS ESCHOM ""
	.SETS ESCCLR ""
	.IF $TERM EQ "" .RETURN
	.IF $TERM EQ "VT100" .GOTO SET100
	.IF $TERM EQ "VT52" .GOTO SET52
	.IF $TERM EQ "LA36" .RETURN
	.IF $TERM EQ "LA120" .RETURN
	; Unrecognized terminal type '$TERM'
.SET52:	.SETS ESCHOM "H"
	.SETS ESCCLR "J"
	.RETURN
.SET100: .SETS ESCHOM "[H"
	.SETS ESCCLR "[2J"
	.RETURN

.TER:
	.PARSE CMD "   " NULL SMAIL NULL
	.SETS $TERM SMAIL
	.TEST $TERM
	.IF <STRLEN> EQ 0 .ASKS $TERM  Terminal type (VT52,VT100)
	.GOTO ESCSEQ
	
.GETNUM:
	.IFF FNEX .SETT FNEX
	.PARSE CMD "  " NULL SMAIL NULL
	.TEST SMAIL
	.IF <STRLEN> EQ 0 .GOTO GTNM2
	.ONERR GTNM1
	.SETN MAILN 'SMAIL'.
	.SETN CURMSG 'MAILN'.
	.ONERR MAIN0
	.RETURN

.GTNM1:	.SETN MAILN 0.
	.ONERR MAIN0
	.RETURN
.GTNM2: .SETN MAILN 'CURMSG'.
	.RETURN

.GETFIL:
	.PARSE CMD "  " NULL FILE TMPS NULL
	.TEST FILE
	.IF <STRLEN> GT 3 .GOTO GF2
.;	.ONERR GF2
.;	.SETN TMPN 'FILE'.
	.SETS TST FILE[1:1]
	.IF TST GT "9" .GOTO GF2
	.SETS FILE TMPS
.GF2:	.ONERR MAIN0
	.TEST FILE
	.IFF <ALPHAN> .GOTO GF3
	.IF <STRLEN> EQ 0 .GOTO GF3
	.IF <STRLEN> GT 7 .GOTO GF3
	.RETURN
.GF3:	.ASKS [1:7] FILE  Mail folder:
	.IFF <ALPHAN> .GOTO GF3
	.RETURN

.DEL:
	.GOSUB GETNUM
	.IF MAILN = 0 .RETURN
	.TESTFILE 'CURFIL''MAILN'.MAI;
	.IF <FILERR> <> 1 .GOTO MFAIL
; Deleting #'MAILN'
	.ENABLE QUIET
	PIP MAIL.TMP/RE/NV='CURFIL''MAILN'.MAI;0
	.DISABLE QUIET
	.RETURN

.DIR:
	.SETF HEAD
	.SETN MAILN 0.
.DIR1:
	.INC MAILN
	.IF MAILN GE NEXMSG .GOTO DIR4
	.TESTFILE 'CURFIL''MAILN'.MAI;0
	.IF <FILERR> = 1 .GOTO DIR2
	.GOTO DIR1

.DIR2:	.ENABLE LOWERCASE
	.SETS LINE1 " --From_line_too_long--"
	.SETS LINE2 " --Subject_too_long--"
	.OPENR 'CURFIL''MAILN'.MAI;0
	.ONERR DIR2A
	.READ LINE1
.DIR2A:	.ONERR DIR2B
	.READ NULL
.DIR2B:	.ONERR DIR2C
	.READ LINE2
.DIR2C:	.CLOSE
	.ONERR MAIN0
	.IFT HEAD .GOTO DIR3
;'ESCHOM''ESCCLR'						File folder: 'CURFIL'
;  #  From                    Date       Subject
	.SETT HEAD
.DIR3:	.SETS NUM "'MAILN'"
	.TEST NUM
	.SETS NULL BLANKS[1:3.-<STRLEN>]
	.SETS NUM "'NULL'"+"'NUM'"
	.PARSE LINE1 "   " NULL LINE1 DATE
	.TEST LINE1
	.IF <STRLEN> GT 70. .SETS LINE1 LINE1[<STRLEN>-70.:<STRLEN>]
 .PARSE LINE1 ":" N1 S2 N3 S4 N5 S6 N7 S8 N9 S10 N11 S12 N13 S14 N15 S16 N17
	.SETN FOO1 <STRLEN>-0.
	.IF FOO1 LT 5 .GOTO DIR3B
	.SETN FOO2 <STRLEN>-2.
	.SETS LINE1 N'FOO2'+"::"+N'FOO1'
.DIR3B:	.TEST LINE1
	.SETS NULL ""
	.IF <STRLEN> GT 23. .SETS LINE1 LINE1[<STRLEN>-23.:<STRLEN>]
	.IF <STRLEN> LT 23. .SETS NULL BLANKS[1:23.-<STRLEN>]
	.SETS LINE1 "'LINE1'"+"'NULL'"
	.TEST DATE
	.IF <STRLEN> LT 18. .SETS DATE BLANKS 
	.TEST DATE
	.SETS DATE DATE[<STRLEN>-17.:<STRLEN>]
	.SETS DATE DATE[1:9.]
	.PARSE LINE2 " " NULL LINE2
	.TEST LINE2
	.IF <STRLEN> > 38. .SETS LINE2 LINE2[1:38.]
;'NUM'  'LINE1' 'DATE'  'LINE2'
	.DISABLE LOWERCASE
	.GOTO DIR1

.DIR4:	.IFF HEAD ; No messages found in 'CURFIL'
	.RETURN

.COP:
	.GOSUB GETNUM
	.IF MAILN EQ 0. .SETN MAILN 'CURMSG'.
	.TESTFILE 'CURFIL''MAILN'.MAI;
	.IF <FILERR> <> 1 .GOTO MFAIL
; Copying message #'MAILN'
	.SETS SW ""
	.GOTO FIL2

.FIL:
	.GOSUB GETNUM
	.IF MAILN EQ 0. .SETN MAILN 'CURMSG'.
	.TESTFILE 'CURFIL''MAILN'.MAI;
	.IF <FILERR> <> 1 .GOTO MFAIL
; Filing message #'MAILN'
	.SETS SW "/RE"
.FIL2:	.GOSUB GETFIL
	.IF MAILN = 0 .RETURN
	.ENABLE QUIET
	PIP MAIL.TMP'SW'/NV='CURFIL''MAILN'.MAI;0
	.SETS SAVFIL CURFIL
	.SETN SAVTOP NEXMSG
	.SETS CURFIL FILE
	.IF SAVFIL NE CURFIL .GOSUB TOP
	PIP 'FILE''NEXMSG'.MAI/RE/NV=MAIL.TMP;0
	.DISABLE QUIET
; Filed as message #'NEXMSG' in folder 'FILE'
	.IF SAVFIL EQ CURFIL .SET NEXMSG NEXMSG+1.
	.SETS CURFIL SAVFIL
	.SETN NEXMSG SAVTOP
.;	.GOSUB TOP
	.RETURN
.EXT:
	.GOSUB GETNUM
	.IF MAILN = 0 .RETURN
; Extracting copy of message #'MAILN'
	.SETS SW ""
	.ASKS FILE   Destination file spec
	.ENABLE QUIET
	PIP 'FILE''SW'/NV='CURFIL''MAILN'.MAI;0
	.DISABLE QUIET	
	.RETURN

.FOR:
	.GOSUB GETNUM
; Forwarding message #'MAILN'
	.ENABLE QUIET
	SEND 'CURFIL''MAILN'.MAI;0
	.DISABLE QUIET
	.RETURN

.HEL:
	.PARSE CMD "  " NULL TOPIC
	.TEST TOPIC
	.IF <STRLEN> EQ 0 .SETS TOPIC ""
;'ESCHOM''ESCCLR'
	.ENABLE QUIET
	HELP/MAI 'TOPIC'
	.DISABLE QUIET
	.RETURN

.PUR:
	.ENABLE QUIET
	PIP MAIL.TMP;*/DE
	.DISABLE QUIET
	.RETURN

.REA:
	.GOSUB GETNUM
	.IF MAILN = 0 ; Bad message number 'SMAIL'
	.IF MAILN = 0 .RETURN
	.TESTFILE 'CURFIL''MAILN'.MAI;
	.IF <FILERR> <> 1 .GOTO MFAIL
;'ESCHOM''ESCCLR'   						File folder: 'CURFIL' #'MAILN'
	.ENABLE QUIET
	PIP TI:='CURFIL''MAILN'.MAI;0/NM
	.DISABLE QUIET
	.RETURN

.MFAIL:
; No such message 'MAILN'
	.RETURN

.MAIN:
	.SETS TST P1[1:3]
	.IF TST EQ "MAI" .SETS P1 P2
	.IF TST EQ "MAI" .SETS P2 P3
	.SETS CMD ""
	.IF P1 NE "" .SETT MCR
	.IFT MCR .SETS CMD P1+" "+P2
	.SETS TST CMD[1:3]
	.IF TST EQ "CHE" .GOTO CHE
	.IF TST EQ "CHR" .GOTO MAIN5
	.IFT MCR .GOSUB TOP
	.IFF MCR .GOSUB CHE
	.SETT PASS2
	.TESTFILE MAIL1.TXT
	.IF <FILERR> EQ 1 .GOSUB CON
	.TEST CMD
	.IF <STRLEN> >=3 .GOTO MAIN1
.MAIN0:
;  
	.ONERR MAIN0
	.DISABLE QUIET
	.DISABLE LOWERCASE
	.ASKS CMD Command 
	.TEST CMD
	.IF <STRLEN> = 0 .GOTO REANXT
	.IF <STRLEN> >= 3. .GOTO MAIN1
.MAIN01:
; Unknown command 'CMD'
	.IFT MCR .EXIT
	.GOTO MAIN0

.MAIN1:	.SETN I 1.
	.SETS TST CMD[1:3]
.MAIN3:
	.SETS NULL CMD1[I:I+2.]
	.SETN I I+3.
	.IF NULL = "." .GOTO MAIN4
	.IF TST <> NULL .GOTO MAIN3
	.GOSUB 'TST'	
	.IFT MCR .EXIT
	.GOTO MAIN0

.MAIN4:	.SETN I 1.
.MAIN4A:
	.SETS NULL CMD2[I:I+2.]
	.SETN I I+3.
	.IF NULL = "." .GOTO MAIN01
	.IF TST <> NULL .GOTO MAIN4A
.MAIN5:	.GOSUB 'TST'	
	.IFT MCR .EXIT
	.GOTO MAIN0

.REANXT:
	.IFF FNEX .DEC CURMSG
	.IFF FNEX .SETT FNEX
	.GOSUB NEX
	.GOTO MAIN0
.CHR:
	.SETT CHRFLG
	.SETS CMD ""
	.GOSUB CHE
	.SETT PASS2
	.IFT CHRFLG .GOSUB REA
	.SETF CHRFLG
	.RETURN

.CHE:
	.TESTFILE MAIL.TXT
	.IF <FILERR> NE 1 .GOTO NOMAIL
;  New Mail received
	.IFT CHRFLG .SETF MCR
	.IFT MCR .GOTO EXI
	.IFF PASS2 .GOSUB TOP
	.ENABLE QUIET
	PIP 'CURFIL''NEXMSG'.MAI/RE=MAIL.TXT;-1
	.DISABLE QUIET
; New message #'NEXMSG'
	.IF NEXMSG > LIMIT ; ** Warning! Message number exceeds normal limit. **
	.SETN CURMSG NEXMSG
	.INC NEXMSG
	.SETF FNEX
.20:
	.TESTFILE MAIL.TXT
	.IF <FILERR> NE 1 .RETURN
	.ENABLE QUIET
	PIP 'CURFIL''NEXMSG'.MAI/RE=MAIL.TXT;-1
	.DISABLE QUIET
; New message #'NEXMSG'
	.IF NEXMSG > LIMIT ; ** Warning! Message number exceeds normal limit. **
	.INC NEXMSG
	.GOTO 20

.NOMAIL:
; No new mail received
	.IFT CHRFLG .SETF CHRFLG
	.IFT MCR .GOTO EXI
	.IFF PASS2 .GOSUB TOP
	.RETURN

.REP:
.SEN:
	.PARSE CMD " " NULL FILE
	.ENABLE QUIET
	.IF FILE = "EDIT" .GOTO SENEDT
	.IF FILE = "LAST" .GOTO SEN3
	.IF FILE = "MEMO" .GOTO SEN4
	SEN 'FILE'
	.DISABLE QUIET
	.RETURN
.SENEDT:
	.IF $EDIT EQ "" .GOTO SENNOE
	'$EDIT' MAILEDIT.TMP
	PIP MAIL.BAK/NV=MAILEDIT.TMP
	PIP MAILEDIT.TMP;*/DE
.SEN3:	SEN MAIL.BAK
	.ASK Q  Delete MAIL.BAK
	.IFT Q PIP MAIL.BAK;0/DE/NM
	.DISABLE QUIET
	.RETURN

.SEN4:	.IF $MEMO EQ "" .GOTO NOMEMO
	.TESTFILE '$MEMO'
	.IF <FILERR> NE 1 .GOTO MEMOER
	PIP MAILEDIT.TMP/NV='$MEMO'
	.GOTO SENEDT

.SENNOE: .DISABLE QUIET
	; No Editor defined ($EDIT)
	.RETURN
.NOMEMO: .DISABLE QUIET
	; No memo template file defined ($MEMO)
	.RETURN
.MEMOER: .DISABLE QUIET
	; Memo file not found '$MEMO'
	.RETURN

.PRI:
	.GOSUB GETNUM
	.IF MAILN = 0 .RETURN
	.TESTFILE 'CURFIL''MAILN'.MAI;
	.IF <FILERR> <> 1 .GOTO MFAIL
; Message #'MAILN' being spooled
	.ENABLE QUIET
	PRI 'CURFIL''MAILN'.MAI;0
	.DISABLE QUIET
	.RETURN


.SQU:	.; SQUEEZE SAVED MAIL
; Mail being squeezed
	.SETN N 1.
	.SETN NEXMSG 1.
.SQU1:
	.TESTFILE 'CURFIL''N'.MAI
	.IF <FILERR> EQ 1 .GOTO SQU2
	.IF N GE LIMIT .GOTO SQU4
	.INC N
	.GOTO SQU1
.SQU2:	.IF N = NEXMSG .GOTO SQU3
	.ENABLE QUIET
	PIP 'CURFIL''NEXMSG'.MAI/NV/RE='CURFIL''N'.MAI
	.DISABLE QUIET
.SQU3:	.INC N
	.INC NEXMSG
	.IF N LE LIMIT .GOTO SQU1
; WARNING - Message limit reached in SQUEEZE
.SQU4:	.SETN CURMSG NEXMSG-1
	.SETF FNEX
	.RETURN

.TOP:	.SETN N 1.
	.SETN GAP 0.
	.SETN NEXMSG 1.
.TOP1:
	.TESTFILE 'CURFIL''N'.MAI
	.IF <FILERR> NE 1 .GOTO TOP3
	.SETN NEXMSG N+1.
	.SETN GAP 0.
.TOP2:
	.INC N
	.GOTO TOP1
.TOP3:
	.INC GAP
	.IF GAP GE GAPLIM .RETURN
	.GOTO TOP2

.NEX:
	.INC CURMSG
	.GOSUB REA
	.RETURN

.BAC:
	.DEC CURMSG
	.IF CURMSG < 1 .SETN CURMSG 1.
	.GOSUB REA
	.RETURN

.UND:
	.ENABLE QUIET
	PIP 'CURFIL''NEXMSG'.MAI/NV/RE=MAIL.TMP;0
	.DISABLE QUIET
; Restored to #'NEXMSG'
	.IF NEXMSG > LIMIT ; ** Warning! Message number exceeds normal limit. **
	.INC NEXMSG
	.RETURN
.NOD:
; Node path data file - LB:[1,2]MAILNODE.DAT
	.ENABLE QUIET
	PIP TI:=LB:[1,2]MAILNODE.DAT
	.DISABLE QUIET
	.RETURN
.USE:
; Mail user file - LB:[1,2]MAILUSER.DAT
	.ENABLE QUIET
	PIP TI:=LB:[1,2]MAILUSER.DAT
	.DISABLE QUIET
	.RETURN

.ACT:
	.ENABLE QUIET
	NCP SHO ACT NODE
	.DISABLE QUIET
	.RETURN

.NTD:	NTD '$NODE'
	.RETURN
.LAS:
	.SETN CURMSG NEXMSG-1.
	.GOSUB REA
	.RETURN
.STA:
	.SETN TMP2 NEXMSG-1.
; Current folder is 'CURFIL'  Message is #'CURMSG'  Highest is #'TMP2'
	.RETURN

.OPE:
	.PARSE CMD " " NULL CURFIL NULL
	.TEST CURFIL
	.IF <STRLEN> = 0 .ASKS [1:7] CURFIL  Mail folder name
	.GOSUB TOP
	.SETN CURMSG NEXMSG-1
	.SETF FNEX
	.GOTO STA

.CAT:
	.ENABLE QUIET
	.;PIP *.MAI/BR
	SRD *.MAI/NA
	.DISABLE QUIET
	.RETURN

.CON: .; Conversion from 2.x to 3.0
; Converting Mail files to Version 3.
	.SETN N 1.
.CON1:
	.TESTFILE MAIL'N'.TXT
	.IF <FILERR> EQ 1 .GOTO CON2
	.IF N GE LIMIT .GOTO CON4
	.INC N
	.GOTO CON1
.CON2:	PIP 'CURFIL''NEXMSG'.MAI/NV/RE=MAIL'N'.TXT
.CON3:	.INC N
	.INC NEXMSG
	.IF N GE LIMIT ; WARNING - Message limit reached in CONVERT
	.GOTO CON1
.CON4:	.SETN CURMSG NEXMSG-1
	.SETF FNEX
	.RETURN
.MCR:
	.PARSE CMD "   " NULL TST
	.IF TST EQ "" .ASKS TST MCR>
.MCR2:
	.ONERR MCR3
	'TST'
.MCR3:	.ASKS TST MCR>
	.IF TST NE "" .GOTO MCR2
	.RETURN

.VER:
; Version 3.17  Last Edit 4/2/81
; SEND MEMO added, Editor for SEND EDIT settable in $EDIT
; Default editor: EDT, Default memo template, MEMO.TXT
	.RETURN

.EXI:
	.ENABLE QUIET
	.TESTFILE MAIL.TMP
	.IF <FILERR> EQ 1 PIP MAIL.TMP;*/DE/NM
.;	.DISABLE QUIET
	.ENABLE LOWERCASE
.EXIT:	.EXIT
