Implement DICS_FLAG_CONFIGSPECIFIC case in SetupDiOpenDevRegKey, by factorizing some...
[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(MessageContext context,
28 string commandName,
29 string parameters)
30 {
31 string winerrorText = parameters;
32 if (winerrorText.Equals(String.Empty))
33 {
34 serviceOutput.WriteLine(context,
35 "Please provide a valid System Error Code value.");
36 return;
37 }
38
39 NumberParser np = new NumberParser();
40 long winerror = np.Parse(winerrorText);
41 if (np.Error)
42 {
43 serviceOutput.WriteLine(context,
44 String.Format("{0} is not a valid System Error Code value.",
45 winerrorText));
46 return;
47 }
48
49 string description = GetWinerrorDescription(winerror);
50 if (description != null)
51 {
52 serviceOutput.WriteLine(context,
53 String.Format("{0} is {1}.",
54 winerrorText,
55 description));
56 }
57 else
58 {
59 serviceOutput.WriteLine(context,
60 String.Format("I don't know about System Error Code {0}.",
61 winerrorText));
62 }
63 }
64
65 public string Help()
66 {
67 return "!winerror <value>";
68 }
69
70 private string GetWinerrorDescription(long winerror)
71 {
72 XmlElement root = winerrorXmlDocument.DocumentElement;
73 XmlNode node = root.SelectSingleNode(String.Format("Winerror[@value='{0}']",
74 winerror));
75 if (node != null)
76 {
77 XmlAttribute text = node.Attributes["text"];
78 if (text == null)
79 throw new Exception("Node has no text attribute.");
80 return text.Value;
81 }
82 else
83 return null;
84 }
85 }
86 }