Skip to content

Commit

Permalink
Merge pull request #69 from ORelio/Indev
Browse files Browse the repository at this point in the history
Merging changes from Indev for 1.8.2 release
  • Loading branch information
ORelio committed Mar 11, 2015
2 parents 2f3f124 + 67e5882 commit 5b43056
Show file tree
Hide file tree
Showing 17 changed files with 360 additions and 210 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/MinecraftClient.suo
/MinecraftClientGUI.v11.suo
/MinecraftClientGUI.suo
/MinecraftClient.userprefs
41 changes: 41 additions & 0 deletions MinecraftClient/AutoTimeout.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Threading;

namespace MinecraftClient
{
/// <summary>
/// Allow easy timeout on pieces of code
/// </summary>
/// <remarks>
/// By ORelio - (c) 2014 - CDDL 1.0
/// </remarks>
public class AutoTimeout
{
/// <summary>
/// Perform the specified action with specified timeout
/// </summary>
/// <param name="action">Action to run</param>
/// <param name="timeout">Maximum timeout in milliseconds</param>
/// <returns>True if the action finished whithout timing out</returns>
public static bool Perform(Action action, int timeout)
{
return Perform(action, TimeSpan.FromMilliseconds(timeout));
}

/// <summary>
/// Perform the specified action with specified timeout
/// </summary>
/// <param name="action">Action to run</param>
/// <param name="timeout">Maximum timeout</param>
/// <returns>True if the action finished whithout timing out</returns>
public static bool Perform(Action action, TimeSpan timeout)
{
Thread thread = new Thread(new ThreadStart(action));
thread.Start();
bool success = thread.Join(timeout);
if (!success)
thread.Abort();
return success;
}
}
}
20 changes: 20 additions & 0 deletions MinecraftClient/ChatBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,26 @@ protected static bool isPrivateMessage(string text, ref string message, ref stri
return isValidName(sender);
}

//Detect HeroChat PMsend
//From Someone: message
else if (text.StartsWith("From "))
{
sender = text.Substring(5).Split(':')[0];
message = text.Substring(text.IndexOf(':') + 2);
return isValidName(sender);
}

//Detect HeroChat Messages
//[Channel] [Rank] User: Message
else if (text.StartsWith("[") && text.Contains(':') && tmp.Length > 2)
{
int name_end = text.IndexOf(':');
int name_start = text.Substring(0, name_end).LastIndexOf(']') + 2;
sender = text.Substring(name_start, name_end - name_start);
message = text.Substring(name_end + 2);
return isValidName(sender);
}

else return false;
}
catch (IndexOutOfRangeException) { return false; }
Expand Down
133 changes: 52 additions & 81 deletions MinecraftClient/ChatBots/Alerts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,125 +2,96 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace MinecraftClient.ChatBots
{
/// <summary>
/// This bot make the console beep on some specified words. Useful to detect when someone is talking to you, for example.
/// </summary>

public class Alerts : ChatBot
{
private string[] dictionary = new string[0];
private string[] excludelist = new string[0];

public override void Initialize()
/// <summary>
/// Import alerts from the specified file
/// </summary>
/// <param name="file"></param>
/// <returns></returns>
private static string[] FromFile(string file)
{
if (System.IO.File.Exists(Settings.Alerts_MatchesFile))
if (File.Exists(file))
{
List<string> tmp_dictionary = new List<string>();
string[] file_lines = System.IO.File.ReadAllLines(Settings.Alerts_MatchesFile);
foreach (string line in file_lines)
if (line.Trim().Length > 0 && !tmp_dictionary.Contains(line.ToLower()))
tmp_dictionary.Add(line.ToLower());
dictionary = tmp_dictionary.ToArray();
//Read all lines from file, remove lines with no text, convert to lowercase,
//remove duplicate entries, convert to a string array, and return the result.
return File.ReadAllLines(file)
.Where(line => !String.IsNullOrWhiteSpace(line))
.Select(line => line.ToLower())
.Distinct().ToArray();
}
else LogToConsole("File not found: " + Settings.Alerts_MatchesFile);

if (System.IO.File.Exists(Settings.Alerts_ExcludesFile))
else
{
List<string> tmp_excludelist = new List<string>();
string[] file_lines = System.IO.File.ReadAllLines(Settings.Alerts_ExcludesFile);
foreach (string line in file_lines)
if (line.Trim().Length > 0 && !tmp_excludelist.Contains(line.Trim().ToLower()))
tmp_excludelist.Add(line.ToLower());
excludelist = tmp_excludelist.ToArray();
LogToConsole("File not found: " + Settings.Alerts_MatchesFile);
return new string[0];
}
else LogToConsole("File not found : " + Settings.Alerts_ExcludesFile);
}

/// <summary>
/// Intitialize the Alerts bot
/// </summary>
public override void Initialize()
{
dictionary = FromFile(Settings.Alerts_MatchesFile);
excludelist = FromFile(Settings.Alerts_ExcludesFile);
}

/// <summary>
/// Process text received from the server to display alerts
/// </summary>
/// <param name="text">Received text</param>
public override void GetText(string text)
{
text = getVerbatim(text);
string comp = text.ToLower();
foreach (string alert in dictionary)
//Remove color codes and convert to lowercase
text = getVerbatim(text).ToLower();

//Proceed only if no exclusions are found in text
if (!excludelist.Any(exclusion => text.Contains(exclusion)))
{
if (comp.Contains(alert))
//Show an alert for each alert item found in text, if any
foreach (string alert in dictionary.Where(alert => text.Contains(alert)))
{
bool ok = true;
if (Settings.Alerts_Beep_Enabled)
Console.Beep(); //Text found !

foreach (string exclusion in excludelist)
{
if (comp.Contains(exclusion))
{
ok = false;
break;
}
}
if (ConsoleIO.basicIO) //Using a GUI? Pass text as is.
ConsoleIO.WriteLine(text.Replace(alert, "§c" + alert + "§r"));

if (ok)
else //Using Consome Prompt : Print text with alert highlighted
{
if (Settings.Alerts_Beep_Enabled) { Console.Beep(); } //Text found !
string[] splitted = text.Split(new string[] { alert }, StringSplitOptions.None);

if (ConsoleIO.basicIO) { ConsoleIO.WriteLine(comp.Replace(alert, "§c" + alert + "§r")); }
else
if (splitted.Length > 0)
{

#region Displaying the text with the interesting part highlighted

Console.BackgroundColor = ConsoleColor.DarkGray;
Console.ForegroundColor = ConsoleColor.White;
ConsoleIO.Write(splitted[0]);

//Will be used for text displaying
string[] temp = comp.Split(alert.Split(','), StringSplitOptions.None);
int p = 0;

//Special case : alert in the beginning of the text
string test = "";
for (int i = 0; i < alert.Length; i++)
{
test += comp[i];
}
if (test == alert)
for (int i = 1; i < splitted.Length; i++)
{
Console.BackgroundColor = ConsoleColor.Yellow;
Console.ForegroundColor = ConsoleColor.Red;
for (int i = 0; i < alert.Length; i++)
{
ConsoleIO.Write(text[p]);
p++;
}
}
ConsoleIO.Write(alert);

//Displaying the rest of the text
for (int i = 0; i < temp.Length; i++)
{
Console.BackgroundColor = ConsoleColor.DarkGray;
Console.ForegroundColor = ConsoleColor.White;
for (int j = 0; j < temp[i].Length; j++)
{
ConsoleIO.Write(text[p]);
p++;
}
Console.BackgroundColor = ConsoleColor.Yellow;
Console.ForegroundColor = ConsoleColor.Red;
try
{
for (int j = 0; j < alert.Length; j++)
{
ConsoleIO.Write(text[p]);
p++;
}
}
catch (IndexOutOfRangeException) { }
ConsoleIO.Write(splitted[i]);
}
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.Gray;
ConsoleIO.Write('\n');

#endregion

}

Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.Gray;
ConsoleIO.Write('\n');
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion MinecraftClient/ChatBots/ChatLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ private void save(string tosave)
if (dateandtime)
tosave = getTimestamp() + ' ' + tosave;

Directory.CreateDirectory(Path.GetDirectoryName(logfile));
string directory = Path.GetDirectoryName(logfile);
if (!String.IsNullOrEmpty(directory) && !Directory.Exists(directory))
Directory.CreateDirectory(directory);
FileStream stream = new FileStream(logfile, FileMode.OpenOrCreate);
StreamWriter writer = new StreamWriter(stream);
stream.Seek(0, SeekOrigin.End);
Expand Down
6 changes: 4 additions & 2 deletions MinecraftClient/ChatBots/RemoteControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public override void GetText(string text)
{
text = getVerbatim(text);
string command = "", sender = "";
if (isPrivateMessage(text, ref command, ref sender) && Settings.Bots_Owners.Contains(sender.ToLower()))
if (isPrivateMessage(text, ref command, ref sender) && Settings.Bots_Owners.Contains(sender.ToLower().Trim()))
{
string response = "";
performInternalCommand(command, ref response);
Expand All @@ -24,7 +24,9 @@ public override void GetText(string text)
SendPrivateMessage(sender, response);
}
}
else if (Settings.RemoteCtrl_AutoTpaccept && isTeleportRequest(text, ref sender) && Settings.Bots_Owners.Contains(sender.ToLower()))
else if (Settings.RemoteCtrl_AutoTpaccept
&& isTeleportRequest(text, ref sender)
&& (Settings.RemoteCtrl_AutoTpaccept_Everyone || Settings.Bots_Owners.Contains(sender.ToLower().Trim())))
{
SendText("/tpaccept");
}
Expand Down
19 changes: 19 additions & 0 deletions MinecraftClient/Commands/List.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MinecraftClient.Commands
{
public class List : Command
{
public override string CMDName { get { return "list"; } }
public override string CMDDesc { get { return "list: get the player list."; } }

public override string Run(McTcpClient handler, string command)
{
return "PlayerList: " + String.Join(", ", handler.getOnlinePlayers());
}
}
}

Loading

0 comments on commit 5b43056

Please sign in to comment.