'Save message, Cap!'

def process_msg(self, msg, attrs, attachments):

    from treeio.messaging.models import Message
    try:
        conf = ModuleSetting.get_for_module('treeio.messaging', 'default_contact_type')[0]
        default_contact_type = ContactType.objects.get(pk=long(conf.value))
    except:
        default_contact_type = None
    (email_author, created) = Contact.get_or_create_by_email(attrs.author_email, attrs.author_name, default_contact_type)
    if created:
        email_author.copy_permissions(self.stream)
    existing = Message.objects.filter(stream=self.stream, title=attrs.subject, author=email_author, body=attrs.body).exists()
    if (not existing):
        message = None
        if (attrs.subject[:3] == 'Re:'):
            if (attrs.subject[:4] == 'Re:    '):
                original_subject = attrs.subject[4:]
            else:
                original_subject = attrs.subject[3:]
            try:
                query = ((Q(reply_to__isnull=True) & Q(recipients=email_author)) & (Q(title=original_subject) | Q(title=attrs.subject)))
                original = Message.objects.filter(query).order_by('-date_created')[:1][0]
                message = Message(title=attrs.subject, body=attrs.body, author=email_author, stream=self.stream, reply_to=original)
                if attrs.email_date:
                    message.date_created = attrs.email_date
                message.save()
                message.copy_permissions(original)
                original.read_by.clear()
            except IndexError:
                pass
        if (not message):
            message = Message(title=attrs.subject, body=attrs.body, author=email_author, stream=self.stream)
            if attrs.email_date:
                message.date_created = attrs.email_date
            message.save()
            message.copy_permissions(self.stream)
            message.recipients.add(email_author)
