-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCollabVmCommon.hpp
More file actions
60 lines (55 loc) · 1.89 KB
/
CollabVmCommon.hpp
File metadata and controls
60 lines (55 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <algorithm>
#include <cctype>
#include <chrono>
#include <string_view>
#include "Guacamole.capnp.h"
namespace CollabVm::Common {
constexpr static auto min_username_len = 3;
constexpr static auto max_username_len = 20;
constexpr static auto max_chat_message_len = 100;
constexpr static auto chat_rate_limit = std::chrono::seconds(2);
constexpr static auto username_change_rate_limit = std::chrono::seconds(30);
constexpr static auto vote_limit = 5;
bool ValidateUsername(const std::string_view username) {
if (username.length() < min_username_len ||
username.length() > max_username_len) {
return false;
}
auto prev_symbol = false;
const auto is_char_allowed = [&prev_symbol](const char c,
const bool allow_space = true) {
const static std::string allowed_symbols = "_-.?!";
if (std::isalnum(c)) {
prev_symbol = false;
return true;
}
const auto is_symbol =
std::find(allowed_symbols.cbegin(), allowed_symbols.cend(), c) !=
allowed_symbols.cend() ||
allow_space && c == ' ';
if (!is_symbol || prev_symbol) {
return false;
}
prev_symbol = true;
return true;
};
return is_char_allowed(username.front(), false) &&
std::all_of(std::next(username.cbegin()), std::prev(username.cend()),
is_char_allowed) &&
is_char_allowed(username.back(), false);
// Alternatively, this beautiful regular expression could be used:
// "^(?:[a-zA-Z0-9]|[_\-\.!\?](?!_|-|\.| |!|\?))"
// "(?:[a-zA-Z0-9]|[_\-\.!\? ](?!_|-|\.| |!|\?)){1,18}"
// "[a-zA-Z0-9_\-\.!\?]$"
}
bool IsGuacamoleClientInstructionAllowed(Guacamole::GuacClientInstruction::Which opcode) {
switch (opcode) {
case Guacamole::GuacClientInstruction::Which::MOUSE:
case Guacamole::GuacClientInstruction::Which::KEY:
return true;
default:
break;
}
return false;
}
}