forked from haraka/Haraka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
154 lines (133 loc) · 4.28 KB
/
utils.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Various utility functions
// copied from http://www.broofa.com/Tools/Math.uuid.js
var CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
exports.uuid = function () {
var chars = CHARS, uuid = new Array(36), rnd=0, r;
for (var i = 0; i < 36; i++) {
if (i==8 || i==13 || i==18 || i==23) {
uuid[i] = '-';
}
else if (i==14) {
uuid[i] = '4';
}
else {
if (rnd <= 0x02) rnd = 0x2000000 + (Math.random()*0x1000000)|0;
r = rnd & 0xf;
rnd = rnd >> 4;
uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
}
}
return uuid.join('');
};
exports.in_array = function (item, array) {
for (var i in array) {
if (item === array[i]) {
return true;
}
}
return false;
};
exports.sort_keys = function (obj) {
return Object.keys(obj).sort();
};
exports.uniq = function (arr) {
var out = [];
var o = 0;
for (var i=0,l=arr.length; i < l; i++) {
if (out.length === 0) {
out.push(arr[i]);
}
else if (out[o] != arr[i]) {
out.push(arr[i]);
o++;
}
}
return out;
}
exports.ISODate = function (d) {
function pad(n) {return n<10 ? '0'+n : n}
return d.getUTCFullYear()+'-'
+ pad(d.getUTCMonth()+1)+'-'
+ pad(d.getUTCDate())+'T'
+ pad(d.getUTCHours())+':'
+ pad(d.getUTCMinutes())+':'
+ pad(d.getUTCSeconds())+'Z'
}
// All scanadlously taken from http://ipv6blog.net/ipv6-validation-javascript/
// substr_count
//
// Support function; a javascript version of an original PHP function
// Found at: http://kevin.vanzonneveld.net
function substr_count (haystack, needle, offset, length) {
var pos = 0, cnt = 0;
haystack += '';
needle += '';
if (isNaN(offset)) {offset = 0;}
if (isNaN(length)) {length = 0;}
offset--;
while ((offset = haystack.indexOf(needle, offset+1)) != -1) {
if (length > 0 && (offset+needle.length) > length) {
return false;
}
else {
cnt++;
}
}
return cnt;
}
// is_ipv4
//
// Test for a valid dotted IPv4 address
//
// Ported from: http://www.dijksterhuis.org/regular-expressions-csharp-practical-use/
var is_ipv4 = exports.is_ipv4 = function (ip) {
var match = ip.match(/^(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/);
return match != null;
}
// is_ipv6
//
// Test if the input is a valid ipv6 address. Javascript version of an original PHP function.
//
// Ported from: http://crisp.tweakblogs.net/blog/2031
exports.is_ipv6 = function (ip) {
// Test for empty address
if (ip.length<3) {
return ip === "::";
}
// Check if part is in IPv4 format
if (ip.indexOf('.')>0) {
lastcolon = ip.lastIndexOf(':');
if (!(lastcolon && is_ipv4(ip.substr(lastcolon + 1))))
return false;
// replace IPv4 part with dummy
ip = ip.substr(0, lastcolon) + ':0:0';
}
// Check uncompressed
if (ip.indexOf('::')<0) {
var match = ip.match(/^(?:[a-f0-9]{1,4}:){7}[a-f0-9]{1,4}$/i);
return match !== null;
}
// Check colon-count for compressed format
if (substr_count(ip, ':') < 8) {
var match = ip.match(/^(?::|(?:[a-f0-9]{1,4}:)+):(?:(?:[a-f0-9]{1,4}:)*[a-f0-9]{1,4})?$/i);
return match !== null;
}
// Not a valid IPv6 address
return false;
}
exports.expand_ipv6 = function (ip) {
function pad(n) {return (new Array(5-n.length).join('0'))+n}
if (ip.indexOf('::')>=0) {
// Sigh, magic number time, we want 7 colons in the final string
// We count the number of colons that are already there to decide how many to add (7 - count)
// We then reduce the count by two (7 - (count - 2)) because of the double colon
// The are prefixing the replacement with a colon so remove one (7 - (count - 2) - 1)
// and then we add one for the size of the array (7 - (count - 2) - 1 + 1)
ip = ip.replace('::', ':' + new Array(9 - substr_count(ip, ':')).join('0:'));
}
var gobbles = ip.split(':');
for (var gobble=0; gobble < 8; gobble++) {
gobbles[gobble]=pad(gobbles[gobble]);
}
return gobbles.join(':');
}