1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 /* global dnsResolve */
7 function dnsDomainIs(host, domain) {
9 host.length >= domain.length &&
10 host.substring(host.length - domain.length) == domain
14 function dnsDomainLevels(host) {
15 return host.split(".").length - 1;
18 function isValidIpAddress(ipchars) {
19 var matches = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/.exec(ipchars);
20 if (matches == null) {
33 function convert_addr(ipchars) {
34 var bytes = ipchars.split(".");
36 ((bytes[0] & 0xff) << 24) |
37 ((bytes[1] & 0xff) << 16) |
38 ((bytes[2] & 0xff) << 8) |
43 function isInNet(ipaddr, pattern, maskstr) {
44 if (!isValidIpAddress(pattern) || !isValidIpAddress(maskstr)) {
47 if (!isValidIpAddress(ipaddr)) {
48 ipaddr = dnsResolve(ipaddr);
53 var host = convert_addr(ipaddr);
54 var pat = convert_addr(pattern);
55 var mask = convert_addr(maskstr);
56 return (host & mask) == (pat & mask);
59 function isPlainHostName(host) {
60 return host.search("(\\.)|:") == -1;
63 function isResolvable(host) {
64 var ip = dnsResolve(host);
68 function localHostOrDomainIs(host, hostdom) {
69 return host == hostdom || hostdom.lastIndexOf(host + ".", 0) == 0;
72 function shExpMatch(url, pattern) {
73 pattern = pattern.replace(/\./g, "\\.");
74 pattern = pattern.replace(/\*/g, ".*");
75 pattern = pattern.replace(/\?/g, ".");
76 var newRe = new RegExp("^" + pattern + "$");
77 return newRe.test(url);
80 var wdays = { SUN: 0, MON: 1, TUE: 2, WED: 3, THU: 4, FRI: 5, SAT: 6 };
96 function weekdayRange() {
97 function getDay(weekday) {
98 if (weekday in wdays) {
99 return wdays[weekday];
103 var date = new Date();
104 var argc = arguments.length;
109 if (arguments[argc - 1] == "GMT") {
111 wday = date.getUTCDay();
113 wday = date.getDay();
115 var wd1 = getDay(arguments[0]);
116 var wd2 = argc == 2 ? getDay(arguments[1]) : wd1;
117 if (wd1 == -1 || wd2 == -1) {
122 return wd1 <= wday && wday <= wd2;
125 return wd2 >= wday || wday >= wd1;
128 function dateRange() {
129 function getMonth(name) {
130 if (name in months) {
135 var date = new Date();
136 var argc = arguments.length;
140 var isGMT = arguments[argc - 1] == "GMT";
145 // function will work even without explict handling of this case
147 let tmp = parseInt(arguments[0]);
150 (isGMT ? date.getUTCMonth() : date.getMonth()) == getMonth(arguments[0])
152 } else if (tmp < 32) {
153 return (isGMT ? date.getUTCDate() : date.getDate()) == tmp;
155 return (isGMT ? date.getUTCFullYear() : date.getFullYear()) == tmp;
157 var year = date.getFullYear();
159 date1 = new Date(year, 0, 1, 0, 0, 0);
160 date2 = new Date(year, 11, 31, 23, 59, 59);
161 var adjustMonth = false;
162 for (let i = 0; i < argc >> 1; i++) {
163 let tmp = parseInt(arguments[i]);
165 let mon = getMonth(arguments[i]);
167 } else if (tmp < 32) {
168 adjustMonth = argc <= 2;
171 date1.setFullYear(tmp);
174 for (let i = argc >> 1; i < argc; i++) {
175 let tmp = parseInt(arguments[i]);
177 let mon = getMonth(arguments[i]);
179 } else if (tmp < 32) {
182 date2.setFullYear(tmp);
186 date1.setMonth(date.getMonth());
187 date2.setMonth(date.getMonth());
191 tmp.setFullYear(date.getUTCFullYear());
192 tmp.setMonth(date.getUTCMonth());
193 tmp.setDate(date.getUTCDate());
194 tmp.setHours(date.getUTCHours());
195 tmp.setMinutes(date.getUTCMinutes());
196 tmp.setSeconds(date.getUTCSeconds());
199 return date1 <= date2
200 ? date1 <= date && date <= date2
201 : date2 >= date || date >= date1;
204 function timeRange() {
205 var argc = arguments.length;
206 var date = new Date();
211 if (arguments[argc - 1] == "GMT") {
216 var hour = isGMT ? date.getUTCHours() : date.getHours();
222 return hour == arguments[0];
223 } else if (argc == 2) {
224 return arguments[0] <= hour && hour <= arguments[1];
228 date1.setSeconds(arguments[2]);
229 date2.setSeconds(arguments[5]);
232 var middle = argc >> 1;
233 date1.setHours(arguments[0]);
234 date1.setMinutes(arguments[1]);
235 date2.setHours(arguments[middle]);
236 date2.setMinutes(arguments[middle + 1]);
238 date2.setSeconds(59);
242 throw new Error("timeRange: bad number of arguments");
246 date.setFullYear(date.getUTCFullYear());
247 date.setMonth(date.getUTCMonth());
248 date.setDate(date.getUTCDate());
249 date.setHours(date.getUTCHours());
250 date.setMinutes(date.getUTCMinutes());
251 date.setSeconds(date.getUTCSeconds());
253 return date1 <= date2
254 ? date1 <= date && date <= date2
255 : date2 >= date || date >= date1;