This file is indexed.

/usr/lib/ruby/1.8/svn/commit-mailer.rb is in libsvn-ruby1.8 1.6.17dfsg-3ubuntu3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
# experimental

require "optparse"
require "ostruct"
require "time"
require "net/smtp"
require "socket"

require "svn/info"

class OptionParser
  class CannotCoexistOption < ParseError
    const_set(:Reason, 'cannot coexist option'.freeze)
  end
end

module Svn
  class CommitMailer
    KILO_SIZE = 1000
    DEFAULT_MAX_SIZE = "100M"

    class << self
      def run(argv=nil)
        argv ||= ARGV
        repository_path, revision, to, options = parse(argv)
        to = [to, *options.to].compact
        mailer = CommitMailer.new(repository_path, revision, to)
        apply_options(mailer, options)
        mailer.run
      end

      def parse(argv)
        options = make_options

        parser = make_parser(options)
        argv = argv.dup
        parser.parse!(argv)
        repository_path, revision, to, *rest = argv

        [repository_path, revision, to, options]
      end

      def format_size(size)
        return "no limit" if size.nil?
        return "#{size}B" if size < KILO_SIZE
        size /= KILO_SIZE.to_f
        return "#{size}KB" if size < KILO_SIZE
        size /= KILO_SIZE.to_f
        return "#{size}MB" if size < KILO_SIZE
        size /= KILO_SIZE.to_f
        "#{size}GB"
      end

      private
      def apply_options(mailer, options)
        mailer.from = options.from
        mailer.from_domain = options.from_domain
        mailer.add_diff = options.add_diff
        mailer.max_size = options.max_size
        mailer.repository_uri = options.repository_uri
        mailer.rss_path = options.rss_path
        mailer.rss_uri = options.rss_uri
        mailer.multi_project = options.multi_project
        mailer.show_path = options.show_path
        mailer.trunk_path = options.trunk_path
        mailer.branches_path = options.branches_path
        mailer.tags_path = options.tags_path
        mailer.name = options.name
        mailer.use_utf7 = options.use_utf7
        mailer.server = options.server
        mailer.port = options.port
      end

      def parse_size(size)
        case size
        when /\A(.+?)GB?\z/i
          Float($1) * KILO_SIZE ** 3
        when /\A(.+?)MB?\z/i
          Float($1) * KILO_SIZE ** 2
        when /\A(.+?)KB?\z/i
          Float($1) * KILO_SIZE
        when /\A(.+?)B?\z/i
          Float($1)
        else
          raise ArgumentError, "invalid size: #{size.inspect}"
        end
      end

      def make_options
        options = OpenStruct.new
        options.to = []
        options.error_to = []
        options.from = nil
        options.from_domain = nil
        options.add_diff = true
        options.max_size = parse_size(DEFAULT_MAX_SIZE)
        options.repository_uri = nil
        options.rss_path = nil
        options.rss_uri = nil
        options.multi_project = false
        options.show_path = false
        options.trunk_path = "trunk"
        options.branches_path = "branches"
        options.tags_path = "tags"
        options.name = nil
        options.use_utf7 = false
        options.server = "localhost"
        options.port = Net::SMTP.default_port
        options
      end

      def make_parser(options)
        OptionParser.new do |opts|
          opts.banner += " REPOSITORY_PATH REVISION TO"

          add_email_options(opts, options)
          add_input_options(opts, options)
          add_rss_options(opts, options)
          add_other_options(opts, options)

          opts.on_tail("--help", "Show this message") do
            puts opts
            exit!
          end
        end
      end

      def add_email_options(opts, options)
        opts.separator ""
        opts.separator "E-mail related options:"

        opts.on("-sSERVER", "--server=SERVER",
                "Use SERVER as SMTP server (#{options.server})") do |server|
          options.server = server
        end

        opts.on("-pPORT", "--port=PORT", Integer,
                "Use PORT as SMTP port (#{options.port})") do |port|
          options.port = port
        end

        opts.on("-tTO", "--to=TO", "Add TO to To: address") do |to|
          options.to << to unless to.nil?
        end

        opts.on("-eTO", "--error-to=TO",
                "Add TO to To: address when an error occurs") do |to|
          options.error_to << to unless to.nil?
        end

        opts.on("-fFROM", "--from=FROM", "Use FROM as from address") do |from|
          if options.from_domain
            raise OptionParser::CannotCoexistOption,
                    "cannot coexist with --from-domain"
          end
          options.from = from
        end

        opts.on("--from-domain=DOMAIN",
                "Use author@DOMAIN as from address") do |domain|
          if options.from
            raise OptionParser::CannotCoexistOption,
                    "cannot coexist with --from"
          end
          options.from_domain = domain
        end
      end

      def add_input_options(opts, options)
        opts.separator ""
        opts.separator "Output related options:"

        opts.on("--[no-]multi-project",
                "Treat as multi-project hosting repository") do |bool|
          options.multi_project = bool
        end

        opts.on("--name=NAME", "Use NAME as repository name") do |name|
          options.name = name
        end

        opts.on("--[no-]show-path",
                "Show commit target path") do |bool|
          options.show_path = bool
        end

        opts.on("--trunk-path=PATH",
                "Treat PATH as trunk path (#{options.trunk_path})") do |path|
          options.trunk_path = path
        end

        opts.on("--branches-path=PATH",
                "Treat PATH as branches path",
                "(#{options.branches_path})") do |path|
          options.branches_path = path
        end

        opts.on("--tags-path=PATH",
                "Treat PATH as tags path (#{options.tags_path})") do |path|
          options.tags_path = path
        end

        opts.on("-rURI", "--repository-uri=URI",
                "Use URI as URI of repository") do |uri|
          options.repository_uri = uri
        end

        opts.on("-n", "--no-diff", "Don't add diffs") do |diff|
          options.add_diff = false
        end

        opts.on("--max-size=SIZE",
                "Limit mail body size to SIZE",
                "G/GB/M/MB/K/KB/B units are available",
                "(#{format_size(options.max_size)})") do |max_size|
          begin
            options.max_size = parse_size(max_size)
          rescue ArgumentError
            raise OptionParser::InvalidArgument, max_size
          end
        end

        opts.on("--no-limit-size",
                "Don't limit mail body size",
                "(#{options.max_size.nil?})") do |not_limit_size|
          options.max_size = nil
        end

        opts.on("--[no-]utf7",
                "Use UTF-7 encoding for mail body instead",
                "of UTF-8 (#{options.use_utf7})") do |use_utf7|
          options.use_utf7 = use_utf7
        end
      end

      def add_rss_options(opts, options)
        opts.separator ""
        opts.separator "RSS related options:"

        opts.on("--rss-path=PATH", "Use PATH as output RSS path") do |path|
          options.rss_path = path
        end

        opts.on("--rss-uri=URI", "Use URI as output RSS URI") do |uri|
          options.rss_uri = uri
        end
      end

      def add_other_options(opts, options)
        opts.separator ""
        opts.separator "Other options:"

        return
        opts.on("-IPATH", "--include=PATH", "Add PATH to load path") do |path|
          $LOAD_PATH.unshift(path)
        end
      end
    end

    attr_reader :to
    attr_writer :from, :add_diff, :multi_project, :show_path, :use_utf7
    attr_accessor :from_domain, :max_size, :repository_uri
    attr_accessor :rss_path, :rss_uri, :trunk_path, :branches_path
    attr_accessor :tags_path, :name, :server, :port

    def initialize(repository_path, revision, to)
      @info = Svn::Info.new(repository_path, revision)
      @to = to
    end

    def from
      @from || "#{@info.author}@#{@from_domain}".sub(/@\z/, '')
    end

    def run
      send_mail(make_mail)
      output_rss
    end

    def use_utf7?
      @use_utf7
    end

    def add_diff?
      @add_diff
    end

    def multi_project?
      @multi_project
    end

    def show_path?
      @show_path
    end

    private
    def extract_email_address(address)
      if /<(.+?)>/ =~ address
        $1
      else
        address
      end
    end

    def send_mail(mail)
      _from = extract_email_address(from)
      to = @to.collect {|address| extract_email_address(address)}
      Net::SMTP.start(@server || "localhost", @port) do |smtp|
        smtp.open_message_stream(_from, to) do |f|
          f.print(mail)
        end
      end
    end

    def output_rss
      return unless rss_output_available?
      prev_rss = nil
      begin
        if File.exist?(@rss_path)
          File.open(@rss_path) do |f|
            prev_rss = RSS::Parser.parse(f)
          end
        end
      rescue RSS::Error
      end

      rss = make_rss(prev_rss).to_s
      File.open(@rss_path, "w") do |f|
        f.print(rss)
      end
    end

    def rss_output_available?
      if @repository_uri and @rss_path and @rss_uri
        begin
          require 'rss'
          true
        rescue LoadError
          false
        end
      else
        false
      end
    end

    def make_mail
      utf8_body = make_body
      utf7_body = nil
      utf7_body = utf8_to_utf7(utf8_body) if use_utf7?
      if utf7_body
        body = utf7_body
        encoding = "utf-7"
        bit = "7bit"
      else
        body = utf8_body
        encoding = "utf-8"
        bit = "8bit"
      end

      unless @max_size.nil?
        body = truncate_body(body, !utf7_body.nil?)
      end

      make_header(encoding, bit) + "\n" + body
    end

    def make_body
      body = ""
      body << "#{@info.author}\t#{format_time(@info.date)}\n"
      body << "\n"
      body << "  New Revision: #{@info.revision}\n"
      body << "\n"
      body << "  Log:\n"
      @info.log.rstrip.each_line do |line|
        body << "    #{line}"
      end
      body << "\n"
      body << added_dirs
      body << added_files
      body << copied_dirs
      body << copied_files
      body << deleted_dirs
      body << deleted_files
      body << modified_dirs
      body << modified_files
      body << "\n"
      body << change_info
      body
    end

    def format_time(time)
      time.strftime('%Y-%m-%d %X %z (%a, %d %b %Y)')
    end

    def changed_items(title, type, items)
      rv = ""
      unless items.empty?
        rv << "  #{title} #{type}:\n"
        if block_given?
          yield(rv, items)
        else
          rv << items.collect {|item| "    #{item}\n"}.join('')
        end
      end
      rv
    end

    def changed_files(title, files, &block)
      changed_items(title, "files", files, &block)
    end

    def added_files
      changed_files("Added", @info.added_files)
    end

    def deleted_files
      changed_files("Removed", @info.deleted_files)
    end

    def modified_files
      changed_files("Modified", @info.updated_files)
    end

    def copied_files
      changed_files("Copied", @info.copied_files) do |rv, files|
        rv << files.collect do |file, from_file, from_rev|
          <<-INFO
    #{file}
      (from rev #{from_rev}, #{from_file})
INFO
        end.join("")
      end
    end

    def changed_dirs(title, files, &block)
      changed_items(title, "directories", files, &block)
    end

    def added_dirs
      changed_dirs("Added", @info.added_dirs)
    end

    def deleted_dirs
      changed_dirs("Removed", @info.deleted_dirs)
    end

    def modified_dirs
      changed_dirs("Modified", @info.updated_dirs)
    end

    def copied_dirs
      changed_dirs("Copied", @info.copied_dirs) do |rv, dirs|
        rv << dirs.collect do |dir, from_dir, from_rev|
          "    #{dir} (from rev #{from_rev}, #{from_dir})\n"
        end.join("")
      end
    end


    CHANGED_TYPE = {
      :added => "Added",
      :modified => "Modified",
      :deleted => "Deleted",
      :copied => "Copied",
      :property_changed => "Property changed",
    }

    CHANGED_MARK = Hash.new("=")
    CHANGED_MARK[:property_changed] = "_"

    def change_info
      result = changed_dirs_info
      result = "\n#{result}" unless result.empty?
      result << "\n"
      diff_info.each do |key, infos|
        infos.each do |desc, link|
          result << "#{desc}\n"
        end
      end
      result
    end

    def changed_dirs_info
      rev = @info.revision
      (@info.added_dirs.collect do |dir|
         "  Added: #{dir}\n"
       end + @info.copied_dirs.collect do |dir, from_dir, from_rev|
         <<-INFO
  Copied: #{dir}
    (from rev #{from_rev}, #{from_dir})
INFO
       end + @info.deleted_dirs.collect do |dir|
     <<-INFO
  Deleted: #{dir}
    % svn ls #{[@repository_uri, dir].compact.join("/")}@#{rev - 1}
INFO
       end + @info.updated_dirs.collect do |dir|
         "  Modified: #{dir}\n"
       end).join("\n")
    end

    def diff_info
      @info.diffs.collect do |key, values|
        [
         key,
         values.collect do |type, value|
           args = []
           rev = @info.revision
           case type
           when :added
             command = "cat"
           when :modified, :property_changed
             command = "diff"
             args.concat(["-r", "#{@info.revision - 1}:#{@info.revision}"])
           when :deleted
             command = "cat"
             rev -= 1
           when :copied
             command = "cat"
           else
             raise "unknown diff type: #{value.type}"
           end

           command += " #{args.join(' ')}" unless args.empty?

           link = [@repository_uri, key].compact.join("/")

           line_info = "+#{value.added_line} -#{value.deleted_line}"
           desc = <<-HEADER
  #{CHANGED_TYPE[value.type]}: #{key} (#{line_info})
#{CHANGED_MARK[value.type] * 67}
HEADER

           if add_diff?
             desc << value.body
           else
             desc << <<-CONTENT
    % svn #{command} #{link}@#{rev}
CONTENT
           end

           [desc, link]
         end
        ]
      end
    end

    def make_header(body_encoding, body_encoding_bit)
      headers = []
      headers << x_author
      headers << x_revision
      headers << x_repository
      headers << x_id
      headers << "MIME-Version: 1.0"
      headers << "Content-Type: text/plain; charset=#{body_encoding}"
      headers << "Content-Transfer-Encoding: #{body_encoding_bit}"
      headers << "From: #{from}"
      headers << "To: #{to.join(', ')}"
      headers << "Subject: #{make_subject}"
      headers << "Date: #{Time.now.rfc2822}"
      headers.find_all do |header|
        /\A\s*\z/ !~ header
      end.join("\n")
    end

    def detect_project
      return nil unless multi_project?
      project = nil
      @info.paths.each do |path, from_path,|
        [path, from_path].compact.each do |target_path|
          first_component = target_path.split("/", 2)[0]
          project ||= first_component
          return nil if project != first_component
        end
      end
      project
    end

    def affected_paths(project)
      paths = []
      [nil, :branches_path, :tags_path].each do |target|
        prefix = [project]
        prefix << send(target) if target
        prefix = prefix.compact.join("/")
        sub_paths = @info.sub_paths(prefix)
        if target.nil?
          sub_paths = sub_paths.find_all do |sub_path|
            sub_path == trunk_path
          end
        end
        paths.concat(sub_paths)
      end
      paths.uniq
    end

    def make_subject
      subject = ""
      project = detect_project
      subject << "#{@name} " if @name
      revision_info = "r#{@info.revision}"
      if show_path?
        _affected_paths = affected_paths(project)
        unless _affected_paths.empty?
          revision_info = "(#{_affected_paths.join(',')}) #{revision_info}"
        end
      end
      if project
        subject << "[#{project} #{revision_info}] "
      else
        subject << "#{revision_info}: "
      end
      subject << @info.log.lstrip.to_a.first.to_s.chomp
      NKF.nkf("-WM", subject)
    end

    def x_author
      "X-SVN-Author: #{@info.author}"
    end

    def x_revision
      "X-SVN-Revision: #{@info.revision}"
    end

    def x_repository
      # "X-SVN-Repository: #{@info.path}"
      "X-SVN-Repository: XXX"
    end

    def x_id
      "X-SVN-Commit-Id: #{@info.entire_sha256}"
    end

    def utf8_to_utf7(utf8)
      require 'iconv'
      Iconv.conv("UTF-7", "UTF-8", utf8)
    rescue InvalidEncoding
      begin
        Iconv.conv("UTF7", "UTF8", utf8)
      rescue Exception
        nil
      end
    rescue Exception
      nil
    end

    def truncate_body(body, use_utf7)
      return body if body.size < @max_size

      truncated_body = body[0, @max_size]
      formatted_size = self.class.format_size(@max_size)
      truncated_message = "... truncated to #{formatted_size}\n"
      truncated_message = utf8_to_utf7(truncated_message) if use_utf7
      truncated_message_size = truncated_message.size

      lf_index = truncated_body.rindex(/(?:\r|\r\n|\n)/)
      while lf_index
        if lf_index + truncated_message_size < @max_size
          truncated_body[lf_index, @max_size] = "\n#{truncated_message}"
          break
        else
          lf_index = truncated_body.rindex(/(?:\r|\r\n|\n)/, lf_index - 1)
        end
      end

      truncated_body
    end

    def make_rss(base_rss)
      RSS::Maker.make("1.0") do |maker|
        maker.encoding = "UTF-8"

        maker.channel.about = @rss_uri
        maker.channel.title = rss_title(@name || @repository_uri)
        maker.channel.link = @repository_uri
        maker.channel.description = rss_title(@name || @repository_uri)
        maker.channel.dc_date = @info.date

        if base_rss
          base_rss.items.each do |item|
            item.setup_maker(maker)
          end
        end

        diff_info.each do |name, infos|
          infos.each do |desc, link|
            item = maker.items.new_item
            item.title = name
            item.description = @info.log
            item.content_encoded = "<pre>#{RSS::Utils.html_escape(desc)}</pre>"
            item.link = link
            item.dc_date = @info.date
            item.dc_creator = @info.author
          end
        end

        maker.items.do_sort = true
        maker.items.max_size = 15
      end
    end

    def rss_title(name)
      "Repository of #{name}"
    end
  end
end