forked from Indysama/node-xdcc
-
Notifications
You must be signed in to change notification settings - Fork 3
/
axdcc.js
executable file
·235 lines (183 loc) · 7.77 KB
/
axdcc.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// See READM.md for Usage
// Get depenencies
var net = require("net");
var fs = require("fs");
var EventEmitter = require("events").EventEmitter;
function Request(client, args) {
var self = this;
var intervalId = false;
this.finished = false;
this.client = client;
this.args = args;
this.pack_info = {};
if(typeof this.args.progressInterval == "undefined"){
this.args.progressInterva = 1;
}
if(typeof this.args.resume == "undefined"){
this.args.resume = true;
}
//Start handler
this.once("start", function () {
//Request
this.client.say(this.args.nick, "XDCC SEND " + this.args.pack);
// Listen for data from the XDCC bot
this.client.on("ctcp-privmsg", dcc_download_handler);
this.once("cancel", function () {
if (this.finished) {
return;
}
// Cancel the pack
this.client.say(this.args.nick, "XDCC CANCEL");
killrequest();
});
this.on("kill", function () {
killrequest()
});
});
function dcc_download_handler(sender, target, message) {
if (self.finished) {
return;
}
//handle DCC massages
if (sender == self.args.nick && target == self.client.nick && message.substr(0, 4) == "DCC ") {
// Split the string into an array
// The String format is as follows:
// DCC {command} ("|'){filename}("|') {ip} {port}( {filesize})
var parser = /DCC (\w+) "?'?(.+?)'?"? (\d+) (\d+) ?(\d+)?/;
var params = message.match(parser);
if(params != null){
// Function to convert Integer to IP address
function int_to_IP(n) {
var octets = [];
octets.unshift(n & 255);
octets.unshift((n >> 8) & 255);
octets.unshift((n >> 16) & 255);
octets.unshift((n >> 24) & 255);
return octets.join(".");
}
switch (params[1]) {
//Got DCC SEND message
case "SEND":
self.pack_info.command = params[1];
self.pack_info.filename = params[2];
self.pack_info.ip = int_to_IP(parseInt(params[3], 10));
self.pack_info.port = parseInt(params[4], 10);
self.pack_info.filesize = parseInt(params[5], 10);
self.pack_info.resumepos = 0;
// Get the download location
self.pack_info.location = self.args.path + (self.args.path.substr(-1, 1) == "/" ? "" : "/")
+ self.pack_info.filename;
// Check for file existence
fs.stat(self.pack_info.location + ".part", function (err, stats) {
// File exists
if (!err && stats.isFile()) {
if (self.args.resume) {
// Resume download
self.client.ctcp(self.args.nick, "privmsg", "DCC RESUME " + self.pack_info.filename + " "
+ self.pack_info.port + " " + stats.size);
self.pack_info.resumepos = stats.size;
} else {
// Dont resume download delete .part file and start download
fs.unlink(self.pack_info.location + ".part", function (err) {
if (err) throw err;
download(self.pack_info);
});
}
} else {
// File dont exists start download
download(self.pack_info);
}
});
break;
// Got DCC ACCEPT message (bot accepts the resume command)
case "ACCEPT":
// Check accept command params
if (self.pack_info.filename == params[2] &&
self.pack_info.port == parseInt(params[3], 10) &&
self.pack_info.resumepos == parseInt(params[4], 10)) {
// Download the file
self.pack_info.command = params[1];
download(self.pack_info);
}
break;
}
}
}
}
// kill this request dont react on further messages
function killrequest() {
self.removeAllListeners();
self.client.removeListener("ctcp-privmsg", dcc_download_handler);
self.finished = true;
self.pack_info = {};
if(self.intervalId){
clearInterval(self.intervalId);
intervalId = false;
}
}
function download(pack) {
if (self.finished) return;
// Write stream to store data
var stream = fs.createWriteStream(pack.location + ".part", {flags: 'a'});
stream.on("open", function () {
var send_buffer = new Buffer(4);
var received = pack.resumepos;
var ack = pack.resumepos;
// Open connection to the bot
var conn = net.connect(pack.port, pack.ip, function () {
self.emit("connect", pack);
intervalId = setInterval(function(){
self.emit("progress", pack, received);
},self.args.progressInterval*1000)
});
// Callback for data
conn.on("data", function (data) {
received += data.length;
//support for large files
ack += data.length;
while (ack > 0xFFFFFFFF) {
ack -= 0xFFFFFFFF;
}
send_buffer.writeUInt32BE(ack, 0);
conn.write(send_buffer);
stream.write(data);
});
// Callback for completion
conn.on("end", function () {
// End the transfer
// Close writestream
stream.end();
// Connection closed
if (received == pack.filesize) {// Download complete
fs.rename(pack.location + ".part", pack.location);
self.emit("complete", pack);
} else if (received != pack.filesize && !self.finished) {// Download incomplete
self.emit("dlerror", pack, "Server unexpected closed connection");
} else if (received != pack.filesize && self.finished) {// Download abborted
self.emit("dlerror", pack, "Server closed connection, download canceled");
}
killrequest();
conn.destroy();
});
// Add error handler
conn.on("error", function (error) {
// Close writestream
stream.end();
// Send error message
self.emit("dlerror", pack, error);
// Destroy the connection
conn.destroy();
killrequest();
});
});
stream.on("error", function (error) {
// Close writestream
stream.end();
self.emit("dlerror", pack, error);
killrequest();
});
}
return(this);
}
Request.prototype = Object.create(EventEmitter.prototype);
exports.Request = Request;