| 1 |
/* |
| 2 |
* The following Javascript code was taken from Mozilla (http://www.mozilla.org) |
| 3 |
* and is licensed according to the LGPLv2.1+. Below is the original copyright |
| 4 |
* header as contained in the source file (nsProxyAutoConfig.js) |
| 5 |
*/ |
| 6 |
|
| 7 |
/* ***** BEGIN LICENSE BLOCK ***** |
| 8 |
* Version: NPL 1.1/GPL 2.0/LGPL 2.1 |
| 9 |
* |
| 10 |
* The contents of this file are subject to the Netscape Public License |
| 11 |
* Version 1.1 (the "License"); you may not use this file except in |
| 12 |
* compliance with the License. You may obtain a copy of the License at |
| 13 |
* http://www.mozilla.org/NPL/ |
| 14 |
* |
| 15 |
* Software distributed under the License is distributed on an "AS IS" basis, |
| 16 |
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
| 17 |
* for the specific language governing rights and limitations under the |
| 18 |
* License. |
| 19 |
* |
| 20 |
* The Original Code is mozilla.org code. |
| 21 |
* |
| 22 |
* The Initial Developer of the Original Code is |
| 23 |
* Netscape Communications Corporation. |
| 24 |
* Portions created by the Initial Developer are Copyright (C) 1998 |
| 25 |
* the Initial Developer. All Rights Reserved. |
| 26 |
* |
| 27 |
* Contributor(s): |
| 28 |
* Akhil Arora <[email protected]> |
| 29 |
* Tomi Leppikangas <[email protected]> |
| 30 |
* |
| 31 |
* Alternatively, the contents of this file may be used under the terms of |
| 32 |
* either the GNU General Public License Version 2 or later (the "GPL"), or |
| 33 |
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), |
| 34 |
* in which case the provisions of the GPL or the LGPL are applicable instead |
| 35 |
* of those above. If you wish to allow use of your version of this file only |
| 36 |
* under the terms of either the GPL or the LGPL, and not to allow others to |
| 37 |
* use your version of this file under the terms of the NPL, indicate your |
| 38 |
* decision by deleting the provisions above and replace them with the notice |
| 39 |
* and other provisions required by the GPL or the LGPL. If you do not delete |
| 40 |
* the provisions above, a recipient may use your version of this file under |
| 41 |
* the terms of any one of the NPL, the GPL or the LGPL. |
| 42 |
* |
| 43 |
* ***** END LICENSE BLOCK ***** */ |
| 44 |
|
| 45 |
#define JAVASCRIPT_ROUTINES \ |
| 46 |
"function dnsDomainIs(host, domain) {\n" \ |
| 47 |
" return (host.length >= domain.length &&\n" \ |
| 48 |
" host.substring(host.length - domain.length) == domain);\n" \ |
| 49 |
"}\n" \ |
| 50 |
"function dnsDomainLevels(host) {\n" \ |
| 51 |
" return host.split('.').length-1;\n" \ |
| 52 |
"}\n" \ |
| 53 |
"function convert_addr(ipchars) {\n" \ |
| 54 |
" var bytes = ipchars.split('.');\n" \ |
| 55 |
" var result = ((bytes[0] & 0xff) << 24) |\n" \ |
| 56 |
" ((bytes[1] & 0xff) << 16) |\n" \ |
| 57 |
" ((bytes[2] & 0xff) << 8) |\n" \ |
| 58 |
" (bytes[3] & 0xff);\n" \ |
| 59 |
" return result;\n" \ |
| 60 |
"}\n" \ |
| 61 |
"function isInNet(ipaddr, pattern, maskstr) {\n"\ |
| 62 |
" var test = /^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$/.exec(ipaddr);\n"\ |
| 63 |
" if (test == null) {\n"\ |
| 64 |
" ipaddr = dnsResolve(ipaddr);\n"\ |
| 65 |
" if (ipaddr == null)\n"\ |
| 66 |
" return false;\n"\ |
| 67 |
" } else if (test[1] > 255 || test[2] > 255 || \n"\ |
| 68 |
" test[3] > 255 || test[4] > 255) {\n"\ |
| 69 |
" return false; // not an IP address\n"\ |
| 70 |
" }\n"\ |
| 71 |
" var host = convert_addr(ipaddr);\n"\ |
| 72 |
" var pat = convert_addr(pattern);\n"\ |
| 73 |
" var mask = convert_addr(maskstr);\n"\ |
| 74 |
" return ((host & mask) == (pat & mask));\n"\ |
| 75 |
" \n"\ |
| 76 |
"}\n"\ |
| 77 |
"function isPlainHostName(host) {\n" \ |
| 78 |
" return (host.search('\\\\.') == -1);\n" \ |
| 79 |
"}\n" \ |
| 80 |
"function isResolvable(host) {\n" \ |
| 81 |
" var ip = dnsResolve(host);\n" \ |
| 82 |
" return (ip != null);\n" \ |
| 83 |
"}\n" \ |
| 84 |
"function localHostOrDomainIs(host, hostdom) {\n" \ |
| 85 |
" if (isPlainHostName(host)) {\n" \ |
| 86 |
" return (hostdom.search('/^' + host + '/') != -1);\n" \ |
| 87 |
" }\n" \ |
| 88 |
" else {\n" \ |
| 89 |
" return (host == hostdom);\n" \ |
| 90 |
" }\n" \ |
| 91 |
"}\n" \ |
| 92 |
"function shExpMatch(url, pattern) {\n" \ |
| 93 |
" pattern = pattern.replace(/\\./g, '\\\\.');\n" \ |
| 94 |
" pattern = pattern.replace(/\\*/g, '.*');\n" \ |
| 95 |
" pattern = pattern.replace(/\\?/g, '.');\n" \ |
| 96 |
" var newRe = new RegExp('^'+pattern+'$');\n" \ |
| 97 |
" return newRe.test(url);\n" \ |
| 98 |
"}\n" \ |
| 99 |
"var wdays = {SUN: 0, MON: 1, TUE: 2, WED: 3, THU: 4, FRI: 5, SAT: 6};\n" \ |
| 100 |
"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};\n"\ |
| 101 |
"function weekdayRange() {\n" \ |
| 102 |
" function getDay(weekday) {\n" \ |
| 103 |
" if (weekday in wdays) {\n" \ |
| 104 |
" return wdays[weekday];\n" \ |
| 105 |
" }\n" \ |
| 106 |
" return -1;\n" \ |
| 107 |
" }\n" \ |
| 108 |
" var date = new Date();\n" \ |
| 109 |
" var argc = arguments.length;\n" \ |
| 110 |
" var wday;\n" \ |
| 111 |
" if (argc < 1)\n" \ |
| 112 |
" return false;\n" \ |
| 113 |
" if (arguments[argc - 1] == 'GMT') {\n" \ |
| 114 |
" argc--;\n" \ |
| 115 |
" wday = date.getUTCDay();\n" \ |
| 116 |
" } else {\n" \ |
| 117 |
" wday = date.getDay();\n" \ |
| 118 |
" }\n" \ |
| 119 |
" var wd1 = getDay(arguments[0]);\n" \ |
| 120 |
" var wd2 = (argc == 2) ? getDay(arguments[1]) : wd1;\n" \ |
| 121 |
" return (wd1 == -1 || wd2 == -1) ? false\n" \ |
| 122 |
" : (wd1 <= wday && wday <= wd2);\n" \ |
| 123 |
"}\n" \ |
| 124 |
"function dateRange() {\n" \ |
| 125 |
" function getMonth(name) {\n" \ |
| 126 |
" if (name in months) {\n" \ |
| 127 |
" return months[name];\n" \ |
| 128 |
" }\n" \ |
| 129 |
" return -1;\n" \ |
| 130 |
" }\n" \ |
| 131 |
" var date = new Date();\n" \ |
| 132 |
" var argc = arguments.length;\n" \ |
| 133 |
" if (argc < 1) {\n" \ |
| 134 |
" return false;\n" \ |
| 135 |
" }\n" \ |
| 136 |
" var isGMT = (arguments[argc - 1] == 'GMT');\n" \ |
| 137 |
" if (isGMT) {\n" \ |
| 138 |
" argc--;\n" \ |
| 139 |
" }\n" \ |
| 140 |
" if (argc == 1) {\n" \ |
| 141 |
" var tmp = parseInt(arguments[0]);\n" \ |
| 142 |
" if (isNaN(tmp)) {\n" \ |
| 143 |
" return ((isGMT ? date.getUTCMonth() : date.getMonth()) == getMonth(arguments[0]));\n" \ |
| 144 |
" } else if (tmp < 32) {\n" \ |
| 145 |
" return ((isGMT ? date.getUTCDate() : date.getDate()) == tmp);\n" \ |
| 146 |
" } else {\n" \ |
| 147 |
" return ((isGMT ? date.getUTCFullYear() : date.getFullYear()) == tmp);\n" \ |
| 148 |
" }\n" \ |
| 149 |
" }\n" \ |
| 150 |
" var year = date.getFullYear();\n" \ |
| 151 |
" var date1, date2;\n" \ |
| 152 |
" date1 = new Date(year, 0, 1, 0, 0, 0);\n" \ |
| 153 |
" date2 = new Date(year, 11, 31, 23, 59, 59);\n" \ |
| 154 |
" var adjustMonth = false;\n" \ |
| 155 |
" for (var i = 0; i < (argc >> 1); i++) {\n" \ |
| 156 |
" var tmp = parseInt(arguments[i]);\n" \ |
| 157 |
" if (isNaN(tmp)) {\n" \ |
| 158 |
" var mon = getMonth(arguments[i]);\n" \ |
| 159 |
" date1.setMonth(mon);\n" \ |
| 160 |
" } else if (tmp < 32) {\n" \ |
| 161 |
" adjustMonth = (argc <= 2);\n" \ |
| 162 |
" date1.setDate(tmp);\n" \ |
| 163 |
" } else {\n" \ |
| 164 |
" date1.setFullYear(tmp);\n" \ |
| 165 |
" }\n" \ |
| 166 |
" }\n" \ |
| 167 |
" for (var i = (argc >> 1); i < argc; i++) {\n" \ |
| 168 |
" var tmp = parseInt(arguments[i]);\n" \ |
| 169 |
" if (isNaN(tmp)) {\n" \ |
| 170 |
" var mon = getMonth(arguments[i]);\n" \ |
| 171 |
" date2.setMonth(mon);\n" \ |
| 172 |
" } else if (tmp < 32) {\n" \ |
| 173 |
" date2.setDate(tmp);\n" \ |
| 174 |
" } else {\n" \ |
| 175 |
" date2.setFullYear(tmp);\n" \ |
| 176 |
" }\n" \ |
| 177 |
" }\n" \ |
| 178 |
" if (adjustMonth) {\n" \ |
| 179 |
" date1.setMonth(date.getMonth());\n" \ |
| 180 |
" date2.setMonth(date.getMonth());\n" \ |
| 181 |
" }\n" \ |
| 182 |
" if (isGMT) {\n" \ |
| 183 |
" var tmp = date;\n" \ |
| 184 |
" tmp.setFullYear(date.getUTCFullYear());\n" \ |
| 185 |
" tmp.setMonth(date.getUTCMonth());\n" \ |
| 186 |
" tmp.setDate(date.getUTCDate());\n" \ |
| 187 |
" tmp.setHours(date.getUTCHours());\n" \ |
| 188 |
" tmp.setMinutes(date.getUTCMinutes());\n" \ |
| 189 |
" tmp.setSeconds(date.getUTCSeconds());\n" \ |
| 190 |
" date = tmp;\n" \ |
| 191 |
" }\n" \ |
| 192 |
" return ((date1 <= date) && (date <= date2));\n" \ |
| 193 |
"}\n" \ |
| 194 |
"function timeRange() {\n" \ |
| 195 |
" var argc = arguments.length;\n" \ |
| 196 |
" var date = new Date();\n" \ |
| 197 |
" var isGMT= false;\n" \ |
| 198 |
" if (argc < 1) {\n" \ |
| 199 |
" return false;\n" \ |
| 200 |
" }\n" \ |
| 201 |
" if (arguments[argc - 1] == 'GMT') {\n" \ |
| 202 |
" isGMT = true;\n" \ |
| 203 |
" argc--;\n" \ |
| 204 |
" }\n" \ |
| 205 |
" var hour = isGMT ? date.getUTCHours() : date.getHours();\n" \ |
| 206 |
" var date1, date2;\n" \ |
| 207 |
" date1 = new Date();\n" \ |
| 208 |
" date2 = new Date();\n" \ |
| 209 |
" if (argc == 1) {\n" \ |
| 210 |
" return (hour == arguments[0]);\n" \ |
| 211 |
" } else if (argc == 2) {\n" \ |
| 212 |
" return ((arguments[0] <= hour) && (hour <= arguments[1]));\n" \ |
| 213 |
" } else {\n" \ |
| 214 |
" switch (argc) {\n" \ |
| 215 |
" case 6:\n" \ |
| 216 |
" date1.setSeconds(arguments[2]);\n" \ |
| 217 |
" date2.setSeconds(arguments[5]);\n" \ |
| 218 |
" case 4:\n" \ |
| 219 |
" var middle = argc >> 1;\n" \ |
| 220 |
" date1.setHours(arguments[0]);\n" \ |
| 221 |
" date1.setMinutes(arguments[1]);\n" \ |
| 222 |
" date2.setHours(arguments[middle]);\n" \ |
| 223 |
" date2.setMinutes(arguments[middle + 1]);\n" \ |
| 224 |
" if (middle == 2) {\n" \ |
| 225 |
" date2.setSeconds(59);\n" \ |
| 226 |
" }\n" \ |
| 227 |
" break;\n" \ |
| 228 |
" default:\n" \ |
| 229 |
" throw 'timeRange: bad number of arguments'\n" \ |
| 230 |
" }\n" \ |
| 231 |
" }\n" \ |
| 232 |
" if (isGMT) {\n" \ |
| 233 |
" date.setFullYear(date.getUTCFullYear());\n" \ |
| 234 |
" date.setMonth(date.getUTCMonth());\n" \ |
| 235 |
" date.setDate(date.getUTCDate());\n" \ |
| 236 |
" date.setHours(date.getUTCHours());\n" \ |
| 237 |
" date.setMinutes(date.getUTCMinutes());\n" \ |
| 238 |
" date.setSeconds(date.getUTCSeconds());\n" \ |
| 239 |
" }\n" \ |
| 240 |
" return ((date1 <= date) && (date <= date2));\n" \ |
| 241 |
"}\n" \ |
| 242 |
"" |