/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/util/simplemapi.py

  • Committer: Breezy landing bot
  • Author(s): Jelmer Vernooij
  • Date: 2020-08-23 01:15:41 UTC
  • mfrom: (7520.1.4 merge-3.1)
  • Revision ID: breezy.the.bot@gmail.com-20200823011541-nv0oh7nzaganx2qy
Merge lp:brz/3.1.

Merged from https://code.launchpad.net/~jelmer/brz/merge-3.1/+merge/389690

Show diffs side-by-side

added added

removed removed

Lines of Context:
44
44
LPLHANDLE = POINTER(LHANDLE)
45
45
 
46
46
# Return codes
47
 
SUCCESS_SUCCESS                 = 0
48
 
MAPI_USER_ABORT                 = 1
49
 
MAPI_E_USER_ABORT               = MAPI_USER_ABORT
50
 
MAPI_E_FAILURE                  = 2
51
 
MAPI_E_LOGON_FAILURE            = 3
52
 
MAPI_E_LOGIN_FAILURE            = MAPI_E_LOGON_FAILURE
53
 
MAPI_E_DISK_FULL                = 4
54
 
MAPI_E_INSUFFICIENT_MEMORY      = 5
55
 
MAPI_E_ACCESS_DENIED            = 6
56
 
MAPI_E_TOO_MANY_SESSIONS        = 8
57
 
MAPI_E_TOO_MANY_FILES           = 9
58
 
MAPI_E_TOO_MANY_RECIPIENTS      = 10
59
 
MAPI_E_ATTACHMENT_NOT_FOUND     = 11
60
 
MAPI_E_ATTACHMENT_OPEN_FAILURE  = 12
 
47
SUCCESS_SUCCESS = 0
 
48
MAPI_USER_ABORT = 1
 
49
MAPI_E_USER_ABORT = MAPI_USER_ABORT
 
50
MAPI_E_FAILURE = 2
 
51
MAPI_E_LOGON_FAILURE = 3
 
52
MAPI_E_LOGIN_FAILURE = MAPI_E_LOGON_FAILURE
 
53
MAPI_E_DISK_FULL = 4
 
54
MAPI_E_INSUFFICIENT_MEMORY = 5
 
55
MAPI_E_ACCESS_DENIED = 6
 
56
MAPI_E_TOO_MANY_SESSIONS = 8
 
57
MAPI_E_TOO_MANY_FILES = 9
 
58
MAPI_E_TOO_MANY_RECIPIENTS = 10
 
59
MAPI_E_ATTACHMENT_NOT_FOUND = 11
 
60
MAPI_E_ATTACHMENT_OPEN_FAILURE = 12
61
61
MAPI_E_ATTACHMENT_WRITE_FAILURE = 13
62
 
MAPI_E_UNKNOWN_RECIPIENT        = 14
63
 
MAPI_E_BAD_RECIPTYPE            = 15
64
 
MAPI_E_NO_MESSAGES              = 16
65
 
MAPI_E_INVALID_MESSAGE          = 17
66
 
MAPI_E_TEXT_TOO_LARGE           = 18
67
 
MAPI_E_INVALID_SESSION          = 19
68
 
MAPI_E_TYPE_NOT_SUPPORTED       = 20
69
 
MAPI_E_AMBIGUOUS_RECIPIENT      = 21
70
 
MAPI_E_AMBIG_RECIP              = MAPI_E_AMBIGUOUS_RECIPIENT
71
 
MAPI_E_MESSAGE_IN_USE           = 22
72
 
MAPI_E_NETWORK_FAILURE          = 23
73
 
MAPI_E_INVALID_EDITFIELDS       = 24
74
 
MAPI_E_INVALID_RECIPS           = 25
75
 
MAPI_E_NOT_SUPPORTED            = 26
 
62
MAPI_E_UNKNOWN_RECIPIENT = 14
 
63
MAPI_E_BAD_RECIPTYPE = 15
 
64
MAPI_E_NO_MESSAGES = 16
 
65
MAPI_E_INVALID_MESSAGE = 17
 
66
MAPI_E_TEXT_TOO_LARGE = 18
 
67
MAPI_E_INVALID_SESSION = 19
 
68
MAPI_E_TYPE_NOT_SUPPORTED = 20
 
69
MAPI_E_AMBIGUOUS_RECIPIENT = 21
 
70
MAPI_E_AMBIG_RECIP = MAPI_E_AMBIGUOUS_RECIPIENT
 
71
MAPI_E_MESSAGE_IN_USE = 22
 
72
MAPI_E_NETWORK_FAILURE = 23
 
73
MAPI_E_INVALID_EDITFIELDS = 24
 
74
MAPI_E_INVALID_RECIPS = 25
 
75
MAPI_E_NOT_SUPPORTED = 26
76
76
# Recipient class
77
 
MAPI_ORIG       = 0
78
 
MAPI_TO         = 1
 
77
MAPI_ORIG = 0
 
78
MAPI_TO = 1
79
79
# Send flags
80
 
MAPI_LOGON_UI   = 1
81
 
MAPI_DIALOG     = 8
 
80
MAPI_LOGON_UI = 1
 
81
MAPI_DIALOG = 8
 
82
 
82
83
 
83
84
class MapiRecipDesc(Structure):
84
85
    _fields_ = [
85
 
        ('ulReserved',      c_ulong),
86
 
        ('ulRecipClass',    c_ulong),
87
 
        ('lpszName',        c_char_p),
88
 
        ('lpszAddress',     c_char_p),
89
 
        ('ulEIDSize',       c_ulong),
90
 
        ('lpEntryID',       c_void_p),
 
86
        ('ulReserved', c_ulong),
 
87
        ('ulRecipClass', c_ulong),
 
88
        ('lpszName', c_char_p),
 
89
        ('lpszAddress', c_char_p),
 
90
        ('ulEIDSize', c_ulong),
 
91
        ('lpEntryID', c_void_p),
91
92
    ]
92
 
lpMapiRecipDesc  = POINTER(MapiRecipDesc)
 
93
 
 
94
 
 
95
lpMapiRecipDesc = POINTER(MapiRecipDesc)
93
96
lppMapiRecipDesc = POINTER(lpMapiRecipDesc)
94
97
 
 
98
 
95
99
class MapiFileDesc(Structure):
96
100
    _fields_ = [
97
 
        ('ulReserved',      c_ulong),
98
 
        ('flFlags',         c_ulong),
99
 
        ('nPosition',       c_ulong),
100
 
        ('lpszPathName',    c_char_p),
101
 
        ('lpszFileName',    c_char_p),
102
 
        ('lpFileType',      c_void_p),
 
101
        ('ulReserved', c_ulong),
 
102
        ('flFlags', c_ulong),
 
103
        ('nPosition', c_ulong),
 
104
        ('lpszPathName', c_char_p),
 
105
        ('lpszFileName', c_char_p),
 
106
        ('lpFileType', c_void_p),
103
107
    ]
 
108
 
 
109
 
104
110
lpMapiFileDesc = POINTER(MapiFileDesc)
105
111
 
 
112
 
106
113
class MapiMessage(Structure):
107
114
    _fields_ = [
108
 
        ('ulReserved',          c_ulong),
109
 
        ('lpszSubject',         c_char_p),
110
 
        ('lpszNoteText',        c_char_p),
111
 
        ('lpszMessageType',     c_char_p),
112
 
        ('lpszDateReceived',    c_char_p),
113
 
        ('lpszConversationID',  c_char_p),
114
 
        ('flFlags',             FLAGS),
115
 
        ('lpOriginator',        lpMapiRecipDesc),
116
 
        ('nRecipCount',         c_ulong),
117
 
        ('lpRecips',            lpMapiRecipDesc),
118
 
        ('nFileCount',          c_ulong),
119
 
        ('lpFiles',             lpMapiFileDesc),
 
115
        ('ulReserved', c_ulong),
 
116
        ('lpszSubject', c_char_p),
 
117
        ('lpszNoteText', c_char_p),
 
118
        ('lpszMessageType', c_char_p),
 
119
        ('lpszDateReceived', c_char_p),
 
120
        ('lpszConversationID', c_char_p),
 
121
        ('flFlags', FLAGS),
 
122
        ('lpOriginator', lpMapiRecipDesc),
 
123
        ('nRecipCount', c_ulong),
 
124
        ('lpRecips', lpMapiRecipDesc),
 
125
        ('nFileCount', c_ulong),
 
126
        ('lpFiles', lpMapiFileDesc),
120
127
    ]
 
128
 
 
129
 
121
130
lpMapiMessage = POINTER(MapiMessage)
122
131
 
123
 
MAPI                    = windll.mapi32
124
 
MAPISendMail            = MAPI.MAPISendMail
125
 
MAPISendMail.restype    = c_ulong
126
 
MAPISendMail.argtypes   = (LHANDLE, c_ulong, lpMapiMessage, FLAGS, c_ulong)
 
132
MAPI = windll.mapi32
 
133
MAPISendMail = MAPI.MAPISendMail
 
134
MAPISendMail.restype = c_ulong
 
135
MAPISendMail.argtypes = (LHANDLE, c_ulong, lpMapiMessage, FLAGS, c_ulong)
127
136
 
128
 
MAPIResolveName         = MAPI.MAPIResolveName
 
137
MAPIResolveName = MAPI.MAPIResolveName
129
138
MAPIResolveName.restype = c_ulong
130
 
MAPIResolveName.argtypes= (LHANDLE, c_ulong, c_char_p, FLAGS, c_ulong, lppMapiRecipDesc)
 
139
MAPIResolveName.argtypes = (
 
140
    LHANDLE, c_ulong, c_char_p, FLAGS, c_ulong, lppMapiRecipDesc)
131
141
 
132
 
MAPIFreeBuffer          = MAPI.MAPIFreeBuffer
133
 
MAPIFreeBuffer.restype  = c_ulong
 
142
MAPIFreeBuffer = MAPI.MAPIFreeBuffer
 
143
MAPIFreeBuffer.restype = c_ulong
134
144
MAPIFreeBuffer.argtypes = (c_void_p, )
135
145
 
136
 
MAPILogon               = MAPI.MAPILogon
137
 
MAPILogon.restype       = c_ulong
138
 
MAPILogon.argtypes      = (LHANDLE, c_char_p, c_char_p, FLAGS, c_ulong, LPLHANDLE)
 
146
MAPILogon = MAPI.MAPILogon
 
147
MAPILogon.restype = c_ulong
 
148
MAPILogon.argtypes = (LHANDLE, c_char_p, c_char_p, FLAGS, c_ulong, LPLHANDLE)
139
149
 
140
 
MAPILogoff              = MAPI.MAPILogoff
141
 
MAPILogoff.restype      = c_ulong
142
 
MAPILogoff.argtypes     = (LHANDLE, c_ulong, FLAGS, c_ulong)
 
150
MAPILogoff = MAPI.MAPILogoff
 
151
MAPILogoff.restype = c_ulong
 
152
MAPILogoff.argtypes = (LHANDLE, c_ulong, FLAGS, c_ulong)
143
153
 
144
154
 
145
155
class MAPIError(WindowsError):
156
166
    pSession = LHANDLE()
157
167
    rc = MAPILogon(0, profileName, password, MAPI_LOGON_UI, 0, byref(pSession))
158
168
    if rc != SUCCESS_SUCCESS:
159
 
        raise MAPIError, rc
 
169
        raise MAPIError(rc)
160
170
    return pSession
161
171
 
162
172
 
163
173
def _logoff(session):
164
174
    rc = MAPILogoff(session, 0, 0, 0)
165
175
    if rc != SUCCESS_SUCCESS:
166
 
        raise MAPIError, rc
 
176
        raise MAPIError(rc)
167
177
 
168
178
 
169
179
def _resolveName(session, name):
170
180
    pRecipDesc = lpMapiRecipDesc()
171
181
    rc = MAPIResolveName(session, 0, name, 0, 0, byref(pRecipDesc))
172
182
    if rc != SUCCESS_SUCCESS:
173
 
        raise MAPIError, rc
 
183
        raise MAPIError(rc)
174
184
    rd = pRecipDesc.contents
175
185
    name, address = rd.lpszName, rd.lpszAddress
176
186
    rc = MAPIFreeBuffer(pRecipDesc)
177
187
    if rc != SUCCESS_SUCCESS:
178
 
        raise MAPIError, rc
 
188
        raise MAPIError(rc)
179
189
    return name, address
180
190
 
181
191
 
217
227
 
218
228
    rc = MAPISendMail(session, 0, byref(msg), MAPI_DIALOG, 0)
219
229
    if rc != SUCCESS_SUCCESS:
220
 
        raise MAPIError, rc
 
230
        raise MAPIError(rc)
221
231
 
222
232
 
223
233
def SendMail(recipient, subject="", body="", attachfiles=""):
231
241
 
232
242
    attach = []
233
243
    AttachWork = attachfiles.split(';')
234
 
    for file in AttachWork:
235
 
        if os.path.exists(file):
236
 
            attach.append(file)
237
 
    attach = map(os.path.abspath, attach)
 
244
    for f in AttachWork:
 
245
        if os.path.exists(f):
 
246
            attach.append(os.path.abspath(f))
238
247
 
239
248
    restore = os.getcwd()
240
249
    try:
254
263
    body = "Hi,\r\n\r\nthis is a quick test message,\r\n\r\ncheers,\r\nJohn."
255
264
    attachment = sys.argv[0]
256
265
    SendMail(recipient, subject, body, attachment)
257