#include <MWUtils.h>
|
|
#include <MW.h>
|
|
|
|
#include <sstream>
|
|
#include <algorithm>
|
|
|
|
using namespace std;
|
|
|
|
static const unsigned int maxNameSize = 12;
|
|
static const unsigned int minNameSize = 3;
|
|
static const string nameRange = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
static const string isEmptyTokens = "\t\r\n\f\v ";
|
|
|
|
static const unsigned int maxMessageSize = 1024;
|
|
|
|
string
|
|
validateName(const string& in)
|
|
{
|
|
if(in.size() > maxNameSize || in.size() < minNameSize)
|
|
{
|
|
ostringstream msg;
|
|
msg << "Your name must be between " << minNameSize << " and " << maxNameSize << " characters in length.";
|
|
throw invalid_argument(msg.str());
|
|
}
|
|
if(in.find_last_not_of(nameRange) != string::npos)
|
|
{
|
|
throw invalid_argument("Invalid character in name. Valid characters are letter and digits.");
|
|
}
|
|
string out = in;
|
|
transform(out.begin(), out.end(), out.begin(), ::tolower);
|
|
if(out.begin() != out.end())
|
|
{
|
|
transform(out.begin(), out.begin() + 1, out.begin(), ::toupper);
|
|
}
|
|
return out;
|
|
}
|
|
|
|
string
|
|
validateMessage(const string& in)
|
|
{
|
|
if(in.size() > maxMessageSize)
|
|
{
|
|
ostringstream os;
|
|
os << "Message length exceeded, maximum length is " << maxMessageSize << " characters.";
|
|
throw MW::InvalidMessageException(os.str());
|
|
}
|
|
if(in.find_last_not_of(isEmptyTokens) == string::npos)
|
|
{
|
|
throw invalid_argument("Your message is empty and was ignored.");
|
|
}
|
|
// Strip html codes in the message
|
|
string out;
|
|
for(char c : in)
|
|
{
|
|
switch(c)
|
|
{
|
|
case '&':
|
|
{
|
|
out.append("&");
|
|
break;
|
|
}
|
|
|
|
case '"':
|
|
{
|
|
out.append(""");
|
|
break;
|
|
}
|
|
|
|
case '\'':
|
|
{
|
|
out.append("'");
|
|
break;
|
|
}
|
|
|
|
case '<':
|
|
{
|
|
out.append("<");
|
|
break;
|
|
}
|
|
|
|
case '>':
|
|
{
|
|
out.append(">");
|
|
break;
|
|
}
|
|
|
|
case '\r':
|
|
case '\n':
|
|
case '\v':
|
|
case '\f':
|
|
case '\t':
|
|
{
|
|
out.append(" ");
|
|
break;
|
|
}
|
|
|
|
default:
|
|
{
|
|
out.push_back(c);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
return out;
|
|
}
|