enigma-bbs/misc/acs_parser.pegjs

228 lines
5.7 KiB
Plaintext
Raw Normal View History

{
const client = options.client;
const user = options.client.user;
2018-11-24 05:02:36 +00:00
const UserProps = require('./user_property.js');
const moment = require('moment');
function checkAccess(acsCode, value) {
2015-11-13 03:47:34 +00:00
try {
return {
LC : function isLocalConnection() {
2015-11-13 03:47:34 +00:00
return client.isLocal();
},
AG : function ageGreaterOrEqualThan() {
2015-11-13 03:47:34 +00:00
return !isNaN(value) && user.getAge() >= value;
},
AS : function accountStatus() {
if(!Array.isArray(value)) {
value = [ value ];
}
2018-11-24 05:02:36 +00:00
const userAccountStatus = user.getPropertyAsNumber(UserProps.AccountStatus);
return value.map(n => parseInt(n, 10)).includes(userAccountStatus);
},
2015-11-13 03:47:34 +00:00
EC : function isEncoding() {
const encoding = client.term.outputEncoding.toLowerCase();
2015-11-13 03:47:34 +00:00
switch(value) {
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() {
if(!Array.isArray(value)) {
2015-11-13 03:47:34 +00:00
return false;
}
return value.some(groupName => user.isGroupMember(groupName));
2015-11-13 03:47:34 +00:00
},
NN : function isNode() {
if(!Array.isArray(value)) {
value = [ value ];
}
return value.map(n => parseInt(n, 10)).includes(client.node);
2015-11-13 03:47:34 +00:00
},
NP : function numberOfPosts() {
2018-11-24 05:02:36 +00:00
const postCount = user.getPropertyAsNumber(UserProps.PostCount) || 0;
return !isNaN(value) && postCount >= value;
2015-11-13 03:47:34 +00:00
},
NC : function numberOfCalls() {
2018-11-24 05:02:36 +00:00
const loginCount = user.getPropertyAsNumber(UserProps.LoginCount);
return !isNaN(value) && loginCount >= value;
2015-11-13 03:47:34 +00:00
},
AA : function accountAge() {
2018-11-24 05:02:36 +00:00
const accountCreated = moment(user.getProperty(UserProps.AccountCreated));
const now = moment();
const daysOld = accountCreated.diff(moment(), 'days');
return !isNaN(value) &&
accountCreated.isValid() &&
now.isAfter(accountCreated) &&
daysOld >= value;
},
BU : function bytesUploaded() {
2018-11-24 05:02:36 +00:00
const bytesUp = user.getPropertyAsNumber(UserProps.FileUlTotalBytes) || 0;
return !isNaN(value) && bytesUp >= value;
},
UP : function uploads() {
2018-11-24 05:02:36 +00:00
const uls = user.getPropertyAsNumber(UserProps.FileUlTotalCount) || 0;
return !isNaN(value) && uls >= value;
},
BD : function bytesDownloaded() {
2018-11-24 05:02:36 +00:00
const bytesDown = user.getPropertyAsNumber(UserProps.FileDlTotalBytes) || 0;
return !isNaN(value) && bytesDown >= value;
},
DL : function downloads() {
2018-11-24 05:02:36 +00:00
const dls = user.getPropertyAsNumber(UserProps.FileDlTotalCount) || 0;
return !isNaN(value) && dls >= value;
},
NR : function uploadDownloadRatioGreaterThan() {
2018-11-24 05:02:36 +00:00
const ulCount = user.getPropertyAsNumber(UserProps.FileUlTotalCount) || 0;
const dlCount = user.getPropertyAsNumber(UserProps.FileDlTotalCount) || 0;
const ratio = ~~((ulCount / dlCount) * 100);
return !isNaN(value) && ratio >= value;
},
KR : function uploadDownloadByteRatioGreaterThan() {
2018-11-24 05:02:36 +00:00
const ulBytes = user.getPropertyAsNumber(UserProps.FileUlTotalBytes) || 0;
const dlBytes = user.getPropertyAsNumber(UserProps.FileDlTotalBytes) || 0;
const ratio = ~~((ulBytes / dlBytes) * 100);
return !isNaN(value) && ratio >= value;
},
PC : function postCallRatio() {
2018-11-24 05:02:36 +00:00
const postCount = user.getPropertyAsNumber(UserProps.PostCount) || 0;
const loginCount = user.getPropertyAsNumber(UserProps.LoginCount) || 0;
const ratio = ~~((postCount / loginCount) * 100);
return !isNaN(value) && ratio >= value;
},
SC : function isSecureConnection() {
2015-11-13 03:47:34 +00:00
return client.session.isSecure;
},
ML : function minutesLeft() {
2015-11-13 03:47:34 +00:00
// :TODO: implement me!
return false;
},
TH : function termHeight() {
return !isNaN(value) && client.term.termHeight >= value;
},
TM : function isOneOfThemes() {
if(!Array.isArray(value)) {
2015-11-13 03:47:34 +00:00
return false;
}
return value.includes(client.currentTheme.name);
2015-11-13 03:47:34 +00:00
},
TT : function isOneOfTermTypes() {
if(!Array.isArray(value)) {
2015-11-13 03:47:34 +00:00
return false;
}
return value.includes(client.term.termType);
2015-11-13 03:47:34 +00:00
},
TW : function termWidth() {
return !isNaN(value) && client.term.termWidth >= value;
},
ID : function isUserId(value) {
if(!Array.isArray(value)) {
value = [ value ];
}
return value.map(n => parseInt(n, 10)).includes(user.userId);
2015-11-13 03:47:34 +00:00
},
WD : function isOneOfDayOfWeek() {
if(!Array.isArray(value)) {
value = [ value ];
}
return value.map(n => parseInt(n, 10)).includes(new Date().getDay());
2015-11-13 03:47:34 +00:00
},
MM : function isMinutesPastMidnight() {
const now = moment();
const midnight = now.clone().startOf('day')
const minutesPastMidnight = now.diff(midnight, 'minutes');
return !isNaN(value) && minutesPastMidnight >= value;
}
}[acsCode](value);
2015-11-13 03:47:34 +00:00
} catch (e) {
client.log.warn( { acsCode : acsCode, value : value }, 'Invalid ACS string!');
2015-11-13 03:47:34 +00:00
return false;
}
}
}
start
= expr
expr
= orExpr
OR
= '|'
AND
= '&'
NOT
= '!'
groupOpen
= '('
groupClose
= ')'
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
atom
= acsCheck
/ groupOpen value:expr groupClose { return value; }
comma
= ','
ws
= ' '
optWs
= ws*
listOpen
= '['
listClose
= ']'
acsCheck
= acs:acsCode a:arg { return checkAccess(acs, a); }
acsCode
2015-11-13 03:47:34 +00:00
= c:([A-Z][A-Z]) { return c.join(''); }
argVar
= a:[A-Za-z0-9\-_\+]+ { return a.join('') }
commaList
= start:(v:argVar optWs comma optWs { return v; })* last:argVar { return start.concat(last); }
list
= listOpen l:commaList listClose { return l; }
number
= d:([0-9]+) { return parseInt(d.join(''), 10); }
arg
= list
/ num:number?