service_matcher-go/imap/imap.go
crusader d5bc04d5ee ing
2018-09-03 22:41:28 +09:00

85 lines
1.3 KiB
Go

package imap
import (
osm "git.loafle.net/overflow/service_matcher-go"
)
const (
PRE_COMPARE_STR = "* OK"
SEND_COMPARE_STR = "* BYE"
)
type IMAPMatcher struct {
osm.Matchers
}
func (m *IMAPMatcher) Key() string {
return "IMAP"
}
func (m *IMAPMatcher) Name(matchCtx *osm.MatchCtx) string {
return "IMAP"
}
func (m *IMAPMatcher) IsPrePacket() bool {
return true
}
func (m *IMAPMatcher) HasResponse(matchCtx *osm.MatchCtx, index int) bool {
return true
}
func (m *IMAPMatcher) IsError(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) bool {
return false
}
func (m *IMAPMatcher) Match(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) error {
switch index {
case 0:
recvStr := string(packet.Bytes())
if len(recvStr) < 3 {
return osm.NotMatchedError()
}
compareStr := recvStr[0:4]
if compareStr == PRE_COMPARE_STR {
return nil
}
case 1:
recvStr := string(packet.Bytes())
if len(recvStr) < 5 {
return osm.NotMatchedError()
}
compareStr := recvStr[0:5]
if compareStr == SEND_COMPARE_STR {
return nil
}
}
return osm.NotMatchedError()
}
func NewMatcher() osm.Matcher {
m := &IMAPMatcher{}
reqStr := "A0001 LOGOUT\r\n"
byte := make([]byte, len(reqStr))
copy(byte[:], reqStr)
m.AddPacket(osm.NewPacket(byte, len(reqStr)))
return m
}