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