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_mutex( QMutex::Recursive ),
41 m_encryptedMode( false ),
42 triedSslVersions( 0 ), doSslFallback( false )
50 SessionThread::~SessionThread()
53 QMetaObject::invokeMethod(
this,
"quit" );
54 if ( !wait( 10 * 1000 ) ) {
55 kWarning() <<
"Session thread refuses to die, killing harder...";
62 void SessionThread::sendData(
const QByteArray &payload )
64 QMutexLocker locker( &m_mutex );
66 m_dataQueue.enqueue( payload );
67 QTimer::singleShot( 0,
this, SLOT(writeDataQueue()) );
70 void SessionThread::writeDataQueue()
72 QMutexLocker locker( &m_mutex );
74 while ( !m_dataQueue.isEmpty() ) {
75 m_socket->write( m_dataQueue.dequeue() );
79 void SessionThread::readMessage()
81 QMutexLocker locker( &m_mutex );
83 if ( m_stream->availableDataSize() == 0 ) {
88 QList<Message::Part> *payload = &message.content;
91 while ( !m_stream->atCommandEnd() ) {
92 if ( m_stream->hasString() ) {
93 QByteArray
string = m_stream->readString();
94 if (
string ==
"NIL" ) {
95 *payload << Message::Part( QList<QByteArray>() );
97 *payload << Message::Part(
string );
99 }
else if ( m_stream->hasList() ) {
100 *payload << Message::Part( m_stream->readParenthesizedList() );
101 }
else if ( m_stream->hasResponseCode() ) {
102 payload = &message.responseCode;
103 }
else if ( m_stream->atResponseCodeEnd() ) {
104 payload = &message.content;
105 }
else if ( m_stream->hasLiteral() ) {
107 while ( !m_stream->atLiteralEnd() ) {
108 literal += m_stream->readLiteralPart();
110 *payload << Message::Part( literal );
114 qWarning(
"Inconsistent state, probably due to some packet loss" );
120 emit responseReceived( message );
122 }
catch ( KIMAP::ImapParserException e ) {
123 qWarning() <<
"The stream parser raised an exception:" << e.what();
126 if ( m_stream->availableDataSize() > 1 ) {
127 QTimer::singleShot( 0,
this, SLOT(readMessage()) );
132 void SessionThread::closeSocket()
134 QTimer::singleShot( 0,
this, SLOT(doCloseSocket()) );
137 void SessionThread::doCloseSocket()
139 m_encryptedMode =
false;
143 void SessionThread::reconnect()
145 QMutexLocker locker( &m_mutex );
147 if ( m_socket->state() != SessionSocket::ConnectedState &&
148 m_socket->state() != SessionSocket::ConnectingState ) {
149 if ( m_encryptedMode ) {
150 m_socket->connectToHostEncrypted( m_hostName, m_port );
152 m_socket->connectToHost( m_hostName, m_port );
157 void SessionThread::run()
159 m_socket =
new SessionSocket;
161 connect( m_socket, SIGNAL(readyRead()),
162 this, SLOT(readMessage()), Qt::QueuedConnection );
164 connect( m_socket, SIGNAL(disconnected()),
165 this, SLOT(socketDisconnected()) );
166 connect( m_socket, SIGNAL(connected()),
167 m_session, SLOT(socketConnected()) );
168 connect( m_socket, SIGNAL(error(KTcpSocket::Error)),
169 this, SLOT(socketError()) );
170 connect( m_socket, SIGNAL(bytesWritten(qint64)),
171 m_session, SLOT(socketActivity()) );
172 if ( m_socket->metaObject()->indexOfSignal(
"encryptedBytesWritten(qint64)" ) > -1 ) {
173 connect( m_socket, SIGNAL(encryptedBytesWritten(qint64)),
174 m_session, SLOT(socketActivity()) );
176 connect( m_socket, SIGNAL(readyRead()),
177 m_session, SLOT(socketActivity()) );
179 connect(
this, SIGNAL(responseReceived(KIMAP::Message)),
180 m_session, SLOT(responseReceived(KIMAP::Message)) );
182 QTimer::singleShot( 0,
this, SLOT(reconnect()) );
189 void SessionThread::startSsl(
const KTcpSocket::SslVersion &version)
191 QMutexLocker locker( &m_mutex );
193 if ( version == KTcpSocket::AnySslVersion ) {
194 doSslFallback =
true;
195 if ( m_socket->advertisedSslVersion() == KTcpSocket::UnknownSslVersion ) {
196 m_socket->setAdvertisedSslVersion( KTcpSocket::AnySslVersion );
197 }
else if ( !( triedSslVersions & KTcpSocket::TlsV1 ) ) {
198 triedSslVersions |= KTcpSocket::TlsV1;
199 m_socket->setAdvertisedSslVersion( KTcpSocket::TlsV1 );
200 }
else if ( !( triedSslVersions & KTcpSocket::SslV3 ) ) {
201 triedSslVersions |= KTcpSocket::SslV3;
202 m_socket->setAdvertisedSslVersion( KTcpSocket::SslV3 );
203 }
else if ( !( triedSslVersions & KTcpSocket::SslV2 ) ) {
204 triedSslVersions |= KTcpSocket::SslV2;
205 m_socket->setAdvertisedSslVersion( KTcpSocket::SslV2 );
206 doSslFallback =
false;
209 m_socket->setAdvertisedSslVersion( version );
212 m_socket->ignoreSslErrors();
213 connect( m_socket, SIGNAL(encrypted()),
this, SLOT(sslConnected()) );
214 m_socket->startClientEncryption();
217 void SessionThread::socketDisconnected()
219 if ( doSslFallback ) {
222 QMetaObject::invokeMethod( m_session,
"socketDisconnected" );
226 void SessionThread::socketError()
228 QMutexLocker locker( &m_mutex );
229 if ( doSslFallback ) {
231 m_socket->disconnectFromHost();
233 QMetaObject::invokeMethod( m_session,
"socketError" );
237 void SessionThread::sslConnected()
239 QMutexLocker locker( &m_mutex );
240 KSslCipher cipher = m_socket->sessionCipher();
242 if ( m_socket->sslErrors().count() > 0 ||
243 m_socket->encryptionMode() != KTcpSocket::SslClientMode ||
244 cipher.isNull() || cipher.usedBits() == 0 ) {
245 kDebug() <<
"Initial SSL handshake failed. cipher.isNull() is" << cipher.isNull()
246 <<
", cipher.usedBits() is" << cipher.usedBits()
247 <<
", the socket says:" << m_socket->errorString()
248 <<
"and the list of SSL errors contains"
249 << m_socket->sslErrors().count() <<
"items.";
250 KSslErrorUiData errorData( m_socket );
251 emit sslError( errorData );
253 doSslFallback =
false;
254 kDebug() <<
"TLS negotiation done.";
255 m_encryptedMode =
true;
256 emit encryptionNegotiationResult(
true, m_socket->negotiatedSslVersion() );
260 void SessionThread::sslErrorHandlerResponse(
bool response)
262 QMutexLocker locker( &m_mutex );
264 m_encryptedMode =
true;
265 emit encryptionNegotiationResult(
true, m_socket->negotiatedSslVersion() );
267 m_encryptedMode =
false;
269 m_socket->disconnectFromHost();
270 m_socket->waitForDisconnected();
271 m_socket->connectToHost( m_hostName, m_port );
272 emit encryptionNegotiationResult(
false, KTcpSocket::UnknownSslVersion );
276 #include "moc_sessionthread_p.cpp"