Skip to content

Instantly share code, notes, and snippets.

@donpdonp
Last active October 12, 2018 20:03
Show Gist options
  • Save donpdonp/b17418c3162fa2b064a25f9671db894f to your computer and use it in GitHub Desktop.
Save donpdonp/b17418c3162fa2b064a25f9671db894f to your computer and use it in GitHub Desktop.
gluon kickstats
(function(){
return {name: "kickstats"}
})
var prefix="kickstats:"
function go(msg) {
if (msg.method == "irc.privmsg") {
var cmd_match = /!kickstats(\s+(.*))?$/.exec(msg.params.message)
if (cmd_match) {
stats(msg, cmd_match[2])
}
}
if (msg.method == "irc.join") {
var say = ["join",msg.params.channel, msg.params.user.nick, msg.params.user.host]
var ip = msg.params.user.host.split('@')[1]
if(ip) {
var url = "http://cli.fyi/"+ip
var json = http.get(url)
if(json) {
var fyi = JSON.parse(json)
if(fyi.type == "Domain Name Information") {
var dip = fyi.data.dns[0].split(' ')
var ip_tail = dip[dip.length-1]
if(ip_tail) {
ip_save('nick', msg.params.user.nick, ip_tail)
var json2 = http.get("http://cli.fyi/"+ip_tail)
var fyi2 = JSON.parse(json2)
}
}
if(fyi.type == "IP Address") {
ip_save('nick', msg.params.user.nick, ip, function(value){})
say.push("["+fyi.data.countryCode +"/"+ fyi.data.organisation+"]")
}
}
}
//bot.say(bot.admin_channel, say.join(' '))
}
if (msg.method == "irc.kick") {
var fullkey = prefix+'nick'+':'+msg.params.nick
db.get(fullkey, function(ip) {
ip_save('ip', ip, null, function(value){
var channel = bot.admin_channel
if(value > 1) channel = '#pdxbots'
bot.say(channel, "KICK "+msg.params.channel+" "+msg.params.nick+" (joined from "+ip+
" "+msg.params.reason+" by "+msg.params.user.nick)
var url = "http://cli.fyi/"+ip
var json = http.get(url)
if(json) {
var fyi = JSON.parse(json)
ip_save('cn', fyi.data.countryCode, null, function(value){
var sass
if(value == 1) {
sass = "has entered the game."
} else {
sass = "scores with "+value+" on the board."
}
bot.say(bot.admin_channel, 'kickstats '+fyi.data.countryCode+' '+sass)
})
}
})
//ip_save('cn', fyi2.data.countryCode, msg.params.user.nick)
})
}
if (msg.method == "irc.mode") {
//bot.say('#pdxbots', "mode "+msg.params.channel+" "+msg.params.mode+" "+msg.params.nick+" by "+msg.params.user.nick)
}
}
function stats(msg) {
// var ips = scan('nick', function(nicks, loop){
// bot.say(msg.params.channel, "kick stats nicks: "+nicks.length+" (after redis loop "+loop+")")
// })
// var ips = scan('ip', function(ips){
// bot.say(msg.params.channel, "kick stats ip: "+ips.length)
// })
var ips = scan('cn', function(cns){
var sorted = cns.sort(function(a,b){return b[1]-a[1]})
bot.say(msg.params.channel, "kick stats countries: "+sorted.join(', '))
})
}
function ip_save(type, key, value, cb) {
var fullkey = prefix+type+':'+key
db.get(fullkey, function(oldvalue) {
if(!value) {
if(!oldvalue) {
oldvalue = 0
}
value = parseInt(oldvalue)+1
}
//bot.say(bot.admin_channel, 'db '+fullkey+' set to '+value+' '+ (oldvalue ? 'was '+oldvalue: ''))
db.set(fullkey, value, function(){if(cb) cb(value)})
})
}
function scan(match, cb, cursor, answers, loop) {
if(cursor == null) { cursor = 0; answers = []; loop = 1 }
loop = loop + 1
var glob = prefix+match+':*'
db.scan(cursor, glob, 100, function(result){
cursor = result[0]
var pairs = result[1].reduce(function(acc, val, idx){
if (idx % 2 == 0) {
var key_parts = val.split(':')
acc.push([key_parts[key_parts.length-1]])
} else {
acc[acc.length-1].push(val)
}
return acc
}, [])
answers = answers.concat(pairs)
if(cursor == 0) {
cb(answers, loop)
} else {
if(loop < 1500) {
scan(match, cb, cursor, answers, loop)
} else {
cb("kickstats scan "+match+" overload. stopping.")
}
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment