from django.core.exceptions import ObjectDoesNotExist
from samplebc.bcmagic import models
+from samplebc.bcmagic.utils import report_error, redirect_to_url
+from samplebc.bcmagic.plugin import bcm_plugin_processor
import json
import re
# There should always be at least one | in a valid scan.
if len(split_text) <= 1:
- return {'mode': 'Error',
- 'status': 'Invalid text: %s' % (text)}
+ return report_error('Invalid text: %s' % (text))
# Keyword is the first element in the list
keyword = split_text[0]
try:
keymap = models.KeywordMap.objects.get(keyword=keyword)
except ObjectDoesNotExist, e:
- return {'mode': 'Error',
- 'status': 'Keyword (%s) is not defined' % (keyword)}
+ return report_error('Keyword (%s) is not defined' % (keyword))
# Remove keyword and only scan the content
content = '|'.join(split_text[1:])
# if the search was invalid
if not mo:
- return {'mode': 'Error',
- 'status': '(%s) failed to match (%s)' % (keymap.regex, content)}
+ return report_error('(%s) failed to match (%s)' % (keymap.regex, content))
t = Template(keymap.url_template)
c = Context(mo.groupdict())
- return {'mode': 'redirect',
- 'url': str(t.render(c)) }
+ return redirect_to_url(str(t.render(c)))
"""
d = {}
+ #Retrieve posted text from processing
if 'text' in request.POST:
text = request.POST['text']
else:
text = None
+
+ #Retrieve bmc_mode for processing
+ if 'bcm_mode' in request.POST:
+ bcm_mode = request.POST['bcm_mode']
+ else:
+ bcm_mode = None
+
+ ################################
+ # Handle some errors
+ ################################
- #return HttpResponse(json.write(request.POST.items()), 'text/plain')
+ # Did not receive text error
if text is None or text.strip() == '':
d['mode'] = 'Error'
d['status'] = 'Did not recieve text'
return HttpResponse(json.write(d), 'text/plain')
- if text.split('|')[0] == 'url':
+ # Did not receive bcm_mode error
+ if bcm_mode is None or bcm_mode.strip() == '':
+ d['mode'] = 'Error'
+ d['status'] = 'Missing bcm_mode information'
+
+
+ ################################
+ # Figure out which mode to use
+ ################################
+ keyword = text.split('|')[0]
+
+ # Handle URL mode by default
+ if keyword == 'url':
d['mode'] = 'redirect'
d['url'] = text.split('|')[1]
+
+ # Pass off processing to plugins
+ elif bcm_mode != 'default':
+ d = bcm_plugin_processor(keyword, text, bcm_mode)
+
+ # Try keyword mapper
else:
d = __magic_process(text)