2005-01-15 19:27:25 +00:00
|
|
|
using System;
|
|
|
|
using System.Xml;
|
|
|
|
|
|
|
|
namespace TechBot.Library
|
|
|
|
{
|
|
|
|
public class HresultCommand : BaseCommand, ICommand
|
|
|
|
{
|
|
|
|
private IServiceOutput serviceOutput;
|
|
|
|
private XmlDocument hresultXmlDocument;
|
|
|
|
|
|
|
|
public HresultCommand(IServiceOutput serviceOutput,
|
|
|
|
string hresultXml)
|
|
|
|
{
|
|
|
|
this.serviceOutput = serviceOutput;
|
|
|
|
hresultXmlDocument = new XmlDocument();
|
|
|
|
hresultXmlDocument.Load(hresultXml);
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool CanHandle(string commandName)
|
|
|
|
{
|
|
|
|
return CanHandle(commandName,
|
|
|
|
new string[] { "hresult" });
|
|
|
|
}
|
|
|
|
|
2005-02-16 21:07:55 +00:00
|
|
|
public void Handle(MessageContext context,
|
|
|
|
string commandName,
|
2005-01-15 19:27:25 +00:00
|
|
|
string parameters)
|
|
|
|
{
|
|
|
|
string hresultText = parameters;
|
|
|
|
if (hresultText.Equals(String.Empty))
|
|
|
|
{
|
2005-02-16 21:07:55 +00:00
|
|
|
serviceOutput.WriteLine(context,
|
|
|
|
"Please provide a valid HRESULT value.");
|
2005-01-15 19:27:25 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NumberParser np = new NumberParser();
|
|
|
|
long hresult = np.Parse(hresultText);
|
|
|
|
if (np.Error)
|
|
|
|
{
|
2005-02-16 21:07:55 +00:00
|
|
|
serviceOutput.WriteLine(context,
|
|
|
|
String.Format("{0} is not a valid HRESULT value.",
|
2005-01-15 19:27:25 +00:00
|
|
|
hresultText));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
string description = GetHresultDescription(hresult);
|
|
|
|
if (description != null)
|
|
|
|
{
|
2005-02-16 21:07:55 +00:00
|
|
|
serviceOutput.WriteLine(context,
|
|
|
|
String.Format("{0} is {1}.",
|
2005-01-15 19:27:25 +00:00
|
|
|
hresultText,
|
|
|
|
description));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-02-16 21:07:55 +00:00
|
|
|
serviceOutput.WriteLine(context,
|
|
|
|
String.Format("I don't know about HRESULT {0}.",
|
2005-01-15 19:27:25 +00:00
|
|
|
hresultText));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public string Help()
|
|
|
|
{
|
|
|
|
return "!hresult <value>";
|
|
|
|
}
|
|
|
|
|
2006-01-05 17:52:39 +00:00
|
|
|
public string GetHresultDescription(long hresult)
|
2005-01-15 19:27:25 +00:00
|
|
|
{
|
|
|
|
XmlElement root = hresultXmlDocument.DocumentElement;
|
|
|
|
XmlNode node = root.SelectSingleNode(String.Format("Hresult[@value='{0}']",
|
|
|
|
hresult.ToString("X8")));
|
|
|
|
if (node != null)
|
|
|
|
{
|
|
|
|
XmlAttribute text = node.Attributes["text"];
|
|
|
|
if (text == null)
|
|
|
|
throw new Exception("Node has no text attribute.");
|
|
|
|
return text.Value;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|