7

I am noob at Js but trying to make life easier by writing api to transfer files from irc to my server After some digging I got a module that downloads file (code below based on npm xdcc) but when I run the module it doesn't exit after completing commands Eg. After spitting out 'Download Complete' I have to press Ctrl+C to exit

var irc = require('xdcc').irc;
var ProgressBar = require('progress');
var progress;


var connectIRC = function (bot, pack) {
    var user = 'desu' + Math.random().toString(36).substr(7, 3);

    var start = 0;

    console.log('Connecting...');

    var client = new irc.Client('irc.rizon.net', user, {
        channels: ['#nibl'],
        userName: user,
        realName: user
    });

    client.on('join', function(channel, nick, message) {
        if (nick !== user) return;
        console.log('Joined', channel);
        client.getXdcc(bot, 'xdcc send #' + pack, '.');
    });

    client.on('xdcc-connect', function(meta) {
        console.log('Connected: ' + meta.ip + ':' + meta.port);
        progress = new ProgressBar('Downloading... [:bar] :percent, :etas remaining', {
            incomplete: ' ',
            total: meta.length,
            width: 20
        });
    });

    var last = 0;
    client.on('xdcc-data', function(received) {
        progress.tick(received - last);
        last = received;
    });

    client.on('xdcc-end', function(received) {
        console.log('Download completed');
    });

    client.on('notice', function(from, to, message) {
        if (to == user && from == bot) {
            console.log("[notice]", message);
        }
    });
    client.on('error', function(message) {
        console.error(message);
    });
};

module.exports.connectIRC = connectIRC;

Also when the irc bot doesn't send any notice , the filename is wrapped in double quotes Eg. Packet name = Node.mkv

If bot sends a notice then name of file download = Node.mkv

If bot doesn't send any notice then name = "Node.mkv" (including double quotes)

Any help would be appreciated

Solaris
  • 674
  • 7
  • 22

1 Answers1

8

If you want auto close your Node JS app you have to call the global process object's exit method so :

  client.on('xdcc-end', function(received) {
        console.log('Download completed');
        process.exit()
    });
Léo R.
  • 2,620
  • 1
  • 10
  • 22