diff --git a/src/MWSessionManagerI.cpp b/src/MWSessionManagerI.cpp index 3eddb46..aceffae 100644 --- a/src/MWSessionManagerI.cpp +++ b/src/MWSessionManagerI.cpp @@ -1,19 +1,19 @@ -#include -#include -#include +#include +#include +#include using namespace std; -ChatSessionManagerI::ChatSessionManagerI(const shared_ptr& chatRoom, bool trace, +MWSessionManagerI::MWSessionManagerI(const shared_ptr& mwmap, bool trace, const shared_ptr& logger) : - _chatRoom(chatRoom), + _MWMap(mwmap), _trace(trace), _logger(logger) { } shared_ptr -ChatSessionManagerI::create(string name, +MWSessionManagerI::create(string name, shared_ptr sessionControl, const Ice::Current& current) { @@ -21,7 +21,7 @@ ChatSessionManagerI::create(string name, try { vname = validateName(name); - _chatRoom->reserve(vname); + _MWMap->reserve(vname); } catch(const exception& ex) { @@ -36,7 +36,7 @@ ChatSessionManagerI::create(string name, shared_ptr proxy; try { - auto session = make_shared(_chatRoom, vname, _trace, _logger); + auto session = make_shared(_MWMap, vname, _trace, _logger); proxy = Ice::uncheckedCast(current.adapter->addWithUUID(session)); Ice::IdentitySeq ids; diff --git a/src/MWUtils.cpp b/src/MWUtils.cpp new file mode 100644 index 0000000..8cd8bae --- /dev/null +++ b/src/MWUtils.cpp @@ -0,0 +1,105 @@ +#include +#include + +#include +#include + +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; +} diff --git a/src/Makefile b/src/Makefile index 1b87c1e..2130f96 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,6 +1,6 @@ CC=c++ IDIR =../include -CFLAGS=-I. -DICE_CPP11_MAPPING -I $(IDIR) -std=c++11 +CFLAGS=-I. -DICE_CPP11_MAPPING -I $(IDIR) -std=c++11 -pthread ODIR=obj LDIR =../lib @@ -10,7 +10,7 @@ LIBS=-lm -lIce++11 _DEPS = MW.h MWMap.h MWSession.h MWSessionI.h MWSessionManagerI.h DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS)) -_OBJ = MW.o MWServer.o MWMap.o MWSession.o MWSesionManagerI.o MWUtils.o +_OBJ = MW.o MWServer.o MWMap.o MWSession.o MWSessionManagerI.o MWUtils.o OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ)) $(ODIR)/%.o: %.cpp $(DEPS)