/** * @file operator-status.cc * @brief Implementation of the STATUS command. * @author Andreas Aardal Hanssen * @date 2002-2005 */ #include "convert.h" #include "depot.h" #include "iodevice.h" #include "iofactory.h" #include "mailbox.h" #include "operators.h" #include "recursivedescent.h" #include "session.h" #include "status.h" #include #include #include #include #include using namespace Binc; StatusOperator::StatusOperator() {} StatusOperator::~StatusOperator() {} const std::string StatusOperator::getName() const { return "STATUS"; } Session::State StatusOperator::getState() const { return Session::State(Session::AUTHENTICATED | Session::SELECTED); } Operator::ProcessResult StatusOperator::process(Depot &depot, Request &command) { Session &session = Session::getInstance(); Status status; if (!depot.getStatus(command.getMailbox(), status)) { session.setLastError(depot.getLastError()); return ProcessResult::NO; } bincClient << "* STATUS " << toImapString(command.getMailbox()) << " ("; std::string prefix; for (auto tmp : command.statuses) { uppercase(tmp); if (tmp == "UIDNEXT") { bincClient << prefix << "UIDNEXT " << status.getUidNext(); prefix = " "; } else if (tmp == "MESSAGES") { bincClient << prefix << "MESSAGES " << status.getMessages(); prefix = " "; } else if (tmp == "RECENT") { bincClient << prefix << "RECENT " << status.getRecent(); prefix = " "; } else if (tmp == "UIDVALIDITY") { bincClient << prefix << "UIDVALIDITY " << status.getUidValidity(); prefix = " "; } else if (tmp == "UNSEEN") { bincClient << prefix << "UNSEEN " << status.getUnseen(); prefix = " "; } } bincClient << ")" << std::endl; return ProcessResult::OK; } Parser::ParseResult StatusOperator::parse(Request &c_in, Parser &p) { constexpr auto ACCEPT = Parser::ParseResult::ACCEPT; Session &session = Session::getInstance(); if (c_in.getUidMode()) return Parser::ParseResult::REJECT; Parser::ParseResult res; if ((res = p.expectSPACE()) != ACCEPT) { session.setLastError("Expected SPACE"); return res; } std::string mailbox; if ((res = p.expectMailbox(mailbox)) != ACCEPT) { session.setLastError("Expected mailbox"); return res; } c_in.setMailbox(mailbox); if ((res = p.expectSPACE()) != ACCEPT) { session.setLastError("Expected SPACE"); return res; } if ((res = p.expectThisString("(")) != ACCEPT) { session.setLastError("Expected ("); return res; } while (1) { if ((res = p.expectThisString("MESSAGES")) == ACCEPT) { c_in.getStatuses().push_back("MESSAGES"); } else if ((res = p.expectThisString("RECENT")) == ACCEPT) { c_in.getStatuses().push_back("RECENT"); } else if ((res = p.expectThisString("UIDNEXT")) == ACCEPT) { c_in.getStatuses().push_back("UIDNEXT"); } else if ((res = p.expectThisString("UIDVALIDITY")) == ACCEPT) { c_in.getStatuses().push_back("UIDVALIDITY"); } else if ((res = p.expectThisString("UNSEEN")) == ACCEPT) { c_in.getStatuses().push_back("UNSEEN"); } else { session.setLastError("Expected status_att"); return res; } if (p.expectSPACE() != ACCEPT) break; } if ((res = p.expectThisString(")")) != ACCEPT) { session.setLastError("Expected )"); return res; } if ((res = p.expectCRLF()) != ACCEPT) { session.setLastError("Expected CRLF"); return Parser::ParseResult::ERROR; } return ACCEPT; }