A2osX/BIN/IRC.S.txt
2020-06-22 07:59:53 +02:00

749 lines
13 KiB
Plaintext
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

NEW
AUTO 3,1
.LIST OFF
.OP 65C02
.OR $2000
.TF bin/irc
*--------------------------------------
.INB inc/macros.i
.INB inc/a2osx.i
.INB inc/eth.i
.INB inc/libtcpip.i
*--------------------------------------
TIMEOUT.MAX .EQ 200 20 sec.
MSGSIZE .EQ 1024
*--------------------------------------
* https://tools.ietf.org/html/rfc2812#section-3.3
*--------------------------------------
.DUMMY
.OR ZPBIN
ZS.START
ZPIPCfgPtr .BS 2
hSocket .BS 1
ZPUsrBufPtr .BS 2
hUsrBuf .BS 1
ZPLineBufPtr .BS 2
hLineBuf .BS 1
TextPtr .BS 1
TextLen .BS 1
bEscMode .BS 1
ZPMsgPtr .BS 2
hMsg .BS 1
hReq .BS 1
ZPReqPtr .BS 2
hRep .BS 1
ZPRepPtr .BS 2
TimeOut .BS 1
ZS.END
.ED
*--------------------------------------
* File Header (16 Bytes)
*--------------------------------------
CS.START cld
jmp (.1,x)
.DA #$61 6502,Level 1 (65c02)
.DA #1 BIN Layout Version 1
.DA #S.PS.F.EVENT S.PS.F
.DA #0
.DA CS.END-CS.START Code Size (without Constants)
.DA DS.END-DS.START Data Segment Size
.DA #64 Stack Size
.DA #ZS.END-ZS.START Zero Page Size
.DA 0
*--------------------------------------
.1 .DA CS.INIT
.DA CS.RUN
.DA CS.DOEVENT
.DA CS.QUIT
L.LIBTCPIP .DA LIBTCPIP
L.SA.LOCAL .DA SA.LOCAL
L.SA.REMOTE .DA SA.REMOTE
L.SA.REMOTE.AD .DA SA.REMOTE+S.SOCKADDR.ADDR
L.MSG.IPKO .DA MSG.IPKO
L.MSG.USAGE .DA MSG.USAGE
L.MSG.UNKNOWN .DA MSG.UNKNOWN
L.MSG.HOSTOK .DA MSG.HOSTOK
L.MSG.SKTKO .DA MSG.SKTKO
L.MSG.SKTOK .DA MSG.SKTOK
L.MSG.SKTERR .DA MSG.SKTERR
L.MSG.NICK .DA MSG.NICK
L.MSG.USER .DA MSG.USER
L.MSG.JOIN .DA MSG.JOIN
L.MSG.TOPBAR .DA MSG.TOPBAR
L.MSG.STATUSBAR .DA MSG.STATUSBAR
L.IRC.NICK .DA IRC.NICK
L.IRC.USER .DA IRC.USER
L.IRC.JOIN .DA IRC.JOIN
L.IRC.JOINKEY .DA IRC.JOINKEY
L.SEQ.INIT .DA SEQ.INIT
L.SEQ.BAR .DA SEQ.BAR
L.SEQ.PRINTMSG .DA SEQ.PRINTMSG
L.SEQ.TXTCLR .DA SEQ.TXTCLR
L.SEQ.RESET .DA SEQ.RESET
J.ESC .DA CS.RUN.CHARIN.LEFT
.DA CS.RUN.CHARIN.DOWN
.DA CS.RUN.CHARIN.UP
.DA CS.RUN.CHARIN.RIGHT
.DA 0
*--------------------------------------
CS.INIT >LDYA L.LIBTCPIP
>SYSCALL LoadLib
bcs .9
sta hLIBTCPIP
.9 rts
*--------------------------------------
CS.RUN >LIBCALL hLIBTCPIP,LIBTCPIP.GETCFG is TCPIP loaded ?
>STYA ZPIPCfgPtr
lda (ZPIPCfgPtr) Configured ?
bmi CS.RUN.IPOK
>LDYA L.MSG.IPKO
>SYSCALL PutS
lda #E.SYN
sec
rts
CS.RUN.IPOK ldy #S.PS.ARGC
lda (pPS),y
cmp #3
bcc .9
ldy #S.IPCFG.IP+3
ldx #3
.1 lda (ZPIPCfgPtr),y
sta SA.LOCAL+S.SOCKADDR.ADDR,x
dey
dex
bpl .1
lda #TIMEOUT.MAX
sta TimeOut
.2 >SLEEP
>PUSHW L.SA.REMOTE.AD
lda #1
>SYSCALL ArgV
>PUSHYA
>LIBCALL hLIBTCPIP,LIBTCPIP.HST.GETBYNAME
bcc CS.RUN.HOSTOK
lda TimeOut
bne .2
>PUSHW L.MSG.UNKNOWN
lda #1
>SYSCALL ArgV
>PUSHYA
>PUSHBI 2
>SYSCALL PrintF
bra .99
.9 >LDYA L.MSG.USAGE
>SYSCALL PutS
.99 lda #E.SYN
sec
rts
CS.RUN.HOSTOK lda #2
>SYSCALL ArgV
>SYSCALL atoi
>STYA SA.REMOTE+S.SOCKADDR.PORT
>PUSHW L.MSG.HOSTOK
ldx #0
.1 >PUSHB SA.REMOTE+S.SOCKADDR.ADDR,x
inx
cpx #4
bne .1
>PUSHW SA.REMOTE+S.SOCKADDR.PORT
lda #1
>SYSCALL ArgV
>PUSHYA
>PUSHBI 8
>SYSCALL PrintF
CS.RUN.OPENSKT >PUSHBI 0 no protocol
lda #S.SOCKET.T.STREAM
>LIBCALL hLIBTCPIP,LIBTCPIP.Socket
bcs .9
sta hSocket
>PUSHW L.SA.LOCAL
lda hSocket
>LIBCALL hLIBTCPIP,LIBTCPIP.Bind
bcc .2
.9 pha
>LDYA L.MSG.SKTKO
>SYSCALL PutS
pla
sec
.99 rts
.2 lda #TIMEOUT.MAX
sta TimeOut
.3 >SLEEP
>PUSHW L.SA.REMOTE
lda hSocket
>LIBCALL hLIBTCPIP,LIBTCPIP.Connect
bcc .4
cmp #ERR.SKT.NOCONN
bne .9
lda TimeOut
bne .3
lda #ERR.SKT.NOCONN
bra .9
.4 >PUSHW L.MSG.SKTOK
>PUSHBI 0
>SYSCALL PrintF
jsr CS.RUN.GETBUF
bcs .99
jsr CS.RUN.NICK
bcs .99
>SLEEP
jsr CS.RUN.USER
bcs .99
>SLEEP
jsr CS.RUN.JOIN
bcs .99
CS.RUN.LOOP >SLEEP
>PUSHW MSGSIZE
>PUSHW ZPMsgPtr
lda hSocket
>SYSCALL fgets
bcc .1
tay
beq .2 NO DATA
bra .9
.1 jsr CS.RUN.REQ
bcs .9
.2 ldy #S.PS.hStdIn
lda (pPS),y
>SYSCALL feof
bcs CS.RUN.ERR
tay
bne CS.RUN.LOOP EOF = true, no char from STDIN
>SYSCALL GetChar
bcs .9
jsr CS.RUN.CHARIN
bcc CS.RUN.LOOP
.9
CS.RUN.ERR php
pha
>PUSHW L.SEQ.RESET
>PUSHBI 0
>SYSCALL PrintF
pla
plp
rts
*--------------------------------------
CS.RUN.GETBUF >LDYAI MSGSIZE
>SYSCALL GetMem
bcs .9
stx hMsg
>STYA ZPMsgPtr
>LDYAI 256
>SYSCALL GetMem
bcs .9
stx hUsrBuf
>STYA ZPUsrBufPtr
>LDYAI 256
>SYSCALL GetMem
bcs .9
stx hLineBuf
>STYA ZPLineBufPtr
.9 rts
*--------------------------------------
CS.RUN.SCRSETUP >PUSHW L.SEQ.INIT
>PUSHBI 0
>SYSCALL PrintF
bcs .9
jsr CS.RUN.TOPUPDATE
bcs .9
jsr CS.RUN.STATUSPDATE
bcs .9
jsr CS.RUN.TXTCLR
.9 rts
*--------------------------------------
CS.RUN.TOPUPDATE
>PUSHW ZPLineBufPtr
>PUSHW L.MSG.TOPBAR
>PUSHB #K.VER
>PUSHB /K.VER
lda #1
>SYSCALL ArgV
>PUSHYA
lda #2
>SYSCALL ArgV
>PUSHYA
lda #3
>SYSCALL ArgV
>PUSHYA
>PUSHBI 8
>SYSCALL SPrintF
>PUSHW L.SEQ.BAR
>PUSHW ZPLineBufPtr
>PUSHBI 2
>SYSCALL PrintF
rts
*--------------------------------------
CS.RUN.PRINTMSG >PUSHW L.SEQ.PRINTMSG
lda #3
>SYSCALL ArgV
>PUSHYA
lda #1
>SYSCALL ArgV
>PUSHYA
>PUSHBI 4
>SYSCALL PrintF
rts
*--------------------------------------
CS.RUN.STATUSPDATE
>PUSHW ZPLineBufPtr
>PUSHW L.MSG.STATUSBAR
lda #4
>SYSCALL ArgV
>PUSHYA
>PUSHW ZPUsrBufPtr
>PUSHBI 4
>SYSCALL SPrintF
>PUSHW L.SEQ.BAR
>PUSHW ZPLineBufPtr
>PUSHBI 2
>SYSCALL PrintF
rts
*--------------------------------------
CS.RUN.TXTCLR lda #0
sta (pData)
stz TextPtr
stz TextLen
stz bEscMode
>PUSHW L.SEQ.TXTCLR
>PUSHBI 0
>SYSCALL PrintF
rts
*--------------------------------------
CS.RUN.NICK >LDYA L.MSG.NICK
>SYSCALL PutS
>PUSHW ZPMsgPtr
>PUSHW L.IRC.NICK
lda #3 nickname
>SYSCALL ArgV
>PUSHYA
>PUSHBI 2
>SYSCALL SPrintF
bcs .9
>PUSHW ZPMsgPtr
lda hSocket
>SYSCALL fputs
>DEBUG
bcs .9
.1 >SLEEP
>PUSHW MSGSIZE
>PUSHW ZPMsgPtr
lda hSocket
>SYSCALL fgets
bcc .2
tay
beq .1 No Data
.9 rts
.2 jsr CS.RUN.SKIPPFX
ldx #$ff
dey
.3 inx
iny
lda IRC.REP.NOIDENT,x
beq .8
cmp (ZPRepPtr),y
beq .3
jsr .8
bra .1
.8
* clc
rts
*--------------------------------------
CS.RUN.USER >LDYA L.MSG.USER
>SYSCALL PutS
>PUSHW ZPMsgPtr
>PUSHW L.IRC.USER
lda #3 nickname
>SYSCALL ArgV
>PUSHYA
lda #3 nickname
>SYSCALL ArgV
>PUSHYA
>PUSHBI 4
>SYSCALL SPrintF
bcs .9
>PUSHW ZPMsgPtr
lda hSocket
>SYSCALL fgets
bcs .9
.1 >SLEEP
>PUSHW MSGSIZE
>PUSHW ZPMsgPtr
lda hSocket
>SYSCALL fgets
bcc .2
tay
beq .1 No Frame
.9 rts
.2 jsr CS.RUN.SKIPPFX
ldx #$ff
dey
.3 inx
iny
lda IRC.REP.001,x
beq .8
cmp (ZPRepPtr),y
beq .3
jsr .8
bra .1
.8 lda hRep
>SYSCALL FreeMem
* clc
rts
*--------------------------------------
CS.RUN.JOIN >LDYA L.MSG.JOIN
>SYSCALL PutS
>PUSHW ZPMsgPtr
lda #5 key
>SYSCALL ArgV
bcs .1
>PUSHW L.IRC.JOINKEY
lda #4 channel
>SYSCALL ArgV
>PUSHYA
lda #5 key
>SYSCALL ArgV
>PUSHYA
>PUSHBI 4
bra .2
.1 >PUSHW L.IRC.JOIN
lda #4 channel
>SYSCALL ArgV
>PUSHYA
>PUSHBI 2
.2 >SYSCALL SPrintF
bcs .9
>PUSHW ZPMsgPtr
lda hSocket
>SYSCALL fputs
bcs .9
.3 >SLEEP
>PUSHW ZPMsgPtr
lda hSocket
>SYSCALL fgets
bcc .4
tay
beq .3 No Frame
.9 rts
.4
bra .8
jsr CS.RUN.SKIPPFX
ldx #$ff
dey
.5 inx
iny
lda IRC.REP.JOIN,x
beq .8
cmp (ZPRepPtr),y
beq .5
jsr .8
bra .4
.8 lda hRep
>SYSCALL FreeMem
* clc
rts
*--------------------------------------
CS.RUN.REQ
clc
rts
*--------------------------------------
CS.RUN.SKIPPFX ldy #S.TCP
lda (ZPRepPtr),y
cmp #':'
bne .8
.1 iny
lda (ZPRepPtr),y
cmp #C.SPACE
bne .1
iny
.8 clc
rts
*--------------------------------------
CS.RUN.CHARIN bit bEscMode
bpl .4
cmp #'['
beq .9
stz bEscMode
ldx #EscChars.Cnt-1
.1 cmp EscChars,x
beq .2
dex
bpl .1
.9 rts
.2 txa
asl
tax
jmp (J.ESC,x)
.4 cmp #C.ESC
bne .5
sec
ror bEscMode
clc
rts
.5 cmp #C.SPACE
bcc CS.RUN.CHARIN.CTRL
cmp #C.DEL
bne CS.RUN.CHARIN.INS
lda TextLen
beq .9
lda TextPtr
beq .9
lda #C.BS
>SYSCALL PutChar
jmp CS.RUN.CHARIN.DEL
*--------------------------------------
CS.RUN.CHARIN.INS
CS.RUN.CHARIN.CTRL
clc
rts
CS.RUN.CHARIN.LEFT
lda TextPtr
beq .9
dec TextPtr
lda #C.BS
>SYSCALL PutChar
.9 rts
CS.RUN.CHARIN.DOWN
CS.RUN.CHARIN.UP
clc
rts
CS.RUN.CHARIN.RIGHT
lda TextPtr
cmp TextLen
beq .9
inc
sta TextPtr
lda #C.FS
>SYSCALL PutChar
.9 rts
*--------------------------------------
CS.RUN.CHARIN.DEL
clc
rts
*--------------------------------------
CS.DOEVENT lda (pEvent)
bpl .9 is it a TIMER event?
lda TimeOut
beq .9
dec TimeOut
.9 sec do not discard TIMER event
rts
*--------------------------------------
CS.QUIT lda hSocket
beq .1
>LIBCALL hLIBTCPIP,LIBTCPIP.Shutdown
.1 lda hMsg
beq .2
>SYSCALL FreeMem
.2 lda hLIBTCPIP
beq .8
>SYSCALL UnloadLib
.8 clc
rts
*--------------------------------------
CS.END
LIBTCPIP .AZ "libtcpip"
hLIBTCPIP .BS 1
MSG.IPKO .AZ "TCP/IP Not Loaded/Configured."
MSG.USAGE .AZ "Usage : IRC <ip|host> <port> <nickname> <#channel> [key]"
MSG.UNKNOWN .AZ "%s: Unknown host\r\n"
MSG.HOSTOK .AZ "Connecting to %d.%d.%d.%d:%D (%s)..."
MSG.SKTKO .AZ "Failed to Open Socket."
MSG.SKTOK .AZ "Connected\r\n(Exit key is Ctrl-T)\r\n"
MSG.SKTERR .AZ "Socket Error : $%h\r\n"
MSG.NICK .AZ "Registering User..."
MSG.USER .AZ "Connecting User..."
MSG.JOIN .AZ "Joining Channel..."
MSG.TOPBAR .AZ "A2osX IRC %d.%d %s:%s %s"
MSG.STATUSBAR .AZ "\e[19;1H%s:%s"
*--------------------------------------
EscChars .AS 'DBAC'
EscChars.Cnt .EQ *-EscChars
EscChars.Remap .DA #C.BS,#C.VT,#C.LF,#C.FS
*--------------------------------------
IRC.MSG .AZ "PING"
IRC.REP.NOIDENT .AZ "NOTICE * :*** No Ident response"
IRC.REP.001 .AZ "001"
IRC.REP.JOIN .AZ "JOIN"
*--------------------------------------
IRC.NICK .AZ "NICK %s\r\n"
IRC.USER .AZ "USER %s 0 * :%s\r\n"
IRC.JOIN .AZ "JOIN %s\r\n"
IRC.JOINKEY .AZ "JOIN %s %s\r\n"
IRC.PRIVMSG .AZ "PRIVMSG %s :%s\r\n"
*--------------------------------------
SEQ.INIT .AZ "\ec\e(B\e)0\e[?7l\e[2;19r"
SEQ.BAR .AZ "\e7\e[7m\e[37;40m%80s\e[0m\e8"
SEQ.PRINTMSG .AZ "\e7\r\n\e[18;1H\e[7m%s:\e[0m%s\e8"
SEQ.TXTCLR .AZ "\e[20;1H\e[K\r\n\e[K\r\n\e[K\e[20;1H"
SEQ.RESET .AZ "\ec"
*--------------------------------------
SA.LOCAL .DA #AF.INET S.SOCKADDR.AF
.BS 1
.BS 4 S.SOCKADDR.ADDR
.BS 2 S.SOCKADDR.PORT
*--------------------------------------
SA.REMOTE .DA #AF.INET S.SOCKADDR.AF
.BS 1
.BS 4 S.SOCKADDR.ADDR
.BS 2
*--------------------------------------
.DUMMY
.OR 0
DS.START
TxtBuf .BS 240
DS.END
.ED
*--------------------------------------
MAN
SAVE usr/src/bin/irc.s
ASM