change reg ex in report to allow minus numbers. Add comments

This commit is contained in:
Carl Glaysher
2012-03-17 13:09:50 +00:00
parent 20e5b7bdac
commit 6434f0058b
2 changed files with 56 additions and 28 deletions

View File

@@ -2,10 +2,29 @@
* User: Carl Glaysher * User: Carl Glaysher
* Date: 17/03/2012 * Date: 17/03/2012
* Time: 08:46 * Time: 08:46
* Description: Server to Test Connections * Description: Front end to check spamc client
*/ */
var spamc = require('./spamc'); var spamc = require('./spamc');
var client = new spamc('localhost'); var client = new spamc();
client.learn('My Message','spam',function(res){
console.log(res); client.report('My Message as String',function(result){
console.log(result);
}); });
/* Example Response
{
responseCode: 0,
responseMessage: 'EX_OK',
isSpam: true,
spamScore: 6.9,
baseSpamScore: 5,
report:[
{
score: '0.0',
name: 'NO_RELAYS',
description: 'Informational',
type: 'message'
}
]
}
*/

View File

@@ -1,8 +1,20 @@
/** /**
* Author: Carl Glaysher * Author: Carl Glaysher
* Date: 17/03/2012 * Date Created: 17/03/2012
* Time: 08:47 * Description: Module to emulate SPAMC client in a node way
* 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 net = require('net');
@@ -37,7 +49,7 @@ var spamc = function (host, port, timeout) {
this.check = function(message,onResponse){ this.check = function(message,onResponse){
exec('CHECK',message,function(data){ exec('CHECK',message,function(data){
var response = processResponse('CHECK',data); var response = processResponse('CHECK',data);
onResponse(response); if(typeof(onResponse)=='function') onResponse(response);
}); });
return self; return self;
}; };
@@ -50,7 +62,7 @@ var spamc = function (host, port, timeout) {
this.symbols = function(message,onResponse){ this.symbols = function(message,onResponse){
exec('SYMBOLS',message,function(data){ exec('SYMBOLS',message,function(data){
var response = processResponse('SYMBOLS',data); var response = processResponse('SYMBOLS',data);
onResponse(response); if(typeof(onResponse)=='function') onResponse(response);
}); });
return self; return self;
}; };
@@ -63,7 +75,7 @@ var spamc = function (host, port, timeout) {
this.report = function(message,onResponse){ this.report = function(message,onResponse){
exec('REPORT',message,function(data){ exec('REPORT',message,function(data){
var response = processResponse('REPORT',data); var response = processResponse('REPORT',data);
onResponse(response); if(typeof(onResponse)=='function') onResponse(response);
}); });
return self; return self;
}; };
@@ -76,7 +88,7 @@ var spamc = function (host, port, timeout) {
this.reportIfSpam = function(message,onResponse){ this.reportIfSpam = function(message,onResponse){
exec('REPORT_IFSPAM',message,function(data){ exec('REPORT_IFSPAM',message,function(data){
var response = processResponse('REPORT_IFSPAM',data); var response = processResponse('REPORT_IFSPAM',data);
onResponse(response); if(typeof(onResponse)=='function') onResponse(response);
}); });
return self; return self;
}; };
@@ -89,7 +101,7 @@ var spamc = function (host, port, timeout) {
this.process = function(message,onResponse){ this.process = function(message,onResponse){
exec('PROCESS',message,function(data){ exec('PROCESS',message,function(data){
var response = processResponse('PROCESS',data); var response = processResponse('PROCESS',data);
onResponse(response); if(typeof(onResponse)=='function') onResponse(response);
}); });
return self; return self;
}; };
@@ -102,7 +114,7 @@ var spamc = function (host, port, timeout) {
this.headers = function(message,onResponse){ this.headers = function(message,onResponse){
exec('HEADERS',message,function(data){ exec('HEADERS',message,function(data){
var response = processResponse('HEADERS',data); var response = processResponse('HEADERS',data);
onResponse(response); if(typeof(onResponse)=='function') onResponse(response);
}); });
return self; return self;
}; };
@@ -144,8 +156,7 @@ var spamc = function (host, port, timeout) {
if(response.responseCode==69){ if(response.responseCode==69){
throw new Error('TELL commands are not enabled, set the --allow-tell switch.'); throw new Error('TELL commands are not enabled, set the --allow-tell switch.');
} }
console.log(data); if(typeof(onResponse)=='function') onResponse(response);
onResponse(response);
},headers); },headers);
return self; return self;
}; };
@@ -165,8 +176,7 @@ var spamc = function (host, port, timeout) {
if(response.responseCode==69){ if(response.responseCode==69){
throw new Error('TELL commands are not enabled, set the --allow-tell switch.'); throw new Error('TELL commands are not enabled, set the --allow-tell switch.');
} }
console.log(data); if(typeof(onResponse)=='function') onResponse(response);
onResponse(response);
},headers); },headers);
return self; return self;
}; };
@@ -186,8 +196,7 @@ var spamc = function (host, port, timeout) {
if(response.responseCode==69){ if(response.responseCode==69){
throw new Error('TELL commands are not enabled, set the --allow-tell switch.'); throw new Error('TELL commands are not enabled, set the --allow-tell switch.');
} }
console.log(data); if(typeof(onResponse)=='function') onResponse(response);
onResponse(response);
},headers); },headers);
return self; return self;
}; };
@@ -217,10 +226,10 @@ var spamc = function (host, port, timeout) {
} }
cmd = cmd+"\r\n"+message; cmd = cmd+"\r\n"+message;
} }
stream.write(cmd); stream.write(cmd+"\r\n");
}); });
stream.on('error',function(data){ stream.on('error',function(data){
throw new Error('spamd returned a error:'+data.toString()); throw new Error('spamd returned a error: '+data.toString());
}); });
stream.on('data',function(data){ stream.on('data',function(data){
var data = data.toString(); var data = data.toString();
@@ -274,7 +283,7 @@ var spamc = function (host, port, timeout) {
} }
} }
if(result==null && cmd!='PROCESS'){ if(result==null && cmd!='PROCESS'){
var pattern = /\s([0-9\.]+)\s([A-Z0-9\_]+)\s([^:]+)\:\s([^\n]+)/g; var pattern = /(\s|-)([0-9\.]+)\s([A-Z0-9\_]+)\s([^:]+)\:\s([^\n]+)/g;
var result = lines[i].match(pattern); var result = lines[i].match(pattern);
if(result!=null){ if(result!=null){
returnObj.report =[]; returnObj.report =[];
@@ -282,13 +291,13 @@ var spamc = function (host, port, timeout) {
/* Remove New Line if Found */ /* Remove New Line if Found */
result[ii] = result[ii].replace(/\n([\s]*)/, ' '); result[ii] = result[ii].replace(/\n([\s]*)/, ' ');
/* Match Sections */ /* Match Sections */
var pattern = /\s([0-9\.]+)\s([A-Z0-9\_]+)\s([^:]+)\:\s([^\s]+)/; var pattern = /(\s|-)([0-9\.]+)\s([A-Z0-9\_]+)\s([^:]+)\:\s([^\s]+)/;
var matches = result[ii].match(pattern); var matches = result[ii].match(pattern);
returnObj.report[returnObj.report.length] = { returnObj.report[returnObj.report.length] = {
score:matches[1], score:matches[2],
name:matches[2], name:matches[3],
description:matches[3].replace(/^\s*([\S\s]*)\b\s*$/, '$1'), description:matches[4].replace(/^\s*([\S\s]*)\b\s*$/, '$1'),
type:matches[4] type:matches[5]
}; };
} }
} }