• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.9.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 "imapstreamparser.h"
28 #include "message_p.h"
29 #include "session.h"
30 
31 using namespace KIMAP;
32 
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>();
37 
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_encryptedMode( false ),
41  triedSslVersions( 0 ), doSslFallback( false )
42 {
43  // Yeah, sounds weird, but QThread object is linked to the parent
44  // thread not to itself, and I'm too lazy to introduce yet another
45  // internal QObject
46  moveToThread(this);
47 }
48 
49 SessionThread::~SessionThread()
50 {
51  // don't call quit() directly, this will deadlock in wait() if exec() hasn't run yet
52  QMetaObject::invokeMethod( this, "quit" );
53  if ( !wait( 10 * 1000 ) ) {
54  kWarning() << "Session thread refuses to die, killing harder...";
55  terminate();
56  // Make sure to wait until it's done, otherwise it can crash when the pthread callback is called
57  wait();
58  }
59 }
60 
61 void SessionThread::sendData( const QByteArray &payload )
62 {
63  QMutexLocker locker(&m_mutex);
64 
65  m_dataQueue.enqueue( payload );
66  QTimer::singleShot( 0, this, SLOT(writeDataQueue()) );
67 }
68 
69 void SessionThread::writeDataQueue()
70 {
71  QMutexLocker locker(&m_mutex);
72 
73  while ( !m_dataQueue.isEmpty() ) {
74  m_socket->write( m_dataQueue.dequeue() );
75  }
76 }
77 
78 void SessionThread::readMessage()
79 {
80  QMutexLocker locker(&m_mutex);
81 
82  if ( m_stream->availableDataSize()==0 ) {
83  return;
84  }
85 
86  Message message;
87  QList<Message::Part> *payload = &message.content;
88 
89  try {
90  while ( !m_stream->atCommandEnd() ) {
91  if ( m_stream->hasString() ) {
92  QByteArray string = m_stream->readString();
93  if ( string == "NIL" ) {
94  *payload << Message::Part( QList<QByteArray>() );
95  } else {
96  *payload << Message::Part(string);
97  }
98  } else if ( m_stream->hasList() ) {
99  *payload << Message::Part(m_stream->readParenthesizedList());
100  } else if ( m_stream->hasResponseCode() ) {
101  payload = &message.responseCode;
102  } else if ( m_stream->atResponseCodeEnd() ) {
103  payload = &message.content;
104  } else if ( m_stream->hasLiteral() ) {
105  QByteArray literal;
106  while ( !m_stream->atLiteralEnd() ) {
107  literal+= m_stream->readLiteralPart();
108  }
109  *payload << Message::Part(literal);
110  } else {
111  // Oops! Something really bad happened, we won't be able to recover
112  // so close the socket immediately
113  qWarning( "Inconsistent state, probably due to some packet loss" );
114  doCloseSocket();
115  return;
116  }
117  }
118 
119  emit responseReceived(message);
120 
121  } catch (KIMAP::ImapParserException e) {
122  qWarning() << "The stream parser raised an exception:" << e.what();
123  }
124 
125  if ( m_stream->availableDataSize()>1 ) {
126  QTimer::singleShot( 0, this, SLOT(readMessage()) );
127  }
128 
129 }
130 
131 void SessionThread::closeSocket()
132 {
133  QTimer::singleShot( 0, this, SLOT(doCloseSocket()) );
134 }
135 
136 void SessionThread::doCloseSocket()
137 {
138  m_encryptedMode = false;
139  m_socket->close();
140 }
141 
142 void SessionThread::reconnect()
143 {
144  QMutexLocker locker(&m_mutex);
145 
146  if ( m_socket->state() != SessionSocket::ConnectedState &&
147  m_socket->state() != SessionSocket::ConnectingState ) {
148  if (m_encryptedMode) {
149  m_socket->connectToHostEncrypted(m_hostName, m_port);
150  } else {
151  m_socket->connectToHost(m_hostName, m_port);
152  }
153  }
154 }
155 
156 void SessionThread::run()
157 {
158  m_socket = new SessionSocket;
159  m_stream = new ImapStreamParser( m_socket );
160  connect( m_socket, SIGNAL(readyRead()),
161  this, SLOT(readMessage()), Qt::QueuedConnection );
162 
163  connect( m_socket, SIGNAL(disconnected()),
164  this, SLOT(socketDisconnected()) );
165  connect( m_socket, SIGNAL(connected()),
166  m_session, SLOT(socketConnected()) );
167  connect( m_socket, SIGNAL(error(KTcpSocket::Error)),
168  this, SLOT(socketError()) );
169  connect( m_socket, SIGNAL(bytesWritten(qint64)),
170  m_session, SLOT(socketActivity()) );
171  if ( m_socket->metaObject()->indexOfSignal("encryptedBytesWritten(qint64)" ) > -1 ) {
172  connect( m_socket, SIGNAL(encryptedBytesWritten(qint64)), // needs kdelibs > 4.8
173  m_session, SLOT(socketActivity()) );
174  }
175  connect( m_socket, SIGNAL(readyRead()),
176  m_session, SLOT(socketActivity()) );
177 
178  connect( this, SIGNAL(responseReceived(KIMAP::Message)),
179  m_session, SLOT(responseReceived(KIMAP::Message)) );
180 
181  QTimer::singleShot( 0, this, SLOT(reconnect()) );
182  exec();
183 
184  delete m_stream;
185  delete m_socket;
186 }
187 
188 void SessionThread::startSsl(const KTcpSocket::SslVersion &version)
189 {
190  QMutexLocker locker(&m_mutex);
191 
192  if ( version == KTcpSocket::AnySslVersion ) {
193  doSslFallback = true;
194  if ( m_socket->advertisedSslVersion() == KTcpSocket::UnknownSslVersion ) {
195  m_socket->setAdvertisedSslVersion( KTcpSocket::AnySslVersion );
196  } else if ( !( triedSslVersions & KTcpSocket::TlsV1 ) ) {
197  triedSslVersions |= KTcpSocket::TlsV1;
198  m_socket->setAdvertisedSslVersion( KTcpSocket::TlsV1 );
199  } else if ( !( triedSslVersions & KTcpSocket::SslV3 ) ) {
200  triedSslVersions |= KTcpSocket::SslV3;
201  m_socket->setAdvertisedSslVersion( KTcpSocket::SslV3 );
202  } else if ( !( triedSslVersions & KTcpSocket::SslV2 ) ) {
203  triedSslVersions |= KTcpSocket::SslV2;
204  m_socket->setAdvertisedSslVersion( KTcpSocket::SslV2 );
205  doSslFallback = false;
206  }
207  } else {
208  m_socket->setAdvertisedSslVersion( version );
209  }
210 
211  m_socket->ignoreSslErrors();
212  connect(m_socket, SIGNAL(encrypted()), this, SLOT(sslConnected()));
213  m_socket->startClientEncryption();
214 }
215 
216 void SessionThread::socketDisconnected()
217 {
218  if ( doSslFallback ) {
219  reconnect();
220  } else {
221  QMetaObject::invokeMethod( m_session, "socketDisconnected" );
222  }
223 }
224 
225 void SessionThread::socketError()
226 {
227  QMutexLocker locker( &m_mutex );
228  if ( doSslFallback ) {
229  locker.unlock(); // disconnectFromHost() ends up calling reconnect()
230  m_socket->disconnectFromHost();
231  } else {
232  QMetaObject::invokeMethod( m_session, "socketError" );
233  }
234 }
235 
236 void SessionThread::sslConnected()
237 {
238  QMutexLocker locker(&m_mutex);
239  KSslCipher cipher = m_socket->sessionCipher();
240 
241  if ( m_socket->sslErrors().count() > 0 || m_socket->encryptionMode() != KTcpSocket::SslClientMode
242  || cipher.isNull() || cipher.usedBits() == 0) {
243  kDebug() << "Initial SSL handshake failed. cipher.isNull() is" << cipher.isNull()
244  << ", cipher.usedBits() is" << cipher.usedBits()
245  << ", the socket says:" << m_socket->errorString()
246  << "and the list of SSL errors contains"
247  << m_socket->sslErrors().count() << "items.";
248  KSslErrorUiData errorData(m_socket);
249  emit sslError(errorData);
250  } else {
251  doSslFallback = false;
252  kDebug() << "TLS negotiation done.";
253  m_encryptedMode = true;
254  emit encryptionNegotiationResult(true, m_socket->negotiatedSslVersion());
255  }
256 }
257 
258 void SessionThread::sslErrorHandlerResponse(bool response)
259 {
260  QMutexLocker locker(&m_mutex);
261  if (response) {
262  m_encryptedMode = true;
263  emit encryptionNegotiationResult(true, m_socket->negotiatedSslVersion());
264  } else {
265  m_encryptedMode = false;
266  //reconnect in unencrypted mode, so new commands can be issued
267  m_socket->disconnectFromHost();
268  m_socket->waitForDisconnected();
269  m_socket->connectToHost(m_hostName, m_port);
270  emit encryptionNegotiationResult(false, KTcpSocket::UnknownSslVersion);
271  }
272 }
273 
274 #include "sessionthread_p.moc"
275 
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jan 5 2013 19:44:10 by doxygen 1.8.1.2 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.9.5 API Reference

Skip menu "kdepimlibs-4.9.5 API Reference"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • 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