summaryrefslogtreecommitdiff
path: root/modules/$common.js
blob: f7ec42ab73aaf8303f2960dce56e8f08e065befa (plain)
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
var path=require("path"),
    basicAuth=require("basic-auth");

var cwd=process.cwd();
var globalAccounts=require(cwd+"/globalAccounts.json");

var statusbotObject={
	// calls cb with http status code
	"send":function statusbotSend(sender,text,cb){
		if(statusbotObject._handler==null){
			console.log("[common.statusbot] No statusbot yet");
			cb(500);
			return;
		}
		statusbotObject._handler(sender,text,cb);
	},
	"_handler":null,  // to be set from the statusbot module
};

module.exports={
	"serverdir":cwd,
	"webfilesdir":cwd+"/web_files",
	"persistdir":cwd+"/persist",

	"simpleHTMLescape":function simpleHTMLescape(str){
		return str.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
	},

	"authgen":function authgen(accounts){ //omit `accounts` to use the globalAccounts list
		if(!accounts)accounts=globalAccounts;
		return function (req,res,next){
			function unauth(res){
				res.set("WWW-Authenticate","Basic realm=Authorization required");
				return res.sendStatus(401);
			};
			var user=basicAuth(req);
			if(!user||!user.name||!user.pass){
				return unauth(res);
			}
			var i;
			for(i=0;i<accounts.length;i++){
				if(accounts[i][0]==user.name&&accounts[i][1]==user.pass){
					req.authuser=user.name;
					return next();
				}
			}
			return unauth(res);
		};
	},

	"statusbot":statusbotObject,
};