summaryrefslogtreecommitdiffstats
path: root/lurker/render/attach.cpp
blob: 334d43bdd874fc92ed10d875e6c3c2cb00a5ffcd (plain)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*  $Id: attach.cpp 1649 2009-10-19 14:35:01Z terpstra $
 *  
 *  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 _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>
#include <cstdlib>

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);
	// Identical error if missing or not allowed
	if ((ok = source.load(db, cfg)) != "" || !source.allowed())
	{
		if (ok == "") ok = "not in a mailbox"; // fake
		error(_("Database attach source pull failure"), ok,
		      _("The specified message does not exist."));
	}
	
	if (source.deleted())
		error(_("Database attach source pull failure"), "not found",
		      _("The specified message has been deleted."));
	
	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";
	
	if (e.Headers().HasContentDisposition())
		cout << "Content-Disposition: " 
		     << unfold_header(e.Headers().ContentDisposition().AsString().c_str())
		     << "\r\n";
	
	cout << "\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;
}