/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): Colin Watson
  • Date: 2020-11-16 21:47:08 UTC
  • mfrom: (7521.1.1 remove-lp-workaround)
  • Revision ID: breezy.the.bot@gmail.com-20201116214708-jos209mgxi41oy15
Remove breezy.git workaround for bazaar.launchpad.net.

Merged from https://code.launchpad.net/~cjwatson/brz/remove-lp-workaround/+merge/393710

Show diffs side-by-side

added added

removed removed

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