r.searches function, fixes, SharedLibraries configuration inclusion

This commit is contained in:
DarkFeather
2016-12-04 19:55:52 -06:00
parent 1b4d0190e0
commit df636e8706
16 changed files with 97 additions and 182 deletions

View File

@@ -17,16 +17,17 @@ namespace AniNIX.TheRaven {
public string Nick { get; private set; } // This is the Nickname for this Raven to use.
private string _nickServPass; // This is the password we will send to NickServ to identify
private string _autoSend; // This is the command we will automatically send to the Host
private string configDir; // This is the configuration directory.
private string _configFile; // This is the configuration directory.
private Connection _connection; //This is the socket to the Host
public List<String> channels = new List<String>(); //This is the list of channels to join
public List<String> whitelist = new List<String>(); //This is the list of admin users.
public List<String> blacklist = new List<String>(); // This is the list of blocked people.
public String helpText = null; // This is the text to send when people ask for help -- this is configurable to allow for skinning
public List<String> searches = new List<String>(); //These are the searches
public String[] magic8 = null; //These are the strings to return like a Magic 8-ball to questions.
public String[] crowFacts = null; //These are the possible CrowFacts
public List<String> channels; //This is the list of channels to join
public List<String> whitelist; //This is the list of admin users.
public List<String> blacklist; // This is the list of blocked people.
public String helpText; // This is the text to send when people ask for help -- this is configurable to allow for skinning
public List<String> searches; //These are the searches
public String searchesIndex; //This is the helptext for the searches
public List<String> magic8; //These are the strings to return like a Magic 8-ball to questions.
public List<String> crowFacts; //These are the possible CrowFacts
public List<String> crowFactsSubscribers = new List<String>(); //These are the subscribers to CrowFacts
public Dictionary<String,String> notifications = new Dictionary<String,String>(); // This is the notifications list for TheRaven.
@@ -46,74 +47,55 @@ namespace AniNIX.TheRaven {
sb.Append(String.Format("Nick: {0}\n",Nick));
sb.Append("NickServPass: ****\n");
sb.Append(String.Format("Auto: {0}\n",_autoSend));
sb.Append(String.Format("Conf: {0}\n",configDir));
sb.Append(String.Format("Conf: {0}\n",_configFile));
sb.Append(String.Format("Verbosity: {0}\n",ReportMessage.verbosity));
return sb.ToString();
}
/// <summary>
/// Read from the files in the directory to configure this Raven
/// Read from the files in the /usr/local/etc/TheRaven directory to configure this Raven
/// </summary>
// TODO: This and ParseArgs may get punted into their own static class to improve readability.
private void ConfigureSelfFromFiles(String configDir) {
private void ConfigureSelfFromFiles() {
if (configDir==null || configDir == "" || !Directory.Exists(configDir)) {
ReportMessage.Log(Verbosity.Error,"Configuration directory does not exist!");
String confFilePath = String.Format("/usr/local/etc/TheRaven/{0}",_configFile);
if (!File.Exists(confFilePath)) {
ReportMessage.Log(Verbosity.Error,"Configuration file doesn't exist.");
return;
}
ReportMessage.Log(Verbosity.Always,String.Format("Reading from files in {0}...",configDir));
ReportMessage.Log(Verbosity.Always,String.Format("Reading from config file in /usr/local/etc/{0} and the global files in the same directory...",_configFile));
Configure conf = new Configure(confFilePath);
//These are locals that will be used throughout
ReportMessage.Log(Verbosity.Verbose,"Reading login defaults");
String[] loginDefaults = RavenConfigure.ReadLineDelimitedFile(Path.Combine(configDir,"loginDefaults.txt")).ToArray();
//We have to populate these properties fom the list explicitly
if (loginDefaults.Length < 4) {
ReportMessage.Log(Verbosity.Error,"Login defaults are incomplete. No changes made.");
} else {
Host = (Host==null) ? loginDefaults[0] : Host;
try {
Port = (Port == 0) ? Int32.Parse(loginDefaults[1]) : Port;
} catch (Exception e) {
ReportMessage.Log(Verbosity.Verbose,"Cannot parse Port.");
e.ToString();
Port = 6667;
}
Nick = (Nick == null) ? loginDefaults[2] : Nick;
_nickServPass = (_nickServPass == null) ? loginDefaults[3] : _nickServPass;
}
Dictionary<String,String> loginDefaults = conf.ReadSection("Login");
this.Host = loginDefaults["host"];
this.Port = Int32.Parse(loginDefaults["port"]);
this.Nick = loginDefaults["username"];
this._nickServPass = loginDefaults["password"];
//Read all the channels to join.
List<String> tempChannels = RavenConfigure.ReadLineDelimitedFile(Path.Combine(configDir,"rooms.txt"));
// Because the convention is to use # for comments, channels in the rooms.txt file do not start with a #
foreach (String channel in tempChannels) {
channels=new List<String>();
foreach (String channel in conf.ReadSectionLines("Rooms")) {
channels.Add(String.Format("#{0}",channel));
}
//Read the whitelist of folks allowed to execute administrative commands
whitelist = RavenConfigure.ReadLineDelimitedFile(Path.Combine(configDir,"whitelist.txt"));
//Read the blacklist of folks not allowed to do anything.
blacklist = RavenConfigure.ReadLineDelimitedFile(Path.Combine(configDir,"blacklist.txt"));
//Read the helptext
helpText = RavenConfigure.ReadFirstLineFromFile(Path.Combine(configDir,"helptext.txt"));
//Read the searches to use
searches = RavenConfigure.ReadLineDelimitedFile(Path.Combine(configDir,"searches.txt"));
//Read the Magic8 options
magic8 = RavenConfigure.ReadLineDelimitedFileToArr(Path.Combine(configDir,"magic8.txt"));
//Read the CrowFacts
crowFacts = RavenConfigure.ReadLineDelimitedFileToArr(Path.Combine(configDir,"crowfacts.txt"));
//Read the notifications
foreach (String combo in RavenConfigure.ReadLineDelimitedFileToArr(Path.Combine(configDir,"notifications.txt"))) {
String[] byPipe = combo.Split('|');
notifications.Add(String.Format("#{0}",byPipe[0]),byPipe[1]);
//Parse the lists.
notifications = conf.ReadSection("Notifications");
whitelist = conf.ReadSectionLines("Whitelist");
blacklist = conf.ReadSectionLines("Blacklist");
helpText = "Available commands are r.raven, r.magic8, r.uptime, r.heartbeat, r.msg <memo for admin>, r.d <dice test>, r.tinyurl <url>, and r.searches";
searches = conf.ReadSectionLines("Searches");
StringBuilder searchIndexBuilder = new StringBuilder();
foreach (String searchLine in searches) {
String[] byPipe = searchLine.Split('|');
if (byPipe.Length > 3) searchIndexBuilder.Append(String.Format("{0} <{1} search>, ",byPipe[0],byPipe[3]));
}
searchesIndex = searchIndexBuilder.ToString();
//Read the globals
magic8 = (new Configure("/usr/local/etc/TheRaven/magic8.txt")).GetLines();
crowFacts = (new Configure("/usr/local/etc/TheRaven/crowfacts.txt")).GetLines();
}
/// <summary>
@@ -156,7 +138,10 @@ namespace AniNIX.TheRaven {
//TODO Add helptext
break;
case "-c":
if (i < args.Length-1) configDir = args[++i];
if (i < args.Length-1) _configFile = args[++i];
break;
case "--version":
ReportMessage.Log(Verbosity.Always,"AniNIX::TheRaven version 0.2");
break;
}
}
@@ -170,33 +155,24 @@ namespace AniNIX.TheRaven {
/// The arguments for creating the bot
/// </param>
public Raven(string[] args) {
ReportMessage.Log(Verbosity.Always,"Reading arguments...");
// If we have arguments
this.ParseArguments(args);
this.ConfigureSelfFromFiles(configDir);
this.ConfigureSelfFromFiles();
ReportMessage.Log(Verbosity.VeryVerbose,"Started with these values:");
ReportMessage.Log(Verbosity.VeryVerbose,this.ToString());
}
/// <summary>
/// Populate the name recognition
/// </summary>
/// <summary>
/// Create a raven with default settings.
/// </summary>
public Raven(String host = "localhost", int port = 6667, String nick = "TheRaven-Guest", String nickServPass = "null", String autoSend = null, String configDir = "/usr/local/etc/TheRaven-Local", Verbosity verbosity = Verbosity.Verbose) {
public Raven(String host = "localhost", int port = 6667, String nick = "TheRaven-Guest", String nickServPass = "null", String autoSend = null, String _configFile = "raven.conf", Verbosity verbosity = Verbosity.Verbose) {
this.Host = host;
Port = port;
Nick = nick;
_nickServPass = nickServPass;
_autoSend = autoSend;
this.configDir = configDir;
this._configFile = _configFile;
ReportMessage.verbosity = verbosity;
}
@@ -219,8 +195,8 @@ namespace AniNIX.TheRaven {
send.CreateCustomMessage(String.Format("NICK {0}\nUSER {0} * * :{0}",Nick));
_connection.Write(send);
ReportMessage.Log(Verbosity.VeryVerbose,"USER and NICK sent");
//thanks to cfrayne for the refactor
//thanks to cfrayne for the refactor
do {
response = _connection.Read();
if (response.msgCode != null && response.msgCode.Equals("433")) throw new AlreadyIdentifiedException();
@@ -284,16 +260,20 @@ namespace AniNIX.TheRaven {
/* CROWFACTS the deserving */
else if (crowFactsSubscribers.Contains(response.user) && randomSeed.Next(10) < 8) {
IRCClientMessage send = new IRCClientMessage();
int location = randomSeed.Next(crowFacts.Length);
int location = randomSeed.Next(crowFacts.Count);
send.PrivMsg(crowFacts[location],response.user);
_connection.Write(send);
}
// If the WebPage
if (WebPageAPI.URLRegEx.Match(response.message).Success) {
try {
String title = WebPageAPI.GetPageTitle(WebPageAPI.URLRegEx.Match(response.message).Value);
if (!String.IsNullOrWhiteSpace(title)) {
IRCClientMessage send = new IRCClientMessage();
send.PrivMsg(String.Format("Web page title: {0}",WebPageAPI.GetPageTitle(WebPageAPI.URLRegEx.Match(response.message).Value)),(response.target.Equals(Nick))?response.user:response.target);
send.PrivMsg(String.Format("Web page title: {0}",title),(response.target.Equals(Nick))?response.user:response.target);
_connection.Write(send);
}
} catch (Exception e) {
e.ToString();
}
@@ -362,8 +342,7 @@ namespace AniNIX.TheRaven {
Host = null;
Port = 0;
_nickServPass = null;
_autoSend = null;
configDir = null;
_autoSend = null; _configFile = null;
whitelist = null;
blacklist = null;
magic8 = null;