summaryrefslogtreecommitdiff
path: root/lib/feed2imap/channel.rb
blob: 3fcc3e8a12841ae61c96f174f63a02ab1472c94d (plain)
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
=begin
Feed2Imap - RSS/Atom Aggregator uploading to an IMAP Server
Copyright (c) 2005 Lucas Nussbaum <lucas@lucas-nussbaum.net>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
=end

# This class allows to retrieve a feed and parse it into a Channel

require 'rexml/document'
require 'time'
require 'rmail'
require 'feed2imap/textconverters'
require 'feed2imap/rubymail_patch'
require 'feed2imap/rexml_patch'
require 'base64'

class UnknownFeedTypeException < RuntimeError
end
# an RSS/Atom channel
class Channel
  attr_reader :title, :link, :description, :creator, :encoding, :items

  # parse str to build a channel
  def initialize(str = nil)
    parse_str(str) if str
  end

  # Determines all the fields using a string containing an
  # XML document
  def parse_str(str)
    # Dirty hack: some feeds contain the & char. It must be changed to &amp;
    str.gsub!(/&(\s+)/, '&amp;\1')
    doc = REXML::Document.new(str)
    # get channel info
    @encoding = doc.encoding
    @title,@link,@description,@creator = nil
    @items = []
    if doc.root.elements['channel'] || doc.root.elements['rss:channel']
      # We have a RSS feed!
      # Title
      if (e = doc.root.elements['channel/title'] ||
        doc.root.elements['rss:channel/rss:title']) && e.text
        @title = e.text.toUTF8(@encoding).rmWhiteSpace!
      end
      # Link
      if (e = doc.root.elements['channel/link'] ||
          doc.root.elements['rss:channel/rss:link']) && e.text
        @link = e.text.rmWhiteSpace!
      end
      # Description
      if (e = doc.root.elements['channel/description'] || 
          doc.root.elements['rss:channel/rss:description']) && e.text
        @description = e.text.toUTF8(@encoding).rmWhiteSpace!
      end
      # Creator
      if ((e = doc.root.elements['channel/dc:creator']) && e.text) ||
          ((e = doc.root.elements['channel/author'] ||
          doc.root.elements['rss:channel/rss:author']) && e.text)
        @creator = e.text.toUTF8(@encoding).rmWhiteSpace!
      end
      # Items
      if doc.root.elements['channel/item']
        query = 'channel/item'
      elsif doc.root.elements['item']
        query = 'item'
      elsif doc.root.elements['rss:channel/rss:item']
        query = 'rss:channel/rss:item'
      else
        query = 'rss:item'
      end
      doc.root.each_element(query) { |e| @items << Item::new(e, self) }

    elsif doc.root.elements['/feed']
      # We have an ATOM feed!
      # Title
      if (e = doc.root.elements['/feed/title']) && e.text
        @title = e.text.toUTF8(@encoding).rmWhiteSpace!
      end
      # Link
      doc.root.each_element('/feed/link') do |e|
        if e.attribute('type').value == 'text/html' or
          e.attribute('type').value == 'application/xhtml' or
          e.attribute('type').value == 'application/xhtml+xml'
          if (h = e.attribute('href')) && h
            @link = h.value.rmWhiteSpace!
          end
        end
      end
      # Description
      if e = doc.root.elements['/feed/info']
        @description = e.elements.to_s.toUTF8(@encoding).rmWhiteSpace!
      end
      # Items
      doc.root.each_element('/feed/entry') do |e|
         @items << AtomItem::new(e, self)
      end
    else
      raise UnknownFeedTypeException::new
    end
  end

  def to_s
    s = "Title: #{@title}\nLink: #{@link}\n\n"
    @items.each { |i| s += i.to_s }
    s
  end
end

# an Item from a channel
class Item
  attr_accessor :title, :link, :content, :date, :creator, :subject,
                :category, :cacheditem
  attr_reader :channel

  def initialize(item = nil, channel = nil)
    @channel = channel
    @title, @link, @content, @date, @creator, @subject, @category = nil
    if item
      # Title
      if ((e = item.elements['title'] || item.elements['rss:title']) &&
          e.text)  ||
          ((e = item.elements['pubDate'] || item.elements['rss:pubDate']) &&
           e.text)
        @title = e.text.toUTF8(@channel.encoding).rmWhiteSpace!
      end
      # Link
      if ((e = item.elements['link'] || item.elements['rss:link']) && e.text)||
          (e = item.elements['guid'] || item.elements['rss:guid'] and
          not (e.attribute('isPermaLink') and
          e.attribute('isPermaLink').value == 'false'))
        @link = e.text.rmWhiteSpace!
      end
      # Content
      if (e = item.elements['content:encoded']) ||
        (e = item.elements['description'] || item.elements['rss:description'])
        if e.children.length > 1
          s = ''
          e.children.each { |c| s += c.to_s }
          @content = s.toUTF8(@channel.encoding).rmWhiteSpace!
        elsif e.children.length == 1
          if e.cdatas[0]
            @content = e.cdatas[0].to_s.toUTF8(@channel.encoding).rmWhiteSpace!
          elsif e.text
            @content = e.text.toUTF8(@channel.encoding).text2html
          end
        end
      end
      # Date
      if e = item.elements['dc:date'] || item.elements['pubDate'] || 
          item.elements['rss:pubDate']
        begin
          @date = Time::xmlschema(e.text)
        rescue
          begin
            @date = Time::rfc2822(e.text)
          rescue
            begin
              @date = Time::parse(e.text)
            rescue
              @date = nil
            end
          end
        end
      end
      # Creator
      @creator = @channel.creator
      if (e = item.elements['dc:creator'] || item.elements['author'] ||
          item.elements['rss:author']) && e.text
        @creator = e.text.toUTF8(@channel.encoding).rmWhiteSpace!
      end
      # Subject
      if (e = item.elements['dc:subject']) && e.text
        @subject = e.text.toUTF8(@channel.encoding).rmWhiteSpace!
      end
      # Category
      if (e = item.elements['dc:category'] || item.elements['category'] ||
          item.elements['rss:category']) && e.text
        @category = e.text.toUTF8(@channel.encoding).rmWhiteSpace!
      end
    end
  end

  def to_s
    "--------------------------------\n" +
      "Title: #{@title}\nLink: #{@link}\n" +
      "Date: #{@date.to_s}\nCreator: #{@creator}\n" +
      "Subject: #{@subject}\nCategory: #{@category}\nContent:\n#{content}\n"
  end

  def to_text
    s = ""
    s += "Channel: "
    s += @channel.title.toISO_8859_1('utf-8') + ' ' if @channel.title
    s += "<#{@channel.link.toISO_8859_1('utf-8')}>" if @channel.link
    s += "\n"
    s += "Item: "
    s += @title.toISO_8859_1('utf-8') + ' ' if @title
    s += "<#{@link.toISO_8859_1('utf-8')}>" if @link
    s += "\n"
    s += "\nDate: #{@date.to_s.toISO_8859_1('utf-8')}" if @date # TODO improve date rendering ?
    s += "\nAuthor: #{@creator.toISO_8859_1('utf-8')}" if @creator

    s += "\nSubject: #{@subject.toISO_8859_1('utf-8')}" if @subject
    s += "\nCategory: #{@category.toISO_8859_1('utf-8')}" if @category
    s += "\n\n"
    s += "#{@content.html2text.toISO_8859_1('utf-8')}" if @content
    s
  end

  def to_html
    s = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">'
    s += '<html>'
    s += '<body>'
    s += "<p>Channel: "
    s += "<a href=\"#{@channel.link}\">" if @channel.link
    s += @channel.title if @channel.title
    s += "</a>" if @channel.link
    s += "<br/>\nItem: "
    s += "<a href=\"#{@link}\">" if @link
    s += @title if @title
    s += "</a>" if @link
    s += "\n"
    s += "<br/>Date: #{@date.to_s}" if @date # TODO improve date rendering ?
    s += "<br/>Author: #{@creator}" if @creator
    s += "<br/>Subject: #{@subject}" if @subject
    s += "<br/>Category: #{@category}" if @category
    s += "</p>"
    s += "<p>#{@content}</p>" if @content
    s += '</body></html>'
    s
  end

  def to_mail(from = 'Feed2Imap')
    message = RMail::Message::new
    message.header['From'] = "#{from} <feed2imap@feed2imap.net>"
    message.header['To'] = "#{from} <feed2imap@feed2imap.net>"
    if @date.nil?
      message.header['Date'] = Time::new.rfc2822
    else
      message.header['Date'] = @date.rfc2822
    end
    message.header['X-Feed2Imap-Version'] = F2I_VERSION if defined?(F2I_VERSION)
    message.header['X-CacheIndex'] = "-#{@cacheditem.index}-"
    message.header['X-F2IStatus'] = "Updated" if @cacheditem.updated
    # treat subject. Might need MIME encoding.
    subj = @title or (@date and @date.to_s) or @link
    if subj
      if subj.needMIME
        message.header['Subject'] = "=?utf-8?b?#{Base64::encode64(subj).gsub("\n",'')}?="
      else
        message.header['Subject'] = subj 
      end
    end
    textpart = RMail::Message::new
    textpart.header['Content-Type'] = 'text/plain; charset=iso-8859-1'
    textpart.header['Content-Transfer-Encoding'] = '7bit'
    textpart.body = to_text
    htmlpart = RMail::Message::new
    htmlpart.header['Content-Type'] = 'text/html; charset=utf-8'
    htmlpart.header['Content-Transfer-Encoding'] = '7bit'
    htmlpart.body = to_html
    message.add_part(textpart)
    message.add_part(htmlpart)
    return message.to_s
  end
end

class AtomItem < Item
  def initialize(item = nil, channel = nil)
    @channel = channel
    @title, @link, @content, @date, @creator, @subject, @category = nil
    if item
      # Title
      if (e = item.elements['title']) && e.text
        @title = e.text.toUTF8(@channel.encoding).rmWhiteSpace!
      end
      # Link
      item.each_element('link') do |e|
        if e.attribute('type').value == 'text/html' or
          e.attribute('type').value == 'application/xhtml' or
          e.attribute('type').value == 'application/xhtml+xml'
          if (h = e.attribute('href')) && h.value
            @link = h.value
          end
        end
      end
      # Content
      if e = item.elements['content'] || item.elements['summary']
        if (e.attribute('mode') and e.attribute('mode').value == 'escaped') &&
          e.text
          @content = e.text.toUTF8(@channel.encoding).rmWhiteSpace!
        else
          # go one step deeper in the recursion if possible
          e = e.elements['div'] || e
          @content = e.to_s.toUTF8(@channel.encoding).rmWhiteSpace!
        end
      end
      # Date
      if (e = item.elements['issued'] || e = item.elements['created']) && e.text
        begin
          @date = Time::xmlschema(e.text)
        rescue
          begin
            @date = Time::rfc2822(e.text)
          rescue
            begin
              @date = Time::parse(e.text)
            rescue
              @date = nil
            end
          end
        end
      end
      # Creator
      @creator = @channel.creator
      if (e = item.elements['author/name']) && e.text
        @creator = e.text.toUTF8(@channel.encoding).rmWhiteSpace!
      end
    end
  end
end