2015-11-11 06:05:42 +00:00
|
|
|
|
|
|
|
{
|
2018-11-24 05:02:36 +00:00
|
|
|
const UserProps = require('./user_property.js');
|
2018-12-28 17:39:41 +00:00
|
|
|
const Log = require('./logger.js').log;
|
2019-04-10 02:07:19 +00:00
|
|
|
const User = require('./user.js');
|
2018-11-24 05:02:36 +00:00
|
|
|
|
2018-12-28 17:39:41 +00:00
|
|
|
const _ = require('lodash');
|
2018-02-08 03:26:29 +00:00
|
|
|
const moment = require('moment');
|
2015-11-12 07:31:25 +00:00
|
|
|
|
2018-12-28 17:39:41 +00:00
|
|
|
const client = _.get(options, 'subject.client');
|
|
|
|
const user = _.get(options, 'subject.user');
|
|
|
|
|
2015-11-13 23:13:20 +00:00
|
|
|
function checkAccess(acsCode, value) {
|
2015-11-13 03:47:34 +00:00
|
|
|
try {
|
|
|
|
return {
|
2015-11-13 23:13:20 +00:00
|
|
|
LC : function isLocalConnection() {
|
2018-12-28 17:39:41 +00:00
|
|
|
return client && client.isLocal();
|
2015-11-13 03:47:34 +00:00
|
|
|
},
|
2015-11-13 23:13:20 +00:00
|
|
|
AG : function ageGreaterOrEqualThan() {
|
2018-12-28 17:39:41 +00:00
|
|
|
return !isNaN(value) && user && user.getAge() >= value;
|
2015-11-13 03:47:34 +00:00
|
|
|
},
|
2015-11-14 19:22:21 +00:00
|
|
|
AS : function accountStatus() {
|
2018-12-28 17:39:41 +00:00
|
|
|
if(!user) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-02-08 03:26:29 +00:00
|
|
|
if(!Array.isArray(value)) {
|
2015-11-14 19:22:21 +00:00
|
|
|
value = [ value ];
|
|
|
|
}
|
2018-11-24 05:02:36 +00:00
|
|
|
const userAccountStatus = user.getPropertyAsNumber(UserProps.AccountStatus);
|
2018-02-08 03:26:29 +00:00
|
|
|
return value.map(n => parseInt(n, 10)).includes(userAccountStatus);
|
2015-11-14 19:22:21 +00:00
|
|
|
},
|
2015-11-13 03:47:34 +00:00
|
|
|
EC : function isEncoding() {
|
2018-12-28 17:39:41 +00:00
|
|
|
const encoding = _.get(client, 'term.outputEncoding', '').toLowerCase();
|
2015-11-13 03:47:34 +00:00
|
|
|
switch(value) {
|
2018-02-08 03:26:29 +00:00
|
|
|
case 0 : return 'cp437' === encoding;
|
|
|
|
case 1 : return 'utf-8' === encoding;
|
2015-11-13 03:47:34 +00:00
|
|
|
default : return false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
GM : function isOneOfGroups() {
|
2018-12-28 17:39:41 +00:00
|
|
|
if(!user) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-02-08 03:26:29 +00:00
|
|
|
if(!Array.isArray(value)) {
|
2015-11-13 03:47:34 +00:00
|
|
|
return false;
|
2015-11-12 07:31:25 +00:00
|
|
|
}
|
2018-02-08 03:26:29 +00:00
|
|
|
return value.some(groupName => user.isGroupMember(groupName));
|
2015-11-13 03:47:34 +00:00
|
|
|
},
|
2015-11-13 23:13:20 +00:00
|
|
|
NN : function isNode() {
|
2018-12-28 17:39:41 +00:00
|
|
|
if(!client) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-02-08 03:26:29 +00:00
|
|
|
if(!Array.isArray(value)) {
|
|
|
|
value = [ value ];
|
|
|
|
}
|
|
|
|
return value.map(n => parseInt(n, 10)).includes(client.node);
|
2015-11-13 03:47:34 +00:00
|
|
|
},
|
2015-11-13 23:13:20 +00:00
|
|
|
NP : function numberOfPosts() {
|
2018-12-28 17:39:41 +00:00
|
|
|
if(!user) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-11-24 05:02:36 +00:00
|
|
|
const postCount = user.getPropertyAsNumber(UserProps.PostCount) || 0;
|
2017-02-25 06:38:30 +00:00
|
|
|
return !isNaN(value) && postCount >= value;
|
2015-11-13 03:47:34 +00:00
|
|
|
},
|
2015-11-13 23:13:20 +00:00
|
|
|
NC : function numberOfCalls() {
|
2018-12-28 17:39:41 +00:00
|
|
|
if(!user) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-11-24 05:02:36 +00:00
|
|
|
const loginCount = user.getPropertyAsNumber(UserProps.LoginCount);
|
2017-02-25 06:38:30 +00:00
|
|
|
return !isNaN(value) && loginCount >= value;
|
2015-11-13 03:47:34 +00:00
|
|
|
},
|
2018-02-08 03:26:29 +00:00
|
|
|
AA : function accountAge() {
|
2018-12-28 17:39:41 +00:00
|
|
|
if(!user) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-11-24 05:02:36 +00:00
|
|
|
const accountCreated = moment(user.getProperty(UserProps.AccountCreated));
|
2018-02-08 03:26:29 +00:00
|
|
|
const now = moment();
|
|
|
|
const daysOld = accountCreated.diff(moment(), 'days');
|
|
|
|
return !isNaN(value) &&
|
|
|
|
accountCreated.isValid() &&
|
|
|
|
now.isAfter(accountCreated) &&
|
|
|
|
daysOld >= value;
|
|
|
|
},
|
|
|
|
BU : function bytesUploaded() {
|
2018-12-28 17:39:41 +00:00
|
|
|
if(!user) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-11-24 05:02:36 +00:00
|
|
|
const bytesUp = user.getPropertyAsNumber(UserProps.FileUlTotalBytes) || 0;
|
2018-02-08 03:26:29 +00:00
|
|
|
return !isNaN(value) && bytesUp >= value;
|
|
|
|
},
|
|
|
|
UP : function uploads() {
|
2018-12-28 17:39:41 +00:00
|
|
|
if(!user) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-11-24 05:02:36 +00:00
|
|
|
const uls = user.getPropertyAsNumber(UserProps.FileUlTotalCount) || 0;
|
2018-02-08 03:26:29 +00:00
|
|
|
return !isNaN(value) && uls >= value;
|
|
|
|
},
|
|
|
|
BD : function bytesDownloaded() {
|
2018-12-28 17:39:41 +00:00
|
|
|
if(!user) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-11-24 05:02:36 +00:00
|
|
|
const bytesDown = user.getPropertyAsNumber(UserProps.FileDlTotalBytes) || 0;
|
2018-02-08 03:26:29 +00:00
|
|
|
return !isNaN(value) && bytesDown >= value;
|
|
|
|
},
|
|
|
|
DL : function downloads() {
|
2018-12-28 17:39:41 +00:00
|
|
|
if(!user) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-11-24 05:02:36 +00:00
|
|
|
const dls = user.getPropertyAsNumber(UserProps.FileDlTotalCount) || 0;
|
2018-02-08 03:26:29 +00:00
|
|
|
return !isNaN(value) && dls >= value;
|
|
|
|
},
|
|
|
|
NR : function uploadDownloadRatioGreaterThan() {
|
2018-12-28 17:39:41 +00:00
|
|
|
if(!user) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-11-24 05:02:36 +00:00
|
|
|
const ulCount = user.getPropertyAsNumber(UserProps.FileUlTotalCount) || 0;
|
|
|
|
const dlCount = user.getPropertyAsNumber(UserProps.FileDlTotalCount) || 0;
|
2018-02-08 03:26:29 +00:00
|
|
|
const ratio = ~~((ulCount / dlCount) * 100);
|
|
|
|
return !isNaN(value) && ratio >= value;
|
|
|
|
},
|
|
|
|
KR : function uploadDownloadByteRatioGreaterThan() {
|
2018-12-28 17:39:41 +00:00
|
|
|
if(!user) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-11-24 05:02:36 +00:00
|
|
|
const ulBytes = user.getPropertyAsNumber(UserProps.FileUlTotalBytes) || 0;
|
|
|
|
const dlBytes = user.getPropertyAsNumber(UserProps.FileDlTotalBytes) || 0;
|
2018-02-08 03:26:29 +00:00
|
|
|
const ratio = ~~((ulBytes / dlBytes) * 100);
|
|
|
|
return !isNaN(value) && ratio >= value;
|
|
|
|
},
|
2018-12-28 17:39:41 +00:00
|
|
|
PC : function postCallRatio() {
|
|
|
|
if(!user) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-11-24 05:02:36 +00:00
|
|
|
const postCount = user.getPropertyAsNumber(UserProps.PostCount) || 0;
|
|
|
|
const loginCount = user.getPropertyAsNumber(UserProps.LoginCount) || 0;
|
2018-02-08 03:26:29 +00:00
|
|
|
const ratio = ~~((postCount / loginCount) * 100);
|
|
|
|
return !isNaN(value) && ratio >= value;
|
|
|
|
},
|
2016-02-03 04:35:59 +00:00
|
|
|
SC : function isSecureConnection() {
|
2018-12-28 17:39:41 +00:00
|
|
|
return _.get(client, 'session.isSecure', false);
|
2015-11-13 03:47:34 +00:00
|
|
|
},
|
2019-04-10 02:07:19 +00:00
|
|
|
AF : function currentAuthFactor() {
|
|
|
|
if(!user) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return !isNaN(value) && user.authFactor >= value;
|
|
|
|
},
|
|
|
|
AR : function authFactorRequired() {
|
|
|
|
if(!user) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
switch(value) {
|
2019-05-11 06:19:11 +00:00
|
|
|
case 1 : return true;
|
|
|
|
case 2 : return user.getProperty(UserProps.AuthFactor2OTP) ? true : false;
|
2019-04-10 02:07:19 +00:00
|
|
|
default : return false;
|
|
|
|
}
|
|
|
|
},
|
2015-11-13 23:13:20 +00:00
|
|
|
ML : function minutesLeft() {
|
2015-11-13 03:47:34 +00:00
|
|
|
// :TODO: implement me!
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
TH : function termHeight() {
|
2018-12-28 17:39:41 +00:00
|
|
|
return !isNaN(value) && _.get(client, 'term.termHeight', 0) >= value;
|
2015-11-13 03:47:34 +00:00
|
|
|
},
|
|
|
|
TM : function isOneOfThemes() {
|
2018-02-08 03:26:29 +00:00
|
|
|
if(!Array.isArray(value)) {
|
2015-11-13 03:47:34 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-12-28 17:39:41 +00:00
|
|
|
return value.includes(_.get(client, 'currentTheme.name'));
|
2015-11-13 03:47:34 +00:00
|
|
|
},
|
|
|
|
TT : function isOneOfTermTypes() {
|
2018-02-08 03:26:29 +00:00
|
|
|
if(!Array.isArray(value)) {
|
2015-11-13 03:47:34 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-12-28 17:39:41 +00:00
|
|
|
return value.includes(_.get(client, 'term.termType'));
|
2015-11-13 03:47:34 +00:00
|
|
|
},
|
|
|
|
TW : function termWidth() {
|
2018-12-28 17:39:41 +00:00
|
|
|
return !isNaN(value) && _.get(client, 'term.termWidth', 0) >= value;
|
2015-11-13 03:47:34 +00:00
|
|
|
},
|
2019-01-11 03:34:52 +00:00
|
|
|
ID : function isUserId() {
|
2018-12-28 17:39:41 +00:00
|
|
|
if(!user) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-02-08 03:26:29 +00:00
|
|
|
if(!Array.isArray(value)) {
|
2015-11-15 00:51:05 +00:00
|
|
|
value = [ value ];
|
|
|
|
}
|
2018-02-08 03:26:29 +00:00
|
|
|
return value.map(n => parseInt(n, 10)).includes(user.userId);
|
2015-11-13 03:47:34 +00:00
|
|
|
},
|
2015-11-13 23:13:20 +00:00
|
|
|
WD : function isOneOfDayOfWeek() {
|
2018-02-08 03:26:29 +00:00
|
|
|
if(!Array.isArray(value)) {
|
2015-11-15 00:51:05 +00:00
|
|
|
value = [ value ];
|
|
|
|
}
|
2018-02-08 03:26:29 +00:00
|
|
|
return value.map(n => parseInt(n, 10)).includes(new Date().getDay());
|
2015-11-13 03:47:34 +00:00
|
|
|
},
|
2015-11-13 23:13:20 +00:00
|
|
|
MM : function isMinutesPastMidnight() {
|
2018-02-08 03:26:29 +00:00
|
|
|
const now = moment();
|
|
|
|
const midnight = now.clone().startOf('day')
|
|
|
|
const minutesPastMidnight = now.diff(midnight, 'minutes');
|
|
|
|
return !isNaN(value) && minutesPastMidnight >= value;
|
2019-01-11 03:34:52 +00:00
|
|
|
},
|
|
|
|
AC : function achievementCount() {
|
|
|
|
if(!user) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const count = user.getPropertyAsNumber(UserProps.AchievementTotalCount) || 0;
|
|
|
|
return !isNan(value) && points >= value;
|
|
|
|
},
|
|
|
|
AP : function achievementPoints() {
|
|
|
|
if(!user) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const points = user.getPropertyAsNumber(UserProps.AchievementTotalPoints) || 0;
|
|
|
|
return !isNan(value) && points >= value;
|
2015-11-12 07:31:25 +00:00
|
|
|
}
|
2015-11-13 23:13:20 +00:00
|
|
|
}[acsCode](value);
|
2015-11-13 03:47:34 +00:00
|
|
|
} catch (e) {
|
2018-12-28 17:39:41 +00:00
|
|
|
const logger = _.get(client, 'log', Log);
|
|
|
|
logger.warn( { acsCode : acsCode, value : value }, 'Invalid ACS string!');
|
|
|
|
|
2015-11-13 03:47:34 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-11-11 06:05:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
start
|
2015-11-12 07:31:25 +00:00
|
|
|
= expr
|
2015-11-11 06:05:42 +00:00
|
|
|
|
|
|
|
expr
|
2015-11-12 07:31:25 +00:00
|
|
|
= orExpr
|
|
|
|
|
|
|
|
OR
|
|
|
|
= '|'
|
|
|
|
|
|
|
|
AND
|
|
|
|
= '&'
|
|
|
|
|
|
|
|
NOT
|
|
|
|
= '!'
|
2015-11-11 06:05:42 +00:00
|
|
|
|
2015-11-12 07:31:25 +00:00
|
|
|
groupOpen
|
|
|
|
= '('
|
2015-11-11 06:05:42 +00:00
|
|
|
|
2015-11-12 07:31:25 +00:00
|
|
|
groupClose
|
|
|
|
= ')'
|
2015-11-11 06:05:42 +00:00
|
|
|
|
2015-11-12 07:31:25 +00:00
|
|
|
orExpr
|
|
|
|
= left:andExpr OR right:expr { return left || right; }
|
|
|
|
/ andExpr
|
|
|
|
|
|
|
|
andExpr
|
|
|
|
= left:notExpr AND? right:expr { return left && right; }
|
|
|
|
/ notExpr
|
|
|
|
|
|
|
|
notExpr
|
|
|
|
= NOT value:atom { return !value; }
|
|
|
|
/ atom
|
2015-11-11 06:05:42 +00:00
|
|
|
|
|
|
|
atom
|
2015-11-12 07:31:25 +00:00
|
|
|
= acsCheck
|
|
|
|
/ groupOpen value:expr groupClose { return value; }
|
2015-11-11 06:05:42 +00:00
|
|
|
|
2015-11-12 07:31:25 +00:00
|
|
|
comma
|
|
|
|
= ','
|
2015-11-11 06:05:42 +00:00
|
|
|
|
2015-11-12 07:31:25 +00:00
|
|
|
ws
|
|
|
|
= ' '
|
2015-11-11 06:05:42 +00:00
|
|
|
|
2015-11-12 07:31:25 +00:00
|
|
|
optWs
|
|
|
|
= ws*
|
2015-11-11 06:05:42 +00:00
|
|
|
|
2015-11-12 07:31:25 +00:00
|
|
|
listOpen
|
|
|
|
= '['
|
|
|
|
|
|
|
|
listClose
|
|
|
|
= ']'
|
2015-11-11 06:05:42 +00:00
|
|
|
|
2015-11-12 07:31:25 +00:00
|
|
|
acsCheck
|
2015-11-13 23:13:20 +00:00
|
|
|
= acs:acsCode a:arg { return checkAccess(acs, a); }
|
2015-11-12 07:31:25 +00:00
|
|
|
|
2015-11-13 23:13:20 +00:00
|
|
|
acsCode
|
2015-11-13 03:47:34 +00:00
|
|
|
= c:([A-Z][A-Z]) { return c.join(''); }
|
2015-11-11 06:05:42 +00:00
|
|
|
|
|
|
|
argVar
|
2015-11-12 07:31:25 +00:00
|
|
|
= a:[A-Za-z0-9\-_\+]+ { return a.join('') }
|
2015-11-11 06:05:42 +00:00
|
|
|
|
|
|
|
commaList
|
2015-11-12 07:31:25 +00:00
|
|
|
= start:(v:argVar optWs comma optWs { return v; })* last:argVar { return start.concat(last); }
|
2015-11-11 06:05:42 +00:00
|
|
|
|
2015-11-12 07:31:25 +00:00
|
|
|
list
|
|
|
|
= listOpen l:commaList listClose { return l; }
|
2015-11-11 06:05:42 +00:00
|
|
|
|
2015-11-12 07:31:25 +00:00
|
|
|
number
|
|
|
|
= d:([0-9]+) { return parseInt(d.join(''), 10); }
|
2015-11-11 06:05:42 +00:00
|
|
|
|
|
|
|
arg
|
2015-11-12 07:31:25 +00:00
|
|
|
= list
|
|
|
|
/ num:number?
|
|
|
|
|
2015-11-11 06:05:42 +00:00
|
|
|
|