Пример #1
0
def get_theme(request):
    '''
    Returns the current theme.

    The theme name can be defined in this order (the first found is used):

        1. GET request query theme=<theme name>
        2. Django session key 'theme'
        3. If the user is authenticated, the configuration key general.theme is read
        4. The default theme defined in the django settings file with the key DEFAULT_THEME
    '''
    # From the GET request
    theme = request.GET.get('theme', None)
    if theme:
        request.session['theme'] = theme
        return theme

    # From the django session
    theme = request.session.get('theme', None)
    if theme:
        return theme

    # From Webpymail configuration
    config = WebpymailConfig(request)
    theme = config.get('general', 'theme')
    if theme:
        request.session['theme'] = theme
        return theme

    # From settings.py
    return DEFAULT_THEME
Пример #2
0
def get_theme(request):
    """
    Returns the current theme.

    The theme name can be defined in this order (the first found is used):

        1. GET request query theme=<theme name>
        2. Django session key 'theme'
        3. If the user is authenticated, the configuration key general.theme is read
        4. The default theme defined in the django settings file with the key DEFAULT_THEME
    """
    # From the GET request
    theme = request.GET.get("theme", None)
    if theme:
        request.session["theme"] = theme
        return theme

    # From the django session
    theme = request.session.get("theme", None)
    if theme:
        return theme

    # From Webpymail configuration
    config = WebpymailConfig(request)
    theme = config.get("general", "theme")
    if theme:
        request.session["theme"] = theme
        return theme

    # From settings.py
    return DEFAULT_THEME
Пример #3
0
def index(request):
    '''
    This is the main index, it reads the configuration file and
    redirects to the default view defined on the configuration
    file.
    '''
    config = WebpymailConfig(request)
    login_page = config.get('general', 'login_page')

    return HttpResponseRedirect(login_page)
Пример #4
0
def index(request):
    '''
    This is the main index, it reads the configuration file and
    redirects to the default view defined on the configuration
    file.
    '''
    config = WebpymailConfig( request )
    login_page = config.get('general','login_page')

    return HttpResponseRedirect(login_page)
Пример #5
0
def logoutView(request):
    # Get the user config
    try:
        config = WebpymailConfig(request)
        logout_page = config.get("general", "logout_page")
    except KeyError:
        logout_page = "/"
    # Do the actual logout
    request.session.modified = True
    logout(request)
    # Redirect to a success page.
    return HttpResponseRedirect(logout_page)
Пример #6
0
def logoutView(request):
    # Get the user config
    try:
        config = WebpymailConfig(request)
        logout_page = config.get('general', 'logout_page')
    except KeyError:
        logout_page = '/'
    # Do the actual logout
    request.session.modified = True
    logout(request)
    # Redirect to a success page.
    return HttpResponseRedirect(logout_page)
Пример #7
0
def send_message(request,
                 text='',
                 to_addr='',
                 cc_addr='',
                 bcc_addr='',
                 subject='',
                 attachments='',
                 headers={},
                 context={}):
    '''Generic send message view
    '''
    # Auxiliary data initialization
    new_data = request.POST.copy()
    other_action = False
    old_files = []
    if 'saved_files' in new_data:
        if new_data['saved_files']:
            old_files = new_data['saved_files'].split(',')
    file_list = request.FILES.getlist('attachment[]')
    uploaded_files = UploadFiles(request.user,
                                 old_files=old_files,
                                 new_files=file_list)

    # Check if there is a request to delete files
    for key in new_data:
        match = delete_re.match(key)
        if match:
            id = int(match.groups()[0])
            uploaded_files.delete_id(id)
            other_action = True

    # Check if the cancel button was pressed
    if 'cancel' in new_data:
        # Delete the files
        uploaded_files.delete()
        # return
        return HttpResponseRedirect('/')

    # create an hidden field with the file list.
    # In case the form does not validate, the user doesn't have
    # to upload it again
    new_data['saved_files'] = ','.join(
        ['%d' % Xi for Xi in uploaded_files.id_list()])
    user_profile = request.user.userprofile

    temp_signature_pri_key_file = request.FILES.get('signature_pri_key_file',
                                                    None)
    if temp_signature_pri_key_file is not None:
        form = ComposeMailForm(
            new_data, {'signature_pri_key_file': temp_signature_pri_key_file},
            request=request)
    else:
        form = ComposeMailForm(new_data, request=request)

    if 'upload' in new_data:
        other_action = True

    if form.is_valid() and not other_action:
        # Read the posted data
        form_data = form.cleaned_data

        subject = form_data['subject']
        from_addr = form_data['from_addr']

        to_addr = join_address_list(form_data['to_addr'])
        cc_addr = join_address_list(form_data['cc_addr'])
        bcc_addr = join_address_list(form_data['bcc_addr'])

        text_format = form_data['text_format']
        message_text = form_data['message_text'].encode('utf-8')

        config = WebpymailConfig(request)

        # Create html message
        # if text_format == MARKDOWN and HAS_MARKDOWN:
        #     md = markdown.Markdown(output_format='HTML')
        #     message_html = md.convert(smart_text(message_text))
        #     css = config.get('message', 'css')
        #     # TODO: use a template to get the html and insert the css
        #     message_html = ('<html>\n<style>%s</style>'
        #                     '<body>\n%s\n</body>\n</html>' %
        #                     (css, message_html))
        # else:
        #     message_html = None
        message_html = None

        # signature plugin
        use_signature = form_data['use_signature']
        if use_signature:
            if form_data['signature_pri_key_file']:
                # save to temporary file
                folder_path = os.path.join('mailapp', 'savedkeys')
                if not os.path.exists(folder_path):
                    os.makedirs(folder_path)
                pri_key_path = os.path.join(folder_path, 'uploaded.pri')
                with form_data['signature_pri_key_file'] as fup, open(
                        pri_key_path, 'wb') as ftemp:
                    ftemp.write(fup.read())
                # load key
                ecc = ECC.load_key(pri_key_path, False)
                a = ecc.a
                b = ecc.b
                p = ecc.p
                d = ecc.d
                n = ecc.n
                Gx, Gy = ecc.G
            else:
                a = form_data['signature_pri_key_a']
                b = form_data['signature_pri_key_b']
                p = form_data['signature_pri_key_p']
                d = form_data['signature_pri_key_d']
                n = form_data['signature_pri_key_n']
                Gx = form_data['signature_pri_key_Gx']
                Gy = form_data['signature_pri_key_Gy']

            message_text += b'\n\n' + b'<ds>' + generate_digital_signature(
                message_text, a, b, p, d, n, Gx, Gy) + b'</ds>'

        # encryption plugin
        use_encryption = form_data['use_encryption']

        # Create the RFC822 message
        # NOTE: the current relevant RFC is RFC 5322, maybe this function
        # name should be changed to reflect this, maybe it shouldn't be
        # named after the RFC!
        if use_encryption:
            # Encryption Message
            # iv = '12345678'
            # key = 'ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEF'
            iv = form_data['encryption_iv']
            key = form_data['encryption_key']

            cipher = STRAIT(key, Mode.CBC)
            message_text_enc = iv.encode('utf-8') + cipher.encrypt(
                message_text, iv)
            message_text_enc = base64.b64encode(message_text_enc)
            print('enc:', message_text_enc)

            # Build message
            message = compose_rfc822(from_addr, to_addr, cc_addr, bcc_addr,
                                     subject, message_text_enc, message_html,
                                     uploaded_files, headers)
        else:
            # Build message
            message = compose_rfc822(from_addr, to_addr, cc_addr, bcc_addr,
                                     subject, message_text, message_html,
                                     uploaded_files, headers)

        # Post the message to the SMTP server
        try:
            host = config.get('smtp', 'host')
            port = config.getint('smtp', 'port')
            user = config.get('smtp', 'user')
            passwd = config.get('smtp', 'passwd')
            security = config.get('smtp', 'security').upper()
            use_imap_auth = config.getboolean('smtp', 'use_imap_auth')
            if use_imap_auth:
                user = request.session['username']
                passwd = request.session['password']
            send_mail(message, host, port, user, passwd, security)
        except SMTPRecipientsRefused as detail:
            error_message = ''.join([
                '<p>%s' % escape(detail.recipients[Xi][1])
                for Xi in detail.recipients
            ])
            context['form'] = form
            context['server_error'] = error_message
            context['uploaded_files'] = uploaded_files
            return render(request, 'mail/send_message.html', context)
        except SMTPException as detail:
            error_message = '<p>%s' % detail
            context['form'] = form
            context['server_error'] = error_message
            context['uploaded_files'] = uploaded_files
            return render(request, 'mail/send_message.html', context)
        except Exception as detail:
            error_message = '<p>%s' % detail
            context['form'] = form
            context['server_error'] = error_message
            context['uploaded_files'] = uploaded_files
            return render(request, 'mail/send_message.html', context)

        # Store the message on the sent folder
        imap_store(request, user_profile.sent_folder, message)
        # Delete the temporary files
        uploaded_files.delete()
        return HttpResponseRedirect('/')
    else:
        # Return to the message composig view
        context['form'] = form
        context['uploaded_files'] = uploaded_files
        return render(request, 'mail/send_message.html', context)
Пример #8
0
def send_message(request,
                 text='',
                 to_addr='',
                 cc_addr='',
                 bcc_addr='',
                 subject='',
                 attachments='',
                 headers={},
                 context={}):
    '''Generic send message view
    '''
    # Auxiliary data initialization
    new_data = request.POST.copy()
    other_action = False
    old_files = []
    if 'saved_files' in new_data:
        if new_data['saved_files']:
            old_files = new_data['saved_files'].split(',')
    file_list = request.FILES.getlist('attachment[]')
    uploaded_files = UploadFiles(request.user,
                                 old_files=old_files,
                                 new_files=file_list)

    # Check if there is a request to delete files
    for key in new_data:
        match = delete_re.match(key)
        if match:
            id = int(match.groups()[0])
            uploaded_files.delete_id(id)
            other_action = True

    # Check if the cancel button was pressed
    if 'cancel' in new_data:
        # Delete the files
        uploaded_files.delete()
        # return
        return HttpResponseRedirect('/')

    # create an hidden field with the file list.
    # In case the form does not validate, the user doesn't have
    # to upload it again
    new_data['saved_files'] = ','.join(
        ['%d' % Xi for Xi in uploaded_files.id_list()])
    user_profile = request.user.userprofile
    form = ComposeMailForm(new_data, request=request)
    if 'upload' in new_data:
        other_action = True

    if form.is_valid() and not other_action:
        # Read the posted data
        form_data = form.cleaned_data

        subject = form_data['subject']
        from_addr = form_data['from_addr']

        to_addr = join_address_list(form_data['to_addr'])
        cc_addr = join_address_list(form_data['cc_addr'])
        bcc_addr = join_address_list(form_data['bcc_addr'])

        text_format = form_data['text_format']
        message_text = form_data['message_text'].encode('utf-8')

        config = WebpymailConfig(request)

        # Create html message
        if text_format == MARKDOWN and HAS_MARKDOWN:
            md = markdown.Markdown(output_format='HTML')
            message_html = md.convert(smart_text(message_text))
            css = config.get('message', 'css')
            # TODO: use a template to get the html and insert the css
            message_html = ('<html>\n<style>%s</style>'
                            '<body>\n%s\n</body>\n</html>' %
                            (css, message_html))
        else:
            message_html = None

        # Create the RFC822 message
        # NOTE: the current relevant RFC is RFC 5322, maybe this function
        # name should be changed to reflect this, maybe it shouldn't be
        # named after the RFC!
        message = compose_rfc822(from_addr, to_addr, cc_addr, bcc_addr,
                                 subject, message_text, message_html,
                                 uploaded_files, headers)

        # Post the message to the SMTP server
        try:
            host = config.get('smtp', 'host')
            port = config.getint('smtp', 'port')
            user = config.get('smtp', 'user')
            passwd = config.get('smtp', 'passwd')
            security = config.get('smtp', 'security').upper()
            use_imap_auth = config.getboolean('smtp', 'use_imap_auth')
            if use_imap_auth:
                user = request.session['username']
                passwd = request.session['password']
            send_mail(message, host, port, user, passwd, security)
        except SMTPRecipientsRefused as detail:
            error_message = ''.join([
                '<p>%s' % escape(detail.recipients[Xi][1])
                for Xi in detail.recipients
            ])
            context['form'] = form
            context['server_error'] = error_message
            context['uploaded_files'] = uploaded_files
            return render(request, 'mail/send_message.html', context)
        except SMTPException as detail:
            error_message = '<p>%s' % detail
            context['form'] = form
            context['server_error'] = error_message
            context['uploaded_files'] = uploaded_files
            return render(request, 'mail/send_message.html', context)
        except Exception as detail:
            error_message = '<p>%s' % detail
            context['form'] = form
            context['server_error'] = error_message
            context['uploaded_files'] = uploaded_files
            return render(request, 'mail/send_message.html', context)

        # Store the message on the sent folder
        imap_store(request, user_profile.sent_folder, message)
        # Delete the temporary files
        uploaded_files.delete()
        return HttpResponseRedirect('/')
    else:
        # Return to the message composig view
        context['form'] = form
        context['uploaded_files'] = uploaded_files
        return render(request, 'mail/send_message.html', context)
Пример #9
0
def send_message(request, text='', to_addr='', cc_addr='', bcc_addr = '', subject='',
    attachments=''):
    '''Generic send message view
    '''
    if request.method == 'POST':
        new_data = request.POST.copy()
        other_action = False

        old_files = []
        if new_data.has_key('saved_files'):
            if new_data['saved_files']:
                old_files = new_data['saved_files'].split(',')

        uploaded_files = UploadFiles( request.user,
            old_files = old_files,
            new_files = request.FILES.getlist('attachment[]'))

        # Check if there is a request to delete files
        for key in new_data:
            match = delete_re.match(key)
            if match:
                id = int(match.groups()[0])
                uploaded_files.delete_id(id)
                other_action = True

        # Check if the cancel button was pressed
        if  new_data.has_key('cancel'):
            # Delete the files
            uploaded_files.delete()
            # return
            return HttpResponseRedirect('/')

        # create an hidden field with the file list.
        # In case the form does not validate, the user doesn't have
        # to upload it again
        new_data['saved_files'] = ','.join([ '%d' % Xi
            for Xi in uploaded_files.id_list()])

        user_profile = request.user.get_profile()

        form = ComposeMailForm(new_data, request = request)

        if new_data.has_key('upload'):
            other_action = True

        if form.is_valid() and not other_action:
            # get the data:
            form_data = form.cleaned_data

            subject = form_data['subject']
            from_addr  = form_data['from_addr']

            to_addr    = join_address_list( form_data['to_addr'] )
            cc_addr    = join_address_list( form_data['cc_addr'] )
            bcc_addr   = join_address_list( form_data['bcc_addr'] )

            text_format = form_data['text_format']
            message_text = form_data['message_text'].encode('utf-8')

            config = WebpymailConfig( request )

            if text_format == MARKDOWN and HAS_MARKDOWN:
                md = markdown.Markdown(output_format='HTML')
                message_html = md.convert(smart_unicode(message_text))
                css = config.get('message', 'css')
                # TODO: use a template to get the html and insert the css
                message_html = '<html>\n<style>%s</style><body>\n%s\n</body>\n</html>' % (css, message_html)
            else:
                message_html = None

            message = compose_rfc822( from_addr, to_addr, cc_addr, bcc_addr,
                subject, message_text, message_html, uploaded_files )

            try:
                host = config.get('smtp', 'host')
                port = config.getint('smtp', 'port')
                user = config.get('smtp', 'user')
                passwd = config.get('smtp', 'passwd')
                security = config.get('smtp', 'security').upper()
                use_imap_auth = config.getboolean('smtp', 'use_imap_auth')

                if use_imap_auth:
                    user = request.session['username']
                    passwd = request.session['password']

                send_mail( message,  host, port, user, passwd, security)
            except SMTPRecipientsRefused, detail:
                error_message = ''.join(
                    ['<p>%s' % escape(detail.recipients[Xi][1])
                     for Xi in detail.recipients ] )
                return render_to_response('mail/send_message.html', {'form':form,
                    'server_error': error_message,
                    'uploaded_files': uploaded_files},
                    context_instance=RequestContext(request))
            except SMTPException, detail:
                return render_to_response('mail/send_message.html', {'form':form,
                    'server_error': '<p>%s' % detail,
                    'uploaded_files': uploaded_files},
                    context_instance=RequestContext(request))
            except Exception, detail:
                error_message = '<p>%s' % detail
                return render_to_response('mail/send_message.html', {'form':form,
                    'server_error': error_message,
                    'uploaded_files': uploaded_files},
                    context_instance=RequestContext(request))
Пример #10
0
def send_message(request,
                 text='',
                 to_addr='',
                 cc_addr='',
                 bcc_addr='',
                 subject='',
                 attachments=''):
    '''Generic send message view
    '''
    if request.method == 'POST':
        new_data = request.POST.copy()
        other_action = False

        old_files = []
        if new_data.has_key('saved_files'):
            if new_data['saved_files']:
                old_files = new_data['saved_files'].split(',')

        uploaded_files = UploadFiles(
            request.user,
            old_files=old_files,
            new_files=request.FILES.getlist('attachment[]'))

        # Check if there is a request to delete files
        for key in new_data:
            match = delete_re.match(key)
            if match:
                id = int(match.groups()[0])
                uploaded_files.delete_id(id)
                other_action = True

        # Check if the cancel button was pressed
        if new_data.has_key('cancel'):
            # Delete the files
            uploaded_files.delete()
            # return
            return HttpResponseRedirect('/')

        # create an hidden field with the file list.
        # In case the form does not validate, the user doesn't have
        # to upload it again
        new_data['saved_files'] = ','.join(
            ['%d' % Xi for Xi in uploaded_files.id_list()])

        user_profile = request.user.get_profile()

        form = ComposeMailForm(new_data, request=request)

        if new_data.has_key('upload'):
            other_action = True

        if form.is_valid() and not other_action:
            # get the data:
            form_data = form.cleaned_data

            subject = form_data['subject']
            from_addr = form_data['from_addr']

            to_addr = join_address_list(form_data['to_addr'])
            cc_addr = join_address_list(form_data['cc_addr'])
            bcc_addr = join_address_list(form_data['bcc_addr'])

            text_format = form_data['text_format']
            message_text = form_data['message_text'].encode('utf-8')

            config = WebpymailConfig(request)

            if text_format == MARKDOWN and HAS_MARKDOWN:
                md = markdown.Markdown(output_format='HTML')
                message_html = md.convert(smart_unicode(message_text))
                css = config.get('message', 'css')
                # TODO: use a template to get the html and insert the css
                message_html = '<html>\n<style>%s</style><body>\n%s\n</body>\n</html>' % (
                    css, message_html)
            else:
                message_html = None

            message = compose_rfc822(from_addr, to_addr, cc_addr, bcc_addr,
                                     subject, message_text, message_html,
                                     uploaded_files)

            try:
                host = config.get('smtp', 'host')
                port = config.getint('smtp', 'port')
                user = config.get('smtp', 'user')
                passwd = config.get('smtp', 'passwd')
                security = config.get('smtp', 'security').upper()
                use_imap_auth = config.getboolean('smtp', 'use_imap_auth')

                if use_imap_auth:
                    user = request.session['username']
                    passwd = request.session['password']

                send_mail(message, host, port, user, passwd, security)
            except SMTPRecipientsRefused, detail:
                error_message = ''.join([
                    '<p>%s' % escape(detail.recipients[Xi][1])
                    for Xi in detail.recipients
                ])
                return render_to_response(
                    'mail/send_message.html', {
                        'form': form,
                        'server_error': error_message,
                        'uploaded_files': uploaded_files
                    },
                    context_instance=RequestContext(request))
            except SMTPException, detail:
                return render_to_response(
                    'mail/send_message.html', {
                        'form': form,
                        'server_error': '<p>%s' % detail,
                        'uploaded_files': uploaded_files
                    },
                    context_instance=RequestContext(request))
            except Exception, detail:
                error_message = '<p>%s' % detail
                return render_to_response(
                    'mail/send_message.html', {
                        'form': form,
                        'server_error': error_message,
                        'uploaded_files': uploaded_files
                    },
                    context_instance=RequestContext(request))