-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathindex.js
334 lines (329 loc) · 11.6 KB
/
index.js
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
/**
* Author: Carl Glaysher
* Date Created: 17/03/2012
* Description: Module to emulate SPAMC client in a node way
* Available Commands:
*
* ping - returns boolean
* check - returns object
* symbols - returns object with matches
* report - returns objects with matches and descriptions
* reportIfSpam - returns object with matches and descriptions
* process - returns object with modified message
* headers - returns object with modified headers only
* learn - TELL spamassassin message is Spam or Ham
* tell - TELL spamassassin message is Spam
* revoke - remove Spammed Message as being spam from spamassassin
*
*/
var net = require('net');
var spamc = function (host, port, timeout) {
var self = this;
var protocolVersion = 1.5;
var host = (host == undefined) ? '127.0.0.1' : host;
var port = (port == undefined) ? 783 : port;
var connTimoutSecs = (timeout == undefined) ? 10 : timeout;
/*
* Description: Sends a Ping to spamd and returns Pong on response
* Param: onResponse {function}
* Returns: self
*/
this.ping = function(onResponse){
exec('PING',null,function(data){
/* Check Response has the word PONG */
if(data[0].indexOf('PONG')>0){
onResponse(true);
}else{
onResponse(false);
}
});
return self;
};
/*
* Description: returns spam score
* Param: message {string}
* Param: onResponse {function}
* Returns: self
*/
this.check = function(message,onResponse){
exec('CHECK',message,function(data){
var response = processResponse('CHECK',data);
if(typeof(onResponse)=='function') onResponse(response);
});
return self;
};
/*
* Description: Returns Spam Score and Matches
* Param: message {string}
* Param: onResponse {function}
* Returns: self
*/
this.symbols = function(message,onResponse){
exec('SYMBOLS',message,function(data){
var response = processResponse('SYMBOLS',data);
if(typeof(onResponse)=='function') onResponse(response);
});
return self;
};
/*
* Description: Returns an object report
* Param: message {string}
* Param: onResponse {function}
* Returns: self
*/
this.report = function(message,onResponse){
exec('REPORT',message,function(data){
var response = processResponse('REPORT',data);
if(typeof(onResponse)=='function') onResponse(response);
});
return self;
};
/*
* Description: Returns Object Report if is spam
* Param: message {string}
* Param: onResponse {function}
* Returns: self
*/
this.reportIfSpam = function(message,onResponse){
exec('REPORT_IFSPAM',message,function(data){
var response = processResponse('REPORT_IFSPAM',data);
if(typeof(onResponse)=='function') onResponse(response);
});
return self;
};
/*
* Description: Returns back a report for the message + the message
* Param: message {string}
* Param: onResponse {function}
* Returns: self
*/
this.process = function(message,onResponse){
exec('PROCESS',message,function(data){
var response = processResponse('PROCESS',data);
if(typeof(onResponse)=='function') onResponse(response);
});
return self;
};
/*
* Description: Returns headers for the message
* Param: message {string}
* Param: onResponse {function}
* Returns: self
*/
this.headers = function(message,onResponse){
exec('HEADERS',message,function(data){
var response = processResponse('HEADERS',data);
if(typeof(onResponse)=='function') onResponse(response);
});
return self;
};
/*
* Description: Tell spamd to learn message is spam/ham or forget
* Param: message {string}
* Param: learnType {string}
* Param: onResponse {function}
* Returns: self
*/
this.learn = function(message,learnType,onResponse){
var headers;
switch(learnType.toUpperCase()){
case 'SPAM':
headers=[
{name:'Message-class','value':'spam'},
{name:'Set','value':'local'}
];
break;
case 'HAM':
case 'NOTSPAM':
case 'NOT_SPAM':
headers=[
{name:'Message-class','value':'ham'},
{name:'Set','value':'local'}
];
break;
case 'FORGET':
headers=[
{name:'Remove','value':'local'}
];
break;
default:
throw new Error('Learn Type Not Found');
}
exec('TELL',message,function(data){
var response = processResponse('HEADERS',data);
if(response.responseCode==69){
throw new Error('TELL commands are not enabled, set the --allow-tell switch.');
}
if(typeof(onResponse)=='function') onResponse(response);
},headers);
return self;
};
/*
* Description: tell spamd message is not spam
* Param: message {string}
* Param: onResponse {function}
* Returns: self
*/
this.revoke = function(message,onResponse){
headers=[
{name:'Message-class','value':'ham'},
{name:'Set','value':'local,remote'}
];
exec('TELL',message,function(data){
var response = processResponse('HEADERS',data);
if(response.responseCode==69){
throw new Error('TELL commands are not enabled, set the --allow-tell switch.');
}
if(typeof(onResponse)=='function') onResponse(response);
},headers);
return self;
};
/*
* Description: Tell spamd message is spam
* Param: message {string}
* Param: onResponse {function}
* Returns: self
*/
this.tell = function(message,onResponse){
headers=[
{name:'Message-class','value':'spam'},
{name:'Set','value':'local,remote'}
];
exec('TELL',message,function(data){
var response = processResponse('HEADERS',data);
if(response.responseCode==69){
throw new Error('TELL commands are not enabled, set the --allow-tell switch.');
}
if(typeof(onResponse)=='function') onResponse(response);
},headers);
return self;
};
/*
* Description: Sends a command to spamd
* Param: cmd {string}
* Param: message {string}
* Param: onData {function(data)}
*/
var exec = function(cmd,message,onData,extraHeaders){
var responseData = [];
var stream = net.createConnection(port,host);
stream.setTimeout(connTimoutSecs*1000,function(){
throw new Error('Connection to spamd Timed Out');
});
stream.on('connect',function(){
/* Create Command to Send to spamd */
cmd = cmd+" SPAMC/"+protocolVersion+"\r\n";
if(typeof(message)=='string'){
message = message+'\r\n';
cmd = cmd+"Content-length: "+(message.length)+"\r\n";
/* Process Extra Headers if Any */
if(typeof(extraHeaders)=='object'){
for(var i=0;i<extraHeaders.length;i++){
cmd = cmd+extraHeaders[i].name+": "+extraHeaders[i].value+"\r\n";
}
}
cmd = cmd+"\r\n"+message;
}
stream.write(cmd+"\r\n");
});
stream.on('error',function(data){
throw new Error('spamd returned a error: '+data.toString());
});
stream.on('data',function(data){
var data = data.toString();
/* Remove Last new Line and Return and Split Lines into Array */
data = data.split("\r\n");
for(var i=0;i<data.length;i++){
if(data[i].length>0){
responseData[responseData.length]=data[i];
}
}
});
stream.on('close',function(){
onData(responseData);
})
};
/*
* Description: Processes Response from spamd and put into a formatted object
* Param: cmd {string}
* Param: lines {array[string]}
* Return: {object}
*/
var processResponse = function(cmd,lines){
var returnObj = {};
var result = lines[0].match(/SPAMD\/([0-9\.]+)\s([0-9]+)\s([0-9A-Z_]+)/);
if(result==null){
throw new Error('spamd unreconized response:'+lines[0]);
}
returnObj.responseCode = parseInt(result[2]);
returnObj.responseMessage = result[3];
if(cmd=='TELL'){
returnObj.didSet=false;
returnObj.didRemove=false;
}
for(var i=0;i<lines.length;i++){
var result = lines[i].match(/Spam:\s(True|False|Yes|No)\s;\s([0-9\.]+)\s\/\s([0-9\.]+)/);
if(result!=null){
returnObj.isSpam =false;
if(result[1]=='True' || result[1]=='Yes'){
returnObj.isSpam = true;
}
returnObj.spamScore = parseFloat(result[2]);
returnObj.baseSpamScore = parseFloat(result[3]);
}
if(result==null){
var result = lines[i].match(/([A-Z0-9\_]+)\,/g);
if(result!=null){
returnObj.matches =[];
for(var ii=0;ii<result.length;ii++){
returnObj.matches[ii] = result[ii].substring(0,result[ii].length-1);
}
}
}
if(result==null && cmd!='PROCESS'){
var pattern = /(\s|-)([0-9\.]+)\s([A-Z0-9\_]+)\s([^:]+)\:\s([^\n]+)/g;
var result = lines[i].match(pattern);
if(result!=null){
returnObj.report =[];
for(var ii=0;ii<result.length;ii++){
/* Remove New Line if Found */
result[ii] = result[ii].replace(/\n([\s]*)/, ' ');
/* Match Sections */
var pattern = /(\s|-)([0-9\.]+)\s([A-Z0-9\_]+)\s([^:]+)\:\s([^\s]+)/;
var matches = result[ii].match(pattern);
returnObj.report[returnObj.report.length] = {
score:matches[2],
name:matches[3],
description:matches[4].replace(/^\s*([\S\s]*)\b\s*$/, '$1'),
type:matches[5]
};
}
}
}
if(lines[i].indexOf('DidSet:')>=0){
returnObj.didSet=true;
}
if(lines[i].indexOf('DidRemove:')>=0){
returnObj.didRemove=true;
}
}
if(cmd=='PROCESS'){
returnObj.message = '';
for(var i=3;i<lines.length;i++){
returnObj.message=returnObj.message+lines[i]+"\r\n";
}
}
if(cmd=='HEADERS'){
returnObj.headers = [];
for(var i=3;i<lines.length;i++){
if(lines[i].indexOf('\t')<0){
returnObj.headers[returnObj.headers.length]=lines[i];
}else{
returnObj.headers[returnObj.headers.length-1]=returnObj.headers[returnObj.headers.length-1]+lines[i];
}
}
}
return returnObj;
};
};
module.exports = spamc;