Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var net = require('net');
- var http = require('http');
- var HOST = 'irc.bsnet.se'; // The server we want to connect to
- var PORT = 6667; // The connection port which is usually 6667
- var NICK = 'wonderbot'; // The bot's nickname
- var IDENT = '****************';
- var REALNAME = 'NODE.JS';
- var CHANNEL = '#wonderlan'; // The default channel for the bot
- var OWNER = 'djazz';
- var lastfm = {
- username: 'ottoremse',
- apiKey: '37d771e988ae15a26ea733c48612a6df',
- secret: '4b0622860b5b89514b52a983dd63a98c',
- limit: 2
- }
- var lastfmUrl = 'http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=' + lastfm.username + '&api_key=' + lastfm.apiKey + '&limit=' + lastfm.limit + '&format=json';
- var cmdLock = false;
- function sendMsg(msg) {
- console.log(msg);
- s.write(msg + '\r\n');
- }
- function sendChat(msg, chan) {
- chan = chan || CHANNEL;
- sendMsg('PRIVMSG '+chan+' :'+msg);
- }
- function scanPorts(ip, callback) {
- var list = [];
- var toScan = 0;
- console.log('scanning ip '+ip);
- for (var i = 0; i < 60000; i+=1000) {
- toScan+=1000;
- (function (i) {
- setTimeout(function () {
- //console.log('trying interval '+i);
- for (var k = 0; k < 1000; k++) {
- var port = i+k;
- (function (port) {
- var client = net.connect({
- host: ip,
- port: port
- }, function () {
- console.log(ip+":"+port);
- list.push(port);
- });
- client.on('error', function () {
- //console.log(port)
- });
- client.on('close', function () {
- toScan--;
- if (toScan === 0) {
- list.sort(function(a,b){return a-b});
- //console.log(list);
- callback(list);
- }
- });
- setTimeout(function () {
- client.destroy();
- }, 300);
- }(port));
- }
- }, (i/1000)*200);
- }(i));
- }
- }
- // Connect
- // Create the socket & Connect to the server
- var s = net.connect(PORT, HOST, function () {
- // Send the nick to server
- sendMsg('NICK '+NICK);
- // Identify to server
- sendMsg('USER '+NICK+' '+HOST+' asdasdsdasd.com :'+REALNAME);
- sendMsg('JOIN ' + CHANNEL);
- /*setTimeout(function () {
- // This is my nick, i promise!
- sendMsg('PRIVMSG nick IDENTIFY '+IDENT);
- }, 20*1000);*/
- //sendChat('javascript');
- });
- s.on('data', function (data) {
- var lines = data.toString().split('\r\n');
- for (var i = 0; i < lines.length; i++) {
- console.log(lines[i]);
- var line = lines[i].split(' ');
- var row = line.slice(3).join(' ').replace(/\:/g, ' ').replace(/\,/g, ' ').replace(/\./g, ' ').replace(/\?/g, ' ').trim().toLowerCase().split(' ');
- if (line[0] === 'PING') {
- sendMsg('PONG '+line[1]);
- } else if (line[1] === 'PRIVMSG') {
- if(line[2] === CHANNEL && row.indexOf(NICK) !== -1) {
- var mentioner = line[0].split("!")[0].split(":")[1];
- if (line[3].indexOf(NICK) && line[4]) { // Command
- if (line[4].length > 0) {
- console.log('got command');
- cmdLock = true;
- switch(line[4]) { // Command name
- case 'music':
- console.log('music command');
- http.get(lastfmUrl, function (res) {
- var data = "";
- res.on('data', function (chunk) {
- data += chunk.toString();
- });
- res.on('end', function () {
- track = JSON.parse(data).recenttracks.track[0];
- if (cmdLock) {
- sendChat("WonderLAN spelar låten "+track.name+" ("+track.url+")", line[2]);
- }
- cmdLock = false;
- });
- }).on('error', function (e) {
- sendChat("Got error: " + e.message, line[2]);
- cmdLock = false;
- });
- break;
- case 'scan':
- console.log('scan command');
- if (isNaN(+line[5])) {
- var ip = line[5];
- scanPorts(ip, function (scanlist) {
- sendChat("Scanned ip "+ip+" for open ports: "+scanlist.join(", "));
- });
- } else {
- var startAddress = "194.47.158.";
- var port = +line[5] || 5357;
- var toScan = 0;
- var scanlist = [];
- console.log('scanning for port '+port);
- for (var i = 0; i < 256; i++) {
- toScan++;
- (function (i) {
- var client = net.connect({
- host: startAddress+i,
- port: port
- }, function () {
- console.log(startAddress+i+"\t:"+port);
- scanlist.push(i);
- });
- client.on('error', function () {});
- client.on('data', function (data) {
- console.log(data.toString());
- });
- client.on('close', function () {
- toScan--;
- if (toScan === 0) {
- scanlist.sort(function(a,b){return a-b});
- sendChat("Scanned port "+port+": 194.47.158.... "+scanlist.join(", "));
- }
- });
- setTimeout(function () {
- client.destroy();
- }, 300);
- }(i));
- }
- }
- break;
- default:
- cmdLock = false;
- sendChat('Okänt kommando: '+line[4]);
- break;
- }
- }
- } else { // Just normal mention
- if (!line[4]) {
- sendChat(mentioner+": Kommandon: music", line[2]);
- }
- }
- } else if (line[2] === NICK) {
- var username = line[0].split("!")[0].substr(1);
- sendChat('Hello!', username);
- }
- }
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement