5e92b818cf616279520bd645183cae825805aaaa
[reactos.git] / irc / TechBot / TechBot.Library / WinerrorCommand.cs
1 using System;
2 using System.Xml;
3
4 namespace TechBot.Library
5 {
6 public class WinerrorCommand : BaseCommand, ICommand
7 {
8 private IServiceOutput serviceOutput;
9 private string winerrorXml;
10 private XmlDocument winerrorXmlDocument;
11
12 public WinerrorCommand(IServiceOutput serviceOutput,
13 string winerrorXml)
14 {
15 this.serviceOutput = serviceOutput;
16 this.winerrorXml = winerrorXml;
17 winerrorXmlDocument = new XmlDocument();
18 winerrorXmlDocument.Load(winerrorXml);
19 }
20
21 public bool CanHandle(string commandName)
22 {
23 return CanHandle(commandName,
24 new string[] { "winerror" });
25 }
26
27 public void Handle(string commandName,
28 string parameters)
29 {
30 string winerrorText = parameters;
31 if (winerrorText.Equals(String.Empty))
32 {
33 serviceOutput.WriteLine("Please provide a valid System Error Code value.");
34 return;
35 }
36
37 NumberParser np = new NumberParser();
38 long winerror = np.Parse(winerrorText);
39 if (np.Error)
40 {
41 serviceOutput.WriteLine(String.Format("{0} is not a valid System Error Code value.",
42 winerrorText));
43 return;
44 }
45
46 string description = GetWinerrorDescription(winerror);
47 if (description != null)
48 {
49 serviceOutput.WriteLine(String.Format("{0} is {1}.",
50 winerrorText,
51 description));
52 }
53 else
54 {
55 serviceOutput.WriteLine(String.Format("I don't know about System Error Code {0}.",
56 winerrorText));
57 }
58 }
59
60 public string Help()
61 {
62 return "!winerror <value>";
63 }
64
65 private string GetWinerrorDescription(long winerror)
66 {
67 XmlElement root = winerrorXmlDocument.DocumentElement;
68 XmlNode node = root.SelectSingleNode(String.Format("Winerror[@value='{0}']",
69 winerror));
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 }