GUIString ChatCommandParser::GetRestOfLine(const GUIString &s1, int thePos) { SkipWhitespace(s1,thePos); if(thePos>=s1.length()) return ""; GUIString aWord; if(s1.at(thePos)=='"') { int anEndQuotePos = s1.find('"',thePos+1); if(anEndQuotePos>=0) { aWord = s1.substr(thePos+1,anEndQuotePos-thePos-1); return aWord; } } int aPos = s1.length()-1; while(aPos>thePos) { if(s1.at(aPos)!=' ') break; aPos--; } if(aPos>thePos) return s1.substr(thePos,aPos-thePos+1); else return ""; }
void HTMLDocumentGen::printf(const GUIString &theText) { int aStartPos = 0; while(true) { int anEndPos = theText.find('%',aStartPos); if(anEndPos<0) break; if(theText.at(anEndPos+1)=='%') { anEndPos++; AddDoc(theText.substr(aStartPos,anEndPos-aStartPos),HTMLDocGenFlag_IsHTML); aStartPos = anEndPos+1; continue; } AddDoc(theText.substr(aStartPos,anEndPos-aStartPos),HTMLDocGenFlag_IsHTML); anEndPos++; aStartPos = anEndPos; while(anEndPos<theText.length() && theText.at(anEndPos)!='%') anEndPos++; GUIString aNumber = theText.substr(aStartPos,anEndPos-aStartPos); int aParamNum = aNumber.atoi()-1; if(aParamNum>=0 && aParamNum<mDocumentVector.size()) mDocument->AddDocument(mDocumentVector[aParamNum]); aStartPos = anEndPos+1; if(aStartPos>=theText.length()) break; } if(aStartPos<theText.length()) AddDoc(theText.substr(aStartPos),HTMLDocGenFlag_IsHTML); }
bool ChatCommandParser::HandleInput(const GUIString &theInput) { mLastInputError = InputError_None; mLastTargetClient = NULL; mLastMatchName.erase(); if(mClientList.get()==NULL) { mLastInputError = InputError_NotLoggedIn; return false; } if(theInput.at(0)==mCommandChar) return HandleCommand(theInput); if(mDoColonEmote && theInput.at(0)==L':') { mLastChatCommand = LobbyChatCommand_Emote; mLastChat = theInput.substr(1); int aPos = 0; SkipWhitespace(mLastChat,aPos); if(aPos==mLastChat.length()) { mLastInputError = InputError_NoChat; return false; } // mServer->mConnection->SendChat(aChat,RoutingChatFlag_IsEmote,theGroupIdContext); return true; } mLastChatCommand = LobbyChatCommand_Broadcast; int aPos = 0; SkipWhitespace(theInput,aPos); if(aPos==theInput.length()) { mLastInputError = InputError_NoChat; return false; } mLastChat = theInput; // mServer->mConnection->SendChat(theInput,0,theGroupIdContext); return true; }
void TextArea::AddFormatedText(const GUIString &theText, bool lineBreak) { int aStartPos = 0; int anEndPos = 0; int aNextStartPos = 0; while(aStartPos < theText.length()) { int anEndPos = aStartPos; while(anEndPos < theText.length()) // find "\r", "\r\n", or "\n" { int aChar = theText.at(anEndPos); if(aChar=='\r') { if(anEndPos+1 < theText.length() && theText.at(anEndPos+1)=='\n') aNextStartPos = anEndPos + 2; else aNextStartPos = anEndPos + 1; break; } else if(aChar=='\n') { aNextStartPos = anEndPos + 1; break; } anEndPos++; } bool foundReturn = true; if(anEndPos>=theText.length()) { aNextStartPos = theText.length(); // Force it to exit next pass. foundReturn = lineBreak; } AddSegment(theText.substr(aStartPos,anEndPos-aStartPos),foundReturn); aStartPos = aNextStartPos; } }
GUIString ChatCommandParser::GetWord(const GUIString &s1, int &thePos, bool doToUpper, bool checkQuotes) { SkipWhitespace(s1,thePos); if(thePos>=s1.length()) return ""; GUIString aWord; if(checkQuotes && s1.at(thePos)=='"') { int anEndQuotePos = s1.find('"',thePos+1); if(anEndQuotePos>=0) { aWord = s1.substr(thePos+1,anEndQuotePos-thePos-1); thePos = anEndQuotePos+1; if(doToUpper) aWord.toUpperCase(); return aWord; } } while(thePos<s1.length()) { wchar_t aChar = s1.at(thePos); if(doToUpper) aChar = towupper(aChar); if(aChar==' ') return aWord; aWord.append(aChar); thePos++; } return aWord; }
bool ChatCommandParser::HandleCommand(const GUIString &theInput) { int aPos = 1; GUIString aCommand = GetWord(theInput,aPos,true,false); CommandStringMap::iterator anItr = mCommandStringMap.find(aCommand); if(anItr==mCommandStringMap.end()) { mLastChatCommand = LobbyChatCommand_None; mLastInputError = InputError_InvalidCommand; return false; } mLastChatCommand = anItr->second; // Extract name if needed bool needName = false; switch(mLastChatCommand) { case LobbyChatCommand_Reply: mLastMatchName = mLastWhisperClientName; needName = true; break; case LobbyChatCommand_Ignore: mLastMatchName = GetRestOfLine(theInput,aPos); needName = (! mLastMatchName.empty()); break; case LobbyChatCommand_Block: mLastMatchName = GetRestOfLine(theInput,aPos); needName = true; break; case LobbyChatCommand_Unmute: case LobbyChatCommand_ServerUnmute: mLastMatchName = GetRestOfLine(theInput,aPos); if(!mLastMatchName.empty()) needName = true; break; case LobbyChatCommand_Warn: case LobbyChatCommand_Whisper: case LobbyChatCommand_Mute: case LobbyChatCommand_ServerMute: case LobbyChatCommand_Ban: case LobbyChatCommand_ServerBan: case LobbyChatCommand_Invite: case LobbyChatCommand_Uninvite: mLastMatchName = GetWord(theInput,aPos,false,true); needName = true; break; } // Find corresponding client/member if(needName) { if(mLastMatchName.empty()) { mLastInputError = InputError_RequireName; return false; } MatchResult aResult; mLastTargetClient = GetClientByName(mLastMatchName, &aResult); if(aResult < mMatchTolerance) { if(aResult==MatchResult_Ambiguous) mLastInputError = InputError_ClientAmbiguous; else mLastInputError = InputError_ClientNotFound; return false; } } SkipWhitespace(theInput,aPos); switch(mLastChatCommand) { /////////////////////////////////////////// /////////////////////////////////////////// case LobbyChatCommand_Warn: case LobbyChatCommand_Whisper: case LobbyChatCommand_Alert: case LobbyChatCommand_Emote: { if(aPos>=theInput.length()) { mLastInputError = InputError_NoChat; return false; } mLastChat = theInput.substr(aPos); return true; } /////////////////////////////////////////// /////////////////////////////////////////// case LobbyChatCommand_Reply: { mLastChat = theInput.substr(aPos); if(!mLastChat.empty()) mLastChatCommand = LobbyChatCommand_Whisper; return true; } /////////////////////////////////////////// /////////////////////////////////////////// case LobbyChatCommand_BecomeModerator: case LobbyChatCommand_Help: case LobbyChatCommand_Ignore: case LobbyChatCommand_Away: case LobbyChatCommand_AbortShutdown: return true; /////////////////////////////////////////// /////////////////////////////////////////// case LobbyChatCommand_Invite: case LobbyChatCommand_Uninvite: { mLastChat = theInput.substr(aPos); mLastBool = mLastChatCommand==LobbyChatCommand_Invite; return true; } /////////////////////////////////////////// /////////////////////////////////////////// case LobbyChatCommand_Mute: case LobbyChatCommand_ServerMute: case LobbyChatCommand_Ban: case LobbyChatCommand_ServerBan: case LobbyChatCommand_StartShutdown: { DWORD aSeconds = 3600; // 1 hour std::string aTimeStr = GetWord(theInput,aPos,true,false); if(aTimeStr=="INFINITE") aSeconds = 0; else if(aTimeStr.length()>0) { char aUnitChar = aTimeStr[aTimeStr.length()-1]; if(isalpha(aUnitChar)) aTimeStr = aTimeStr.substr(0,aTimeStr.length()-1); aSeconds = atoi(aTimeStr.c_str()); switch(aUnitChar) { case 'D': aSeconds*=24; case 'H': aSeconds*=60; case 'M': aSeconds*=60; case 'S': break; default: mLastInputError = InputError_BadTime; return false; } if(aSeconds==0) { mLastInputError = InputError_BadTime; return false; } } mLastInputDuration = aSeconds; mLastChat = GetRestOfLine(theInput,aPos); return true; } /////////////////////////////////////////// /////////////////////////////////////////// case LobbyChatCommand_Unmute: case LobbyChatCommand_ServerUnmute: { mLastChat = GetRestOfLine(theInput,aPos); return true; } } return true; }