4ffd4939e85bc8777546afb99ac608c83977ba2f
[reactos.git] / irc / TechBot / TechBot.Library / Commands / WmCommand.cs
1 using System;
2 using System.Xml;
3
4 namespace TechBot.Library
5 {
6 [Command("wm" , Help = "!wm <value> or !wm <name>")]
7 public class WMCommand : XmlCommand
8 {
9 private string m_WMText = null;
10
11 public WMCommand()
12 {
13 }
14
15 public override string XmlFile
16 {
17 get { return Settings.Default.WMXml; }
18 }
19
20 [CommandParameter("wm", "The windows message to check")]
21 public string WMText
22 {
23 get { return m_WMText; }
24 set { m_WMText = value; }
25 }
26
27 public override void ExecuteCommand()
28 {
29 if (WMText.Equals(String.Empty))
30 {
31 TechBot.ServiceOutput.WriteLine(Context,
32 "Please provide a valid window message value or name.");
33 return;
34 }
35
36 NumberParser np = new NumberParser();
37 long wm = np.Parse(WMText);
38 string output;
39 if (np.Error)
40 {
41 // Assume "!wm <name>" form.
42 output = GetWmNumber(WMText);
43 }
44 else
45 {
46 output = GetWmDescription(wm);
47 }
48
49 if (output != null)
50 {
51 TechBot.ServiceOutput.WriteLine(Context,
52 String.Format("{0} is {1}.",
53 WMText,
54 output));
55 }
56 else
57 {
58 TechBot.ServiceOutput.WriteLine(Context,
59 String.Format("I don't know about window message {0}.",
60 WMText));
61 }
62 }
63
64 private string GetWmDescription(long wm)
65 {
66 XmlElement root = base.m_XmlDocument.DocumentElement;
67 XmlNode node = root.SelectSingleNode(String.Format("WindowMessage[@value='{0}']",
68 wm));
69 if (node != null)
70 {
71 XmlAttribute text = node.Attributes["text"];
72 if (text == null)
73 throw new Exception("Node has no text attribute.");
74 return text.Value;
75 }
76 else
77 return null;
78 }
79
80 private string GetWmNumber(string wmName)
81 {
82 XmlElement root = base.m_XmlDocument.DocumentElement;
83 XmlNode node = root.SelectSingleNode(String.Format("WindowMessage[@text='{0}']",
84 wmName));
85 if (node != null)
86 {
87 XmlAttribute value = node.Attributes["value"];
88 if (value == null)
89 throw new Exception("Node has no value attribute.");
90 return value.Value;
91 }
92 else
93 return null;
94 }
95 }
96 }