- code refactoring

- made more and more easily extensible:
   * commands automatically loaded from plugins dlls
   * declarative and automatic command parameter parsing
   * common code moved to base classes
- other fixes

svn path=/trunk/; revision=33344
This commit is contained in:
Marc Piulachs 2008-05-07 14:59:28 +00:00
parent e3407fdd9c
commit d7b2077ed8
42 changed files with 2263 additions and 1689 deletions

View file

@ -1,14 +0,0 @@
<?xml version="1.0"?>
<project name="TechBot" default="build">
<target name="build" description="Build components">
<delete dir="bin" failonerror="false" />
<nant buildfile="Compression/Default.build" />
<nant buildfile="CHMLibrary/Default.build" />
<nant buildfile="TechBot.IRCLibrary/Default.build" />
<nant buildfile="TechBot.Library/Default.build" />
<nant buildfile="TechBot.Console/Default.build" />
<nant buildfile="TechBot/Default.build" />
</target>
</project>

View file

@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Text;
using TechBot.Library;
namespace TechBot.Console
{
public class ConsoleServiceOutput : IServiceOutput
{
public void WriteLine(MessageContext context,
string message)
{
System.Console.WriteLine(message);
}
}
public class ConsoleTechBotService : TechBotService
{
public ConsoleTechBotService(
string chmPath,
string mainChm)
: base(new ConsoleServiceOutput(), chmPath, mainChm)
{
}
public override void Run()
{
//Call the base class
base.Run();
while (true)
{
string s = System.Console.ReadLine();
InjectMessage(null, s);
}
}
}
}

View file

@ -4,16 +4,6 @@ using TechBot.Library;
namespace TechBot.Console namespace TechBot.Console
{ {
public class ConsoleServiceOutput : IServiceOutput
{
public void WriteLine(MessageContext context,
string message)
{
System.Console.WriteLine(message);
}
}
class MainClass class MainClass
{ {
private static void VerifyRequiredOption(string optionName, private static void VerifyRequiredOption(string optionName,
@ -208,53 +198,43 @@ namespace TechBot.Console
} }
private static void RunIrcService() //private static void RunIrcService()
{ //{
IrcService ircService = new IrcService(IRCServerHostName, // IrcTechBotService ircService = new IrcTechBotService(IRCServerHostName,
IRCServerHostPort, // IRCServerHostPort,
IRCChannelNames, // IRCChannelNames,
IRCBotName, // IRCBotName,
IRCBotPassword, // IRCBotPassword,
ChmPath, // ChmPath,
MainChm, // MainChm);
//NtstatusXml, // ircService.Run();
//WinerrorXml, //}
//HresultXml,
//WmXml, public static void Main(string[] args)
//SvnCommand, {
BugUrl, TechBotService m_TechBot = null;
WineBugUrl,
SambaBugUrl); if (args.Length > 0 && args[0].ToLower().Equals("irc"))
ircService.Run(); {
} m_TechBot = new IrcTechBotService(IRCServerHostName,
IRCServerHostPort,
public static void Main(string[] args) IRCChannelNames,
{ IRCBotName,
if (args.Length > 0 && args[0].ToLower().Equals("irc")) IRCBotPassword,
{ ChmPath,
RunIrcService(); MainChm);
return; }
} else
{
System.Console.WriteLine("TechBot running console service..."); System.Console.WriteLine("TechBot running console service...");
TechBotService service = new TechBotService(new ConsoleServiceOutput(), m_TechBot = new ConsoleTechBotService(
ChmPath, ChmPath,
MainChm); MainChm);
//NtstatusXml,
//WinerrorXml,
//HresultXml, }
//WmXml,
//SvnCommand, m_TechBot.Run();
//BugUrl, }
//WineBugUrl,
//SambaBugUrl);
service.Run();
while (true)
{
string s = System.Console.ReadLine();
service.InjectMessage(null,
s);
}
}
} }
} }

View file

@ -38,6 +38,7 @@
--> -->
<ItemGroup> <ItemGroup>
<Compile Include="AssemblyInfo.cs" /> <Compile Include="AssemblyInfo.cs" />
<Compile Include="ConsoleTechBotService.cs" />
<Compile Include="Main.cs" /> <Compile Include="Main.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View file

@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace TechBot.Library
{
public class CommandAttribute : Attribute
{
private string m_Name = null;
private string m_Help = "No help for this command is available";
private string m_Desc = "No description for this command is available";
public CommandAttribute(string name)
{
m_Name = name;
}
public string Name
{
get { return m_Name; }
}
public string Help
{
get { return m_Help; }
set { m_Help = value; }
}
public string Description
{
get { return m_Desc; }
set { m_Desc = value; }
}
}
}

View file

@ -0,0 +1,34 @@
using System;
namespace TechBot.Library
{
/// <summary>
/// This class implements an alias attribute to work in conjunction
/// with the <see cref="CommandLineSwitchAttribute">CommandLineSwitchAttribute</see>
/// attribute. If the CommandLineSwitchAttribute exists, then this attribute
/// defines an alias for it.
/// </summary>
[AttributeUsage( AttributeTargets.Property )]
public class CommandParameterAliasAttribute : Attribute
{
#region Private Variables
protected string m_Alias = "";
#endregion
#region Public Properties
public string Alias
{
get { return m_Alias; }
}
#endregion
#region Constructors
public CommandParameterAliasAttribute(string alias)
{
m_Alias = alias;
}
#endregion
}
}

View file

@ -0,0 +1,43 @@
using System;
namespace TechBot.Library
{
/// <summary>Implements a basic command-line switch by taking the
/// switching name and the associated description.</summary>
/// <remark>Only currently is implemented for properties, so all
/// auto-switching variables should have a get/set method supplied.</remark>
[AttributeUsage( AttributeTargets.Property )]
public class CommandParameterAttribute : Attribute
{
#region Private Variables
private string m_name = "";
private string m_description = "";
private bool m_Required = true;
#endregion
#region Public Properties
/// <summary>Accessor for retrieving the switch-name for an associated
/// property.</summary>
public string Name { get { return m_name; } }
/// <summary>Accessor for retrieving the description for a switch of
/// an associated property.</summary>
public string Description { get { return m_description; } }
public bool Required { get { return m_Required; } }
#endregion
#region Constructors
/// <summary>
/// Attribute constructor.
/// </summary>
public CommandParameterAttribute(string name, string description)
{
m_name = name;
m_description = description;
}
#endregion
}
}

View file

@ -1,50 +0,0 @@
using System;
namespace TechBot.Library
{
public abstract class BugCommand : Command//, ICommand
{
public BugCommand(TechBotService techBot) : base (techBot)
{
}
public override void Handle(MessageContext context,
string commandName,
string parameters)
{
string bugText = parameters;
if (bugText.Equals(String.Empty))
{
TechBot.ServiceOutput.WriteLine(context,
"Please provide a valid bug number.");
return;
}
NumberParser np = new NumberParser();
long bug = np.Parse(bugText);
if (np.Error)
{
TechBot.ServiceOutput.WriteLine(context,
String.Format("{0} is not a valid bug number.",
bugText));
return;
}
/*
string bugUrl = this.RosBugUrl;
if (context is ChannelMessageContext)
{
ChannelMessageContext channelContext = context as ChannelMessageContext;
if (channelContext.Channel.Name == "winehackers")
bugUrl = this.WineBugUrl;
else if (channelContext.Channel.Name == "samba-technical")
bugUrl = this.SambaBugUrl;
}*/
TechBot.ServiceOutput.WriteLine(context, String.Format(BugUrl, bug));
}
protected abstract string BugUrl { get; }
}
}

View file

@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace TechBot.Library
{
public class CommandBuilderCollection : List<CommandBuilder>
{
public CommandBuilder Find(string name)
{
foreach (CommandBuilder command in this)
{
if (command.Name == name)
return command;
}
return null;
}
}
}

View file

@ -1,53 +0,0 @@
using System;
namespace TechBot.Library
{
/*
public interface ICommand
{
bool CanHandle(string commandName);
void Handle(MessageContext context,
string commandName,
string parameters);
//string Help();
}*/
public abstract class Command
{
protected TechBotService m_TechBotService = null;
public Command(TechBotService techbot)
{
m_TechBotService = techbot;
}
public TechBotService TechBot
{
get { return m_TechBotService; }
}
public abstract string[] AvailableCommands { get; }
public abstract void Handle(MessageContext context,
string commandName,
string parameters);
/*
protected bool CanHandle(string commandName,
string[] availableCommands)
{
foreach (string availableCommand in availableCommands)
{
if (String.Compare(availableCommand, commandName, true) == 0)
return true;
}
return false;
}
*/
public virtual string Help()
{
return "No help is available for this command";
}
}
}

View file

@ -1,353 +1,333 @@
using System; using System;
using System.IO; using System.IO;
using System.Data; using System.Data;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using HtmlHelp; using HtmlHelp;
using HtmlHelp.ChmDecoding; using HtmlHelp.ChmDecoding;
namespace TechBot.Library namespace TechBot.Library
{ {
public class ApiCommand : Command [Command("api", Help = "!api <apiname>")]
{ public class ApiCommand : Command
private const bool IsVerbose = false; {
private const bool IsVerbose = false;
private HtmlHelpSystem chm;
private string chmPath; private HtmlHelpSystem chm;
private string mainChm; private string chmPath;
private string mainChm;
public ApiCommand(TechBotService techBot)
: base(techBot) public ApiCommand()
{ {
Run(); Run();
} }
private void WriteIfVerbose(MessageContext context, private void WriteIfVerbose(MessageContext context,
string message) string message)
{ {
if (IsVerbose) if (IsVerbose)
TechBot.ServiceOutput.WriteLine(context, TechBot.ServiceOutput.WriteLine(context,
message); message);
} }
private void Run() private void Run()
{ {
string CHMFilename = Path.Combine(chmPath, mainChm); string CHMFilename = Path.Combine(chmPath, mainChm);
chm = new HtmlHelpSystem(); chm = new HtmlHelpSystem();
chm.OpenFile(CHMFilename, null); chm.OpenFile(CHMFilename, null);
Console.WriteLine(String.Format("Loaded main CHM: {0}", Console.WriteLine(String.Format("Loaded main CHM: {0}",
Path.GetFileName(CHMFilename))); Path.GetFileName(CHMFilename)));
foreach (string filename in Directory.GetFiles(chmPath)) foreach (string filename in Directory.GetFiles(chmPath))
{ {
if (!Path.GetExtension(filename).ToLower().Equals(".chm")) if (!Path.GetExtension(filename).ToLower().Equals(".chm"))
continue; continue;
if (Path.GetFileName(filename).ToLower().Equals(mainChm)) if (Path.GetFileName(filename).ToLower().Equals(mainChm))
continue; continue;
Console.WriteLine(String.Format("Loading CHM: {0}", Console.WriteLine(String.Format("Loading CHM: {0}",
Path.GetFileName(filename))); Path.GetFileName(filename)));
try try
{ {
chm.MergeFile(filename); chm.MergeFile(filename);
} }
catch (Exception ex) catch (Exception ex)
{ {
Console.WriteLine(String.Format("Could not load CHM: {0}. Exception {1}", Console.WriteLine(String.Format("Could not load CHM: {0}. Exception {1}",
Path.GetFileName(filename), Path.GetFileName(filename),
ex)); ex));
} }
} }
Console.WriteLine(String.Format("Loaded {0} CHMs", Console.WriteLine(String.Format("Loaded {0} CHMs",
chm.FileList.Length)); chm.FileList.Length));
} }
public override string[] AvailableCommands public override void Handle(MessageContext context)
{ {
get { return new string[] { "api" }; } if (parameters.Trim().Equals(String.Empty))
} DisplayNoKeyword(context);
else
/* Search(context,
public bool CanHandle(string commandName) parameters);
{ }
return CanHandle(commandName,
new string[] { "api" }); private bool SearchIndex(MessageContext context,
} string keyword)
*/ {
if (chm.HasIndex)
public override void Handle(MessageContext context, {
string commandName, IndexItem item = chm.Index.SearchIndex(keyword,
string parameters) IndexType.KeywordLinks);
{ if (item != null && item.Topics.Count > 0)
if (parameters.Trim().Equals(String.Empty)) {
DisplayNoKeyword(context); WriteIfVerbose(context,
else String.Format("Keyword {0} found in index",
Search(context, item.KeyWord));
parameters); IndexTopic indexTopic = item.Topics[0] as IndexTopic;
} return DisplayResult(context,
keyword,
public override string Help() indexTopic);
{ }
return "!api <apiname>"; else
} {
WriteIfVerbose(context,
private bool SearchIndex(MessageContext context, String.Format("Keyword {0} not found in index",
string keyword) keyword));
{ return false;
if (chm.HasIndex) }
{ }
IndexItem item = chm.Index.SearchIndex(keyword, else
IndexType.KeywordLinks); return false;
if (item != null && item.Topics.Count > 0) }
{
WriteIfVerbose(context, private void SearchFullText(MessageContext context,
String.Format("Keyword {0} found in index", string keyword)
item.KeyWord)); {
IndexTopic indexTopic = item.Topics[0] as IndexTopic; string sort = "Rating ASC";
return DisplayResult(context, WriteIfVerbose(context,
keyword, String.Format("Searching fulltext database for {0}",
indexTopic); keyword));
}
else bool partialMatches = false;
{ bool titlesOnly = true;
WriteIfVerbose(context, int maxResults = 100;
String.Format("Keyword {0} not found in index", DataTable results = chm.PerformSearch(keyword,
keyword)); maxResults,
return false; partialMatches,
} titlesOnly);
} WriteIfVerbose(context,
else String.Format("results.Rows.Count = {0}",
return false; results != null ?
} results.Rows.Count.ToString() : "(none)"));
if (results != null && results.Rows.Count > 0)
private void SearchFullText(MessageContext context, {
string keyword) results.DefaultView.Sort = sort;
{ if (!DisplayResult(context,
string sort = "Rating ASC"; keyword,
WriteIfVerbose(context, results))
String.Format("Searching fulltext database for {0}", {
keyword)); DisplayNoResult(context,
keyword);
bool partialMatches = false; }
bool titlesOnly = true; }
int maxResults = 100; else
DataTable results = chm.PerformSearch(keyword, {
maxResults, DisplayNoResult(context,
partialMatches, keyword);
titlesOnly); }
WriteIfVerbose(context, }
String.Format("results.Rows.Count = {0}",
results != null ? private void Search(MessageContext context,
results.Rows.Count.ToString() : "(none)")); string keyword)
if (results != null && results.Rows.Count > 0) {
{ if (!SearchIndex(context,
results.DefaultView.Sort = sort; keyword))
if (!DisplayResult(context, SearchFullText(context,
keyword, keyword);
results)) }
{
DisplayNoResult(context, private bool DisplayResult(MessageContext context,
keyword); string keyword,
} IndexTopic indexTopic)
} {
else keyword = keyword.Trim().ToLower();
{ string url = indexTopic.URL;
DisplayNoResult(context, WriteIfVerbose(context,
keyword); String.Format("URL from index search {0}",
} url));
} string prototype = ExtractPrototype(context,
url);
private void Search(MessageContext context, if (prototype == null || prototype.Trim().Equals(String.Empty))
string keyword) return false;
{ string formattedPrototype = FormatPrototype(prototype);
if (!SearchIndex(context, TechBot.ServiceOutput.WriteLine(context,
keyword)) formattedPrototype);
SearchFullText(context, return true;
keyword); }
}
private bool DisplayResult(MessageContext context,
private bool DisplayResult(MessageContext context, string keyword,
string keyword, DataTable results)
IndexTopic indexTopic) {
{ keyword = keyword.Trim().ToLower();
keyword = keyword.Trim().ToLower(); for (int i = 0; i < results.DefaultView.Count; i++)
string url = indexTopic.URL; {
WriteIfVerbose(context, DataRowView row = results.DefaultView[i];
String.Format("URL from index search {0}", string title = row["Title"].ToString();
url)); WriteIfVerbose(context,
string prototype = ExtractPrototype(context, String.Format("Examining {0}", title));
url); if (title.Trim().ToLower().Equals(keyword))
if (prototype == null || prototype.Trim().Equals(String.Empty)) {
return false; string location = row["Location"].ToString();
string formattedPrototype = FormatPrototype(prototype); string rating = row["Rating"].ToString();
TechBot.ServiceOutput.WriteLine(context, string url = row["Url"].ToString();
formattedPrototype); string prototype = ExtractPrototype(context,
return true; url);
} if (prototype == null || prototype.Trim().Equals(String.Empty))
continue;
private bool DisplayResult(MessageContext context, string formattedPrototype = FormatPrototype(prototype);
string keyword, TechBot.ServiceOutput.WriteLine(context,
DataTable results) formattedPrototype);
{ return true;
keyword = keyword.Trim().ToLower(); }
for (int i = 0; i < results.DefaultView.Count; i++) }
{ return false;
DataRowView row = results.DefaultView[i]; }
string title = row["Title"].ToString();
WriteIfVerbose(context, private void DisplayNoResult(MessageContext context,
String.Format("Examining {0}", title)); string keyword)
if (title.Trim().ToLower().Equals(keyword)) {
{ TechBot.ServiceOutput.WriteLine(context,
string location = row["Location"].ToString(); String.Format("I don't know about keyword {0}",
string rating = row["Rating"].ToString(); keyword));
string url = row["Url"].ToString(); }
string prototype = ExtractPrototype(context,
url); private void DisplayNoKeyword(MessageContext context)
if (prototype == null || prototype.Trim().Equals(String.Empty)) {
continue; TechBot.ServiceOutput.WriteLine(context,
string formattedPrototype = FormatPrototype(prototype); "Please give me a keyword.");
TechBot.ServiceOutput.WriteLine(context, }
formattedPrototype);
return true; private string ReplaceComments(string s)
} {
} return Regex.Replace(s, "//(.+)\r\n", "");
return false; }
}
private string ReplaceLineEndings(string s)
private void DisplayNoResult(MessageContext context, {
string keyword) return Regex.Replace(s, "(\r\n)+", " ");
{ }
TechBot.ServiceOutput.WriteLine(context,
String.Format("I don't know about keyword {0}", private string ReplaceSpaces(string s)
keyword)); {
} return Regex.Replace(s, @" +", " ");
}
private void DisplayNoKeyword(MessageContext context)
{ private string ReplaceSpacesBeforeLeftParenthesis(string s)
TechBot.ServiceOutput.WriteLine(context, {
"Please give me a keyword."); return Regex.Replace(s, @"\( ", @"(");
} }
private string ReplaceComments(string s) private string ReplaceSpacesBeforeRightParenthesis(string s)
{ {
return Regex.Replace(s, "//(.+)\r\n", ""); return Regex.Replace(s, @" \)", @")");
} }
private string ReplaceLineEndings(string s) private string ReplaceSemicolon(string s)
{ {
return Regex.Replace(s, "(\r\n)+", " "); return Regex.Replace(s, @";", @"");
} }
private string ReplaceSpaces(string s) private string FormatPrototype(string prototype)
{ {
return Regex.Replace(s, @" +", " "); string s = ReplaceComments(prototype);
} s = ReplaceLineEndings(s);
s = ReplaceSpaces(s);
private string ReplaceSpacesBeforeLeftParenthesis(string s) s = ReplaceSpacesBeforeLeftParenthesis(s);
{ s = ReplaceSpacesBeforeRightParenthesis(s);
return Regex.Replace(s, @"\( ", @"("); s = ReplaceSemicolon(s);
} return s;
}
private string ReplaceSpacesBeforeRightParenthesis(string s)
{ private string ExtractPrototype(MessageContext context,
return Regex.Replace(s, @" \)", @")"); string url)
} {
string page = GetPage(context,
private string ReplaceSemicolon(string s) url);
{ Match match = Regex.Match(page,
return Regex.Replace(s, @";", @""); "<PRE class=\"?syntax\"?>(.+)</PRE>",
} RegexOptions.Multiline |
RegexOptions.Singleline);
private string FormatPrototype(string prototype) if (match.Groups.Count > 1)
{ {
string s = ReplaceComments(prototype); string prototype = match.Groups[1].ToString();
s = ReplaceLineEndings(s); return StripHtml(StripAfterSlashPre(prototype));
s = ReplaceSpaces(s); }
s = ReplaceSpacesBeforeLeftParenthesis(s);
s = ReplaceSpacesBeforeRightParenthesis(s); return "";
s = ReplaceSemicolon(s); }
return s;
} private string StripAfterSlashPre(string html)
{
private string ExtractPrototype(MessageContext context, int index = html.IndexOf("</PRE>");
string url) if (index != -1)
{ {
string page = GetPage(context, return html.Substring(0, index);
url); }
Match match = Regex.Match(page, else
"<PRE class=\"?syntax\"?>(.+)</PRE>", return html;
RegexOptions.Multiline | }
RegexOptions.Singleline);
if (match.Groups.Count > 1) private string StripHtml(string html)
{ {
string prototype = match.Groups[1].ToString(); return Regex.Replace(html, @"<(.|\n)*?>", String.Empty);
return StripHtml(StripAfterSlashPre(prototype)); }
}
private string GetPage(MessageContext context,
return ""; string url)
} {
string CHMFileName = "";
private string StripAfterSlashPre(string html) string topicName = "";
{ string anchor = "";
int index = html.IndexOf("</PRE>"); CHMStream.CHMStream baseStream;
if (index != -1) if (!chm.BaseStream.GetCHMParts(url, ref CHMFileName, ref topicName, ref anchor))
{ {
return html.Substring(0, index); baseStream = chm.BaseStream;
} CHMFileName = baseStream.CHMFileName;
else topicName = url;
return html; anchor = "";
} }
else
private string StripHtml(string html) {
{ baseStream = GetBaseStreamFromCHMFileName(context,
return Regex.Replace(html, @"<(.|\n)*?>", String.Empty); CHMFileName);
} }
private string GetPage(MessageContext context, if ((topicName == "") || (CHMFileName == "") || (baseStream == null))
string url) {
{ return "";
string CHMFileName = ""; }
string topicName = "";
string anchor = ""; return baseStream.ExtractTextFile(topicName);
CHMStream.CHMStream baseStream; }
if (!chm.BaseStream.GetCHMParts(url, ref CHMFileName, ref topicName, ref anchor))
{ private CHMStream.CHMStream GetBaseStreamFromCHMFileName(MessageContext context,
baseStream = chm.BaseStream; string CHMFileName)
CHMFileName = baseStream.CHMFileName; {
topicName = url; foreach (CHMFile file in chm.FileList)
anchor = ""; {
} WriteIfVerbose(context,
else String.Format("Compare: {0} <> {1}",
{ file.ChmFilePath,
baseStream = GetBaseStreamFromCHMFileName(context, CHMFileName));
CHMFileName); if (file.ChmFilePath.ToLower().Equals(CHMFileName.ToLower()))
} {
return file.BaseStream;
if ((topicName == "") || (CHMFileName == "") || (baseStream == null)) }
{ }
return ""; WriteIfVerbose(context,
} String.Format("Could not find loaded CHM file in list: {0}",
CHMFileName));
return baseStream.ExtractTextFile(topicName); return null;
} }
}
private CHMStream.CHMStream GetBaseStreamFromCHMFileName(MessageContext context, }
string CHMFileName)
{
foreach (CHMFile file in chm.FileList)
{
WriteIfVerbose(context,
String.Format("Compare: {0} <> {1}",
file.ChmFilePath,
CHMFileName));
if (file.ChmFilePath.ToLower().Equals(CHMFileName.ToLower()))
{
return file.BaseStream;
}
}
WriteIfVerbose(context,
String.Format("Could not find loaded CHM file in list: {0}",
CHMFileName));
return null;
}
}
}

View file

@ -0,0 +1,51 @@
using System;
namespace TechBot.Library
{
public abstract class Command
{
protected TechBotService m_TechBotService = null;
protected MessageContext m_Context = null;
public TechBotService TechBot
{
get { return m_TechBotService; }
set { m_TechBotService = value; }
}
public MessageContext Context
{
get { return m_Context; }
set { m_Context = value; }
}
public string Name
{
get
{
CommandAttribute commandAttribute = (CommandAttribute)
Attribute.GetCustomAttribute(GetType(), typeof(CommandAttribute));
return commandAttribute.Name;
}
}
public void ParseParameters(string paramaters)
{
ParametersParser parser = new ParametersParser(paramaters, this);
parser.Parse();
}
protected virtual void Say(string message)
{
TechBot.ServiceOutput.WriteLine(Context, message);
}
protected virtual void Say(string format , params object[] args)
{
TechBot.ServiceOutput.WriteLine(Context, String.Format(format, args));
}
public abstract void ExecuteCommand();
}
}

View file

@ -9,8 +9,7 @@ namespace TechBot.Library
{ {
protected XmlDocument m_XmlDocument; protected XmlDocument m_XmlDocument;
public XmlCommand(TechBotService techBot) public XmlCommand()
: base(techBot)
{ {
m_XmlDocument = new XmlDocument(); m_XmlDocument = new XmlDocument();
m_XmlDocument.Load(XmlFile); m_XmlDocument.Load(XmlFile);

View file

@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace TechBot.Library
{
public abstract class XmlLookupCommand : XmlCommand
{
private string m_Text = null;
[CommandParameter("text", "The value to check")]
public string Text
{
get { return m_Text; }
set { m_Text = value; }
}
}
}

View file

@ -0,0 +1,41 @@
using System;
namespace TechBot.Library
{
public abstract class BugCommand : Command
{
private string m_BugID = null;
public BugCommand()
{
}
[CommandParameter("id", "The bug ID")]
public string BugID
{
get { return m_BugID; }
set { m_BugID = value; }
}
public override void ExecuteCommand()
{
if (BugID == null)
{
Say("Please provide a valid bug number.");
}
else
{
try
{
Say(BugUrl, Int32.Parse(BugID));
}
catch (Exception)
{
Say("{0} is not a valid bug number.", BugID);
}
}
}
protected abstract string BugUrl { get; }
}
}

View file

@ -1,206 +1,185 @@
using System; using System;
using System.Xml; using System.Xml;
using System.Collections; using System.Collections;
namespace TechBot.Library namespace TechBot.Library
{ {
public class ErrorCommand : Command [Command("error", Help = "!error <value>")]
{ public class ErrorCommand : Command
private NtStatusCommand ntStatus; {
private WinerrorCommand winerror; private NtStatusCommand ntStatus;
private HResultCommand hresult; private WinErrorCommand winerror;
private HResultCommand hresult;
public ErrorCommand(TechBotService techBot)
: base(techBot) public ErrorCommand()
{ {
this.ntStatus = new NtStatusCommand(techBot); this.ntStatus = new NtStatusCommand();
this.winerror = new WinerrorCommand(techBot); this.winerror = new WinErrorCommand();
this.hresult = new HResultCommand(techBot); this.hresult = new HResultCommand();
} }
/* private static int GetSeverity(long error)
public bool CanHandle(string commandName) {
{ return (int)((error >> 30) & 0x3);
return CanHandle(commandName, }
new string[] { "error" });
} private static bool IsCustomer(long error)
*/ {
return (error & 0x20000000) != 0;
public override string[] AvailableCommands }
{
get { return new string[] { "error" }; } private static bool IsReserved(long error)
} {
return (error & 0x10000000) != 0;
private static int GetSeverity(long error) }
{
return (int)((error >> 30) & 0x3); private static int GetFacility(long error)
} {
return (int)((error >> 16) & 0xFFF);
private static bool IsCustomer(long error) }
{
return (error & 0x20000000) != 0; private static short GetCode(long error)
} {
return (short)((error >> 0) & 0xFFFF);
private static bool IsReserved(long error) }
{
return (error & 0x10000000) != 0; private static string FormatSeverity(long error)
} {
int severity = GetSeverity(error);
private static int GetFacility(long error) switch (severity)
{ {
return (int)((error >> 16) & 0xFFF); case 0: return "SUCCESS";
} case 1: return "INFORMATIONAL";
case 2: return "WARNING";
private static short GetCode(long error) case 3: return "ERROR";
{ }
return (short)((error >> 0) & 0xFFFF); return null;
} }
private static string FormatSeverity(long error) private static string FormatFacility(long error)
{ {
int severity = GetSeverity(error); int facility = GetFacility(error);
switch (severity) return facility.ToString();
{ }
case 0: return "SUCCESS";
case 1: return "INFORMATIONAL"; private static string FormatCode(long error)
case 2: return "WARNING"; {
case 3: return "ERROR"; int code = GetCode(error);
} return code.ToString();
return null; }
}
public override void Handle(MessageContext context)
private static string FormatFacility(long error) {
{ if (Text.Equals(String.Empty))
int facility = GetFacility(error); {
return facility.ToString(); TechBot.ServiceOutput.WriteLine(context,
} "Please provide an Error Code.");
return;
private static string FormatCode(long error) }
{
int code = GetCode(error); string errorText = originalErrorText;
return code.ToString();
} retry:
NumberParser np = new NumberParser();
public override void Handle(MessageContext context, long error = np.Parse(errorText);
string commandName, if (np.Error)
string parameters) {
{ TechBot.ServiceOutput.WriteLine(context,
string originalErrorText = parameters.Trim(); String.Format("{0} is not a valid Error Code.",
if (originalErrorText.Equals(String.Empty)) originalErrorText));
{ return;
TechBot.ServiceOutput.WriteLine(context, }
"Please provide an Error Code.");
return; ArrayList descriptions = new ArrayList();
}
// Error is out of bounds
string errorText = originalErrorText; if ((ulong)error > uint.MaxValue)
{
retry: // Do nothing
NumberParser np = new NumberParser(); }
long error = np.Parse(errorText); // Error is outside of the range [0, 65535]: it cannot be a plain Win32 error code
if (np.Error) else if ((ulong)error > ushort.MaxValue)
{ {
TechBot.ServiceOutput.WriteLine(context, // Customer bit is set: custom error code
String.Format("{0} is not a valid Error Code.", if (IsCustomer(error))
originalErrorText)); {
return; string description = String.Format("[custom, severity {0}, facility {1}, code {2}]",
} FormatSeverity(error),
FormatFacility(error),
ArrayList descriptions = new ArrayList(); FormatCode(error));
descriptions.Add(description);
// Error is out of bounds }
if ((ulong)error > uint.MaxValue) // Reserved bit is set: HRESULT_FROM_NT(ntstatus)
{ else if (IsReserved(error))
// Do nothing {
} int status = (int)(error & 0xCFFFFFFF);
// Error is outside of the range [0, 65535]: it cannot be a plain Win32 error code string description = ntStatus.GetNtstatusDescription(status);
else if ((ulong)error > ushort.MaxValue)
{ if (description == null)
// Customer bit is set: custom error code description = status.ToString("X");
if (IsCustomer(error))
{ description = String.Format("HRESULT_FROM_NT({0})", description);
string description = String.Format("[custom, severity {0}, facility {1}, code {2}]", descriptions.Add(description);
FormatSeverity(error), }
FormatFacility(error), // Win32 facility: HRESULT_FROM_WIN32(winerror)
FormatCode(error)); else if (GetFacility(error) == 7)
descriptions.Add(description); {
} // Must be an error code
// Reserved bit is set: HRESULT_FROM_NT(ntstatus) if (GetSeverity(error) == 2)
else if (IsReserved(error)) {
{ short err = GetCode(error);
int status = (int)(error & 0xCFFFFFFF); string description = winerror.GetWinerrorDescription(err);
string description = ntStatus.GetNtstatusDescription(status);
if (description == null)
if (description == null) description = err.ToString("D");
description = status.ToString("X");
description = String.Format("HRESULT_FROM_WIN32({0})", description);
description = String.Format("HRESULT_FROM_NT({0})", description); descriptions.Add(description);
descriptions.Add(description); }
} }
// Win32 facility: HRESULT_FROM_WIN32(winerror) }
else if (GetFacility(error) == 7)
{ string winerrorDescription = winerror.GetWinerrorDescription(error);
// Must be an error code string ntstatusDescription = ntStatus.GetNtstatusDescription(error);
if (GetSeverity(error) == 2) string hresultDescription = hresult.GetHresultDescription(error);
{
short err = GetCode(error); if (winerrorDescription != null)
string description = winerror.GetWinerrorDescription(err); descriptions.Add(winerrorDescription);
if (ntstatusDescription != null)
if (description == null) descriptions.Add(ntstatusDescription);
description = err.ToString("D"); if (hresultDescription != null)
descriptions.Add(hresultDescription);
description = String.Format("HRESULT_FROM_WIN32({0})", description);
descriptions.Add(description); if (descriptions.Count == 0)
} {
} // Last chance heuristics: attempt to parse a 8-digit decimal as hexadecimal
} if (errorText.Length == 8)
{
string winerrorDescription = winerror.GetWinerrorDescription(error); errorText = "0x" + errorText;
string ntstatusDescription = ntStatus.GetNtstatusDescription(error); goto retry;
string hresultDescription = hresult.GetHresultDescription(error); }
if (winerrorDescription != null) TechBot.ServiceOutput.WriteLine(context,
descriptions.Add(winerrorDescription); String.Format("I don't know about Error Code {0}.",
if (ntstatusDescription != null) originalErrorText));
descriptions.Add(ntstatusDescription); }
if (hresultDescription != null) else if (descriptions.Count == 1)
descriptions.Add(hresultDescription); {
string description = (string)descriptions[0];
if (descriptions.Count == 0) TechBot.ServiceOutput.WriteLine(context,
{ String.Format("{0} is {1}.",
// Last chance heuristics: attempt to parse a 8-digit decimal as hexadecimal originalErrorText,
if (errorText.Length == 8) description));
{ }
errorText = "0x" + errorText; else
goto retry; {
} TechBot.ServiceOutput.WriteLine(context,
String.Format("{0} could be:",
TechBot.ServiceOutput.WriteLine(context, originalErrorText));
String.Format("I don't know about Error Code {0}.",
originalErrorText)); foreach(string description in descriptions)
} TechBot.ServiceOutput.WriteLine(context, String.Format("\t{0}", description));
else if (descriptions.Count == 1) }
{ }
string description = (string)descriptions[0]; }
TechBot.ServiceOutput.WriteLine(context, }
String.Format("{0} is {1}.",
originalErrorText,
description));
}
else
{
TechBot.ServiceOutput.WriteLine(context,
String.Format("{0} could be:",
originalErrorText));
foreach(string description in descriptions)
TechBot.ServiceOutput.WriteLine(context, String.Format("\t{0}", description));
}
}
public override string Help()
{
return "!error <value>";
}
}
}

View file

@ -0,0 +1,51 @@
using System;
using System.Collections;
namespace TechBot.Library
{
[Command("help", Help = "!help")]
public class HelpCommand : Command
{
private string m_CommandName = null;
public HelpCommand()
{
}
[CommandParameter("Name", "The command name to show help")]
public string CommandName
{
get { return m_CommandName; }
set { m_CommandName = value; }
}
public override void ExecuteCommand()
{
if (CommandName == null)
{
Say("I support the following commands:");
foreach (CommandBuilder command in TechBot.Commands)
{
Say("!{0} - {1}",
command.Name,
command.Description);
}
}
else
{
CommandBuilder cmdBuilder = TechBot.Commands.Find(CommandName);
if (cmdBuilder == null)
{
Say("Command '{0}' is not recognized. Type '!help' to show all available commands", CommandName);
}
else
{
Say("Command '{0}' help:", CommandName);
Say("");
}
}
}
}
}

View file

@ -1,90 +1,69 @@
using System; using System;
using System.Xml; using System.Xml;
namespace TechBot.Library namespace TechBot.Library
{ {
public class HResultCommand : XmlCommand [Command("hresult", Help = "!hresult <value>")]
{ public class HResultCommand : XmlLookupCommand
public HResultCommand(TechBotService techBot) {
: base(techBot) public HResultCommand()
{ {
} }
public override string XmlFile public override string XmlFile
{ {
get { return Settings.Default.HResultXml; } get { return Settings.Default.HResultXml; }
} }
public override string[] AvailableCommands public override void ExecuteCommand()
{ {
get { return new string[] { "hresult" }; } if (Text.Equals(String.Empty))
} {
TechBot.ServiceOutput.WriteLine(Context,
/* "Please provide a valid HRESULT value.");
public bool CanHandle(string commandName) return;
{ }
return CanHandle(commandName,
new string[] { "hresult" }); NumberParser np = new NumberParser();
} long hresult = np.Parse(Text);
*/ if (np.Error)
{
public override void Handle(MessageContext context, TechBot.ServiceOutput.WriteLine(Context,
string commandName, String.Format("{0} is not a valid HRESULT value.",
string parameters) Text));
{ return;
string hresultText = parameters; }
if (hresultText.Equals(String.Empty))
{ string description = GetHresultDescription(hresult);
TechBot.ServiceOutput.WriteLine(context, if (description != null)
"Please provide a valid HRESULT value."); {
return; TechBot.ServiceOutput.WriteLine(Context,
} String.Format("{0} is {1}.",
Text,
NumberParser np = new NumberParser(); description));
long hresult = np.Parse(hresultText); }
if (np.Error) else
{ {
TechBot.ServiceOutput.WriteLine(context, TechBot.ServiceOutput.WriteLine(Context,
String.Format("{0} is not a valid HRESULT value.", String.Format("I don't know about HRESULT {0}.",
hresultText)); Text));
return; }
} }
string description = GetHresultDescription(hresult); public string GetHresultDescription(long hresult)
if (description != null) {
{ XmlElement root = base.m_XmlDocument.DocumentElement;
TechBot.ServiceOutput.WriteLine(context, XmlNode node = root.SelectSingleNode(String.Format("Hresult[@value='{0}']",
String.Format("{0} is {1}.", hresult.ToString("X8")));
hresultText, if (node != null)
description)); {
} XmlAttribute text = node.Attributes["text"];
else if (text == null)
{ throw new Exception("Node has no text attribute.");
TechBot.ServiceOutput.WriteLine(context, return text.Value;
String.Format("I don't know about HRESULT {0}.", }
hresultText)); else
} return null;
} }
}
public override string Help() }
{
return "!hresult <value>";
}
public string GetHresultDescription(long hresult)
{
XmlElement root = base.m_XmlDocument.DocumentElement;
XmlNode node = root.SelectSingleNode(String.Format("Hresult[@value='{0}']",
hresult.ToString("X8")));
if (node != null)
{
XmlAttribute text = node.Attributes["text"];
if (text == null)
throw new Exception("Node has no text attribute.");
return text.Value;
}
else
return null;
}
}
}

View file

@ -1,88 +1,69 @@
using System; using System;
using System.Xml; using System.Xml;
namespace TechBot.Library namespace TechBot.Library
{ {
public class NtStatusCommand : XmlCommand [Command("ntstatus", Help = "!ntstatus <value>")]
{ public class NtStatusCommand : XmlLookupCommand
public NtStatusCommand(TechBotService techBot) {
: base(techBot) public NtStatusCommand()
{ {
} }
public override string XmlFile public override string XmlFile
{ {
get { return Settings.Default.NtStatusXml; } get { return Settings.Default.NtStatusXml; }
} }
public override string[] AvailableCommands public override void ExecuteCommand()
{ {
get { return new string[] { "ntstatus" }; } if (Text.Equals(String.Empty))
} {
/* TechBot.ServiceOutput.WriteLine(Context,
public bool CanHandle(string commandName) "Please provide a valid NTSTATUS value.");
{ return;
return CanHandle(commandName, }
new string[] { "ntstatus" });
} NumberParser np = new NumberParser();
*/ long ntstatus = np.Parse(Text);
public override void Handle(MessageContext context, if (np.Error)
string commandName, {
string parameters) TechBot.ServiceOutput.WriteLine(Context,
{ String.Format("{0} is not a valid NTSTATUS value.",
string ntstatusText = parameters; Text));
if (ntstatusText.Equals(String.Empty)) return;
{ }
TechBot.ServiceOutput.WriteLine(context,
"Please provide a valid NTSTATUS value."); string description = GetNtstatusDescription(ntstatus);
return; if (description != null)
} {
TechBot.ServiceOutput.WriteLine(Context,
NumberParser np = new NumberParser(); String.Format("{0} is {1}.",
long ntstatus = np.Parse(ntstatusText); Text,
if (np.Error) description));
{ }
TechBot.ServiceOutput.WriteLine(context, else
String.Format("{0} is not a valid NTSTATUS value.", {
ntstatusText)); TechBot.ServiceOutput.WriteLine(Context,
return; String.Format("I don't know about NTSTATUS {0}.",
} Text));
}
string description = GetNtstatusDescription(ntstatus); }
if (description != null)
{ public string GetNtstatusDescription(long ntstatus)
TechBot.ServiceOutput.WriteLine(context, {
String.Format("{0} is {1}.", XmlElement root = base.m_XmlDocument.DocumentElement;
ntstatusText, XmlNode node = root.SelectSingleNode(String.Format("Ntstatus[@value='{0}']",
description)); ntstatus.ToString("X8")));
} if (node != null)
else {
{ XmlAttribute text = node.Attributes["text"];
TechBot.ServiceOutput.WriteLine(context, if (text == null)
String.Format("I don't know about NTSTATUS {0}.", throw new Exception("Node has no text attribute.");
ntstatusText)); return text.Value;
} }
} else
return null;
public override string Help() }
{ }
return "!ntstatus <value>"; }
}
public string GetNtstatusDescription(long ntstatus)
{
XmlElement root = base.m_XmlDocument.DocumentElement;
XmlNode node = root.SelectSingleNode(String.Format("Ntstatus[@value='{0}']",
ntstatus.ToString("X8")));
if (node != null)
{
XmlAttribute text = node.Attributes["text"];
if (text == null)
throw new Exception("Node has no text attribute.");
return text.Value;
}
else
return null;
}
}
}

View file

@ -4,26 +4,16 @@ using System.Text;
namespace TechBot.Library namespace TechBot.Library
{ {
[Command("rosbug", Help = "!rosbug <number>")]
class ReactOSBugUrl : BugCommand class ReactOSBugUrl : BugCommand
{ {
public ReactOSBugUrl(TechBotService techBot) public ReactOSBugUrl()
: base(techBot)
{ {
} }
public override string[] AvailableCommands
{
get { return new string[] { "rosbug" }; }
}
protected override string BugUrl protected override string BugUrl
{ {
get { return "http://www.reactos.org/bugzilla/show_bug.cgi?id={0}"; } get { return "http://www.reactos.org/bugzilla/show_bug.cgi?id={0}"; }
} }
public override string Help()
{
return "!rosbug <number>";
}
} }
} }

View file

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace TechBot.Library
{
[Command("sambabug", Help = "!sambabug <number>")]
class SambaBugUrl : BugCommand
{
public SambaBugUrl()
{
}
protected override string BugUrl
{
get { return "https://bugzilla.samba.org/show_bug.cgi?id={0}"; }
}
}
}

View file

@ -0,0 +1,20 @@
using System;
namespace TechBot.Library
{
[Command("svn", Help = "!svn")]
public class SvnCommand : Command
{
private string m_SvnRoot;
public SvnCommand()
{
m_SvnRoot = Settings.Default.SVNRoot;
}
public override void ExecuteCommand()
{
Say("svn co {0}", m_SvnRoot);
}
}
}

View file

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace TechBot.Library
{
[Command("winebug", Help = "!winebug <number>")]
class WineBugUrl : BugCommand
{
public WineBugUrl()
{
}
protected override string BugUrl
{
get { return "http://bugs.winehq.org/show_bug.cgi?id={0}"; }
}
}
}

View file

@ -1,82 +1,69 @@
using System; using System;
using System.Xml; using System.Xml;
namespace TechBot.Library namespace TechBot.Library
{ {
public class WinerrorCommand : XmlCommand [Command("winerror", Help = "!winerror <value>")]
{ public class WinErrorCommand : XmlLookupCommand
public WinerrorCommand(TechBotService techBot) {
: base(techBot) public WinErrorCommand()
{ {
} }
public override string XmlFile public override string XmlFile
{ {
get { return Settings.Default.WinErrorXml; } get { return Settings.Default.WinErrorXml; }
} }
public override string[] AvailableCommands public override void ExecuteCommand()
{ {
get { return new string[] { "winerror" }; } if (Text.Equals(String.Empty))
} {
TechBot.ServiceOutput.WriteLine(Context,
public override void Handle(MessageContext context, "Please provide a valid System Error Code value.");
string commandName, return;
string parameters) }
{
string winerrorText = parameters; NumberParser np = new NumberParser();
if (winerrorText.Equals(String.Empty)) long winerror = np.Parse(Text);
{ if (np.Error)
TechBot.ServiceOutput.WriteLine(context, {
"Please provide a valid System Error Code value."); TechBot.ServiceOutput.WriteLine(Context,
return; String.Format("{0} is not a valid System Error Code value.",
} Text));
return;
NumberParser np = new NumberParser(); }
long winerror = np.Parse(winerrorText);
if (np.Error) string description = GetWinerrorDescription(winerror);
{ if (description != null)
TechBot.ServiceOutput.WriteLine(context, {
String.Format("{0} is not a valid System Error Code value.", TechBot.ServiceOutput.WriteLine(Context,
winerrorText)); String.Format("{0} is {1}.",
return; Text,
} description));
}
string description = GetWinerrorDescription(winerror); else
if (description != null) {
{ TechBot.ServiceOutput.WriteLine(Context,
TechBot.ServiceOutput.WriteLine(context, String.Format("I don't know about System Error Code {0}.",
String.Format("{0} is {1}.", Text));
winerrorText, }
description)); }
}
else public string GetWinerrorDescription(long winerror)
{ {
TechBot.ServiceOutput.WriteLine(context, XmlElement root = base.m_XmlDocument.DocumentElement;
String.Format("I don't know about System Error Code {0}.", XmlNode node = root.SelectSingleNode(String.Format("Winerror[@value='{0}']",
winerrorText)); Text));
} if (node != null)
} {
XmlAttribute text = node.Attributes["text"];
public override string Help() if (text == null)
{ throw new Exception("Node has no text attribute.");
return "!winerror <value>"; return text.Value;
} }
else
public string GetWinerrorDescription(long winerror) return null;
{ }
XmlElement root = base.m_XmlDocument.DocumentElement; }
XmlNode node = root.SelectSingleNode(String.Format("Winerror[@value='{0}']", }
winerror));
if (node != null)
{
XmlAttribute text = node.Attributes["text"];
if (text == null)
throw new Exception("Node has no text attribute.");
return text.Value;
}
else
return null;
}
}
}

View file

@ -1,100 +1,96 @@
using System; using System;
using System.Xml; using System.Xml;
namespace TechBot.Library namespace TechBot.Library
{ {
public class WMCommand : XmlCommand [Command("wm" , Help = "!wm <value> or !wm <name>")]
{ public class WMCommand : XmlCommand
public WMCommand(TechBotService techBot) {
: base(techBot) private string m_WMText = null;
{
} public WMCommand()
{
public override string XmlFile }
{
get { return Settings.Default.WMXml; } public override string XmlFile
} {
get { return Settings.Default.WMXml; }
public override string[] AvailableCommands }
{
get { return new string[] { "wm" }; } [CommandParameter("wm", "The windows message to check")]
} public string WMText
{
public override void Handle(MessageContext context, get { return m_WMText; }
string commandName, set { m_WMText = value; }
string parameters) }
{
string wmText = parameters; public override void ExecuteCommand()
if (wmText.Equals(String.Empty)) {
{ if (WMText.Equals(String.Empty))
TechBot.ServiceOutput.WriteLine(context, {
"Please provide a valid window message value or name."); TechBot.ServiceOutput.WriteLine(Context,
return; "Please provide a valid window message value or name.");
} return;
}
NumberParser np = new NumberParser();
long wm = np.Parse(wmText); NumberParser np = new NumberParser();
string output; long wm = np.Parse(WMText);
if (np.Error) string output;
{ if (np.Error)
// Assume "!wm <name>" form. {
output = GetWmNumber(wmText); // Assume "!wm <name>" form.
} output = GetWmNumber(WMText);
else }
{ else
output = GetWmDescription(wm); {
} output = GetWmDescription(wm);
}
if (output != null)
{ if (output != null)
TechBot.ServiceOutput.WriteLine(context, {
String.Format("{0} is {1}.", TechBot.ServiceOutput.WriteLine(Context,
wmText, String.Format("{0} is {1}.",
output)); WMText,
} output));
else }
{ else
TechBot.ServiceOutput.WriteLine(context, {
String.Format("I don't know about window message {0}.", TechBot.ServiceOutput.WriteLine(Context,
wmText)); String.Format("I don't know about window message {0}.",
} WMText));
} }
}
public override string Help()
{ private string GetWmDescription(long wm)
return "!wm <value> or !wm <name>"; {
} XmlElement root = base.m_XmlDocument.DocumentElement;
XmlNode node = root.SelectSingleNode(String.Format("WindowMessage[@value='{0}']",
private string GetWmDescription(long wm) wm));
{ if (node != null)
XmlElement root = base.m_XmlDocument.DocumentElement; {
XmlNode node = root.SelectSingleNode(String.Format("WindowMessage[@value='{0}']", XmlAttribute text = node.Attributes["text"];
wm)); if (text == null)
if (node != null) throw new Exception("Node has no text attribute.");
{ return text.Value;
XmlAttribute text = node.Attributes["text"]; }
if (text == null) else
throw new Exception("Node has no text attribute."); return null;
return text.Value; }
}
else private string GetWmNumber(string wmName)
return null; {
} XmlElement root = base.m_XmlDocument.DocumentElement;
XmlNode node = root.SelectSingleNode(String.Format("WindowMessage[@text='{0}']",
private string GetWmNumber(string wmName) wmName));
{ if (node != null)
XmlElement root = base.m_XmlDocument.DocumentElement; {
XmlNode node = root.SelectSingleNode(String.Format("WindowMessage[@text='{0}']", XmlAttribute value = node.Attributes["value"];
wmName)); if (value == null)
if (node != null) throw new Exception("Node has no value attribute.");
{ return value.Value;
XmlAttribute value = node.Attributes["value"]; }
if (value == null) else
throw new Exception("Node has no value attribute."); return null;
return value.Value; }
} }
else }
return null;
}
}
}

View file

@ -0,0 +1,52 @@
using System;
using System.Reflection;
using System.Collections.Generic;
using System.Text;
namespace TechBot.Library
{
public class CommandBuilder
{
private Type m_CommandType;
private string m_CommandName;
private string m_CommandHelp;
private string m_CommandDesc;
public CommandBuilder(Type commandType)
{
m_CommandType = commandType;
CommandAttribute commandAttribute = (CommandAttribute)
Attribute.GetCustomAttribute(commandType, typeof(CommandAttribute));
m_CommandName = commandAttribute.Name;
m_CommandHelp = commandAttribute.Help;
m_CommandDesc = commandAttribute.Description;
}
public string Name
{
get { return m_CommandName; }
}
public string Help
{
get { return m_CommandHelp; }
}
public string Description
{
get { return m_CommandDesc; }
}
public Type Type
{
get { return m_CommandType; }
}
public Command CreateCommand()
{
return (Command)Type.Assembly.CreateInstance(Type.FullName, true);
}
}
}

View file

@ -0,0 +1,54 @@
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
namespace TechBot.Library
{
public class CommandFactory
{
private static CommandBuilderCollection m_Commands = new CommandBuilderCollection();
private CommandFactory()
{
}
public static void LoadPlugins()
{
//get the file names of the dll files in the current directory.
FileInfo objExeInfo = new FileInfo(@"C:\Ros\current\irc\TechBot\TechBot.Console\bin\Debug\");
foreach (FileInfo objInfo in objExeInfo.Directory.GetFiles("*.dll"))
{
LoadPluginsFromDLLFile(objInfo.FullName);
}
}
private static void LoadPluginsFromDLLFile(string sFile)
{
Assembly assPlugin = Assembly.LoadFile(sFile);
if (assPlugin != null)
{
foreach (Type pluginType in assPlugin.GetTypes())
{
if (pluginType.IsSubclassOf(typeof(Command)))
{
if (pluginType.IsAbstract == false)
{
//Add it to the list.
Commands.Add(new CommandBuilder(pluginType));
}
}
}
}
}
public static CommandBuilderCollection Commands
{
get { return m_Commands; }
}
}
}

View file

@ -1,37 +0,0 @@
using System;
using System.Collections;
namespace TechBot.Library
{
public class HelpCommand : Command
{
public HelpCommand(TechBotService techBot)
: base(techBot)
{
}
public override string[] AvailableCommands
{
get { return new string[] { "help" }; }
}
public override void Handle(
MessageContext context,
string commandName,
string parameters)
{
TechBot.ServiceOutput.WriteLine(context, "I support the following commands:");
foreach (Command command in TechBot.Commands)
{
TechBot.ServiceOutput.WriteLine(context,
command.Help());
}
}
public override string Help()
{
return "!help";
}
}
}

View file

@ -7,43 +7,33 @@ namespace TechBot.Library
{ {
} }
public class ChannelMessageContext : MessageContext public class ChannelMessageContext : MessageContext
{ {
private IrcChannel channel; private IrcChannel m_IrcChannel;
public IrcChannel Channel public IrcChannel Channel
{ {
get get { return m_IrcChannel; }
{ }
return channel;
}
}
public ChannelMessageContext(IrcChannel channel) public ChannelMessageContext(IrcChannel channel)
{ {
this.channel = channel; m_IrcChannel = channel;
} }
} }
public class UserMessageContext : MessageContext public class UserMessageContext : MessageContext
{ {
private IrcUser user; private IrcUser m_IrcUser;
public IrcUser User public IrcUser User
{ {
get get { return m_IrcUser; }
{ }
return user;
}
}
public UserMessageContext(IrcUser user) public UserMessageContext(IrcUser user)
{ {
this.user = user; m_IrcUser = user;
} }
} }
} }

View file

@ -0,0 +1,537 @@
using System;
using System.Text.RegularExpressions;
//Code taken from : http://www.codeproject.com/KB/recipes/commandlineparser.aspx
namespace TechBot.Library
{
/// <summary>Implementation of a command-line parsing class. Is capable of
/// having switches registered with it directly or can examine a registered
/// class for any properties with the appropriate attributes appended to
/// them.</summary>
public class ParametersParser
{
/// <summary>A simple internal class for passing back to the caller
/// some information about the switch. The internals/implementation
/// of this class has privillaged access to the contents of the
/// SwitchRecord class.</summary>
public class SwitchInfo
{
#region Private Variables
private object m_Switch = null;
#endregion
#region Public Properties
public string Name { get { return (m_Switch as SwitchRecord).Name; } }
public string Description { get { return (m_Switch as SwitchRecord).Description; } }
public string[] Aliases { get { return (m_Switch as SwitchRecord).Aliases; } }
public System.Type Type { get { return (m_Switch as SwitchRecord).Type; } }
public object Value { get { return (m_Switch as SwitchRecord).Value; } }
public object InternalValue { get { return (m_Switch as SwitchRecord).InternalValue; } }
public bool IsEnum { get { return (m_Switch as SwitchRecord).Type.IsEnum; } }
public string[] Enumerations { get { return (m_Switch as SwitchRecord).Enumerations; } }
#endregion
/// <summary>
/// Constructor for the SwitchInfo class. Note, in order to hide to the outside world
/// information not necessary to know, the constructor takes a System.Object (aka
/// object) as it's registering type. If the type isn't of the correct type, an exception
/// is thrown.
/// </summary>
/// <param name="rec">The SwitchRecord for which this class store information.</param>
/// <exception cref="ArgumentException">Thrown if the rec parameter is not of
/// the type SwitchRecord.</exception>
public SwitchInfo( object rec )
{
if ( rec is SwitchRecord )
m_Switch = rec;
else
throw new ArgumentException();
}
}
/// <summary>
/// The SwitchRecord is stored within the parser's collection of registered
/// switches. This class is private to the outside world.
/// </summary>
private class SwitchRecord
{
#region Private Variables
private string m_name = "";
private string m_description = "";
private object m_value = null;
private System.Type m_switchType = typeof(bool);
private System.Collections.ArrayList m_Aliases = null;
private string m_Pattern = "";
// The following advanced functions allow for callbacks to be
// made to manipulate the associated data type.
private System.Reflection.MethodInfo m_SetMethod = null;
private System.Reflection.MethodInfo m_GetMethod = null;
private object m_PropertyOwner = null;
#endregion
#region Private Utility Functions
private void Initialize( string name, string description )
{
m_name = name;
m_description = description;
BuildPattern();
}
private void BuildPattern()
{
string matchString = Name;
if ( Aliases != null && Aliases.Length > 0 )
foreach( string s in Aliases )
matchString += "|" + s;
string strPatternStart = @"(\s|^)(?<match>(-{1,2}|/)(";
string strPatternEnd; // To be defined below.
// The common suffix ensures that the switches are followed by
// a white-space OR the end of the string. This will stop
// switches such as /help matching /helpme
//
string strCommonSuffix = @"(?=(\s|$))";
if ( Type == typeof(bool) )
strPatternEnd = @")(?<value>(\+|-){0,1}))";
else if ( Type == typeof(string) )
strPatternEnd = @")(?::|\s+))((?:"")(?<value>.+)(?:"")|(?<value>\S+))";
else if ( Type == typeof(int) )
strPatternEnd = @")(?::|\s+))((?<value>(-|\+)[0-9]+)|(?<value>[0-9]+))";
else if ( Type.IsEnum )
{
string[] enumNames = Enumerations;
string e_str = enumNames[0];
for ( int e=1; e<enumNames.Length; e++ )
e_str += "|" + enumNames[e];
strPatternEnd = @")(?::|\s+))(?<value>" + e_str + @")";
}
else
throw new System.ArgumentException();
// Set the internal regular expression pattern.
m_Pattern = strPatternStart + matchString + strPatternEnd + strCommonSuffix;
}
#endregion
#region Public Properties
public object Value
{
get
{
if ( ReadValue != null )
return ReadValue;
else
return m_value;
}
}
public object InternalValue
{
get { return m_value; }
}
public string Name
{
get { return m_name; }
set { m_name = value; }
}
public string Description
{
get { return m_description; }
set { m_description = value; }
}
public System.Type Type
{
get { return m_switchType; }
}
public string[] Aliases
{
get { return (m_Aliases != null) ? (string[])m_Aliases.ToArray(typeof(string)): null; }
}
public string Pattern
{
get { return m_Pattern; }
}
public System.Reflection.MethodInfo SetMethod
{
set { m_SetMethod = value; }
}
public System.Reflection.MethodInfo GetMethod
{
set { m_GetMethod = value; }
}
public object PropertyOwner
{
set { m_PropertyOwner = value; }
}
public object ReadValue
{
get
{
object o = null;
if ( m_PropertyOwner != null && m_GetMethod != null )
o = m_GetMethod.Invoke( m_PropertyOwner, null );
return o;
}
}
public string[] Enumerations
{
get
{
if ( m_switchType.IsEnum )
return System.Enum.GetNames( m_switchType );
else
return null;
}
}
#endregion
#region Constructors
public SwitchRecord( string name, string description )
{
Initialize( name, description );
}
public SwitchRecord( string name, string description, System.Type type )
{
if ( type == typeof(bool) ||
type == typeof(string) ||
type == typeof(int) ||
type.IsEnum )
{
m_switchType = type;
Initialize( name, description );
}
else
throw new ArgumentException("Currently only Ints, Bool and Strings are supported");
}
#endregion
#region Public Methods
public void AddAlias( string alias )
{
if ( m_Aliases == null )
m_Aliases = new System.Collections.ArrayList();
m_Aliases.Add( alias );
BuildPattern();
}
public void Notify( object value )
{
if ( m_PropertyOwner != null && m_SetMethod != null )
{
object[] parameters = new object[1];
parameters[0] = value;
m_SetMethod.Invoke( m_PropertyOwner, parameters );
}
m_value = value;
}
#endregion
}
#region Private Variables
private string m_commandLine = "";
private string m_workingString = "";
private string m_applicationName = "";
private string[] m_splitParameters = null;
private System.Collections.ArrayList m_switches = null;
#endregion
#region Private Utility Functions
private void ExtractApplicationName()
{
Regex r = new Regex(@"^(?<commandLine>("".+""|(\S)+))(?<remainder>.+)",
RegexOptions.ExplicitCapture);
Match m = r.Match(m_commandLine);
if ( m != null && m.Groups["commandLine"] != null )
{
m_applicationName = m.Groups["commandLine"].Value;
m_workingString = m.Groups["remainder"].Value;
}
}
private void SplitParameters()
{
// Populate the split parameters array with the remaining parameters.
// Note that if quotes are used, the quotes are removed.
// e.g. one two three "four five six"
// 0 - one
// 1 - two
// 2 - three
// 3 - four five six
// (e.g. 3 is not in quotes).
Regex r = new Regex(@"((\s*(""(?<param>.+?)""|(?<param>\S+))))",
RegexOptions.ExplicitCapture);
MatchCollection m = r.Matches( m_workingString );
if ( m != null )
{
m_splitParameters = new string[ m.Count ];
for ( int i=0; i < m.Count; i++ )
m_splitParameters[i] = m[i].Groups["param"].Value;
}
}
private void HandleSwitches()
{
if ( m_switches != null )
{
foreach ( SwitchRecord s in m_switches )
{
Regex r = new Regex( s.Pattern,
RegexOptions.ExplicitCapture
| RegexOptions.IgnoreCase );
MatchCollection m = r.Matches( m_workingString );
if ( m != null )
{
for ( int i=0; i < m.Count; i++ )
{
string value = null;
if ( m[i].Groups != null && m[i].Groups["value"] != null )
value = m[i].Groups["value"].Value;
if ( s.Type == typeof(bool))
{
bool state = true;
// The value string may indicate what value we want.
if ( m[i].Groups != null && m[i].Groups["value"] != null )
{
switch ( value )
{
case "+": state = true;
break;
case "-": state = false;
break;
case "": if ( s.ReadValue != null )
state = !(bool)s.ReadValue;
break;
default: break;
}
}
s.Notify( state );
break;
}
else if ( s.Type == typeof(string) )
s.Notify( value );
else if ( s.Type == typeof(int) )
s.Notify( int.Parse( value ) );
else if ( s.Type.IsEnum )
s.Notify( System.Enum.Parse(s.Type,value,true) );
}
}
m_workingString = r.Replace(m_workingString, " ");
}
}
}
#endregion
#region Public Properties
public string ApplicationName
{
get { return m_applicationName; }
}
public string[] Parameters
{
get { return m_splitParameters; }
}
public SwitchInfo[] Switches
{
get
{
if ( m_switches == null )
return null;
else
{
SwitchInfo[] si = new SwitchInfo[ m_switches.Count ];
for ( int i=0; i<m_switches.Count; i++ )
si[i] = new SwitchInfo( m_switches[i] );
return si;
}
}
}
public object this[string name]
{
get
{
if ( m_switches != null )
for ( int i=0; i<m_switches.Count; i++ )
if ( string.Compare( (m_switches[i] as SwitchRecord).Name, name, true )==0 )
return (m_switches[i] as SwitchRecord).Value;
return null;
}
}
/// <summary>This function returns a list of the unhandled switches
/// that the parser has seen, but not processed.</summary>
/// <remark>The unhandled switches are not removed from the remainder
/// of the command-line.</remark>
public string[] UnhandledSwitches
{
get
{
string switchPattern = @"(\s|^)(?<match>(-{1,2}|/)(.+?))(?=(\s|$))";
Regex r = new Regex( switchPattern,
RegexOptions.ExplicitCapture
| RegexOptions.IgnoreCase );
MatchCollection m = r.Matches( m_workingString );
if ( m != null )
{
string[] unhandled = new string[ m.Count ];
for ( int i=0; i < m.Count; i++ )
unhandled[i] = m[i].Groups["match"].Value;
return unhandled;
}
else
return null;
}
}
#endregion
#region Public Methods
public void AddSwitch( string name, string description )
{
if ( m_switches == null )
m_switches = new System.Collections.ArrayList();
SwitchRecord rec = new SwitchRecord( name, description );
m_switches.Add( rec );
}
public void AddSwitch( string[] names, string description )
{
if ( m_switches == null )
m_switches = new System.Collections.ArrayList();
SwitchRecord rec = new SwitchRecord( names[0], description );
for ( int s=1; s<names.Length; s++ )
rec.AddAlias( names[s] );
m_switches.Add( rec );
}
public bool Parse()
{
ExtractApplicationName();
// Remove switches and associated info.
HandleSwitches();
// Split parameters.
SplitParameters();
return true;
}
public object InternalValue(string name)
{
if ( m_switches != null )
for ( int i=0; i<m_switches.Count; i++ )
if ( string.Compare( (m_switches[i] as SwitchRecord).Name, name, true )==0 )
return (m_switches[i] as SwitchRecord).InternalValue;
return null;
}
#endregion
#region Constructors
public ParametersParser( string commandLine )
{
m_commandLine = commandLine;
}
public ParametersParser( string commandLine,
object classForAutoAttributes )
{
m_commandLine = commandLine;
Type type = classForAutoAttributes.GetType();
System.Reflection.MemberInfo[] members = type.GetMembers();
for(int i=0; i<members.Length; i++)
{
object[] attributes = members[i].GetCustomAttributes(false);
if(attributes.Length > 0)
{
SwitchRecord rec = null;
foreach ( Attribute attribute in attributes )
{
if ( attribute is CommandParameterAttribute )
{
CommandParameterAttribute switchAttrib =
(CommandParameterAttribute) attribute;
// Get the property information. We're only handling
// properties at the moment!
if ( members[i] is System.Reflection.PropertyInfo )
{
System.Reflection.PropertyInfo pi = (System.Reflection.PropertyInfo) members[i];
rec = new SwitchRecord( switchAttrib.Name,
switchAttrib.Description,
pi.PropertyType );
// Map in the Get/Set methods.
rec.SetMethod = pi.GetSetMethod();
rec.GetMethod = pi.GetGetMethod();
rec.PropertyOwner = classForAutoAttributes;
// Can only handle a single switch for each property
// (otherwise the parsing of aliases gets silly...)
break;
}
}
}
// See if any aliases are required. We can only do this after
// a switch has been registered and the framework doesn't make
// any guarantees about the order of attributes, so we have to
// walk the collection a second time.
if ( rec != null )
{
foreach ( Attribute attribute in attributes )
{
if (attribute is CommandParameterAliasAttribute)
{
CommandParameterAliasAttribute aliasAttrib =
(CommandParameterAliasAttribute)attribute;
rec.AddAlias( aliasAttrib.Alias );
}
}
}
// Assuming we have a switch record (that may or may not have
// aliases), add it to the collection of switches.
if ( rec != null )
{
if ( m_switches == null )
m_switches = new System.Collections.ArrayList();
m_switches.Add( rec );
}
}
}
}
#endregion
}
}

View file

@ -1,29 +0,0 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace TechBot.Library
{
class SambaBugUrl : BugCommand
{
public SambaBugUrl(TechBotService techBot)
: base(techBot)
{
}
public override string[] AvailableCommands
{
get { return new string[] { "sambabug" }; }
}
protected override string BugUrl
{
get { return "https://bugzilla.samba.org/show_bug.cgi?id={0}"; }
}
public override string Help()
{
return "!sambabug <number>";
}
}
}

View file

@ -2,9 +2,8 @@ using System;
namespace TechBot.Library namespace TechBot.Library
{ {
public interface IServiceOutput public interface IServiceOutput
{ {
void WriteLine(MessageContext context, void WriteLine(MessageContext context, string message);
string message); }
}
} }

View file

@ -1,32 +0,0 @@
using System;
namespace TechBot.Library
{
public class SvnCommand : Command
{
private string m_SvnRoot;
public SvnCommand(TechBotService techBot)
: base(techBot)
{
m_SvnRoot = Settings.Default.SVNRoot;
}
public override string[] AvailableCommands
{
get { return new string[] { "svn" }; }
}
public override void Handle(MessageContext context,
string commandName,
string parameters)
{
TechBot.ServiceOutput.WriteLine(context, string.Format("svn co {0}" , m_SvnRoot));
}
public override string Help()
{
return "!svn";
}
}
}

View file

@ -39,21 +39,29 @@
</Target> </Target>
--> -->
<ItemGroup> <ItemGroup>
<Compile Include="ApiCommand.cs" /> <Compile Include="Attributes\CommandAttribute.cs" />
<Compile Include="BugCommand.cs" /> <Compile Include="Attributes\CommandParameterAliasAttribute.cs" />
<Compile Include="Attributes\CommandParameterAttribute.cs" />
<Compile Include="Collections\CommandBuilderCollection.cs" />
<Compile Include="Commands\Base\XmlLookupCommand.cs" />
<Compile Include="Factory\CommandBuilder.cs" />
<Compile Include="Factory\CommandFactory.cs" />
<Compile Include="Commands\Base\Command.cs" />
<Compile Include="Commands\Base\XmlCommand.cs" /> <Compile Include="Commands\Base\XmlCommand.cs" />
<Compile Include="SambaBugUrl.cs" /> <Compile Include="Commands\BugCommand.cs" />
<Compile Include="WineBugUrl.cs" /> <Compile Include="Commands\HelpCommand.cs" />
<Compile Include="ErrorCommand.cs" /> <Compile Include="Commands\HResultCommand.cs" />
<Compile Include="HelpCommand.cs" /> <Compile Include="Commands\NtStatusCommand.cs" />
<Compile Include="HresultCommand.cs" /> <Compile Include="Commands\ReactOSBugUrl.cs" />
<Compile Include="Command.cs" /> <Compile Include="Commands\SambaBugUrl.cs" />
<Compile Include="IrcService.cs" /> <Compile Include="Commands\SvnCommand.cs" />
<Compile Include="Commands\WineBugUrl.cs" />
<Compile Include="Commands\WinerrorCommand.cs" />
<Compile Include="Commands\WMCommand.cs" />
<Compile Include="MessageContext.cs" /> <Compile Include="MessageContext.cs" />
<Compile Include="NtStatusCommand.cs" />
<Compile Include="NumberParser.cs" /> <Compile Include="NumberParser.cs" />
<Compile Include="ParametersParser.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ReactOSBugUrl.cs" />
<Compile Include="ServiceOutput.cs" /> <Compile Include="ServiceOutput.cs" />
<Compile Include="Settings.cs" /> <Compile Include="Settings.cs" />
<Compile Include="Settings.Designer.cs"> <Compile Include="Settings.Designer.cs">
@ -61,10 +69,8 @@
<DesignTimeSharedInput>True</DesignTimeSharedInput> <DesignTimeSharedInput>True</DesignTimeSharedInput>
<DependentUpon>Settings.settings</DependentUpon> <DependentUpon>Settings.settings</DependentUpon>
</Compile> </Compile>
<Compile Include="SvnCommand.cs" /> <Compile Include="TechBotIrcService.cs" />
<Compile Include="TechBotService.cs" /> <Compile Include="TechBotService.cs" />
<Compile Include="WinerrorCommand.cs" />
<Compile Include="WmCommand.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Reference Include="System" /> <Reference Include="System" />

View file

@ -1,296 +1,267 @@
using System; using System;
using System.Text; using System.Text;
using System.Collections; using System.Collections;
using System.Threading; using System.Threading;
using TechBot.IRCLibrary; using TechBot.IRCLibrary;
namespace TechBot.Library namespace TechBot.Library
{ {
public class IrcService : IServiceOutput public class IrcServiceOutput : IServiceOutput
{ {
private string hostname; public void WriteLine(MessageContext context,
private int port; string message)
private string channelnames; {
private string botname; if (context is ChannelMessageContext)
private string password; {
private string chmPath; ChannelMessageContext channelContext = context as ChannelMessageContext;
private string mainChm; channelContext.Channel.Talk(message);
private string ntstatusXml; }
private string winerrorXml; else if (context is UserMessageContext)
private string hresultXml; {
private string wmXml; UserMessageContext userContext = context as UserMessageContext;
private string svnCommand; userContext.User.Talk(message);
private string bugUrl, WineBugUrl, SambaBugUrl; }
private IrcClient m_IrcClient; else
private ArrayList channels = new ArrayList(); /* IrcChannel */ {
private TechBotService service; throw new InvalidOperationException(String.Format("Unhandled message context '{0}'",
private bool isStopped = false; context.GetType()));
}
public IrcService(string hostname, }
int port, }
string channelnames,
string botname, public class IrcTechBotService : TechBotService
string password, {
string chmPath, private int port;
string mainChm, private string hostname;
//string ntstatusXml, private string channelnames;
//string winerrorXml, private string botname;
//string hresultXml, private string password;
//string wmXml, private string chmPath;
//string svnCommand, private string mainChm;
string BugUrl, private IrcClient m_IrcClient;
string WineBugUrl, private ArrayList channels = new ArrayList();
string SambaBugUrl) private bool isStopped = false;
{
this.hostname = hostname; public IrcTechBotService(string hostname,
this.port = port; int port,
this.channelnames = channelnames; string channelnames,
this.botname = botname; string botname,
if (password == null || password.Trim() == "") string password,
this.password = null; string chmPath,
else string mainChm)
this.password = password; : base (new IrcServiceOutput() , chmPath , mainChm)
this.chmPath = chmPath; {
this.mainChm = mainChm; this.hostname = hostname;
this.ntstatusXml = ntstatusXml; this.port = port;
this.winerrorXml = winerrorXml; this.channelnames = channelnames;
this.hresultXml = hresultXml; this.botname = botname;
this.wmXml = wmXml; if (password == null || password.Trim() == "")
this.svnCommand = svnCommand; this.password = null;
this.bugUrl = BugUrl; else
this.WineBugUrl = WineBugUrl; this.password = password;
this.SambaBugUrl = SambaBugUrl; this.chmPath = chmPath;
} this.mainChm = mainChm;
}
public void Run()
{ public override void Run()
service = new TechBotService(this, {
chmPath, //Call the base class
mainChm); base.Run();
//ntstatusXml,
//winerrorXml, m_IrcClient = new IrcClient();
//hresultXml, m_IrcClient.Encoding = Encoding.GetEncoding("iso-8859-1");
//wmXml, m_IrcClient.MessageReceived += new MessageReceivedHandler(client_MessageReceived);
//svnCommand, m_IrcClient.ChannelUserDatabaseChanged += new ChannelUserDatabaseChangedHandler(client_ChannelUserDatabaseChanged);
//bugUrl, Console.WriteLine("Connecting to {0} port {1}",
//WineBugUrl, hostname,
//SambaBugUrl); port);
service.Run(); m_IrcClient.Connect(hostname, port);
Console.WriteLine("Connected...");
m_IrcClient = new IrcClient(); m_IrcClient.Register(botname, password, null);
m_IrcClient.Encoding = Encoding.GetEncoding("iso-8859-1"); Console.WriteLine("Registered as {0}...", botname);
m_IrcClient.MessageReceived += new MessageReceivedHandler(client_MessageReceived); JoinChannels();
m_IrcClient.ChannelUserDatabaseChanged += new ChannelUserDatabaseChangedHandler(client_ChannelUserDatabaseChanged);
Console.WriteLine("Connecting to {0} port {1}", while (!isStopped)
hostname, {
port); Thread.Sleep(1000);
m_IrcClient.Connect(hostname, port); }
Console.WriteLine("Connected...");
m_IrcClient.Register(botname, password, null); PartChannels();
Console.WriteLine("Registered as {0}...", botname); m_IrcClient.Diconnect();
JoinChannels(); Console.WriteLine("Disconnected...");
}
while (!isStopped)
{ public void Stop()
Thread.Sleep(1000); {
} isStopped = true;
}
PartChannels();
m_IrcClient.Diconnect(); private void JoinChannels()
Console.WriteLine("Disconnected..."); {
} foreach (string channelname in channelnames.Split(new char[] { ';' }))
{
public void Stop() IrcChannel channel = m_IrcClient.JoinChannel(channelname);
{ channels.Add(channel);
isStopped = true; System.Console.WriteLine(String.Format("Joined channel #{0}...",
} channel.Name));
}
private void JoinChannels() }
{
foreach (string channelname in channelnames.Split(new char[] { ';' })) private void PartChannels()
{ {
IrcChannel channel = m_IrcClient.JoinChannel(channelname); foreach (IrcChannel channel in channels)
channels.Add(channel); {
System.Console.WriteLine(String.Format("Joined channel #{0}...", m_IrcClient.PartChannel(channel, "Caught in the bitstream...");
channel.Name)); System.Console.WriteLine(String.Format("Parted channel #{0}...",
} channel.Name));
} }
}
private void PartChannels()
{ private string GetMessageSource(MessageContext context)
foreach (IrcChannel channel in channels) {
{ if (context is ChannelMessageContext)
m_IrcClient.PartChannel(channel, "Caught in the bitstream..."); {
System.Console.WriteLine(String.Format("Parted channel #{0}...", ChannelMessageContext channelContext = context as ChannelMessageContext;
channel.Name)); return String.Format("#{0}",
} channelContext.Channel.Name);
} }
else if (context is UserMessageContext)
private string GetMessageSource(MessageContext context) {
{ UserMessageContext userContext = context as UserMessageContext;
if (context is ChannelMessageContext) return userContext.User.Nickname;
{ }
ChannelMessageContext channelContext = context as ChannelMessageContext; else
return String.Format("#{0}", {
channelContext.Channel.Name); throw new InvalidOperationException(String.Format("Unhandled message context '{0}'",
} context.GetType()));
else if (context is UserMessageContext) }
{ }
UserMessageContext userContext = context as UserMessageContext;
return userContext.User.Nickname; private void ExtractMessage(string parameters,
} out string message)
else {
{ int startIndex = parameters.IndexOf(':');
throw new InvalidOperationException(String.Format("Unhandled message context '{0}'", if (startIndex != -1)
context.GetType())); {
} message = parameters.Substring(startIndex + 1);
} }
else
public void WriteLine(MessageContext context, {
string message) message = parameters;
{ }
if (context is ChannelMessageContext) }
{
ChannelMessageContext channelContext = context as ChannelMessageContext; private bool GetChannelName(IrcMessage message,
channelContext.Channel.Talk(message); out string channelName)
} {
else if (context is UserMessageContext) if (message.Parameters == null || !message.Parameters.StartsWith("#"))
{ {
UserMessageContext userContext = context as UserMessageContext; channelName = null;
userContext.User.Talk(message); return false;
} }
else
{ int index = message.Parameters.IndexOf(' ');
throw new InvalidOperationException(String.Format("Unhandled message context '{0}'", if (index == -1)
context.GetType())); index = message.Parameters.Length;
} else
} index = index - 1;
channelName = message.Parameters.Substring(1, index);
private void ExtractMessage(string parameters, return true;
out string message) }
{
int startIndex = parameters.IndexOf(':'); private bool GetTargetNickname(IrcMessage message,
if (startIndex != -1) out string nickname)
{ {
message = parameters.Substring(startIndex + 1); if (message.Parameters == null)
} {
else nickname = null;
{ return false;
message = parameters; }
}
} int index = message.Parameters.IndexOf(' ');
if (index == -1)
private bool GetChannelName(IrcMessage message, index = message.Parameters.Length;
out string channelName) nickname = message.Parameters.Substring(0, index);
{ Console.WriteLine("nickname: " + nickname);
if (message.Parameters == null || !message.Parameters.StartsWith("#")) return true;
{ }
channelName = null;
return false; private bool ShouldAcceptMessage(IrcMessage message,
} out MessageContext context)
{
int index = message.Parameters.IndexOf(' '); if (message.Command.ToUpper().Equals("PRIVMSG"))
if (index == -1) {
index = message.Parameters.Length; string channelName;
else string nickname;
index = index - 1; if (GetChannelName(message,
channelName = message.Parameters.Substring(1, index); out channelName))
return true; {
} foreach (IrcChannel channel in channels)
{
private bool GetTargetNickname(IrcMessage message, if (String.Compare(channel.Name, channelName, true) == 0)
out string nickname) {
{ context = new ChannelMessageContext(channel);
if (message.Parameters == null) return true;
{ }
nickname = null; }
return false; }
} else if (GetTargetNickname(message,
out nickname))
int index = message.Parameters.IndexOf(' '); {
if (index == -1) IrcUser targetUser = new IrcUser(m_IrcClient,
index = message.Parameters.Length; nickname);
nickname = message.Parameters.Substring(0, index); if (String.Compare(targetUser.Nickname, botname, true) == 0)
Console.WriteLine("nickname: " + nickname); {
return true; IrcUser sourceUser = new IrcUser(m_IrcClient,
} message.PrefixNickname);
context = new UserMessageContext(sourceUser);
private bool ShouldAcceptMessage(IrcMessage message, return true;
out MessageContext context) }
{ }
if (message.Command.ToUpper().Equals("PRIVMSG")) }
{ context = null;
string channelName; return false;
string nickname; }
if (GetChannelName(message,
out channelName)) private void client_MessageReceived(IrcMessage message)
{ {
foreach (IrcChannel channel in channels) try
{ {
if (String.Compare(channel.Name, channelName, true) == 0) if (message.Command != null &&
{ message.Parameters != null)
context = new ChannelMessageContext(channel); {
return true; string injectMessage;
} ExtractMessage(message.Parameters,
} out injectMessage);
} MessageContext context;
else if (GetTargetNickname(message, if (ShouldAcceptMessage(message,
out nickname)) out context))
{ {
IrcUser targetUser = new IrcUser(m_IrcClient, Console.WriteLine(String.Format("Injecting: {0} from {1}",
nickname); injectMessage,
if (String.Compare(targetUser.Nickname, botname, true) == 0) GetMessageSource(context)));
{ InjectMessage(context,
IrcUser sourceUser = new IrcUser(m_IrcClient, injectMessage);
message.PrefixNickname); }
context = new UserMessageContext(sourceUser); else
return true; {
} Console.WriteLine("Received: " + message.Line);
} }
} }
context = null; else
return false; {
} Console.WriteLine("Received: " + message.Line);
}
private void client_MessageReceived(IrcMessage message) }
{ catch (Exception ex)
try {
{ Console.WriteLine(String.Format("Exception: {0}", ex));
if (message.Command != null && }
message.Parameters != null) }
{
string injectMessage; private void client_ChannelUserDatabaseChanged(IrcChannel channel)
ExtractMessage(message.Parameters, {
out injectMessage); }
MessageContext context; }
if (ShouldAcceptMessage(message, }
out context))
{
Console.WriteLine(String.Format("Injecting: {0} from {1}",
injectMessage,
GetMessageSource(context)));
service.InjectMessage(context,
injectMessage);
}
else
{
Console.WriteLine("Received: " + message.Line);
}
}
else
{
Console.WriteLine("Received: " + message.Line);
}
}
catch (Exception ex)
{
Console.WriteLine(String.Format("Exception: {0}", ex));
}
}
private void client_ChannelUserDatabaseChanged(IrcChannel channel)
{
}
}
}

View file

@ -4,63 +4,29 @@ using System.Collections.Generic;
using System.IO; using System.IO;
using System.Data; using System.Data;
using System.Threading; using System.Threading;
using TechBot.IRCLibrary; using TechBot.IRCLibrary;
namespace TechBot.Library namespace TechBot.Library
{ {
public class TechBotService public abstract class TechBotService
{ {
private IServiceOutput serviceOutput; protected IServiceOutput serviceOutput;
private string chmPath; private string chmPath;
private string mainChm; private string mainChm;
private string ntstatusXml;
private string winerrorXml;
private string hresultXml;
private string wmXml;
private string svnCommand;
private string bugUrl, WineBugUrl, SambaBugUrl;
private List<Command> commands = new List<Command>();
public TechBotService(IServiceOutput serviceOutput, public TechBotService(IServiceOutput serviceOutput,
string chmPath, string chmPath,
string mainChm) string mainChm)
//string ntstatusXml,
//string winerrorXml,
//string hresultXml,
//string wmXml,
//string svnCommand,
//string bugUrl,
//string WineBugUrl,
//string SambaBugUrl)
{ {
this.serviceOutput = serviceOutput; this.serviceOutput = serviceOutput;
this.chmPath = chmPath; this.chmPath = chmPath;
this.mainChm = mainChm; this.mainChm = mainChm;
this.ntstatusXml = ntstatusXml;
this.winerrorXml = winerrorXml;
this.hresultXml = hresultXml;
this.wmXml = wmXml;
this.svnCommand = svnCommand;
this.bugUrl = bugUrl;
this.WineBugUrl = WineBugUrl;
this.SambaBugUrl = SambaBugUrl;
} }
public void Run() public virtual void Run()
{ {
commands.Add(new HelpCommand(this)); CommandFactory.LoadPlugins();
/*commands.Add(new ApiCommand(serviceOutput,
chmPath,
mainChm));*/
commands.Add(new NtStatusCommand(this));
commands.Add(new WinerrorCommand(this));
commands.Add(new HResultCommand(this));
commands.Add(new ErrorCommand(this));
commands.Add(new WMCommand(this));
commands.Add(new SvnCommand(this));
commands.Add(new ReactOSBugUrl(this));
commands.Add(new SambaBugUrl(this));
commands.Add(new WineBugUrl(this));
} }
public IServiceOutput ServiceOutput public IServiceOutput ServiceOutput
@ -68,18 +34,16 @@ namespace TechBot.Library
get { return serviceOutput; } get { return serviceOutput; }
} }
public IList<Command> Commands public CommandBuilderCollection Commands
{ {
get { return commands; } get { return CommandFactory.Commands; }
}
public void InjectMessage(MessageContext context, string message)
{
ParseCommandMessage(context,
message);
} }
public void InjectMessage(MessageContext context,
string message)
{
if (message.StartsWith("!"))
ParseCommandMessage(context,
message);
}
private bool IsCommandMessage(string message) private bool IsCommandMessage(string message)
{ {
@ -104,19 +68,21 @@ namespace TechBot.Library
else else
commandName = message.Trim(); commandName = message.Trim();
foreach (Command command in commands) foreach (CommandBuilder command in Commands)
{ {
foreach (string cmd in command.AvailableCommands) if (command.Name == commandName)
{ {
if (cmd == commandName) //Create a new instance of the required command type
{ Command cmd = command.CreateCommand();
command.Handle(context,
commandName, cmd.TechBot = this;
commandParams); cmd.Context = context;
return; cmd.ParseParameters(message);
} cmd.ExecuteCommand();
return;
} }
} }
} }
} }
} }

View file

@ -1,29 +0,0 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace TechBot.Library
{
class WineBugUrl : BugCommand
{
public WineBugUrl(TechBotService techBot)
: base(techBot)
{
}
public override string[] AvailableCommands
{
get { return new string[] { "winebug" }; }
}
protected override string BugUrl
{
get { return "http://bugs.winehq.org/show_bug.cgi?id={0}"; }
}
public override string Help()
{
return "!winebug <number>";
}
}
}

View file

@ -1,36 +0,0 @@
<Combine fileversion="1.0" name="TechBot" description="">
<StartMode startupentry="TechBot" single="True">
<Execute entry="TechBot" type="None" />
<Execute entry="TechBot.Library" type="None" />
<Execute entry="CHMLibrary" type="None" />
<Execute entry="Compression" type="None" />
<Execute entry="TechBot.Console" type="None" />
<Execute entry="TechBot.IRCLibrary" type="None" />
</StartMode>
<Entries>
<Entry filename=".\TechBot\TechBot.prjx" />
<Entry filename=".\TechBot.Library\TechBot.Library.prjx" />
<Entry filename=".\CHMLibrary\CHMLibrary.prjx" />
<Entry filename=".\Compression\Compression.prjx" />
<Entry filename=".\TechBot.Console\TechBot.Console.prjx" />
<Entry filename=".\TechBot.IRCLibrary\TechBot.IRCLibrary.prjx" />
</Entries>
<Configurations active="Debug">
<Configuration name="Release">
<Entry name="TechBot" configurationname="Debug" build="False" />
<Entry name="TechBot.Library" configurationname="Debug" build="False" />
<Entry name="CHMLibrary" configurationname="Debug" build="False" />
<Entry name="Compression" configurationname="Debug" build="False" />
<Entry name="TechBot.Console" configurationname="Debug" build="False" />
<Entry name="TechBot.IRCLibrary" configurationname="Debug" build="False" />
</Configuration>
<Configuration name="Debug">
<Entry name="TechBot" configurationname="Debug" build="False" />
<Entry name="TechBot.Library" configurationname="Debug" build="False" />
<Entry name="CHMLibrary" configurationname="Debug" build="False" />
<Entry name="Compression" configurationname="Debug" build="False" />
<Entry name="TechBot.Console" configurationname="Debug" build="False" />
<Entry name="TechBot.IRCLibrary" configurationname="Debug" build="False" />
</Configuration>
</Configurations>
</Combine>

View file

@ -0,0 +1,28 @@
using System;
using System.ComponentModel;
using System.ServiceProcess;
using System.Configuration.Install;
using System.Collections.Generic;
using System.Text;
namespace TechBot
{
[RunInstaller(true)]
public class ProjectInstaller : Installer
{
public ProjectInstaller()
{
ServiceProcessInstaller spi = null;
ServiceInstaller si = null;
spi = new ServiceProcessInstaller();
spi.Account = ServiceAccount.LocalSystem;
si = new ServiceInstaller();
si.ServiceName = "TechBot";
si.StartType = ServiceStartMode.Automatic;
Installers.AddRange(new Installer[] { spi, si });
}
}
}

View file

@ -51,21 +51,21 @@ namespace TechBot
SetupConfiguration(); SetupConfiguration();
System.Console.WriteLine("TechBot irc service..."); System.Console.WriteLine("TechBot irc service...");
IrcService ircService = new IrcService(IRCServerHostName, IrcTechBotService ircService = new IrcTechBotService(IRCServerHostName,
IRCServerHostPort, IRCServerHostPort,
IRCChannelNames, IRCChannelNames,
IRCBotName, IRCBotName,
IRCBotPassword, IRCBotPassword,
ChmPath, ChmPath,
MainChm, MainChm);
//NtstatusXml, //NtstatusXml,
//WinerrorXml, //WinerrorXml,
//HresultXml, //HresultXml,
//WmXml, //WmXml,
//SvnCommand, //SvnCommand,
BugUrl, //BugUrl,
WineBugUrl, //WineBugUrl,
SambaBugUrl); //SambaBugUrl);
ircService.Run(); ircService.Run();
} }

View file

@ -42,6 +42,9 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="AssemblyInfo.cs" /> <Compile Include="AssemblyInfo.cs" />
<Compile Include="ProjectInstaller.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="ServiceThread.cs" /> <Compile Include="ServiceThread.cs" />
<Compile Include="TechBotService.cs"> <Compile Include="TechBotService.cs">
<SubType>Component</SubType> <SubType>Component</SubType>
@ -52,6 +55,7 @@
<Reference Include="System.Configuration.Install" /> <Reference Include="System.Configuration.Install" />
<Reference Include="System.Data" /> <Reference Include="System.Data" />
<Reference Include="System.ServiceProcess" /> <Reference Include="System.ServiceProcess" />
<Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\TechBot.IRCLibrary\TechBot.IRCLibrary.csproj"> <ProjectReference Include="..\TechBot.IRCLibrary\TechBot.IRCLibrary.csproj">

View file

@ -80,18 +80,3 @@ namespace TechBot
} }
} }
} }
[RunInstaller(true)]
public class ProjectInstaller : Installer
{
public ProjectInstaller()
{
ServiceProcessInstaller spi = new ServiceProcessInstaller();
spi.Account = ServiceAccount.LocalSystem;
ServiceInstaller si = new ServiceInstaller();
si.ServiceName = "TechBot";
si.StartType = ServiceStartMode.Automatic;
Installers.AddRange(new Installer[] {spi, si});
}
}