Example #1
0
    def process_incoming(cls, case, message):
        # Extract text and quote
        if message.text:
            text = quotations.extract_from(message.text, 'text/plain')
            quote = message.text.replace(text, '')
        else:
            text = quotations.extract_from(message.html, 'text/html')
            quote = message.text.replace(text, '')

        # Create Letter
        obj = cls.objects.create(author_institution=case.institution,
                                 email=message.from_address[0],
                                 case=case,
                                 title=message.subject,
                                 body=text,
                                 quote=quote,
                                 eml=File(message.eml, message.eml.name),
                                 message=message)
        attachments = []
        # Append attachments
        for attachment in message.attachments.all():
            name = attachment.get_filename() or 'Unknown.bin'
            if len(name) > 70:
                name, ext = os.path.splitext(name)
                ext = ext[:70]
                name = name[:70 - len(ext)] + ext
            file_obj = File(attachment.document, name)
            attachments.append(Attachment(letter=obj, attachment=file_obj))
        Attachment.objects.bulk_create(attachments)
        return obj, attachments
Example #2
0
def test_extract_from_respects_content_type(extract_from_plain,
                                            extract_from_html):
    msg_body = "Hi there"

    quotations.extract_from(msg_body, 'text/plain')
    extract_from_plain.assert_called_with(msg_body)

    quotations.extract_from(msg_body, 'text/html')
    extract_from_html.assert_called_with(msg_body)

    eq_(msg_body, quotations.extract_from(msg_body, 'text/blah'))
Example #3
0
    # Identify case
    try:  # TODO: Is it old case?
        case = Case.objects.by_msg(message).get()
    except Case.DoesNotExist:
        print("Case creating")
        case = Case(name=message.subject, created_by=user, client=user)
        case.save()
        user.notify(actor=user,
                    verb='registered',
                    target=case,
                    from_email=case.get_email())
    print("Case: ", case)
    # Prepare text
    if message.text:
        text = quotations.extract_from(message.text, 'text/plain')
        signature = message.text.replace(text, '')
    else:
        text = html2text.html2text(
            quotations.extract_from(message.html, 'text/html'))
        signature = message.text.replace(text, '')

    # Calculate letter status
    if user.is_staff:
        if user.has_perm('cases.can_send_to_client', case):
            status = Letter.STATUS.done
        else:
            status = Letter.STATUS.staff
    else:
        status = Letter.STATUS.done
Example #4
0
import claw, sys, json
import HTMLParser

from claw import quotations

claw.init()

content_type = sys.argv[1]
message_body = HTMLParser.HTMLParser().unescape(unicode(sys.argv[2], 'utf-8'))

reply = quotations.extract_from(message_body, content_type)

print json.dumps({'reply':reply})

# sudo curl https://bootstrap.pypa.io/get-pip.py -o - | sudo python
# pip install claw
Example #5
0
def test_crash_inside_extract_from():
    msg_body = "Hi there"
    eq_(msg_body, quotations.extract_from(msg_body, 'text/plain'))
Example #6
0
def mail_process(sender, message, **args):
    # new_user + poradnia@ => new_user @ new_user
    # new_user + case => FAIL
    # old_user + case => PASS
    # many_case => FAIL

    # Skip autoreply messages - see RFC3834
    if (lambda x: 'Auto-Submitted' in x and
            x['Auto-Submitted'] == 'auto-replied')(message.get_email_object()):
        logger.info("Delete .eml from {email} as auto-replied".format(
                    email=message.from_address[0]))
        message.eml.delete(save=True)
        return

    # Identify user
    user = get_user_model().objects.get_by_email_or_create(message.from_address[0])
    logger.debug("Identified user: %s", user)

    # Identify case
    try:  # TODO: Is it old case?
        case = Case.objects.by_msg(message).get()
    except Case.DoesNotExist:
        logger.debug("Case creating")
        case = Case(name=message.subject, created_by=user, client=user)
        case.save()
        user.notify(actor=user, verb='registered', target=case, from_email=case.get_email())
    logger.debug("Case: %s", case)
    # Prepare text
    if message.text:
        text = quotations.extract_from(message.text, 'text/plain')
        signature = message.text.replace(text, '')
    else:
        text = html2text.html2text(quotations.extract_from(message.html, 'text/html'))
        signature = message.text.replace(text, '')

    # Calculate letter status
    if user.is_staff:
        if user.has_perm('cases.can_send_to_client', case):
            status = Letter.STATUS.done
        else:
            status = Letter.STATUS.staff
    else:
        status = Letter.STATUS.done

    # Update case status (re-open)
    case_updated = False
    if not user.is_staff and case.status == Case.STATUS.closed:
        case.status_update(reopen=True, save=False)
        case_updated = True
    if user.is_staff:
        case.handled = True
        case_updated = True
    if user.is_staff and status == Letter.STATUS.done:
        case.has_project = False
        case_updated = True

    if case_updated:
        case.save()

    obj = Letter(name=message.subject,
                 created_by=user,
                 case=case,
                 status=status,
                 text=text,
                 message=message,
                 signature=signature,
                 eml=message.eml)
    obj.save()

    logger.debug("Letter: %s", obj)
    # Convert attachments
    attachments = []
    for attachment in message.attachments.all():
        name = attachment.get_filename() or 'Unknown.bin'
        if len(name) > 70:
            name, ext = os.path.splitext(name)
            ext = ext[:70]
            name = name[:70 - len(ext)] + ext
        att_file = File(attachment.document, name)
        att = Attachment(letter=obj, attachment=att_file)
        attachments.append(att)
    Attachment.objects.bulk_create(attachments)
    case.update_counters()
    obj.send_notification(actor=user, verb='created')