* refactor the code to make it more OOP and extensible
[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 : XmlCommand
7 {
8 public HResultCommand(TechBotService techBot)
9 : base(techBot)
10 {
11 }
12
13 public override string XmlFile
14 {
15 get { return Settings.Default.HResultXml; }
16 }
17
18 public override string[] AvailableCommands
19 {
20 get { return new string[] { "hresult" }; }
21 }
22
23 /*
24 public bool CanHandle(string commandName)
25 {
26 return CanHandle(commandName,
27 new string[] { "hresult" });
28 }
29 */
30
31 public override void Handle(MessageContext context,
32 string commandName,
33 string parameters)
34 {
35 string hresultText = parameters;
36 if (hresultText.Equals(String.Empty))
37 {
38 TechBot.ServiceOutput.WriteLine(context,
39 "Please provide a valid HRESULT value.");
40 return;
41 }
42
43 NumberParser np = new NumberParser();
44 long hresult = np.Parse(hresultText);
45 if (np.Error)
46 {
47 TechBot.ServiceOutput.WriteLine(context,
48 String.Format("{0} is not a valid HRESULT value.",
49 hresultText));
50 return;
51 }
52
53 string description = GetHresultDescription(hresult);
54 if (description != null)
55 {
56 TechBot.ServiceOutput.WriteLine(context,
57 String.Format("{0} is {1}.",
58 hresultText,
59 description));
60 }
61 else
62 {
63 TechBot.ServiceOutput.WriteLine(context,
64 String.Format("I don't know about HRESULT {0}.",
65 hresultText));
66 }
67 }
68
69 public override string Help()
70 {
71 return "!hresult <value>";
72 }
73
74 public string GetHresultDescription(long hresult)
75 {
76 XmlElement root = base.m_XmlDocument.DocumentElement;
77 XmlNode node = root.SelectSingleNode(String.Format("Hresult[@value='{0}']",
78 hresult.ToString("X8")));
79 if (node != null)
80 {
81 XmlAttribute text = node.Attributes["text"];
82 if (text == null)
83 throw new Exception("Node has no text attribute.");
84 return text.Value;
85 }
86 else
87 return null;
88 }
89 }
90 }