Unofficial CardLife revival project, pronounced like "celery"
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.7KB

  1. using System.Reflection;
  2. using System.Text.RegularExpressions;
  3. namespace CLre_server.Tweaks.Chat
  4. {
  5. public class Attributes
  6. {
  7. }
  8. [System.AttributeUsage(System.AttributeTargets.Method)]
  9. public class ChatCommandAttribute : System.Attribute
  10. {
  11. public readonly string Name;
  12. private readonly Regex _pattern;
  13. private readonly Regex _usernamePattern;
  14. public ChatCommandAttribute(string name, string pattern,
  15. string usernamePattern = null,
  16. RegexOptions options = RegexOptions.Compiled | RegexOptions.IgnoreCase)
  17. {
  18. this.Name = name;
  19. this._pattern = new Regex(pattern, options);
  20. this._usernamePattern = usernamePattern == null ? null : new Regex(pattern, options);
  21. Assembly asm = Assembly.GetCallingAssembly();
  22. if (!ChatConnectionEngine._assembliesToCheck.Contains(asm))
  23. {
  24. ChatConnectionEngine._assembliesToCheck.Add(asm);
  25. }
  26. if (ChatHandler.IsAuthenticationReady && CLre.Config.chat_commands)
  27. {
  28. // Chat system is already started
  29. // TODO
  30. }
  31. }
  32. public Regex GetPattern()
  33. {
  34. return _pattern;
  35. }
  36. public Match RegexMatch(string sender, string message)
  37. {
  38. if (this._usernamePattern != null)
  39. {
  40. if (this._usernamePattern.IsMatch(sender))
  41. {
  42. return _pattern.Match(message);
  43. }
  44. }
  45. else
  46. {
  47. return _pattern.Match(message);
  48. }
  49. return System.Text.RegularExpressions.Match.Empty;
  50. }
  51. }
  52. }