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