Class: ImapAccount
- Inherits:
-
Object
- Object
- ImapAccount
- Defined in:
- lib/feed2imap/imap.rb
Overview
This class is an IMAP account, with the given fd once the connection has been established
Constant Summary collapse
- @@no_ssl_verify =
false
Instance Attribute Summary collapse
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Class Method Summary collapse
Instance Method Summary collapse
-
#cleanup(folder, dryrun = false) ⇒ Object
remove mails in a folder according to a criteria.
-
#connect ⇒ Object
connects to the IMAP server raises an exception if it fails.
-
#create_folder_if_not_exists(folder) ⇒ Object
tests if the folder exists and create it if not.
-
#disconnect ⇒ Object
disconnect from the IMAP server.
-
#initialize(uri) ⇒ ImapAccount
constructor
A new instance of ImapAccount.
-
#putmail(folder, mail, date = Time::now) ⇒ Object
Put the mail in the given folder You should check whether the folder exist first.
-
#to_s ⇒ Object
convert to string.
-
#updatemail(folder, mail, id, date = Time::now, reupload_if_updated = true) ⇒ Object
update a mail.
Constructor Details
#initialize(uri) ⇒ ImapAccount
Returns a new instance of ImapAccount.
58 59 60 61 62 |
# File 'lib/feed2imap/imap.rb', line 58 def initialize(uri) @uri = uri @existing_folders = [] self end |
Instance Attribute Details
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
51 52 53 |
# File 'lib/feed2imap/imap.rb', line 51 def uri @uri end |
Class Method Details
.no_ssl_verify=(v) ⇒ Object
54 55 56 |
# File 'lib/feed2imap/imap.rb', line 54 def ImapAccount::no_ssl_verify=(v) @@no_ssl_verify = v end |
Instance Method Details
#cleanup(folder, dryrun = false) ⇒ Object
remove mails in a folder according to a criteria
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 |
# File 'lib/feed2imap/imap.rb', line 150 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')).force_encoding('utf-8') elsif s =~ /^=\?iso-8859-1\?b\?/ s = Base64::decode64(s.gsub(/^=\?iso-8859-1\?b\?(.*)\?=$/, '\1')).force_encoding('iso-8859-1').encode('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 |
#connect ⇒ Object
connects to the IMAP server raises an exception if it fails
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 |
# File 'lib/feed2imap/imap.rb', line 66 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 if usessl = { verify_mode: @@no_ssl_verify ? OpenSSL::SSL::VERIFY_NONE : OpenSSL::SSL::VERIFY_PEER, } else = false end @connection = Net::IMAP::new( uri.host, port: port, ssl: , ) user, password = CGI::unescape(uri.userinfo).split(':',2) @connection.login(user, password) self end |
#create_folder_if_not_exists(folder) ⇒ Object
tests if the folder exists and create it if not
107 108 109 110 111 112 113 114 |
# File 'lib/feed2imap/imap.rb', line 107 def create_folder_if_not_exists(folder) return if @existing_folders.include?(folder) if @connection.list('', folder).nil? @connection.create(folder) @connection.subscribe(folder) end @existing_folders << folder end |
#disconnect ⇒ Object
disconnect from the IMAP server
99 100 101 102 103 104 |
# File 'lib/feed2imap/imap.rb', line 99 def disconnect if @connection @connection.logout @connection.disconnect end end |
#putmail(folder, mail, date = Time::now) ⇒ Object
Put the mail in the given folder You should check whether the folder exist first.
118 119 120 121 |
# File 'lib/feed2imap/imap.rb', line 118 def putmail(folder, mail, date = Time::now) create_folder_if_not_exists(folder) @connection.append(folder, mail.gsub(/\n/, "\r\n"), nil, date) end |
#to_s ⇒ Object
convert to string
143 144 145 146 147 |
# File 'lib/feed2imap/imap.rb', line 143 def to_s u2 = uri.clone u2.password = 'PASSWORD' u2.to_s end |
#updatemail(folder, mail, id, date = Time::now, reupload_if_updated = true) ⇒ Object
update a mail
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/feed2imap/imap.rb', line 124 def updatemail(folder, mail, id, date = Time::now, reupload_if_updated = true) create_folder_if_not_exists(folder) @connection.select(folder) searchres = @connection.search(['HEADER', 'Message-Id', id]) 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 flags -= [ :Recent ] # avoids errors with dovecot elsif not reupload_if_updated # mail not present, and we don't want to re-upload it return end @connection.append(folder, mail.gsub(/\n/, "\r\n"), flags, date) end |