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