-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
101 lines (87 loc) · 2.19 KB
/
index.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
var dns = require('dns');
module.exports = function(jarvis, module) {
var recordTypes = [ 'A', 'AAAA', 'MX', 'TXT', 'PTR', 'NS', 'CNAME' ];
module.addAction(module.createCommand({
match: /^resolve (?:(.*) record for )?(.*)$/i,
func: function(message, type, domain) {
if (type === undefined) {
type = 'A';
}
type = type.toUpperCase();
if (recordTypes.indexOf(type) === -1) {
jarvis.reply(message, 'Invalid record type. ' + recordTypes.slice(0, -1).join(', ') + ' and ' + recordTypes.slice(-1) + ' available.');
return;
}
dns.resolve(domain, type, function(err, addresses) {
var reply;
if (err) {
jarvis.reply(message, handleError(err.code));
return;
}
reply = type + ' record for ' + domain + ' resolves to ';
switch (type) {
case 'A':
case 'AAAA':
reply += addresses[0];
break;
case 'MX':
var domains = [];
addresses.forEach(function(value) {
domains.push(value.exchange + ' (' + value.priority + ')');
});
reply += domains.join(', ');
break;
case 'TXT':
var results = [];
addresses.forEach(function(value) {
results.push('"' + value + '"');
});
reply += results.join(', ');
break;
case 'PTR':
case 'NS':
case 'CNAME':
reply += addresses.join(', ');
break;
}
jarvis.reply(message, reply);
});
}
}));
module.addAction(module.createCommand({
match: 'reverse resolve :ip',
func: function(message, ip) {
dns.reverse(ip, function(err, domains) {
if (err) {
jarvis.reply(message, handleError(err.code));
return;
}
jarvis.reply(message, ip + ' reverse resolves to ' + domains.join(', '));
});
}
}));
};
function handleError(code) {
var reply;
switch(code) {
case 'ETEMPFAIL':
reply = 'Timeout containg DNS server.';
break;
case 'EPROTOCOL':
reply = 'Got garbled reply.';
break;
case 'ENXDOMAIN':
reply = 'Domain does not exist.';
break;
case 'ENODATA':
reply = 'No record found.';
break;
case 'ENOMEM':
reply = 'Out of memory while processing.';
break;
case 'EBADQUERY':
reply = 'The query was malformed.';
break;
}
return reply;
}