kmail

kmservertest.cpp
1 /*
2  kmservertest.cpp
3 
4  This file is part of KMail, the KDE mail client.
5  Copyright (c) 2001-2002 Michael Haeckel <haeckel@kde.org>
6  Copyright (c) 2003 Marc Mutz <mutz@kde.org>
7 
8  KMail is free software; you can redistribute it and/or modify it
9  under the terms of the GNU General Public License, version 2, as
10  published by the Free Software Foundation.
11 
12  KMail is distributed in the hope that it will be useful, but
13  WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 
21  In addition, as a special exception, the copyright holders give
22  permission to link the code of this program with any edition of
23  the TQt library by Trolltech AS, Norway (or with modified versions
24  of TQt that use the same license as TQt), and distribute linked
25  combinations including the two. You must obey the GNU General
26  Public License in all respects for all of the code used other than
27  TQt. If you modify this file, you may extend this exception to
28  your version of the file, but you are not obligated to do so. If
29  you do not wish to do so, delete this exception statement from
30  your version.
31 */
32 
33 #include <config.h>
34 
35 #include "kmservertest.h"
36 
37 #include <tdelocale.h>
38 #include <tdemessagebox.h>
39 #include <kdebug.h>
40 #include <kurl.h>
41 #include <tdeapplication.h>
42 #include <tdeio/scheduler.h>
43 #include <tdeio/slave.h>
44 #include <tdeio/job.h>
45 #include <tdeio/global.h>
46 
47 //-----------------------------------------------------------------------------
48 KMServerTest::KMServerTest( const TQString & protocol, const TQString & host, int port )
49  : TQObject(),
50  mProtocol( protocol ), mHost( host ),
51  mSSL( false ), mJob( 0 ), mSlave( 0 ), mConnectionErrorCount( 0 )
52 {
53  TDEIO::Scheduler::connect(
54  TQ_SIGNAL(slaveError(TDEIO::Slave *, int, const TQString &)),
55  this, TQ_SLOT(slotSlaveResult(TDEIO::Slave *, int, const TQString &)));
56 
57  if ( port == 993 || port == 995 || port == 465 )
58  port = 0;
59 
60  startOffSlave( port );
61 }
62 
63 //-----------------------------------------------------------------------------
64 KMServerTest::~KMServerTest()
65 {
66  if (mJob) mJob->kill(TRUE);
67 }
68 
69 
70 TDEIO::MetaData KMServerTest::slaveConfig() const {
71  TDEIO::MetaData md;
72  md.insert( "nologin", "on" );
73  return md;
74 }
75 
76 void KMServerTest::startOffSlave( int port ) {
77  KURL url;
78  url.setProtocol( mSSL ? mProtocol + 's' : mProtocol );
79  url.setHost( mHost );
80  if ( port )
81  url.setPort( port );
82 
83  mSlave = TDEIO::Scheduler::getConnectedSlave( url, slaveConfig() );
84  if ( !mSlave ) {
85  slotSlaveResult( 0, 1 );
86  return;
87  }
88  connect( mSlave, TQ_SIGNAL(metaData(const TDEIO::MetaData&)),
89  TQ_SLOT(slotMetaData(const TDEIO::MetaData&)) );
90 
91  TQByteArray packedArgs;
92  TQDataStream stream( packedArgs, IO_WriteOnly );
93 
94  stream << (int) 'c';
95 
96  mJob = TDEIO::special( url, packedArgs, false );
97  TDEIO::Scheduler::assignJobToSlave( mSlave, mJob );
98  connect( mJob, TQ_SIGNAL(result(TDEIO::Job*)), TQ_SLOT(slotResult(TDEIO::Job*)) );
99  connect( mJob, TQ_SIGNAL(infoMessage(TDEIO::Job*,const TQString&)),
100  TQ_SLOT(slotData(TDEIO::Job*,const TQString&)) );
101 }
102 
103 
104 //-----------------------------------------------------------------------------
105 void KMServerTest::slotData(TDEIO::Job *, const TQString &data)
106 {
107  if ( mSSL )
108  mListSSL = TQStringList::split(' ', data);
109  else
110  mListNormal = TQStringList::split(' ', data);
111 }
112 
113 
114 void KMServerTest::slotMetaData( const TDEIO::MetaData & md ) {
115  TDEIO::MetaData::const_iterator it = md.find( "PLAIN AUTH METHODS" );
116  if ( it != md.end() ) {
117  mAuthNone = it.data();
118  kdDebug(5006) << "mAuthNone: " << mAuthNone << endl;
119  }
120  it = md.find( "TLS AUTH METHODS" );
121  if ( it != md.end() ) {
122  mAuthTLS = it.data();
123  kdDebug(5006) << "mAuthTLS: " << mAuthTLS << endl;
124  }
125  it = md.find( "SSL AUTH METHODS" );
126  if ( it != md.end() ) {
127  mAuthSSL = it.data();
128  kdDebug(5006) << "mAuthSSL: " << mAuthSSL << endl;
129  }
130 }
131 
132 //-----------------------------------------------------------------------------
133 void KMServerTest::slotResult(TDEIO::Job *job)
134 {
135  slotSlaveResult(mSlave, job->error());
136 }
137 
138 //-----------------------------------------------------------------------------
139 void KMServerTest::slotSlaveResult(TDEIO::Slave *aSlave, int error,
140  const TQString &errorText)
141 {
142  if (aSlave != mSlave) return;
143  if ( mSSL && error == 0 ) {
144  // add a dummy entry to the list of SSL capabilities so that the receiver
145  // of the capabilities signal can use mListSSL.isEmpty() in order to find
146  // out whether SSL is supported
147  mListSSL.append("SSL");
148  }
149 
150  if (error != TDEIO::ERR_SLAVE_DIED && mSlave)
151  {
152  // disconnect slave after every connect
153  TDEIO::Scheduler::disconnectSlave(mSlave);
154  mSlave = 0;
155  }
156  if ( error == TDEIO::ERR_COULD_NOT_CONNECT )
157  {
158  // if one of the two connection tests fails we ignore the error
159  // if both fail the host is probably not correct so we display the error
160  if ( mConnectionErrorCount == 0 )
161  {
162  error = 0;
163  }
164  ++mConnectionErrorCount;
165  }
166  if ( error )
167  {
168  mJob = 0;
169  KMessageBox::error( kapp->activeWindow(),
170  TDEIO::buildErrorString( error, errorText ),
171  i18n("Error") );
172  emit capabilities( mListNormal, mListSSL );
173  emit capabilities( mListNormal, mListSSL, mAuthNone, mAuthSSL, mAuthTLS );
174  return;
175  }
176  if (!mSSL) {
177  mSSL = true;
178  mListNormal.append("NORMAL-CONNECTION");
179  startOffSlave();
180  } else {
181  mJob = 0;
182 
183  emit capabilities( mListNormal, mListSSL );
184  emit capabilities( mListNormal, mListSSL, mAuthNone, mAuthSSL, mAuthTLS );
185  }
186 }
187 
188 
189 #include "kmservertest.moc"