File indexing completed on 2024-11-24 04:44:17

0001 /*
0002  * SPDX-FileCopyrightText: 2012 Sofia Balicka <balicka@kolabsys.com>
0003  * SPDX-FileCopyrightText: 2013 Christian Mollekopf <mollekopf@kolabsys.com>
0004  *
0005  * SPDX-License-Identifier: LGPL-3.0-or-later
0006  */
0007 
0008 #include "kolabformat/errorhandler.h"
0009 #include "kolabformat/kolabobject.h"
0010 #include <QFile>
0011 #include <QString>
0012 #include <iostream>
0013 #include <kolabformat.h>
0014 #include <string>
0015 #include <vector>
0016 using namespace std;
0017 
0018 KMime::Message::Ptr readMimeFile(const QString &fileName, bool &ok)
0019 {
0020     QFile file(fileName);
0021     ok = file.open(QFile::ReadOnly);
0022     if (!ok) {
0023         cout << "failed to open file: " << fileName.toStdString() << endl;
0024         return {};
0025     }
0026     const QByteArray data = file.readAll();
0027     KMime::Message::Ptr msg = KMime::Message::Ptr(new KMime::Message);
0028     msg->setContent(KMime::CRLFtoLF(data));
0029     msg->parse();
0030     return msg;
0031 }
0032 
0033 int main(int argc, char *argv[])
0034 {
0035     vector<string> inputFiles;
0036     inputFiles.reserve(argc - 1);
0037     for (int i = 1; i < argc; ++i) {
0038         inputFiles.emplace_back(argv[i]);
0039     }
0040     if (inputFiles.empty()) {
0041         cout << "Specify input-file\n";
0042         return -1;
0043     }
0044 
0045     int returnValue = 0;
0046 
0047     cout << endl;
0048 
0049     for (auto it = inputFiles.begin(); it != inputFiles.end(); ++it) {
0050         cout << "File: " << *it << endl;
0051 
0052         bool ok;
0053         KMime::Message::Ptr message = readMimeFile(QString::fromStdString(*it), ok);
0054 
0055         if (!ok) {
0056             returnValue = -1;
0057             cout << endl;
0058             continue;
0059         }
0060 
0061         Kolab::KolabObjectReader reader(message);
0062 
0063         if (Kolab::ErrorHandler::errorOccured()) {
0064             cout << "Errors occurred during parsing." << endl;
0065             returnValue = -1;
0066         } else {
0067             cout << "Parsed message without error." << endl;
0068         }
0069 
0070         cout << endl;
0071     }
0072 
0073     return returnValue;
0074 }