/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 bzrlib/util/simplemapi.py

  • Committer: Richard Wilbur
  • Date: 2016-02-04 19:07:28 UTC
  • mto: This revision was merged to the branch mainline in revision 6618.
  • Revision ID: richard.wilbur@gmail.com-20160204190728-p0zvfii6zase0fw7
Update COPYING.txt from the original http://www.gnu.org/licenses/gpl-2.0.txt  (Only differences were in whitespace.)  Thanks to Petr Stodulka for pointing out the discrepancy.

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
 
 
 
82
MAPI_LOGON_UI   = 1
 
83
MAPI_DIALOG     = 8
83
84
 
84
85
class MapiRecipDesc(Structure):
85
86
    _fields_ = [
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),
 
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),
92
93
    ]
93
 
 
94
 
 
95
 
lpMapiRecipDesc = POINTER(MapiRecipDesc)
 
94
lpMapiRecipDesc  = POINTER(MapiRecipDesc)
96
95
lppMapiRecipDesc = POINTER(lpMapiRecipDesc)
97
96
 
98
 
 
99
97
class MapiFileDesc(Structure):
100
98
    _fields_ = [
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),
 
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),
107
105
    ]
108
 
 
109
 
 
110
106
lpMapiFileDesc = POINTER(MapiFileDesc)
111
107
 
112
 
 
113
108
class MapiMessage(Structure):
114
109
    _fields_ = [
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),
 
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),
127
122
    ]
128
 
 
129
 
 
130
123
lpMapiMessage = POINTER(MapiMessage)
131
124
 
132
 
MAPI = windll.mapi32
133
 
MAPISendMail = MAPI.MAPISendMail
134
 
MAPISendMail.restype = c_ulong
135
 
MAPISendMail.argtypes = (LHANDLE, c_ulong, lpMapiMessage, FLAGS, c_ulong)
 
125
MAPI                    = windll.mapi32
 
126
MAPISendMail            = MAPI.MAPISendMail
 
127
MAPISendMail.restype    = c_ulong
 
128
MAPISendMail.argtypes   = (LHANDLE, c_ulong, lpMapiMessage, FLAGS, c_ulong)
136
129
 
137
 
MAPIResolveName = MAPI.MAPIResolveName
 
130
MAPIResolveName         = MAPI.MAPIResolveName
138
131
MAPIResolveName.restype = c_ulong
139
 
MAPIResolveName.argtypes = (
140
 
    LHANDLE, c_ulong, c_char_p, FLAGS, c_ulong, lppMapiRecipDesc)
 
132
MAPIResolveName.argtypes= (LHANDLE, c_ulong, c_char_p, FLAGS, c_ulong, lppMapiRecipDesc)
141
133
 
142
 
MAPIFreeBuffer = MAPI.MAPIFreeBuffer
143
 
MAPIFreeBuffer.restype = c_ulong
 
134
MAPIFreeBuffer          = MAPI.MAPIFreeBuffer
 
135
MAPIFreeBuffer.restype  = c_ulong
144
136
MAPIFreeBuffer.argtypes = (c_void_p, )
145
137
 
146
 
MAPILogon = MAPI.MAPILogon
147
 
MAPILogon.restype = c_ulong
148
 
MAPILogon.argtypes = (LHANDLE, c_char_p, c_char_p, FLAGS, c_ulong, LPLHANDLE)
 
138
MAPILogon               = MAPI.MAPILogon
 
139
MAPILogon.restype       = c_ulong
 
140
MAPILogon.argtypes      = (LHANDLE, c_char_p, c_char_p, FLAGS, c_ulong, LPLHANDLE)
149
141
 
150
 
MAPILogoff = MAPI.MAPILogoff
151
 
MAPILogoff.restype = c_ulong
152
 
MAPILogoff.argtypes = (LHANDLE, c_ulong, FLAGS, c_ulong)
 
142
MAPILogoff              = MAPI.MAPILogoff
 
143
MAPILogoff.restype      = c_ulong
 
144
MAPILogoff.argtypes     = (LHANDLE, c_ulong, FLAGS, c_ulong)
153
145
 
154
146
 
155
147
class MAPIError(WindowsError):
166
158
    pSession = LHANDLE()
167
159
    rc = MAPILogon(0, profileName, password, MAPI_LOGON_UI, 0, byref(pSession))
168
160
    if rc != SUCCESS_SUCCESS:
169
 
        raise MAPIError(rc)
 
161
        raise MAPIError, rc
170
162
    return pSession
171
163
 
172
164
 
173
165
def _logoff(session):
174
166
    rc = MAPILogoff(session, 0, 0, 0)
175
167
    if rc != SUCCESS_SUCCESS:
176
 
        raise MAPIError(rc)
 
168
        raise MAPIError, rc
177
169
 
178
170
 
179
171
def _resolveName(session, name):
180
172
    pRecipDesc = lpMapiRecipDesc()
181
173
    rc = MAPIResolveName(session, 0, name, 0, 0, byref(pRecipDesc))
182
174
    if rc != SUCCESS_SUCCESS:
183
 
        raise MAPIError(rc)
 
175
        raise MAPIError, rc
184
176
    rd = pRecipDesc.contents
185
177
    name, address = rd.lpszName, rd.lpszAddress
186
178
    rc = MAPIFreeBuffer(pRecipDesc)
187
179
    if rc != SUCCESS_SUCCESS:
188
 
        raise MAPIError(rc)
 
180
        raise MAPIError, rc
189
181
    return name, address
190
182
 
191
183
 
227
219
 
228
220
    rc = MAPISendMail(session, 0, byref(msg), MAPI_DIALOG, 0)
229
221
    if rc != SUCCESS_SUCCESS:
230
 
        raise MAPIError(rc)
 
222
        raise MAPIError, rc
231
223
 
232
224
 
233
225
def SendMail(recipient, subject="", body="", attachfiles=""):
241
233
 
242
234
    attach = []
243
235
    AttachWork = attachfiles.split(';')
244
 
    for f in AttachWork:
245
 
        if os.path.exists(f):
246
 
            attach.append(os.path.abspath(f))
 
236
    for file in AttachWork:
 
237
        if os.path.exists(file):
 
238
            attach.append(file)
 
239
    attach = map(os.path.abspath, attach)
247
240
 
248
241
    restore = os.getcwd()
249
242
    try:
263
256
    body = "Hi,\r\n\r\nthis is a quick test message,\r\n\r\ncheers,\r\nJohn."
264
257
    attachment = sys.argv[0]
265
258
    SendMail(recipient, subject, body, attachment)
 
259