Sync trunk head (r41026)
[reactos.git] / irc / TechBot / TechBot.Commands.Common / HResultCommand.cs
1 using System;
2 using System.Xml;
3
4 using TechBot.Library;
5
6 namespace TechBot.Commands.Common
7 {
8 [Command("hresult", Help = "!hresult <value>")]
9 public class HResultCommand : XmlLookupCommand
10 {
11 public HResultCommand()
12 {
13 }
14
15 public override string XmlFile
16 {
17 get { return Settings.Default.HResultXml; }
18 }
19
20 public override void ExecuteCommand()
21 {
22 if (string.IsNullOrEmpty(Text))
23 {
24 Say("Please provide a valid HRESULT value.");
25 }
26 else
27 {
28 NumberParser np = new NumberParser();
29 long hresult = np.Parse(Text);
30 if (np.Error)
31 {
32 Say("{0} is not a valid HRESULT value.", Text);
33 return;
34 }
35
36 string description = GetHresultDescription(hresult);
37 if (description != null)
38 {
39 Say("{0} is {1}.",
40 Text,
41 description);
42 }
43 else
44 {
45 Say("I don't know about HRESULT {0}.", Text);
46 }
47 }
48 }
49
50 public string GetHresultDescription(long hresult)
51 {
52 XmlElement root = base.m_XmlDocument.DocumentElement;
53 XmlNode node = root.SelectSingleNode(String.Format("Hresult[@value='{0}']",
54 hresult.ToString("X8")));
55 if (node != null)
56 {
57 XmlAttribute text = node.Attributes["text"];
58 if (text == null)
59 throw new Exception("Node has no text attribute.");
60 return text.Value;
61 }
62 else
63 return null;
64 }
65 }
66 }