Welcome, guest | Sign In | My Account | Store | Cart

some utilities for working with GroupWise through COM-interface

Python, 90 lines
 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
from __future__ import print_function, unicode_literals

import win32com.client
import codecs

FILE = 1
DRAFT = 4

def login(user, password):
    app = win32com.client.Dispatch('NovellGroupWareSession')
    account = app.MultiLogin(user, None, password, 1)
        # 0 - promptIfNeeded
        # 1 - noverPrompt
        # 2 - allowPasswordPrompt

def incoming(account):
    return account.MailBox.Messages.Find('(mail) and (box_type = incoming)')

def msg_atts(msg):
    'att generator'
    for att in msg.Attachments:
        if att.ObjType == FILE:
            fn = att.FileName
            if not fn:
                continue
            elif fn == 'Mime.822':
                # email from Thunderbird through smtp
                continue
            elif fn == 'Header':
                # forwarded from Thunderbird through smtp
                continue
        yield att
    return None

def att_save(att, fpath):
    if att.AttachmentSize > 0:
        att.Save(fpath)
    else:
        # GW-error workaround, cat > fpath
        with open(fpath, 'wb'):
            pass

def msg_move(msg, fromFolder, toFolder):
    fromFolder.Messages.Move(msg, toFolder.Messages)

def msg_move2(msg, toFolder):
    'move from Inbox'
    inbox = msg.Parent.MailBox
    folders = msg.EnclosingFolders
    if inbox in folders:
        msg_move(msg, inbox, toFolder)
    elif not toFolder in folders:
        toFolder.Messages.Add(msg)
        
class AttStream:
    def __init__(self, att):
        self.stream = att.Stream
        self.size = att.AttachmentSize
    def read(self, size = -1):
        if size < 0:
            size = self.size
        data =  self.stream.Read(size)
        return str(data)
    def close(self):
        pass
        
def att_text(att, encoding):
    fp = AttStream(att)
    return fp.read().decode(encoding)
    
def att_reader(att, encoding):
    '''
    with att_reader(att, encoding) as fp:
        do_something
    '''
    fp = AttStream(att)
    return codecs.getreader(encoding)(fp)

def create_msg(folder):
    return folder.Messages.Add('GW.MESSAGE.MAIL', DRAFT)

def add_recipients(msg, *addrL):
    for addr in addrL:
        msg.Recipients.Add(addr)
    
def add_file(msg, fpath, fn = None):
    if fn:
        msg.Attachments.Add(fpath, FILE, fn)
    else:
        msg.Attachments.Add(fpath, FILE)