IMAPResult IMAPSearchParser::ParseCommand(std::shared_ptr<IMAPCommandArgument> pArgument, bool bIsSort) { // Replace literals in the command. std::shared_ptr<IMAPSimpleCommandParser> pSimpleParser = std::shared_ptr<IMAPSimpleCommandParser> (new IMAPSimpleCommandParser); if (bIsSort) { pSimpleParser->Parse(pArgument); pSimpleParser->UnliteralData(); std::shared_ptr<IMAPSimpleWord> pSort = pSimpleParser->Word(0); if (pSort->Paranthezied()) { sort_parser_ = std::shared_ptr<IMAPSortParser>(new IMAPSortParser); sort_parser_->Parse(pSort->Value()); } // Second part should be character set. if (pSimpleParser->WordCount() < 2) { return IMAPResult(IMAPResult::ResultBad, "SearchCharacter set must be specified."); } charset_name_ = pSimpleParser->Word(1)->Value(); if (!IsValidCharset_(charset_name_)) return IMAPResult(IMAPResult::ResultNo, "[BADCHARSET]"); // Trim away the SORT part of the SEARCH expresson // since we only care about SEARCH below. String tempString = pArgument->Command(); if (tempString.Find(_T(")")) > 0) tempString = tempString.Mid(tempString.Find(_T(")"))+2); pArgument->Command(tempString); } /* Remove all parenthesis outside of strings. Some IMAP clients sends parenthesis, some doesn't. We remove them here to prevent the behaviors from differing. It should be safe to do this, since Criteria1 OR (Criteria2 Criteria 3) means the same as Criteria1 OR Criteria2 Criteria 3 */ String resultString; const String inputString = pArgument->Command(); bool insideString = false; for (int i = 0; i < inputString.GetLength(); i++) { wchar_t curChar = inputString.GetAt(i); switch (curChar) { case '"': insideString = !insideString; break; case '(': case ')': if (!insideString) continue; } resultString += curChar; } // Replace literals in the command. pSimpleParser = std::shared_ptr<IMAPSimpleCommandParser> (new IMAPSimpleCommandParser); pArgument->Command(resultString); pSimpleParser->Parse(pArgument); pSimpleParser->UnliteralData(); std::shared_ptr<IMAPSearchCriteria> pCriteria = std::shared_ptr<IMAPSearchCriteria> (new IMAPSearchCriteria); int currentWord = 0; IMAPResult result = ParseSegment_(pSimpleParser, currentWord, pCriteria, 0); if (result.GetResult() != IMAPResult::ResultOK) return result; result_criteria_ = pCriteria; return IMAPResult(); }
void IMAPSimpleCommandParser::Parse(std::shared_ptr<IMAPCommandArgument> pArgument) { if (!Validate_(pArgument->Command())) return; int iCurWordStartPos = 0; const String sCommand = pArgument->Command(); int iCurrentLiteralPos = 0; while (1) { bool bCurWordIsQuoted = false; bool bCurWordIsParanthezed = false; bool bCurWordIsClammerized = false; if (iCurWordStartPos >= sCommand.GetLength()) break; if (sCommand.GetAt(iCurWordStartPos) == '\"') { // Move to the first character in the quoted string. iCurWordStartPos ++; bCurWordIsQuoted = true; } else if (sCommand.GetAt(iCurWordStartPos) == '(') { iCurWordStartPos ++; bCurWordIsParanthezed = true; } else if (sCommand.GetAt(iCurWordStartPos) == '{') { iCurWordStartPos ++; bCurWordIsClammerized = true; } int iCurWordEndPos = 0; if (bCurWordIsQuoted) { iCurWordEndPos = FindEndOfQuotedString_(sCommand, iCurWordStartPos); if (iCurWordEndPos < 0) return; } else if (bCurWordIsParanthezed) { // Find the end parentheses. bool bInString = false; int nestDeep = 1; for (int i = iCurWordStartPos; i < sCommand.GetLength(); i++) { wchar_t sCurPos = sCommand.GetAt(i); if (sCurPos == '"') { bInString = !bInString; continue; } if (bInString) continue; if (sCurPos == '(') nestDeep++; if (sCurPos == ')') { nestDeep--; if (nestDeep == 0) { iCurWordEndPos = i; break; } } } if (iCurWordEndPos <= 0) iCurWordEndPos = sCommand.GetLength(); } else if (bCurWordIsClammerized) { iCurWordEndPos = sCommand.Find(_T("}"), iCurWordStartPos); if (iCurWordEndPos < 0) return; } else { iCurWordEndPos = sCommand.Find(_T(" "), iCurWordStartPos); if (iCurWordEndPos == -1) { // Found end of string. iCurWordEndPos = sCommand.GetLength(); } } int iCurWordLength = iCurWordEndPos - iCurWordStartPos; String sCurWord = sCommand.Mid(iCurWordStartPos, iCurWordLength); if (bCurWordIsQuoted) { // The characters " and \ are escaped so we need to unescape them. IMAPFolder::UnescapeFolderString(sCurWord); } std::shared_ptr<IMAPSimpleWord> pWord = std::shared_ptr<IMAPSimpleWord>(new IMAPSimpleWord); pWord->Value(sCurWord); pWord->Quoted(bCurWordIsQuoted); pWord->Paranthezied(bCurWordIsParanthezed); pWord->Clammerized(bCurWordIsClammerized); if (bCurWordIsClammerized) { pWord->LiteralData(pArgument->Literal(iCurrentLiteralPos)); iCurrentLiteralPos++; } parsed_words_.push_back(pWord); if (bCurWordIsQuoted || bCurWordIsParanthezed || bCurWordIsClammerized) iCurWordStartPos = iCurWordEndPos + 2; else iCurWordStartPos = iCurWordEndPos + 1; } }
IMAPResult IMAPCommandSEARCH::ExecuteCommand(std::shared_ptr<IMAPConnection> pConnection, std::shared_ptr<IMAPCommandArgument> pArgument) { if (is_sort_ && !Configuration::Instance()->GetIMAPConfiguration()->GetUseIMAPSort()) return IMAPResult(IMAPResult::ResultNo, "IMAP SORT is not enabled."); if (!pConnection->IsAuthenticated()) return IMAPResult(IMAPResult::ResultNo, "Authenticate first"); if (!pConnection->GetCurrentFolder()) return IMAPResult(IMAPResult::ResultNo, "No folder selected."); if (!pArgument) return IMAPResult(IMAPResult::ResultNo, "Internal error IMAP-SEARCH-1."); { // The IMAP Search parser should not parse // the beginning of the command, UID SEARCH // or SEARCH String sCommand = pArgument->Command(); int iCommandStartPos; if (is_uid_) iCommandStartPos = sCommand.Find(_T(" "), 4) + 1; else iCommandStartPos = sCommand.Find(_T(" ")) + 1; sCommand = sCommand.Mid(iCommandStartPos); // 3 as in UID pArgument->Command(sCommand); } std::shared_ptr<IMAPSearchParser> pParser = std::shared_ptr<IMAPSearchParser>(new IMAPSearchParser()); IMAPResult result = pParser->ParseCommand(pArgument, is_sort_); if (result.GetResult() != IMAPResult::ResultOK) return result; if (is_sort_ && !pParser->GetSortParser()) return IMAPResult(IMAPResult::ResultBad, "Incorrect search commands."); // Mails in current box std::shared_ptr<IMAPFolder> pCurFolder = pConnection->GetCurrentFolder(); if (!pCurFolder) return IMAPResult(IMAPResult::ResultBad, "No selected folder"); std::vector<std::shared_ptr<Message>> messages = pCurFolder->GetMessages()->GetCopy(); std::vector<String> sMatchingVec; if (messages.size() > 0) { // Iterate through the messages and see which ones match. std::vector<std::pair<int, std::shared_ptr<Message> > > vecMatchingMessages; int index = 0; for(std::shared_ptr<Message> pMessage : messages) { const String fileName = PersistentMessage::GetFileName(pConnection->GetAccount(), pMessage); index++; if (pMessage && DoesMessageMatch_(pParser->GetCriteria(), fileName, pMessage, index)) { // Yup we got a match. vecMatchingMessages.push_back(make_pair(index, pMessage)); } } if (is_sort_) { IMAPSort oSorter; oSorter.Sort(pConnection, vecMatchingMessages, pParser->GetSortParser()); // Sort the message vector } typedef std::pair<int, std::shared_ptr<Message> > MessagePair; for(MessagePair messagePair : vecMatchingMessages) { int index = messagePair.first; std::shared_ptr<Message> pMessage = messagePair.second; String sID; if (is_uid_) sID.Format(_T("%u"), pMessage->GetUID()); else sID.Format(_T("%d"), index); sMatchingVec.push_back(sID); } } // Send response String sMatching; if (sMatchingVec.size() > 0) { // If we don't find any matches, we shouldn't return a whitespace // after SEARCH/SORT below. That's why we add the white space here. sMatching = " " + StringParser::JoinVector(sMatchingVec, " ") ; } String sResponse; if (is_sort_) sResponse = "* SORT" + sMatching + "\r\n"; else sResponse = "* SEARCH" + sMatching + "\r\n"; if (!is_uid_) // if this is a UID command, IMAPCommandUID takes care of the below line. sResponse += pArgument->Tag() + " OK Search completed\r\n"; pConnection->SendAsciiData(sResponse); return IMAPResult(); }
IMAPResult IMAPCommandUID::ExecuteCommand(std::shared_ptr<IMAPConnection> pConnection, std::shared_ptr<IMAPCommandArgument> pArgument) { if (!pConnection->IsAuthenticated()) return IMAPResult(IMAPResult::ResultNo, "Authenticate first"); String sTag = pArgument->Tag(); String sCommand = pArgument->Command(); if (!pConnection->GetCurrentFolder()) return IMAPResult(IMAPResult::ResultNo, "No folder selected."); std::shared_ptr<IMAPSimpleCommandParser> pParser = std::shared_ptr<IMAPSimpleCommandParser>(new IMAPSimpleCommandParser()); pParser->Parse(pArgument); if (pParser->WordCount() < 2) return IMAPResult(IMAPResult::ResultBad, "Command requires at least 1 parameter."); String sTypeOfUID = pParser->Word(1)->Value(); if (sTypeOfUID.CompareNoCase(_T("FETCH")) == 0) { if (pParser->WordCount() < 4) return IMAPResult(IMAPResult::ResultBad, "Command requires at least 3 parameters."); command_ = std::shared_ptr<IMAPFetch>(new IMAPFetch()); } else if (sTypeOfUID.CompareNoCase(_T("COPY")) == 0) { if (pParser->WordCount() < 4) return IMAPResult(IMAPResult::ResultBad, "Command requires at least 3 parameters."); command_ = std::shared_ptr<IMAPCopy>(new IMAPCopy()); } else if (sTypeOfUID.CompareNoCase(_T("STORE")) == 0) { if (pParser->WordCount() < 4) return IMAPResult(IMAPResult::ResultBad, "Command requires at least 3 parameters."); command_ = std::shared_ptr<IMAPStore>(new IMAPStore()); } else if (sTypeOfUID.CompareNoCase(_T("SEARCH")) == 0) { std::shared_ptr<IMAPCommandSEARCH> pCommand = std::shared_ptr<IMAPCommandSEARCH> (new IMAPCommandSEARCH(false)); pCommand->SetIsUID(); IMAPResult result = pCommand->ExecuteCommand(pConnection, pArgument); if (result.GetResult() == IMAPResult::ResultOK) pConnection->SendAsciiData(sTag + " OK UID completed\r\n"); return result; } else if (sTypeOfUID.CompareNoCase(_T("SORT")) == 0) { std::shared_ptr<IMAPCommandSEARCH> pCommand = std::shared_ptr<IMAPCommandSEARCH> (new IMAPCommandSEARCH(true)); pCommand->SetIsUID(); IMAPResult result = pCommand->ExecuteCommand(pConnection, pArgument); if (result.GetResult() == IMAPResult::ResultOK) pConnection->SendAsciiData(sTag + " OK UID completed\r\n"); return result; } if (!command_) return IMAPResult(IMAPResult::ResultBad, "Bad command."); command_->SetIsUID(true); // Copy the first word containing the message sequence long lSecWordStartPos = sCommand.Find(_T(" "), 5) + 1; long lSecWordEndPos = sCommand.Find(_T(" "), lSecWordStartPos); long lSecWordLength = lSecWordEndPos - lSecWordStartPos; String sMailNo = sCommand.Mid(lSecWordStartPos, lSecWordLength); // Copy the second word containing the actual command. String sShowPart = sCommand.Mid(lSecWordEndPos + 1); if (sMailNo.IsEmpty()) return IMAPResult(IMAPResult::ResultBad, "No mail number specified"); if (!StringParser::ValidateString(sMailNo, "01234567890,.:*")) return IMAPResult(IMAPResult::ResultBad, "Incorrect mail number"); // Set the command to execute as argument pArgument->Command(sShowPart); // Execute the command. If we have gotten this far, it means that the syntax // of the command is correct. If we fail now, we should return NO. IMAPResult result = command_->DoForMails(pConnection, sMailNo, pArgument); if (result.GetResult() == IMAPResult::ResultOK) pConnection->SendAsciiData(pArgument->Tag() + " OK UID completed\r\n"); return result; }
IMAPResult IMAPStore::DoAction(std::shared_ptr<IMAPConnection> pConnection, int messageIndex, std::shared_ptr<Message> pMessage, const std::shared_ptr<IMAPCommandArgument> pArgument) { if (!pMessage || !pArgument) return IMAPResult(IMAPResult::ResultBad, "Invalid parameters"); if (pConnection->GetCurrentFolderReadOnly()) { return IMAPResult(IMAPResult::ResultNo, "Store command on read-only folder."); } bool bSilent = false; String sCommand = pArgument->Command(); if (sCommand.FindNoCase(_T("FLAGS.SILENT")) >= 0) bSilent = true; else bSilent = false; // Read flags from command. bool bSeen = (sCommand.FindNoCase(_T("\\Seen")) >= 0); bool bDeleted = (sCommand.FindNoCase(_T("\\Deleted")) >= 0); bool bDraft = (sCommand.FindNoCase(_T("\\Draft")) >= 0); bool bAnswered = (sCommand.FindNoCase(_T("\\Answered")) >= 0); bool bFlagged = (sCommand.FindNoCase(_T("\\Flagged")) >= 0); if (bSeen) { // ACL: If user tries to change the Seen flag, check that he has permission to do so. if (!pConnection->CheckPermission(pConnection->GetCurrentFolder(), ACLPermission::PermissionWriteSeen)) return IMAPResult(IMAPResult::ResultNo, "ACL: WriteSeen permission denied (Required for STORE command)."); } if (bDeleted) { if (!pConnection->CheckPermission(pConnection->GetCurrentFolder(), ACLPermission::PermissionWriteDeleted)) return IMAPResult(IMAPResult::ResultNo, "ACL: DeleteMessages permission denied (Required for STORE command)."); } if (bDraft || bAnswered || bFlagged) { if (!pConnection->CheckPermission(pConnection->GetCurrentFolder(), ACLPermission::PermissionWriteOthers)) return IMAPResult(IMAPResult::ResultNo, "ACL: WriteOthers permission denied (Required for STORE command)."); } if (sCommand.FindNoCase(_T("-FLAGS")) >= 0) { // Remove flags if (bSeen) pMessage->SetFlagSeen(false); if (bDeleted) pMessage->SetFlagDeleted(false); if (bDraft) pMessage->SetFlagDraft(false); if (bAnswered) pMessage->SetFlagAnswered(false); if (bFlagged) pMessage->SetFlagFlagged(false); } else if (sCommand.FindNoCase(_T("+FLAGS")) >= 0) { // Add flags if (bSeen) pMessage->SetFlagSeen(true); if (bDeleted) pMessage->SetFlagDeleted(true); if (bDraft) pMessage->SetFlagDraft(true); if (bAnswered) pMessage->SetFlagAnswered(true); if (bFlagged) pMessage->SetFlagFlagged(true); } else if (sCommand.FindNoCase(_T("FLAGS")) >= 0) { // Set flags pMessage->SetFlagSeen(bSeen); pMessage->SetFlagDeleted(bDeleted); pMessage->SetFlagDraft(bDraft); pMessage->SetFlagAnswered(bAnswered); pMessage->SetFlagFlagged(bFlagged); } bool result = Application::Instance()->GetFolderManager()->UpdateMessageFlags( (int) pConnection->GetCurrentFolder()->GetAccountID(), (int) pConnection->GetCurrentFolder()->GetID(), pMessage->GetID(), pMessage->GetFlags()); if (!result) { return IMAPResult(IMAPResult::ResultNo, "Unable to store message flags."); } if (!bSilent) { pConnection->SendAsciiData(GetMessageFlags(pMessage, messageIndex)); } // BEGIN IMAP IDLE // Notify the mailbox notifier that the mailbox contents have changed. std::vector<__int64> effectedMessages; effectedMessages.push_back(pMessage->GetID()); std::shared_ptr<ChangeNotification> pNotification = std::shared_ptr<ChangeNotification>(new ChangeNotification(pConnection->GetCurrentFolder()->GetAccountID(), pConnection->GetCurrentFolder()->GetID(), ChangeNotification::NotificationMessageFlagsChanged, effectedMessages)); Application::Instance()->GetNotificationServer()->SendNotification(pConnection->GetNotificationClient(), pNotification); // END IMAP IDLE return IMAPResult(); }