From 6434f0058b833afe24aaa6f4c86f4e7a23ee89a7 Mon Sep 17 00:00:00 2001 From: Carl Glaysher Date: Sat, 17 Mar 2012 13:09:50 +0000 Subject: [PATCH] change reg ex in report to allow minus numbers. Add comments --- server.js | 29 ++++++++++++++++++++++++----- spamc.js | 55 ++++++++++++++++++++++++++++++++----------------------- 2 files changed, 56 insertions(+), 28 deletions(-) diff --git a/server.js b/server.js index 5f3f7e0..a6d84b5 100644 --- a/server.js +++ b/server.js @@ -2,10 +2,29 @@ * User: Carl Glaysher * Date: 17/03/2012 * Time: 08:46 - * Description: Server to Test Connections + * Description: Front end to check spamc client */ var spamc = require('./spamc'); -var client = new spamc('localhost'); -client.learn('My Message','spam',function(res){ - console.log(res); -}); \ No newline at end of file +var client = new spamc(); + +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' + } + ] + } +*/ \ No newline at end of file diff --git a/spamc.js b/spamc.js index c271a37..7017b6c 100644 --- a/spamc.js +++ b/spamc.js @@ -1,8 +1,20 @@ /** * Author: Carl Glaysher - * Date: 17/03/2012 - * Time: 08:47 - * Description: Module to Emulate SPAMC Client in a node way + * 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'); @@ -37,7 +49,7 @@ var spamc = function (host, port, timeout) { this.check = function(message,onResponse){ exec('CHECK',message,function(data){ var response = processResponse('CHECK',data); - onResponse(response); + if(typeof(onResponse)=='function') onResponse(response); }); return self; }; @@ -50,7 +62,7 @@ var spamc = function (host, port, timeout) { this.symbols = function(message,onResponse){ exec('SYMBOLS',message,function(data){ var response = processResponse('SYMBOLS',data); - onResponse(response); + if(typeof(onResponse)=='function') onResponse(response); }); return self; }; @@ -63,7 +75,7 @@ var spamc = function (host, port, timeout) { this.report = function(message,onResponse){ exec('REPORT',message,function(data){ var response = processResponse('REPORT',data); - onResponse(response); + if(typeof(onResponse)=='function') onResponse(response); }); return self; }; @@ -76,7 +88,7 @@ var spamc = function (host, port, timeout) { this.reportIfSpam = function(message,onResponse){ exec('REPORT_IFSPAM',message,function(data){ var response = processResponse('REPORT_IFSPAM',data); - onResponse(response); + if(typeof(onResponse)=='function') onResponse(response); }); return self; }; @@ -89,7 +101,7 @@ var spamc = function (host, port, timeout) { this.process = function(message,onResponse){ exec('PROCESS',message,function(data){ var response = processResponse('PROCESS',data); - onResponse(response); + if(typeof(onResponse)=='function') onResponse(response); }); return self; }; @@ -102,7 +114,7 @@ var spamc = function (host, port, timeout) { this.headers = function(message,onResponse){ exec('HEADERS',message,function(data){ var response = processResponse('HEADERS',data); - onResponse(response); + if(typeof(onResponse)=='function') onResponse(response); }); return self; }; @@ -144,8 +156,7 @@ var spamc = function (host, port, timeout) { if(response.responseCode==69){ throw new Error('TELL commands are not enabled, set the --allow-tell switch.'); } - console.log(data); - onResponse(response); + if(typeof(onResponse)=='function') onResponse(response); },headers); return self; }; @@ -165,8 +176,7 @@ var spamc = function (host, port, timeout) { if(response.responseCode==69){ throw new Error('TELL commands are not enabled, set the --allow-tell switch.'); } - console.log(data); - onResponse(response); + if(typeof(onResponse)=='function') onResponse(response); },headers); return self; }; @@ -186,8 +196,7 @@ var spamc = function (host, port, timeout) { if(response.responseCode==69){ throw new Error('TELL commands are not enabled, set the --allow-tell switch.'); } - console.log(data); - onResponse(response); + if(typeof(onResponse)=='function') onResponse(response); },headers); return self; }; @@ -217,10 +226,10 @@ var spamc = function (host, port, timeout) { } cmd = cmd+"\r\n"+message; } - stream.write(cmd); + stream.write(cmd+"\r\n"); }); 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){ var data = data.toString(); @@ -274,7 +283,7 @@ var spamc = function (host, port, timeout) { } } 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); if(result!=null){ returnObj.report =[]; @@ -282,13 +291,13 @@ var spamc = function (host, port, timeout) { /* 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 pattern = /(\s|-)([0-9\.]+)\s([A-Z0-9\_]+)\s([^:]+)\:\s([^\s]+)/; var matches = result[ii].match(pattern); returnObj.report[returnObj.report.length] = { - score:matches[1], - name:matches[2], - description:matches[3].replace(/^\s*([\S\s]*)\b\s*$/, '$1'), - type:matches[4] + score:matches[2], + name:matches[3], + description:matches[4].replace(/^\s*([\S\s]*)\b\s*$/, '$1'), + type:matches[5] }; } }