Archived
1
0
This repository has been archived on 2023-08-12. You can view files and clone it, but cannot push or open issues or pull requests.

83 lines
1.6 KiB
Go
Raw Normal View History

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"
2017-11-07 14:32:37 -05:00
"log"
2017-11-06 18:12:18 -05:00
"net"
2017-11-07 14:13:17 -05:00
"os/user"
2017-11-07 14:33:41 -05:00
"strings"
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 14:57:05 -05:00
var ErrMessageIsSpam = errors.New("message is spam")
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 15:07:10 -05:00
func (sp *SpamFilter) Enabled(newsgroup string) bool {
return sp.enabled && newsgroup != "ctl"
2017-11-07 13:37:22 -05:00
}
type SpamResult struct {
Err error
IsSpam bool
}
func (sp *SpamFilter) Rewrite(msg io.Reader, out io.WriteCloser, group string) (result SpamResult) {
2017-11-06 18:12:18 -05:00
var buff [65636]byte
2017-11-07 15:07:10 -05:00
if !sp.Enabled(group) {
result.Err = ErrSpamFilterNotEnabled
return
2017-11-07 13:37:22 -05:00
}
var addr *net.TCPAddr
var c *net.TCPConn
var u *user.User
addr, result.Err = net.ResolveTCPAddr("tcp", sp.addr)
if result.Err != nil {
return
2017-11-07 13:37:22 -05:00
}
c, result.Err = net.DialTCP("tcp", nil, addr)
if result.Err != nil {
return
2017-11-06 18:12:18 -05:00
}
u, result.Err = user.Current()
if result.Err != nil {
return
2017-11-07 14:13:17 -05:00
}
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 {
var l string
l, result.Err = r.ReadString(10)
if result.Err != nil {
return
2017-11-07 14:25:42 -05:00
}
2017-11-07 14:33:41 -05:00
l = strings.TrimSpace(l)
2017-11-07 14:57:05 -05:00
if strings.HasPrefix(l, "Spam: True ") {
result.IsSpam = true
2017-11-07 14:57:05 -05:00
}
2017-11-07 14:32:37 -05:00
log.Println("SpamFilter:", l)
2017-11-07 14:33:41 -05:00
if l == "" {
_, result.Err = io.CopyBuffer(out, r, buff[:])
2017-11-07 14:30:34 -05:00
c.Close()
out.Close()
return
2017-11-07 14:25:42 -05:00
}
}
result.Err = ErrSpamFilterFailed
return
2017-11-06 18:12:18 -05:00
}