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

Skip menu "kdepimlibs-4.9.1 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