service_matcher-go/imap/imap.go
crusader 6ce87348ba ing
2018-09-03 15:42:56 +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 (i *IMAPMatcher) Key() string {
return "IMAP"
}
func (i *IMAPMatcher) Name() string {
return "IMAP"
}
func (i *IMAPMatcher) Meta() osm.Metadata {
return nil
}
func (i *IMAPMatcher) IsPrePacket() bool {
return true
}
func (i *IMAPMatcher) HasResponse(index int) bool {
return true
}
func (i *IMAPMatcher) IsError(info osm.MatchInfo, index int, packet *osm.Packet) bool {
return false
}
func (i *IMAPMatcher) Match(info osm.MatchInfo, 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
}