forked from andyt10/anon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
anon.coffee
62 lines (54 loc) · 1.69 KB
/
anon.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
Twit = require 'twit'
Netmask = require('netmask').Netmask
minimist = require 'minimist'
WikiChanges = require('wikichanges').WikiChanges
argv = minimist process.argv.slice(2), default: config: './config.json'
ipToInt = (ip) ->
octets = (parseInt(s) for s in ip.split('.'))
result = 0
result += n*Math.pow(255,i) for n,i in octets.reverse()
result
compareIps = (ip1, ip2) ->
q1 = ipToInt(ip1)
q2 = ipToInt(ip2)
if q1 == q2
r = 0
else if q1 < q2
r = -1
else
r = 1
return r
isIpInRange = (ip, block) ->
if Array.isArray block
return compareIps(ip, block[0]) >= 0 and compareIps(ip, block[1]) <= 0
else
return new Netmask(block).contains ip
isIpInAnyRange = (ip, blocks) ->
for block in blocks
if isIpInRange(ip, block)
return true
return false
main = ->
config = require(argv.config)
twitter = new Twit config unless argv['noop']
wikipedia = new WikiChanges(ircNickname: config.nick)
wikipedia.listen (edit) ->
# if we have an anonymous edit, then edit.user will be the ip address
# we iterate through each group of ip ranges looking for a match
if edit.anonymous
for name, ranges of config.ranges
if isIpInAnyRange edit.user, ranges
status = edit.page + ' Wikipedia article edited anonymously by ' + name + ' ' + edit.url
console.log status
return if argv.noop
twitter.post 'statuses/update', status: status, (err, d, r) ->
if err
console.log err
return
if require.main == module
main()
# export these for testing
exports.compareIps = compareIps
exports.isIpInRange = isIpInRange
exports.isIpInAnyRange = isIpInAnyRange
exports.run = main