# Path: will/settings.py
# def auto_key():
# def import_settings(quiet=True):
#         DEPRECATED_BUT_MAPPED_SETTINGS = {
#             "USERNAME": "HIPCHAT_USERNAME",
#             "PASSWORD": "HIPCHAT_PASSWORD",
#             "V1_TOKEN": "HIPCHAT_V1_TOKEN",
#             "V2_TOKEN": "HIPCHAT_V2_TOKEN",
#             "TOKEN": "HIPCHAT_V1_TOKEN",
#             "ROOMS": "HIPCHAT_ROOMS",
#             "NAME": "HIPCHAT_NAME",
#             "HANDLE": "HIPCHAT_HANDLE",
#             "DEFAULT_ROOM": "HIPCHAT_DEFAULT_ROOM",
#             "SLACK_DEFAULT_ROOM": "SLACK_DEFAULT_CHANNEL",
#         }
#             IO_BACKENDS = []
# 
# Path: will/abstractions.py
# class Event(Bunch):
#     will_internal_type = "Event"
# 
#     REQUIRED_FIELDS = [
#         "type",
#         "version",
#     ]
# 
#     def __init__(self, *args, **kwargs):
#         super(Event, self).__init__(*args, **kwargs)
#         self.version = 1
# 
#         for f in self.REQUIRED_FIELDS:
#             if not f in kwargs and not hasattr(self, f):
#                 raise Exception("Missing %s in Event construction." % f)
# 
#         if "timestamp" in kwargs:
#             self.timestamp = kwargs["timestamp"]
#         else:
#             self.timestamp = datetime.datetime.now()
# 
#         h = hashlib.md5()
#         h.update(self.timestamp.strftime("%s").encode("utf-8"))
#         h.update(self.type.encode("utf-8"))
#         self.hash = h.hexdigest()
#         if not "original_incoming_event_hash" in kwargs:
#             if hasattr(self, "original_incoming_event") and hasattr(self.original_incoming_event, "hash"):
#                 self.original_incoming_event_hash = self.original_incoming_event.hash
#             else:
#                 self.original_incoming_event_hash = self.hash
# 
# class Message(object):
#     will_internal_type = "Message"
#     REQUIRED_FIELDS = [
#         "is_direct",
#         "is_private_chat",
#         "is_group_chat",
#         "will_is_mentioned",
#         "will_said_it",
#         "sender",
#         "backend_supports_acl",
#         "content",
#         "backend",
#         "original_incoming_event",
#     ]
# 
#     def __init__(self, *args, **kwargs):
#         for f in self.REQUIRED_FIELDS:
#             if not f in kwargs:
#                 raise Exception("Missing %s in Message construction." % f)
# 
#         for f in kwargs:
#             self.__dict__[f] = kwargs[f]
# 
#         if "timestamp" in kwargs:
#             self.timestamp = kwargs["timestamp"]
#         else:
#             self.timestamp = datetime.datetime.now()
# 
#         # Clean content.
#         self.content = self._clean_message_content(self.content)
# 
#         h = hashlib.md5()
#         h.update(self.timestamp.strftime("%s").encode("utf-8"))
#         h.update(self.content.encode("utf-8"))
#         self.hash = h.hexdigest()
# 
#         self.metadata = Bunch()
#         if not "original_incoming_event_hash" in kwargs:
#             if hasattr(self, "original_incoming_event") and hasattr(self.original_incoming_event, "hash"):
#                 self.original_incoming_event_hash = self.original_incoming_event.hash
#             else:
#                 self.original_incoming_event_hash = self.hash
# 
#     def __unicode__(self, *args, **kwargs):
#         if len(self.content) > 20:
#             content_str = "%s..." % self.content[:20]
#         else:
#             content_str = self.content
#         return u"Message: \"%s\"\n  %s (%s) " % (
#             content_str,
#             self.timestamp.strftime('%Y-%m-%d %H:%M:%S'),
#             self.backend,
#         )
# 
#     def __str__(self, *args, **kwargs):
#         return self.__unicode__(*args, **kwargs)
# 
#     def _clean_message_content(self, s):
#         # Clear out 'smart' quotes and the like.
#         s = s.replace("’", "'").replace("‘", "'").replace('“', '"').replace('”', '"')
#         s = s.replace(u"\u2018", "'").replace(u"\u2019", "'")
#         s = s.replace(u"\u201c", '"').replace(u"\u201d", '"')
#         return s


import re
import logging
from bottle import request
from will import settings
from will.abstractions import Event, Message
from will.mixins import NaturalTimeMixin, ScheduleMixin, StorageMixin, SettingsMixin, \
    EmailMixin, PubSubMixin
        super().__init__(*args, **kwargs)

    @staticmethod
    def _prepared_content(content, _, __):
        content = re.sub(r'>\s+<', '><', content)
        return content

    @staticmethod
    def _trim_for_execution(message):
        # Trim it down
        if hasattr(message, "analysis"):
            message.analysis = None
        if hasattr(message, "source_message") and hasattr(message.source_message, "analysis"):
            message.source_message.analysis = None
        return message

    @staticmethod
    def get_backend(message: Message, service: str = None):
        'Select the correct backend (module path)'
        backend = False
        if service:
            for b in settings.IO_BACKENDS:  # pylint: disable=no-member
                if service in b:
                    return b

        if hasattr(message, "backend"):
            backend = message.backend
        elif message and hasattr(message, "data") and hasattr(message.data, "backend"):
            backend = message.data.backend
        else:

# Path: will/settings.py
# def auto_key():
# def import_settings(quiet=True):
#         DEPRECATED_BUT_MAPPED_SETTINGS = {
#             "USERNAME": "HIPCHAT_USERNAME",
#             "PASSWORD": "HIPCHAT_PASSWORD",
#             "V1_TOKEN": "HIPCHAT_V1_TOKEN",
#             "V2_TOKEN": "HIPCHAT_V2_TOKEN",
#             "TOKEN": "HIPCHAT_V1_TOKEN",
#             "ROOMS": "HIPCHAT_ROOMS",
#             "NAME": "HIPCHAT_NAME",
#             "HANDLE": "HIPCHAT_HANDLE",
#             "DEFAULT_ROOM": "HIPCHAT_DEFAULT_ROOM",
#             "SLACK_DEFAULT_ROOM": "SLACK_DEFAULT_CHANNEL",
#         }
#             IO_BACKENDS = []
# 
# Path: will/abstractions.py
# class Event(Bunch):
#     will_internal_type = "Event"
# 
#     REQUIRED_FIELDS = [
#         "type",
#         "version",
#     ]
# 
#     def __init__(self, *args, **kwargs):
#         super(Event, self).__init__(*args, **kwargs)
#         self.version = 1
# 
#         for f in self.REQUIRED_FIELDS:
#             if not f in kwargs and not hasattr(self, f):
#                 raise Exception("Missing %s in Event construction." % f)
# 
#         if "timestamp" in kwargs:
#             self.timestamp = kwargs["timestamp"]
#         else:
#             self.timestamp = datetime.datetime.now()
# 
#         h = hashlib.md5()
#         h.update(self.timestamp.strftime("%s").encode("utf-8"))
#         h.update(self.type.encode("utf-8"))
#         self.hash = h.hexdigest()
#         if not "original_incoming_event_hash" in kwargs:
#             if hasattr(self, "original_incoming_event") and hasattr(self.original_incoming_event, "hash"):
#                 self.original_incoming_event_hash = self.original_incoming_event.hash
#             else:
#                 self.original_incoming_event_hash = self.hash
# 
# class Message(object):
#     will_internal_type = "Message"
#     REQUIRED_FIELDS = [
#         "is_direct",
#         "is_private_chat",
#         "is_group_chat",
#         "will_is_mentioned",
#         "will_said_it",
#         "sender",
#         "backend_supports_acl",
#         "content",
#         "backend",
#         "original_incoming_event",
#     ]
# 
#     def __init__(self, *args, **kwargs):
#         for f in self.REQUIRED_FIELDS:
#             if not f in kwargs:
#                 raise Exception("Missing %s in Message construction." % f)
# 
#         for f in kwargs:
#             self.__dict__[f] = kwargs[f]
# 
#         if "timestamp" in kwargs:
#             self.timestamp = kwargs["timestamp"]
#         else:
#             self.timestamp = datetime.datetime.now()
# 
#         # Clean content.
#         self.content = self._clean_message_content(self.content)
# 
#         h = hashlib.md5()
#         h.update(self.timestamp.strftime("%s").encode("utf-8"))
#         h.update(self.content.encode("utf-8"))
#         self.hash = h.hexdigest()
# 
#         self.metadata = Bunch()
#         if not "original_incoming_event_hash" in kwargs:
#             if hasattr(self, "original_incoming_event") and hasattr(self.original_incoming_event, "hash"):
#                 self.original_incoming_event_hash = self.original_incoming_event.hash
#             else:
#                 self.original_incoming_event_hash = self.hash
# 
#     def __unicode__(self, *args, **kwargs):
#         if len(self.content) > 20:
#             content_str = "%s..." % self.content[:20]
#         else:
#             content_str = self.content
#         return u"Message: \"%s\"\n  %s (%s) " % (
#             content_str,
#             self.timestamp.strftime('%Y-%m-%d %H:%M:%S'),
#             self.backend,
#         )
# 
#     def __str__(self, *args, **kwargs):
#         return self.__unicode__(*args, **kwargs)
# 
#     def _clean_message_content(self, s):
#         # Clear out 'smart' quotes and the like.
#         s = s.replace("’", "'").replace("‘", "'").replace('“', '"').replace('”', '"')
#         s = s.replace(u"\u2018", "'").replace(u"\u2019", "'")
#         s = s.replace(u"\u201c", '"').replace(u"\u201d", '"')
#         return s


# Path: will/plugin.py
import re
import logging
from bottle import request
from will import settings
from will.abstractions import Event, Message
from will.mixins import NaturalTimeMixin, ScheduleMixin, StorageMixin, SettingsMixin, \
    EmailMixin, PubSubMixin

        super().__init__(*args, **kwargs)

    @staticmethod
    def _prepared_content(content, _, __):
        content = re.sub(r'>\s+<', '><', content)
        return content

    @staticmethod
    def _trim_for_execution(message):
        # Trim it down
        if hasattr(message, "analysis"):
            message.analysis = None
        if hasattr(message, "source_message") and hasattr(message.source_message, "analysis"):
            message.source_message.analysis = None
        return message

    @staticmethod
    def get_backend(message: Message, service: str = None):
        'Select the correct backend (module path)'
        backend = False
        if service:
            for b in settings.IO_BACKENDS:  # pylint: disable=no-member
                if service in b:
                    return b

        if hasattr(message, "backend"):
            backend = message.backend
        elif message and hasattr(message, "data") and hasattr(message.data, "backend"):
            backend = message.data.backend
        else:

            backend = settings.DEFAULT_BACKEND  # pylint: disable=no-member