/* Copyright (C) 2020 C. McEnroe * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include #include #include #include #include #include #include #include #include "archive.h" #include "imap.h" bool exportFetch(FILE *imap, enum Atom tag, struct List threads) { struct List uids = {0}; listFlatten(&uids, threads); char path[PATH_MAX]; for (size_t i = uids.len - 1; i < uids.len; --i) { uint32_t uid = dataCheck(uids.ptr[i], Number).number; int error = 0 || access(pathUID(path, uid, "atom"), F_OK) || access(pathUID(path, uid, "html"), F_OK) || access(pathUID(path, uid, "mbox"), F_OK); if (!error) uids.ptr[i] = uids.ptr[--uids.len]; } if (!uids.len) { listFree(uids); return false; } fprintf(imap, "%s UID FETCH ", Atoms[tag]); for (size_t i = 0; i < uids.len; ++i) { fprintf(imap, "%s%" PRIu32, (i ? "," : ""), uids.ptr[i].number); } fprintf( imap, " (UID ENVELOPE BODYSTRUCTURE" " BODY[HEADER.FIELDS (" MBOX_HEADERS ")] BODY[TEXT])\r\n" ); return true; } static void exportEnvelope(uint32_t uid, const struct Envelope *envelope) { int error; FILE *file; char path[PATH_MAX]; file = fopen(pathUID(path, uid, "html"), "w"); if (!file) err(EX_CANTCREAT, "%s", path); error = 0 || htmlMessageHead(file, envelope) || htmlMessageTail(file) || fclose(file); if (error) err(EX_IOERR, "%s", path); file = fopen(pathUID(path, uid, "atom"), "w"); if (!file) err(EX_CANTCREAT, "%s", path); error = 0 || atomEntryOpen(file, envelope) || atomEntryClose(file) || fclose(file); if (error) err(EX_IOERR, "%s", path); } static void exportRaw( uint32_t uid, const struct Envelope *envelope, const char *header, const char *body ) { char src[PATH_MAX]; FILE *file = fopen(pathUID(src, uid, "mbox"), "w"); if (!file) err(EX_CANTCREAT, "%s", src); int error = 0 || mboxFrom(file) || mboxHeader(file, header) || mboxBody(file, body) || fclose(file); if (error) err(EX_IOERR, "%s", src); char dst[PATH_MAX]; pathMessage(dst, envelope->messageID, "mbox"); unlink(dst); error = link(src, dst); if (error) err(EX_CANTCREAT, "%s", dst); } static void exportBodyPart( uint32_t uid, const struct BodyPart *structure, const struct BodyPart *part, const char *content ) { } bool exportData(FILE *imap, enum Atom tag, struct List items) { uint32_t uid = 0; struct Envelope envelope = {0}; struct BodyPart structure = {0}; const char *header = NULL; const char *body = NULL; for (size_t i = 0; i + 1 < items.len; i += 2) { enum Atom name; if (items.ptr[i].type == Atom) { name = items.ptr[i].atom; } else if ( items.ptr[i].type == List && items.ptr[i].list.len && items.ptr[i].list.ptr[0].type == Atom ) { name = items.ptr[i].list.ptr[0].atom; } else { errx(EX_PROTOCOL, "invalid data item name"); } struct Data data = items.ptr[i + 1]; switch (name) { break; case AtomBody: i--; break; case AtomUID: uid = dataCheck(data, Number).number; break; case AtomEnvelope: parseEnvelope(&envelope, dataCheck(data, List).list); break; case AtomBodyStructure: parseBodyPart(&structure, dataCheck(data, List).list); break; case AtomHeaderFields: header = dataCheck(data, String).string; break; case AtomText: body = dataCheck(data, String).string; break; default:; } } if (!uid) errx(EX_PROTOCOL, "missing UID data item"); if (!envelope.subject) errx(EX_PROTOCOL, "missing ENVELOPE data item"); if (!header) errx(EX_PROTOCOL, "missing BODY[HEADER.FIELDS] data item"); if (!body) errx(EX_PROTOCOL, "missing BODY[TEXT] data item"); exportRaw(uid, &envelope, header, body); exportEnvelope(uid, &envelope); if (structure.multipart) { // TODO: FETCH each body part. } else { exportBodyPart(uid, &structure, &structure, body); } envelopeFree(envelope); bodyPartFree(structure); return false; }