- Moved commands outside TechBot.Library to TechBot.Commands.Common and TechBot.Commands.MSDN except for Command base classes

- Made TechBot more configurable through .config files
- Code refactoring
- Removed automatic parameter parsing support to make everyone happy

svn path=/trunk/; revision=33586
This commit is contained in:
Marc Piulachs 2008-05-18 15:54:43 +00:00
parent 6d51a10a1b
commit ac77d9d3a6
47 changed files with 1783 additions and 489 deletions

View file

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

View file

@ -0,0 +1,35 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("TechBot.Commands.MSDN")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Sand")]
[assembly: AssemblyProduct("TechBot.Commands.MSDN")]
[assembly: AssemblyCopyright("Copyright © Sand 2008")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("8a6332ce-82e3-4fbd-a799-8f4b8d025955")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View file

@ -0,0 +1,50 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:2.0.50727.1433
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace TechBot.Commands.MSDN {
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "8.0.0.0")]
internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
public static Settings Default {
get {
return defaultInstance;
}
}
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("C:\\IRC\\TechBot\\CHM")]
public string ChmPath {
get {
return ((string)(this["ChmPath"]));
}
set {
this["ChmPath"] = value;
}
}
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("kmarch.chm")]
public string MainChm {
get {
return ((string)(this["MainChm"]));
}
set {
this["MainChm"] = value;
}
}
}
}

View file

@ -0,0 +1,12 @@
<?xml version='1.0' encoding='utf-8'?>
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="TechBot.Commands.MSDN" GeneratedClassName="Settings">
<Profiles />
<Settings>
<Setting Name="ChmPath" Type="System.String" Scope="User">
<Value Profile="(Default)">C:\IRC\TechBot\CHM</Value>
</Setting>
<Setting Name="MainChm" Type="System.String" Scope="User">
<Value Profile="(Default)">kmarch.chm</Value>
</Setting>
</Settings>
</SettingsFile>

View file

@ -0,0 +1,69 @@
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.50727</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{ADBF1ED6-A586-4707-BD59-4CD53448D0FE}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>TechBot.Commands.MSDN</RootNamespace>
<AssemblyName>TechBot.Commands.MSDN</AssemblyName>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="ApiCommand.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Settings.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTimeSharedInput>True</DesignTimeSharedInput>
<DependentUpon>Settings.settings</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CHMLibrary\CHMLibrary.csproj">
<Project>{72E5CCA1-6318-4D62-964D-CB23A5C743B5}</Project>
<Name>CHMLibrary</Name>
</ProjectReference>
<ProjectReference Include="..\TechBot.Library\TechBot.Library.csproj">
<Project>{1114F34D-F388-4F38-AE27-C0EE1B10B777}</Project>
<Name>TechBot.Library</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<None Include="Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
</None>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

View file

@ -0,0 +1,5 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ProjectView>ProjectFiles</ProjectView>
</PropertyGroup>
</Project>

View file

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="TechBot.Commands.MSDN.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<userSettings>
<TechBot.Commands.MSDN.Settings>
<setting name="ChmPath" serializeAs="String">
<value>C:\IRC\TechBot\CHM</value>
</setting>
<setting name="MainChm" serializeAs="String">
<value>kmarch.chm</value>
</setting>
</TechBot.Commands.MSDN.Settings>
</userSettings>
</configuration>