17using namespace ::
Binc;
24IMAPServer::IMAPServer(
int argc,
char **argv)
28 this->stubMode =
false;
29 Session::getInstance().setState(Session::AUTHENTICATED);
32IMAPServer::~IMAPServer(
void)
36int IMAPServer::initialize(
void)
38 Session &session = Session::getInstance();
39 if (!session.
initialize(argc, argv))
return 111;
43void IMAPServer::prepareForNextRequest(
void)
49 bincClient.setMaxInputBufferSize(INPUT_BUFFER_LIMIT);
51 Session::getInstance().setLastError(
"");
52 Session::getInstance().clearResponseCode();
55int IMAPServer::runStub(
void)
57 bincDebug <<
"IMAPServer::runStub(), running stub" << endl;
58 this->stubMode =
true;
59 Session::getInstance().setState(Session::NONAUTHENTICATED);
63int IMAPServer::run(
void)
65 Session &session = Session::getInstance();
66 bincLog.setOutputLevelLimit(IODevice::InfoLevel);
67 string pid = to_string(session.
getPid());
68 std::string ip = session.
getEnv(
"TCPREMOTEIP");
69 std::string userID = session.
getEnv(
"AUTH_USER");
71 bincDebug <<
"IMAPServer::run(), started server" << endl;
74 if (session.
hasEnv(
"PROTOCOLDUMP"))
76 bincLog <<
"bincimap-up: pid " << pid
77 <<
" Connected: " << ip <<
"\n";
80 bincLog <<
"bincimapd: pid " << pid <<
" Session::" << session.
getEnv(
"AUTH") <<
" <"
81 << userID <<
"> from: " << ip <<
"\n";
86 bincDebug <<
"IMAPServer::run(), preparing for next request" << endl;
88 prepareForNextRequest();
94 bincDebug <<
"IMAPServer::run(), found broker " << (uintptr_t) broker
95 <<
" for state " << session.
getState() << endl;
97 bool skipToNextRequest =
false;
101 int stubParseResult = broker->
parseStub(clientRequest);
102 if (stubParseResult == Operator::TIMEOUT) {
103 serverStatus = Timeout;
105 }
else if (stubParseResult == Operator::REJECT) {
106 serverStatus = RequestRejected;
107 }
else if (stubParseResult == Operator::ERROR) {
108 serverStatus = RequestError;
114 serverStatus = RequestRejected;
115 string err =
"The command \"";
116 if (clientRequest.
getUidMode()) err +=
"UID ";
117 err += clientRequest.
getName();
118 err +=
"\" is unsupported in this state. ";
120 skipToNextRequest =
true;
122 int parseResult = o->
parse(clientRequest);
123 if (parseResult == Operator::TIMEOUT) {
124 serverStatus = Timeout;
125 }
else if (parseResult == Operator::REJECT) {
126 serverStatus = RequestRejected;
127 }
else if (parseResult == Operator::ERROR) {
128 serverStatus = RequestError;
133 int processResult = o->
process(*dep, clientRequest);
134 if (processResult == Operator::OK) {
135 }
else if (processResult == Operator::NO) {
136 serverStatus = RequestRejected;
137 }
else if (processResult == Operator::BAD) {
138 serverStatus = RequestError;
139 }
else if (processResult == Operator::NOTHING) {
140 serverStatus = RequestIgnore;
141 }
else if (processResult == Operator::ABORT) {
150 if (serverStatus == RequestRejected) {
153 << clientRequest.
getName() <<
" failed: "
155 }
else if (serverStatus == RequestError) {
158 skipToNextRequest =
true;
159 }
else if (serverStatus == RequestIgnore) {
161 }
else if (serverStatus == OK && session.
getState() != Session::LOGOUT) {
165 << clientRequest.
getName() <<
" completed";
177 if (skipToNextRequest) {
179 if (
bincClient.getLastError() == IODevice::Timeout) {
180 serverStatus = Timeout;
182 serverStatus = ClientDisconnected;
186 }
while (session.
getState() != Session::LOGOUT);
190 if (this->stubMode) {
191 bincLog <<
"bincimap-up: pid " << pid
195 bincLog <<
"bincimapd: pid " << pid
200 if (serverStatus == Timeout) {
202 <<
" seconds of inactivity\n";
204 bincLog <<
"bincimapd: pid " << pid <<
" Timed out: <" << userID <<
"> after "
205 << IDLE_TIMEOUT <<
"s";
206 }
else if (serverStatus == ServerStatus::ClientDisconnected) {
207 bincLog <<
"bincimapd: pid " << pid <<
" Disconnected: " << ip <<
"\n";
209 if (this->stubMode) {
210 bincLog <<
"bincimap-up: pid " << pid <<
" Disconnected: " << ip <<
"\n";
212 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.