20 #include "sessionthread_p.h"
22 #include <QtCore/QDebug>
23 #include <QtCore/QTimer>
29 #include "imapstreamparser.h"
30 #include "message_p.h"
33 using namespace KIMAP;
35 Q_DECLARE_METATYPE( KTcpSocket::Error )
36 Q_DECLARE_METATYPE( KSslErrorUiData )
37 static const
int _kimap_socketErrorTypeId = qRegisterMetaType<KTcpSocket::Error>();
38 static const
int _kimap_sslErrorUiData = qRegisterMetaType<KSslErrorUiData>();
40 SessionThread::SessionThread( const QString &hostName, quint16 port, Session *parent )
41 : QThread(), m_hostName( hostName ), m_port( port ),
42 m_session( parent ), m_socket( 0 ), m_stream( 0 ), m_mutex( QMutex::Recursive ),
43 m_encryptedMode( false ),
44 triedSslVersions( 0 ),
45 doSslFallback( false ),
46 m_alreadyHandlingErrors( false )
54 SessionThread::~SessionThread()
57 QMetaObject::invokeMethod(
this,
"quit" );
58 if ( !wait( 10 * 1000 ) ) {
59 kWarning() <<
"Session thread refuses to die, killing harder...";
66 void SessionThread::sendData(
const QByteArray &payload )
68 QMutexLocker locker( &m_mutex );
70 m_dataQueue.enqueue( payload );
71 QTimer::singleShot( 0,
this, SLOT(writeDataQueue()) );
74 void SessionThread::writeDataQueue()
76 QMutexLocker locker( &m_mutex );
78 while ( !m_dataQueue.isEmpty() ) {
79 m_socket->write( m_dataQueue.dequeue() );
83 void SessionThread::readMessage()
85 QMutexLocker locker( &m_mutex );
87 if ( m_stream->availableDataSize() == 0 ) {
92 QList<Message::Part> *payload = &message.content;
95 while ( !m_stream->atCommandEnd() ) {
96 if ( m_stream->hasString() ) {
97 QByteArray
string = m_stream->readString();
98 if (
string ==
"NIL" ) {
99 *payload << Message::Part( QList<QByteArray>() );
101 *payload << Message::Part(
string );
103 }
else if ( m_stream->hasList() ) {
104 *payload << Message::Part( m_stream->readParenthesizedList() );
105 }
else if ( m_stream->hasResponseCode() ) {
106 payload = &message.responseCode;
107 }
else if ( m_stream->atResponseCodeEnd() ) {
108 payload = &message.content;
109 }
else if ( m_stream->hasLiteral() ) {
111 while ( !m_stream->atLiteralEnd() ) {
112 literal += m_stream->readLiteralPart();
114 *payload << Message::Part( literal );
118 qWarning(
"Inconsistent state, probably due to some packet loss" );
124 emit responseReceived( message );
126 }
catch ( KIMAP::ImapParserException e ) {
127 qWarning() <<
"The stream parser raised an exception:" << e.what();
130 if ( m_stream->availableDataSize() > 1 ) {
131 QTimer::singleShot( 0,
this, SLOT(readMessage()) );
136 void SessionThread::closeSocket()
138 QTimer::singleShot( 0,
this, SLOT(doCloseSocket()) );
141 void SessionThread::doCloseSocket()
143 m_encryptedMode =
false;
147 void SessionThread::reconnect()
149 QMutexLocker locker( &m_mutex );
150 kDebug() <<
this <<
"state=" << m_socket->state();
151 if ( m_socket->state() != SessionSocket::ConnectedState &&
152 m_socket->state() != SessionSocket::ConnectingState ) {
153 if ( m_encryptedMode ) {
154 m_socket->connectToHostEncrypted( m_hostName, m_port );
156 m_socket->connectToHost( m_hostName, m_port );
161 void SessionThread::run()
163 m_socket =
new SessionSocket;
165 connect( m_socket, SIGNAL(readyRead()),
166 this, SLOT(readMessage()), Qt::QueuedConnection );
169 connect( m_socket, SIGNAL(disconnected()),
170 this, SLOT(socketDisconnected()), Qt::QueuedConnection );
171 connect( m_socket, SIGNAL(connected()),
172 m_session, SLOT(socketConnected()) );
173 connect( m_socket, SIGNAL(error(KTcpSocket::Error)),
174 this, SLOT(socketError()) );
175 connect( m_socket, SIGNAL(bytesWritten(qint64)),
176 m_session, SLOT(socketActivity()) );
177 if ( m_socket->metaObject()->indexOfSignal(
"encryptedBytesWritten(qint64)" ) > -1 ) {
178 connect( m_socket, SIGNAL(encryptedBytesWritten(qint64)),
179 m_session, SLOT(socketActivity()) );
181 connect( m_socket, SIGNAL(readyRead()),
182 m_session, SLOT(socketActivity()) );
184 connect(
this, SIGNAL(responseReceived(KIMAP::Message)),
185 m_session, SLOT(responseReceived(KIMAP::Message)) );
187 QTimer::singleShot( 0,
this, SLOT(reconnect()) );
194 void SessionThread::startSsl(
const KTcpSocket::SslVersion &version)
196 QMutexLocker locker( &m_mutex );
198 if ( version == KTcpSocket::AnySslVersion ) {
199 doSslFallback =
true;
200 if ( m_socket->advertisedSslVersion() == KTcpSocket::UnknownSslVersion ) {
201 m_socket->setAdvertisedSslVersion( KTcpSocket::AnySslVersion );
202 }
else if ( !( triedSslVersions & KTcpSocket::TlsV1 ) ) {
203 triedSslVersions |= KTcpSocket::TlsV1;
204 m_socket->setAdvertisedSslVersion( KTcpSocket::TlsV1 );
205 }
else if ( !( triedSslVersions & KTcpSocket::SslV3 ) ) {
206 triedSslVersions |= KTcpSocket::SslV3;
207 m_socket->setAdvertisedSslVersion( KTcpSocket::SslV3 );
208 }
else if ( !( triedSslVersions & KTcpSocket::SslV2 ) ) {
209 triedSslVersions |= KTcpSocket::SslV2;
210 m_socket->setAdvertisedSslVersion( KTcpSocket::SslV2 );
211 doSslFallback =
false;
214 m_socket->setAdvertisedSslVersion( version );
217 m_socket->ignoreSslErrors();
218 connect( m_socket, SIGNAL(encrypted()),
this, SLOT(sslConnected()) );
219 m_socket->startClientEncryption();
222 void SessionThread::socketDisconnected()
224 if ( doSslFallback ) {
227 QMetaObject::invokeMethod( m_session,
"socketDisconnected" );
229 m_alreadyHandlingErrors =
false;
232 void SessionThread::socketError()
234 QMutexLocker locker( &m_mutex );
235 if ( doSslFallback && !m_alreadyHandlingErrors ) {
236 m_alreadyHandlingErrors =
true;
238 m_socket->disconnectFromHost();
240 QMetaObject::invokeMethod( m_session,
"socketError" );
244 void SessionThread::sslConnected()
246 QMutexLocker locker( &m_mutex );
247 KSslCipher cipher = m_socket->sessionCipher();
249 if ( m_socket->sslErrors().count() > 0 ||
250 m_socket->encryptionMode() != KTcpSocket::SslClientMode ||
251 cipher.isNull() || cipher.usedBits() == 0 ) {
252 kDebug() <<
"Initial SSL handshake failed. cipher.isNull() is" << cipher.isNull()
253 <<
", cipher.usedBits() is" << cipher.usedBits()
254 <<
", the socket says:" << m_socket->errorString()
255 <<
"and the list of SSL errors contains"
256 << m_socket->sslErrors().count() <<
"items.";
257 KSslErrorUiData errorData( m_socket );
258 emit sslError( errorData );
260 doSslFallback =
false;
261 kDebug() <<
"TLS negotiation done.";
262 m_encryptedMode =
true;
263 emit encryptionNegotiationResult(
true, m_socket->negotiatedSslVersion() );
267 void SessionThread::sslErrorHandlerResponse(
bool response)
269 QMutexLocker locker( &m_mutex );
271 m_encryptedMode =
true;
272 emit encryptionNegotiationResult(
true, m_socket->negotiatedSslVersion() );
274 m_encryptedMode =
false;
276 m_socket->disconnectFromHost();
277 m_socket->waitForDisconnected();
278 m_socket->connectToHost( m_hostName, m_port );
279 emit encryptionNegotiationResult(
false, KTcpSocket::UnknownSslVersion );
283 #include "moc_sessionthread_p.cpp"