- Moved commands outside TechBot.Library to TechBot.Commands.Common and TechBot.Comma...
[reactos.git] / irc / TechBot / TechBot.Commands.Common / WMCommand.cs
1 using System;
2 using System.Xml;
3
4 using TechBot.Library;
5
6 namespace TechBot.Commands.Common
7 {
8 [Command("wm" , Help = "!wm <value> or !wm <name>")]
9 public class WMCommand : XmlCommand
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" , DefaultParameter = true)]
21 public string WMText
22 {
23 get { return Parameters; }
24 set { Parameters = value; }
25 }
26
27 public override void ExecuteCommand()
28 {
29 if (WMText == null)
30 {
31 Say("Please provide a valid window message value or name.");
32
33 }
34 else
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 Say("{0} is {1}.",
52 WMText,
53 output);
54 }
55 else
56 {
57 Say("I don't know about window message {0}.", WMText);
58 }
59 }
60 }
61
62 private string GetWmDescription(long wm)
63 {
64 XmlElement root = base.m_XmlDocument.DocumentElement;
65 XmlNode node = root.SelectSingleNode(String.Format("WindowMessage[@value='{0}']",
66 wm));
67 if (node != null)
68 {
69 XmlAttribute text = node.Attributes["text"];
70 if (text == null)
71 throw new Exception("Node has no text attribute.");
72 return text.Value;
73 }
74 else
75 return null;
76 }
77
78 private string GetWmNumber(string wmName)
79 {
80 XmlElement root = base.m_XmlDocument.DocumentElement;
81 XmlNode node = root.SelectSingleNode(String.Format("WindowMessage[@text='{0}']",
82 wmName));
83 if (node != null)
84 {
85 XmlAttribute value = node.Attributes["value"];
86 if (value == null)
87 throw new Exception("Node has no value attribute.");
88 return value.Value;
89 }
90 else
91 return null;
92 }
93 }
94 }