package pop

import (
	"git.loafle.net/overflow/overflow_probe/matcher/packet"
	"git.loafle.net/overflow/overflow_probe/model/scaninfo"
)

const (
	COMPARE_STR = "+OK"
)

type POPMatcher struct {
	sendPackets []*packet.Packet
}

func (p *POPMatcher) Match(index int, packet *packet.Packet, info scaninfo.ServiceScanInfo) bool {

	switch index {
	case 0:
		fallthrough
	case 1:

		recvStr := string(packet.Buffer)

		if len(recvStr) < 3 {
			return false
		}

		compareStr := recvStr[0:3]

		if compareStr == COMPARE_STR {
			return true
		}

	}

	return false
}

func (p *POPMatcher) PacketCount() int {
	return len(p.sendPackets)
}
func (p *POPMatcher) Packet(index int) *packet.Packet {
	return p.sendPackets[index]
}
func (p *POPMatcher) ServiceName() string {
	return "POPMatcher"
}

func (p *POPMatcher) IsError(index int, packet *packet.Packet, info scaninfo.ServiceScanInfo) bool {
	return false
}
func (p *POPMatcher) IsNoResponse(index int) bool {

	return false
}

func (p *POPMatcher) IsPrePacket() bool {
	return true
}

func NewPOPMatcher() *POPMatcher {

	pm := POPMatcher{}

	reqStr := "QUIT\r\n"
	byte := make([]byte, len(reqStr))
	copy(byte[:], reqStr)
	pm.sendPackets = append(pm.sendPackets, packet.NewPacket(byte, len(reqStr)))

	return &pm

}
