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