85 lines
1.2 KiB
Go
85 lines
1.2 KiB
Go
|
package imap
|
||
|
|
||
|
import (
|
||
|
csm "git.loafle.net/commons/service_matcher-go"
|
||
|
)
|
||
|
|
||
|
const (
|
||
|
PRE_COMPARE_STR = "* OK"
|
||
|
SEND_COMPARE_STR = "* BYE"
|
||
|
)
|
||
|
|
||
|
type IMAPMatcher struct {
|
||
|
csm.Matchers
|
||
|
}
|
||
|
|
||
|
func (i *IMAPMatcher) Key() string {
|
||
|
return "IMAP"
|
||
|
}
|
||
|
|
||
|
func (i *IMAPMatcher) String() string {
|
||
|
return "IMAP"
|
||
|
}
|
||
|
|
||
|
func (i *IMAPMatcher) IsPrePacket() bool {
|
||
|
return true
|
||
|
}
|
||
|
|
||
|
func (i *IMAPMatcher) HasResponse(index int) bool {
|
||
|
return true
|
||
|
}
|
||
|
|
||
|
func (i *IMAPMatcher) IsError(info csm.MatchInfo, index int, packet *csm.Packet) bool {
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
func (i *IMAPMatcher) Match(info csm.MatchInfo, index int, packet *csm.Packet) bool {
|
||
|
|
||
|
switch index {
|
||
|
case 0:
|
||
|
|
||
|
recvStr := string(packet.Buffer)
|
||
|
|
||
|
if len(recvStr) < 3 {
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
compareStr := recvStr[0:4]
|
||
|
|
||
|
if compareStr == PRE_COMPARE_STR {
|
||
|
return true
|
||
|
}
|
||
|
|
||
|
case 1:
|
||
|
|
||
|
recvStr := string(packet.Buffer)
|
||
|
|
||
|
if len(recvStr) < 5 {
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
compareStr := recvStr[0:5]
|
||
|
|
||
|
if compareStr == SEND_COMPARE_STR {
|
||
|
return true
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
func NewMatcher() csm.Matcher {
|
||
|
|
||
|
m := &IMAPMatcher{}
|
||
|
|
||
|
reqStr := "A0001 LOGOUT\r\n"
|
||
|
byte := make([]byte, len(reqStr))
|
||
|
copy(byte[:], reqStr)
|
||
|
|
||
|
m.AddPacket(csm.NewPacket(byte, len(reqStr)))
|
||
|
|
||
|
return m
|
||
|
|
||
|
}
|