service_matcher-go/imap/imap.go

89 lines
1.3 KiB
Go
Raw Normal View History

2018-08-13 07:48:32 +00:00
package imap
import (
2018-08-15 07:17:18 +00:00
osm "git.loafle.net/overflow/service_matcher-go"
2018-08-13 07:48:32 +00:00
)
const (
PRE_COMPARE_STR = "* OK"
SEND_COMPARE_STR = "* BYE"
)
type IMAPMatcher struct {
2018-08-15 07:17:18 +00:00
osm.Matchers
2018-08-13 07:48:32 +00:00
}
func (i *IMAPMatcher) Key() string {
return "IMAP"
}
2018-09-03 06:42:56 +00:00
func (i *IMAPMatcher) Name() string {
2018-08-13 07:48:32 +00:00
return "IMAP"
}
2018-09-03 06:42:56 +00:00
func (i *IMAPMatcher) Meta() osm.Metadata {
return nil
}
2018-08-13 07:48:32 +00:00
func (i *IMAPMatcher) IsPrePacket() bool {
return true
}
func (i *IMAPMatcher) HasResponse(index int) bool {
return true
}
2018-08-15 07:17:18 +00:00
func (i *IMAPMatcher) IsError(info osm.MatchInfo, index int, packet *osm.Packet) bool {
2018-08-13 07:48:32 +00:00
return false
}
2018-09-03 06:42:56 +00:00
func (i *IMAPMatcher) Match(info osm.MatchInfo, index int, packet *osm.Packet) error {
2018-08-13 07:48:32 +00:00
switch index {
case 0:
2018-09-03 06:42:56 +00:00
recvStr := string(packet.Bytes())
2018-08-13 07:48:32 +00:00
if len(recvStr) < 3 {
2018-09-03 06:42:56 +00:00
return osm.NotMatchedError()
2018-08-13 07:48:32 +00:00
}
compareStr := recvStr[0:4]
if compareStr == PRE_COMPARE_STR {
2018-09-03 06:42:56 +00:00
return nil
2018-08-13 07:48:32 +00:00
}
case 1:
2018-09-03 06:42:56 +00:00
recvStr := string(packet.Bytes())
2018-08-13 07:48:32 +00:00
if len(recvStr) < 5 {
2018-09-03 06:42:56 +00:00
return osm.NotMatchedError()
2018-08-13 07:48:32 +00:00
}
compareStr := recvStr[0:5]
if compareStr == SEND_COMPARE_STR {
2018-09-03 06:42:56 +00:00
return nil
2018-08-13 07:48:32 +00:00
}
}
2018-09-03 06:42:56 +00:00
return osm.NotMatchedError()
2018-08-13 07:48:32 +00:00
}
2018-08-15 07:17:18 +00:00
func NewMatcher() osm.Matcher {
2018-08-13 07:48:32 +00:00
m := &IMAPMatcher{}
reqStr := "A0001 LOGOUT\r\n"
byte := make([]byte, len(reqStr))
copy(byte[:], reqStr)
2018-08-15 07:17:18 +00:00
m.AddPacket(osm.NewPacket(byte, len(reqStr)))
2018-08-13 07:48:32 +00:00
return m
}