20 #include "sessionthread_p.h"
22 #include <QtCore/QDebug>
23 #include <QtCore/QThread>
27 #include "imapstreamparser.h"
28 #include "message_p.h"
30 using namespace KIMAP;
32 Q_DECLARE_METATYPE( KTcpSocket::Error )
33 Q_DECLARE_METATYPE( KSslErrorUiData )
34 static const
int _kimap_socketErrorTypeId = qRegisterMetaType<KTcpSocket::Error>();
35 static const
int _kimap_sslErrorUiData = qRegisterMetaType<KSslErrorUiData>();
37 SessionThread::SessionThread( const QString &hostName, quint16 port )
38 : QObject(), m_hostName( hostName ), m_port( port ),
39 m_socket( 0 ), m_stream( 0 ), m_mutex(),
40 m_encryptedMode( false ),
41 triedSslVersions( 0 ), doSslFallback( false )
45 QThread* thread =
new QThread();
46 moveToThread( thread );
48 QMetaObject::invokeMethod(
this,
"threadInit" );
51 SessionThread::~SessionThread()
53 QMetaObject::invokeMethod(
this,
"threadQuit" );
54 if ( !thread()->wait( 10 * 1000 ) ) {
55 kWarning() <<
"Session thread refuses to die, killing harder...";
56 thread()->terminate();
64 void SessionThread::sendData(
const QByteArray &payload )
66 QMutexLocker locker( &m_mutex );
68 m_dataQueue.enqueue( payload );
69 QMetaObject::invokeMethod(
this,
"writeDataQueue" );
73 void SessionThread::writeDataQueue()
75 Q_ASSERT( QThread::currentThread() == thread() );
78 QMutexLocker locker( &m_mutex );
80 while ( !m_dataQueue.isEmpty() ) {
81 m_socket->write( m_dataQueue.dequeue() );
86 void SessionThread::readMessage()
88 Q_ASSERT( QThread::currentThread() == thread() );
89 if ( !m_stream || m_stream->availableDataSize() == 0 ) {
94 QList<Message::Part> *payload = &message.content;
97 while ( !m_stream->atCommandEnd() ) {
98 if ( m_stream->hasString() ) {
99 QByteArray
string = m_stream->readString();
100 if (
string ==
"NIL" ) {
101 *payload << Message::Part( QList<QByteArray>() );
103 *payload << Message::Part(
string );
105 }
else if ( m_stream->hasList() ) {
106 *payload << Message::Part( m_stream->readParenthesizedList() );
107 }
else if ( m_stream->hasResponseCode() ) {
108 payload = &message.responseCode;
109 }
else if ( m_stream->atResponseCodeEnd() ) {
110 payload = &message.content;
111 }
else if ( m_stream->hasLiteral() ) {
113 while ( !m_stream->atLiteralEnd() ) {
114 literal += m_stream->readLiteralPart();
116 *payload << Message::Part( literal );
120 qWarning(
"Inconsistent state, probably due to some packet loss" );
126 emit responseReceived( message );
128 }
catch ( KIMAP::ImapParserException e ) {
129 qWarning() <<
"The stream parser raised an exception:" << e.what();
132 if ( m_stream->availableDataSize() > 1 ) {
133 QMetaObject::invokeMethod(
this,
"readMessage", Qt::QueuedConnection );
139 void SessionThread::closeSocket()
141 QMetaObject::invokeMethod(
this,
"doCloseSocket", Qt::QueuedConnection );
145 void SessionThread::doCloseSocket()
147 Q_ASSERT( QThread::currentThread() == thread() );
150 m_encryptedMode =
false;
156 void SessionThread::reconnect()
158 Q_ASSERT( QThread::currentThread() == thread() );
161 if ( m_socket->state() != SessionSocket::ConnectedState &&
162 m_socket->state() != SessionSocket::ConnectingState ) {
163 if ( m_encryptedMode ) {
164 kDebug() <<
"connectToHostEncrypted" << m_hostName << m_port;
165 m_socket->connectToHostEncrypted( m_hostName, m_port );
167 kDebug() <<
"connectToHost" << m_hostName << m_port;
168 m_socket->connectToHost( m_hostName, m_port );
174 void SessionThread::threadInit()
176 Q_ASSERT( QThread::currentThread() == thread() );
177 m_socket =
new SessionSocket;
179 connect( m_socket, SIGNAL(readyRead()),
180 this, SLOT(readMessage()), Qt::QueuedConnection );
183 connect( m_socket, SIGNAL(disconnected()),
184 this, SLOT(slotSocketDisconnected()), Qt::QueuedConnection );
185 connect( m_socket, SIGNAL(connected()),
186 this, SIGNAL(socketConnected()) );
187 connect( m_socket, SIGNAL(error(KTcpSocket::Error)),
188 this, SLOT(socketError(KTcpSocket::Error)) );
189 connect( m_socket, SIGNAL(bytesWritten(qint64)),
190 this, SIGNAL(socketActivity()) );
191 if ( m_socket->metaObject()->indexOfSignal(
"encryptedBytesWritten(qint64)" ) > -1 ) {
192 connect( m_socket, SIGNAL(encryptedBytesWritten(qint64)),
193 this, SIGNAL(socketActivity()) );
195 connect( m_socket, SIGNAL(readyRead()),
196 this, SIGNAL(socketActivity()) );
198 QMetaObject::invokeMethod(
this,
"reconnect", Qt::QueuedConnection);
202 void SessionThread::threadQuit()
204 Q_ASSERT( QThread::currentThread() == thread() );
213 void SessionThread::startSsl( KTcpSocket::SslVersion version )
215 QMetaObject::invokeMethod(
this,
"doStartSsl", Q_ARG(KTcpSocket::SslVersion, version) );
219 void SessionThread::doStartSsl( KTcpSocket::SslVersion version )
221 Q_ASSERT( QThread::currentThread() == thread() );
224 if ( version == KTcpSocket::AnySslVersion ) {
225 doSslFallback =
true;
226 if ( m_socket->advertisedSslVersion() == KTcpSocket::UnknownSslVersion ) {
227 m_socket->setAdvertisedSslVersion( KTcpSocket::AnySslVersion );
228 }
else if ( !( triedSslVersions & KTcpSocket::TlsV1 ) ) {
229 triedSslVersions |= KTcpSocket::TlsV1;
230 m_socket->setAdvertisedSslVersion( KTcpSocket::TlsV1 );
231 }
else if ( !( triedSslVersions & KTcpSocket::SslV3 ) ) {
232 triedSslVersions |= KTcpSocket::SslV3;
233 m_socket->setAdvertisedSslVersion( KTcpSocket::SslV3 );
234 }
else if ( !( triedSslVersions & KTcpSocket::SslV2 ) ) {
235 triedSslVersions |= KTcpSocket::SslV2;
236 m_socket->setAdvertisedSslVersion( KTcpSocket::SslV2 );
237 doSslFallback =
false;
240 m_socket->setAdvertisedSslVersion( version );
243 m_socket->ignoreSslErrors();
244 connect( m_socket, SIGNAL(encrypted()),
this, SLOT(sslConnected()) );
245 m_socket->startClientEncryption();
249 void SessionThread::slotSocketDisconnected()
251 Q_ASSERT( QThread::currentThread() == thread() );
252 if ( doSslFallback ) {
255 emit socketDisconnected();
260 void SessionThread::socketError(KTcpSocket::Error error)
262 Q_ASSERT( QThread::currentThread() == thread() );
266 if ( doSslFallback ) {
275 void SessionThread::sslConnected()
277 doSslFallback =
false;
278 Q_ASSERT( QThread::currentThread() == thread() );
281 KSslCipher cipher = m_socket->sessionCipher();
283 if ( m_socket->sslErrors().count() > 0 ||
284 m_socket->encryptionMode() != KTcpSocket::SslClientMode ||
285 cipher.isNull() || cipher.usedBits() == 0 ) {
286 kDebug() <<
"Initial SSL handshake failed. cipher.isNull() is" << cipher.isNull()
287 <<
", cipher.usedBits() is" << cipher.usedBits()
288 <<
", the socket says:" << m_socket->errorString()
289 <<
"and the list of SSL errors contains"
290 << m_socket->sslErrors().count() <<
"items.";
291 KSslErrorUiData errorData( m_socket );
292 emit sslError( errorData );
294 kDebug() <<
"TLS negotiation done.";
295 m_encryptedMode =
true;
296 emit encryptionNegotiationResult(
true, m_socket->negotiatedSslVersion() );
300 void SessionThread::sslErrorHandlerResponse(
bool response)
302 QMetaObject::invokeMethod(
this,
"doSslErrorHandlerResponse", Q_ARG(
bool, response));
306 void SessionThread::doSslErrorHandlerResponse(
bool response)
308 Q_ASSERT( QThread::currentThread() == thread() );
312 m_encryptedMode =
true;
313 emit encryptionNegotiationResult(
true, m_socket->negotiatedSslVersion() );
315 m_encryptedMode =
false;
317 m_socket->disconnectFromHost();
318 m_socket->waitForDisconnected();
319 m_socket->connectToHost( m_hostName, m_port );
320 emit encryptionNegotiationResult(
false, KTcpSocket::UnknownSslVersion );
324 #include "moc_sessionthread_p.cpp"
Parser for IMAP messages that operates on a local socket stream.