16using namespace ::
Binc;
23IMAPServer::IMAPServer(
int argc,
char **argv)
27 this->stubMode =
false;
28 Session::getInstance().setState(Session::AUTHENTICATED);
31IMAPServer::~IMAPServer(
void)
35int IMAPServer::initialize(
void)
37 Session &session = Session::getInstance();
38 if (!session.
initialize(argc, argv))
return 111;
42void IMAPServer::prepareForNextRequest(
void)
48 bincClient.setMaxInputBufferSize(INPUT_BUFFER_LIMIT);
50 Session::getInstance().setLastError(
"");
51 Session::getInstance().clearResponseCode();
54int IMAPServer::runStub(
void)
56 bincDebug <<
"IMAPServer::runStub(), running stub" << endl;
57 this->stubMode =
true;
58 Session::getInstance().setState(Session::NONAUTHENTICATED);
62int IMAPServer::run(
void)
64 Session &session = Session::getInstance();
65 bincLog.setOutputLevelLimit(IODevice::InfoLevel);
66 string pid = to_string(session.
getPid());
67 std::string ip = session.
getEnv(
"TCPREMOTEIP");
68 std::string userID = session.
getEnv(
"AUTH_USER");
70 bincDebug <<
"IMAPServer::run(), started server" << endl;
73 if (session.
hasEnv(
"PROTOCOLDUMP"))
75 bincLog <<
"bincimap-up: pid " << pid
76 <<
" Connected: " << ip <<
"\n";
79 bincLog <<
"bincimapd: pid " << pid <<
" Session::" << session.
getEnv(
"AUTH") <<
" <"
80 << userID <<
"> from: " << ip <<
"\n";
85 bincDebug <<
"IMAPServer::run(), preparing for next request" << endl;
87 prepareForNextRequest();
93 bincDebug <<
"IMAPServer::run(), found broker " << (uintptr_t) broker
94 <<
" for state " << session.
getState() << endl;
96 bool skipToNextRequest =
false;
100 int stubParseResult = broker->
parseStub(clientRequest);
101 if (stubParseResult == Operator::TIMEOUT) {
102 serverStatus = Timeout;
104 }
else if (stubParseResult == Operator::REJECT) {
105 serverStatus = RequestRejected;
106 }
else if (stubParseResult == Operator::ERROR) {
107 serverStatus = RequestError;
113 serverStatus = RequestRejected;
114 string err =
"The command \"";
115 if (clientRequest.
getUidMode()) err +=
"UID ";
116 err += clientRequest.
getName();
117 err +=
"\" is unsupported in this state. ";
119 skipToNextRequest =
true;
121 int parseResult = o->
parse(clientRequest);
122 if (parseResult == Operator::TIMEOUT) {
123 serverStatus = Timeout;
124 }
else if (parseResult == Operator::REJECT) {
125 serverStatus = RequestRejected;
126 }
else if (parseResult == Operator::ERROR) {
127 serverStatus = RequestError;
132 int processResult = o->
process(*dep, clientRequest);
133 if (processResult == Operator::OK) {
134 }
else if (processResult == Operator::NO) {
135 serverStatus = RequestRejected;
136 }
else if (processResult == Operator::BAD) {
137 serverStatus = RequestError;
138 }
else if (processResult == Operator::NOTHING) {
139 serverStatus = RequestIgnore;
140 }
else if (processResult == Operator::ABORT) {
149 if (serverStatus == RequestRejected) {
152 << clientRequest.
getName() <<
" failed: "
154 }
else if (serverStatus == RequestError) {
157 skipToNextRequest =
true;
158 }
else if (serverStatus == RequestIgnore) {
160 }
else if (serverStatus == OK && session.
getState() != Session::LOGOUT) {
164 << clientRequest.
getName() <<
" completed";
176 if (skipToNextRequest) {
178 if (
bincClient.getLastError() == IODevice::Timeout) {
179 serverStatus = Timeout;
181 serverStatus = ClientDisconnected;
185 }
while (session.
getState() != Session::LOGOUT);
189 if (this->stubMode) {
190 bincLog <<
"bincimap-up: pid " << pid
194 bincLog <<
"bincimapd: pid " << pid
199 if (serverStatus == Timeout) {
201 <<
" seconds of inactivity\n";
203 bincLog <<
"bincimapd: pid " << pid <<
" Timed out: <" << userID <<
"> after "
204 << IDLE_TIMEOUT <<
"s" <<
"\n";
205 }
else if (serverStatus == ServerStatus::ClientDisconnected) {
206 bincLog <<
"bincimapd: pid " << pid <<
" Disconnected: " << ip <<
"\n";
208 if (this->stubMode) {
209 bincLog <<
"bincimap-up: pid " << pid <<
" Disconnected: " << ip <<
"\n";
211 bincLog <<
"bincimapd: pid " << pid <<
" Logged out: <" << userID <<
">\n";
Declaration of the Broker class.
Broker * getBroker(int state)
Operator * get(const std::string &name) const
Operator::ParseResult parseStub(Request &cmd)
virtual ParseResult parse(Request &) const =0
virtual ProcessResult process(Depot &, Request &)=0
const std::string & getTag(void) const
const std::string & getName(void) const
bool getUidMode(void) const
const std::string & getContextInfo(void) const
const int getState(void) const
bool initialize(int argc, char *argv[])
void setLastError(const std::string &error) const
const std::string & getLastError(void) const
Depot * getDepot(void) const
int getBodies(void) const
int getWriteBytes(void) const
int getReadBytes(void) const
int getStatements(void) const
const std::string & getResponseCode(void) const
std::string getEnv(const std::string &key)
bool hasEnv(const std::string &key) const
Declaration of the common items for parsing IMAP input.
Declaration of the IMAPServer class.
Declaration of the IODevice class.
Declaration of the IOFactory class.