Import TechBot
[reactos.git] / irc / TechBot / TechBot.Library / HresultCommand.cs
1 using System;
2 using System.Xml;
3
4 namespace TechBot.Library
5 {
6 public class HresultCommand : BaseCommand, ICommand
7 {
8 private IServiceOutput serviceOutput;
9 private string hresultXml;
10 private XmlDocument hresultXmlDocument;
11
12 public HresultCommand(IServiceOutput serviceOutput,
13 string hresultXml)
14 {
15 this.serviceOutput = serviceOutput;
16 this.hresultXml = hresultXml;
17 hresultXmlDocument = new XmlDocument();
18 hresultXmlDocument.Load(hresultXml);
19 }
20
21 public bool CanHandle(string commandName)
22 {
23 return CanHandle(commandName,
24 new string[] { "hresult" });
25 }
26
27 public void Handle(string commandName,
28 string parameters)
29 {
30 string hresultText = parameters;
31 if (hresultText.Equals(String.Empty))
32 {
33 serviceOutput.WriteLine("Please provide a valid HRESULT value.");
34 return;
35 }
36
37 NumberParser np = new NumberParser();
38 long hresult = np.Parse(hresultText);
39 if (np.Error)
40 {
41 serviceOutput.WriteLine(String.Format("{0} is not a valid HRESULT value.",
42 hresultText));
43 return;
44 }
45
46 string description = GetHresultDescription(hresult);
47 if (description != null)
48 {
49 serviceOutput.WriteLine(String.Format("{0} is {1}.",
50 hresultText,
51 description));
52 }
53 else
54 {
55 serviceOutput.WriteLine(String.Format("I don't know about HRESULT {0}.",
56 hresultText));
57 }
58 }
59
60 public string Help()
61 {
62 return "!hresult <value>";
63 }
64
65 private string GetHresultDescription(long hresult)
66 {
67 XmlElement root = hresultXmlDocument.DocumentElement;
68 XmlNode node = root.SelectSingleNode(String.Format("Hresult[@value='{0}']",
69 hresult.ToString("X8")));
70 if (node != null)
71 {
72 XmlAttribute text = node.Attributes["text"];
73 if (text == null)
74 throw new Exception("Node has no text attribute.");
75 return text.Value;
76 }
77 else
78 return null;
79 }
80 }
81 }