mirror of
https://github.com/reactos/reactos.git
synced 2024-12-27 01:24:38 +00:00
Support multiple channels
svn path=/trunk/; revision=13604
This commit is contained in:
parent
6536947239
commit
70350430a5
15 changed files with 251 additions and 109 deletions
|
@ -3,7 +3,7 @@
|
||||||
<appSettings>
|
<appSettings>
|
||||||
<add key="IRCServerHostName" value="irc.eu.freenode.net" />
|
<add key="IRCServerHostName" value="irc.eu.freenode.net" />
|
||||||
<add key="IRCServerHostPort" value="6667" />
|
<add key="IRCServerHostPort" value="6667" />
|
||||||
<add key="IRCChannelName" value="channel" />
|
<add key="IRCChannelNames" value="channel1;channel2" />
|
||||||
<add key="IRCBotName" value="MyBot" />
|
<add key="IRCBotName" value="MyBot" />
|
||||||
<add key="ChmPath" value="C:\IRC\TechBot\CHM" />
|
<add key="ChmPath" value="C:\IRC\TechBot\CHM" />
|
||||||
<add key="MainChm" value="kmarch.chm" />
|
<add key="MainChm" value="kmarch.chm" />
|
||||||
|
|
|
@ -6,7 +6,8 @@ namespace TechBot.Console
|
||||||
{
|
{
|
||||||
public class ConsoleServiceOutput : IServiceOutput
|
public class ConsoleServiceOutput : IServiceOutput
|
||||||
{
|
{
|
||||||
public void WriteLine(string message)
|
public void WriteLine(MessageContext context,
|
||||||
|
string message)
|
||||||
{
|
{
|
||||||
System.Console.WriteLine(message);
|
System.Console.WriteLine(message);
|
||||||
}
|
}
|
||||||
|
@ -49,11 +50,11 @@ namespace TechBot.Console
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string IRCChannelName
|
private static string IRCChannelNames
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
string optionName = "IRCChannelName";
|
string optionName = "IRCChannelNames";
|
||||||
string s = ConfigurationSettings.AppSettings[optionName];
|
string s = ConfigurationSettings.AppSettings[optionName];
|
||||||
VerifyRequiredOption(optionName,
|
VerifyRequiredOption(optionName,
|
||||||
s);
|
s);
|
||||||
|
@ -149,7 +150,7 @@ namespace TechBot.Console
|
||||||
{
|
{
|
||||||
IrcService ircService = new IrcService(IRCServerHostName,
|
IrcService ircService = new IrcService(IRCServerHostName,
|
||||||
IRCServerHostPort,
|
IRCServerHostPort,
|
||||||
IRCChannelName,
|
IRCChannelNames,
|
||||||
IRCBotName,
|
IRCBotName,
|
||||||
ChmPath,
|
ChmPath,
|
||||||
MainChm,
|
MainChm,
|
||||||
|
@ -180,7 +181,8 @@ namespace TechBot.Console
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
string s = System.Console.ReadLine();
|
string s = System.Console.ReadLine();
|
||||||
service.InjectMessage(s);
|
service.InjectMessage(null,
|
||||||
|
s);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,10 +26,12 @@ namespace TechBot.Library
|
||||||
Run();
|
Run();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void WriteIfVerbose(string message)
|
private void WriteIfVerbose(MessageContext context,
|
||||||
|
string message)
|
||||||
{
|
{
|
||||||
if (IsVerbose)
|
if (IsVerbose)
|
||||||
serviceOutput.WriteLine(message);
|
serviceOutput.WriteLine(context,
|
||||||
|
message);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Run()
|
private void Run()
|
||||||
|
@ -70,13 +72,15 @@ namespace TechBot.Library
|
||||||
new string[] { "api" });
|
new string[] { "api" });
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Handle(string commandName,
|
public void Handle(MessageContext context,
|
||||||
|
string commandName,
|
||||||
string parameters)
|
string parameters)
|
||||||
{
|
{
|
||||||
if (parameters.Trim().Equals(String.Empty))
|
if (parameters.Trim().Equals(String.Empty))
|
||||||
DisplayNoKeyword();
|
DisplayNoKeyword(context);
|
||||||
else
|
else
|
||||||
Search(parameters);
|
Search(context,
|
||||||
|
parameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
public string Help()
|
public string Help()
|
||||||
|
@ -84,7 +88,8 @@ namespace TechBot.Library
|
||||||
return "!api <apiname>";
|
return "!api <apiname>";
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool SearchIndex(string keyword)
|
private bool SearchIndex(MessageContext context,
|
||||||
|
string keyword)
|
||||||
{
|
{
|
||||||
if (chm.HasIndex)
|
if (chm.HasIndex)
|
||||||
{
|
{
|
||||||
|
@ -92,14 +97,18 @@ namespace TechBot.Library
|
||||||
IndexType.KeywordLinks);
|
IndexType.KeywordLinks);
|
||||||
if (item != null && item.Topics.Count > 0)
|
if (item != null && item.Topics.Count > 0)
|
||||||
{
|
{
|
||||||
WriteIfVerbose(String.Format("Keyword {0} found in index",
|
WriteIfVerbose(context,
|
||||||
|
String.Format("Keyword {0} found in index",
|
||||||
item.KeyWord));
|
item.KeyWord));
|
||||||
IndexTopic indexTopic = item.Topics[0] as IndexTopic;
|
IndexTopic indexTopic = item.Topics[0] as IndexTopic;
|
||||||
return DisplayResult(keyword, indexTopic);
|
return DisplayResult(context,
|
||||||
|
keyword,
|
||||||
|
indexTopic);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
WriteIfVerbose(String.Format("Keyword {0} not found in index",
|
WriteIfVerbose(context,
|
||||||
|
String.Format("Keyword {0} not found in index",
|
||||||
keyword));
|
keyword));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -108,14 +117,12 @@ namespace TechBot.Library
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void SearchFullText(string keyword)
|
private void SearchFullText(MessageContext context,
|
||||||
|
string keyword)
|
||||||
{
|
{
|
||||||
string sort = "Rating ASC";
|
string sort = "Rating ASC";
|
||||||
/*
|
WriteIfVerbose(context,
|
||||||
sort = "Location ASC");
|
String.Format("Searching fulltext database for {0}",
|
||||||
sort = "Title ASC");
|
|
||||||
*/
|
|
||||||
WriteIfVerbose(String.Format("Searching fulltext database for {0}",
|
|
||||||
keyword));
|
keyword));
|
||||||
|
|
||||||
bool partialMatches = false;
|
bool partialMatches = false;
|
||||||
|
@ -125,47 +132,58 @@ namespace TechBot.Library
|
||||||
maxResults,
|
maxResults,
|
||||||
partialMatches,
|
partialMatches,
|
||||||
titlesOnly);
|
titlesOnly);
|
||||||
WriteIfVerbose(String.Format("results.Rows.Count = {0}",
|
WriteIfVerbose(context,
|
||||||
|
String.Format("results.Rows.Count = {0}",
|
||||||
results != null ?
|
results != null ?
|
||||||
results.Rows.Count.ToString() : "(none)"));
|
results.Rows.Count.ToString() : "(none)"));
|
||||||
if (results != null && results.Rows.Count > 0)
|
if (results != null && results.Rows.Count > 0)
|
||||||
{
|
{
|
||||||
results.DefaultView.Sort = sort;
|
results.DefaultView.Sort = sort;
|
||||||
if (!DisplayResult(keyword, results))
|
if (!DisplayResult(context,
|
||||||
|
keyword,
|
||||||
|
results))
|
||||||
{
|
{
|
||||||
DisplayNoResult(keyword);
|
DisplayNoResult(context,
|
||||||
|
keyword);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
DisplayNoResult(keyword);
|
DisplayNoResult(context,
|
||||||
|
keyword);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Search(string keyword)
|
private void Search(MessageContext context,
|
||||||
|
string keyword)
|
||||||
{
|
{
|
||||||
if (!SearchIndex(keyword))
|
if (!SearchIndex(context,
|
||||||
{
|
keyword))
|
||||||
SearchFullText(keyword);
|
SearchFullText(context,
|
||||||
}
|
keyword);
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool DisplayResult(string keyword,
|
private bool DisplayResult(MessageContext context,
|
||||||
|
string keyword,
|
||||||
IndexTopic indexTopic)
|
IndexTopic indexTopic)
|
||||||
{
|
{
|
||||||
keyword = keyword.Trim().ToLower();
|
keyword = keyword.Trim().ToLower();
|
||||||
string url = indexTopic.URL;
|
string url = indexTopic.URL;
|
||||||
WriteIfVerbose(String.Format("URL from index search {0}",
|
WriteIfVerbose(context,
|
||||||
|
String.Format("URL from index search {0}",
|
||||||
url));
|
url));
|
||||||
string prototype = ExtractPrototype(url);
|
string prototype = ExtractPrototype(context,
|
||||||
|
url);
|
||||||
if (prototype == null || prototype.Trim().Equals(String.Empty))
|
if (prototype == null || prototype.Trim().Equals(String.Empty))
|
||||||
return false;
|
return false;
|
||||||
string formattedPrototype = FormatPrototype(prototype);
|
string formattedPrototype = FormatPrototype(prototype);
|
||||||
serviceOutput.WriteLine(formattedPrototype);
|
serviceOutput.WriteLine(context,
|
||||||
|
formattedPrototype);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool DisplayResult(string keyword,
|
private bool DisplayResult(MessageContext context,
|
||||||
|
string keyword,
|
||||||
DataTable results)
|
DataTable results)
|
||||||
{
|
{
|
||||||
keyword = keyword.Trim().ToLower();
|
keyword = keyword.Trim().ToLower();
|
||||||
|
@ -173,31 +191,38 @@ namespace TechBot.Library
|
||||||
{
|
{
|
||||||
DataRowView row = results.DefaultView[i];
|
DataRowView row = results.DefaultView[i];
|
||||||
string title = row["Title"].ToString();
|
string title = row["Title"].ToString();
|
||||||
WriteIfVerbose(String.Format("Examining {0}", title));
|
WriteIfVerbose(context,
|
||||||
|
String.Format("Examining {0}", title));
|
||||||
if (title.Trim().ToLower().Equals(keyword))
|
if (title.Trim().ToLower().Equals(keyword))
|
||||||
{
|
{
|
||||||
string location = row["Location"].ToString();
|
string location = row["Location"].ToString();
|
||||||
string rating = row["Rating"].ToString();
|
string rating = row["Rating"].ToString();
|
||||||
string url = row["Url"].ToString();
|
string url = row["Url"].ToString();
|
||||||
string prototype = ExtractPrototype(url);
|
string prototype = ExtractPrototype(context,
|
||||||
|
url);
|
||||||
if (prototype == null || prototype.Trim().Equals(String.Empty))
|
if (prototype == null || prototype.Trim().Equals(String.Empty))
|
||||||
continue;
|
continue;
|
||||||
string formattedPrototype = FormatPrototype(prototype);
|
string formattedPrototype = FormatPrototype(prototype);
|
||||||
serviceOutput.WriteLine(formattedPrototype);
|
serviceOutput.WriteLine(context,
|
||||||
|
formattedPrototype);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void DisplayNoResult(string keyword)
|
private void DisplayNoResult(MessageContext context,
|
||||||
|
string keyword)
|
||||||
{
|
{
|
||||||
serviceOutput.WriteLine(String.Format("I don't know about keyword {0}", keyword));
|
serviceOutput.WriteLine(context,
|
||||||
|
String.Format("I don't know about keyword {0}",
|
||||||
|
keyword));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void DisplayNoKeyword()
|
private void DisplayNoKeyword(MessageContext context)
|
||||||
{
|
{
|
||||||
serviceOutput.WriteLine("Please give me a keyword.");
|
serviceOutput.WriteLine(context,
|
||||||
|
"Please give me a keyword.");
|
||||||
}
|
}
|
||||||
|
|
||||||
private string ReplaceComments(string s)
|
private string ReplaceComments(string s)
|
||||||
|
@ -241,9 +266,11 @@ namespace TechBot.Library
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
private string ExtractPrototype(string url)
|
private string ExtractPrototype(MessageContext context,
|
||||||
|
string url)
|
||||||
{
|
{
|
||||||
string page = GetPage(url);
|
string page = GetPage(context,
|
||||||
|
url);
|
||||||
Match match = Regex.Match(page,
|
Match match = Regex.Match(page,
|
||||||
"<PRE class=\"?syntax\"?>(.+)</PRE>",
|
"<PRE class=\"?syntax\"?>(.+)</PRE>",
|
||||||
RegexOptions.Multiline |
|
RegexOptions.Multiline |
|
||||||
|
@ -273,7 +300,8 @@ namespace TechBot.Library
|
||||||
return Regex.Replace(html, @"<(.|\n)*?>", String.Empty);
|
return Regex.Replace(html, @"<(.|\n)*?>", String.Empty);
|
||||||
}
|
}
|
||||||
|
|
||||||
private string GetPage(string url)
|
private string GetPage(MessageContext context,
|
||||||
|
string url)
|
||||||
{
|
{
|
||||||
string CHMFileName = "";
|
string CHMFileName = "";
|
||||||
string topicName = "";
|
string topicName = "";
|
||||||
|
@ -288,7 +316,8 @@ namespace TechBot.Library
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
baseStream = GetBaseStreamFromCHMFileName(CHMFileName);
|
baseStream = GetBaseStreamFromCHMFileName(context,
|
||||||
|
CHMFileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((topicName == "") || (CHMFileName == "") || (baseStream == null))
|
if ((topicName == "") || (CHMFileName == "") || (baseStream == null))
|
||||||
|
@ -299,11 +328,13 @@ namespace TechBot.Library
|
||||||
return baseStream.ExtractTextFile(topicName);
|
return baseStream.ExtractTextFile(topicName);
|
||||||
}
|
}
|
||||||
|
|
||||||
private CHMStream.CHMStream GetBaseStreamFromCHMFileName(string CHMFileName)
|
private CHMStream.CHMStream GetBaseStreamFromCHMFileName(MessageContext context,
|
||||||
|
string CHMFileName)
|
||||||
{
|
{
|
||||||
foreach (CHMFile file in chm.FileList)
|
foreach (CHMFile file in chm.FileList)
|
||||||
{
|
{
|
||||||
WriteIfVerbose(String.Format("Compare: {0} <> {1}",
|
WriteIfVerbose(context,
|
||||||
|
String.Format("Compare: {0} <> {1}",
|
||||||
file.ChmFilePath,
|
file.ChmFilePath,
|
||||||
CHMFileName));
|
CHMFileName));
|
||||||
if (file.ChmFilePath.ToLower().Equals(CHMFileName.ToLower()))
|
if (file.ChmFilePath.ToLower().Equals(CHMFileName.ToLower()))
|
||||||
|
@ -311,7 +342,8 @@ namespace TechBot.Library
|
||||||
return file.BaseStream;
|
return file.BaseStream;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
WriteIfVerbose(String.Format("Could not find loaded CHM file in list: {0}",
|
WriteIfVerbose(context,
|
||||||
|
String.Format("Could not find loaded CHM file in list: {0}",
|
||||||
CHMFileName));
|
CHMFileName));
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,12 +21,17 @@ namespace TechBot.Library
|
||||||
new string[] { "help" });
|
new string[] { "help" });
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Handle(string commandName,
|
public void Handle(MessageContext context,
|
||||||
|
string commandName,
|
||||||
string parameters)
|
string parameters)
|
||||||
{
|
{
|
||||||
serviceOutput.WriteLine("I support the following commands:");
|
serviceOutput.WriteLine(context,
|
||||||
|
"I support the following commands:");
|
||||||
foreach (ICommand command in commands)
|
foreach (ICommand command in commands)
|
||||||
serviceOutput.WriteLine(command.Help());
|
{
|
||||||
|
serviceOutput.WriteLine(context,
|
||||||
|
command.Help());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public string Help()
|
public string Help()
|
||||||
|
|
|
@ -24,13 +24,15 @@ namespace TechBot.Library
|
||||||
new string[] { "hresult" });
|
new string[] { "hresult" });
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Handle(string commandName,
|
public void Handle(MessageContext context,
|
||||||
|
string commandName,
|
||||||
string parameters)
|
string parameters)
|
||||||
{
|
{
|
||||||
string hresultText = parameters;
|
string hresultText = parameters;
|
||||||
if (hresultText.Equals(String.Empty))
|
if (hresultText.Equals(String.Empty))
|
||||||
{
|
{
|
||||||
serviceOutput.WriteLine("Please provide a valid HRESULT value.");
|
serviceOutput.WriteLine(context,
|
||||||
|
"Please provide a valid HRESULT value.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -38,7 +40,8 @@ namespace TechBot.Library
|
||||||
long hresult = np.Parse(hresultText);
|
long hresult = np.Parse(hresultText);
|
||||||
if (np.Error)
|
if (np.Error)
|
||||||
{
|
{
|
||||||
serviceOutput.WriteLine(String.Format("{0} is not a valid HRESULT value.",
|
serviceOutput.WriteLine(context,
|
||||||
|
String.Format("{0} is not a valid HRESULT value.",
|
||||||
hresultText));
|
hresultText));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -46,13 +49,15 @@ namespace TechBot.Library
|
||||||
string description = GetHresultDescription(hresult);
|
string description = GetHresultDescription(hresult);
|
||||||
if (description != null)
|
if (description != null)
|
||||||
{
|
{
|
||||||
serviceOutput.WriteLine(String.Format("{0} is {1}.",
|
serviceOutput.WriteLine(context,
|
||||||
|
String.Format("{0} is {1}.",
|
||||||
hresultText,
|
hresultText,
|
||||||
description));
|
description));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
serviceOutput.WriteLine(String.Format("I don't know about HRESULT {0}.",
|
serviceOutput.WriteLine(context,
|
||||||
|
String.Format("I don't know about HRESULT {0}.",
|
||||||
hresultText));
|
hresultText));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,8 @@ namespace TechBot.Library
|
||||||
public interface ICommand
|
public interface ICommand
|
||||||
{
|
{
|
||||||
bool CanHandle(string commandName);
|
bool CanHandle(string commandName);
|
||||||
void Handle(string commandName,
|
void Handle(MessageContext context,
|
||||||
|
string commandName,
|
||||||
string parameters);
|
string parameters);
|
||||||
string Help();
|
string Help();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using TechBot.IRCLibrary;
|
using TechBot.IRCLibrary;
|
||||||
|
|
||||||
|
@ -8,7 +9,7 @@ namespace TechBot.Library
|
||||||
{
|
{
|
||||||
private string hostname;
|
private string hostname;
|
||||||
private int port;
|
private int port;
|
||||||
private string channelname;
|
private string channelnames;
|
||||||
private string botname;
|
private string botname;
|
||||||
private string chmPath;
|
private string chmPath;
|
||||||
private string mainChm;
|
private string mainChm;
|
||||||
|
@ -17,13 +18,13 @@ namespace TechBot.Library
|
||||||
private string hresultXml;
|
private string hresultXml;
|
||||||
private string svnCommand;
|
private string svnCommand;
|
||||||
private IrcClient client;
|
private IrcClient client;
|
||||||
private IrcChannel channel1;
|
private ArrayList channels = new ArrayList(); /* IrcChannel */
|
||||||
private TechBotService service;
|
private TechBotService service;
|
||||||
private bool isStopped = false;
|
private bool isStopped = false;
|
||||||
|
|
||||||
public IrcService(string hostname,
|
public IrcService(string hostname,
|
||||||
int port,
|
int port,
|
||||||
string channelname,
|
string channelnames,
|
||||||
string botname,
|
string botname,
|
||||||
string chmPath,
|
string chmPath,
|
||||||
string mainChm,
|
string mainChm,
|
||||||
|
@ -34,7 +35,7 @@ namespace TechBot.Library
|
||||||
{
|
{
|
||||||
this.hostname = hostname;
|
this.hostname = hostname;
|
||||||
this.port = port;
|
this.port = port;
|
||||||
this.channelname = channelname;
|
this.channelnames = channelnames;
|
||||||
this.botname = botname;
|
this.botname = botname;
|
||||||
this.chmPath = chmPath;
|
this.chmPath = chmPath;
|
||||||
this.mainChm = mainChm;
|
this.mainChm = mainChm;
|
||||||
|
@ -65,15 +66,14 @@ namespace TechBot.Library
|
||||||
System.Console.WriteLine("Connected...");
|
System.Console.WriteLine("Connected...");
|
||||||
client.Register(botname, null);
|
client.Register(botname, null);
|
||||||
System.Console.WriteLine(String.Format("Registered as {0}...", botname));
|
System.Console.WriteLine(String.Format("Registered as {0}...", botname));
|
||||||
channel1 = client.JoinChannel(channelname);
|
JoinChannels();
|
||||||
System.Console.WriteLine(String.Format("Joined channel {0}...", channelname));
|
|
||||||
|
|
||||||
while (!isStopped)
|
while (!isStopped)
|
||||||
{
|
{
|
||||||
Thread.Sleep(1000);
|
Thread.Sleep(1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
client.PartChannel(channel1, "Caught in the bitstream...");
|
PartChannels();
|
||||||
client.Diconnect();
|
client.Diconnect();
|
||||||
System.Console.WriteLine("Disconnected...");
|
System.Console.WriteLine("Disconnected...");
|
||||||
}
|
}
|
||||||
|
@ -83,10 +83,34 @@ namespace TechBot.Library
|
||||||
isStopped = true;
|
isStopped = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void WriteLine(string message)
|
private void JoinChannels()
|
||||||
{
|
{
|
||||||
Console.WriteLine(String.Format("Sending: {0}", message));
|
foreach (string channelname in channelnames.Split(new char[] { ';' }))
|
||||||
channel1.Talk(message);
|
{
|
||||||
|
IrcChannel channel = client.JoinChannel(channelname);
|
||||||
|
channels.Add(channel);
|
||||||
|
System.Console.WriteLine(String.Format("Joined channel #{0}...",
|
||||||
|
channel.Name));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void PartChannels()
|
||||||
|
{
|
||||||
|
foreach (IrcChannel channel in channels)
|
||||||
|
{
|
||||||
|
client.PartChannel(channel, "Caught in the bitstream...");
|
||||||
|
System.Console.WriteLine(String.Format("Parted channel #{0}...",
|
||||||
|
channel.Name));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void WriteLine(MessageContext context,
|
||||||
|
string message)
|
||||||
|
{
|
||||||
|
Console.WriteLine(String.Format("Sending: {0} to #{1}",
|
||||||
|
message,
|
||||||
|
context.Channel != null ? context.Channel.Name : "(null)"));
|
||||||
|
context.Channel.Talk(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ExtractMessage(string parameters,
|
private void ExtractMessage(string parameters,
|
||||||
|
@ -103,21 +127,64 @@ namespace TechBot.Library
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private bool GetChannelName(IrcMessage message,
|
||||||
|
out string channelName)
|
||||||
|
{
|
||||||
|
if (message.Parameters == null || !message.Parameters.StartsWith("#"))
|
||||||
|
{
|
||||||
|
channelName = null;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
int index = message.Parameters.IndexOf(' ');
|
||||||
|
if (index == -1)
|
||||||
|
index = message.Parameters.Length;
|
||||||
|
channelName = message.Parameters.Substring(1, index - 1);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool ShouldAcceptMessage(IrcMessage message,
|
||||||
|
out MessageContext context)
|
||||||
|
{
|
||||||
|
if (message.Command.ToUpper().Equals("PRIVMSG"))
|
||||||
|
{
|
||||||
|
string channelName;
|
||||||
|
if (GetChannelName(message,
|
||||||
|
out channelName))
|
||||||
|
{
|
||||||
|
foreach (IrcChannel channel in channels)
|
||||||
|
{
|
||||||
|
if (String.Compare(channel.Name, channelName, true) == 0)
|
||||||
|
{
|
||||||
|
context = new MessageContext(channel);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
context = null;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
private void client_MessageReceived(IrcMessage message)
|
private void client_MessageReceived(IrcMessage message)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (channel1 != null &&
|
if (message.Command != null &&
|
||||||
channel1.Name != null &&
|
|
||||||
message.Parameters != null)
|
message.Parameters != null)
|
||||||
{
|
{
|
||||||
string injectMessage;
|
string injectMessage;
|
||||||
ExtractMessage(message.Parameters, out injectMessage);
|
ExtractMessage(message.Parameters,
|
||||||
if ((message.Command.ToUpper().Equals("PRIVMSG")) &&
|
out injectMessage);
|
||||||
(message.Parameters.ToLower().StartsWith("#" + channel1.Name.ToLower() + " ")))
|
MessageContext context;
|
||||||
|
if (ShouldAcceptMessage(message,
|
||||||
|
out context))
|
||||||
{
|
{
|
||||||
Console.WriteLine("Injecting: " + injectMessage);
|
Console.WriteLine(String.Format("Injecting: {0} from #{1}",
|
||||||
service.InjectMessage(injectMessage);
|
injectMessage,
|
||||||
|
context.Channel.Name));
|
||||||
|
service.InjectMessage(context,
|
||||||
|
injectMessage);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
23
irc/TechBot/TechBot.Library/MessageContext.cs
Normal file
23
irc/TechBot/TechBot.Library/MessageContext.cs
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
using System;
|
||||||
|
using TechBot.IRCLibrary;
|
||||||
|
|
||||||
|
namespace TechBot.Library
|
||||||
|
{
|
||||||
|
public class MessageContext
|
||||||
|
{
|
||||||
|
private IrcChannel channel;
|
||||||
|
|
||||||
|
public IrcChannel Channel
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return channel;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public MessageContext(IrcChannel channel)
|
||||||
|
{
|
||||||
|
this.channel = channel;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -24,13 +24,15 @@ namespace TechBot.Library
|
||||||
new string[] { "ntstatus" });
|
new string[] { "ntstatus" });
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Handle(string commandName,
|
public void Handle(MessageContext context,
|
||||||
|
string commandName,
|
||||||
string parameters)
|
string parameters)
|
||||||
{
|
{
|
||||||
string ntstatusText = parameters;
|
string ntstatusText = parameters;
|
||||||
if (ntstatusText.Equals(String.Empty))
|
if (ntstatusText.Equals(String.Empty))
|
||||||
{
|
{
|
||||||
serviceOutput.WriteLine("Please provide a valid NTSTATUS value.");
|
serviceOutput.WriteLine(context,
|
||||||
|
"Please provide a valid NTSTATUS value.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -38,7 +40,8 @@ namespace TechBot.Library
|
||||||
long ntstatus = np.Parse(ntstatusText);
|
long ntstatus = np.Parse(ntstatusText);
|
||||||
if (np.Error)
|
if (np.Error)
|
||||||
{
|
{
|
||||||
serviceOutput.WriteLine(String.Format("{0} is not a valid NTSTATUS value.",
|
serviceOutput.WriteLine(context,
|
||||||
|
String.Format("{0} is not a valid NTSTATUS value.",
|
||||||
ntstatusText));
|
ntstatusText));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -46,13 +49,15 @@ namespace TechBot.Library
|
||||||
string description = GetNtstatusDescription(ntstatus);
|
string description = GetNtstatusDescription(ntstatus);
|
||||||
if (description != null)
|
if (description != null)
|
||||||
{
|
{
|
||||||
serviceOutput.WriteLine(String.Format("{0} is {1}.",
|
serviceOutput.WriteLine(context,
|
||||||
|
String.Format("{0} is {1}.",
|
||||||
ntstatusText,
|
ntstatusText,
|
||||||
description));
|
description));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
serviceOutput.WriteLine(String.Format("I don't know about NTSTATUS {0}.",
|
serviceOutput.WriteLine(context,
|
||||||
|
String.Format("I don't know about NTSTATUS {0}.",
|
||||||
ntstatusText));
|
ntstatusText));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ namespace TechBot.Library
|
||||||
{
|
{
|
||||||
public interface IServiceOutput
|
public interface IServiceOutput
|
||||||
{
|
{
|
||||||
void WriteLine(string message);
|
void WriteLine(MessageContext context,
|
||||||
|
string message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,10 +20,12 @@ namespace TechBot.Library
|
||||||
new string[] { "svn" });
|
new string[] { "svn" });
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Handle(string commandName,
|
public void Handle(MessageContext context,
|
||||||
|
string commandName,
|
||||||
string parameters)
|
string parameters)
|
||||||
{
|
{
|
||||||
serviceOutput.WriteLine(svnCommand);
|
serviceOutput.WriteLine(context,
|
||||||
|
svnCommand);
|
||||||
}
|
}
|
||||||
|
|
||||||
public string Help()
|
public string Help()
|
||||||
|
|
|
@ -9,8 +9,6 @@ namespace TechBot.Library
|
||||||
{
|
{
|
||||||
public class TechBotService
|
public class TechBotService
|
||||||
{
|
{
|
||||||
private const bool IsVerbose = false;
|
|
||||||
|
|
||||||
private IServiceOutput serviceOutput;
|
private IServiceOutput serviceOutput;
|
||||||
private string chmPath;
|
private string chmPath;
|
||||||
private string mainChm;
|
private string mainChm;
|
||||||
|
@ -37,12 +35,6 @@ namespace TechBot.Library
|
||||||
this.svnCommand = svnCommand;
|
this.svnCommand = svnCommand;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void WriteIfVerbose(string message)
|
|
||||||
{
|
|
||||||
if (IsVerbose)
|
|
||||||
serviceOutput.WriteLine(message);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Run()
|
public void Run()
|
||||||
{
|
{
|
||||||
commands.Add(new HelpCommand(serviceOutput,
|
commands.Add(new HelpCommand(serviceOutput,
|
||||||
|
@ -60,12 +52,12 @@ namespace TechBot.Library
|
||||||
svnCommand));
|
svnCommand));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void InjectMessage(string message)
|
public void InjectMessage(MessageContext context,
|
||||||
|
string message)
|
||||||
{
|
{
|
||||||
if (message.StartsWith("!"))
|
if (message.StartsWith("!"))
|
||||||
{
|
ParseCommandMessage(context,
|
||||||
ParseCommandMessage(message);
|
message);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool IsCommandMessage(string message)
|
private bool IsCommandMessage(string message)
|
||||||
|
@ -73,7 +65,8 @@ namespace TechBot.Library
|
||||||
return message.StartsWith("!");
|
return message.StartsWith("!");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ParseCommandMessage(string message)
|
public void ParseCommandMessage(MessageContext context,
|
||||||
|
string message)
|
||||||
{
|
{
|
||||||
if (!IsCommandMessage(message))
|
if (!IsCommandMessage(message))
|
||||||
return;
|
return;
|
||||||
|
@ -94,7 +87,8 @@ namespace TechBot.Library
|
||||||
{
|
{
|
||||||
if (command.CanHandle(commandName))
|
if (command.CanHandle(commandName))
|
||||||
{
|
{
|
||||||
command.Handle(commandName, parameters);
|
command.Handle(context,
|
||||||
|
commandName, parameters);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,13 +24,15 @@ namespace TechBot.Library
|
||||||
new string[] { "winerror" });
|
new string[] { "winerror" });
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Handle(string commandName,
|
public void Handle(MessageContext context,
|
||||||
|
string commandName,
|
||||||
string parameters)
|
string parameters)
|
||||||
{
|
{
|
||||||
string winerrorText = parameters;
|
string winerrorText = parameters;
|
||||||
if (winerrorText.Equals(String.Empty))
|
if (winerrorText.Equals(String.Empty))
|
||||||
{
|
{
|
||||||
serviceOutput.WriteLine("Please provide a valid System Error Code value.");
|
serviceOutput.WriteLine(context,
|
||||||
|
"Please provide a valid System Error Code value.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -38,7 +40,8 @@ namespace TechBot.Library
|
||||||
long winerror = np.Parse(winerrorText);
|
long winerror = np.Parse(winerrorText);
|
||||||
if (np.Error)
|
if (np.Error)
|
||||||
{
|
{
|
||||||
serviceOutput.WriteLine(String.Format("{0} is not a valid System Error Code value.",
|
serviceOutput.WriteLine(context,
|
||||||
|
String.Format("{0} is not a valid System Error Code value.",
|
||||||
winerrorText));
|
winerrorText));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -46,13 +49,15 @@ namespace TechBot.Library
|
||||||
string description = GetWinerrorDescription(winerror);
|
string description = GetWinerrorDescription(winerror);
|
||||||
if (description != null)
|
if (description != null)
|
||||||
{
|
{
|
||||||
serviceOutput.WriteLine(String.Format("{0} is {1}.",
|
serviceOutput.WriteLine(context,
|
||||||
|
String.Format("{0} is {1}.",
|
||||||
winerrorText,
|
winerrorText,
|
||||||
description));
|
description));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
serviceOutput.WriteLine(String.Format("I don't know about System Error Code {0}.",
|
serviceOutput.WriteLine(context,
|
||||||
|
String.Format("I don't know about System Error Code {0}.",
|
||||||
winerrorText));
|
winerrorText));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
<appSettings>
|
<appSettings>
|
||||||
<add key="IRCServerHostName" value="irc.eu.freenode.net" />
|
<add key="IRCServerHostName" value="irc.eu.freenode.net" />
|
||||||
<add key="IRCServerHostPort" value="6667" />
|
<add key="IRCServerHostPort" value="6667" />
|
||||||
<add key="IRCChannelName" value="channel" />
|
<add key="IRCChannelNames" value="channel1;channel2" />
|
||||||
<add key="IRCBotName" value="MyBot" />
|
<add key="IRCBotName" value="MyBot" />
|
||||||
<add key="ChmPath" value="C:\IRC\TechBot\CHM" />
|
<add key="ChmPath" value="C:\IRC\TechBot\CHM" />
|
||||||
<add key="MainChm" value="kmarch.chm" />
|
<add key="MainChm" value="kmarch.chm" />
|
||||||
|
|
|
@ -9,7 +9,7 @@ namespace TechBot
|
||||||
{
|
{
|
||||||
private string IRCServerHostName;
|
private string IRCServerHostName;
|
||||||
private int IRCServerHostPort;
|
private int IRCServerHostPort;
|
||||||
private string IRCChannelName;
|
private string IRCChannelNames;
|
||||||
private string IRCBotName;
|
private string IRCBotName;
|
||||||
private string ChmPath;
|
private string ChmPath;
|
||||||
private string MainChm;
|
private string MainChm;
|
||||||
|
@ -28,7 +28,7 @@ namespace TechBot
|
||||||
{
|
{
|
||||||
IRCServerHostName = ConfigurationSettings.AppSettings["IRCServerHostName"];
|
IRCServerHostName = ConfigurationSettings.AppSettings["IRCServerHostName"];
|
||||||
IRCServerHostPort = Int32.Parse(ConfigurationSettings.AppSettings["IRCServerHostPort"]);
|
IRCServerHostPort = Int32.Parse(ConfigurationSettings.AppSettings["IRCServerHostPort"]);
|
||||||
IRCChannelName = ConfigurationSettings.AppSettings["IRCChannelName"];
|
IRCChannelNames = ConfigurationSettings.AppSettings["IRCChannelNames"];
|
||||||
IRCBotName = ConfigurationSettings.AppSettings["IRCBotName"];
|
IRCBotName = ConfigurationSettings.AppSettings["IRCBotName"];
|
||||||
ChmPath = ConfigurationSettings.AppSettings["ChmPath"];
|
ChmPath = ConfigurationSettings.AppSettings["ChmPath"];
|
||||||
MainChm = ConfigurationSettings.AppSettings["MainChm"];
|
MainChm = ConfigurationSettings.AppSettings["MainChm"];
|
||||||
|
@ -45,7 +45,7 @@ namespace TechBot
|
||||||
|
|
||||||
IrcService ircService = new IrcService(IRCServerHostName,
|
IrcService ircService = new IrcService(IRCServerHostName,
|
||||||
IRCServerHostPort,
|
IRCServerHostPort,
|
||||||
IRCChannelName,
|
IRCChannelNames,
|
||||||
IRCBotName,
|
IRCBotName,
|
||||||
ChmPath,
|
ChmPath,
|
||||||
MainChm,
|
MainChm,
|
||||||
|
|
Loading…
Reference in a new issue