2017-11-06 18:12:18 -05:00
|
|
|
package srnd
|
|
|
|
|
|
|
|
import (
|
2017-11-07 14:07:55 -05:00
|
|
|
"bufio"
|
2017-11-07 13:37:22 -05:00
|
|
|
"errors"
|
2017-11-07 14:13:17 -05:00
|
|
|
"fmt"
|
2017-11-06 18:12:18 -05:00
|
|
|
"io"
|
|
|
|
"net"
|
2017-11-07 14:13:17 -05:00
|
|
|
"os/user"
|
2017-11-06 18:12:18 -05:00
|
|
|
)
|
|
|
|
|
2017-11-07 13:37:22 -05:00
|
|
|
var ErrSpamFilterNotEnabled = errors.New("spam filter access attempted when disabled")
|
2017-11-07 14:30:34 -05:00
|
|
|
var ErrSpamFilterFailed = errors.New("spam filter failed")
|
2017-11-07 13:37:22 -05:00
|
|
|
|
2017-11-06 18:12:18 -05:00
|
|
|
type SpamFilter struct {
|
|
|
|
addr string
|
|
|
|
enabled bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sp *SpamFilter) Configure(c SpamConfig) {
|
|
|
|
sp.enabled = c.enabled
|
|
|
|
sp.addr = c.addr
|
|
|
|
}
|
|
|
|
|
2017-11-07 13:37:22 -05:00
|
|
|
func (sp *SpamFilter) Enabled() bool {
|
|
|
|
return sp.enabled
|
|
|
|
}
|
|
|
|
|
2017-11-06 18:12:18 -05:00
|
|
|
func (sp *SpamFilter) Rewrite(msg io.Reader, out io.WriteCloser) error {
|
|
|
|
var buff [65636]byte
|
2017-11-07 13:37:22 -05:00
|
|
|
if !sp.Enabled() {
|
|
|
|
return ErrSpamFilterNotEnabled
|
|
|
|
}
|
|
|
|
addr, err := net.ResolveTCPAddr("tcp", sp.addr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c, err := net.DialTCP("tcp", nil, addr)
|
|
|
|
if err != nil {
|
2017-11-06 18:12:18 -05:00
|
|
|
return err
|
|
|
|
}
|
2017-11-07 14:13:17 -05:00
|
|
|
u, err := user.Current()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Fprintf(c, "PROCESS SPAMC/1.5\r\nUser: %s\r\n\r\n", u.Username)
|
2017-11-07 13:37:22 -05:00
|
|
|
io.CopyBuffer(c, msg, buff[:])
|
|
|
|
c.CloseWrite()
|
2017-11-07 14:07:55 -05:00
|
|
|
r := bufio.NewReader(c)
|
2017-11-07 14:25:42 -05:00
|
|
|
for {
|
|
|
|
l, err := r.ReadString(10)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-11-07 14:27:52 -05:00
|
|
|
if l == "\n" {
|
|
|
|
r.ReadString(10)
|
2017-11-07 14:30:34 -05:00
|
|
|
_, err = io.CopyBuffer(out, r, buff[:])
|
|
|
|
c.Close()
|
|
|
|
out.Close()
|
|
|
|
return err
|
2017-11-07 14:25:42 -05:00
|
|
|
}
|
|
|
|
}
|
2017-11-07 14:30:34 -05:00
|
|
|
return ErrSpamFilterFailed
|
2017-11-06 18:12:18 -05:00
|
|
|
}
|