1 # -*- coding: utf-8 -*-
2 # Copyright (C) 1998-2019 by the Free Software Foundation, Inc.
4 # This file is part of Postorius.
6 # Postorius is free software: you can redistribute it and/or modify it under
7 # the terms of the GNU General Public License as published by the Free
8 # Software Foundation, either version 3 of the License, or (at your option)
11 # Postorius is distributed in the hope that it will be useful, but WITHOUT
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 # You should have received a copy of the GNU General Public License along with
17 # Postorius. If not, see <http://www.gnu.org/licenses/>.
20 from email
import message_from_string
, policy
21 from email
.parser
import HeaderParser
23 from django
.contrib
.auth
.decorators
import login_required
24 from django
.http
import Http404
, HttpResponse
25 from django
.urls
import reverse
26 from django
.utils
.translation
import gettext
as _
28 from django_mailman3
.lib
.scrub
import Scrubber
30 from postorius
.auth
.decorators
import list_moderator_required
31 from postorius
.models
import List
35 msgobj
= message_from_string(message
, policy
=policy
.SMTP
)
36 header_parser
= HeaderParser()
39 headers_dict
= header_parser
.parsestr(message
)
40 for key
in headers_dict
.keys():
41 headers
.append('{}: {}'.format(key
, headers_dict
[key
]))
42 content
= Scrubber(msgobj
).scrub()[0]
45 'headers': '\n'.join(headers
),
49 def get_attachments(message
):
50 message
= message_from_string(message
, policy
=policy
.SMTP
)
51 return Scrubber(message
).scrub()[1]
55 @list_moderator_required
56 def get_held_message(request
, list_id
, held_id
=-1):
57 """Return a held message as a json object
60 raise Http404(_('Message does not exist'))
62 held_message
= List
.objects
.get_or_404(
63 fqdn_listname
=list_id
).get_held_message(held_id
)
64 if 'raw' in request
.GET
:
65 return HttpResponse(held_message
.msg
, content_type
='text/plain')
66 response_data
= dict()
67 response_data
['sender'] = held_message
.sender
68 response_data
['subject'] = held_message
.subject
69 response_data
['reason'] = held_message
.reason
70 response_data
['hold_date'] = held_message
.hold_date
71 response_data
['msg'] = parse(held_message
.msg
)
72 response_data
['msgid'] = held_message
.request_id
73 response_data
['attachments'] = []
74 attachments
= get_attachments(held_message
.msg
)
75 for attachment
in attachments
:
76 counter
, name
, content_type
, encoding
, content
= attachment
77 response_data
['attachments'].append(
78 (reverse('rest_attachment_for_held_message',
79 args
=(list_id
, held_id
, counter
)), name
))
81 return HttpResponse(json
.dumps(response_data
),
82 content_type
='application/json')
86 @list_moderator_required
87 def get_attachment_for_held_message(request
, list_id
, held_id
, attachment_id
):
88 held_message
= List
.objects
.get_or_404(
89 fqdn_listname
=list_id
).get_held_message(held_id
)
90 attachments
= get_attachments(held_message
.msg
)
91 for attachment
in attachments
:
92 if attachment
[0] == int(attachment_id
):
93 response
= HttpResponse(attachment
[4], content_type
=attachment
[2])
94 response
['Content-Disposition'] = \
95 'attachment;filename="{}"'.format(attachment
[1])
97 raise Http404(_('Attachment does not exist'))