Esempio n. 1
0
    def __init__(self, *args, **kwargs):

        request = kwargs.pop('request')
        super(ComposeMailForm, self).__init__(*args, **kwargs)

        # Populate the identity choices
        config = WebpymailConfig(request)
        identity_list = config.identities()
        from_list = []
        for identity in identity_list:
            if identity['user_name'] and identity['mail_address']:
                addr = '"%s" <%s>' % (identity['user_name'],
                                      identity['mail_address'])
            elif identity['mail_address']:
                addr = '<%s>' % identity['mail_address']
            else:
                continue

            from_list.append((addr, addr))

        if not from_list:
            addr = '<%s@%s>' % (request.session['username'],
                                request.session['host'])
            from_list = [(addr, addr)]
        self.fields['from_addr'].choices = from_list
Esempio n. 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
Esempio n. 3
0
def show_message(request, folder, uid):
    '''Show the message
    '''
    config = WebpymailConfig(request)

    folder_name = base64.urlsafe_b64decode(str(folder))

    M = serverLogin(request)
    folder = M[folder_name]
    message = folder[int(uid)]

    # If it's a POST request
    if request.method == 'POST':
        try:
            msgactions.message_change(request, message)
        except hlimap.imapmessage.MessageNotFound:
            return redirect('message_list', folder=folder.url())

    return render_to_response(
        'mail/message_body.html', {
            'folder': folder,
            'message': message,
            'inline_img': config.getboolean('message', 'show_images_inline')
        },
        context_instance=RequestContext(request))
Esempio n. 4
0
    def __init__(self, *args, **kwargs):

        request = kwargs.pop('request')
        super(ComposeMailForm, self).__init__(*args, **kwargs)

        # Populate the identity choices
        config =  WebpymailConfig( request )
        identity_list = config.identities()
        from_list = []
        for identity in identity_list:
            if identity['user_name'] and identity['mail_address']:
                addr = '"%s" <%s>' % (identity['user_name'],
                                      identity['mail_address'] )
            elif identity['mail_address']:
                addr = '<%s>' % identity['mail_address']
            else:
                continue

            from_list.append( (addr, addr ) )

        if not from_list:
            addr = '<%s@%s>' % ( request.session['username'],
                                 request.session['host'] )
            from_list = [ (addr, addr )]
        self.fields['from_addr'].choices = from_list
Esempio n. 5
0
def show_folders_view(request):
    '''Show the account folders.
    '''
    # Login to the server:
    M = serverLogin( request )

    # Special folders
    # TODO: will get this from the user config
    M.set_special_folders('INBOX', 'INBOX.Drafts', 'INBOX.Templates')

    # Expandable folders
    expand_folders = request.user.folderstoexpand_set.all()
    if expand_folders:
        M.set_expand_list(*[ f.folder_name for f in expand_folders ])

    # Read the subscribed folder list:
    M.refresh_folders(subscribed=True)

    # Get the default identity
    config =  WebpymailConfig( request )
    identity_list = config.identities()
    default_address = identity_list[0]['mail_address']

    return render_to_response('mail/folder_list.html',
        {'server': M,
         'address': default_address },
        context_instance=RequestContext(request))
Esempio n. 6
0
def show_folders_view(request):
    '''Show the account folders.
    '''
    # Login to the server:
    M = serverLogin(request)

    # Special folders
    # TODO: will get this from the user config
    M.set_special_folders('INBOX', 'INBOX.Drafts', 'INBOX.Templates')

    # Expandable folders
    expand_folders = request.user.folderstoexpand_set.all()
    if expand_folders:
        M.set_expand_list(*[f.folder_name for f in expand_folders])

    # Read the subscribed folder list:
    M.refresh_folders(subscribed=True)

    # Get the default identity
    config = WebpymailConfig(request)
    identity_list = config.identities()
    default_address = identity_list[0]['mail_address']

    return render_to_response('mail/folder_list.html', {
        'server': M,
        'address': default_address
    },
                              context_instance=RequestContext(request))
Esempio n. 7
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
Esempio n. 8
0
def show_message_list_view(request, folder=settings.DEFAULT_FOLDER):
    '''Show the selected Folder message list.
    '''
    M = serverLogin(request)
    folder_name = base64.urlsafe_b64decode(str(folder))
    folder = M[folder_name]

    message_list = folder.message_list

    # Set the search criteria:
    search_criteria = 'ALL'
    query = None

    flag = request.GET.get('flag', None)

    if flag:
        query = 'flag=%s' % flag
        search_criteria = 'KEYWORD %s' % flag

    message_list.set_search_expression(search_criteria)
    message_list.refresh_messages()

    # Message action form
    raw_message_list = [(uid, uid) for uid in message_list.flat_message_list]
    form = MessageActionForm(data={}, message_list=raw_message_list, server=M)

    # If it's a POST request
    if request.method == 'POST':
        msgactions.batch_change(request, folder, raw_message_list)

    # Pagination
    message_list.paginator.msg_per_page = 40
    try:
        page = int(request.GET.get('page', 1))
    except:
        page = request.GET.get('page', 1)
        if page == 'all':
            message_list.paginator.msg_per_page = -1
        page = 1
    message_list.paginator.current_page = page
    message_list.add_messages_range()

    # Get the default identity
    config = WebpymailConfig(request)
    identity_list = config.identities()
    default_address = identity_list[0]['mail_address']

    # Show the message list
    return render_to_response('mail/message_list.html', {
        'folder': folder,
        'address': default_address,
        'paginator': folder.paginator(),
        'query': query,
        'form': form
    },
                              context_instance=RequestContext(request))
Esempio n. 9
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)
Esempio n. 10
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)
Esempio n. 11
0
def show_message_list_view(request, folder=settings.DEFAULT_FOLDER):
    '''Show the selected Folder message list.
    '''
    M = serverLogin( request )
    folder_name =  base64.urlsafe_b64decode(str(folder))
    folder = M[folder_name]

    message_list = folder.message_list

    # Set the search criteria:
    search_criteria = 'ALL'
    query = None

    flag = request.GET.get('flag', None)

    if flag:
        query = 'flag=%s' % flag
        search_criteria = 'KEYWORD %s' % flag

    message_list.set_search_expression(search_criteria)
    message_list.refresh_messages()

    # Message action form
    raw_message_list = [ (uid,uid) for uid in message_list.flat_message_list ]
    form = MessageActionForm(data={}, message_list=raw_message_list, server=M)

    # If it's a POST request
    if request.method == 'POST':
        msgactions.batch_change( request, folder, raw_message_list )

    # Pagination
    message_list.paginator.msg_per_page = 40
    try:
        page = int(request.GET.get('page',1))
    except:
        page = request.GET.get('page',1)
        if page == 'all':
            message_list.paginator.msg_per_page = -1
        page = 1
    message_list.paginator.current_page = page
    message_list.add_messages_range()

    # Get the default identity
    config =  WebpymailConfig( request )
    identity_list = config.identities()
    default_address = identity_list[0]['mail_address']

    # Show the message list
    return render_to_response( 'mail/message_list.html',{
            'folder':folder,
            'address': default_address,
            'paginator': folder.paginator(),
            'query':query,
            'form':form },
            context_instance=RequestContext(request))
Esempio n. 12
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)
Esempio n. 13
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)
Esempio n. 14
0
def show_message(request, folder, uid):
    '''Show the message
    '''
    config = WebpymailConfig( request )

    folder_name =  base64.urlsafe_b64decode(str(folder))

    M = serverLogin( request )
    folder = M[folder_name]
    message = folder[int(uid)]

    # If it's a POST request
    if request.method == 'POST':
        try:
            msgactions.message_change( request, message )
        except hlimap.imapmessage.MessageNotFound:
            return redirect('message_list', folder=folder.url() )

    return render_to_response('mail/message_body.html',{'folder':folder,
        'message':message,
        'inline_img': config.getboolean('message', 'show_images_inline')},
        context_instance=RequestContext(request))
Esempio n. 15
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)
Esempio n. 16
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))
Esempio n. 17
0
def message_process(request, folder, uid):
    if request.method == 'POST':
        form = ProcessEmailForm(request.POST, request.FILES)
        if form.is_valid():
            # get form data
            form_data = form.cleaned_data

            # get message object
            config = WebpymailConfig(request)
            folder_name = base64.urlsafe_b64decode(str(folder))
            M = serverLogin(request)
            folder = M[folder_name]
            message = folder[int(uid)]

            # Get text/plain part
            text_plain = get_text_plain(message)

            # decryption plugin
            use_decryption = form_data['use_decryption']
            message_text_dec = None
            decryption_error = None
            if use_decryption:
                try:
                    # decrypt
                    # decryption_key = 'ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEF'
                    decryption_key = form_data['decryption_key'].ljust(32, '0')[:32]
                    text_plain = base64.b64decode(text_plain)
                    cipher = STRAIT(decryption_key, Mode.CBC)
                    iv, message_text_enc = text_plain[:8].decode('utf-8'), text_plain[8:]
                    message_text_dec = cipher.decrypt(message_text_enc, iv).decode('utf-8', 'ignore')
                    text_to_validate = message_text_dec
                except Exception as e:
                    decryption_error = 'Failed to decrypt email content: ' + str(e)
                    text_to_validate = ''
            else:
                text_to_validate = text_plain

            # validation plugin
            use_validation = form_data['use_validation']
            validation = None
            validation_error = None
            if use_validation:
                try:
                    # validate
                    if form_data['validation_pub_key_file']:
                        # save to temporary file
                        folder_path = os.path.join('mailapp', 'savedkeys')
                        if not os.path.exists(folder_path):
                            os.makedirs(folder_path)
                        pub_key_path = os.path.join(folder_path, 'uploaded.pub')
                        with form_data['validation_pub_key_file'] as fup, open(pub_key_path, 'wb') as ftemp:
                            ftemp.write(fup.read())
                        # load key
                        try:
                            ecc = ECC.load_key(pub_key_path, True)
                        except Exception as e:
                            raise Exception('Load public key from file failed')
                        else:
                            a = ecc.a
                            b = ecc.b
                            p = ecc.p
                            Qx, Qy = ecc.Q
                            n = ecc.n
                            Gx, Gy = ecc.G
                    else:
                        a = form_data['validation_pub_key_a']
                        b = form_data['validation_pub_key_b']
                        p = form_data['validation_pub_key_p']
                        Qx = form_data['validation_pub_key_Qx']
                        Qy = form_data['validation_pub_key_Qy']
                        n = form_data['validation_pub_key_n']
                        Gx = form_data['validation_pub_key_Gx']
                        Gy = form_data['validation_pub_key_Gy']
                    # validate digital signature
                    validation = check_digital_signature(text_to_validate, a, b, p, Qx, Qy, n, Gx, Gy)
                except Exception as e:
                    validation_error = 'Failed to validate signature: ' + str(e)

            # Check the query string
            try:
                external_images = config.getboolean('message', 'external_images')
                external_images = request.GET.get('external_images', external_images)
                external_images = bool(int(external_images))
            except ValueError:
                external_images = config.getboolean('message', 'external_images')

            return render(request, 'mail/plugins/message_process.html', {
                'folder': folder,
                'message': message,
                'show_images_inline': config.getboolean('message',
                                                        'show_images_inline'),
                'show_html': config.getboolean('message', 'show_html'),
                'external_images': external_images,
                'use_validation': use_validation,
                'validation': validation,
                'validation_error': validation_error,
                'use_decryption': use_decryption,
                'message_text_dec': message_text_dec,
                'decryption_error': decryption_error,
                })

    else:
        form = ProcessEmailForm()

    return render(request, 'mail/plugins/message_process_form.html', {
        'folder': folder,
        'uid': uid,
        'form': form,
        })
Esempio n. 18
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))
Esempio n. 19
0
def show_message_list_view(request, folder=settings.DEFAULT_FOLDER):
    '''Show the selected Folder message list.
    '''
    M = serverLogin(request)
    folder_name = base64.urlsafe_b64decode(str(folder))
    folder = M[folder_name]
    message_list = folder.message_list

    # Set the search criteria:
    search_criteria = 'ALL'
    message_list.set_search_expression(search_criteria)

    # Handle GET queries
    query = request.GET.copy()

    flag = request.GET.get('flag', None)
    if flag:
        search_criteria = 'KEYWORD %s' % flag

    show_style = request.GET.get('show_style', 'sorted')
    if show_style.upper() == 'THREADED':
        message_list.set_threaded()

    sort_order = request.GET.get('sort_order', 'DATE').upper()
    sort = request.GET.get('sort', 'DESCENDING').upper()
    if sort == 'DESCENDING':
        sort = '-'
    else:
        sort = ''
    if sort_order in SORT_KEYS:
        message_list.set_sort_program('%s%s' % (sort, sort_order))

    try:
        page = int(request.GET.get('page', 1))
    except:
        page = request.GET.get('page', 1)
        if page == 'all':
            message_list.paginator.msg_per_page = -1
        page = 1
    if 'page' in query:
        query.pop('page')

    # Pagination
    message_list.paginator.msg_per_page = 40
    message_list.paginator.current_page = page

    # Message action form
    message_list.refresh_messages()  # Necessary to get the flat_message_list
    raw_message_list = [(uid, uid) for uid in message_list.flat_message_list]
    form = MessageActionForm(data={}, message_list=raw_message_list, server=M)

    # If it's a POST request
    if request.method == 'POST':
        msgactions.batch_change(request, folder, raw_message_list)
        # TODO: When setting message flags the MessageList's messages objects
        # are not updated, so we have to refresh the messages to reflect the
        # changes in the message list. This should not be necessary, the set
        # and reset flags method in the Folder objects should update the
        # message information, saving this way a refresh_messages call.
        message_list.refresh_messages()

    # Get the default identity
    config = WebpymailConfig(request)
    identity_list = config.identities()
    default_address = identity_list[0]['mail_address']

    # Show the message list
    return render(
        request, 'mail/message_list.html', {
            'folder': folder,
            'address': default_address,
            'paginator': folder.paginator(),
            'query': query,
            'form': form
        })
Esempio n. 20
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)