ews-api 0.1.0.a

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,476 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ # ntlm authentication works using httpclient and rubyntlm
4
+ Handsoap.http_driver = :http_client
5
+ Handsoap.xml_query_driver = :nokogiri
6
+
7
+ module EWS
8
+ # Implementation of Exchange Web Services
9
+ #
10
+ # @see http://msdn.microsoft.com/en-us/library/bb409286.aspx Exchange Web Services Operations
11
+ # @see http://msdn.microsoft.com/en-us/library/aa580545.aspx BaseShape
12
+ class Service < Handsoap::Service
13
+ def self.endpoint(uri)
14
+ super :uri => uri, :version => 1
15
+ end
16
+
17
+ @@username, @@password = nil, nil
18
+
19
+ def set_auth(username, password)
20
+ @@username, @@password = username, password
21
+ end
22
+
23
+ def on_after_create_http_request(req)
24
+ if @@username && @@password
25
+ req.set_auth @@username, @@password
26
+ end
27
+ end
28
+
29
+ def on_create_document(doc)
30
+ # register namespaces for the request
31
+ doc.alias 'tns', 'http://schemas.microsoft.com/exchange/services/2006/messages'
32
+ doc.alias 't', 'http://schemas.microsoft.com/exchange/services/2006/types'
33
+ end
34
+
35
+ def on_response_document(doc)
36
+ apply_namespaces! doc
37
+ parser.parse_response_message doc
38
+ end
39
+
40
+ def apply_namespaces!(doc)
41
+ doc.add_namespace 'soap', '"http://schemas.xmlsoap.org/soap/envelope'
42
+ doc.add_namespace 't', 'http://schemas.microsoft.com/exchange/services/2006/types'
43
+ doc.add_namespace 'm', 'http://schemas.microsoft.com/exchange/services/2006/messages'
44
+ end
45
+ # public methods
46
+
47
+ def resolve_names!
48
+ soap_action = 'http://schemas.microsoft.com/exchange/services/2006/messages/ResolveNames'
49
+ response = invoke('tns:ResolveNames', soap_action) do |message|
50
+ raise "TODO"
51
+ end
52
+ end
53
+
54
+ def expand_dl!
55
+ soap_action = 'http://schemas.microsoft.com/exchange/services/2006/messages/ExpandDL'
56
+ response = invoke('tns:ExpandDL', soap_action) do |message|
57
+ raise "TODO"
58
+ end
59
+ end
60
+
61
+ # Finds folders for the given parent folder.
62
+ #
63
+ # @param [Hash] opts Options to manipulate the FindFolder response
64
+ # @option opts [String, Symbol] :base_shape (Default) IdOnly, Default, AllProperties
65
+ #
66
+ # @example Request
67
+ # <FindFolder Traversal="Shallow" xmlns="http://schemas.microsoft.com/exchange/services/2006/messages">
68
+ # <FolderShape>
69
+ # <t:BaseShape>Default</t:BaseShape>
70
+ # </FolderShape>
71
+ # <ParentFolderIds>
72
+ # <t:DistinguishedFolderId Id="inbox"/>
73
+ # </ParentFolderIds>
74
+ # </FindFolder>
75
+ #
76
+ # @see http://msdn.microsoft.com/en-us/library/aa563918.aspx
77
+ # FindFolder
78
+ #
79
+ # @see http://msdn.microsoft.com/en-us/library/aa494311.aspx
80
+ # FolderShape
81
+ #
82
+ # @todo Support options
83
+ # Traversal: +Shallow, Deep, SoftDeleted+
84
+ # FolderShape: +IdOnly, Default, AllProperties+
85
+ def find_folder(parent_folder_name = :root, opts = {})
86
+ soap_action = 'http://schemas.microsoft.com/exchange/services/2006/messages/FindFolder'
87
+ opts[:base_shape] ||= :Default
88
+
89
+ response = invoke('tns:FindFolder', soap_action) do |find_folder|
90
+ find_folder.set_attr 'Traversal', 'Deep'
91
+ find_folder.add('tns:FolderShape') do |shape|
92
+ shape.add 't:BaseShape', opts[:base_shape]
93
+ end
94
+ find_folder.add('tns:ParentFolderIds') do |ids|
95
+ ids.add('t:DistinguishedFolderId') do |id|
96
+ id.set_attr 'Id', parent_folder_name.to_s.downcase
97
+ end
98
+ end
99
+ end
100
+ parser.parse_find_folder response.document
101
+ end
102
+
103
+ # @param [Hash] opts Options to manipulate the FindFolder response
104
+ # @option opts [String, Symbol] :base_shape (Default) IdOnly, Default, AllProperties
105
+ #
106
+ # @example Request
107
+ #
108
+ # <GetFolder xmlns="http://schemas.microsoft.com/exchange/services/2006/messages"
109
+ # xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
110
+ # <FolderShape>
111
+ # <t:BaseShape>Default</t:BaseShape>
112
+ # </FolderShape>
113
+ # <FolderIds>
114
+ # <t:DistinguishedFolderId Id="inbox"/>
115
+ # </FolderIds>
116
+ # </GetFolder>
117
+ #
118
+ # @see http://msdn.microsoft.com/en-us/library/aa580274.aspx
119
+ # MSDN - GetFolder operation
120
+ #
121
+ def get_folder(name = :root, opts = {})
122
+ soap_action = 'http://schemas.microsoft.com/exchange/services/2006/messages/GetFolder'
123
+ opts[:base_shape] ||= :Default
124
+
125
+ response = invoke('tns:GetFolder', soap_action) do |get_folder|
126
+ get_folder.add('tns:FolderShape') do |shape|
127
+ shape.add 't:BaseShape', opts[:base_shape]
128
+ end
129
+ get_folder.add('tns:FolderIds') do |ids|
130
+ ids.add('t:DistinguishedFolderId') { |id| id.set_attr 'Id', name }
131
+ end
132
+ end
133
+ parser.parse_get_folder response.document
134
+ end
135
+
136
+ def convert_id!
137
+ soap_action = 'http://schemas.microsoft.com/exchange/services/2006/messages/ConvertId'
138
+ response = invoke('tns:ConvertId', soap_action) do |message|
139
+ raise "TODO"
140
+ end
141
+ end
142
+
143
+ def create_folder!
144
+ soap_action = 'http://schemas.microsoft.com/exchange/services/2006/messages/CreateFolder'
145
+ response = invoke('tns:CreateFolder', soap_action) do |message|
146
+ raise "TODO"
147
+ end
148
+ end
149
+
150
+ def delete_folder!
151
+ soap_action = 'http://schemas.microsoft.com/exchange/services/2006/messages/DeleteFolder'
152
+ response = invoke('tns:DeleteFolder', soap_action) do |message|
153
+ raise "TODO"
154
+ end
155
+ end
156
+
157
+ def update_folder!
158
+ soap_action = 'http://schemas.microsoft.com/exchange/services/2006/messages/UpdateFolder'
159
+ response = invoke('tns:UpdateFolder', soap_action) do |message|
160
+ raise "TODO"
161
+ end
162
+ end
163
+
164
+ def move_folder!
165
+ soap_action = 'http://schemas.microsoft.com/exchange/services/2006/messages/MoveFolder'
166
+ response = invoke('tns:MoveFolder', soap_action) do |message|
167
+ raise "TODO"
168
+ end
169
+ end
170
+
171
+ def copy_folder!
172
+ soap_action = 'http://schemas.microsoft.com/exchange/services/2006/messages/CopyFolder'
173
+ response = invoke('tns:CopyFolder', soap_action) do |message|
174
+ raise "TODO"
175
+ end
176
+ end
177
+
178
+ def subscribe!
179
+ soap_action = 'http://schemas.microsoft.com/exchange/services/2006/messages/Subscribe'
180
+ response = invoke('tns:Subscribe', soap_action) do |message|
181
+ raise "TODO"
182
+ end
183
+ end
184
+
185
+ def unsubscribe!
186
+ soap_action = 'http://schemas.microsoft.com/exchange/services/2006/messages/Unsubscribe'
187
+ response = invoke('tns:Unsubscribe', soap_action) do |message|
188
+ raise "TODO"
189
+ end
190
+ end
191
+
192
+ def get_events
193
+ soap_action = 'http://schemas.microsoft.com/exchange/services/2006/messages/GetEvents'
194
+ response = invoke('tns:GetEvents', soap_action) do |message|
195
+ raise "TODO"
196
+ end
197
+ end
198
+
199
+ def sync_folder_hierarchy!
200
+ soap_action = 'http://schemas.microsoft.com/exchange/services/2006/messages/SyncFolderHierarchy'
201
+ response = invoke('tns:SyncFolderHierarchy', soap_action) do |message|
202
+ raise "TODO"
203
+ end
204
+ end
205
+
206
+ def sync_folder_items!
207
+ soap_action = 'http://schemas.microsoft.com/exchange/services/2006/messages/SyncFolderItems'
208
+ response = invoke('tns:SyncFolderItems', soap_action) do |message|
209
+ raise "TODO"
210
+ end
211
+ end
212
+
213
+ def create_managed_folder!
214
+ soap_action = 'http://schemas.microsoft.com/exchange/services/2006/messages/CreateManagedFolder'
215
+ response = invoke('tns:CreateManagedFolder', soap_action) do |message|
216
+ raise "TODO"
217
+ end
218
+ end
219
+
220
+ # @param [Hash] opts Options to manipulate the FindItem response
221
+ # @option opts [String, Symbol] :base_shape (Default) IdOnly, Default, AllProperties
222
+ #
223
+ # @example Request
224
+ # <FindItem xmlns="http://schemas.microsoft.com/exchange/services/2006/messages"
225
+ # xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types"
226
+ # Traversal="Shallow">
227
+ # <ItemShape>
228
+ # <t:BaseShape>IdOnly</t:BaseShape>
229
+ # </ItemShape>
230
+ # <ParentFolderIds>
231
+ # <t:DistinguishedFolderId Id="deleteditems"/>
232
+ # </ParentFolderIds>
233
+ # </FindItem>
234
+ #
235
+ #
236
+ # @see http://msdn.microsoft.com/en-us/library/aa566107(EXCHG.80).aspx
237
+ # FindItem - Exchange 2007
238
+ #
239
+ # @see http://msdn.microsoft.com/en-us/library/aa566107.aspx
240
+ # FindItem - Exchange 2010
241
+ #
242
+ # @see http://msdn.microsoft.com/en-us/library/aa580545.aspx
243
+ # BaseShape
244
+ def find_item(parent_folder_name = :root, opts = {})
245
+ soap_action = 'http://schemas.microsoft.com/exchange/services/2006/messages/FindItem'
246
+ opts[:base_shape] ||= :IdOnly
247
+
248
+ response = invoke('tns:FindItem', soap_action) do |find_item|
249
+ find_item.set_attr 'Traversal', 'Shallow'
250
+ find_item.add('tns:ItemShape') do |shape|
251
+ shape.add 't:BaseShape', opts[:base_shape]
252
+ end
253
+ find_item.add('tns:ParentFolderIds') do |ids|
254
+ ids.add('t:DistinguishedFolderId') do |folder_id|
255
+ folder_id.set_attr 'Id', parent_folder_name.to_s.downcase
256
+ end
257
+ end
258
+ end
259
+ parser.parse_find_item response.document
260
+ end
261
+
262
+ # @param [Hash] opts Options to manipulate the FindFolder response
263
+ # @option opts [String, Symbol] :base_shape (Default) IdOnly, Default, AllProperties
264
+ # @option opts [String] :change_key
265
+ #
266
+ # @example Request for getting a mail message
267
+ # <GetItem xmlns="http://schemas.microsoft.com/exchange/services/2006/messages"
268
+ # xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
269
+ # <ItemShape>
270
+ # <t:BaseShape>Default</t:BaseShape>
271
+ # <t:IncludeMimeContent>true</t:IncludeMimeContent>
272
+ # </ItemShape>
273
+ # <ItemIds>
274
+ # <t:ItemId Id="AAAlAF" ChangeKey="CQAAAB" />
275
+ # </ItemIds>
276
+ # </GetItem>
277
+ #
278
+ # @see http://msdn.microsoft.com/en-us/library/aa566013.aspx
279
+ # GetItem (E-mail Message)
280
+ #
281
+ # @see http://msdn.microsoft.com/en-us/library/aa565261.aspx
282
+ # ItemShape
283
+ #
284
+ # @see http://msdn.microsoft.com/en-us/library/aa563525.aspx
285
+ # ItmeIds
286
+ def get_item(item_id, opts = {})
287
+ soap_action = 'http://schemas.microsoft.com/exchange/services/2006/messages/GetItem'
288
+ opts[:base_shape] ||= :Default
289
+
290
+ response = invoke('tns:GetItem', soap_action) do |get_item|
291
+ get_item.add('tns:ItemShape') do |shape|
292
+ shape.add 't:BaseShape', opts[:base_shape]
293
+ shape.add 't:IncludeMimeContent', false
294
+ end
295
+ get_item.add('tns:ItemIds') do |ids|
296
+ ids.add('t:ItemId') do |item|
297
+ item.set_attr 'Id', item_id
298
+ if opts[:change_key]
299
+ item.set_attr 'ChangeKey', opts[:change_key]
300
+ end
301
+ end
302
+ end
303
+ end
304
+ parser.parse_get_item response.document
305
+ end
306
+
307
+ def create_item!
308
+ soap_action = 'http://schemas.microsoft.com/exchange/services/2006/messages/CreateItem'
309
+ response = invoke('tns:CreateItem', soap_action) do |message|
310
+ raise "TODO"
311
+ end
312
+ end
313
+
314
+ def delete_item!
315
+ soap_action = 'http://schemas.microsoft.com/exchange/services/2006/messages/DeleteItem'
316
+ response = invoke('tns:DeleteItem', soap_action) do |message|
317
+ raise "TODO"
318
+ end
319
+ end
320
+
321
+ def update_item!
322
+ soap_action = 'http://schemas.microsoft.com/exchange/services/2006/messages/UpdateItem'
323
+ response = invoke('tns:UpdateItem', soap_action) do |message|
324
+ raise "TODO"
325
+ end
326
+ end
327
+
328
+ def send_item!
329
+ soap_action = 'http://schemas.microsoft.com/exchange/services/2006/messages/SendItem'
330
+ response = invoke('tns:SendItem', soap_action) do |message|
331
+ raise "TODO"
332
+ end
333
+ end
334
+
335
+ # @param folder_id Name of the destination folder
336
+ # @param item_ids [Array] List of item ids to be moved
337
+ #
338
+ # @example Request
339
+ # <MoveItem xmlns="http://schemas.microsoft.com/exchange/services/2006/messages"
340
+ # xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
341
+ # <ToFolderId>
342
+ # <t:FolderId Id="drafts"/>
343
+ # </ToFolderId>
344
+ # <ItemIds>
345
+ # <t:ItemId Id="AAAtAEF/swbAAA=" ChangeKey="EwAAABYA/s4b"/>
346
+ # </ItemIds>
347
+ # </MoveItem>
348
+ #
349
+ # @see http://msdn.microsoft.com/en-us/library/aa565781%28EXCHG.80%29.aspx
350
+ # MoveItem
351
+ #
352
+ # @see http://msdn.microsoft.com/en-us/library/aa580808%28EXCHG.80%29.aspx
353
+ # DistinguishedFolderId
354
+ #
355
+ def move_item!(folder_id, item_ids)
356
+ soap_action = 'http://schemas.microsoft.com/exchange/services/2006/messages/MoveItem'
357
+ response = invoke('tns:MoveItem', soap_action) do |move_item|
358
+ move_item.add('tns:ToFolderId') do |to_folder|
359
+
360
+ # TODO: Support both FolderID and DistinguishedFolderId
361
+ to_folder.add('t:FolderId') do |folder_id_node|
362
+ folder_id_node.set_attr 'Id', folder_id
363
+ end
364
+ end
365
+ move_item.add('tns:ItemIds') do |ids|
366
+ item_ids.each do |item_id|
367
+ ids.add('t:ItemId') {|item_node| item_node.set_attr 'Id', item_id }
368
+ end
369
+ end
370
+ end
371
+ end
372
+
373
+ def copy_item!
374
+ soap_action = 'http://schemas.microsoft.com/exchange/services/2006/messages/CopyItem'
375
+ response = invoke('tns:CopyItem', soap_action) do |message|
376
+ raise "TODO"
377
+ end
378
+ end
379
+
380
+ def create_attachment!
381
+ soap_action = 'http://schemas.microsoft.com/exchange/services/2006/messages/CreateAttachment'
382
+ response = invoke('tns:CreateAttachment', soap_action) do |message|
383
+ raise "TODO"
384
+ end
385
+ end
386
+
387
+ def delete_attachment!
388
+ soap_action = 'http://schemas.microsoft.com/exchange/services/2006/messages/DeleteAttachment'
389
+ response = invoke('tns:DeleteAttachment', soap_action) do |message|
390
+ raise "TODO"
391
+ end
392
+ end
393
+
394
+ # @example Request
395
+ # <GetAttachment xmlns="http://schemas.microsoft.com/exchange/services/2006/messages"
396
+ # xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
397
+ # <AttachmentShape/>
398
+ # <AttachmentIds>
399
+ # <t:AttachmentId Id="AAAtAEFkbWluaX..."/>
400
+ # </AttachmentIds>
401
+ # </GetAttachment>
402
+ #
403
+ # @see http://msdn.microsoft.com/en-us/library/aa494316.aspx
404
+ # GetAttachment
405
+ def get_attachment(attachment_id)
406
+ soap_action = 'http://schemas.microsoft.com/exchange/services/2006/messages/GetAttachment'
407
+ response = invoke('tns:GetAttachment', soap_action) do |get_attachment|
408
+ get_attachment.add('tns:AttachmentShape')
409
+ get_attachment.add('tns:AttachmentIds') do |ids|
410
+ ids.add('t:AttachmentId') do |attachment|
411
+ attachment.set_attr 'Id', attachment_id
412
+ end
413
+ end
414
+ end
415
+ parser.parse_get_attachment response.document
416
+ end
417
+
418
+ def get_delegate
419
+ soap_action = 'http://schemas.microsoft.com/exchange/services/2006/messages/GetDelegate'
420
+ response = invoke('tns:GetDelegate', soap_action) do |message|
421
+ raise "TODO"
422
+ end
423
+ end
424
+
425
+ def add_delegate!
426
+ soap_action = 'http://schemas.microsoft.com/exchange/services/2006/messages/AddDelegate'
427
+ response = invoke('tns:AddDelegate', soap_action) do |message|
428
+ raise "TODO"
429
+ end
430
+ end
431
+
432
+ def remove_delegate!
433
+ soap_action = 'http://schemas.microsoft.com/exchange/services/2006/messages/RemoveDelegate'
434
+ response = invoke('tns:RemoveDelegate', soap_action) do |message|
435
+ raise "TODO"
436
+ end
437
+ end
438
+
439
+ def update_delegate!
440
+ soap_action = 'http://schemas.microsoft.com/exchange/services/2006/messages/UpdateDelegate'
441
+ response = invoke('tns:UpdateDelegate', soap_action) do |message|
442
+ raise "TODO"
443
+ end
444
+ end
445
+
446
+ def get_user_availability
447
+ soap_action = 'http://schemas.microsoft.com/exchange/services/2006/messages/GetUserAvailability'
448
+ response = invoke('tns:GetUserAvailability', soap_action) do |message|
449
+ raise "TODO"
450
+ end
451
+ end
452
+
453
+ def get_user_oof_settings
454
+ soap_action = 'http://schemas.microsoft.com/exchange/services/2006/messages/GetUserOofSettings'
455
+ response = invoke('tns:GetUserOofSettings', soap_action) do |message|
456
+ raise "TODO"
457
+ end
458
+ end
459
+
460
+ def set_user_oof_settings!
461
+ soap_action = 'http://schemas.microsoft.com/exchange/services/2006/messages/SetUserOofSettings'
462
+ response = invoke('tns:SetUserOofSettings', soap_action) do |message|
463
+ raise "TODO"
464
+ end
465
+ end
466
+
467
+ private
468
+ def parser
469
+ @parser ||= Parser.new
470
+ end
471
+
472
+ # helpers
473
+
474
+ # TODO
475
+ end
476
+ end