summaryrefslogtreecommitdiff
path: root/Noddybox.WindowsPhone.FTP/ServerDialog.cs
blob: 4291afb0998bad5e72f8fc6d4fd8d6640dbdf236 (plain)
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
// This file is part of the Noddybox.WindowsPhone C# suite.
//
// Noddybox.Emulation is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Noddybox.Emulation is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Noddybox.Emulation.  If not, see <http://www.gnu.org/licenses/>.
//
// Copyright (c) 2012 Ian Cowburn
//
using System;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace Noddybox.WindowsPhone.Net
{
	/// <summary>
	/// Provides a wrapper around a stream to interact with servers
	/// </summary>
	public class ServerDialog
	{
		// -------------------------------------------------
		// - PUBLIC MEMBERS                                -
		// -------------------------------------------------
		
		/// <summary>
		/// Used for returns from the server
		/// </summary>
		public class Reply
		{
			/// <summary>
			/// Default constructor - initializes all fields to default values
			/// </summary>
			public Reply()
			{
				m_code=-1;
				m_text="";
			}
			
			/// <summary>
			/// The code returned by the server
			/// </summary>
			public int Code
			{
				get {return m_code;}
				set {m_code=value;}
			}
			
			/// <summary>
			/// The text returned by the server
			/// </summary>
			public string Text
			{
				get {return m_text;}
				set {m_text=value;}
			}

			private int		m_code;
			private string	m_text;
			
		}
		
		/// <summary>
		/// Default constructor - initializes all fields to default values
		/// </summary>
		public ServerDialog()
		{
			m_encode=new System.Text.ASCIIEncoding();
			m_code=new Regex("^[0-9][0-9][0-9]");
			m_code_end=new Regex("^[0-9][0-9][0-9][^-]");

		}
		
		/// <summary>
		/// Sends a command to the server and returns the reply
		/// </summary>
		public Reply Send(Stream str, string command)
		{
			Reply r=new Reply();
			
			if (command!=null)
			{
				byte[] b=ToBytes(command);
			
				str.Write(b,0,b.Length);
			}
			
			GetResult(str,r);
			    
			return r;
		}
		
		// -------------------------------------------------
		// - PRIVATE MEMBERS                               -
		// -------------------------------------------------
		
		private ASCIIEncoding	m_encode;
		private Regex 			m_code;
		private Regex 			m_code_end;


		
		private byte[] ToBytes(string s)
		{
			return m_encode.GetBytes(s);
		}
		
		private string GetLine(Stream str)
		{
			byte[] b=new byte[1];
			int c;
			string r="";
			
			while((c=str.ReadByte())!=-1)
			{
				b[0]=(byte)c;
				r+=m_encode.GetString(b);
				
				if (c==10)
				{
					break;
				}
			}
			
			return r;
		}
		
		private void GetResult(Stream str, Reply r)
		{
			string s;
			string all="";
			bool first_line=true;

			do
			{
				s=GetLine(str);
				
				if (first_line)
				{
					if (m_code.IsMatch(s))
					{
						r.Code=Convert.ToInt32(s.Substring(0,3));
					}
					else
					{
						throw new NetException(NetException.Type.ALL_NoReplyCode);
					}
					
					first_line=false;
				}

				if (m_code.IsMatch(s))
				{
					all+=s.Substring(4);
				}
				else
				{
					all+=s;
				}
				
			} while (!m_code_end.IsMatch(s));
			
			all=all.TrimEnd(null);
			r.Text=all;
		}

	}
}