File indexing completed on 2024-04-28 16:37:52

0001 #include "kmime_mdn.h"
0002 using namespace KMime::MDN;
0003 
0004 #include <QByteArray>
0005 #include <QString>
0006 
0007 #include <iostream>
0008 using std::cout;
0009 using std::cerr;
0010 #include <cstdlib>
0011 using std::exit;
0012 using std::endl;
0013 
0014 #define _GNU_SOURCE 1
0015 #include <getopt.h>
0016 
0017 void usage(const char *msg = nullptr)
0018 {
0019     if (msg) {
0020         cerr << msg << endl;
0021     }
0022     cerr << "usage: test_mdn <options>\n"
0023          "where options include the following:" << endl
0024          << "FIXME" << endl;
0025     exit(1);
0026 }
0027 
0028 int main(int argc, char *argv[])
0029 {
0030 
0031     QString finalRecipient;
0032     QString originalRecipient;
0033     QByteArray originalMessageId;
0034     ActionMode actionMode = ManualAction;
0035     SendingMode sendingMode = SentManually;
0036     DispositionType dispositionType = Displayed;
0037     QList<DispositionModifier> dispositionModifiers;
0038     QString special;
0039 
0040     while (true) {
0041         int option_index = 0;
0042         static const struct option long_options[] = {
0043             { "action-mode", 1, nullptr, 'a' },
0044             { "disposition-type", 1, nullptr, 'd' },
0045             { "final-recipient", 1, nullptr, 'f' },
0046             { "original-message-id", 1, nullptr, 'i' },
0047             { "disposition-modifiers", 1, nullptr, 'm' },
0048             { "original-recipient", 1, nullptr, 'o' },
0049             { "sending-mode", 1, nullptr, 's' },
0050             { nullptr, 0, nullptr, 0 }
0051         };
0052 
0053         int c = getopt_long(argc, argv, "a:d:f:i:m:o:s:",
0054                             long_options, &option_index);
0055         if (c == -1) {
0056             break;
0057         }
0058 
0059 #define EQUALS(x) !qstricmp( optarg, x )
0060 
0061         switch (c) {
0062 
0063         case 'a': // --action-mode
0064             if (EQUALS("manual-action")) {
0065                 actionMode = ManualAction;
0066             } else if (EQUALS("automatic-action")) {
0067                 actionMode = AutomaticAction;
0068             } else {
0069                 usage("unknown action mode!");
0070             }
0071             break;
0072 
0073         case 'd': // --disposition-type
0074             if (EQUALS("displayed")) {
0075                 dispositionType = Displayed;
0076             } else if (EQUALS("deleted")) {
0077                 dispositionType = Deleted;
0078             } else if (EQUALS("dispatched")) {
0079                 dispositionType = Dispatched;
0080             } else if (EQUALS("processed")) {
0081                 dispositionType = Processed;
0082             } else if (EQUALS("denied")) {
0083                 dispositionType = Denied;
0084             } else if (EQUALS("failed")) {
0085                 dispositionType = Failed;
0086             } else {
0087                 usage("unknown disposition type!");
0088             }
0089             break;
0090 
0091         case 'f': // --final-recipient
0092             if (optarg && *optarg) {
0093                 finalRecipient = QString::fromUtf8(optarg);
0094             } else {
0095                 usage("--final-recipient is missing a value");
0096             }
0097             break;
0098 
0099         case 'i': // --original-message-id
0100             if (optarg && *optarg) {
0101                 originalMessageId = optarg;
0102             } else {
0103                 usage("--original-message-id is missing a value");
0104             }
0105             break;
0106 
0107         case 'm': // --disposition-modifier
0108             if (EQUALS("error")) {
0109                 dispositionModifiers << Error;
0110             } else if (EQUALS("warning")) {
0111                 dispositionModifiers << Warning;
0112             } else if (EQUALS("superseded")) {
0113                 dispositionModifiers << Superseded;
0114             } else if (EQUALS("expired")) {
0115                 dispositionModifiers << Expired;
0116             } else if (EQUALS("mailbox-terminated")) {
0117                 dispositionModifiers << MailboxTerminated;
0118             } else {
0119                 usage("unknown disposition modifier!");
0120             }
0121             break;
0122 
0123         case 'o': // --original-recipient
0124             if (optarg && *optarg) {
0125                 originalRecipient = QString::fromUtf8(optarg);
0126             } else {
0127                 usage("--original-recipient is missing a value");
0128             }
0129             break;
0130 
0131         case 's': // --sending-mode
0132             if (EQUALS("MDN-sent-manually")) {
0133                 sendingMode = SentManually;
0134             } else if (EQUALS("MDN-sent-automatically")) {
0135                 sendingMode = SentAutomatically;
0136             } else {
0137                 usage("unknown sending mode");
0138             }
0139             break;
0140 
0141         default:
0142             usage("unknown option encountered!");
0143         }
0144     }
0145 
0146     if (optind < argc) {
0147         special = QString::fromUtf8(argv[optind++]);
0148     }
0149     if (optind < argc) {
0150         usage("too many arguments!");
0151     }
0152 
0153     QByteArray result = dispositionNotificationBodyContent(finalRecipient,
0154                         originalRecipient.toLatin1(),
0155                         originalMessageId,
0156                         dispositionType,
0157                         actionMode,
0158                         sendingMode,
0159                         dispositionModifiers,
0160                         special);
0161     cout << "Result:\n" << result.data();
0162     return 0;
0163 }