;;; Primitive ATSIGN CHAOS  DLW 1/26/78 -*-Fundamental-*-

TITLE ATSIGN CHAOS

;;; This program is loaded by the system when an RFC is received and nobody
;;; else is listening.

	A=1
	B=2
	C=3
	D=4
	E=5

	DSKI==1			; Channel for loading server.

.INSRT SYSTEM;CHSDEF >

DEBUG:	0			; -1 => Debug mode.
DEFNAM:	SIXBIT /DFAULT/		; FN2 of the default server (if CHAOS <foo> is not found).
PBUF:	BLOCK %CPMXW		; Packet buffer.

GO:	.CALL [ SETZ		; Get a pending RFC.
		SIXBIT /CHAOSQ/
		SETZI PBUF]
	 JSR LOGOUT
	MOVE A,[440800,,PBUF+%CPKDT]	; First byte in packet.
	MOVE B,[440600,,D]
	MOVEI D,0
	LDB E,[$CPKNB+PBUF]	; The NBYTES field of the packet.
	CAILE E,6		; If > 6, usae 6 instead.
	 MOVEI E,6
CHLOOP:	ILDB C,A		; Convert 8-bit codes from packet into a sixbit word
	CAIN C,40		; in D.
	 JRST ENDLUP
	CAIL C,"a
	 CAILE C,"z
	  SUBI C,40
	IDPB C,B
	SOJG E,CHLOOP
ENDLUP:	MOVE 0,D		;Contact name, sixbitified, is passed in in AC0
	.SUSET [.SSNAME,,D]	;Also pass it in the sname, for debugging
	.CALL [	SETZ		; Open DEVICE;CHAOS <conn>
		SIXBIT /OPEN/
		[.UII,,DSKI]
		[SIXBIT /DSK/]
		[SIXBIT /CHAOS/]
		D
		SETZ [SIXBIT /DEVICE/]]
	 JRST [	CAMN D,DEFNAM	; Were we trying the default server?
		 JSR LOGOUT	; Yes, lose big!
		MOVE D,DEFNAM	; No, try loading the default server.
		JRST .-1]
	MOVE 17,[ACBLK,,1]	; Load it in and start it.
	BLT 17,ACBLKL
	JRST 1

ACBLK:	.CORE 0			;1  - vital to clear core!!
	 .LOSE			;2
	.CALL 7			;3
	 .LOGOUT 1,		;4
	.IOT DSKI,17		;5
	JRST (17)		;6
	SETZ			;7
	SIXBIT /LOAD/		;10
	MOVEI %JSELF		;11
	SETZI DSKI		;12
ACBLKL=.-ACBLK

LOGOUT:	0
	SKIPE DEBUG
	 .VALUE
	.LOGOUT 1,

	END GO
