service_matcher-go/imap/imap.go
2018-09-12 13:26:27 +09:00

89 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) Type() string {
return "MAIL"
}
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
}