File: optimize-imap.py

package info (click to toggle)
kopanocore 8.7.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 15,400 kB
  • sloc: cpp: 175,422; python: 24,623; perl: 7,319; php: 6,056; sh: 2,172; makefile: 1,294; xml: 45; ansic: 1
file content (56 lines) | stat: -rwxr-xr-x 1,949 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/python3
# SPDX-License-Identifier: AGPL-3.0-only

import sys
import logging

from MAPI.Tags import PR_EC_IMAP_EMAIL_SIZE, PR_EC_IMAP_BODYSTRUCTURE, PR_EC_IMAP_BODY, PR_EC_IMAP_EMAIL, PT_STRING8
from MAPI.Struct import MAPIErrorNotFound, SNotRestriction, SExistRestriction
import inetmapi

from kopano import Restriction, Server, parser


def logger(options):
    logging.basicConfig(stream=sys.stdout, level=options.loglevel)
    return logging.getLogger('optimize-imap')


def generate_imap_message(item):
    eml = item.eml(received_date=True)
    envelope, body, bodystructure = inetmapi.createIMAPProperties(eml)

    item.create_prop(PR_EC_IMAP_EMAIL, eml)
    item.create_prop(PR_EC_IMAP_EMAIL_SIZE, len(eml))
    item.create_prop(PR_EC_IMAP_BODYSTRUCTURE, bodystructure)
    item.create_prop(PR_EC_IMAP_BODY, body)
    item.create_prop('imap:1', envelope, proptype=PT_STRING8)
    item.mapiobj.SaveChanges(0)


def main():
    options, _ = parser('ksplu').parse_args()
    log = logger(options)
    server = Server(options=options, auth_user='SYSTEM', auth_pass='')
    restriction = Restriction(mapiobj=SNotRestriction(SExistRestriction(PR_EC_IMAP_EMAIL_SIZE)))

    for user in server.users(parse=True):  # XXX multi-company..
        # Skip users without IMAP enabled
        if not 'imap' in user.features:
            log.info('Skipping user %s, IMAP disabled', user.name)
            continue

        log.debug('Processing user %s', user.name)
        for folder in user.store.folders():
            # Inbox folder's container class is None..
            if folder.container_class != 'IPF.Note' and folder.container_class != None:
                continue

            log.info('Processing folder %s', folder.name)
            for item in folder.items(restriction=restriction):
                log.debug('Processing item %s', item.subject)
                generate_imap_message(item)


if __name__ == "__main__":
    main()