Beispiel #1
0
def get_email(user):
    server = IMAPClient(HOST, use_uid=True, ssl=True)
    username = db.get("user:%s:login" % user)
    password = db.get("user:%s:password" % user)
    server.login(username, password)
    server.select_folder('INBOX', readonly=True)
    messages = server.search(['NOT DELETED','SINCE 15-May-2014'])
    response = server.fetch(messages, ['RFC822', 'FLAGS'])
    for msgid, data in response.iteritems():
        # check for duplicates
        duplicate = db.zrangebyscore("mail:%s:inbox" % user, msgid, msgid)
        if duplicate:
            continue
        emailUTF8 = data['RFC822'].encode('utf-8')
        msg = parser.parsestr(emailUTF8)
        body = extract_body(msg).encode('utf-8')
        msg['message'] = body
        msg['subject'] = ('NoSubj' if (msg['Subject'] == None or msg['Subject'].encode('utf-8') == "".encode('utf-8'))  else msg['Subject'])
        msg['to'] = ('NoTo' if (msg['To'] == None) else msg['To'])
        plain = {'plain_body': extract_body_text(msg).encode('utf-8'), 'subject': msg['subject']}
        # TODO set unread
        email = {'id': msgid, 'from': msg['From'], 'to': msg['To'], 'subject': msg['Subject'],
                'date': msg['Date'], 'cc': msg['CC'], 'read': False,
                'message': body, 'categorized': False, 'summary': shorten(plain), 'archived': False}

        trained = db.get("user:%s:trained" % user)
        if trained == "true":
            email['category'] = int(classify(msg, user))
        else:
            email['category'] = 1
        emailJSON = json.dumps(email, sort_keys=True, indent=4, separators=(',', ': '))
        db.zadd("mail:%s:inbox" % user, emailJSON, msgid)
        db.sadd("mail:%s:%s" % (user, email['category']), msgid)
    server.logout()
Beispiel #2
0
def mark_email_unread():
    email = json.loads(request.data)
    mail = db.zrevrangebyscore("mail:%s:inbox" % current_user.user, email["id"], email["id"])
    pMail = json.loads(mail[0])
    pMail["read"] = False
    emailJSON = json.dumps(pMail, sort_keys=True, indent=4, separators=(",", ": "))
    db.zremrangebyscore("mail:%s:inbox" % current_user.user, email["id"], email["id"])
    db.zadd("mail:%s:inbox" % current_user.user, emailJSON, email["id"])
    return "Success"
Beispiel #3
0
def categorize_email():
    email = json.loads(request.data)
    mail = db.zrevrangebyscore("mail:%s:inbox" % current_user.user, email["id"], email["id"])
    pMail = json.loads(mail[0])
    oldCategory = pMail["category"]
    pMail["category"] = email["category"]
    pMail["categorized"] = True
    emailJSON = json.dumps(pMail, sort_keys=True, indent=4, separators=(",", ": "))
    db.zremrangebyscore("mail:%s:inbox" % current_user.user, email["id"], email["id"])
    db.zadd("mail:%s:inbox" % current_user.user, emailJSON, email["id"])
    db.smove(
        "mail:%s:%s" % (current_user.user, oldCategory),
        "mail:%s:%s" % (current_user.user, pMail["category"]),
        email["id"],
    )
    return "Success"
Beispiel #4
0
def delete_category():
    category = json.loads(request.data)["category"]
    newCategory = 1
    mail = db.smembers("mail:%s:%s" % (current_user.user, category))
    print mail
    for emailID in mail:
        # Move email in Redis
        print db.smove(
            "mail:%s:%s" % (current_user.user, category), "mail:%s:%s" % (current_user.user, newCategory), emailID
        )
        # Change category in object
        emailObj = db.zrevrangebyscore("mail:%s:inbox" % current_user.user, emailID, emailID)
        pMail = json.loads(emailObj[0])
        pMail["category"] = newCategory
        emailJSON = json.dumps(pMail, sort_keys=True, indent=4, separators=(",", ": "))
        print db.zremrangebyscore("mail:%s:inbox" % current_user.user, emailID, emailID)
        print db.zadd("mail:%s:inbox" % current_user.user, emailJSON, emailID)
    return "Success"