示例#1
0
def auto_release(request, message_uuid, template='messages/release.html'):
    "Releases message from the quarantine without need to login"

    success = False

    release_record = get_object_or_404(Release, uuid=message_uuid, released=0)
    message_details = get_object_or_404(Message, id=release_record.message_id)

    if not host_is_local(message_details.hostname):
        remote_response = remote_release(message_details.hostname,
            release_record.uuid)
        response = remote_response['response']
        if request.is_ajax():
            return HttpResponse(response,
                content_type='application/javascript; charset=utf-8')
        try:
            json_response = simplejson.loads(response)
            if json_response['success']:
                success = True
                release_record.released = 1
                release_record.save()
        except:
            pass
    else:
        file_name = search_quarantine(message_details.date,
            release_record.message_id)
        if not file_name is None:
            if release_mail(file_name, message_details.to_address,
                message_details.from_address):
                success = True
                release_record.released = 1
                release_record.save()
        else:
            raise Http404

    html = render_to_string('messages/released.html',
        {'id': message_details.id, 'addrs':message_details.to_address,
        'success':success})

    if request.is_ajax():
        response = simplejson.dumps({'success':success, 'html': html})
        return HttpResponse(response,
            content_type='application/javascript; charset=utf-8')
    return render_to_response(template,
        {'message_id':release_record.message_id,
        'release_address':message_details.to_address, 'success':success},
        context_instance=RequestContext(request))
示例#2
0
def preview(request, message_id, is_attach=False, attachment_id=0, 
        archive=False):
    """
    Returns a message preview of a quarantined message, depending on
    the call it returns XHTML or JSON
    """
    if archive:
        obj = Archive
    else:
        obj = Message
    message_details = get_object_or_404(obj, id=message_id)
    if not message_details.can_access(request):
        return HttpResponseForbidden(
            'You are not authorized to access this page')

    if host_is_local(message_details.hostname):
        file_name = search_quarantine(message_details.date, message_id)
        if not file_name is None:
            try:
                import email
                fip = open(file_name, 'r')
                msg = email.message_from_file(fip)
                fip.close()
                email_parser = EmailParser()
                if is_attach:
                    message = email_parser.get_attachment(msg, attachment_id)
                    if message:
                        import base64
                        attachment_data = message.getvalue()
                        mimetype = message.content_type
                        if request.is_ajax():
                            json = simplejson.dumps({'success':True,
                            'attachment':base64.encodestring(attachment_data),
                            'mimetype':mimetype, 'name':message.name})
                            response = HttpResponse(json,
                            content_type='application/javascript; charset=utf-8')
                            message.close()
                            return response
                        response = HttpResponse(
                            attachment_data, mimetype=mimetype)
                        response['Content-Disposition'] = (
                            'attachment; filename=%s' % message.name)
                        message.close()
                        return response
                    else:
                        raise Http404
                else:
                    message = email_parser.parse_msg(msg)
                if request.is_ajax():
                    response = simplejson.dumps({'message':message,
                        'message_id':message_details.id})
                    return HttpResponse(response,
                        content_type='application/javascript; charset=utf-8')
                return render_to_response('messages/preview.html', 
                    {'message':message, 'message_id':message_details.id},
                 context_instance=RequestContext(request))
            except:
                raise Http404
        else:
            raise Http404
    else:
        #remote
        if is_attach:
            remote_response = remote_attachment_download(
                message_details.hostname, request.META['HTTP_COOKIE'], 
                message_id, attachment_id, archive)
            if remote_response['success']:
                import base64
                data = remote_response['response']
                attach = simplejson.loads(data)
                if attach['success']:
                    attachment_data = base64.decodestring(attach['attachment'])
                    mimetype = attach['mimetype']
                    response = HttpResponse(attachment_data, mimetype=mimetype)
                    response['Content-Disposition'] = (
                        'attachment; filename=%s' % attach['name'])
                    return response
            raise Http404
        else:
            remote_response = remote_preview(message_details.hostname,
                request.META['HTTP_COOKIE'], message_id, archive)
            if remote_response['success']:
                data = remote_response['response']
                items = simplejson.loads(data)
                message = items['message']

                if request.is_ajax():
                    response = simplejson.dumps({'message':message,
                        'message_id':message_id})
                    return HttpResponse(response,
                        content_type='application/javascript; charset=utf-8')
                else:
                    return render_to_response('messages/preview.html',
                        {'message':message, 'message_id':message_id},
                        context_instance=RequestContext(request))
            else:
                raise Http404
示例#3
0
def detail(request, message_id, archive=False):
    """
    Displays details of a message, and processes quarantined messages
    """
    if archive:
        obj = Archive
    else:
        obj = Message
    message_details = get_object_or_404(obj, id=message_id)
    if not message_details.can_access(request):
        return HttpResponseForbidden(
            'You are not authorized to access this page')

    error_list = ''
    quarantine_form = QuarantineProcessForm()
    quarantine_form.fields[
    'message_id'].widget.attrs['value'] = message_details.id

    if request.method == 'POST':
        quarantine_form = QuarantineProcessForm(request.POST)
        success = True
        if quarantine_form.is_valid():
            if not host_is_local(message_details.hostname):
                params = urllib.urlencode(request.POST)
                remote_response = remote_process(message_details.hostname,
                    request.META['HTTP_COOKIE'], message_id, params)
                response = remote_response['response']
                if request.is_ajax():
                    return HttpResponse(response,
                        content_type='application/javascript; charset=utf-8')
                try:
                    resp = simplejson.loads(response)
                    if resp['success']:
                        success = True
                    else:
                        success = False
                    error_list = resp['response']
                except:
                    success = False
                    error_list = 'Error: Empty server response'
            else:
                file_name = search_quarantine(message_details.date, message_id)
                if not file_name is None:
                    if quarantine_form.cleaned_data['release']:
                        # release
                        if quarantine_form.cleaned_data['use_alt']:
                            to_addr = quarantine_form.cleaned_data[
                            'altrecipients']
                        else:
                            to_addr = message_details.to_address
                        to_addr = to_addr.split(',')
                        if not release_mail(file_name, to_addr,
                            message_details.from_address):
                            success = False
                        template = 'messages/released.html'
                        html = render_to_string(template, 
                            {'id': message_details.id, 'addrs':to_addr, 
                            'success':success})
                    if quarantine_form.cleaned_data['salearn']:
                        #salean
                        salearn_opts = ('spam', 'ham', 'forget')
                        template = "messages/salearn.html"
                        salearn = int(
                                    quarantine_form.cleaned_data['salearn_as'])
                        if salearn <= 2:
                            status = sa_learn(file_name, salearn_opts[salearn])
                            if not status['success']:
                                success = False
                            html = render_to_string(template,
                                {'id': message_details.id,
                                'msg':status['output'], 'success':success})
                        else:
                            success = False
                            html = 'Invalid salearn options supplied'
                    if quarantine_form.cleaned_data['todelete']:
                        #delete
                        import os
                        if os.path.exists(file_name):
                            try:
                                os.remove(file_name)
                                message_details.quarantined = 0
                                message_details.save()
                            except:
                                success = False
                        template = "messages/delete.html"
                        html = render_to_string(template,
                            {'id': message_details.id, 'success':success})
                else:
                    html = 'The quarantined file could not be processed'
                    success = False
        else:
            error_list = quarantine_form.errors.values()[0]
            error_list = error_list[0]
            html = error_list
            success = False
        if request.is_ajax():
            response = simplejson.dumps({'success':success, 'html': html})
            return HttpResponse(response,
                content_type='application/javascript; charset=utf-8')

    quarantine_form.fields['altrecipients'].widget.attrs['size'] = '55'
    return render_to_response('messages/detail.html', locals(),
        context_instance=RequestContext(request))