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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#![crate_name = "ftp"]
#![crate_type = "lib"]

extern crate regex;

use std::io::{Error, ErrorKind, Read, BufReader, BufWriter , Cursor, Write, copy};
use std::net::TcpStream;
use std::result::Result;
use std::string::String;
use std::str::FromStr;
use regex::Regex;

/// Stream to interface with the FTP server. This interface is only for the command stream.
pub struct FTPStream {
	command_stream: TcpStream,
	pub host: &'static str,
	pub command_port: u16
}

impl FTPStream {

	/// Creates an FTP Stream.
	pub fn connect(host: &'static str, port: u16) -> Result<FTPStream, Error> {
		let connect_string = format!("{}:{}", host, port);
		let tcp_stream = try!(TcpStream::connect(&*connect_string));
		let mut ftp_stream = FTPStream {
			command_stream: tcp_stream,
			host: host,
			command_port: port
		};
		match ftp_stream.read_response(220) {
			Ok(_) => (),
			Err(e) => return Err(Error::new(ErrorKind::Other, e))
		}
		Ok(ftp_stream)
	}

	fn write_str(&mut self, s: &str) -> Result<(), Error> {
		return self.command_stream.write_fmt(format_args!("{}", s));
	}

	/// Log in to the FTP server.
	pub fn login(&mut self, user: &str, password: &str) -> Result<(), String> {
		let user_command = format!("USER {}\r\n", user);
		let pass_command = format!("PASS {}\r\n", password);

		match self.write_str(&user_command) {
			Ok(_) => (),
			Err(_) => return Err(format!("Write Error"))
		}

		match self.read_response(331) {
			Ok(_) => {

				match self.write_str(&pass_command) {
					Ok(_) => (),
					Err(_) => return Err(format!("Write Error"))
				}

				match self.read_response(230) {
					Ok(_) => Ok(()),
					Err(s) => Err(s)
				}
			},
			Err(s) => Err(s)
		}
	}

	/// Change the current directory to the path specified.
	pub fn change_dir(&mut self, path: &str) -> Result<(), String> {
		let cwd_command = format!("CWD {}\r\n", path);

		match self.write_str(&cwd_command) {
			Ok(_) => (),
			Err(_) => return Err(format!("Write Error"))
		}

		match self.read_response(250) {
			Ok(_) => Ok(()),
			Err(e) => Err(e)
		}
	}

	/// Move the current directory to the parent directory.
	pub fn change_dir_to_parent(&mut self) -> Result<(), String> {
		let cdup_command = format!("CDUP\r\n");

		match self.write_str(&cdup_command) {
			Ok(_) => (),
			Err(_) => return Err(format!("Write Error"))
		}

		match self.read_response(250) {
			Ok(_) => Ok(()),
			Err(s) => Err(s)
		}
	}

	/// Gets the current directory
	pub fn current_dir(&mut self) -> Result<String, String> {
		fn index_of(string: &str, ch: char) -> isize {
			let mut i = -1;
			let mut index = 0;
			for c in string.chars() {
				if c == ch {
					i = index;
					return i
				}
				index+=1;
			}
			return i;
		}

		fn last_index_of(string: &str, ch: char) -> isize {
			let mut i = -1;
			let mut index = 0;
			for c in string.chars() {
				if c == ch {
					i = index;
				}
				index+=1;
			}
			return i;
		}
		let pwd_command = format!("PWD\r\n");

		match self.write_str(&pwd_command) {
			Ok(_) => (),
			Err(e) => return Err(format!("{}", e))
		}

		match self.read_response(257) {
			Ok((_, line)) => {
				let begin = index_of(&line, '"');
				let end = last_index_of(&line, '"');

				if begin == -1 || end == -1 {
					return Err(format!("Invalid PWD Response: {}", line))
				}
				let b = begin as usize;
				let e = end as usize;

				return Ok(line[b+1..e].to_string())
			},
			Err(e) => Err(e)
		}
	}

	/// This does nothing. This is usually just used to keep the connection open.
	pub fn noop(&mut self) -> Result<(), String> {
		let noop_command = format!("NOOP\r\n");

		match self.write_str(&noop_command) {
			Ok(_) => (),
			Err(_) => return Err(format!("Write Error"))
		}

		match self.read_response(200) {
			Ok(_) => Ok(()),
			Err(s) => Err(s)
		}
	}

	/// This creates new directories on the server.
	pub fn make_dir(&mut self, pathname: &str) -> Result<(), String> {
		let mkdir_command = format!("MKD {}\r\n", pathname);

		match self.write_str(&mkdir_command) {
			Ok(_) => (),
			Err(_) => return Err(format!("Write Error"))
		}

		match self.read_response(257) {
			Ok(_) => Ok(()),
			Err(e) => Err(e)
		}
	}

	/// Runs the PASV command.
	pub fn pasv(&mut self) -> Result<(isize), String> {
		let pasv_command = format!("PASV\r\n");

		match self.write_str(&pasv_command) {
			Ok(_) => (),
			Err(_) => return Err(format!("Write Error"))
		}

		//PASV response format : 227 Entering Passive Mode (h1,h2,h3,h4,p1,p2).

		let response_regex = match Regex::new(r"(.*)\(\d+,\d+,\d+,\d+,(\d+),(\d+)\)(.*)") {
			Ok(re) => re,
    		Err(_) => panic!("Invaid Regex!!"),
		};

		match self.read_response(227) {
			Ok((_, line)) => {
				let caps = response_regex.captures(&line).unwrap();
				let caps_2 = match caps.at(2) {
					Some(s) => s,
					None => return Err(format!("Problems parsing reponse"))
				};
				let caps_3 = match caps.at(3) {
					Some(s) => s,
					None => return Err(format!("Problems parsing reponse"))
				};
				let first_part_port: isize = FromStr::from_str(caps_2).unwrap();
				let second_part_port: isize = FromStr::from_str(caps_3).unwrap();
				Ok((first_part_port*256)+second_part_port)
			},
			Err(s) => Err(s)
		}
	}

	/// Quits the current FTP session.
	pub fn quit(&mut self) -> Result<(isize, String), String> {
		let quit_command = format!("QUIT\r\n");

		match self.write_str(&quit_command) {
			Ok(_) => (),
			Err(_) => return Err(format!("Write Error"))
		}

		match self.read_response(221) {
			Ok((code, message)) => Ok((code, message)),
			Err(message) => Err(message),
		}
	}

	/// Retrieves the file name specified from the server. This method is a more complicated way to retrieve a file. The reader returned should be dropped.
	/// Also you will have to read the response to make sure it has the correct value.
	pub fn retr(&mut self, file_name: &str) -> Result<BufReader<TcpStream>, String> {
		let retr_command = format!("RETR {}\r\n", file_name);

		let port = match self.pasv() {
			Ok(p) => p,
			Err(e) => return Err(e)
		};

		let connect_string = format!("{}:{}", self.host, port);
		let data_stream = BufReader::new(TcpStream::connect(&*connect_string).unwrap());

		match self.write_str(&retr_command) {
			Ok(_) => (),
			Err(_) => return Err(format!("Write Error"))
		}

		match self.read_response(150) {
			Ok(_) => Ok(data_stream),
			Err(e) => Err(e)
		}
	}

	fn simple_retr_(&mut self, file_name: &str) -> Result<Cursor<Vec<u8>>, String> {
		let mut data_stream = match self.retr(file_name) {
			Ok(s) => s,
			Err(e) => return Err(e)
		};

		let buffer: &mut Vec<u8> = &mut Vec::new();
		loop {
			let mut buf = [0; 256];
			let len = match data_stream.read(&mut buf) {
            	Ok(len) => len,
            	//Err(ref e) if e.kind == EndOfFile => break,
            	Err(e) => return Err(format!("{}", e)),
        	};
        	if len == 0 {
				break;
			}
        	match buffer.write(&buf[0..len]) {
        		Ok(_) => (),
        		Err(e) => return Err(format!("{}", e))
        	};
		}

		drop(data_stream);

		Ok(Cursor::new(buffer.clone()))
	}

	/// Simple way to retr a file from the server. This stores the file in memory.
	pub fn simple_retr(&mut self, file_name: &str) -> Result<Cursor<Vec<u8>>, String> {
		let r = match self.simple_retr_(file_name) {
			Ok(reader) => reader,
			Err(e) => return Err(e)
		};

		match self.read_response(226) {
			Ok(_) => Ok(r),
			Err(e) => Err(e)
		}
	}

	/// Removes the remote pathname from the server.
	pub fn remove_dir(&mut self, pathname: &str) -> Result<(), String> {
		let rmd_command = format!("RMD {}\r\n", pathname);

		match self.write_str(&rmd_command) {
			Ok(_) => (),
			Err(_) => return Err(format!("Write Error"))
		}

		match self.read_response(250) {
			Ok(_) => Ok(()),
			Err(e) => Err(e)
		}
	}

	fn stor_<R: Read>(&mut self, filename: &str, r: &mut R) -> Result<(), String> {
		let stor_command = format!("STOR {}\r\n", filename);

		let port = match self.pasv() {
			Ok(p) => p,
			Err(e) => return Err(e)
		};

		let connect_string = format!("{}:{}", self.host, port);
		let data_stream: &mut BufWriter<TcpStream> = &mut BufWriter::new(TcpStream::connect(&*connect_string).unwrap());

		match self.write_str(&stor_command) {
			Ok(_) => (),
			Err(_) => return Err(format!("Write Error"))
		}

		match self.read_response(150) {
			Ok(_) => (),
			Err(e) => return Err(e)
		}

		match copy(r, data_stream) {
			Ok(_) => {
				drop(data_stream);
				Ok(())
			},
			Err(_) => {
				drop(data_stream);
				Err(format!("Error Writing"))
			}
		}
	}

	/// This stores a file on the server.
	pub fn stor<R: Read>(&mut self, filename: &str, r: &mut R) -> Result<(), String> {
		match self.stor_(filename, r) {
			Ok(_) => (),
			Err(e) => return Err(e)
		};

		match self.read_response(226) {
			Ok(_) => Ok(()),
			Err(e) => Err(e)
		}
	}

	//Retrieve single line response
	pub fn read_response(&mut self, expected_code: isize) -> Result<(isize, String), String> {
		//Carriage return
		let cr = 0x0d;
		//Line Feed
		let lf = 0x0a;
		let mut line_buffer: Vec<u8> = Vec::new();

		while line_buffer.len() < 2 || (line_buffer[line_buffer.len()-1] != lf && line_buffer[line_buffer.len()-2] != cr) {
				let byte_buffer: &mut [u8] = &mut [0];
				match self.command_stream.read(byte_buffer) {
					Ok(_) => {},
					Err(_) => return Err(format!("Error reading response")),
				}
				line_buffer.push(byte_buffer[0]);
		}

		let response = String::from_utf8(line_buffer).unwrap();
		let chars_to_trim: &[char] = &['\r', '\n'];
		let trimmed_response = response.trim_matches(chars_to_trim);
    	let trimmed_response_vec: Vec<char> = trimmed_response.chars().collect();
    	if trimmed_response_vec.len() < 5 || trimmed_response_vec[3] != ' ' {
    		return Err(format!("Invalid response"));
    	}

    	let v: Vec<&str> = trimmed_response.splitn(2, ' ').collect();
    	let code: isize = FromStr::from_str(v[0]).unwrap();
    	let message = v[1];
    	if code != expected_code {
    		return Err(format!("Invalid response: {} {}", code, message))
    	}
    	Ok((code, message.to_string()))
	}
}