forked from Ricket/nodebot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathregexFactory.js
64 lines (53 loc) · 1.77 KB
/
regexFactory.js
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
53
54
55
56
57
58
59
60
61
62
63
/**
* Scripts should use these functions to populate the regex parameter
* of the listen function in a standardized way.
*/
require("./config.js");
var _ = require("lodash");
// http://stackoverflow.com/a/6969486
function escapeRegExp(str) {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
}
function ensureArray(stringOrArray) {
if (typeof stringOrArray === "string") {
return [stringOrArray];
} else {
return stringOrArray;
}
}
function makePrefix(prefixed) {
if (prefixed === false) {
return "";
} else {
return "(?:"
+ escapeRegExp(nodebot_prefs.command_prefix) + " ?"
+ "|"
+ escapeRegExp(nodebot_prefs.nickname) + " *[:,]? +"
+ ")"
+ (prefixed === "optional" ? "?" : "");
}
}
function matchAny(strings, escaped) {
return "(?:"
+ _.map(strings, function (s) { return (escaped === false) ? "(?:" + s + ")" : escapeRegExp(s); }).join("|")
+ ")";
}
exports.password = function () {
return new RegExp(
"^(PASS |PRIVMSG " + nodebot_prefs.nickserv_nickname + " :IDENTIFY )", "i");
};
exports.only = function (keywords, prefixed) {
keywords = ensureArray(keywords);
return new RegExp(
"PRIVMSG [^ ]+ :" + makePrefix(prefixed) + matchAny(keywords) + "$", "i");
};
exports.startsWith = function (keywords, prefixed) {
keywords = ensureArray(keywords);
return new RegExp(
"PRIVMSG [^ ]+ :" + makePrefix(prefixed) + matchAny(keywords) + "\\b ?(.*)$", "i");
};
exports.matches = function (regexStrings, prefixed, only) {
regexStrings = ensureArray(regexStrings);
return new RegExp(
"PRIVMSG [^ ]+ :" + makePrefix(prefixed) + matchAny(regexStrings, false) + (only ? "$" : ""), "i");
};