Skip to content

Commit 5169843

Browse files
author
Indy
committed
Initial commit
0 parents  commit 5169843

File tree

4 files changed

+252
-0
lines changed

4 files changed

+252
-0
lines changed

README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#Node.js XDCC library
2+
requires Node.js IRC library. (`npm install irc`)
3+
4+
IRC library for downloading files from XDCC bots.
5+
6+
##Usage
7+
8+
```xdcc.request(client, args);```
9+
10+
####Requests an XDCC from `{client}` pack based on `{args}`
11+
12+
`{client}` IRC client (from IRC library)
13+
`{args}` Information about the XDCC pack
14+
15+
args = {
16+
"pack" : < XDCC Pack ID >,
17+
"nick" : < XDCC Bot Nick >,
18+
"path" : < Path to download to >,
19+
"notify" : <*Channel/nick to notify about progress >,
20+
"overwrite" : <*Boolean, overwrite instead of resume >
21+
}
22+
``` {*} indicates optional argument```
23+
24+
##Callbacks
25+
26+
```client.on('xdcc-connect', pack);```
27+
28+
####Emitted when an XDCC transfer starts
29+
`{pack}` is the XDCC pack information, see `Pack format` below
30+
31+
```client.on('xdcc-data', pack, recieved);```
32+
####Emitted when an XDCC transfer receives data
33+
`{pack}` is the XDCC pack information, see `Pack format` below
34+
`{recieved}` is the amount of data received
35+
36+
```client.on('xdcc-complete', pack);```
37+
####Emitted when an XDCC transfer is complete
38+
`{pack}` is the XDCC pack information, see `Pack format` below
39+
40+
```client.on('xdcc-error', pack, error);```
41+
####Emitted when an XDCC transfer encounters an error
42+
`{pack}` is the XDCC pack information, see `Pack format` below
43+
`{error}` is the error data
44+
45+
##Listeners
46+
```client.emit('xdcc-cancel', nick);```
47+
####When emitted, all XDCC transfers to `{nick}` are cancelled.
48+
`{nick}` nick to cancel XDCC transfers from
49+
50+
##Pack format
51+
pack = {
52+
"filename" : <Name of file being transferred>
53+
"filesize" : <Size of file being transferred>
54+
"nick" : <Nick of file sender>
55+
"ip" : <IP of file sender>
56+
"port" : <Port of file sender>
57+
"notify" : <Channel/nick to notify about progress>
58+
}

example.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* For this to run, example.js must be placed one directory higher
3+
* and the library ``irc'' must be installed. (`npm install irc`)
4+
*
5+
*/
6+
7+
// Get dependencies
8+
var irc = require("irc");
9+
var xdcc = require("xdcc");
10+
11+
// Set IRC configuration
12+
var config = {
13+
server : "irc.rizon.net",
14+
nick : "xdcc-er",
15+
options : {
16+
channels : ["#NEWS"],
17+
userName : "xdcc-er",
18+
realName : "xdcc-er"
19+
}
20+
};
21+
22+
// Connect to the server
23+
var client = new irc.client(config.server, config.nick, config.options);
24+
console.log("-- CONNECTING TO "+config.server+" AS "+config.nick);
25+
26+
// Request pack #2463 from ``XDCC-Bot''
27+
// Store the file in ``/path/to/Downloads''
28+
// And notify ``owner'' about the progress
29+
xdcc.request(client, {
30+
pack : "#2463",
31+
nick : "XDCC-Bot",
32+
path : "/path/to/Downloads",
33+
notify : "owner"
34+
});
35+
36+
// XDCC handlers
37+
client.on("xdcc-connect", function(pack) {
38+
client.say(pack.notify, "Beginning download of "+pack.filename);
39+
console.log("-- BEGGINING XDCC OF "+pack.filename);
40+
});
41+
42+
client.on("xdcc-data", function(pack, recieved) {
43+
var progress = Math.floor(recieved / pack.size);
44+
console.log("-- "+progress+"% DONE "+pack.filename);
45+
});
46+
47+
client.on("xdcc-complete", function(pack) {
48+
client.say(pack.notify, "Completed download of "+pack.filename);
49+
console.log("-- COMPLETED XDCC OF "+pack.filename);
50+
});
51+
52+
client.on("xdcc-error", function(pack, error) {
53+
client.say(pack.notify, "Error with "+pack.filename+" from "+pack.nick);
54+
console.log("-- XDCC ERROR WITH "+pack.filename+": "+JSON.stringify(error));
55+
});

lib/xdcc.js

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
// See READM.md for Usage
2+
3+
// Get depenencies
4+
var net = require("net");
5+
var fs = require("fs");
6+
7+
exports.request = function(client, args) {
8+
// Set finished state to false
9+
var finished = false;
10+
11+
// Function to parse pack information the bot sends
12+
function parse_pack_info(string) {
13+
// Function to convert Integer to IP address
14+
function int_to_IP(n) {
15+
var octets = [];
16+
17+
octets.unshift(n & 255);
18+
octets.unshift((n >> 8) & 255);
19+
octets.unshift((n >> 16) & 255);
20+
octets.unshift((n >> 24) & 255);
21+
22+
return octets.join(".");
23+
}
24+
25+
// Split the string into an array
26+
var params = string.split(" ");
27+
28+
return {
29+
notify : args.notify,
30+
nick : args.nick,
31+
filename : params[2],
32+
ip : int_to_IP(parseInt(params[3], 10)),
33+
port : parseInt(params[4], 10),
34+
filesize : parseInt(params[5], 10)
35+
};
36+
}
37+
38+
// Function for checking if the data is part of an XDCC transfer
39+
function is_data(message) { return (message.substr(0, 9) == "DCC SEND "); }
40+
41+
// Request the pack
42+
client.say(args.nick, "XDCC SEND "+args.pack);
43+
44+
// Add handler for cancel event
45+
client.on("xdcc-cancel", function(nick) {
46+
if (finished) { return; }
47+
48+
// Cancel the pack
49+
// TODO restart other packs from the same bot
50+
if (nick == args.nick) {
51+
client.say(nick, "XDCC CANCEL");
52+
53+
finished = true;
54+
}
55+
});
56+
57+
// Listen for data from the XDCC bot
58+
client.on("ctcp-privmsg", function(sender, target, message) {
59+
if (finished) { return; }
60+
61+
if (sender == args.nick && target == client.nick && is_data(message)) {
62+
// Parse the bot message and get the pack information
63+
var pack = parse_pack_info(message);
64+
65+
// Get the download location
66+
var location = args.path+(args.path.substr(-1, 1) == "/" ? "" : "/")
67+
+pack.filename;
68+
69+
// Create write stream to the file
70+
var stream = fs.createWriteStream(location);
71+
72+
stream.on("open", function() {
73+
var send_buffer = new Buffer(4);
74+
var received = 0;
75+
76+
// Open connection to the bot
77+
var conn = net.connect(pack.port, pack.ip, function() {
78+
client.emit("xdcc-connect", pack);
79+
});
80+
81+
// Callback for data
82+
conn.on("data", function(data) {
83+
stream.write(data);
84+
85+
received += data.length;
86+
87+
send_buffer.writeUInt32BE(received, 0);
88+
conn.write(send_buffer);
89+
90+
client.emit("xdcc-data", pack, received);
91+
});
92+
93+
// Callback for completion
94+
conn.on("end", function() {
95+
// End the transfer
96+
finished = true;
97+
98+
stream.end();
99+
conn.destroy();
100+
101+
client.emit("xdcc-complete", pack);
102+
});
103+
104+
// Add error handler
105+
conn.on("error", function(error) {
106+
// Send error message and cancel pack
107+
client.emit("xdcc-error", pack, error);
108+
client.emit("xdcc-cancel", pack);
109+
});
110+
});
111+
112+
// Add error handler
113+
stream.on("error", function(error) {
114+
// Send error message and cancel pack
115+
client.emit("xdcc-error", pack, error);
116+
client.emit("xdcc-cancel", pack);
117+
});
118+
}
119+
});
120+
};

package.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "xdcc",
3+
"description": "XDCC library for node",
4+
"version": "0.1",
5+
"contributors": [],
6+
"repository": {
7+
"type": "git",
8+
"url": "http://github.com/indysama/node-xdcc"
9+
},
10+
"bugs": {
11+
"mail": "indy@osakachan.net",
12+
"url": "http://github.com/indysama/node-xdcc/issues"
13+
},
14+
"main": "lib/xdcc",
15+
"engines": {
16+
"node": ">=0.4.0"
17+
},
18+
"_from": "xdcc"
19+
}

0 commit comments

Comments
 (0)