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,582 @@
1
+ require 'json'
2
+ module Tilia
3
+ module VObject
4
+ # This is the CLI interface for sabre-vobject.
5
+ class Cli
6
+ # No output.
7
+ #
8
+ # @var bool
9
+ # RUBY: attr_accessor :quiet
10
+
11
+ # Help display.
12
+ #
13
+ # @var bool
14
+ # RUBY: attr_accessor :show_help
15
+
16
+ # Wether to spit out 'mimedir' or 'json' format.
17
+ #
18
+ # @var string
19
+ # RUBY: attr_accessor :format
20
+
21
+ # JSON pretty print.
22
+ #
23
+ # @var bool
24
+ # RUBY: attr_accessor :pretty
25
+
26
+ # Source file.
27
+ #
28
+ # @var string
29
+ # RUBY: attr_accessor :input_path
30
+
31
+ # Destination file.
32
+ #
33
+ # @var string
34
+ # RUBY: attr_accessor :output_path
35
+
36
+ # output stream.
37
+ #
38
+ # @var resource
39
+ # RUBY: attr_accessor :stdout
40
+
41
+ # stdin.
42
+ #
43
+ # @var resource
44
+ # RUBY: attr_accessor :stdin
45
+
46
+ # stderr.
47
+ #
48
+ # @var resource
49
+ # RUBY: attr_accessor :stderr
50
+
51
+ # Input format (one of json or mimedir).
52
+ #
53
+ # @var string
54
+ # RUBY: attr_accessor :input_format
55
+
56
+ # Makes the parser less strict.
57
+ #
58
+ # @var bool
59
+ # RUBY: attr_accessor :forgiving
60
+
61
+ # Main function.
62
+ #
63
+ # @return int
64
+ def main(argv)
65
+ # @codeCoverageIgnoreStart
66
+ # We cannot easily test this, so we'll skip it. Pretty basic anyway.
67
+
68
+ @stderr = STDERR unless @stderr
69
+ @stdout = STDOUT unless @stdout
70
+ @stdin = STDIN unless @stdin
71
+
72
+ begin
73
+ (options, positional) = parse_arguments(argv)
74
+
75
+ @quiet = true if options['q']
76
+ log(colorize('green', 'tilia/vobject ') + colorize('yellow', Version::VERSION))
77
+
78
+ options.each do |name, value|
79
+ case name
80
+ when 'q'
81
+ when 'h', 'help'
82
+ show_help
83
+ return 0
84
+ when 'format'
85
+ formats = %w(jcard jcal vcard21 vcard30 vcard40 icalendar20 json mimedir icalendar vcard)
86
+ fail ArgumentError, "Unkown format: #{value}" unless formats.include?(value)
87
+ @format = value
88
+ when 'pretty'
89
+ @pretty = true
90
+ when 'forgiving'
91
+ @forgiving = true
92
+ when 'inputformat'
93
+ case value
94
+ # json formats
95
+ when 'jcard', 'jcal', 'json'
96
+ @input_format = 'json'
97
+ # mimedir formats
98
+ when 'mimedir', 'icalendar', 'vcard', 'vcard21', 'vcard30', 'vcard40', 'icalendar20'
99
+ @input_format = 'mimedir'
100
+ else
101
+ fail ArgumentError, "Unknown format: #{value}"
102
+ end
103
+ end
104
+ end
105
+
106
+ if positional.empty?
107
+ show_help
108
+ return 1
109
+ end
110
+
111
+ if positional.size == 1
112
+ fail ArgumentError, 'Inputfile is a required argument'
113
+ end
114
+
115
+ fail ArgumentError, 'Too many arguments' if positional.size > 3
116
+
117
+ unless %w(validate repair convert color).include?(positional[0])
118
+ fail ArgumentError, "Uknown command: #{positional[0]}"
119
+ end
120
+ rescue ArgumentError => e
121
+ show_help
122
+ log("Error: #{e}", 'red')
123
+ return 1
124
+ end
125
+
126
+ command = positional[0]
127
+
128
+ @input_path = positional[1]
129
+ @output_path = positional[2] ? positional[2] : '-'
130
+
131
+ @stdout = File.open(@output_path, 'w') if @output_path != '-'
132
+
133
+ unless @input_format
134
+ if @input_path[-5..-1] == '.json'
135
+ @input_format = 'json'
136
+ else
137
+ @input_format = 'mimedir'
138
+ end
139
+ end
140
+
141
+ unless @format
142
+ if @output_path[-5..-1] == '.json'
143
+ @format = 'json'
144
+ else
145
+ @format = 'mimedir'
146
+ end
147
+ end
148
+
149
+ real_code = 0
150
+
151
+ begin
152
+ loop do
153
+ input = read_input
154
+ break unless input
155
+
156
+ return_code = send(command, input)
157
+ real_code = return_code unless return_code == 0
158
+ end
159
+ rescue EofException => e
160
+ # end of file
161
+ rescue Exception => e
162
+ log("Error: #{e}", 'red')
163
+ return 2
164
+ end
165
+
166
+ real_code
167
+ end
168
+
169
+ protected
170
+
171
+ # Shows the help message.
172
+ #
173
+ # @return void
174
+ def show_help
175
+ log('Usage:', 'yellow')
176
+ log(' vobject [options] command [arguments]')
177
+ log('')
178
+ log('Options:', 'yellow')
179
+ log(colorize('green', ' -q ') + "Don't output anything.")
180
+ log(colorize('green', ' -help -h ') + 'Display this help message.')
181
+ log(colorize('green', ' --format ') + 'Convert to a specific format. Must be one of: vcard, vcard21,')
182
+ log(colorize('green', ' --forgiving ') + 'Makes the parser less strict.')
183
+ log(' vcard30, vcard40, icalendar20, jcal, jcard, json, mimedir.')
184
+ log(colorize('green', ' --inputformat ') + 'If the input format cannot be guessed from the extension, it')
185
+ log(' must be specified here.')
186
+ log(colorize('green', ' --pretty ') + 'json pretty-print.')
187
+ log('')
188
+ log('Commands:', 'yellow')
189
+ log(colorize('green', ' validate') + ' source_file Validates a file for correctness.')
190
+ log(colorize('green', ' repair') + ' source_file [output_file] Repairs a file.')
191
+ log(colorize('green', ' convert') + ' source_file [output_file] Converts a file.')
192
+ log(colorize('green', ' color') + ' source_file Colorize a file, useful for debbugging.')
193
+ log(
194
+ <<HELP
195
+
196
+ If source_file is set as '-', STDIN will be used.
197
+ If output_file is omitted, STDOUT will be used.
198
+ All other output is sent to STDERR.
199
+ HELP
200
+ )
201
+
202
+ log('Examples:', 'yellow')
203
+ log(' vobject convert contact.vcf contact.json')
204
+ log(' vobject convert --format=vcard40 old.vcf new.vcf')
205
+ log(' vobject convert --inputformat=json --format=mimedir - -')
206
+ log(' vobject color calendar.ics')
207
+ log('')
208
+ log('https://github.com/fruux/sabre-vobject', 'purple')
209
+ end
210
+
211
+ # Validates a VObject file.
212
+ #
213
+ # @param Component v_obj
214
+ #
215
+ # @return int
216
+ def validate(v_obj)
217
+ return_code = 0
218
+
219
+ case v_obj.name
220
+ when 'VCALENDAR'
221
+ log('iCalendar: ' + v_obj['VERSION'].to_s)
222
+ when 'VCARD'
223
+ log('vCard: ' + v_obj['VERSION'].to_s)
224
+ end
225
+
226
+ warnings = v_obj.validate
227
+ if warnings.empty?
228
+ log(' No warnings!')
229
+ else
230
+ levels = {
231
+ 1 => 'REPAIRED',
232
+ 2 => 'WARNING',
233
+ 3 => 'ERROR'
234
+ }
235
+
236
+ return_code = 2
237
+ warnings.each do |warning|
238
+ extra = ''
239
+ if warning['node'].is_a?(Property)
240
+ extra = ' (property: "' + warning['node'].name + '")'
241
+ end
242
+ log(' [' + levels[warning['level']] + '] ' + warning['message'] + extra)
243
+ end
244
+ end
245
+
246
+ return_code
247
+ end
248
+
249
+ # Repairs a VObject file.
250
+ #
251
+ # @param Component v_obj
252
+ #
253
+ # @return int
254
+ def repair(v_obj)
255
+ return_code = 0
256
+
257
+ case v_obj.name
258
+ when 'VCALENDAR'
259
+ log('iCalendar: ' + v_obj['VERSION'].to_s)
260
+ when 'VCARD'
261
+ log('vCard: ' + v_obj['VERSION'].to_s)
262
+ end
263
+
264
+ warnings = v_obj.validate(Node::REPAIR)
265
+ if warnings.empty?
266
+ log(' No warnings!')
267
+ else
268
+ levels = {
269
+ 1 => 'REPAIRED',
270
+ 2 => 'WARNING',
271
+ 3 => 'ERROR'
272
+ }
273
+
274
+ return_code = 2
275
+ warnings.each do |warning|
276
+ extra = ''
277
+ if warning['node'].is_a?(Property)
278
+ extra = ' (property: "' + warning['node'].name + '")'
279
+ end
280
+ log(' [' + levels[warning['level']] + '] ' + warning['message'] + extra)
281
+ end
282
+ end
283
+
284
+ @stdout.write(v_obj.serialize)
285
+
286
+ return_code
287
+ end
288
+
289
+ # Converts a vObject file to a new format.
290
+ #
291
+ # @param Component v_obj
292
+ #
293
+ # @return int
294
+ def convert(v_obj)
295
+ json = false
296
+ convert_version = nil
297
+ force_input = nil
298
+
299
+ case @format
300
+ when 'json'
301
+ json = true
302
+ convert_version = Document::VCARD40 if v_obj.name == 'VCARD'
303
+ when 'jcard'
304
+ json = true
305
+ force_input = 'VCARD'
306
+ convert_version = Document::VCARD40
307
+ when 'jcal'
308
+ json = true
309
+ force_input = 'VCALENDAR'
310
+ when 'mimedir', 'icalendar', 'icalendar20', 'vcard'
311
+ when 'vcard21'
312
+ convert_version = Document::VCARD21
313
+ when 'vcard30'
314
+ convert_version = Document::VCARD30
315
+ when 'vcard40'
316
+ convert_version = Document::VCARD40
317
+ end
318
+
319
+ if force_input && v_obj.name != force_input
320
+ fail "You cannot convert a #{v_obj.name.downcase} to #{@format}"
321
+ end
322
+
323
+ v_obj = v_obj.convert(convert_version) if convert_version
324
+ if json
325
+ if @pretty
326
+ @stdout.write(JSON.pretty_generate(v_obj.json_serialize))
327
+ else
328
+ @stdout.write(JSON.generate(v_obj.json_serialize))
329
+ end
330
+ else
331
+ @stdout.write(v_obj.serialize)
332
+ end
333
+
334
+ 0
335
+ end
336
+
337
+ # Colorizes a file.
338
+ #
339
+ # @param Component v_obj
340
+ #
341
+ # @return int
342
+ def color(v_obj)
343
+ @stdout.write(serialize_component(v_obj))
344
+ 0 # otherwise bytes written will be returned
345
+ end
346
+
347
+ # Returns an ansi color string for a color name.
348
+ #
349
+ # @param string color
350
+ #
351
+ # @return string
352
+ def colorize(color, str, reset_to = 'default')
353
+ colors = {
354
+ 'cyan' => '1;36',
355
+ 'red' => '1;31',
356
+ 'yellow' => '1;33',
357
+ 'blue' => '0;34',
358
+ 'green' => '0;32',
359
+ 'default' => '0',
360
+ 'purple' => '0;35'
361
+ }
362
+ "\033[#{colors[color]}m#{str}\033[#{colors[reset_to]}m"
363
+ end
364
+
365
+ # Writes out a string in specific color.
366
+ #
367
+ # @param string color
368
+ # @param string str
369
+ #
370
+ # @return void
371
+ def c_write(color, str)
372
+ @stdout.write(colorize(color, str))
373
+ end
374
+
375
+ def serialize_component(v_obj)
376
+ c_write('cyan', 'BEGIN')
377
+ c_write('red', ':')
378
+ c_write('yellow', v_obj.name + "\n")
379
+
380
+ # Gives a component a 'score' for sorting purposes.
381
+ #
382
+ # This is solely used by the childrenSort method.
383
+ #
384
+ # A higher score means the item will be lower in the list.
385
+ # To avoid score collisions, each "score category" has a reasonable
386
+ # space to accomodate elements. The key is added to the score to
387
+ # preserve the original relative order of elements.
388
+ #
389
+ # @param int key
390
+ # @param array array
391
+ #
392
+ # @return int
393
+ sort_score = lambda do |key, array|
394
+ key = array.index(key)
395
+ if array[key].is_a?(Component)
396
+ # We want to encode VTIMEZONE first, this is a personal
397
+ # preference.
398
+ if array[key].name == 'VTIMEZONE'
399
+ score = 300_000_000
400
+ return score + key
401
+ else
402
+ score = 400_000_000
403
+ return score + key
404
+ end
405
+ else
406
+ # Properties get encoded first
407
+ # VCARD version 4.0 wants the VERSION property to appear first
408
+ if array[key].is_a?(Property)
409
+ if array[key].name == 'VERSION'
410
+ score = 100_000_000
411
+ return score + key
412
+ else
413
+ # All other properties
414
+ score = 200_000_000
415
+ return score + key
416
+ end
417
+ end
418
+ end
419
+ end
420
+
421
+ tmp = v_obj.children.sort do |a, b|
422
+ s_a = sort_score.call(a, v_obj.children)
423
+ s_b = sort_score.call(b, v_obj.children)
424
+ s_a - s_b
425
+ end
426
+
427
+ tmp.each do |child|
428
+ if child.is_a?(Component)
429
+ serialize_component(child)
430
+ else
431
+ serialize_property(child)
432
+ end
433
+ end
434
+
435
+ c_write('cyan', 'END')
436
+ c_write('red', ':')
437
+ c_write('yellow', v_obj.name + "\n")
438
+ end
439
+
440
+ # Colorizes a property.
441
+ #
442
+ # @param Property property
443
+ #
444
+ # @return void
445
+ def serialize_property(property)
446
+ if property.group
447
+ c_write('default', property.group)
448
+ c_write('red', '.')
449
+ end
450
+
451
+ c_write('yellow', property.name)
452
+
453
+ property.parameters.each do |_, param|
454
+ c_write('red', ';')
455
+ c_write('blue', param.serialize)
456
+ end
457
+
458
+ c_write('red', ':')
459
+
460
+ if property.is_a?(Property::Binary)
461
+ c_write('default', "embedded binary stripped. (#{property.value.size} bytes)")
462
+ else
463
+ parts = property.parts
464
+ first1 = true
465
+ # Looping through property values
466
+ parts.each do |part|
467
+ if first1
468
+ first1 = false
469
+ else
470
+ c_write('red', property.delimiter)
471
+ end
472
+
473
+ first2 = true
474
+ # Looping through property sub-values
475
+ part = [part] unless part.is_a?(Array)
476
+ part.each do |sub_part|
477
+ if first2
478
+ first2 = false
479
+ else
480
+ # The sub-value delimiter is always comma
481
+ c_write('red', ',')
482
+ end
483
+
484
+ sub_part = sub_part.gsub(
485
+ /[\\;,\r\n]/,
486
+ '\\' => colorize('purple', '\\\\', 'green'),
487
+ ';' => colorize('purple', '\\;', 'green'),
488
+ ',' => colorize('purple', '\\,', 'green'),
489
+ "\n" => colorize('purple', "\\n\n\t", 'green'),
490
+ "\r" => ''
491
+ )
492
+
493
+ c_write('green', sub_part)
494
+ end
495
+ end
496
+ end
497
+
498
+ c_write('default', "\n")
499
+ end
500
+
501
+ # Parses the list of arguments.
502
+ #
503
+ # @param array argv
504
+ #
505
+ # @return void
506
+ def parse_arguments(argv)
507
+ positional = []
508
+ options = {}
509
+
510
+ ii = -1
511
+ loop do
512
+ ii += 1
513
+ break unless ii < argv.size
514
+
515
+ # Ruby ARGV is without command as first argument
516
+ # Skipping the first argument.
517
+ # next if ii == 0
518
+
519
+ v = argv[ii]
520
+
521
+ if v[0, 2] == '--'
522
+ # This is a long-form option.
523
+ option_name = v[2..-1]
524
+ option_value = true
525
+ if option_name.index('=')
526
+ (option_name, option_value) = option_name.split('=')
527
+ end
528
+ options[option_name] = option_value
529
+ elsif v[0] == '-' && v.length > 1
530
+ # This is a short-form option.
531
+ v[1..-1].chars.each do |option|
532
+ options[option] = true
533
+ end
534
+ else
535
+ positional << v
536
+ end
537
+ end
538
+
539
+ [options, positional]
540
+ end
541
+
542
+ # RUBY: attr_accessor :parser
543
+
544
+ # Reads the input file.
545
+ #
546
+ # @return Component
547
+ def read_input
548
+ unless @parser
549
+ @stdin = File.open(@input_path, 'r') if @input_path != '-'
550
+
551
+ if @input_format == 'mimedir'
552
+ @parser = Parser::MimeDir.new(@stdin, (@forgiving ? Reader::OPTION_FORGIVING : 0))
553
+ else
554
+ @parser = Parser::Json.new(@stdin, (@forgiving ? Reader::OPTION_FORGIVING : 0))
555
+ end
556
+ end
557
+
558
+ @parser.parse
559
+ end
560
+
561
+ # Sends a message to STDERR.
562
+ #
563
+ # @param string msg
564
+ #
565
+ # @return void
566
+ def log(msg, color = 'default')
567
+ return if @quiet
568
+
569
+ msg = colorize(color, msg) unless color == 'default'
570
+ @stderr.write(msg + "\n")
571
+ end
572
+
573
+ public
574
+
575
+ def initialize
576
+ @quiet = false
577
+ @show_help = false
578
+ @forgiving = false
579
+ end
580
+ end
581
+ end
582
+ end
@@ -0,0 +1,107 @@
1
+ module Tilia
2
+ module VObject
3
+ class Component
4
+ # The Available sub-component.
5
+ #
6
+ # This component adds functionality to a component, specific for AVAILABLE
7
+ # components.
8
+ class Available < Component
9
+ # Returns the 'effective start' and 'effective end' of this VAVAILABILITY
10
+ # component.
11
+ #
12
+ # We use the DTSTART and DTEND or DURATION to determine this.
13
+ #
14
+ # The returned value is an array containing DateTimeImmutable instances.
15
+ # If either the start or end is 'unbounded' its value will be null
16
+ # instead.
17
+ #
18
+ # @return array
19
+ def effective_start_end
20
+ effective_start = self['DTSTART'].date_time
21
+ if key? 'DTEND'
22
+ effective_end = self['DTEND'].date_time
23
+ else
24
+ effective_end = effective_start + DateTimeParser.parse_duration(self['DURATION'])
25
+ end
26
+
27
+ [effective_start, effective_end]
28
+ end
29
+
30
+ # A simple list of validation rules.
31
+ #
32
+ # This is simply a list of properties, and how many times they either
33
+ # must or must not appear.
34
+ #
35
+ # Possible values per property:
36
+ # * 0 - Must not appear.
37
+ # * 1 - Must appear exactly once.
38
+ # * + - Must appear at least once.
39
+ # * * - Can appear any number of times.
40
+ # * ? - May appear, but not more than once.
41
+ #
42
+ # @var array
43
+ def validation_rules
44
+ {
45
+ 'UID' => 1,
46
+ 'DTSTART' => 1,
47
+ 'DTSTAMP' => 1,
48
+
49
+ 'DTEND' => '?',
50
+ 'DURATION' => '?',
51
+
52
+ 'CREATED' => '?',
53
+ 'DESCRIPTION' => '?',
54
+ 'LAST-MODIFIED' => '?',
55
+ 'RECURRENCE-ID' => '?',
56
+ 'RRULE' => '?',
57
+ 'SUMMARY' => '?',
58
+
59
+ 'CATEGORIES' => '*',
60
+ 'COMMENT' => '*',
61
+ 'CONTACT' => '*',
62
+ 'EXDATE' => '*',
63
+ 'RDATE' => '*',
64
+
65
+ 'AVAILABLE' => '*'
66
+ }
67
+ end
68
+
69
+ # Validates the node for correctness.
70
+ #
71
+ # The following options are supported:
72
+ # Node::REPAIR - May attempt to automatically repair the problem.
73
+ # Node::PROFILE_CARDDAV - Validate the vCard for CardDAV purposes.
74
+ # Node::PROFILE_CALDAV - Validate the iCalendar for CalDAV purposes.
75
+ #
76
+ # This method returns an array with detected problems.
77
+ # Every element has the following properties:
78
+ #
79
+ # * level - problem level.
80
+ # * message - A human-readable string describing the issue.
81
+ # * node - A reference to the problematic node.
82
+ #
83
+ # The level means:
84
+ # 1 - The issue was repaired (only happens if REPAIR was turned on).
85
+ # 2 - A warning.
86
+ # 3 - An error.
87
+ #
88
+ # @param int options
89
+ #
90
+ # @return array
91
+ def validate(options = 0)
92
+ result = super(options)
93
+
94
+ if key?('DTEND') && key?('DURATION')
95
+ result << {
96
+ 'level' => 3,
97
+ 'message' => 'DTEND and DURATION cannot both be present',
98
+ 'node' => self
99
+ }
100
+ end
101
+
102
+ result
103
+ end
104
+ end
105
+ end
106
+ end
107
+ end