forked from lefthand/hubot-dns-watch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dns-watch.coffee
66 lines (57 loc) · 1.98 KB
/
dns-watch.coffee
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
# Description:
# Upon request, watch domain for DNS changes
#
# Dependencies:
# None
#
# Configuration:
# None
#
# Commands:
# dns watch for <domain>
# dns watches - Returns a list of current watches
# end/stop/cancel dns watch for <domain>
#
# Author:
# lefthand
schedule = require('node-schedule');
dns = require('dns');
removeWatch = (robot, domain) ->
delete robot.brain.data.dns_watches[domain];
checkDomain = (robot, domain, watcher) ->
dns.lookup domain, (err, addresses, family) ->
if addresses != watcher.address
envelope = user: watcher.user_id, room: watcher.room
robot.send envelope, "@#{watcher.user_name} DNS for #{domain} changed to #{addresses}! See global propagation: https://www.whatsmydns.net/#A/#{domain}"
removeWatch robot, domain
module.exports = (robot) ->
robot.brain.data.dns_watches or= {}
regex = /dns.+(watch|change|update).* (http(s)?:\/\/)?([^\s/$.?#].[^\s^\/]*)/i
robot.respond regex, (res) ->
domain = res.match[4]
dns.lookup domain, (err, addresses, family) ->
watch =
address: addresses
user_id: res.message.user.id
user_name: res.message.user.name
room: res.message.user.room
robot.brain.data.dns_watches[domain] = watch
res.send "I'll post to ##{res.message.user.room} when DNS for #{domain} changes from #{addresses}"
return
regex = /dns watches/i
robot.respond regex, (res) ->
report = for domain, details of robot.brain.data.dns_watches
"#{domain}: #{details.address}"
if report.length > 0
res.send "```#{report.join("\n")}```"
else
res.send "No DNS watches are active."
regex = /(cancel|stop|end) dns watch.* (http(s)?:\/\/)?([^\s/$.?#].[^\s^\/]*)/i
robot.respond regex, (res) ->
domain = res.match[4]
removeWatch robot, domain
res.send "Ending DNS watch for #{domain}."
schedule.scheduleJob '* * * * *', () ->
for domain, details of robot.brain.data.dns_watches
checkDomain robot, domain, details
return