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 )
45 moveToThread( thread );
50 SessionThread::~SessionThread()
53 if ( !thread()->wait( 10 * 1000 ) ) {
54 kWarning() <<
"Session thread refuses to die, killing harder...";
63 void SessionThread::sendData(
const QByteArray &payload )
67 m_dataQueue.enqueue( payload );
72 void SessionThread::writeDataQueue()
79 while ( !m_dataQueue.isEmpty() ) {
80 m_socket->write( m_dataQueue.dequeue() );
85 void SessionThread::readMessage()
88 if ( !m_stream || m_stream->availableDataSize() == 0 ) {
96 while ( !m_stream->atCommandEnd() ) {
97 if ( m_stream->hasString() ) {
99 if (
string ==
"NIL" ) {
100 *payload << Message::Part( QList<QByteArray>() );
102 *payload << Message::Part(
string );
104 }
else if ( m_stream->hasList() ) {
105 *payload << Message::Part( m_stream->readParenthesizedList() );
106 }
else if ( m_stream->hasResponseCode() ) {
107 payload = &message.responseCode;
108 }
else if ( m_stream->atResponseCodeEnd() ) {
109 payload = &message.content;
110 }
else if ( m_stream->hasLiteral() ) {
112 while ( !m_stream->atLiteralEnd() ) {
113 literal += m_stream->readLiteralPart();
115 *payload << Message::Part( literal );
119 qWarning(
"Inconsistent state, probably due to some packet loss" );
125 emit responseReceived( message );
127 }
catch ( KIMAP::ImapParserException e ) {
128 qWarning() <<
"The stream parser raised an exception:" << e.what();
131 if ( m_stream->availableDataSize() > 1 ) {
138 void SessionThread::closeSocket()
144 void SessionThread::doCloseSocket()
149 m_encryptedMode =
false;
155 void SessionThread::reconnect()
160 if ( m_socket->state() != SessionSocket::ConnectedState &&
161 m_socket->state() != SessionSocket::ConnectingState ) {
162 if ( m_encryptedMode ) {
163 kDebug() <<
"connectToHostEncrypted" << m_hostName << m_port;
164 m_socket->connectToHostEncrypted( m_hostName, m_port );
166 kDebug() <<
"connectToHost" << m_hostName << m_port;
167 m_socket->connectToHost( m_hostName, m_port );
173 void SessionThread::threadInit()
176 m_socket =
new SessionSocket;
178 connect( m_socket, SIGNAL(readyRead()),
179 this, SLOT(readMessage()), Qt::QueuedConnection );
182 connect( m_socket, SIGNAL(disconnected()),
183 this, SLOT(slotSocketDisconnected()), Qt::QueuedConnection );
184 connect( m_socket, SIGNAL(connected()),
185 this, SIGNAL(socketConnected()) );
186 connect( m_socket, SIGNAL(error(KTcpSocket::Error)),
187 this, SLOT(slotSocketError(KTcpSocket::Error)) );
188 connect( m_socket, SIGNAL(bytesWritten(qint64)),
189 this, SIGNAL(socketActivity()) );
190 if ( m_socket->metaObject()->indexOfSignal(
"encryptedBytesWritten(qint64)" ) > -1 ) {
191 connect( m_socket, SIGNAL(encryptedBytesWritten(qint64)),
192 this, SIGNAL(socketActivity()) );
194 connect( m_socket, SIGNAL(readyRead()),
195 this, SIGNAL(socketActivity()) );
201 void SessionThread::threadQuit()
212 void SessionThread::startSsl( KTcpSocket::SslVersion version )
218 void SessionThread::doStartSsl( KTcpSocket::SslVersion version )
224 m_socket->setAdvertisedSslVersion( version );
225 m_socket->ignoreSslErrors();
226 connect( m_socket, SIGNAL(encrypted()),
this, SLOT(sslConnected()) );
227 m_socket->startClientEncryption();
231 void SessionThread::slotSocketDisconnected()
234 emit socketDisconnected();
238 void SessionThread::slotSocketError(KTcpSocket::Error error)
244 emit socketError(error);
248 void SessionThread::sslConnected()
253 KSslCipher cipher = m_socket->sessionCipher();
255 if ( m_socket->sslErrors().count() > 0 ||
256 m_socket->encryptionMode() != KTcpSocket::SslClientMode ||
257 cipher.isNull() || cipher.usedBits() == 0 ) {
258 kDebug() <<
"Initial SSL handshake failed. cipher.isNull() is" << cipher.isNull()
259 <<
", cipher.usedBits() is" << cipher.usedBits()
260 <<
", the socket says:" << m_socket->errorString()
261 <<
"and the list of SSL errors contains"
262 << m_socket->sslErrors().count() <<
"items.";
263 KSslErrorUiData errorData( m_socket );
264 emit sslError( errorData );
266 kDebug() <<
"TLS negotiation done.";
267 m_encryptedMode =
true;
268 emit encryptionNegotiationResult(
true, m_socket->negotiatedSslVersion() );
272 void SessionThread::sslErrorHandlerResponse(
bool response)
278 void SessionThread::doSslErrorHandlerResponse(
bool response)
284 m_encryptedMode =
true;
285 emit encryptionNegotiationResult(
true, m_socket->negotiatedSslVersion() );
287 m_encryptedMode =
false;
289 m_socket->disconnectFromHost();
290 m_socket->waitForDisconnected();
291 m_socket->connectToHost( m_hostName, m_port );
292 emit encryptionNegotiationResult(
false, KTcpSocket::UnknownSslVersion );
296 #include "moc_sessionthread_p.cpp"
Parser for IMAP messages that operates on a local socket stream.
void start(Priority priority)
bool wait(unsigned long time)
QThread * currentThread()