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))
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))