From 839186ad13ce67afe23885176b76f6aabc110e18 Mon Sep 17 00:00:00 2001 From: Flummi Date: Sun, 14 Aug 2016 03:41:31 +0200 Subject: [PATCH] modified: src/main.js --- src/main.js | 53 ++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 42 insertions(+), 11 deletions(-) diff --git a/src/main.js b/src/main.js index f0b24e6..7c82b80 100644 --- a/src/main.js +++ b/src/main.js @@ -10,6 +10,18 @@ var allowed = [ 'image/png', 'video/webm' ]; +var sql; +function haDC() { + sql = mysql.createConnection(cfg.mysql); + sql.connect((err) => { + if(err) setTimeout(haDC,2000); + }); + sql.on('error', (err) => { + if(err.code === 'PROTOCOL_CONNECTION_LOST') haDC(); + }); +} +haDC(); + cfg.server.forEach((e,i,a) => { bot.add({ "name": e.name, @@ -40,6 +52,7 @@ bot.on('message', (e) => { if(cb.status === true) { var tmp = getUser(e.user.getNick(), e.network); setTimeout(()=>{ + sql.query("insert into `f0ck`.`items` values (?,?,?,?,?,?,?)",url, "./b/"+path.parse(entry).base,tmp['username'],e.channel.getName(),new Date().getTime(),0); e.reply(cb.msg+" for "+tmp['nick']+" ("+tmp['username']+"@"+tmp['hostname']+")"); }, 1600); } @@ -55,7 +68,6 @@ bot.on('message', (e) => { } }); - var getUser = (u, n) => { bot.whois(u, n, (fn)=>{ }); // send whois var start = Date.now(); @@ -68,16 +80,21 @@ var dl = (url, dest, cb) => { var request = (url.match(/^https/)?https:http).get(url, (response) => { try { if(allowed.indexOf(response.headers['content-type']) != -1) { - response.pipe(file); - file.on('finish', () => { - file.close(); - cb({'status':true, 'msg':'downloaded '+dest}); - }); - file.on('error', (err) => { - fs.unlink(dest); - file.close(); - cb({'status':false, 'msg':err.message}); - }); + if(checkRepost(url)) { + response.pipe(file); + file.on('finish', () => { + file.close(); + cb({'status':true, 'msg':'downloaded '+dest}); + }); + file.on('error', (err) => { + fs.unlink(dest); + file.close(); + cb({'status':false, 'msg':err.message}); + }); + } + else { + cb({'status':false, 'msg':'repost motherf0cker'}); + } } else { cb({'status':false, 'msg':'f0ck you'}); @@ -87,4 +104,18 @@ var dl = (url, dest, cb) => { e.reply(ex); } }); +}; + +var checkRepost = (url) => { + var result = false; + sql.query( + "select count(*) as count from `f0ck`.`items` where `src` = ?", url, (err, rows, fields) => { + result = (rows[0].count == 0)?true:false; + console.log("1: "+result); + } + ); + var start = Date.now(); + while(Date.now() < start + 1000) {} + console.log("2: "+result); + return result; }; \ No newline at end of file