1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
/* $Id: attach.cpp,v 1.10 2004/08/19 14:52:29 terpstra Exp $
*
* attach.cpp - Handle a attach/ command
*
* Copyright (C) 2002 - Wesley W. Terpstra
*
* License: GPL
*
* Authors: 'Wesley W. Terpstra' <wesley@terpstra.ca>
*
* 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; version 2.
*
* 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#define _XOPEN_SOURCE 500
#define _FILE_OFFSET_BITS 64
#include <mimelib/message.h>
#include <mimelib/headers.h>
#include <mimelib/bodypart.h>
#include <mimelib/body.h>
#include <mimelib/enum.h>
#include <mimelib/mediatyp.h>
#include <mimelib/utility.h>
#include "commands.h"
#include "Summary.h"
#include <iostream>
using std::cout;
DwEntity& attach_find(DwEntity& e, long& x)
{
// We are the requested entity.
if (--x == 0) return e;
// if (e.hasHeaders() &&
if (e.Headers().HasContentType())
{
DwMediaType& t = e.Headers().ContentType();
switch (t.Type())
{
case DwMime::kTypeMessage:
if (e.Body().Message())
return attach_find(*e.Body().Message(), x);
break;
case DwMime::kTypeMultipart:
for (DwBodyPart* p = e.Body().FirstBodyPart(); p != 0; p = p->Next())
{
DwEntity& o = attach_find(*p, x);
if (x == 0) return o;
}
break;
}
}
return e;
}
string unfold_header(const char* hdr)
{
string out;
while (*hdr != 0)
{
if (*hdr == '\r' || *hdr == '\n' || *hdr == '\t')
out += ' ';
else out += *hdr;
++hdr;
}
return out;
}
int handle_attach(const Config& cfg, ESort::Reader* db, const string& param)
{
string::size_type o = param.find('@');
long n = atol(param.c_str());
if (!cfg.raw_email)
error(_("Permission Denied"), param,
_("Access to mail attachments has been disabled. "
"Contact the site administrator if this is a problem."));
if (o == string::npos || n <= 0 ||
!MessageId::is_full(param.c_str()+o+1))
error(_("Bad request"), param,
_("The given parameter was not of the correct format. "
"An attach request must be formatted like: "
"attach/id@YYYYMMDD.HHMMSS.hashcode.xml where id "
"is the number of the attachment."));
MessageId id(param.c_str()+o+1);
string ok;
Summary source(id);
if ((ok = source.load(db, cfg)) != "")
error(_("Database attach source pull failure"), ok,
_("The specified message does not exist."));
DwMessage message;
if ((ok = source.message(cfg.dbdir, message)) != "")
error(_("MBox read failure"), ok,
_("Unable to open message in the mailbox. "
"Perhaps it has been deleted or moved?"));
DwEntity& e = attach_find(message, n);
// Cannot cache an attachment because they have strange content-type
cout << "Status: 200 OK\r\n"
<< "Content-Type: ";
// if (e.hasHeaders() &&
if (e.Headers().HasContentType())
cout << unfold_header(
e.Headers().ContentType().AsString().c_str());
else cout << "text/plain";
cout << "\r\n\r\n";
DwString out;
// if (e.hasHeaders() &&
if (e.Headers().HasContentTransferEncoding())
{
switch (e.Headers().ContentTransferEncoding().AsEnum())
{
case DwMime::kCteQuotedPrintable:
DwDecodeQuotedPrintable(e.Body().AsString(), out);
break;
case DwMime::kCteBase64:
DwDecodeBase64(e.Body().AsString(), out);
break;
case DwMime::kCteNull:
case DwMime::kCteUnknown:
case DwMime::kCte7bit:
case DwMime::kCte8bit:
case DwMime::kCteBinary:
out = e.Body().AsString();
break;
}
}
else
{
out = e.Body().AsString();
}
cout.write(out.c_str(), out.length());
return 0;
}
|