📄 imapconnection.cpp
字号:
if (respDataList[i]->continue_req()) { response = respDataList[i]->continue_req()->resp_text()->text(); hasResponse = true; break; } } if (!hasResponse) { cont = false; continue; } byte_t* challenge = 0; int challengeLen = 0; byte_t* resp = 0; int respLen = 0; try { // Extract challenge saslContext->decodeB64(response, &challenge, &challengeLen); // Prepare response saslSession->evaluateChallenge (challenge, challengeLen, &resp, &respLen); // Send response send(false, saslContext->encodeB64(resp, respLen), true); } catch (exceptions::sasl_exception& e) { if (challenge) { delete [] challenge; challenge = NULL; } if (resp) { delete [] resp; resp = NULL; } // Cancel SASL exchange send(false, "*", true); } catch (...) { if (challenge) delete [] challenge; if (resp) delete [] resp; throw; } if (challenge) delete [] challenge; if (resp) delete [] resp; } } } throw exceptions::authentication_error ("Could not authenticate using SASL: all mechanisms failed.");}#endif // VMIME_HAVE_SASL_SUPPORT#if VMIME_HAVE_TLS_SUPPORTvoid IMAPConnection::startTLS(){ try { send(true, "STARTTLS", true); utility::auto_ptr <IMAPParser::response> resp(m_parser->readResponse()); if (resp->isBad() || resp->response_done()->response_tagged()-> resp_cond_state()->status() != IMAPParser::resp_cond_state::OK) { throw exceptions::command_error ("STARTTLS", m_parser->lastLine(), "bad response"); } ref <tls::TLSSession> tlsSession = vmime::create <tls::TLSSession>(m_store.acquire()->getCertificateVerifier()); ref <tls::TLSSocket> tlsSocket = tlsSession->getSocket(m_socket); tlsSocket->handshake(m_timeoutHandler); m_socket = tlsSocket; m_parser->setSocket(m_socket); m_secured = true; m_cntInfos = vmime::create <tls::TLSSecuredConnectionInfos> (m_cntInfos->getHost(), m_cntInfos->getPort(), tlsSession, tlsSocket); } catch (exceptions::command_error&) { // Non-fatal error throw; } catch (exception&) { // Fatal error internalDisconnect(); throw; }}#endif // VMIME_HAVE_TLS_SUPPORTconst std::vector <string> IMAPConnection::getCapabilities(){ send(true, "CAPABILITY", true); utility::auto_ptr <IMAPParser::response> resp(m_parser->readResponse()); std::vector <string> res; if (resp->response_done()->response_tagged()-> resp_cond_state()->status() == IMAPParser::resp_cond_state::OK) { const std::vector <IMAPParser::continue_req_or_response_data*>& respDataList = resp->continue_req_or_response_data(); for (unsigned int i = 0 ; i < respDataList.size() ; ++i) { if (respDataList[i]->response_data() == NULL) continue; const IMAPParser::capability_data* capaData = respDataList[i]->response_data()->capability_data(); if (capaData == NULL) continue; std::vector <IMAPParser::capability*> caps = capaData->capabilities(); for (unsigned int j = 0 ; j < caps.size() ; ++j) { if (caps[j]->auth_type()) res.push_back("AUTH=" + caps[j]->auth_type()->name()); else res.push_back(caps[j]->atom()->value()); } } } return res;}ref <security::authenticator> IMAPConnection::getAuthenticator(){ return m_auth;}bool IMAPConnection::isConnected() const{ return (m_socket && m_socket->isConnected() && (m_state == STATE_AUTHENTICATED || m_state == STATE_SELECTED));}bool IMAPConnection::isSecuredConnection() const{ return m_secured;}ref <connectionInfos> IMAPConnection::getConnectionInfos() const{ return m_cntInfos;}void IMAPConnection::disconnect(){ if (!isConnected()) throw exceptions::not_connected(); internalDisconnect();}void IMAPConnection::internalDisconnect(){ if (isConnected()) { send(true, "LOGOUT", true); m_socket->disconnect(); m_socket = NULL; } m_timeoutHandler = NULL; m_state = STATE_LOGOUT; m_secured = false; m_cntInfos = NULL;}void IMAPConnection::initHierarchySeparator(){ send(true, "LIST \"\" \"\"", true); vmime::utility::auto_ptr <IMAPParser::response> resp(m_parser->readResponse()); if (resp->isBad() || resp->response_done()->response_tagged()-> resp_cond_state()->status() != IMAPParser::resp_cond_state::OK) { internalDisconnect(); throw exceptions::command_error("LIST", m_parser->lastLine(), "bad response"); } const std::vector <IMAPParser::continue_req_or_response_data*>& respDataList = resp->continue_req_or_response_data(); bool found = false; for (unsigned int i = 0 ; !found && i < respDataList.size() ; ++i) { if (respDataList[i]->response_data() == NULL) continue; const IMAPParser::mailbox_data* mailboxData = static_cast <const IMAPParser::response_data*> (respDataList[i]->response_data())->mailbox_data(); if (mailboxData == NULL || mailboxData->type() != IMAPParser::mailbox_data::LIST) continue; if (mailboxData->mailbox_list()->quoted_char() != '\0') { m_hierarchySeparator = mailboxData->mailbox_list()->quoted_char(); found = true; } } if (!found) // default m_hierarchySeparator = '/';}void IMAPConnection::send(bool tag, const string& what, bool end){#if VMIME_DEBUG std::ostringstream oss; if (tag) { ++(*m_tag); oss << string(*m_tag); oss << " "; } oss << what; if (end) oss << "\r\n"; m_socket->send(oss.str());#else if (tag) { ++(*m_tag); m_socket->send(*m_tag); m_socket->send(" "); } m_socket->send(what); if (end) { m_socket->send("\r\n"); }#endif}void IMAPConnection::sendRaw(const char* buffer, const int count){ m_socket->sendRaw(buffer, count);}IMAPParser::response* IMAPConnection::readResponse(IMAPParser::literalHandler* lh){ return (m_parser->readResponse(lh));}IMAPConnection::ProtocolStates IMAPConnection::state() const{ return (m_state);}void IMAPConnection::setState(const ProtocolStates state){ m_state = state;}char IMAPConnection::hierarchySeparator() const{ return (m_hierarchySeparator);}ref <const IMAPTag> IMAPConnection::getTag() const{ return (m_tag);}ref <const IMAPParser> IMAPConnection::getParser() const{ return (m_parser);}ref <const IMAPStore> IMAPConnection::getStore() const{ return m_store.acquire();}ref <IMAPStore> IMAPConnection::getStore(){ return m_store.acquire();}ref <session> IMAPConnection::getSession(){ return m_store.acquire()->getSession();}} // imap} // net} // vmime
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -