using System;
using System.Collections;
using MSNP;
namespace MSNP.CommandParsers{
/// <summary>
/// Summary description for MSGCommandParser.
/// </summary>
internal class MSGCommandParser : ICommandParser
{
protected internal MSGCommandParser() {}
public Response ParseResponse(string rawResponse)
{
string curCommand;
string curParameters;
string curName;
string curFriendlyName;
string curLength;
Hashtable curMsgHeaders = new Hashtable();
string curMessage = "";
string[] allParts = rawResponse.Split(new char[] {' '}, 2);
int curPart = 0;
if( allParts.Length > 0 )
{
curCommand = allParts[curPart++];
curParameters = allParts[curPart++];
string[] paramParts = curParameters.Split(new char[] {' ','\n'}, 4);
if( paramParts.Length > 0 )
{
curPart = 0;
curName = paramParts[curPart++];
curFriendlyName = paramParts[curPart++];
curLength = paramParts[curPart++].Trim();
string headerAndMessage = paramParts[curPart++];
// Find the header/message seperator: \r\n\r\n
int seperator = headerAndMessage.IndexOf("\r\n\r\n");
if( seperator == -1 )
throw new Exception("Invalid message syntax: " + rawResponse);
string[] headerParts = headerAndMessage.Substring(0, seperator).Split(new char[] {'\n'});
curMessage = headerAndMessage.Substring(seperator + 4).TrimEnd(new char[]{'\r','\n'});
for(int i = 0; i < headerParts.Length; i++)
{
string[] parts = headerParts[i].Trim().Split(new char[]{':'},2);
curMsgHeaders.Add(parts[0].Trim(), parts[1].Trim());
}
}
else
throw new Exception("Invalid message from server: " + rawResponse);
return new Response(curCommand, -1, new string[] { curName, curFriendlyName, curLength }, curMsgHeaders, curMessage);
}
else
throw new Exception("Invalid response from server: " + rawResponse);
}
}
}
|