1
0
Fork 1
mirror of https://github.com/coddingtonbear/django-mailbox.git synced 2026-07-09 22:38:19 +02:00
This commit is contained in:
Manuel Gomez 2019-06-19 22:09:53 +00:00 committed by GitHub
commit 2b841a568c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 64 additions and 6 deletions

View file

@ -8,6 +8,7 @@ console.
import logging
from django import forms
from django.conf import settings
from django.contrib import admin
from django.utils.translation import ugettext_lazy as _
@ -39,11 +40,29 @@ resend_message_received_signal.short_description = (
_('Re-send message received signal')
)
class MailboxForm(forms.ModelForm):
uri = forms.CharField(widget=forms.PasswordInput,
help_text="For security, the URI will not be shown and will "
"be encrypted in database "
"<br />"
"Example: imap+ssl://myusername:mypassword@someserver <br />"
"<br />"
"Internet transports include 'imap' and 'pop3'; "
"common local file transports include 'maildir', 'mbox', "
"and less commonly 'babyl', 'mh', and 'mmdf'. <br />"
"<br />"
"Be sure to urlencode your username and password should they "
"contain illegal characters (like @, :, etc)."
)
class Meta:
model = Mailbox
fields = ('name', 'uri', 'from_email', 'active',)
class MailboxAdmin(admin.ModelAdmin):
form = MailboxForm
list_display = (
'name',
'uri',
'from_email',
'active',
'last_polling',
@ -51,6 +70,10 @@ class MailboxAdmin(admin.ModelAdmin):
readonly_fields = ['last_polling', ]
actions = [get_new_mail]
def save_model(self, request, obj, form, change):
if request:
obj.uri = obj.encrypt_uri()
obj.save()
class MessageAttachmentAdmin(admin.ModelAdmin):
raw_id_fields = ('message', )

View file

@ -17,6 +17,7 @@ import os.path
import sys
import uuid
from tempfile import NamedTemporaryFile
from Crypto.Cipher import AES
import six
from six.moves.urllib.parse import parse_qs, unquote, urlparse
@ -113,9 +114,39 @@ class Mailbox(models.Model):
objects = models.Manager()
active_mailboxes = ActiveMailboxManager()
def pad(self, key):
length = 32 - (len(key) % 32)
return key + chr(length).encode('utf-8') * length
def unpad(self, key):
return key[0:-ord(key[-1])]
def encrypt_uri(self):
secret_key = self.pad(django_settings.SECRET_KEY)
self.uri = unicode(self.pad(self.uri)).encode('utf-8')
cipher = AES.new(secret_key)
self.uri = cipher.encrypt(self.uri)
self.uri = base64.b64encode(self.uri)
return self.uri
def decrypt_uri(self):
secret_key = self.pad(django_settings.SECRET_KEY)
uri = self.uri
uri = self.pad(uri)
uri = base64.b64decode(uri)
cipher = AES.new(secret_key)
uri = cipher.decrypt(uri)
return self.unpad(uri)
@property
def _protocol_info(self):
return urlparse(self.uri)
uri = self.uri
uri = self.decrypt_uri()
return urlparse(uri)
@property
def _query_string(self):
@ -351,10 +382,12 @@ class Mailbox(models.Model):
if settings['store_original_message']:
self._process_save_original_message(message, msg)
msg.mailbox = self
# Fix to accept subject emojis in utf-8
if 'subject' in message:
msg.subject = (
utils.convert_header_to_unicode(message['subject'])[0:255]
utils.convert_header_to_unicode(message['subject'].decode('raw-unicode-escape'))[0:255]
)
msg.subject = unicode(email.header.decode_header(msg.subject)[0][0], errors='ignore')
if 'message-id' in message:
msg.message_id = message['message-id'][0:255].strip()
if 'from' in message:

View file

@ -66,8 +66,8 @@ def get_settings():
),
'default_charset': getattr(
settings,
'DJANGO_MAILBOX_default_charset',
'iso8859-1',
'DJANGO_MAILBOX_DEFAULT_CHARSET',
'utf8',
)
}

View file

@ -1,2 +1,3 @@
django>=1.9,<1.10
six
pycrypto==2.6.1

View file

@ -54,6 +54,7 @@ setup(
packages=find_packages(),
include_package_data=True,
install_requires=[
'six>=1.6.1'
'six>=1.6.1',
'pycrypto==2.6.1'
]
)