File indexing completed on 2024-05-12 05:17:26

0001 /**
0002   * This file is part of the KDE project
0003   * Copyright (C) 2009 Kevin Ottens <ervin@kde.org>
0004   *
0005   * This library is free software; you can redistribute it and/or
0006   * modify it under the terms of the GNU Library General Public
0007   * License as published by the Free Software Foundation; either
0008   * version 2 of the License, or (at your option) any later version.
0009   *
0010   * This library is distributed in the hope that it will be useful,
0011   * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013   * Library General Public License for more details.
0014   *
0015   * You should have received a copy of the GNU Library General Public License
0016   * along with this library; see the file COPYING.LIB.  If not, write to
0017   * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018   * Boston, MA 02110-1301, USA.
0019   */
0020 
0021 #include <qdebug.h>
0022 #include <QCoreApplication>
0023 #include <qsignalspy.h>
0024 
0025 #include "session.h"
0026 #include "capabilitiesjob.h"
0027 #include "idlejob.h"
0028 #include "loginjob.h"
0029 #include "logoutjob.h"
0030 #include "selectjob.h"
0031 #include "closejob.h"
0032 
0033 using namespace KIMAP2;
0034 
0035 int main(int argc, char **argv)
0036 {
0037     QCoreApplication::setApplicationName(QStringLiteral("TestImapIdle"));
0038 
0039     if (argc < 4) {
0040         qCritical() << "Not enough parameters, expecting: <server> <user> <password>";
0041     }
0042 
0043     QString server = QString::fromLocal8Bit(argv[1]);
0044     int port = 143;
0045     if (server.count(QLatin1Char(':')) == 1) {
0046         port = server.split(QLatin1Char(':')).last().toInt();
0047         server = server.split(QLatin1Char(':')).first();
0048     }
0049     QString user = QString::fromLocal8Bit(argv[2]);
0050     QString password = QString::fromLocal8Bit(argv[3]);
0051 
0052     qDebug() << "Listening:" << server << port << user << password;
0053     qDebug();
0054 
0055     QCoreApplication app(argc, argv);
0056     Session session(server, port);
0057 
0058     QObject::connect(&session, &KIMAP2::Session::sslErrors, [&session](const QList<QSslError> &errors) {
0059         qWarning() << "Got ssl error: " << errors;
0060         session.ignoreErrors(errors);
0061     });
0062 
0063     qDebug() << "Logging in...";
0064     LoginJob *login = new LoginJob(&session);
0065     login->setUserName(user);
0066     login->setPassword(password);
0067     login->exec();
0068     Q_ASSERT_X(login->error() == 0, "LoginJob", login->errorString().toLocal8Bit().constData());
0069     Q_ASSERT(session.state() == Session::Authenticated);
0070     qDebug();
0071 
0072     qDebug() << "Asking for capabilities:";
0073     CapabilitiesJob *capabilities = new CapabilitiesJob(&session);
0074     capabilities->exec();
0075     Q_ASSERT_X(capabilities->error() == 0, "CapabilitiesJob", capabilities->errorString().toLocal8Bit().constData());
0076     Q_ASSERT(session.state() == Session::Authenticated);
0077     qDebug() << capabilities->capabilities();
0078     qDebug();
0079 
0080     Q_ASSERT(capabilities->capabilities().contains(QStringLiteral("IDLE")));
0081 
0082     qDebug() << "Selecting INBOX:";
0083     SelectJob *select = new SelectJob(&session);
0084     select->setMailBox(QStringLiteral("INBOX"));
0085     select->exec();
0086     Q_ASSERT_X(select->error() == 0, "SelectJob", select->errorString().toLocal8Bit().constData());
0087     Q_ASSERT(session.state() == Session::Selected);
0088     qDebug() << "Flags:" << select->flags();
0089     qDebug() << "Permanent flags:" << select->permanentFlags();
0090     qDebug() << "Total Number of Messages:" << select->messageCount();
0091     qDebug() << "Number of recent Messages:" << select->recentCount();
0092     qDebug() << "First Unseen Message Index:" << select->firstUnseenIndex();
0093     qDebug() << "UID validity:" << select->uidValidity();
0094     qDebug() << "Next UID:" << select->nextUid();
0095     qDebug();
0096 
0097     qDebug() << "Start idling...";
0098     IdleJob *idle = new IdleJob(&session);
0099     QObject::connect(idle, SIGNAL(mailBoxStats(KIMAP2::IdleJob*,QString,int,int)),
0100                      idle, SLOT(stop()));
0101     idle->exec();
0102     qDebug() << "Idling done for" << idle->lastMailBox()
0103              << "message count:" << idle->lastMessageCount()
0104              << "recent count:" << idle->lastRecentCount();
0105 
0106     qDebug() << "Closing INBOX:";
0107     CloseJob *close = new CloseJob(&session);
0108     close->exec();
0109     Q_ASSERT(session.state() == Session::Authenticated);
0110     qDebug();
0111 
0112     qDebug() << "Logging out...";
0113     LogoutJob *logout = new LogoutJob(&session);
0114     logout->exec();
0115     Q_ASSERT_X(logout->error() == 0, "LogoutJob", logout->errorString().toLocal8Bit().constData());
0116     Q_ASSERT(session.state() == Session::Disconnected);
0117 
0118     return 0;
0119 }