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 );
165 connect( m_socket, SIGNAL(disconnected()),
166 this, SLOT(socketDisconnected()), Qt::QueuedConnection );
167 connect( m_socket, SIGNAL(connected()),
168 m_session, SLOT(socketConnected()) );
169 connect( m_socket, SIGNAL(error(KTcpSocket::Error)),
170 this, SLOT(socketError()) );
171 connect( m_socket, SIGNAL(bytesWritten(qint64)),
172 m_session, SLOT(socketActivity()) );
173 if ( m_socket->metaObject()->indexOfSignal(
"encryptedBytesWritten(qint64)" ) > -1 ) {
174 connect( m_socket, SIGNAL(encryptedBytesWritten(qint64)),
175 m_session, SLOT(socketActivity()) );
177 connect( m_socket, SIGNAL(readyRead()),
178 m_session, SLOT(socketActivity()) );
180 connect(
this, SIGNAL(responseReceived(KIMAP::Message)),
181 m_session, SLOT(responseReceived(KIMAP::Message)) );
183 QTimer::singleShot( 0,
this, SLOT(reconnect()) );
190 void SessionThread::startSsl(
const KTcpSocket::SslVersion &version)
192 QMutexLocker locker( &m_mutex );
194 if ( version == KTcpSocket::AnySslVersion ) {
195 doSslFallback =
true;
196 if ( m_socket->advertisedSslVersion() == KTcpSocket::UnknownSslVersion ) {
197 m_socket->setAdvertisedSslVersion( KTcpSocket::AnySslVersion );
198 }
else if ( !( triedSslVersions & KTcpSocket::TlsV1 ) ) {
199 triedSslVersions |= KTcpSocket::TlsV1;
200 m_socket->setAdvertisedSslVersion( KTcpSocket::TlsV1 );
201 }
else if ( !( triedSslVersions & KTcpSocket::SslV3 ) ) {
202 triedSslVersions |= KTcpSocket::SslV3;
203 m_socket->setAdvertisedSslVersion( KTcpSocket::SslV3 );
204 }
else if ( !( triedSslVersions & KTcpSocket::SslV2 ) ) {
205 triedSslVersions |= KTcpSocket::SslV2;
206 m_socket->setAdvertisedSslVersion( KTcpSocket::SslV2 );
207 doSslFallback =
false;
210 m_socket->setAdvertisedSslVersion( version );
213 m_socket->ignoreSslErrors();
214 connect( m_socket, SIGNAL(encrypted()),
this, SLOT(sslConnected()) );
215 m_socket->startClientEncryption();
218 void SessionThread::socketDisconnected()
220 if ( doSslFallback ) {
223 QMetaObject::invokeMethod( m_session,
"socketDisconnected" );
227 void SessionThread::socketError()
229 QMutexLocker locker( &m_mutex );
230 if ( doSslFallback ) {
232 m_socket->disconnectFromHost();
234 QMetaObject::invokeMethod( m_session,
"socketError" );
238 void SessionThread::sslConnected()
240 QMutexLocker locker( &m_mutex );
241 KSslCipher cipher = m_socket->sessionCipher();
243 if ( m_socket->sslErrors().count() > 0 ||
244 m_socket->encryptionMode() != KTcpSocket::SslClientMode ||
245 cipher.isNull() || cipher.usedBits() == 0 ) {
246 kDebug() <<
"Initial SSL handshake failed. cipher.isNull() is" << cipher.isNull()
247 <<
", cipher.usedBits() is" << cipher.usedBits()
248 <<
", the socket says:" << m_socket->errorString()
249 <<
"and the list of SSL errors contains"
250 << m_socket->sslErrors().count() <<
"items.";
251 KSslErrorUiData errorData( m_socket );
252 emit sslError( errorData );
254 doSslFallback =
false;
255 kDebug() <<
"TLS negotiation done.";
256 m_encryptedMode =
true;
257 emit encryptionNegotiationResult(
true, m_socket->negotiatedSslVersion() );
261 void SessionThread::sslErrorHandlerResponse(
bool response)
263 QMutexLocker locker( &m_mutex );
265 m_encryptedMode =
true;
266 emit encryptionNegotiationResult(
true, m_socket->negotiatedSslVersion() );
268 m_encryptedMode =
false;
270 m_socket->disconnectFromHost();
271 m_socket->waitForDisconnected();
272 m_socket->connectToHost( m_hostName, m_port );
273 emit encryptionNegotiationResult(
false, KTcpSocket::UnknownSslVersion );
277 #include "moc_sessionthread_p.cpp"