-
Notifications
You must be signed in to change notification settings - Fork 1
/
soapquery.py
430 lines (337 loc) · 11.9 KB
/
soapquery.py
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# -*- coding: utf-8 -*-
from xml.etree import ElementTree as ET
from xml.parsers.expat import ExpatError
from helper.etsearch import elementsearch
from namespaces import *
import httplib
import base64
import re
import os
import sys
from re import *
# Make /tmp/{in,out}.SoapQuery files containing all queries and answers
debug = True
# workaround for a python bug (ref. soaplib)
httplib.HTTPConnection._http_vsn_str = 'HTTP/1.0'
types = "http://schemas.microsoft.com/exchange/services/2006/types"
messages = "http://schemas.microsoft.com/exchange/services/2006/messages"
class QueryError(Exception):
def __init__(self, str, query=None, result=None, status=None):
self.msg = str
self.query = query
self.status = status
self.maybe_write(query, 'query')
self.result = result
self.maybe_write(result, 'result')
def maybe_write(self, str, name):
if str:
tmpnam = os.tmpnam() + '.xml'
f = open(tmpnam, 'w')
f.write(str)
self.msg += "\n [%s written to %s]" %(name, tmpnam)
def __str__(self):
return repr(self.msg)
class SoapConn:
def __init__(self,host,https=False,user=None,password=None,auth=None):
self.host = sub('^http://', '', host)
self.https = https
self.user = user
self.password = password
self.headers = \
{
"Content-Type" : 'text/xml; charset="utf-8"',
"Accept" :
'application/soap+xml, application/dime, multipart/related, text/*',
"User-Agent" : 'MySoap/0.1',
}
if auth:
self.auth = auth
elif (self.user and self.password):
auth = base64.b64encode("%s:%s" %(self.user, self.password))
self.auth = ("Authorization", "Basic %s" % auth)
def make_connection(self):
if self.https:
conn = httplib.HTTPSConnection(self.host)
else:
conn = httplib.HTTPConnection(self.host)
if self.auth:
self.headers[self.auth[0]] = self.auth[1]
return conn
def do_query(self,path,query,action,extra_headers=[]):
headers = self.headers
headers["SOAPAction"] = action
headers["Content-Length"] = len(query)
for k,v in extra_headers:
headers[k] = v
conn = self.make_connection()
conn.request("POST", path, body=query, headers=headers)
res = conn.getresponse()
data = res.read()
if debug:
fin = open('/tmp/in.SoapQuery.log', 'a')
fout = open('/tmp/out.SoapQuery.log', 'a')
fin.write('\n' + '=' * 72 + '\n')
fin.write(query)
fout.write('\n' + '=' * 72 + '\n')
fout.write(data)
if str(res.status) not in ['200', '202']:
raise QueryError('Error: %s\n %s\n'%(res.status,
res.reason),
query=query, result=data, status=res.status)
return data
def soapmethod(f):
def new(self, *args, **kws):
xml = ET.XML(f(self, *args, **kws))
body = elementsearch(xml, "{http://schemas.xmlsoap.org/soap/envelope}Body")
if body == None:
raise ValueError("Could not find <soap:Body/> element")
soap_action = body.getchildren()[0].tag
return self.soapconn.do_query("/ews/Exchange.asmx",f(*args,**kws),soap_action)
class SoapQuery:
def msquery(self,string,header=""):
header += '<t:RequestServerVersion Version="Exchange2007_SP1"/>'
query = \
"""<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="%s" xmlns:t="%s">
<soap:Header>%s</soap:Header>
<soap:Body>
%s
</soap:Body>
</soap:Envelope>""" %(messages, types, header, string)
# Handy for debugging last request
f = open('/tmp/lastquery.xml', 'a')
f.write("\n\n\n"+query)
f.close()
try:
xml = ET.XML(query)
except ExpatError:
e = sys.exc_value
raise QueryError(str(e), query=query)
body = elementsearch(xml, "{http://schemas.xmlsoap.org/soap/envelope/}Body")
if body == None:
raise QueryError("Could not find soap:Body element", query=query)
soap_action = body.getchildren()[0].tag
soap_action = re.sub('{', '', soap_action)
soap_action = re.sub('}', '/', soap_action)
return self.soapconn.do_query("/ews/Exchange.asmx",query,soap_action)
def __init__(self,soapconn):
self.soapconn = soapconn
def get_folder(self,foldername):
return self.msquery("""
<GetFolder>
<FolderShape>
<t:BaseShape>AllProperties</t:BaseShape>
</FolderShape>
<FolderIds>
<t:DistinguishedFolderId Id=\"%s\"/>
</FolderIds>
</GetFolder>
""" % foldername)
def find_items(self,parent_folder,baseshape="AllProperties",extra_props=[]):
props = ""
for p in extra_props:
props += "<t:FieldURI FieldURI=\"%s\"/>" % p
if props != "":
props = """
<t:AdditionalProperties>
%s
</t:AdditionalProperties>""" % props
return self.msquery("""
<FindItem Traversal="Shallow">
<ItemShape>
<t:BaseShape>%s</t:BaseShape>
%s
</ItemShape>
<ParentFolderIds>
<t:DistinguishedFolderId Id="%s"/>
</ParentFolderIds>
</FindItem>
""" % (baseshape, props, parent_folder))
def delete_items(self,itemids,delete_type="MoveToDeletedItems",
send_cancellations="SendToNone"):
"""
delete_type: {HardDelete, SoftDelete, *MoveToDeletedItems*}
send_cancellations: {*SendToNone*, SendOnlyToAll, SendToAllAndSaveCopy}
"""
ids = ""
if type(itemids) != list:
itemids = [itemids]
for itemid,chkey in itemids:
ids += """<t:ItemId Id="%s"/>""" % itemid
return self.msquery("""
<DeleteItem DeleteType="%s"
SendMeetingCancellations="%s">
<ItemIds>
%s
</ItemIds>
</DeleteItem>
""" %(delete_type, send_cancellations, ids))
def create_items(self,calendar_items,send_invitations="SendToNone"):
"""
send_cancellations: {*SendToNone*, SendOnlyToAll, SendToAllAndSaveCopy}
"""
return self.msquery("""
<CreateItem SendMeetingInvitations="%s">
%s
</CreateItem>
"""%(send_invitations,
calendar_items))
def update_item(self, itemid, chkey, item_changes,
conflict_resolution='AlwaysOverwrite',
send_invitations='SendToNone'):
if chkey:
iid = '<t:ItemId Id="%s" ChangeKey="%s"/>' %(itemid,chkey)
else:
iid = '<t:ItemId Id="%s"/>' % itemid
return self.msquery("""
<UpdateItem ConflictResolution="%s"
MessageDisposition="SaveOnly"
SendMeetingInvitationsOrCancellations="%s">
<ItemChanges>
<t:ItemChange>
%s
%s
</t:ItemChange>
</ItemChanges>
</UpdateItem>
""" % (conflict_resolution, send_invitations, iid, item_changes))
def get_item(self, itemids, shape="AllProperties",
extra_props=""):
"""
Get an item
"""
props = ""
for p in extra_props:
props += "<t:FieldURI FieldURI=\"%s\"/>" % p
if props != "":
props = """
<t:AdditionalProperties>
%s
</t:AdditionalProperties>""" % props
ids = ""
if type(itemids) != list:
itemids = [itemids]
for itemid,chkey in itemids:
ids += """<t:ItemId Id="%s"/>""" % itemid
return self.msquery("""
<GetItem>
<ItemShape>
<t:BaseShape>%s</t:BaseShape>
%s
</ItemShape>
<ItemIds>
%s
</ItemIds>
</GetItem>
""" %(shape,props,ids))
def get_all_calendar_items(self,extra_props=None):
"""
Return all items with all needed properties
"""
r = ET.XML(self.find_items('calendar', baseshape="IdOnly"))
id_elems = elementsearch(r, t('ItemId'), all=True)
item_ids = [(i.attrib['Id'], i.attrib['ChangeKey']) for i in id_elems]
if len(item_ids) == 0: return "<Items></Items>"
if extra_props:
return self.get_item(item_ids, shape="IdOnly",
extra_props=extra_props)
else:
# By default, get items with body
return self.get_item(item_ids, extra_props=['item:Body'])
def get_attachment(self, ids):
if not type(ids) == list:
ids = [ids]
itemids = ""
for i in ids:
itemids += '<t:AttachmentId Id="%s"/>' % i
return self.msquery("""
<GetAttachment>
<AttachmentIds>
%s
</AttachmentIds>
</GetAttachment>
""" % itemids)
def get_named_attachment(self, itemid, name):
"""
Return the attachment with name `name'
"""
props = """
<t:FieldURI FieldURI="item:Attachments"/>
<t:FieldURI FieldURI="item:HasAttachments"/>"""
item_id, item_chkey = itemid
item = self.get_item((item_id, item_chkey),shape="IdOnly",
extra_props=props)
# Find all attachments
et = ET.XML(item)
attachments = elementsearch(et, t('ItemAttachment'), True)
def name_filter(e):
t_name = e.find(t('Name'))
if t_name == None: return False
return t_name.text == name
attachments = filter(name_filter, attachments)
# if len(attachments) > 1: oops?
if len(attachments) == 0:
return None
# Just take the first for now
attachment = attachments[0]
attachment_id = attachment.find(t('AttachmentId')).attrib['Id']
return self.get_attachment(attachment_id)
def get_attached_note(self, itemid, name):
"""
Get an attached note created by `createAttachedNote'
"""
attachment = self.get_named_attachment(itemid, name)
at_elem = ET.XML(attachment) # feilsjekking
body = elementsearch(at_elem, t('Body'))
return body.text
def delete_attachment(self, attId):
if not type(attId) == list:
attId = [attId]
attachments = ""
for aid in attId:
attachments += "<t:AttachmentId Id=\"%s\"/>" % aid
return self.msquery("""
<DeleteAttachment>
<AttachmentIds>%s</AttachmentIds>
</DeleteAttachment>
""" % attachments)
def create_attached_note(self, itemid, name, content):
"""
Create an Attachment to itemid = (parent_id,parent_chkey)
"""
parent_id, parent_chkey = itemid
return self.msquery("""
<CreateAttachment>
<ParentItemId Id="%s" ChangeKey="%s" />
<Attachments>
<t:ItemAttachment>
<t:Name>%s</t:Name>
<t:Message>
<t:Body BodyType="Text">%s</t:Body>
</t:Message>
</t:ItemAttachment>
</Attachments>
</CreateAttachment>
""" % (parent_id, parent_chkey, name, content))
def delete_named_attachment(self, itemid, name):
attachment = self.get_named_attachment(itemid, name)
at_elem = ET.XML(attachment)
print attachment
# TODO: feilsjekking
a_id_elem = elementsearch(at_elem, t('AttachmentId'))
a_id = a_id_elem.attrib['Id']
return self.delete_attachment(a_id)
def replace_attachment(self, itemid, name, new_content):
self.delete_named_attachment(itemid, name)
return self.create_attached_note(itemid, name, new_content)
def find_items_with_subject(self, subject):
items = self.get_all_calendar_items()
cal = Calendar.from_xml(items)
for i in cal.calendar_items:
if i.get('t:Subject') != subject:
cal.remove_calendaritem(i)
return ET.tostring(cal.et)