// Noddybox.Net - Provides simple server interaction classes // Copyright (C) 2003 Ian Cowburn // // This program 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 2 // of the License, or (at your option) any later version. // // This program 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 this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. // // $Id$ // namespace Noddybox.Net { using System; using System.Net; using System.Net.Sockets; using System.IO; using System.Text; using System.Text.RegularExpressions; /// /// Provides a wrapper around a stream to interact with servers /// /// /// created by - ianc /// created on - 07/09/2003 22:54:39 /// public class ServerDialog { // ------------------------------------------------- // - PUBLIC MEMBERS - // ------------------------------------------------- /// /// Used for returns from the server /// public class Reply { /// /// Default constructor - initializes all fields to default values /// public Reply() { m_code=-1; m_text=""; } /// /// The code returned by the server /// public int Code { get {return m_code;} set {m_code=value;} } /// /// The text returned by the server /// public string Text { get {return m_text;} set {m_text=value;} } private int m_code; private string m_text; } /// /// Default constructor - initializes all fields to default values /// 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][^-]"); } /// /// Sends a command to the server and returns the reply /// 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; } } }