From 6ffce6e3864fa38543500bbbf00c8b38e7fb5c7a Mon Sep 17 00:00:00 2001
From: deuce <>
Date: Tue, 19 Jan 2016 06:58:39 +0000
Subject: [PATCH] Split out FREQIT into a separate SRIF handler and FREQIT
 object to allow embedding REQ handling into binkit.

---
 exec/freqit.js             | 89 ++++++++------------------------------
 exec/load/freqit_common.js | 58 +++++++++++++++++++++++++
 2 files changed, 76 insertions(+), 71 deletions(-)
 create mode 100644 exec/load/freqit_common.js

diff --git a/exec/freqit.js b/exec/freqit.js
index c9aa0d809f..e6cd0e2a49 100644
--- a/exec/freqit.js
+++ b/exec/freqit.js
@@ -5,10 +5,21 @@
  * exec "/sbbs/exec/jsexec freqit *S" *.req *.REQ
  */
 
-load("filebase.js");
 load("fidocfg.js");
+load("freqit_common.js");
 
-var cfg = new FREQITCfg();
+FREQIT.add_file = function(filename, resp, cfg)
+{
+	if (filename === undefined)
+		return;
+	if (FREQIT.files >= cfg.maxfiles)
+		return;
+	if (FREQIT.added[filename] !== undefined)
+		return;
+	resp.writeln('+'+filename);
+	FREQIT.files++;
+	FREQIT.added[filename]='';
+};
 
 function parse_srif(fname)
 {
@@ -21,7 +32,7 @@ function parse_srif(fname)
 		log(LOG_ERROR, "Unable to find SRIF file '"+f.name+"'");
 		return undefined;
 	}
-	while(l = f.readln(65535)) {
+	while((l = f.readln(65535))) {
 		if ((m=l.match(/^\s*([^ ]+)\s+(.*)$/)) !== null)
 			srif[m[1].toLowerCase()] = m[2];
 	}
@@ -29,71 +40,6 @@ function parse_srif(fname)
 	return srif;
 }
 
-var dircache={};
-var added={};
-var files=0;
-
-function add_file(filename, resp)
-{
-	if (filename === undefined)
-		return;
-	if (files >= cfg.maxfiles)
-		return;
-	if (added[filename] !== undefined)
-		return;
-	resp.writeln('+'+filename);
-	files++;
-	added[filename]='';
-}
-
-/*
- * TODO: built-in magic names... FILES and NEW
- * FILES lists all FREQable files and
- * NEW lists ones newer than 10 days.
- */
-
-function handle_magic(magic, resp, protected, pw)
-{
-	var file=undefined;
-
-	if (magic.secure && !protected)
-		return;
-	if (dircache[magic.dir] === undefined)
-		dircache[magic.dir] = FileBase(magic.dir);
-	dircache[magic.dir].forEach(function(fent) {
-		if (wildmatch(fent.name, magic.match, true)) {
-			if (file === undefined || fent.uldate > file.uldate)
-				file = fent;
-		}
-	});
-	if (file !== undefined) {
-		add_file(file.path, resp);
-		return 1;
-	}
-	return 0;
-}
-
-function handle_regular(match, resp, protected, pw)
-{
-	var file=undefined;
-	var count=0;
-
-	function handle_list(list) {
-		list.forEach(function(dir) {
-			if (dircache[dir] === undefined)
-				dircache[dir] = FileBase(dir);
-			dircache[dir].forEach(function(fent) {
-				if (wildmatch(fent.name, match, true))
-					add_file(fent.path, resp);
-			});
-		});
-	}
-
-	if (protected)
-		handle_list(cfg.securedirs);
-	handle_list(cfg.dirs);
-}
-
 function handle_srif(srif)
 {
 	var req=new File(srif.requestlist);
@@ -101,6 +47,7 @@ function handle_srif(srif)
 	var m;
 	var fname;
 	var pw;
+	var cfg = new FREQITCfg();
 
 	if (!req.open("r"))
 		return;
@@ -109,7 +56,7 @@ function handle_srif(srif)
 	resp.position = resp.length;
 
 	next_file:
-	while(fname=req.readln()) {
+	while((fname=req.readln())) {
 		if ((m=fname.match(/^(.*) !(.*?)$/))!==null) {
 			pw=m[2];
 			fname=m[1];
@@ -117,13 +64,13 @@ function handle_srif(srif)
 		// First, check for magic!
 		for (m in cfg.magic) {
 			if (m == fname.toLowerCase()) {
-				handle_magic(cfg.magic[m], resp, srif.remotestatus.toLowerCase() === 'protected', pw);
+				FREQIT.handle_magic(cfg.magic[m], resp, srif.remotestatus.toLowerCase() === 'protected', pw, cfg);
 				continue next_file;
 			}
 		}
 
 		// Now, check for the file...
-		handle_regular(fname, resp, srif.remotestatus.toLowerCase() === 'protected', pw);
+		FREQIT.handle_regular(fname, resp, srif.remotestatus.toLowerCase() === 'protected', pw, cfg);
 	}
 }
 
diff --git a/exec/load/freqit_common.js b/exec/load/freqit_common.js
new file mode 100644
index 0000000000..5a2a0a9fcf
--- /dev/null
+++ b/exec/load/freqit_common.js
@@ -0,0 +1,58 @@
+/*
+ * TODO: built-in magic names... FILES and NEW
+ * FILES lists all FREQable files and
+ * NEW lists ones newer than 10 days.
+ */
+
+load("filebase.js");
+
+var FREQIT = {
+	dircache:{},
+	added:{},
+	files:0,
+	reset:function()
+	{
+		this.added = {};
+		this.files = 0;
+	},
+	handle_magic:function (magic, cb_data, protected, pw, cfg)
+	{
+		var file=undefined;
+
+		if (magic.secure && !protected)
+			return;
+		if (this.dircache[magic.dir] === undefined)
+			this.dircache[magic.dir] = new FileBase(magic.dir);
+		this.dircache[magic.dir].forEach(function(fent) {
+			if (wildmatch(fent.name, magic.match, true)) {
+				if (file === undefined || fent.uldate > file.uldate)
+					file = fent;
+			}
+		});
+		if (file !== undefined) {
+			this.add_file(file.path, cb_data, cfg);
+			return 1;
+		}
+		return 0;
+	},
+	handle_regular:function (match, cb_data, protected, pw, cfg)
+	{
+		var file=undefined;
+		var count=0;
+
+		function handle_list(list) {
+			list.forEach(function(dir) {
+				if (this.dircache[dir] === undefined)
+					this.dircache[dir] = new FileBase(dir);
+				this.dircache[dir].forEach(function(fent) {
+					if (wildmatch(fent.name, match, true))
+						this.add_file(fent.path, cb_data);
+				});
+			});
+		}
+
+		if (protected)
+			handle_list(cfg.securedirs);
+		handle_list(cfg.dirs);
+	}
+};
-- 
GitLab