2005-01-15 19:27:25 +00:00
|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
using System.Data;
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
using HtmlHelp;
|
|
|
|
using HtmlHelp.ChmDecoding;
|
|
|
|
|
|
|
|
namespace TechBot.Library
|
|
|
|
{
|
|
|
|
public class ApiCommand : BaseCommand, ICommand
|
|
|
|
{
|
|
|
|
private const bool IsVerbose = false;
|
|
|
|
|
|
|
|
private HtmlHelpSystem chm;
|
|
|
|
private IServiceOutput serviceOutput;
|
|
|
|
private string chmPath;
|
|
|
|
private string mainChm;
|
|
|
|
|
|
|
|
public ApiCommand(IServiceOutput serviceOutput,
|
|
|
|
string chmPath,
|
|
|
|
string mainChm)
|
|
|
|
{
|
|
|
|
this.serviceOutput = serviceOutput;
|
|
|
|
this.chmPath = chmPath;
|
|
|
|
this.mainChm = mainChm;
|
|
|
|
Run();
|
|
|
|
}
|
|
|
|
|
2005-02-16 21:07:55 +00:00
|
|
|
private void WriteIfVerbose(MessageContext context,
|
|
|
|
string message)
|
2005-01-15 19:27:25 +00:00
|
|
|
{
|
|
|
|
if (IsVerbose)
|
2005-02-16 21:07:55 +00:00
|
|
|
serviceOutput.WriteLine(context,
|
|
|
|
message);
|
2005-01-15 19:27:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void Run()
|
|
|
|
{
|
|
|
|
string CHMFilename = Path.Combine(chmPath, 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(chmPath))
|
|
|
|
{
|
|
|
|
if (!Path.GetExtension(filename).ToLower().Equals(".chm"))
|
|
|
|
continue;
|
|
|
|
if (Path.GetFileName(filename).ToLower().Equals(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 bool CanHandle(string commandName)
|
|
|
|
{
|
|
|
|
return CanHandle(commandName,
|
|
|
|
new string[] { "api" });
|
|
|
|
}
|
|
|
|
|
2005-02-16 21:07:55 +00:00
|
|
|
public void Handle(MessageContext context,
|
|
|
|
string commandName,
|
2005-01-15 19:27:25 +00:00
|
|
|
string parameters)
|
|
|
|
{
|
|
|
|
if (parameters.Trim().Equals(String.Empty))
|
2005-02-16 21:07:55 +00:00
|
|
|
DisplayNoKeyword(context);
|
2005-01-15 19:27:25 +00:00
|
|
|
else
|
2005-02-16 21:07:55 +00:00
|
|
|
Search(context,
|
|
|
|
parameters);
|
2005-01-15 19:27:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public string Help()
|
|
|
|
{
|
|
|
|
return "!api <apiname>";
|
|
|
|
}
|
|
|
|
|
2005-02-16 21:07:55 +00:00
|
|
|
private bool SearchIndex(MessageContext context,
|
|
|
|
string keyword)
|
2005-01-15 19:27:25 +00:00
|
|
|
{
|
|
|
|
if (chm.HasIndex)
|
|
|
|
{
|
|
|
|
IndexItem item = chm.Index.SearchIndex(keyword,
|
|
|
|
IndexType.KeywordLinks);
|
|
|
|
if (item != null && item.Topics.Count > 0)
|
|
|
|
{
|
2005-02-16 21:07:55 +00:00
|
|
|
WriteIfVerbose(context,
|
|
|
|
String.Format("Keyword {0} found in index",
|
2005-01-15 19:27:25 +00:00
|
|
|
item.KeyWord));
|
|
|
|
IndexTopic indexTopic = item.Topics[0] as IndexTopic;
|
2005-02-16 21:07:55 +00:00
|
|
|
return DisplayResult(context,
|
|
|
|
keyword,
|
|
|
|
indexTopic);
|
2005-01-15 19:27:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-02-16 21:07:55 +00:00
|
|
|
WriteIfVerbose(context,
|
|
|
|
String.Format("Keyword {0} not found in index",
|
2005-01-15 19:27:25 +00:00
|
|
|
keyword));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-02-16 21:07:55 +00:00
|
|
|
private void SearchFullText(MessageContext context,
|
|
|
|
string keyword)
|
2005-01-15 19:27:25 +00:00
|
|
|
{
|
|
|
|
string sort = "Rating ASC";
|
2005-02-16 21:07:55 +00:00
|
|
|
WriteIfVerbose(context,
|
|
|
|
String.Format("Searching fulltext database for {0}",
|
2005-01-15 19:27:25 +00:00
|
|
|
keyword));
|
|
|
|
|
|
|
|
bool partialMatches = false;
|
|
|
|
bool titlesOnly = true;
|
|
|
|
int maxResults = 100;
|
|
|
|
DataTable results = chm.PerformSearch(keyword,
|
|
|
|
maxResults,
|
|
|
|
partialMatches,
|
|
|
|
titlesOnly);
|
2005-02-16 21:07:55 +00:00
|
|
|
WriteIfVerbose(context,
|
|
|
|
String.Format("results.Rows.Count = {0}",
|
2005-01-15 19:27:25 +00:00
|
|
|
results != null ?
|
|
|
|
results.Rows.Count.ToString() : "(none)"));
|
|
|
|
if (results != null && results.Rows.Count > 0)
|
|
|
|
{
|
|
|
|
results.DefaultView.Sort = sort;
|
2005-02-16 21:07:55 +00:00
|
|
|
if (!DisplayResult(context,
|
|
|
|
keyword,
|
|
|
|
results))
|
2005-01-15 19:27:25 +00:00
|
|
|
{
|
2005-02-16 21:07:55 +00:00
|
|
|
DisplayNoResult(context,
|
|
|
|
keyword);
|
2005-01-15 19:27:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-02-16 21:07:55 +00:00
|
|
|
DisplayNoResult(context,
|
|
|
|
keyword);
|
2005-01-15 19:27:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-02-16 21:07:55 +00:00
|
|
|
private void Search(MessageContext context,
|
|
|
|
string keyword)
|
2005-01-15 19:27:25 +00:00
|
|
|
{
|
2005-02-16 21:07:55 +00:00
|
|
|
if (!SearchIndex(context,
|
|
|
|
keyword))
|
|
|
|
SearchFullText(context,
|
|
|
|
keyword);
|
2005-01-15 19:27:25 +00:00
|
|
|
}
|
|
|
|
|
2005-02-16 21:07:55 +00:00
|
|
|
private bool DisplayResult(MessageContext context,
|
|
|
|
string keyword,
|
2005-01-15 19:27:25 +00:00
|
|
|
IndexTopic indexTopic)
|
|
|
|
{
|
|
|
|
keyword = keyword.Trim().ToLower();
|
|
|
|
string url = indexTopic.URL;
|
2005-02-16 21:07:55 +00:00
|
|
|
WriteIfVerbose(context,
|
|
|
|
String.Format("URL from index search {0}",
|
2005-01-15 19:27:25 +00:00
|
|
|
url));
|
2005-02-16 21:07:55 +00:00
|
|
|
string prototype = ExtractPrototype(context,
|
|
|
|
url);
|
2005-01-15 19:27:25 +00:00
|
|
|
if (prototype == null || prototype.Trim().Equals(String.Empty))
|
|
|
|
return false;
|
|
|
|
string formattedPrototype = FormatPrototype(prototype);
|
2005-02-16 21:07:55 +00:00
|
|
|
serviceOutput.WriteLine(context,
|
|
|
|
formattedPrototype);
|
2005-01-15 19:27:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-02-16 21:07:55 +00:00
|
|
|
private bool DisplayResult(MessageContext context,
|
|
|
|
string keyword,
|
2005-01-15 19:27:25 +00:00
|
|
|
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();
|
2005-02-16 21:07:55 +00:00
|
|
|
WriteIfVerbose(context,
|
|
|
|
String.Format("Examining {0}", title));
|
2005-01-15 19:27:25 +00:00
|
|
|
if (title.Trim().ToLower().Equals(keyword))
|
|
|
|
{
|
|
|
|
string location = row["Location"].ToString();
|
|
|
|
string rating = row["Rating"].ToString();
|
|
|
|
string url = row["Url"].ToString();
|
2005-02-16 21:07:55 +00:00
|
|
|
string prototype = ExtractPrototype(context,
|
|
|
|
url);
|
2005-01-15 19:27:25 +00:00
|
|
|
if (prototype == null || prototype.Trim().Equals(String.Empty))
|
|
|
|
continue;
|
|
|
|
string formattedPrototype = FormatPrototype(prototype);
|
2005-02-16 21:07:55 +00:00
|
|
|
serviceOutput.WriteLine(context,
|
|
|
|
formattedPrototype);
|
2005-01-15 19:27:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-02-16 21:07:55 +00:00
|
|
|
private void DisplayNoResult(MessageContext context,
|
|
|
|
string keyword)
|
2005-01-15 19:27:25 +00:00
|
|
|
{
|
2005-02-16 21:07:55 +00:00
|
|
|
serviceOutput.WriteLine(context,
|
|
|
|
String.Format("I don't know about keyword {0}",
|
|
|
|
keyword));
|
2005-01-15 19:27:25 +00:00
|
|
|
}
|
|
|
|
|
2005-02-16 21:07:55 +00:00
|
|
|
private void DisplayNoKeyword(MessageContext context)
|
2005-01-15 19:27:25 +00:00
|
|
|
{
|
2005-02-16 21:07:55 +00:00
|
|
|
serviceOutput.WriteLine(context,
|
|
|
|
"Please give me a keyword.");
|
2005-01-15 19:27:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2005-02-16 21:07:55 +00:00
|
|
|
private string ExtractPrototype(MessageContext context,
|
|
|
|
string url)
|
2005-01-15 19:27:25 +00:00
|
|
|
{
|
2005-02-16 21:07:55 +00:00
|
|
|
string page = GetPage(context,
|
|
|
|
url);
|
2005-01-15 19:27:25 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2005-02-16 21:07:55 +00:00
|
|
|
private string GetPage(MessageContext context,
|
|
|
|
string url)
|
2005-01-15 19:27:25 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
|
{
|
2005-02-16 21:07:55 +00:00
|
|
|
baseStream = GetBaseStreamFromCHMFileName(context,
|
|
|
|
CHMFileName);
|
2005-01-15 19:27:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((topicName == "") || (CHMFileName == "") || (baseStream == null))
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
return baseStream.ExtractTextFile(topicName);
|
|
|
|
}
|
|
|
|
|
2005-02-16 21:07:55 +00:00
|
|
|
private CHMStream.CHMStream GetBaseStreamFromCHMFileName(MessageContext context,
|
|
|
|
string CHMFileName)
|
2005-01-15 19:27:25 +00:00
|
|
|
{
|
|
|
|
foreach (CHMFile file in chm.FileList)
|
|
|
|
{
|
2005-02-16 21:07:55 +00:00
|
|
|
WriteIfVerbose(context,
|
|
|
|
String.Format("Compare: {0} <> {1}",
|
2005-01-15 19:27:25 +00:00
|
|
|
file.ChmFilePath,
|
|
|
|
CHMFileName));
|
|
|
|
if (file.ChmFilePath.ToLower().Equals(CHMFileName.ToLower()))
|
|
|
|
{
|
|
|
|
return file.BaseStream;
|
|
|
|
}
|
|
|
|
}
|
2005-02-16 21:07:55 +00:00
|
|
|
WriteIfVerbose(context,
|
|
|
|
String.Format("Could not find loaded CHM file in list: {0}",
|
2005-01-15 19:27:25 +00:00
|
|
|
CHMFileName));
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|