summaryrefslogtreecommitdiff
path: root/lib/feed2imap/imap.rb
blob: 6957489c90eb1989f90abefc0f91cf040ef65803 (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
=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

# Imap connection handling
require 'feed2imap/rubyimap'
begin
require 'openssl'
rescue LoadError
end
require 'uri'

# This class is a container of IMAP accounts.
# Thanks to it, accounts are re-used : several feeds
# using the same IMAP account will create only one
# IMAP connection.
class ImapAccounts < Hash
  def add_account(uri)
    u = URI::Generic::build({ :scheme => uri.scheme,
                              :userinfo => uri.userinfo,
                              :host => uri.host,
                              :port => uri.port })
    if not include?(u)
      ac = ImapAccount::new(u)
      self[u] = ac
    end
    return self[u]
  end
end

# This class is an IMAP account, with the given fd
# once the connection has been established
class ImapAccount
  attr_reader :uri

  def initialize(uri)
    @uri = uri
    self
  end

  # connects to the IMAP server
  # raises an exception if it fails
  def connect
    port = 143
    usessl = false
    if uri.scheme == 'imap'
      port = 143
      usessl = false
    elsif uri.scheme == 'imaps'
      port = 993
      usessl = true
    else
      raise "Unknown scheme: #{uri.scheme}"
    end
    # use given port if port given
    port = uri.port if uri.port 
    @connection = Net::IMAP::new(uri.host, port, usessl)
    user, password = URI::unescape(uri.userinfo).split(':',2)
    @connection.login(user, password)
    self
  end

  # disconnect from the IMAP server
  def disconnect
    @connection.disconnect if @connection
  end

  # Returns true if the folder exist
  def folder_exist?(folder)
    return !@connection.list('', folder).nil?
  end

  # Creates the given folder
  def create_folder(folder)
    @connection.create(folder)
    @connection.subscribe(folder)
    self
  end

  # Put the mail in the given folder
  # You should check whether the folder exist first.
  def putmail(folder, mail)
    @connection.append(folder, mail.gsub(/\n/, "\r\n"))
  end

  # update a mail
  def updatemail(folder, mail, idx)
    @connection.select(folder)
    searchres = @connection.search(['HEADER', 'X-CacheIndex', "-#{idx}-"])
    flags = nil
    if searchres.length > 0
      # we get the flags from the first result and delete everything
      flags = @connection.fetch(searchres[0], 'FLAGS')[0].attr['FLAGS']
      searchres.each { |m| @connection.store(m, "+FLAGS", [:Deleted]) }
      @connection.expunge
    end
    @connection.append(folder, mail.gsub(/\n/, "\r\n"), flags)
  end

  # convert to string
  def to_s
    uri.to_s
  end

  # remove mails in a folder according to a criteria
  def cleanup(folder, dryrun = false)
    puts "-- Considering #{folder}:"
    @connection.select(folder)
    a = ['SEEN', 'NOT', 'FLAGGED', 'BEFORE', (Date::today - 3).strftime('%d-%b-%Y')]
    todel = @connection.search(a)
    todel.each do |m|
      f = @connection.fetch(m, "FULL")
      d = f[0].attr['INTERNALDATE']
      s = f[0].attr['ENVELOPE'].subject
      if s =~ /^=\?utf-8\?b\?/
        s = Base64::decode64(s.gsub(/^=\?utf-8\?b\?(.*)\?=$/, '\1')).toISO_8859_1('utf-8')
      end
      if dryrun
        puts "To remove: #{s} (#{d})"
      else
        puts "Removing: #{s} (#{d})"
        @connection.store(m, "+FLAGS", [:Deleted])
      end
    end
    puts "-- Deleted #{todel.length} messages."
    if not dryrun
      @connection.expunge
    end
    return todel.length
  end
end