mirror of
https://github.com/coddingtonbear/django-mailbox.git
synced 2026-07-10 06:48:19 +02:00
Merge 204d67a57d into 0650bacbea
This commit is contained in:
commit
2b841a568c
5 changed files with 64 additions and 6 deletions
|
|
@ -8,6 +8,7 @@ console.
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
from django import forms
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
|
|
@ -39,11 +40,29 @@ resend_message_received_signal.short_description = (
|
||||||
_('Re-send message received signal')
|
_('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):
|
class MailboxAdmin(admin.ModelAdmin):
|
||||||
|
form = MailboxForm
|
||||||
list_display = (
|
list_display = (
|
||||||
'name',
|
'name',
|
||||||
'uri',
|
|
||||||
'from_email',
|
'from_email',
|
||||||
'active',
|
'active',
|
||||||
'last_polling',
|
'last_polling',
|
||||||
|
|
@ -51,6 +70,10 @@ class MailboxAdmin(admin.ModelAdmin):
|
||||||
readonly_fields = ['last_polling', ]
|
readonly_fields = ['last_polling', ]
|
||||||
actions = [get_new_mail]
|
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):
|
class MessageAttachmentAdmin(admin.ModelAdmin):
|
||||||
raw_id_fields = ('message', )
|
raw_id_fields = ('message', )
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,7 @@ import os.path
|
||||||
import sys
|
import sys
|
||||||
import uuid
|
import uuid
|
||||||
from tempfile import NamedTemporaryFile
|
from tempfile import NamedTemporaryFile
|
||||||
|
from Crypto.Cipher import AES
|
||||||
|
|
||||||
import six
|
import six
|
||||||
from six.moves.urllib.parse import parse_qs, unquote, urlparse
|
from six.moves.urllib.parse import parse_qs, unquote, urlparse
|
||||||
|
|
@ -113,9 +114,39 @@ class Mailbox(models.Model):
|
||||||
objects = models.Manager()
|
objects = models.Manager()
|
||||||
active_mailboxes = ActiveMailboxManager()
|
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
|
@property
|
||||||
def _protocol_info(self):
|
def _protocol_info(self):
|
||||||
return urlparse(self.uri)
|
uri = self.uri
|
||||||
|
uri = self.decrypt_uri()
|
||||||
|
return urlparse(uri)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def _query_string(self):
|
def _query_string(self):
|
||||||
|
|
@ -351,10 +382,12 @@ class Mailbox(models.Model):
|
||||||
if settings['store_original_message']:
|
if settings['store_original_message']:
|
||||||
self._process_save_original_message(message, msg)
|
self._process_save_original_message(message, msg)
|
||||||
msg.mailbox = self
|
msg.mailbox = self
|
||||||
|
# Fix to accept subject emojis in utf-8
|
||||||
if 'subject' in message:
|
if 'subject' in message:
|
||||||
msg.subject = (
|
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:
|
if 'message-id' in message:
|
||||||
msg.message_id = message['message-id'][0:255].strip()
|
msg.message_id = message['message-id'][0:255].strip()
|
||||||
if 'from' in message:
|
if 'from' in message:
|
||||||
|
|
|
||||||
|
|
@ -66,8 +66,8 @@ def get_settings():
|
||||||
),
|
),
|
||||||
'default_charset': getattr(
|
'default_charset': getattr(
|
||||||
settings,
|
settings,
|
||||||
'DJANGO_MAILBOX_default_charset',
|
'DJANGO_MAILBOX_DEFAULT_CHARSET',
|
||||||
'iso8859-1',
|
'utf8',
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,2 +1,3 @@
|
||||||
django>=1.9,<1.10
|
django>=1.9,<1.10
|
||||||
six
|
six
|
||||||
|
pycrypto==2.6.1
|
||||||
|
|
|
||||||
3
setup.py
3
setup.py
|
|
@ -54,6 +54,7 @@ setup(
|
||||||
packages=find_packages(),
|
packages=find_packages(),
|
||||||
include_package_data=True,
|
include_package_data=True,
|
||||||
install_requires=[
|
install_requires=[
|
||||||
'six>=1.6.1'
|
'six>=1.6.1',
|
||||||
|
'pycrypto==2.6.1'
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue