forked from saintienn/go-spamc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
514 lines (451 loc) · 13.3 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
package spamc
import (
"bufio"
"errors"
"fmt"
"io"
"net"
"regexp"
"strconv"
"strings"
"time"
)
//Error Types
const EX_OK = 0 //no problems
const EX_USAGE = 64 // command line usage error
const EX_DATAERR = 65 // data format error
const EX_NOINPUT = 66 // cannot open input
const EX_NOUSER = 67 // addressee unknown
const EX_NOHOST = 68 // host name unknown
const EX_UNAVAILABLE = 69 // service unavailable
const EX_SOFTWARE = 70 // internal software error
const EX_OSERR = 71 // system error (e.g., can't fork)
const EX_OSFILE = 72 // critical OS file missing
const EX_CANTCREAT = 73 // can't create (user) output file
const EX_IOERR = 74 // input/output error
const EX_TEMPFAIL = 75 // temp failure; user is invited to retry
const EX_PROTOCOL = 76 // remote error in protocol
const EX_NOPERM = 77 // permission denied
const EX_CONFIG = 78 // configuration error
const EX_TIMEOUT = 79 // read timeout
//map of default spamD protocol errors v1.5
var SpamDError = map[int]string{
EX_USAGE: "Command line usage error",
EX_DATAERR: "Data format error",
EX_NOINPUT: "Cannot open input",
EX_NOUSER: "Addressee unknown",
EX_NOHOST: "Host name unknown",
EX_UNAVAILABLE: "Service unavailable",
EX_SOFTWARE: "Internal software error",
EX_OSERR: "System error",
EX_OSFILE: "Critical OS file missing",
EX_CANTCREAT: "Can't create (user) output file",
EX_IOERR: "Input/output error",
EX_TEMPFAIL: "Temp failure; user is invited to retry",
EX_PROTOCOL: "Remote error in protocol",
EX_NOPERM: "Permission denied",
EX_CONFIG: "Configuration error",
EX_TIMEOUT: "Read timeout",
}
//Default parameters
const PROTOCOL_VERSION = "1.5"
const DEFAULT_TIMEOUT = 10
//Command types
const CHECK = "CHECK"
const SYMBOLS = "SYMBOLS"
const REPORT = "REPORT"
const REPORT_IGNOREWARNING = "REPORT_IGNOREWARNING"
const REPORT_IFSPAM = "REPORT_IFSPAM"
const SKIP = "SKIP"
const PING = "PING"
const TELL = "TELL"
const PROCESS = "PROCESS"
const HEADERS = "HEADERS"
//Learn types
const LEARN_SPAM = "SPAM"
const LEARN_HAM = "HAM"
const LEARN_NOTSPAM = "NOTSPAM"
const LEARN_NOT_SPAM = "NOT_SPAM"
const LEARN_FORGET = "FORGET"
//Test Types
const TEST_INFO = "info"
const TEST_BODY = "body"
const TEST_RAWBODY = "rawbody"
const TEST_HEADER = "header"
const TEST_FULL = "full"
const TEST_URI = "uri"
const TEST_TXT = "text"
//only for parse use !important
const SPLIT = "§"
const TABLE_MARK = "----"
//Types
type Client struct {
ConnTimoutSecs int
ProtocolVersion string
Host string
User string
}
//Default response struct
type SpamDOut struct {
Code int
Message string
Vars map[string]interface{}
}
//Default callback to SpanD response
type FnCallback func(*bufio.Reader) (*SpamDOut, error)
//new instance of Client
func New(host string, timeout int) *Client {
return &Client{timeout, PROTOCOL_VERSION, host, ""}
}
func (s *Client) SetUnixUser(user string) {
s.User = user
}
// Return a confirmation that spamd is alive.
func (s *Client) Ping() (r *SpamDOut, err error) {
return s.simpleCall(PING, []string{})
}
// Just check if the passed message is spam or not and return score
func (s *Client) Check(msgpars ...string) (reply *SpamDOut, err error) {
return s.simpleCall(CHECK, msgpars)
}
//Ignore this message -- client opened connection then changed its mind
func (s *Client) Skip(msgpars ...string) (reply *SpamDOut, err error) {
return s.simpleCall(SKIP, msgpars)
}
//Check if message is spam or not, and return score plus list of symbols hit
func (s *Client) Symbols(msgpars ...string) (reply *SpamDOut, err error) {
return s.simpleCall(SYMBOLS, msgpars)
}
//Check if message is spam or not, and return score plus report
func (s *Client) Report(msgpars ...string) (reply *SpamDOut, err error) {
return s.simpleCall(REPORT, msgpars)
}
//Check if message is spam or not, and return score plus report
func (s *Client) ReportIgnoreWarning(msgpars ...string) (reply *SpamDOut, err error) {
return s.simpleCall(REPORT_IGNOREWARNING, msgpars)
}
//Check if message is spam or not, and return score plus report if the message is spam
func (s *Client) ReportIfSpam(msgpars ...string) (reply *SpamDOut, err error) {
return s.simpleCall(REPORT_IFSPAM, msgpars)
}
//Process this message and return a modified message - on deloy
func (s *Client) Process(msgpars ...string) (reply *SpamDOut, err error) {
return s.simpleCall(PROCESS, msgpars)
}
//Same as PROCESS, but return only modified headers, not body (new in protocol 1.4)
func (s *Client) Headers(msgpars ...string) (reply *SpamDOut, err error) {
return s.simpleCall(HEADERS, msgpars)
}
//Sign the message as spam
func (s *Client) ReportingSpam(msgpars ...string) (reply *SpamDOut, err error) {
headers := map[string]string{
"Message-class": "spam",
"Set": "local,remote",
}
return s.Tell(msgpars, &headers)
}
//Sign the message as false-positive
func (s *Client) RevokeSpam(msgpars ...string) (reply *SpamDOut, err error) {
headers := map[string]string{
"Message-class": "ham",
"Set": "local,remote",
}
return s.Tell(msgpars, &headers)
}
//Learn if a message is spam or not
func (s *Client) Learn(learnType string, msgpars ...string) (reply *SpamDOut, err error) {
headers := make(map[string]string)
switch strings.ToUpper(learnType) {
case LEARN_SPAM:
headers["Message-class"] = "spam"
headers["Set"] = "local"
case LEARN_HAM, LEARN_NOTSPAM, LEARN_NOT_SPAM:
headers["Message-class"] = "ham"
headers["Set"] = "local"
case LEARN_FORGET:
headers["Remove"] = "local"
default:
err = errors.New("Learn Type Not Found")
return
}
return s.Tell(msgpars, &headers)
}
//wrapper to simple calls
func (s *Client) simpleCall(cmd string, msgpars []string) (reply *SpamDOut, err error) {
return s.call(cmd, msgpars, func(data *bufio.Reader) (r *SpamDOut, e error) {
r, e = processResponse(cmd, data)
if r.Code == EX_OK {
e = nil
}
return
}, nil)
}
//external wrapper to simple call
func (s *Client) SimpleCall(cmd string, msgpars ...string) (reply *SpamDOut, err error) {
return s.simpleCall(strings.ToUpper(cmd), msgpars)
}
//Tell what type of we are to process and what should be done
//with that message. This includes setting or removing a local
//or a remote database (learning, reporting, forgetting, revoking)
func (s *Client) Tell(msgpars []string, headers *map[string]string) (reply *SpamDOut, err error) {
return s.call(TELL, msgpars, func(data *bufio.Reader) (r *SpamDOut, e error) {
r, e = processResponse(TELL, data)
if r.Code == EX_UNAVAILABLE {
e = errors.New("TELL commands are not enabled, set the --allow-tell switch.")
return
}
if r.Code == EX_OK {
e = nil
return
}
return
}, headers)
}
//here a TCP socket is created to call SPAMD
func (s *Client) call(cmd string, msgpars []string, onData FnCallback, extraHeaders *map[string]string) (reply *SpamDOut, err error) {
if extraHeaders == nil {
extraHeaders = &map[string]string{}
}
switch len(msgpars) {
case 1:
if s.User != "" {
x := *extraHeaders
x["User"] = s.User
*extraHeaders = x
}
case 2:
x := *extraHeaders
x["User"] = msgpars[1]
*extraHeaders = x
default:
if cmd != PING {
err = errors.New("Message parameters wrong size")
} else {
msgpars = []string{""}
}
return
}
if cmd == REPORT_IGNOREWARNING {
cmd = REPORT
}
// Create a new connection
stream, err := net.Dial("tcp", s.Host)
if err != nil {
err = errors.New("Connection dial error to spamd: " + err.Error())
return
}
// Set connection timeout
timeout := time.Now().Add(time.Duration(s.ConnTimoutSecs) * time.Duration(time.Second))
errTimeout := stream.SetDeadline(timeout)
if errTimeout != nil {
err = errors.New("Connection to spamd Timed Out:" + errTimeout.Error())
return
}
defer stream.Close()
// Create Command to Send to spamd
cmd += " SPAMC/" + s.ProtocolVersion + "\r\n"
cmd += "Content-length: " + fmt.Sprintf("%v\r\n", len(msgpars[0])+2)
//Process Extra Headers if Any
if len(*extraHeaders) > 0 {
for hname, hvalue := range *extraHeaders {
cmd = cmd + hname + ": " + hvalue + "\r\n"
}
}
cmd += "\r\n" + msgpars[0] + "\r\n\r\n"
_, errwrite := stream.Write([]byte(cmd))
if errwrite != nil {
err = errors.New("spamd returned a error: " + errwrite.Error())
return
}
// Execute onData callback throwing the buffer like parameter
reply, err = onData(bufio.NewReader(stream))
return
}
//SpamD reply processor
func processResponse(cmd string, data *bufio.Reader) (returnObj *SpamDOut, err error) {
defer func() {
data.UnreadByte()
}()
returnObj = new(SpamDOut)
returnObj.Code = -1
//read the first line
line, _, _ := data.ReadLine()
lineStr := string(line)
r := regexp.MustCompile(`(?i)SPAMD\/([0-9\.]+)\s([0-9]+)\s([0-9A-Z_]+)`)
var result = r.FindStringSubmatch(lineStr)
if len(result) < 4 {
if cmd != "SKIP" {
err = errors.New("spamd unreconized reply:" + lineStr)
} else {
returnObj.Code = EX_OK
returnObj.Message = "SKIPPED"
}
return
}
returnObj.Code, _ = strconv.Atoi(result[2])
returnObj.Message = result[3]
//verify a mapped error...
if SpamDError[returnObj.Code] != "" {
err = errors.New(SpamDError[returnObj.Code])
returnObj.Vars = make(map[string]interface{})
returnObj.Vars["error_description"] = SpamDError[returnObj.Code]
return
}
returnObj.Vars = make(map[string]interface{})
//start didSet
if cmd == TELL {
returnObj.Vars["didSet"] = false
returnObj.Vars["didRemove"] = false
for {
line, _, err = data.ReadLine()
if err == io.EOF || err != nil {
if err == io.EOF {
err = nil
}
break
}
if strings.Contains(string(line), "DidRemove") {
returnObj.Vars["didRemove"] = true
}
if strings.Contains(string(line), "DidSet") {
returnObj.Vars["didSet"] = true
}
}
return
}
//read the second line
line, _, err = data.ReadLine()
//finish here if line is empty
if len(line) == 0 {
if err == io.EOF {
err = nil
}
return
}
//ignore content-length header..
lineStr = string(line)
switch cmd {
case SYMBOLS, CHECK, REPORT, REPORT_IFSPAM, REPORT_IGNOREWARNING, PROCESS, HEADERS:
switch cmd {
case SYMBOLS, REPORT, REPORT_IFSPAM, REPORT_IGNOREWARNING, PROCESS, HEADERS:
//ignore content-length header..
line, _, err = data.ReadLine()
lineStr = string(line)
}
r := regexp.MustCompile(`(?i)Spam:\s(True|False|Yes|No)\s;\s([0-9\.]+)\s\/\s([0-9\.]+)`)
var result = r.FindStringSubmatch(lineStr)
if len(result) > 0 {
returnObj.Vars["isSpam"] = false
switch result[1][0:1] {
case "T", "t", "Y", "y":
returnObj.Vars["isSpam"] = true
}
returnObj.Vars["spamScore"], _ = strconv.ParseFloat(result[2], 64)
returnObj.Vars["baseSpamScore"], _ = strconv.ParseFloat(result[3], 64)
}
switch cmd {
case PROCESS, HEADERS:
lines := ""
for {
line, _, err = data.ReadLine()
if err == io.EOF || err != nil {
if err == io.EOF {
err = nil
}
return
}
lines += string(line) + "\r\n"
returnObj.Vars["body"] = lines
}
return
case SYMBOLS:
//ignore line break...
data.ReadLine()
//read
line, _, err = data.ReadLine()
returnObj.Vars["symbolList"] = strings.Split(string(line), ",")
case REPORT, REPORT_IFSPAM, REPORT_IGNOREWARNING:
//ignore line break...
data.ReadLine()
for {
line, _, err = data.ReadLine()
if len(line) > 0 {
lineStr = string(line)
//TXT Table found, prepare to parse..
// Note: this line can be blank too, so defend against slice out of bounds panic
if len(lineStr) >= 4 {
if lineStr[0:4] == TABLE_MARK {
section := []map[string]interface{}{}
tt := 0
for {
line, _, err = data.ReadLine()
//Stop read the text table if last line or Void line
if err == io.EOF || err != nil || len(line) == 0 {
if err == io.EOF {
err = nil
}
break
}
//Parsing
lineStr = string(line)
spc := 2
if lineStr[0:1] == "-" {
spc = 1
}
lineStr = strings.Replace(lineStr, " ", SPLIT, spc)
lineStr = strings.Replace(lineStr, " ", SPLIT, 1)
if spc > 1 {
lineStr = " " + lineStr[2:]
}
x := strings.Split(lineStr, SPLIT)
if lineStr[1:3] == SPLIT {
section[tt-1]["message"] = fmt.Sprintf("%v %v", section[tt-1]["message"], strings.TrimSpace(lineStr[5:]))
} else {
if len(x) != 0 {
message := strings.TrimSpace(x[2])
score, _ := strconv.ParseFloat(strings.TrimSpace(x[0]), 64)
section = append(section, map[string]interface{}{
"score": score,
"symbol": x[1],
"message": message,
})
tt++
}
}
}
if REPORT_IGNOREWARNING == cmd {
nsection := []map[string]interface{}{}
for _, c := range section {
if c["score"].(float64) != 0 {
nsection = append(nsection, c)
}
}
section = nsection
}
returnObj.Vars["report"] = section
break
}
}
}
if err == io.EOF || err != nil {
if err == io.EOF {
err = nil
}
break
}
}
}
}
if err != io.EOF {
for {
line, _, err = data.ReadLine()
if err == io.EOF || err != nil {
if err == io.EOF {
err = nil
}
break
}
}
}
return
}