using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Globalization;
namespace Woodpecker.Specialized.Text
{
///
/// Provides various functions for the in-game chat.
///
public static class stringFunctions
{
///
/// Returns a boolean that indicates if a certain string contains ' or \.
///
/// The string to check.
public static bool containsVulnerableStuff(string s)
{
return (false); // char 9 etc
}
///
/// Returns a boolean that indicates if a certain string contains at least one number.
///
/// The string to check.
public static bool hasNumbers(string s)
{
return Regex.IsMatch(s, @"\d+");
}
///
/// Returns a boolean that indicates if a certain string contains numbers only.
///
/// The string to check.
public static bool isNumeric(string s)
{
foreach (char chr in s)
{
if (!Char.IsNumber(chr))
return false;
}
return true;
}
public static bool usernameIsValid(string Username)
{
const string Allowed = "1234567890qwertyuiopasdfghjklzxcvbnm-+=?!@:.,$";
foreach (char chr in Username.ToLower())
{
if(!Allowed.Contains(chr.ToString()))
return false;
}
return true;
}
public static void filterVulnerableStuff(ref string s, bool filterChar13)
{
s = s.Replace(Convert.ToChar(2), ' ');
s = s.Replace(Convert.ToChar(9), ' ');
s = s.Replace(Convert.ToChar(9), ' ');
s = s.Replace(Convert.ToChar(10), ' ');
s = s.Replace(Convert.ToChar(12), ' ');
if (filterChar13)
s = s.Replace(Convert.ToChar(13), ' ');
//return s;
}
///
/// Returns a boolean that indicates if an email address is valid.
///
/// The email address to check.
public static bool emailIsValid(string Email)
{
try
{
// mr.nillus@nillus.net
string[] Parts = Email.Split('@');
return (Parts.Length == 2 && Parts[1].Contains("."));
}
catch { return false; }
}
///
/// Returns a boolean that indicates if a string is valid as a password for a user. The string shouldn't be the same as the username, it should be 6-9 characters uint, it should contain at least one number and no 'stripslashed' characters.
///
/// The username for this password.
/// The password to check.
public static bool passwordIsValid(string Username, string Password)
{
return (Username != Password && (Password.Length > 5 && Password.Length < 10) && hasNumbers(Password));
}
///
/// Garbles a string with a given intensity.
///
///
///
public static void garbleText(ref string Text, int Intensity)
{
StringBuilder sb = new StringBuilder();
Random v = new Random(DateTime.Now.Millisecond + Intensity);
for (int pos = 0; pos < Text.Length; pos++)
{
if (v.Next(Intensity, 7) > 3 &&
Text[pos] != ' ' &&
Text[pos] != ',' &&
Text[pos] != '?' &&
Text[pos] != '!')
sb.Append('.');
else
sb.Append(Text[pos]);
}
Text = sb.ToString();
}
///
/// Formats a given floating point value to the format 0.00 and returns it as a string.
///
/// The floating point value to format and return.
public static string formatFloatForClient(float f)
{
return f.ToString("0.00", CultureInfo.InvariantCulture);
}
}
}