Skip to content
This repository has been archived by the owner on Nov 26, 2018. It is now read-only.

Commit

Permalink
Account for misconfigured/misbehaving bots
Browse files Browse the repository at this point in the history
  • Loading branch information
ipmb committed Nov 20, 2015
1 parent d472484 commit 8649fea
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions botbot/apps/plugins/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,25 @@ def __init__(self, packet, app):

self.is_direct_message = self.check_direct_message()

def is_valid(self):
if self._chatbot and self._channel:
return True
return False

@property
def _chatbot(self):
"""Simple caching for ChatBot model"""
if not hasattr(self, '_chatbot_cache'):
cache_key = 'chatbot:{0}'.format(self._chatbot_id)
chatbot = cache.get(cache_key)
if not chatbot:
chatbot = bots_models.ChatBot.objects.get(id=self._chatbot_id)
try:
chatbot = bots_models.ChatBot.objects.get(
id=self._chatbot_id)
except bots_models.ChatBot.DoesNotExist:
LOG.warn('Chatbot %s does not exist. Line dropped.',
self._chatbot_id)
return None
cache.set(cache_key, chatbot, CACHE_TIMEOUT_2H)
self._chatbot_cache = chatbot
return self._chatbot_cache
Expand All @@ -63,8 +74,14 @@ def _channel(self):
channel = cache.get(cache_key)

if not channel and self._channel_name.startswith("#"):
channel = self._chatbot.channel_set.get(
name=self._channel_name)
try:
channel = self._chatbot.channel_set.get(
name=self._channel_name)
except self._chatbot.channel_set.model.DoesNotExist:
LOG.warn('Chatbot %s should not be listening to %s. '
'Line dropped.',
self._chatbot_id, self._channel_name)
return None
cache.set(cache_key, channel, CACHE_TIMEOUT_2H)

"""
Expand Down Expand Up @@ -197,7 +214,8 @@ def listen(self):
statsd.timing(".".join(["plugins", "latency"]),
delta.total_seconds() * 1000)

self.dispatch(line)
if line.is_valid():
self.dispatch(line)
except Exception:
LOG.error("Line Dispatch Failed", exc_info=True, extra={
"line": val
Expand Down

0 comments on commit 8649fea

Please sign in to comment.