void AcceptResponse(string line = null) { if (line == null) { line = ClientReader.ReadLine(); } ProcessMessage(new FtpResponse(line)); }
void ProcessMessage(FtpResponse response, bool suppressSet = false) { var code = response.Code; var multiline = response.Multiline; var message = response.Message; var raw = response.Raw; if (ResponseReceived != null) { ResponseReceived(this, new ResponseReceivedEventArgs(response)); } // Log the message LogMessage(code, message); // Set flags indicating whether the response // indicates success or failure var state = ClientState.None; if (code.IsInRange(100, 300)) { state |= ClientState.GoodResponse; } else if (code.IsInRange(300, 600)) { state |= ClientState.BadResponse; } // Check the message code and act accordingly switch (code) { case 110: // Restart marker reply break; case 120: // Service ready in nnn minutes break; case 125: // Data connection already open; transfer starting break; case 150: // File status okay; about to open data connection break; case 200: // Command okay break; case 202: // Command not implemented, superfluous at this site break; case 211: // System status, or system help reply // Response to FEAT if (multiline) { string line = ClientReader.ReadLine(); while (!Regex.IsMatch(line, RegexConstants.Multiline_End)) { ProcessMessage(new FtpResponse(000, line, false), true); line = ClientReader.ReadLine(); } AcceptResponse(line); } break; case 212: // Directory status break; case 213: // File status break; case 214: // Help message break; case 215: // System type break; case 220: // Service ready for new user //if (ServiceReady) // suppressSet = true; //ServiceReady = true; break; case 221: // Service closing control connection break; case 225: // Data connection open; no traClientStreamfer in progress break; case 226: // Closing data connection if (DataConnectionClosed != null) { DataConnectionClosed(this, EventArgs.Empty); } break; case 227: // Entering passive mode (h1,h2,h3,h4,p1,p2) IPEndPoint endpoint = null; try { endpoint = Parsers.ParsePasvResponse(message); Data.EndPoint = endpoint; } catch (Exception e) { LogMessage(000, string.Format("Error parsing endpoint: {0}", e.Message)); } state |= ClientState.OperationFailed; break; case 230: // User logged in, proceed break; case 250: // Requested file action okay, completed break; case 257: // "PATHNAME" created break; case 331: // User name okay, need password if (string.IsNullOrEmpty(Password)) { state |= ClientState.ExitRequested; } break; case 332: // Need account for login break; case 350: // Requested file action pending further information case 421: // Service not available, closing control connection // Also: Login time exceeded state |= ClientState.ExitRequested; break; case 425: // Can't open data connection break; case 426: // Connection closed; transfer aborted break; case 450: // Requested file action not taken break; case 451: // Requested action aborted. Local error in processing break; case 452: // Requested action not taken // Insufficient storage space in system break; case 500: // Syntax error or // Command unrecognized break; case 501: // Syntax error in parameters or arguments break; case 502: // Command not implemented break; case 503: // Bad sequence of commands break; case 504: // Command not implemented for that parameter break; case 530: // Not logged in break; case 532: // Need account for storing files break; case 550: // Requested action not taken break; case 551: // Requested action aborted. Page type unknown break; case 552: // Requested file action aborted // Exceeded storage allocation break; case 553: // Requested action not taken // File name not allowed break; } // Allow the operation to continue CurrentState = state; if (!suppressSet) { MessageHandler.Set(); MessageHandler.Reset(); } }
private void ClientListening() { string message; lock (clientLocker) { if (ClientConnected) { try { ClientWriter.WriteLine("-- " + Properties.strings.motd + ": " + ConnectedServer.WelcomeMessage); ClientWriter.Flush(); ClientWriter.Write("-- " + Properties.strings.enterNickname + ": "); ClientWriter.Flush(); } catch (IOException) { } } } try { do { string name = ClientReader.ReadLine(); if (string.IsNullOrWhiteSpace(name)) { lock (clientLocker) { if (ClientConnected) { ClientWriter.Write("-- " + Properties.strings.enterNickname + ": "); ClientWriter.Flush(); } } } else { Name = name; } } while (string.IsNullOrWhiteSpace(name)); } catch (IOException) { } ConnectedServer.RefreshInfo(); ConnectedServer.WriteToAllClients("-- " + string.Format(Properties.strings.connectedWithClient, Name + "@" + ClientIep.Address, ClientIep.Port)); lock (clientLocker) { if (ClientConnected) { try { ClientWriter.WriteLine("-- " + Properties.strings.commands); ClientWriter.Flush(); } catch (IOException) { } } } while (ClientConnected) { try { message = ClientReader.ReadLine(); if (message != null) { //Handles available commands if (!string.IsNullOrWhiteSpace(message)) { if (message[0] == '#') { if (message == Properties.strings.exit) { lock (clientLocker) if (ClientConnected) { CloseConnection(); } } else if (message == Properties.strings.list) { lock (clientLocker) if (ClientConnected) { ConnectedServer.List(this); } } else if (message == Properties.strings.cmd) { lock (clientLocker) { if (ClientConnected) { ClientWriter.WriteLine("-- " + Properties.strings.availableCommands + ": "); ClientWriter.Flush(); ClientWriter.WriteLine("\t-- " + Properties.strings.cmd + ": " + Properties.strings.cmdInfo); ClientWriter.Flush(); ClientWriter.WriteLine("\t-- " + Properties.strings.exit + ": " + Properties.strings.exitInfo); ClientWriter.Flush(); ClientWriter.WriteLine("\t-- " + Properties.strings.list + ": " + Properties.strings.listInfo); ClientWriter.Flush(); } } } else { lock (clientLocker) { if (ClientConnected) { ClientWriter.WriteLine("-- " + Properties.strings.unknownCmd); ClientWriter.Flush(); } } } } else { ConnectedServer.WriteToAllClients("|| " + Name + "@" + ClientIep.Address + " >> " + message); } } else { lock (clientLocker) { if (ClientConnected) { ClientWriter.WriteLine("-- " + Properties.strings.enterMessage); ClientWriter.Flush(); } } } } else { CloseConnection(); } } catch (IOException) { break; } } CloseConnection(); }