20 #include "sessionthread_p.h"
22 #include <QtCore/QDebug>
23 #include <QtCore/QTimer>
27 #include "imapstreamparser.h"
28 #include "message_p.h"
31 using namespace KIMAP;
33 Q_DECLARE_METATYPE(KTcpSocket::Error)
34 Q_DECLARE_METATYPE(KSslErrorUiData)
35 static const
int _kimap_socketErrorTypeId = qRegisterMetaType<KTcpSocket::Error>();
36 static const
int _kimap_sslErrorUiData = qRegisterMetaType<KSslErrorUiData>();
38 SessionThread::SessionThread( const QString &hostName, quint16 port, Session *parent )
39 : QThread(), m_hostName(hostName), m_port(port),
40 m_session(parent), m_socket(0), m_stream(0), m_encryptedMode(false)
48 SessionThread::~SessionThread()
51 QMetaObject::invokeMethod(
this,
"quit" );
52 if ( !wait( 10 * 1000 ) ) {
53 kWarning() <<
"Session thread refuses to die, killing harder...";
60 void SessionThread::sendData(
const QByteArray &payload )
62 QMutexLocker locker(&m_mutex);
64 m_dataQueue.enqueue( payload );
65 QTimer::singleShot( 0,
this, SLOT(writeDataQueue()) );
68 void SessionThread::writeDataQueue()
70 QMutexLocker locker(&m_mutex);
72 while ( !m_dataQueue.isEmpty() ) {
73 m_socket->write( m_dataQueue.dequeue() );
77 void SessionThread::readMessage()
79 QMutexLocker locker(&m_mutex);
81 if ( m_stream->availableDataSize()==0 ) {
86 QList<Message::Part> *payload = &message.content;
89 while ( !m_stream->atCommandEnd() ) {
90 if ( m_stream->hasString() ) {
91 QByteArray
string = m_stream->readString();
92 if (
string ==
"NIL" ) {
93 *payload << Message::Part( QList<QByteArray>() );
95 *payload << Message::Part(
string);
97 }
else if ( m_stream->hasList() ) {
98 *payload << Message::Part(m_stream->readParenthesizedList());
99 }
else if ( m_stream->hasResponseCode() ) {
100 payload = &message.responseCode;
101 }
else if ( m_stream->atResponseCodeEnd() ) {
102 payload = &message.content;
103 }
else if ( m_stream->hasLiteral() ) {
105 while ( !m_stream->atLiteralEnd() ) {
106 literal+= m_stream->readLiteralPart();
108 *payload << Message::Part(literal);
112 qWarning(
"Inconsistent state, probably due to some packet loss" );
118 emit responseReceived(message);
120 }
catch (KIMAP::ImapParserException e) {
121 qWarning() <<
"The stream parser raised an exception:" << e.what();
124 if ( m_stream->availableDataSize()>1 ) {
125 QTimer::singleShot( 0,
this, SLOT(readMessage()) );
130 void SessionThread::closeSocket()
132 QTimer::singleShot( 0,
this, SLOT(doCloseSocket()) );
135 void SessionThread::doCloseSocket()
137 m_encryptedMode =
false;
141 void SessionThread::reconnect()
143 QMutexLocker locker(&m_mutex);
145 if ( m_socket->state() != SessionSocket::ConnectedState &&
146 m_socket->state() != SessionSocket::ConnectingState ) {
147 if (m_encryptedMode) {
148 m_socket->connectToHostEncrypted(m_hostName, m_port);
150 m_socket->connectToHost(m_hostName, m_port);
155 void SessionThread::run()
157 m_socket =
new SessionSocket;
159 connect( m_socket, SIGNAL(readyRead()),
160 this, SLOT(readMessage()), Qt::QueuedConnection );
162 connect( m_socket, SIGNAL(disconnected()),
163 m_session, SLOT(socketDisconnected()) );
164 connect( m_socket, SIGNAL(connected()),
165 m_session, SLOT(socketConnected()) );
166 connect( m_socket, SIGNAL(error(KTcpSocket::Error)),
167 m_session, SLOT(socketError()) );
168 connect( m_socket, SIGNAL(bytesWritten(qint64)),
169 m_session, SLOT(socketActivity()) );
170 if ( m_socket->metaObject()->indexOfSignal(
"encryptedBytesWritten(qint64)" ) > -1 ) {
171 connect( m_socket, SIGNAL(encryptedBytesWritten(qint64)),
172 m_session, SLOT(socketActivity()) );
174 connect( m_socket, SIGNAL(readyRead()),
175 m_session, SLOT(socketActivity()) );
177 connect(
this, SIGNAL(responseReceived(KIMAP::Message)),
178 m_session, SLOT(responseReceived(KIMAP::Message)) );
180 QTimer::singleShot( 0,
this, SLOT(reconnect()) );
187 void SessionThread::startSsl(
const KTcpSocket::SslVersion &version)
189 QMutexLocker locker(&m_mutex);
191 m_socket->setAdvertisedSslVersion(version);
192 m_socket->ignoreSslErrors();
193 connect(m_socket, SIGNAL(encrypted()),
this, SLOT(sslConnected()));
194 m_socket->startClientEncryption();
197 void SessionThread::sslConnected()
199 QMutexLocker locker(&m_mutex);
200 KSslCipher cipher = m_socket->sessionCipher();
202 if ( m_socket->sslErrors().count() > 0 || m_socket->encryptionMode() != KTcpSocket::SslClientMode
203 || cipher.isNull() || cipher.usedBits() == 0) {
204 kDebug() <<
"Initial SSL handshake failed. cipher.isNull() is" << cipher.isNull()
205 <<
", cipher.usedBits() is" << cipher.usedBits()
206 <<
", the socket says:" << m_socket->errorString()
207 <<
"and the list of SSL errors contains"
208 << m_socket->sslErrors().count() <<
"items.";
209 KSslErrorUiData errorData(m_socket);
210 emit sslError(errorData);
212 kDebug() <<
"TLS negotiation done.";
213 m_encryptedMode =
true;
214 emit encryptionNegotiationResult(
true, m_socket->negotiatedSslVersion());
218 void SessionThread::sslErrorHandlerResponse(
bool response)
220 QMutexLocker locker(&m_mutex);
222 m_encryptedMode =
true;
223 emit encryptionNegotiationResult(
true, m_socket->negotiatedSslVersion());
225 m_encryptedMode =
false;
227 m_socket->disconnectFromHost();
228 m_socket->waitForDisconnected();
229 m_socket->connectToHost(m_hostName, m_port);
230 emit encryptionNegotiationResult(
false, KTcpSocket::UnknownSslVersion);
234 #include "sessionthread_p.moc"