diff --git a/src/main.js b/src/main.js index 5ddb0f6..b66169e 100644 --- a/src/main.js +++ b/src/main.js @@ -1,4 +1,5 @@ var cfg = require('../../cfg.json'); +var fs = require('fs'); var request = require('request'); var mysql = require('mysql'); var bot = require('coffea')(); @@ -30,19 +31,53 @@ bot.on('message', (e) => { var orig = e.message; if(e.channel.getName().toString() == "#f0ck") { - - /*var u = "f0ck"; - bot.whois(u, e.network, (m)=>{ }); - setTimeout(() => { - var user = bot.getUser(u, e.network); - e.reply(user); - }, 2000);*/ - if(orig.match(/(https?:\/\/)?[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?/gi)) { var tmp = orig.match(/(https?:\/\/)?[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?/gi); - e.reply(tmp); + tmp.forEach((entry,i,a) => { + dl(entry, "./b/test.png", (cb) => { + e.reply(cb); + }); + }); } + else if(orig.match(/^\.test/)) { + var tmp = getUser(e.user.getNick(), e.network); + + setTimeout(()=>{ + e.reply(tmp); + }, 2000); + } } -}); \ No newline at end of file +}); + + +var getUser = (u, n) => { + bot.whois(u, n, (fn)=>{ }); + bot.getUser(u, n); + + var start = Date.now(); + while (Date.now() < start + 3000) {} + + return bot.getUser(u, n); +}; + +var dl = (url, dest, cb) => { + var file = fs.createWriteStream(dest); + var sendReq = request.get(url); + sendReq.on('response', (response) => { + if(response.statusCode !== 200) return cb('Response status was ' + response.statusCode); + }); + sendReq.on('error', (err) => { + fs.unlink(dest); + if(cb) return cb(err.message); + }); + sendReq.pipe(file); + file.on('finish', () => { + file.close(cb); + }); + file.on('error', (err) => { + fs.unlink(dest); + if(cb) return cb(err.message); + }); +}; \ No newline at end of file