Class: Podgraph::Posterous

Inherits:
Object
  • Object
show all
Defined in:
lib/podgraph/posterous.rb

Overview

Reads XHTML file, analyses it, finds images, checks if they can be inlined, generates multipart/relative or multipart/mixed MIME mail.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(trestle, filename, to, from, mode) ⇒ Posterous

Analyses filename. It must be a XHTML file. to, from are email. mode is 1 of ‘related’ or ‘mixed’ string.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/podgraph/posterous.rb', line 24

def initialize(trestle, filename, to, from, mode)
  @o = Hash.new()
  @o[:user_agent] = Podgraph::Meta::NAME + ?/ + Podgraph::Meta::VERSION
  @o[:subject] = ''
  @o[:body] = []
  @o[:attachment] = []
  @o[:a_marks] = {}
  @o[:mode] = mode
  @o[:to] = to
  @o[:from] = from

  @trestle = trestle

  fp = (filename == STDIN ? STDIN : File.new(filename))
  begin
    make(fp)
  rescue
    raise $!
  ensure
    fp.close unless fp == STDIN
  end
end

Instance Attribute Details

#oObject

some options for mail generator; change with care



16
17
18
# File 'lib/podgraph/posterous.rb', line 16

def o
  @o
end

#trestleObject

a Trestle object



19
20
21
# File 'lib/podgraph/posterous.rb', line 19

def trestle
  @trestle
end

Instance Method Details

#dump(e = '') ⇒ Object

Print Mail object to stdout. e is an optional encoding.



157
158
159
# File 'lib/podgraph/posterous.rb', line 157

def dump(e = '')
  puts (e == '' ? generate().to_s : generate().to_s.encode(e))
end

#generateObject

Returns ready for delivery Mail object.



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
# File 'lib/podgraph/posterous.rb', line 100

def generate()
  m = Mail.new()
  m.from(@o[:from])
  m.to(@o[:to])
  m.transport_encoding = Mail::Encodings.get_encoding('8bit')
#      m.content_transfer_encoding('8bit')
  m.subject(@o[:subject])
  m.headers({'User-Agent' => @o[:user_agent]})

  @trestle.veputs(2, "Body lines=#{@o[:body].size}, bytes=#{@o[:body].to_s.bytesize}")
  if @o[:attachment].size == 0
    m.content_disposition('inline')
    m.content_type('text/html; charset="UTF-8"')
    m.body(@o[:body])
  else
    if @o[:mode] == 'related'
      m.content_type('Multipart/Related')
    end
    m.html_part = Mail::Part.new {
      content_type('text/html; charset=UTF-8')
    }
    m.html_part.body = @o[:body]
    m.html_part.content_disposition('inline') if @o[:mode] == 'mixed'
    
    begin
      @o[:attachment].each { |i| m.add_file(i) }
    rescue
      raise("cannot attach: #{$!}")
    end

    if @o[:mode] == 'related'
      if (fqdn = Socket.gethostname() ) == ''
        raise 'hostname is not set!'
      end
      cid = {}
      m.parts[1..-1].each { |i|
        i.content_disposition('inline')
        cid[i.filename] = i.content_id("<#{Mail.random_tag}@#{fqdn}.NO_mail>")
      }

      @o[:a_marks].each { |k, v|
        if cid.key?(k)
          @trestle.veputs(2, "mark #{k} = #{v}; -> to #{cid[k]}")
          # replace marks with corresponding content-id
          m.html_part.body.raw_source.sub!(v, "cid:#{cid[k][1..-1]}")
        else
          raise("orphan key in cid: #{k}")
        end
      }
    end
  end # a.size
  
  return m
end