/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: Jelmer Vernooij
  • Date: 2020-04-05 19:11:34 UTC
  • mto: (7490.7.16 work)
  • mto: This revision was merged to the branch mainline in revision 7501.
  • Revision ID: jelmer@jelmer.uk-20200405191134-0aebh8ikiwygxma5
Populate the .gitignore file.

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
 
39
41
import os
40
42
from ctypes import *
41
43
 
44
46
LPLHANDLE = POINTER(LHANDLE)
45
47
 
46
48
# 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
 
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
61
63
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
 
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
76
78
# Recipient class
77
 
MAPI_ORIG       = 0
78
 
MAPI_TO         = 1
 
79
MAPI_ORIG = 0
 
80
MAPI_TO = 1
79
81
# Send flags
80
 
MAPI_LOGON_UI   = 1
81
 
MAPI_DIALOG     = 8
 
82
MAPI_LOGON_UI = 1
 
83
MAPI_DIALOG = 8
 
84
 
82
85
 
83
86
class MapiRecipDesc(Structure):
84
87
    _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),
 
88
        ('ulReserved', c_ulong),
 
89
        ('ulRecipClass', c_ulong),
 
90
        ('lpszName', c_char_p),
 
91
        ('lpszAddress', c_char_p),
 
92
        ('ulEIDSize', c_ulong),
 
93
        ('lpEntryID', c_void_p),
91
94
    ]
92
 
lpMapiRecipDesc  = POINTER(MapiRecipDesc)
 
95
 
 
96
 
 
97
lpMapiRecipDesc = POINTER(MapiRecipDesc)
93
98
lppMapiRecipDesc = POINTER(lpMapiRecipDesc)
94
99
 
 
100
 
95
101
class MapiFileDesc(Structure):
96
102
    _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),
 
103
        ('ulReserved', c_ulong),
 
104
        ('flFlags', c_ulong),
 
105
        ('nPosition', c_ulong),
 
106
        ('lpszPathName', c_char_p),
 
107
        ('lpszFileName', c_char_p),
 
108
        ('lpFileType', c_void_p),
103
109
    ]
 
110
 
 
111
 
104
112
lpMapiFileDesc = POINTER(MapiFileDesc)
105
113
 
 
114
 
106
115
class MapiMessage(Structure):
107
116
    _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),
 
117
        ('ulReserved', c_ulong),
 
118
        ('lpszSubject', c_char_p),
 
119
        ('lpszNoteText', c_char_p),
 
120
        ('lpszMessageType', c_char_p),
 
121
        ('lpszDateReceived', c_char_p),
 
122
        ('lpszConversationID', c_char_p),
 
123
        ('flFlags', FLAGS),
 
124
        ('lpOriginator', lpMapiRecipDesc),
 
125
        ('nRecipCount', c_ulong),
 
126
        ('lpRecips', lpMapiRecipDesc),
 
127
        ('nFileCount', c_ulong),
 
128
        ('lpFiles', lpMapiFileDesc),
120
129
    ]
 
130
 
 
131
 
121
132
lpMapiMessage = POINTER(MapiMessage)
122
133
 
123
 
MAPI                    = windll.mapi32
124
 
MAPISendMail            = MAPI.MAPISendMail
125
 
MAPISendMail.restype    = c_ulong
126
 
MAPISendMail.argtypes   = (LHANDLE, c_ulong, lpMapiMessage, FLAGS, c_ulong)
 
134
MAPI = windll.mapi32
 
135
MAPISendMail = MAPI.MAPISendMail
 
136
MAPISendMail.restype = c_ulong
 
137
MAPISendMail.argtypes = (LHANDLE, c_ulong, lpMapiMessage, FLAGS, c_ulong)
127
138
 
128
 
MAPIResolveName         = MAPI.MAPIResolveName
 
139
MAPIResolveName = MAPI.MAPIResolveName
129
140
MAPIResolveName.restype = c_ulong
130
 
MAPIResolveName.argtypes= (LHANDLE, c_ulong, c_char_p, FLAGS, c_ulong, lppMapiRecipDesc)
 
141
MAPIResolveName.argtypes = (
 
142
    LHANDLE, c_ulong, c_char_p, FLAGS, c_ulong, lppMapiRecipDesc)
131
143
 
132
 
MAPIFreeBuffer          = MAPI.MAPIFreeBuffer
133
 
MAPIFreeBuffer.restype  = c_ulong
 
144
MAPIFreeBuffer = MAPI.MAPIFreeBuffer
 
145
MAPIFreeBuffer.restype = c_ulong
134
146
MAPIFreeBuffer.argtypes = (c_void_p, )
135
147
 
136
 
MAPILogon               = MAPI.MAPILogon
137
 
MAPILogon.restype       = c_ulong
138
 
MAPILogon.argtypes      = (LHANDLE, c_char_p, c_char_p, FLAGS, c_ulong, LPLHANDLE)
 
148
MAPILogon = MAPI.MAPILogon
 
149
MAPILogon.restype = c_ulong
 
150
MAPILogon.argtypes = (LHANDLE, c_char_p, c_char_p, FLAGS, c_ulong, LPLHANDLE)
139
151
 
140
 
MAPILogoff              = MAPI.MAPILogoff
141
 
MAPILogoff.restype      = c_ulong
142
 
MAPILogoff.argtypes     = (LHANDLE, c_ulong, FLAGS, c_ulong)
 
152
MAPILogoff = MAPI.MAPILogoff
 
153
MAPILogoff.restype = c_ulong
 
154
MAPILogoff.argtypes = (LHANDLE, c_ulong, FLAGS, c_ulong)
143
155
 
144
156
 
145
157
class MAPIError(WindowsError):
156
168
    pSession = LHANDLE()
157
169
    rc = MAPILogon(0, profileName, password, MAPI_LOGON_UI, 0, byref(pSession))
158
170
    if rc != SUCCESS_SUCCESS:
159
 
        raise MAPIError, rc
 
171
        raise MAPIError(rc)
160
172
    return pSession
161
173
 
162
174
 
163
175
def _logoff(session):
164
176
    rc = MAPILogoff(session, 0, 0, 0)
165
177
    if rc != SUCCESS_SUCCESS:
166
 
        raise MAPIError, rc
 
178
        raise MAPIError(rc)
167
179
 
168
180
 
169
181
def _resolveName(session, name):
170
182
    pRecipDesc = lpMapiRecipDesc()
171
183
    rc = MAPIResolveName(session, 0, name, 0, 0, byref(pRecipDesc))
172
184
    if rc != SUCCESS_SUCCESS:
173
 
        raise MAPIError, rc
 
185
        raise MAPIError(rc)
174
186
    rd = pRecipDesc.contents
175
187
    name, address = rd.lpszName, rd.lpszAddress
176
188
    rc = MAPIFreeBuffer(pRecipDesc)
177
189
    if rc != SUCCESS_SUCCESS:
178
 
        raise MAPIError, rc
 
190
        raise MAPIError(rc)
179
191
    return name, address
180
192
 
181
193
 
217
229
 
218
230
    rc = MAPISendMail(session, 0, byref(msg), MAPI_DIALOG, 0)
219
231
    if rc != SUCCESS_SUCCESS:
220
 
        raise MAPIError, rc
 
232
        raise MAPIError(rc)
221
233
 
222
234
 
223
235
def SendMail(recipient, subject="", body="", attachfiles=""):
231
243
 
232
244
    attach = []
233
245
    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)
 
246
    for f in AttachWork:
 
247
        if os.path.exists(f):
 
248
            attach.append(os.path.abspath(f))
238
249
 
239
250
    restore = os.getcwd()
240
251
    try:
254
265
    body = "Hi,\r\n\r\nthis is a quick test message,\r\n\r\ncheers,\r\nJohn."
255
266
    attachment = sys.argv[0]
256
267
    SendMail(recipient, subject, body, attachment)
257