This file is indexed.

/usr/lib/ruby/1.8/rmail/serialize.rb is in librmail-ruby1.8 0.17-1.1.

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
#--
#   Copyright (C) 2002, 2003 Matt Armstrong.  All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
#    this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. The name of the author may not be used to endorse or promote products
#    derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
# NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
#++
# Implements the RMail::Serialize class.

module RMail

  # The RMail::Serialize class writes an RMail::Message object into an
  # IO object or string.  The result is a standard mail message in
  # text form.
  #
  # To do this, you pass the RMail::Message object to the
  # RMail::Serialize object.  RMail::Serialize can write into any
  # object supporting the << method.
  #
  # As a convenience, RMail::Serialize.write is a class method you can
  # use directly:
  #
  #  # Write to a file
  #  File.open('my-message', 'w') { |f|
  #    RMail::Serialize.write(f, message)
  #  }
  #
  # # Write to a new string
  # string = RMail::Serialize.write('', message)
  class Serialize

    @@boundary_count = 0

    # Initialize this Serialize object with an output stream.  If
    # escape_from is not nil, lines with a leading From are escaped.
    def initialize(output, escape_from = nil)
      @output = output
      @escape_from = escape_from
    end

    # Serialize a given message into this object's output object.
    def serialize(message)
      calculate_boundaries(message) if message.multipart?
      serialize_low(message)
    end

    # Serialize a message into a given output object.  The output
    # object must support the << method in the same way that an IO or
    # String object does.
    def Serialize.write(output, message)
      Serialize.new(output).serialize(message)
    end

    private

    def serialize_low(message, depth = 0)
      if message.multipart?
        delimiters, delimiters_boundary = message.get_delimiters
        unless delimiters
          boundary = "\n--" + message.header.param('Content-Type', 'boundary')
          delimiters = Array.new(message.body.length + 1, boundary + "\n")
          delimiters[-1] = boundary + "--\n"
        end

        @output << message.header.to_s

        if message.body.length > 0 or message.preamble or
            delimiters.last.length > 0
          @output << "\n"
        end

        if message.preamble
          @output << message.preamble
        end

        delimiter = 0
        message.each_part { |part|
          @output << delimiters[delimiter]
          delimiter = delimiter.succ
          serialize_low(part, depth + 1)
        }

        @output << delimiters[delimiter]

        if message.epilogue
          @output << message.epilogue
        end

      else
        @output << message.header.to_s
        unless message.body.nil?
          @output << "\n"
          @output << message.body
          if depth == 0 and message.body.length > 0 and
              message.body[-1] != ?\n
            @output << "\n"
          end
        end
      end
      @output
    end

    # Walk the multipart tree and make sure the boundaries generated
    # will actually work.
    def calculate_boundaries(message)
      calculate_boundaries_low(message, [])
      unless message.header['MIME-Version']
        message.header['MIME-Version'] = "1.0"
      end
    end

    def calculate_boundaries_low(part, boundaries)
      # First, come up with a candidate boundary for this part and
      # save it in our list of boundaries.
      boundary = make_and_set_unique_boundary(part, boundaries)

      # Now walk through each part and make sure the boundaries are
      # suitable.  We dup the boundaries array before recursing since
      # sibling multipart can re-use boundary strings (though it isn't
      # a good idea).
      boundaries.push(boundary)
      part.each_part { |p|
        calculate_boundaries_low(p, boundaries) if p.multipart?
      }
      boundaries.pop
    end

    # Generate a random boundary
    def generate_boundary
      @@boundary_count += 1
      t = Time.now
      sprintf("=-%d-%d-%d-%d-%d-=",
              t.tv_sec.to_s,
              t.tv_usec.to_s,
              Process.pid.to_s,
              rand(10000),
              @@boundary_count)
    end

    # Returns a boundary that will probably work out.  Extracts any
    # existing boundary from the header, but will generate a default
    # one if the header doesn't have one set yet.
    def make_and_set_unique_boundary(part, boundaries)
      candidate = part.header.param('content-type', 'boundary')
      unique = make_unique_boundary(candidate || generate_boundary, boundaries)
      if candidate.nil? or candidate != unique
        part.header.set_boundary(unique)
      end
      unique
    end

    # Make the passed boundary unique among the passed boundaries and
    # return it.
    def make_unique_boundary(boundary, boundaries)
      continue = true
      while continue
        continue = false
        boundaries.each do |existing|
          if boundary == existing[0, boundary.length]
            continue = true
            break
          end
        end
        break unless continue
        boundary = generate_boundary
      end
      boundary
    end

  end
end