tilia-vobject 4.0.0.pre.alpha2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (193) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +19 -0
  3. data/.rubocop.yml +32 -0
  4. data/.simplecov +4 -0
  5. data/.travis.yml +3 -0
  6. data/CHANGELOG.sabre.md +626 -0
  7. data/CONTRIBUTING.md +25 -0
  8. data/Gemfile +17 -0
  9. data/Gemfile.lock +68 -0
  10. data/LICENSE +27 -0
  11. data/LICENSE.sabre +27 -0
  12. data/README.md +63 -0
  13. data/Rakefile +17 -0
  14. data/bin/vobject +7 -0
  15. data/lib/tilia/v_object/birthday_calendar_generator.rb +142 -0
  16. data/lib/tilia/v_object/cli.rb +582 -0
  17. data/lib/tilia/v_object/component/available.rb +107 -0
  18. data/lib/tilia/v_object/component/v_alarm.rb +114 -0
  19. data/lib/tilia/v_object/component/v_availability.rb +128 -0
  20. data/lib/tilia/v_object/component/v_calendar.rb +468 -0
  21. data/lib/tilia/v_object/component/v_card.rb +457 -0
  22. data/lib/tilia/v_object/component/v_event.rb +127 -0
  23. data/lib/tilia/v_object/component/v_free_busy.rb +81 -0
  24. data/lib/tilia/v_object/component/v_journal.rb +75 -0
  25. data/lib/tilia/v_object/component/v_time_zone.rb +51 -0
  26. data/lib/tilia/v_object/component/v_todo.rb +147 -0
  27. data/lib/tilia/v_object/component.rb +591 -0
  28. data/lib/tilia/v_object/date_time_parser.rb +486 -0
  29. data/lib/tilia/v_object/document.rb +218 -0
  30. data/lib/tilia/v_object/element_list.rb +18 -0
  31. data/lib/tilia/v_object/eof_exception.rb +8 -0
  32. data/lib/tilia/v_object/free_busy_data.rb +149 -0
  33. data/lib/tilia/v_object/free_busy_generator.rb +465 -0
  34. data/lib/tilia/v_object/i_tip/broker.rb +909 -0
  35. data/lib/tilia/v_object/i_tip/i_tip_exception.rb +9 -0
  36. data/lib/tilia/v_object/i_tip/message.rb +109 -0
  37. data/lib/tilia/v_object/i_tip/same_organizer_for_all_components_exception.rb +13 -0
  38. data/lib/tilia/v_object/i_tip.rb +10 -0
  39. data/lib/tilia/v_object/node.rb +192 -0
  40. data/lib/tilia/v_object/parameter.rb +327 -0
  41. data/lib/tilia/v_object/parse_exception.rb +7 -0
  42. data/lib/tilia/v_object/parser/json.rb +149 -0
  43. data/lib/tilia/v_object/parser/mime_dir.rb +543 -0
  44. data/lib/tilia/v_object/parser/parser.rb +61 -0
  45. data/lib/tilia/v_object/parser/xml/element/key_value.rb +60 -0
  46. data/lib/tilia/v_object/parser/xml/element.rb +11 -0
  47. data/lib/tilia/v_object/parser/xml.rb +322 -0
  48. data/lib/tilia/v_object/parser.rb +10 -0
  49. data/lib/tilia/v_object/property/binary.rb +96 -0
  50. data/lib/tilia/v_object/property/boolean.rb +57 -0
  51. data/lib/tilia/v_object/property/flat_text.rb +52 -0
  52. data/lib/tilia/v_object/property/float_value.rb +107 -0
  53. data/lib/tilia/v_object/property/i_calendar/cal_address.rb +49 -0
  54. data/lib/tilia/v_object/property/i_calendar/date.rb +15 -0
  55. data/lib/tilia/v_object/property/i_calendar/date_time.rb +330 -0
  56. data/lib/tilia/v_object/property/i_calendar/duration.rb +65 -0
  57. data/lib/tilia/v_object/property/i_calendar/period.rb +124 -0
  58. data/lib/tilia/v_object/property/i_calendar/recur.rb +173 -0
  59. data/lib/tilia/v_object/property/i_calendar.rb +14 -0
  60. data/lib/tilia/v_object/property/integer_value.rb +60 -0
  61. data/lib/tilia/v_object/property/text.rb +352 -0
  62. data/lib/tilia/v_object/property/time.rb +85 -0
  63. data/lib/tilia/v_object/property/unknown.rb +30 -0
  64. data/lib/tilia/v_object/property/uri.rb +78 -0
  65. data/lib/tilia/v_object/property/utc_offset.rb +56 -0
  66. data/lib/tilia/v_object/property/v_card/date.rb +31 -0
  67. data/lib/tilia/v_object/property/v_card/date_and_or_time.rb +343 -0
  68. data/lib/tilia/v_object/property/v_card/date_time.rb +22 -0
  69. data/lib/tilia/v_object/property/v_card/language_tag.rb +41 -0
  70. data/lib/tilia/v_object/property/v_card/time_stamp.rb +74 -0
  71. data/lib/tilia/v_object/property/v_card.rb +13 -0
  72. data/lib/tilia/v_object/property.rb +532 -0
  73. data/lib/tilia/v_object/reader.rb +73 -0
  74. data/lib/tilia/v_object/recur/event_iterator.rb +417 -0
  75. data/lib/tilia/v_object/recur/no_instances_exception.rb +11 -0
  76. data/lib/tilia/v_object/recur/r_date_iterator.rb +138 -0
  77. data/lib/tilia/v_object/recur/r_rule_iterator.rb +717 -0
  78. data/lib/tilia/v_object/recur.rb +10 -0
  79. data/lib/tilia/v_object/settings.rb +32 -0
  80. data/lib/tilia/v_object/splitter/i_calendar.rb +95 -0
  81. data/lib/tilia/v_object/splitter/splitter_interface.rb +31 -0
  82. data/lib/tilia/v_object/splitter/v_card.rb +56 -0
  83. data/lib/tilia/v_object/splitter.rb +9 -0
  84. data/lib/tilia/v_object/string_util.rb +58 -0
  85. data/lib/tilia/v_object/time_zone_data/exchange_zones.rb +96 -0
  86. data/lib/tilia/v_object/time_zone_data/lotus_zones.rb +104 -0
  87. data/lib/tilia/v_object/time_zone_data/php_zones.rb +49 -0
  88. data/lib/tilia/v_object/time_zone_data/windows_zones.rb +121 -0
  89. data/lib/tilia/v_object/time_zone_data.rb +10 -0
  90. data/lib/tilia/v_object/time_zone_util.rb +213 -0
  91. data/lib/tilia/v_object/uuid_util.rb +51 -0
  92. data/lib/tilia/v_object/v_card_converter.rb +354 -0
  93. data/lib/tilia/v_object/version.rb +9 -0
  94. data/lib/tilia/v_object/writer.rb +56 -0
  95. data/lib/tilia/v_object.rb +45 -0
  96. data/lib/tilia/vobject.rb +1 -0
  97. data/resources/schema/xcal.rng +1192 -0
  98. data/resources/schema/xcard.rng +388 -0
  99. data/test/test_helper.rb +56 -0
  100. data/test/v_object/attach_issue_test.rb +19 -0
  101. data/test/v_object/birthday_calendar_generator_test.rb +463 -0
  102. data/test/v_object/cli_mock.rb +19 -0
  103. data/test/v_object/cli_test.rb +460 -0
  104. data/test/v_object/component/available_test.rb +59 -0
  105. data/test/v_object/component/v_alarm_test.rb +160 -0
  106. data/test/v_object/component/v_availability_test.rb +388 -0
  107. data/test/v_object/component/v_calendar_test.rb +646 -0
  108. data/test/v_object/component/v_card_test.rb +258 -0
  109. data/test/v_object/component/v_event_test.rb +85 -0
  110. data/test/v_object/component/v_free_busy_test.rb +59 -0
  111. data/test/v_object/component/v_journal_test.rb +85 -0
  112. data/test/v_object/component/v_time_zone_test.rb +47 -0
  113. data/test/v_object/component/v_todo_test.rb +172 -0
  114. data/test/v_object/component_test.rb +419 -0
  115. data/test/v_object/date_time_parser_test.rb +526 -0
  116. data/test/v_object/document_test.rb +71 -0
  117. data/test/v_object/element_list_test.rb +27 -0
  118. data/test/v_object/em_client_test.rb +53 -0
  119. data/test/v_object/empty_parameter_test.rb +65 -0
  120. data/test/v_object/empty_value_issue_test.rb +25 -0
  121. data/test/v_object/fake_component.rb +21 -0
  122. data/test/v_object/free_busy_data_test.rb +285 -0
  123. data/test/v_object/free_busy_generator_test.rb +637 -0
  124. data/test/v_object/google_colon_escaping_test.rb +27 -0
  125. data/test/v_object/i_calendar/attach_parse_test.rb +24 -0
  126. data/test/v_object/i_tip/broker_attendee_reply_test.rb +1042 -0
  127. data/test/v_object/i_tip/broker_delete_event_test.rb +175 -0
  128. data/test/v_object/i_tip/broker_new_event_test.rb +440 -0
  129. data/test/v_object/i_tip/broker_process_message_test.rb +153 -0
  130. data/test/v_object/i_tip/broker_process_reply_test.rb +402 -0
  131. data/test/v_object/i_tip/broker_tester.rb +71 -0
  132. data/test/v_object/i_tip/broker_update_event_test.rb +763 -0
  133. data/test/v_object/i_tip/evolution_test.rb +2644 -0
  134. data/test/v_object/i_tip/message_test.rb +25 -0
  135. data/test/v_object/issue153.vcf +352 -0
  136. data/test/v_object/issue153_test.rb +12 -0
  137. data/test/v_object/issue26_test.rb +25 -0
  138. data/test/v_object/issue36_work_around_test.rb +37 -0
  139. data/test/v_object/issue40_test.rb +26 -0
  140. data/test/v_object/issue64.vcf +351 -0
  141. data/test/v_object/issue64_test.rb +17 -0
  142. data/test/v_object/issue96_test.rb +22 -0
  143. data/test/v_object/issue_undefined_index_test.rb +24 -0
  144. data/test/v_object/j_cal_test.rb +150 -0
  145. data/test/v_object/j_card_test.rb +192 -0
  146. data/test/v_object/line_folding_issue_test.rb +19 -0
  147. data/test/v_object/mock_document.rb +6 -0
  148. data/test/v_object/parameter_test.rb +109 -0
  149. data/test/v_object/parser/json_test.rb +370 -0
  150. data/test/v_object/parser/mime_dir_test.rb +14 -0
  151. data/test/v_object/parser/quoted_printable_test.rb +78 -0
  152. data/test/v_object/parser/xml_test.rb +2563 -0
  153. data/test/v_object/property/binary_test.rb +12 -0
  154. data/test/v_object/property/boolean_test.rb +18 -0
  155. data/test/v_object/property/compound_test.rb +43 -0
  156. data/test/v_object/property/float_test.rb +20 -0
  157. data/test/v_object/property/i_calendar/cal_address_test.rb +26 -0
  158. data/test/v_object/property/i_calendar/date_time_test.rb +303 -0
  159. data/test/v_object/property/i_calendar/duration_test.rb +14 -0
  160. data/test/v_object/property/i_calendar/recur_test.rb +39 -0
  161. data/test/v_object/property/text_test.rb +81 -0
  162. data/test/v_object/property/v_card/date_and_or_time_test.rb +205 -0
  163. data/test/v_object/property/v_card/language_tag_test.rb +35 -0
  164. data/test/v_object/property_test.rb +338 -0
  165. data/test/v_object/reader_test.rb +403 -0
  166. data/test/v_object/recur/event_iterator/by_month_in_daily_test.rb +52 -0
  167. data/test/v_object/recur/event_iterator/by_set_pos_hang_test.rb +55 -0
  168. data/test/v_object/recur/event_iterator/expand_floating_times_test.rb +109 -0
  169. data/test/v_object/recur/event_iterator/fifth_tuesday_problem_test.rb +45 -0
  170. data/test/v_object/recur/event_iterator/incorrect_expand_test.rb +53 -0
  171. data/test/v_object/recur/event_iterator/infinite_loop_problem_test.rb +75 -0
  172. data/test/v_object/recur/event_iterator/issue48_test.rb +43 -0
  173. data/test/v_object/recur/event_iterator/issue50_test.rb +123 -0
  174. data/test/v_object/recur/event_iterator/main_test.rb +1222 -0
  175. data/test/v_object/recur/event_iterator/missing_overridden_test.rb +55 -0
  176. data/test/v_object/recur/event_iterator/no_instances_test.rb +32 -0
  177. data/test/v_object/recur/event_iterator/override_first_event_test.rb +106 -0
  178. data/test/v_object/recur/r_date_iterator_test.rb +44 -0
  179. data/test/v_object/recur/r_rule_iterator_test.rb +608 -0
  180. data/test/v_object/recurrence_iterator/UntilRespectsTimezoneTest.ics +39 -0
  181. data/test/v_object/slash_r_test.rb +15 -0
  182. data/test/v_object/splitter/i_calendar_test.rb +299 -0
  183. data/test/v_object/splitter/v_card_test.rb +173 -0
  184. data/test/v_object/string_util_test.rb +37 -0
  185. data/test/v_object/test_case.rb +42 -0
  186. data/test/v_object/time_zone_util_test.rb +271 -0
  187. data/test/v_object/uuid_util_test.rb +18 -0
  188. data/test/v_object/v_card21_test.rb +43 -0
  189. data/test/v_object/v_card_converter_test.rb +419 -0
  190. data/test/v_object/version_test.rb +15 -0
  191. data/test/v_object/writer_test.rb +33 -0
  192. data/tilia-vobject.gemspec +17 -0
  193. metadata +308 -0
@@ -0,0 +1,909 @@
1
+ require 'digest'
2
+ module Tilia
3
+ module VObject
4
+ module ITip
5
+ # The ITip\Broker class is a utility class that helps with processing
6
+ # so-called iTip messages.
7
+ #
8
+ # iTip is defined in rfc5546, stands for iCalendar Transport-Independent
9
+ # Interoperability Protocol, and describes the underlying mechanism for
10
+ # using iCalendar for scheduling for for example through email (also known as
11
+ # IMip) and CalDAV Scheduling.
12
+ #
13
+ # This class helps by:
14
+ #
15
+ # 1. Creating individual invites based on an iCalendar event for each
16
+ # attendee.
17
+ # 2. Generating invite updates based on an iCalendar update. This may result
18
+ # in new invites, updates and cancellations for attendees, if that list
19
+ # changed.
20
+ # 3. On the receiving end, it can create a local iCalendar event based on
21
+ # a received invite.
22
+ # 4. It can also process an invite update on a local event, ensuring that any
23
+ # overridden properties from attendees are retained.
24
+ # 5. It can create a accepted or declined iTip reply based on an invite.
25
+ # 6. It can process a reply from an invite and update an events attendee
26
+ # status based on a reply.
27
+ class Broker
28
+ # This setting determines whether the rules for the SCHEDULE-AGENT
29
+ # parameter should be followed.
30
+ #
31
+ # This is a parameter defined on ATTENDEE properties, introduced by RFC
32
+ # 6638. This parameter allows a caldav client to tell the server 'Don't do
33
+ # any scheduling operations'.
34
+ #
35
+ # If this setting is turned on, any attendees with SCHEDULE-AGENT set to
36
+ # CLIENT will be ignored. This is the desired behavior for a CalDAV
37
+ # server, but if you're writing an iTip application that doesn't deal with
38
+ # CalDAV, you may want to ignore this parameter.
39
+ #
40
+ # @var bool
41
+ attr_accessor :schedule_agent_server_rules
42
+
43
+ # The broker will try during 'parseEvent' figure out whether the change
44
+ # was significant.
45
+ #
46
+ # It uses a few different ways to do this. One of these ways is seeing if
47
+ # certain properties changed values. This list of specified here.
48
+ #
49
+ # This list is taken from:
50
+ # * http://tools.ietf.org/html/rfc5546#section-2.1.4
51
+ #
52
+ # @var string[]
53
+ attr_accessor :significant_change_properties
54
+
55
+ # This method is used to process an incoming itip message.
56
+ #
57
+ # Examples:
58
+ #
59
+ # 1. A user is an attendee to an event. The organizer sends an updated
60
+ # meeting using a new iTip message with METHOD:REQUEST. This function
61
+ # will process the message and update the attendee's event accordingly.
62
+ #
63
+ # 2. The organizer cancelled the event using METHOD:CANCEL. We will update
64
+ # the users event to state STATUS:CANCELLED.
65
+ #
66
+ # 3. An attendee sent a reply to an invite using METHOD:REPLY. We can
67
+ # update the organizers event to update the ATTENDEE with its correct
68
+ # PARTSTAT.
69
+ #
70
+ # The existing_object is updated in-place. If there is no existing object
71
+ # (because it's a new invite for example) a new object will be created.
72
+ #
73
+ # If an existing object does not exist, and the method was CANCEL or
74
+ # REPLY, the message effectively gets ignored, and no 'existingObject'
75
+ # will be created.
76
+ #
77
+ # The updated existing_object is also returned from this function.
78
+ #
79
+ # If the iTip message was not supported, we will always return false.
80
+ #
81
+ # @param Message itip_message
82
+ # @param VCalendar existing_object
83
+ #
84
+ # @return VCalendar|null
85
+ def process_message(itip_message, existing_object = nil)
86
+ # We only support events at the moment.
87
+ return false unless itip_message.component == 'VEVENT'
88
+
89
+ case itip_message.method
90
+ when 'REQUEST'
91
+ process_message_request(itip_message, existing_object)
92
+ when 'CANCEL'
93
+ process_message_cancel(itip_message, existing_object)
94
+ when 'REPLY'
95
+ process_message_reply(itip_message, existing_object)
96
+ end
97
+ end
98
+
99
+ # This function parses a VCALENDAR object and figure out if any messages
100
+ # need to be sent.
101
+ #
102
+ # A VCALENDAR object will be created from the perspective of either an
103
+ # attendee, or an organizer. You must pass a string identifying the
104
+ # current user, so we can figure out who in the list of attendees or the
105
+ # organizer we are sending this message on behalf of.
106
+ #
107
+ # It's possible to specify the current user as an array, in case the user
108
+ # has more than one identifying href (such as multiple emails).
109
+ #
110
+ # It old_calendar is specified, it is assumed that the operation is
111
+ # updating an existing event, which means that we need to look at the
112
+ # differences between events, and potentially send old attendees
113
+ # cancellations, and current attendees updates.
114
+ #
115
+ # If calendar is null, but old_calendar is specified, we treat the
116
+ # operation as if the user has deleted an event. If the user was an
117
+ # organizer, this means that we need to send cancellation notices to
118
+ # people. If the user was an attendee, we need to make sure that the
119
+ # organizer gets the 'declined' message.
120
+ #
121
+ # @param VCalendar|string calendar
122
+ # @param string|array user_href
123
+ # @param VCalendar|string old_calendar
124
+ #
125
+ # @return array
126
+ def parse_event(calendar, user_href, old_calendar = nil)
127
+ if old_calendar
128
+ if old_calendar.is_a?(String)
129
+ old_calendar = Tilia::VObject::Reader.read(old_calendar)
130
+ end
131
+ unless old_calendar.key?('VEVENT')
132
+ # We only support events at the moment
133
+ return []
134
+ end
135
+
136
+ old_event_info = parse_event_info(old_calendar)
137
+ else
138
+ old_event_info = {
139
+ 'organizer' => nil,
140
+ 'significant_change_hash' => '',
141
+ 'attendees' => {}
142
+ }
143
+ end
144
+
145
+ user_href = [user_href] unless user_href.is_a?(Array)
146
+
147
+ if calendar
148
+ if calendar.is_a?(String)
149
+ calendar = Tilia::VObject::Reader.read(calendar)
150
+ end
151
+
152
+ unless calendar.key?('VEVENT')
153
+ # We only support events at the moment
154
+ return []
155
+ end
156
+
157
+ event_info = parse_event_info(calendar)
158
+ if (!event_info['attendees'] || event_info['attendees'].empty?) &&
159
+ (!old_event_info['attendees'] || old_event_info['attendees'].empty?)
160
+ # If there were no attendees on either side of the equation,
161
+ # we don't need to do anything.
162
+ return []
163
+ end
164
+
165
+ if event_info['organizer'].blank? && old_event_info['organizer'].blank?
166
+ # There was no organizer before or after the change.
167
+ return []
168
+ end
169
+
170
+ base_calendar = calendar
171
+
172
+ # If the new object didn't have an organizer, the organizer
173
+ # changed the object from a scheduling object to a non-scheduling
174
+ # object. We just copy the info from the old object.
175
+ if event_info['organizer'].blank? && !old_event_info['organizer'].blank?
176
+ event_info['organizer'] = old_event_info['organizer']
177
+ event_info['organizer_name'] = old_event_info['organizer_name']
178
+ end
179
+ else
180
+ # The calendar object got deleted, we need to process this as a
181
+ # cancellation / decline.
182
+ unless old_calendar
183
+ # No old and no new calendar, there's no thing to do.
184
+ return []
185
+ end
186
+
187
+ event_info = old_event_info.deep_dup
188
+
189
+ if user_href.include?(event_info['organizer'])
190
+ # This is an organizer deleting the event.
191
+ event_info['attendees'] = {}
192
+
193
+ # Increasing the sequence, but only if the organizer deleted
194
+ # the event.
195
+ event_info['sequence'] += 1
196
+ else
197
+ # This is an attendee deleting the event.
198
+ event_info['attendees'].each do |key, attendee|
199
+ if user_href.include?(attendee['href'])
200
+ event_info['attendees'][key]['instances'] = {
201
+ 'master' => { 'id' => 'master', 'partstat' => 'DECLINED' }
202
+ }
203
+ end
204
+ end
205
+ end
206
+
207
+ base_calendar = old_calendar
208
+ end
209
+
210
+ if user_href.include?(event_info['organizer'])
211
+ return parse_event_for_organizer(base_calendar, event_info, old_event_info)
212
+ elsif old_calendar
213
+ # We need to figure out if the user is an attendee, but we're only
214
+ # doing so if there's an oldCalendar, because we only want to
215
+ # process updates, not creation of new events.
216
+ event_info['attendees'].each do |_, attendee|
217
+ if user_href.include?(attendee['href'])
218
+ return parse_event_for_attendee(base_calendar, event_info, old_event_info, attendee['href'])
219
+ end
220
+ end
221
+ end
222
+
223
+ return []
224
+ end
225
+
226
+ protected
227
+
228
+ # Processes incoming REQUEST messages.
229
+ #
230
+ # This is message from an organizer, and is either a new event
231
+ # invite, or an update to an existing one.
232
+ #
233
+ #
234
+ # @param Message itip_message
235
+ # @param VCalendar existing_object
236
+ #
237
+ # @return VCalendar|null
238
+ def process_message_request(itip_message, existing_object = nil)
239
+ if !existing_object
240
+ # This is a new invite, and we're just going to copy over
241
+ # all the components from the invite.
242
+ existing_object = Tilia::VObject::Component::VCalendar.new
243
+ itip_message.message.components.each do |component|
244
+ existing_object.add(component.clone)
245
+ end
246
+ else
247
+ # We need to update an existing object with all the new
248
+ # information. We can just remove all existing components
249
+ # and create new ones.
250
+ existing_object.components.each do |component|
251
+ existing_object.remove(component)
252
+ end
253
+ itip_message.message.components.each do |component|
254
+ existing_object.add(component.clone)
255
+ end
256
+ end
257
+
258
+ existing_object
259
+ end
260
+
261
+ # Processes incoming CANCEL messages.
262
+ #
263
+ # This is a message from an organizer, and means that either an
264
+ # attendee got removed from an event, or an event got cancelled
265
+ # altogether.
266
+ #
267
+ # @param Message itip_message
268
+ # @param VCalendar existing_object
269
+ #
270
+ # @return VCalendar|null
271
+ def process_message_cancel(itip_message, existing_object = nil)
272
+ if !existing_object
273
+ # The event didn't exist in the first place, so we're just
274
+ # ignoring this message.
275
+ else
276
+ existing_object['VEVENT'].each do |vevent|
277
+ vevent['STATUS'] = 'CANCELLED'
278
+ vevent['SEQUENCE'] = itip_message.sequence
279
+ end
280
+ end
281
+
282
+ existing_object
283
+ end
284
+
285
+ # Processes incoming REPLY messages.
286
+ #
287
+ # The message is a reply. This is for example an attendee telling
288
+ # an organizer he accepted the invite, or declined it.
289
+ #
290
+ # @param Message itip_message
291
+ # @param VCalendar existing_object
292
+ #
293
+ # @return VCalendar|null
294
+ def process_message_reply(itip_message, existing_object = nil)
295
+ # A reply can only be processed based on an existing object.
296
+ # If the object is not available, the reply is ignored.
297
+ return nil unless existing_object
298
+
299
+ instances = {}
300
+ request_status = '2.0'
301
+
302
+ # Finding all the instances the attendee replied to.
303
+ itip_message.message['VEVENT'].each do |vevent|
304
+ recur_id = vevent.key?('RECURRENCE-ID') ? vevent['RECURRENCE-ID'].value : 'master'
305
+ attendee = vevent['ATTENDEE']
306
+ instances[recur_id] = attendee['PARTSTAT'].value
307
+ if vevent.key?('REQUEST-STATUS')
308
+ request_status = vevent['REQUEST-STATUS'].value
309
+ request_status = request_status.split(';').first
310
+ end
311
+ end
312
+
313
+ # Now we need to loop through the original organizer event, to find
314
+ # all the instances where we have a reply for.
315
+ master_object = nil
316
+ existing_object['VEVENT'].each do |vevent|
317
+ recur_id = vevent.key?('RECURRENCE-ID') ? vevent['RECURRENCE-ID'].value : 'master'
318
+ master_object = vevent if recur_id == 'master'
319
+
320
+ if instances.key?(recur_id)
321
+ attendee_found = false
322
+ if vevent.key?('ATTENDEE')
323
+ vevent['ATTENDEE'].each do |attendee|
324
+ if attendee.value == itip_message.sender
325
+ attendee_found = true
326
+ attendee['PARTSTAT'] = instances[recur_id]
327
+ attendee['SCHEDULE-STATUS'] = request_status
328
+ # Un-setting the RSVP status, because we now know
329
+ # that the attendee already replied.
330
+ attendee.delete('RSVP')
331
+ break
332
+ end
333
+ end
334
+ end
335
+
336
+ unless attendee_found
337
+ # Adding a new attendee. The iTip documentation calls this
338
+ # a party crasher.
339
+ attendee = vevent.add('ATTENDEE', itip_message.sender, 'PARTSTAT' => instances[recur_id])
340
+ if itip_message.sender_name
341
+ attendee['CN'] = itip_message.sender_name
342
+ end
343
+ end
344
+
345
+ instances.delete(recur_id)
346
+ end
347
+ end
348
+
349
+ unless master_object
350
+ # No master object, we can't add new instances.
351
+ return nil
352
+ end
353
+
354
+ # If we got replies to instances that did not exist in the
355
+ # original list, it means that new exceptions must be created.
356
+ instances.each do |recur_id, partstat|
357
+ recurrence_iterator = Tilia::VObject::Recur::EventIterator.new(existing_object, itip_message.uid)
358
+ found = false
359
+ iterations = 1000
360
+
361
+ new_object = nil
362
+ loop do
363
+ new_object = recurrence_iterator.event_object
364
+ recurrence_iterator.next
365
+
366
+ if new_object.key?('RECURRENCE-ID') && new_object['RECURRENCE-ID'].value == recur_id
367
+ found = true
368
+ end
369
+ iterations -= 1
370
+ break unless recurrence_iterator.valid && !found && iterations > 0
371
+ end
372
+
373
+ # Invalid recurrence id. Skipping this object.
374
+ next unless found
375
+
376
+ new_object.delete('RRULE')
377
+ new_object.delete('EXDATE')
378
+ new_object.delete('RDATE')
379
+
380
+ attendee_found = false
381
+ if new_object.key?('ATTENDEE')
382
+ new_object['ATTENDEE'].each do |attendee|
383
+ if attendee.value == itip_message.sender
384
+ attendee_found = true
385
+ attendee['PARTSTAT'] = partstat
386
+ break
387
+ end
388
+ end
389
+ end
390
+
391
+ unless attendee_found
392
+ # Adding a new attendee
393
+ attendee = new_object.add('ATTENDEE', itip_message.sender, 'PARTSTAT' => partstat )
394
+
395
+ if itip_message.sender_name
396
+ attendee['CN'] = itip_message.sender_name
397
+ end
398
+ end
399
+
400
+ existing_object.add(new_object)
401
+ end
402
+
403
+ existing_object
404
+ end
405
+
406
+ # This method is used in cases where an event got updated, and we
407
+ # potentially need to send emails to attendees to let them know of updates
408
+ # in the events.
409
+ #
410
+ # We will detect which attendees got added, which got removed and create
411
+ # specific messages for these situations.
412
+ #
413
+ # @param VCalendar calendar
414
+ # @param array event_info
415
+ # @param array old_event_info
416
+ #
417
+ # @return array
418
+ def parse_event_for_organizer(calendar, event_info, old_event_info)
419
+ # Merging attendee lists.
420
+ attendees = {}
421
+ old_event_info['attendees'].each do |_, attendee|
422
+ attendees[attendee['href']] = {
423
+ 'href' => attendee['href'],
424
+ 'oldInstances' => attendee['instances'],
425
+ 'newInstances' => {},
426
+ 'name' => attendee['name'],
427
+ 'forceSend' => nil
428
+ }
429
+ end
430
+ event_info['attendees'].each do |_, attendee|
431
+ if attendees.key?(attendee['href'])
432
+ attendees[attendee['href']]['name'] = attendee['name']
433
+ attendees[attendee['href']]['newInstances'] = attendee['instances']
434
+ attendees[attendee['href']]['forceSend'] = attendee['forceSend']
435
+ else
436
+ attendees[attendee['href']] = {
437
+ 'href' => attendee['href'],
438
+ 'oldInstances' => {},
439
+ 'newInstances' => attendee['instances'],
440
+ 'name' => attendee['name'],
441
+ 'forceSend' => attendee['forceSend']
442
+ }
443
+ end
444
+ end
445
+
446
+ messages = []
447
+
448
+ attendees.each do |_, attendee|
449
+ # An organizer can also be an attendee. We should not generate any
450
+ # messages for those.
451
+ next if attendee['href'] == event_info['organizer']
452
+
453
+ message = Tilia::VObject::ITip::Message.new
454
+ message.uid = event_info['uid']
455
+ message.component = 'VEVENT'
456
+ message.sequence = event_info['sequence']
457
+ message.sender = event_info['organizer']
458
+ message.sender_name = event_info['organizer_name']
459
+ message.recipient = attendee['href']
460
+ message.recipient_name = attendee['name']
461
+
462
+ if attendee['newInstances'].empty?
463
+ # If there are no instances the attendee is a part of, it
464
+ # means the attendee was removed and we need to send him a
465
+ # CANCEL.
466
+ message.method = 'CANCEL'
467
+
468
+ # Creating the new iCalendar body.
469
+ ical_msg = Tilia::VObject::Component::VCalendar.new
470
+ ical_msg['METHOD'] = message.method
471
+ event = ical_msg.add(
472
+ 'VEVENT',
473
+ 'UID' => message.uid,
474
+ 'SEQUENCE' => message.sequence
475
+ )
476
+ if calendar['VEVENT'].key?('SUMMARY')
477
+ event.add('SUMMARY', calendar['VEVENT']['SUMMARY'].value)
478
+ end
479
+
480
+ event.add(calendar['VEVENT']['DTSTART'].clone)
481
+ org = event.add('ORGANIZER', event_info['organizer'])
482
+ if event_info['organizer_name']
483
+ org['CN'] = event_info['organizer_name']
484
+ end
485
+ event.add(
486
+ 'ATTENDEE',
487
+ attendee['href'],
488
+ 'CN' => attendee['name']
489
+ )
490
+ message.significant_change = true
491
+ else
492
+ # The attendee gets the updated event body
493
+ message.method = 'REQUEST'
494
+
495
+ # Creating the new iCalendar body.
496
+ ical_msg = Tilia::VObject::Component::VCalendar.new
497
+ ical_msg['METHOD'] = message.method
498
+
499
+ calendar.select('VTIMEZONE').each do |timezone|
500
+ ical_msg.add(timezone.clone)
501
+ end
502
+
503
+ # We need to find out that this change is significant. If it's
504
+ # not, systems may opt to not send messages.
505
+ #
506
+ # We do this based on the 'significantChangeHash' which is
507
+ # some value that changes if there's a certain set of
508
+ # properties changed in the event, or simply if there's a
509
+ # difference in instances that the attendee is invited to.
510
+
511
+ message.significant_change =
512
+ attendee['forceSend'] == 'REQUEST' ||
513
+ attendee['oldInstances'].values != attendee['newInstances'].values ||
514
+ old_event_info['significant_change_hash'] != event_info['significant_change_hash']
515
+
516
+ attendee['newInstances'].each do |instance_id, _instance_info|
517
+ current_event = event_info['instances'][instance_id].clone
518
+ if instance_id == 'master'
519
+ # We need to find a list of events that the attendee
520
+ # is not a part of to add to the list of exceptions.
521
+ exceptions = []
522
+ event_info['instances'].each do |instance_id, _vevent|
523
+ unless attendee['newInstances'].key?(instance_id)
524
+ exceptions << instance_id
525
+ end
526
+ end
527
+
528
+ # If there were exceptions, we need to add it to an
529
+ # existing EXDATE property, if it exists.
530
+ if exceptions.any?
531
+ if current_event.key?('EXDATE')
532
+ current_event['EXDATE'].parts = current_event['EXDATE'].parts + exceptions
533
+ else
534
+ current_event['EXDATE'] = exceptions
535
+ end
536
+ end
537
+
538
+ # Cleaning up any scheduling information that
539
+ # shouldn't be sent along.
540
+ current_event['ORGANIZER'].delete('SCHEDULE-FORCE-SEND')
541
+ current_event['ORGANIZER'].delete('SCHEDULE-STATUS')
542
+
543
+ current_event['ATTENDEE'].each do |attendee|
544
+ attendee.delete('SCHEDULE-FORCE-SEND')
545
+ attendee.delete('SCHEDULE-STATUS')
546
+
547
+ # We're adding PARTSTAT=NEEDS-ACTION to ensure that
548
+ # iOS shows an "Inbox Item"
549
+ unless attendee.key?('PARTSTAT')
550
+ attendee['PARTSTAT'] = 'NEEDS-ACTION'
551
+ end
552
+ end
553
+ end
554
+
555
+ ical_msg.add(current_event)
556
+ end
557
+ end
558
+
559
+ message.message = ical_msg
560
+ messages << message
561
+ end
562
+
563
+ return messages
564
+ end
565
+
566
+ # Parse an event update for an attendee.
567
+ #
568
+ # This function figures out if we need to send a reply to an organizer.
569
+ #
570
+ # @param VCalendar calendar
571
+ # @param array event_info
572
+ # @param array old_event_info
573
+ # @param string attendee
574
+ #
575
+ # @return Message[]
576
+ def parse_event_for_attendee(calendar, event_info, old_event_info, attendee)
577
+ if schedule_agent_server_rules && event_info['organizer_schedule_agent'] == 'CLIENT'
578
+ return []
579
+ end
580
+
581
+ # Don't bother generating messages for events that have already been
582
+ # cancelled.
583
+ return [] if event_info['status'] == 'CANCELLED'
584
+
585
+ if old_event_info['attendees'].key?(attendee)
586
+ old_instances = old_event_info['attendees'][attendee]['instances'] || {}
587
+ else
588
+ old_instances = {}
589
+ end
590
+
591
+ instances = {}
592
+ old_instances.each do |_, instance|
593
+ instances[instance['id']] = {
594
+ 'id' => instance['id'],
595
+ 'oldstatus' => instance['partstat'],
596
+ 'newstatus' => nil
597
+ }
598
+ end
599
+
600
+ event_info['attendees'][attendee]['instances'].each do |_, instance|
601
+ if instances.key?(instance['id'])
602
+ instances[instance['id']]['newstatus'] = instance['partstat']
603
+ else
604
+ instances[instance['id']] = {
605
+ 'id' => instance['id'],
606
+ 'oldstatus' => nil,
607
+ 'newstatus' => instance['partstat']
608
+ }
609
+ end
610
+ end
611
+
612
+ # We need to also look for differences in EXDATE. If there are new
613
+ # items in EXDATE, it means that an attendee deleted instances of an
614
+ # event, which means we need to send DECLINED specifically for those
615
+ # instances.
616
+ # We only need to do that though, if the master event is not declined.
617
+ if instances.key?('master') && instances['master']['newstatus'] != 'DECLINED'
618
+ event_info['exdate'].each do |ex_date|
619
+ unless old_event_info['exdate'].include?(ex_date)
620
+ if instances.key?(ex_date)
621
+ instances[ex_date]['newstatus'] = 'DECLINED'
622
+ else
623
+ instances[ex_date] = {
624
+ 'id' => ex_date,
625
+ 'oldstatus' => nil,
626
+ 'newstatus' => 'DECLINED'
627
+ }
628
+ end
629
+ end
630
+ end
631
+ end
632
+
633
+ # Gathering a few extra properties for each instance.
634
+ instances.each do |recur_id, _instance_info|
635
+ if event_info['instances'].key?(recur_id)
636
+ instances[recur_id]['dtstart'] = event_info['instances'][recur_id]['DTSTART'].clone
637
+ else
638
+ instances[recur_id]['dtstart'] = recur_id
639
+ end
640
+ end
641
+
642
+ message = Tilia::VObject::ITip::Message.new
643
+ message.uid = event_info['uid']
644
+ message.method = 'REPLY'
645
+ message.component = 'VEVENT'
646
+ message.sequence = event_info['sequence']
647
+ message.sender = attendee
648
+ message.sender_name = event_info['attendees'][attendee]['name']
649
+ message.recipient = event_info['organizer']
650
+ message.recipient_name = event_info['organizer_name']
651
+
652
+ ical_msg = Tilia::VObject::Component::VCalendar.new
653
+ ical_msg['METHOD'] = 'REPLY'
654
+
655
+ has_reply = false
656
+
657
+ instances.each do |_, instance|
658
+ if instance['oldstatus'] == instance['newstatus'] && event_info['organizer_force_send'] != 'REPLY'
659
+ # Skip
660
+ next
661
+ end
662
+
663
+ event = ical_msg.add(
664
+ 'VEVENT',
665
+ 'UID' => message.uid,
666
+ 'SEQUENCE' => message.sequence
667
+ )
668
+
669
+ summary = calendar['VEVENT'].key?('SUMMARY') ? calendar['VEVENT']['SUMMARY'].value : ''
670
+ # Adding properties from the correct source instance
671
+ if event_info['instances'].key?(instance['id'])
672
+ instance_obj = event_info['instances'][instance['id']]
673
+
674
+ event.add(instance_obj['DTSTART'].clone)
675
+ if instance_obj.key?('SUMMARY')
676
+ event.add('SUMMARY', instance_obj['SUMMARY'].value)
677
+ elsif !summary.blank?
678
+ event.add('SUMMARY', summary)
679
+ end
680
+ else
681
+ # This branch of the code is reached, when a reply is
682
+ # generated for an instance of a recurring event, through the
683
+ # fact that the instance has disappeared by showing up in
684
+ # EXDATE
685
+ dt = Tilia::VObject::DateTimeParser.parse(instance['id'], event_info['timezone'])
686
+
687
+ # Treat is as a DATE field
688
+ if instance['id'].size <= 8
689
+ recur = event.add('DTSTART', dt, 'VALUE' => 'DATE')
690
+ else
691
+ recur = event.add('DTSTART', dt)
692
+ end
693
+
694
+ event.add('SUMMARY', summary) unless summary.blank?
695
+ end
696
+
697
+ if instance['id'] != 'master'
698
+ dt = Tilia::VObject::DateTimeParser.parse(instance['id'], event_info['timezone'])
699
+ # Treat is as a DATE field
700
+ if instance['id'].size <= 8
701
+ recur = event.add('RECURRENCE-ID', dt, 'VALUE' => 'DATE')
702
+ else
703
+ recur = event.add('RECURRENCE-ID', dt)
704
+ end
705
+ end
706
+
707
+ organizer = event.add('ORGANIZER', message.recipient)
708
+ organizer['CN'] = message.recipient_name if message.recipient_name
709
+
710
+ attendee = event.add(
711
+ 'ATTENDEE',
712
+ message.sender,
713
+ 'PARTSTAT' => instance['newstatus']
714
+ )
715
+ attendee['CN'] = message.sender_name if message.sender_name
716
+
717
+ has_reply = true
718
+ end
719
+
720
+ if has_reply
721
+ message.message = ical_msg
722
+ [message]
723
+ else
724
+ []
725
+ end
726
+ end
727
+
728
+ # Returns attendee information and information about instances of an
729
+ # event.
730
+ #
731
+ # Returns an array with the following keys:
732
+ #
733
+ # 1. uid
734
+ # 2. organizer
735
+ # 3. organizer_name
736
+ # 4. attendees
737
+ # 5. instances
738
+ #
739
+ # @param VCalendar calendar
740
+ #
741
+ # @return array
742
+ def parse_event_info(calendar = nil)
743
+ uid = nil
744
+ organizer = nil
745
+ organizer_name = nil
746
+ organizer_force_send = nil
747
+ sequence = nil
748
+ timezone = nil
749
+ status = nil
750
+ organizer_schedule_agent = 'SERVER'
751
+
752
+ significant_change_hash = ''
753
+
754
+ # Now we need to collect a list of attendees, and which instances they
755
+ # are a part of.
756
+ attendees = {}
757
+
758
+ instances = {}
759
+ exdate = []
760
+
761
+ calendar['VEVENT'].each do |vevent|
762
+ if uid.nil?
763
+ uid = vevent['UID'].value
764
+ else
765
+ if uid != vevent['UID'].value
766
+ fail Tilia::VObject::ITip::ITipException, 'If a calendar contained more than one event, they must have the same UID.'
767
+ end
768
+ end
769
+
770
+ unless vevent.key?('DTSTART')
771
+ fail Tilia::VObject::ITip::ITipException, 'An event MUST have a DTSTART property.'
772
+ end
773
+
774
+ if vevent.key?('ORGANIZER')
775
+ if organizer.nil?
776
+ organizer = vevent['ORGANIZER'].normalized_value
777
+ organizer_name = vevent['ORGANIZER'].key?('CN') ? vevent['ORGANIZER']['CN'] : nil
778
+ else
779
+ if organizer != vevent['ORGANIZER'].normalized_value
780
+ fail Tilia::VObject::ITip::SameOrganizerForAllComponentsException, 'Every instance of the event must have the same organizer.'
781
+ end
782
+ end
783
+ organizer_force_send =
784
+ vevent['ORGANIZER'].key?('SCHEDULE-FORCE-SEND') ?
785
+ vevent['ORGANIZER']['SCHEDULE-FORCE-SEND'].to_s.upcase :
786
+ nil
787
+ organizer_schedule_agent =
788
+ vevent['ORGANIZER'].key?('SCHEDULE-AGENT') ?
789
+ vevent['ORGANIZER']['SCHEDULE-AGENT'].to_s.upcase :
790
+ 'SERVER'
791
+ end
792
+
793
+ if sequence.nil? && vevent.key?('SEQUENCE')
794
+ sequence = vevent['SEQUENCE'].value
795
+ end
796
+
797
+ if vevent.key?('EXDATE')
798
+ vevent.select('EXDATE').each do |val|
799
+ exdate = exdate + val.parts
800
+ end
801
+ exdate.sort!
802
+ end
803
+
804
+ status = vevent['STATUS'].value.upcase if vevent.key?('STATUS')
805
+
806
+ recur_id = vevent.key?('RECURRENCE-ID') ? vevent['RECURRENCE-ID'].value : 'master'
807
+ if recur_id == 'master'
808
+ timezone = vevent['DTSTART'].date_time.time_zone
809
+ end
810
+
811
+ if vevent.key?('ATTENDEE')
812
+ vevent['ATTENDEE'].each do |attendee|
813
+ if schedule_agent_server_rules &&
814
+ attendee.key?('SCHEDULE-AGENT') &&
815
+ attendee['SCHEDULE-AGENT'].value.upcase == 'CLIENT'
816
+ next
817
+ end
818
+
819
+ part_stat =
820
+ attendee.key?('PARTSTAT') ?
821
+ attendee['PARTSTAT'].to_s.upcase :
822
+ 'NEEDS-ACTION'
823
+
824
+ force_send =
825
+ attendee.key?('SCHEDULE-FORCE-SEND') ?
826
+ attendee['SCHEDULE-FORCE-SEND'].to_s.upcase :
827
+ nil
828
+
829
+ if attendees.key?(attendee.normalized_value)
830
+ attendees[attendee.normalized_value]['instances'][recur_id] = {
831
+ 'id' => recur_id,
832
+ 'partstat' => part_stat,
833
+ 'force-send' => force_send
834
+ }
835
+ else
836
+ attendees[attendee.normalized_value] = {
837
+ 'href' => attendee.normalized_value,
838
+ 'instances' => {
839
+ recur_id => {
840
+ 'id' => recur_id,
841
+ 'partstat' => part_stat
842
+ }
843
+ },
844
+ 'name' => attendee.key?('CN') ? attendee['CN'].to_s : nil,
845
+ 'forceSend' => force_send
846
+ }
847
+ end
848
+ end
849
+
850
+ instances[recur_id] = vevent
851
+ end
852
+
853
+ significant_change_properties.each do |prop|
854
+ if vevent.key?(prop)
855
+ property_values = vevent.select(prop)
856
+
857
+ significant_change_hash += prop + ':'
858
+
859
+ if prop == 'EXDATE'
860
+ significant_change_hash += exdate.join(',') + ';'
861
+ else
862
+ property_values.each do |val|
863
+ significant_change_hash += val.value + ';'
864
+ end
865
+ end
866
+ end
867
+ end
868
+ end
869
+
870
+ significant_change_hash = Digest::MD5.hexdigest(significant_change_hash)
871
+
872
+ to_return = {}
873
+
874
+ to_return['uid'] = uid if uid
875
+ to_return['organizer'] = organizer if organizer
876
+ to_return['organizer_name'] = organizer_name if organizer_name
877
+ to_return['organizer_schedule_agent'] = organizer_schedule_agent if organizer_schedule_agent
878
+ to_return['organizer_force_send'] = organizer_force_send if organizer_force_send
879
+ to_return['instances'] = instances if instances
880
+ to_return['attendees'] = attendees if attendees
881
+ to_return['sequence'] = sequence if sequence
882
+ to_return['exdate'] = exdate if exdate
883
+ to_return['timezone'] = timezone if timezone
884
+ to_return['significant_change_hash'] = significant_change_hash if significant_change_hash
885
+ to_return['status'] = status if status
886
+
887
+ to_return
888
+ end
889
+
890
+ public
891
+
892
+ # TODO: document
893
+ def initialize
894
+ @schedule_agent_server_rules = true
895
+ @significant_change_properties = [
896
+ 'DTSTART',
897
+ 'DTEND',
898
+ 'DURATION',
899
+ 'DUE',
900
+ 'RRULE',
901
+ 'RDATE',
902
+ 'EXDATE',
903
+ 'STATUS'
904
+ ]
905
+ end
906
+ end
907
+ end
908
+ end
909
+ end