2 * Copyright 2011 Hans Leidekker for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18 * Based on nsProxyAutoConfig.js from mozilla.org.
21 function myIpAddress() {
23 return dns_resolve('');
29 function dnsResolve(host
) {
31 return dns_resolve(host
);
37 function dnsDomainIs(host
, domain
) {
38 return (host
.length
>= domain
.length
&&
39 host
.substring(host
.length
- domain
.length
) == domain
);
42 function dnsDomainLevels(host
) {
43 return host
.split('.').length
-1;
46 function convert_addr(ipchars
) {
47 var bytes
= ipchars
.split('.');
48 var result
= ((bytes
[0] & 0xff) << 24) |
49 ((bytes
[1] & 0xff) << 16) |
50 ((bytes
[2] & 0xff) << 8) |
55 function isInNet(ipaddr
, pattern
, maskstr
) {
56 var test
= /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/.exec(ipaddr
);
58 ipaddr
= dnsResolve(ipaddr
);
61 } else if (test
[1] > 255 || test
[2] > 255 ||
62 test
[3] > 255 || test
[4] > 255) {
63 return false; // not an IP address
65 var host
= convert_addr(ipaddr
);
66 var pat
= convert_addr(pattern
);
67 var mask
= convert_addr(maskstr
);
68 return ((host
& mask
) == (pat
& mask
));
71 function isPlainHostName(host
) {
72 return (host
.search('\\.') == -1);
75 function isResolvable(host
) {
76 var ip
= dnsResolve(host
);
80 function localHostOrDomainIs(host
, hostdom
) {
81 return (host
== hostdom
) ||
82 (hostdom
.lastIndexOf(host
+ '.', 0) == 0);
85 function shExpMatch(url
, pattern
) {
86 pattern
= pattern
.replace(/\./g, '\\.');
87 pattern
= pattern
.replace(/\*/g, '.*');
88 pattern
= pattern
.replace(/\?/g, '.');
89 var newRe
= new RegExp('^'+pattern
+'$');
90 return newRe
.test(url
);
93 var wdays
= {SUN
: 0, MON
: 1, TUE
: 2, WED
: 3, THU
: 4, FRI
: 5, SAT
: 6};
94 var months
= {JAN
: 0, FEB
: 1, MAR
: 2, APR
: 3, MAY
: 4, JUN
: 5, JUL
: 6, AUG
: 7, SEP
: 8, OCT
: 9, NOV
: 10, DEC
: 11};
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
;
108 if (arguments
[argc
- 1] == 'GMT') {
110 wday
= date
.getUTCDay();
112 wday
= date
.getDay();
114 var wd1
= getDay(arguments
[0]);
115 var wd2
= (argc
== 2) ? getDay(arguments
[1]) : wd1
;
116 return (wd1
== -1 || wd2
== -1) ? false
117 : (wd1
<= wday
&& wday
<= wd2
);
120 function dateRange() {
121 function getMonth(name
) {
122 if (name
in months
) {
127 var date
= new Date();
128 var argc
= arguments
.length
;
132 var isGMT
= (arguments
[argc
- 1] == 'GMT');
137 // function will work even without explicit handling of this case
139 var tmp
= parseInt(arguments
[0]);
141 return ((isGMT
? date
.getUTCMonth() : date
.getMonth()) == getMonth(arguments
[0]));
142 } else if (tmp
< 32) {
143 return ((isGMT
? date
.getUTCDate() : date
.getDate()) == tmp
);
145 return ((isGMT
? date
.getUTCFullYear() : date
.getFullYear()) == tmp
);
148 var year
= date
.getFullYear();
150 date1
= new Date(year
, 0, 1, 0, 0, 0);
151 date2
= new Date(year
, 11, 31, 23, 59, 59);
152 var adjustMonth
= false;
153 for (var i
= 0; i
< (argc
>> 1); i
++) {
154 var tmp
= parseInt(arguments
[i
]);
156 var mon
= getMonth(arguments
[i
]);
158 } else if (tmp
< 32) {
159 adjustMonth
= (argc
<= 2);
162 date1
.setFullYear(tmp
);
165 for (var i
= (argc
>> 1); i
< argc
; i
++) {
166 var tmp
= parseInt(arguments
[i
]);
168 var mon
= getMonth(arguments
[i
]);
170 } else if (tmp
< 32) {
173 date2
.setFullYear(tmp
);
177 date1
.setMonth(date
.getMonth());
178 date2
.setMonth(date
.getMonth());
182 tmp
.setFullYear(date
.getUTCFullYear());
183 tmp
.setMonth(date
.getUTCMonth());
184 tmp
.setDate(date
.getUTCDate());
185 tmp
.setHours(date
.getUTCHours());
186 tmp
.setMinutes(date
.getUTCMinutes());
187 tmp
.setSeconds(date
.getUTCSeconds());
190 return ((date1
<= date
) && (date
<= date2
));
193 function timeRange() {
194 var argc
= arguments
.length
;
195 var date
= new Date();
201 if (arguments
[argc
- 1] == 'GMT') {
206 var hour
= isGMT
? date
.getUTCHours() : date
.getHours();
212 return (hour
== arguments
[0]);
213 } else if (argc
== 2) {
214 return ((arguments
[0] <= hour
) && (hour
<= arguments
[1]));
218 date1
.setSeconds(arguments
[2]);
219 date2
.setSeconds(arguments
[5]);
221 var middle
= argc
>> 1;
222 date1
.setHours(arguments
[0]);
223 date1
.setMinutes(arguments
[1]);
224 date2
.setHours(arguments
[middle
]);
225 date2
.setMinutes(arguments
[middle
+ 1]);
227 date2
.setSeconds(59);
231 throw 'timeRange: bad number of arguments'
236 date
.setFullYear(date
.getUTCFullYear());
237 date
.setMonth(date
.getUTCMonth());
238 date
.setDate(date
.getUTCDate());
239 date
.setHours(date
.getUTCHours());
240 date
.setMinutes(date
.getUTCMinutes());
241 date
.setSeconds(date
.getUTCSeconds());
243 return ((date1
<= date
) && (date
<= date2
));