• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.10.5 API Reference
  • KDE Home
  • Contact Us
 

KIMAP Library

  • kimap
sessionthread.cpp
1 /*
2  Copyright (c) 2009 Kevin Ottens <ervin@kde.org>
3 
4  This library is free software; you can redistribute it and/or modify it
5  under the terms of the GNU Library General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or (at your
7  option) any later version.
8 
9  This library is distributed in the hope that it will be useful, but WITHOUT
10  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12  License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to the
16  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  02110-1301, USA.
18 */
19 
20 #include "sessionthread_p.h"
21 
22 #include <QtCore/QDebug>
23 #include <QtCore/QTimer>
24 
25 #include <KDE/KDebug>
26 
27 #include <cstdio>
28 
29 #include "imapstreamparser.h"
30 #include "message_p.h"
31 #include "session.h"
32 
33 using namespace KIMAP;
34 
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>();
39 
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 )
47 {
48  // Yeah, sounds weird, but QThread object is linked to the parent
49  // thread not to itself, and I'm too lazy to introduce yet another
50  // internal QObject
51  moveToThread( this );
52 }
53 
54 SessionThread::~SessionThread()
55 {
56  // don't call quit() directly, this will deadlock in wait() if exec() hasn't run yet
57  QMetaObject::invokeMethod( this, "quit" );
58  if ( !wait( 10 * 1000 ) ) {
59  kWarning() << "Session thread refuses to die, killing harder...";
60  terminate();
61  // Make sure to wait until it's done, otherwise it can crash when the pthread callback is called
62  wait();
63  }
64 }
65 
66 void SessionThread::sendData( const QByteArray &payload )
67 {
68  QMutexLocker locker( &m_mutex );
69 
70  m_dataQueue.enqueue( payload );
71  QTimer::singleShot( 0, this, SLOT(writeDataQueue()) );
72 }
73 
74 void SessionThread::writeDataQueue()
75 {
76  QMutexLocker locker( &m_mutex );
77 
78  while ( !m_dataQueue.isEmpty() ) {
79  m_socket->write( m_dataQueue.dequeue() );
80  }
81 }
82 
83 void SessionThread::readMessage()
84 {
85  QMutexLocker locker( &m_mutex );
86 
87  if ( m_stream->availableDataSize() == 0 ) {
88  return;
89  }
90 
91  Message message;
92  QList<Message::Part> *payload = &message.content;
93 
94  try {
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>() );
100  } else {
101  *payload << Message::Part( string );
102  }
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() ) {
110  QByteArray literal;
111  while ( !m_stream->atLiteralEnd() ) {
112  literal += m_stream->readLiteralPart();
113  }
114  *payload << Message::Part( literal );
115  } else {
116  // Oops! Something really bad happened, we won't be able to recover
117  // so close the socket immediately
118  qWarning( "Inconsistent state, probably due to some packet loss" );
119  doCloseSocket();
120  return;
121  }
122  }
123 
124  emit responseReceived( message );
125 
126  } catch ( KIMAP::ImapParserException e ) {
127  qWarning() << "The stream parser raised an exception:" << e.what();
128  }
129 
130  if ( m_stream->availableDataSize() > 1 ) {
131  QTimer::singleShot( 0, this, SLOT(readMessage()) );
132  }
133 
134 }
135 
136 void SessionThread::closeSocket()
137 {
138  QTimer::singleShot( 0, this, SLOT(doCloseSocket()) );
139 }
140 
141 void SessionThread::doCloseSocket()
142 {
143  m_encryptedMode = false;
144  m_socket->close();
145 }
146 
147 void SessionThread::reconnect()
148 {
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 );
155  } else {
156  m_socket->connectToHost( m_hostName, m_port );
157  }
158  }
159 }
160 
161 void SessionThread::run()
162 {
163  m_socket = new SessionSocket;
164  m_stream = new ImapStreamParser( m_socket );
165  connect( m_socket, SIGNAL(readyRead()),
166  this, SLOT(readMessage()), Qt::QueuedConnection );
167 
168  // Delay the call to socketDisconnected so that it finishes disconnecting before we call reconnect()
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)), // needs kdelibs > 4.8
179  m_session, SLOT(socketActivity()) );
180  }
181  connect( m_socket, SIGNAL(readyRead()),
182  m_session, SLOT(socketActivity()) );
183 
184  connect( this, SIGNAL(responseReceived(KIMAP::Message)),
185  m_session, SLOT(responseReceived(KIMAP::Message)) );
186 
187  QTimer::singleShot( 0, this, SLOT(reconnect()) );
188  exec();
189 
190  delete m_stream;
191  delete m_socket;
192 }
193 
194 void SessionThread::startSsl(const KTcpSocket::SslVersion &version)
195 {
196  QMutexLocker locker( &m_mutex );
197 
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;
212  }
213  } else {
214  m_socket->setAdvertisedSslVersion( version );
215  }
216 
217  m_socket->ignoreSslErrors();
218  connect( m_socket, SIGNAL(encrypted()), this, SLOT(sslConnected()) );
219  m_socket->startClientEncryption();
220 }
221 
222 void SessionThread::socketDisconnected()
223 {
224  if ( doSslFallback ) {
225  reconnect();
226  } else {
227  QMetaObject::invokeMethod( m_session, "socketDisconnected" );
228  }
229  m_alreadyHandlingErrors = false;
230 }
231 
232 void SessionThread::socketError()
233 {
234  QMutexLocker locker( &m_mutex );
235  if ( doSslFallback && !m_alreadyHandlingErrors ) {
236  m_alreadyHandlingErrors = true;
237  locker.unlock(); // disconnectFromHost() ends up calling reconnect()
238  m_socket->disconnectFromHost();
239  } else {
240  QMetaObject::invokeMethod( m_session, "socketError" );
241  }
242 }
243 
244 void SessionThread::sslConnected()
245 {
246  QMutexLocker locker( &m_mutex );
247  KSslCipher cipher = m_socket->sessionCipher();
248 
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 );
259  } else {
260  doSslFallback = false;
261  kDebug() << "TLS negotiation done.";
262  m_encryptedMode = true;
263  emit encryptionNegotiationResult( true, m_socket->negotiatedSslVersion() );
264  }
265 }
266 
267 void SessionThread::sslErrorHandlerResponse(bool response)
268 {
269  QMutexLocker locker( &m_mutex );
270  if ( response ) {
271  m_encryptedMode = true;
272  emit encryptionNegotiationResult( true, m_socket->negotiatedSslVersion() );
273  } else {
274  m_encryptedMode = false;
275  //reconnect in unencrypted mode, so new commands can be issued
276  m_socket->disconnectFromHost();
277  m_socket->waitForDisconnected();
278  m_socket->connectToHost( m_hostName, m_port );
279  emit encryptionNegotiationResult( false, KTcpSocket::UnknownSslVersion );
280  }
281 }
282 
283 #include "moc_sessionthread_p.cpp"
284 
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jul 13 2013 01:25:17 by doxygen 1.8.3.1 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KIMAP Library

Skip menu "KIMAP Library"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Related Pages

kdepimlibs-4.10.5 API Reference

Skip menu "kdepimlibs-4.10.5 API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal