validation.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // SPDX-FileCopyrightText: Copyright 2017 Citra Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <QRegularExpression>
  5. #include <QString>
  6. #include <QValidator>
  7. class Validation {
  8. public:
  9. Validation()
  10. : room_name(room_name_regex), nickname(nickname_regex), ip(ip_regex), port(0, UINT16_MAX) {}
  11. ~Validation() = default;
  12. const QValidator* GetRoomName() const {
  13. return &room_name;
  14. }
  15. const QValidator* GetNickname() const {
  16. return &nickname;
  17. }
  18. const QValidator* GetIP() const {
  19. return &ip;
  20. }
  21. const QValidator* GetPort() const {
  22. return &port;
  23. }
  24. private:
  25. /// room name can be alphanumeric and " " "_" "." and "-" and must have a size of 4-20
  26. QRegularExpression room_name_regex =
  27. QRegularExpression(QStringLiteral("^[a-zA-Z0-9._ -]{4,20}"));
  28. QRegularExpressionValidator room_name;
  29. /// nickname can be alphanumeric and " " "_" "." and "-" and must have a size of 4-20
  30. const QRegularExpression nickname_regex =
  31. QRegularExpression(QStringLiteral("^[a-zA-Z0-9._ -]{4,20}"));
  32. QRegularExpressionValidator nickname;
  33. /// ipv4 / ipv6 / hostnames
  34. QRegularExpression ip_regex = QRegularExpression(QStringLiteral(
  35. // IPv4 regex
  36. "^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$|"
  37. // IPv6 regex
  38. "^((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|"
  39. "(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-"
  40. "5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3})|:))|"
  41. "(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)"
  42. "(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3})|:))|"
  43. "(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]"
  44. "\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|"
  45. "(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2["
  46. "0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|"
  47. "(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2["
  48. "0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|"
  49. "(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2["
  50. "0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|"
  51. "(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?"
  52. "\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:)))(%.+)?$|"
  53. // Hostname regex
  54. "^([a-zA-Z0-9]+(-[a-zA-Z0-9]+)*\\.)+[a-zA-Z]{2,}$"));
  55. QRegularExpressionValidator ip;
  56. /// port must be between 0 and 65535
  57. QIntValidator port;
  58. };