Created
November 4, 2011 22:32
-
-
Save jmervine/1340666 to your computer and use it in GitHub Desktop.
module SimpleImapReader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module SimpleImapReader | |
require 'net/imap' | |
#require 'mail' | |
class << self | |
def config | |
@config ||= SimpleImapReader::Config.new | |
end | |
def configure(&block) | |
config.configure(&block) | |
end | |
end | |
# Net::IMAP Connection | |
class Connection | |
def initialize(user, password) | |
@imap = Net::IMAP.new(SimpleImapReader.config.host, SimpleImapReader.config.port, SimpleImapReader.config.use_ssl, SimpleImapReader.config.validate_cert) | |
@imap.login(user, password) | |
@imap.examine(SimpleImapReader.config.folder) | |
end | |
# Net::IMAP logout and disconnect | |
def terminate | |
begin | |
@imap.logout | |
@imap.disconnect | |
rescue | |
end | |
end | |
# Net::IMAP fetch | |
def search *args | |
@imap.search *args | |
end | |
# Net::IMAP fetch | |
def fetch *args | |
@imap.fetch *args | |
end | |
def method_missing(method, *args) | |
@imap.send(method, *args) | |
end | |
end | |
# Configurator class | |
class Config | |
attr_writer :host, :port, :use_ssl, :validate_cert, :folder | |
def initialize(&block) | |
configure(&block) if block_given? | |
end | |
def configure(&block) | |
yield(self) | |
end | |
# return host or default | |
def host | |
@host ||= 'mail.attinteractive.com' | |
end | |
# return port for default | |
def port | |
@port ||= 993 | |
end | |
# return use_ssl or default | |
def use_ssl | |
@use_ssl ||= true | |
end | |
# return validate_cert or default | |
def validate_cert | |
@validate_cert ||= false | |
end | |
# return folder or default | |
def folder | |
@folder ||= 'INBOX' | |
end | |
# acceptions hash { :host, :port, :use_ssl, :validate_cert } | |
# and configures applications. | |
def configure=(options) | |
if options[:host] | |
@host = options[:host] | |
end | |
if options[:port] | |
@port = options[:port] | |
end | |
if options[:use_ssl] | |
@use_ssl = options[:use_ssl] | |
end | |
if options[:validate_cert] | |
@validate_cert = options[:validate_cert] | |
end | |
if options[:folder] | |
@folder = options[:folder] | |
end | |
end | |
end | |
# Messages -- returns array of message ids. | |
class Messages | |
attr_reader :cached | |
def initialize(connection) | |
@connection = connection | |
end | |
def all | |
find('ALL') | |
end | |
def find(key, value=nil) | |
search = [key] | |
search.push(value) unless value.nil? | |
@cached = @connection.search(search)||[] | |
@cached | |
end | |
end | |
# Message | |
class Message | |
#require 'mail' | |
attr_reader :envelope, :id | |
def initialize(connection, id) | |
@id = id | |
@connection = connection | |
@envelope = envelope | |
end | |
def envelope | |
@envelope||=@connection.fetch(@id, ["ENVELOPE"])[0].attr["ENVELOPE"] | |
end | |
#def parsed_message | |
#if @parsed.nil? | |
#parsed = Mail.new(raw_message) | |
#if parsed.multipart? | |
#print "m" | |
#prased.parts.each do |part| | |
#if part.content_type == "text/html" | |
#@parsed = part.decode | |
#end | |
#end | |
#else | |
#print "s" | |
#@parsed = parsed.body.decoded | |
#end | |
#else | |
#@parsed | |
#end | |
#end | |
def raw_message | |
@raw_message ||= @connection.fetch(@id, ["BODY[TEXT]"])[0].attr['BODY[TEXT]'] | |
end | |
def self.today | |
date(Time.now) | |
end | |
def self.date(date) | |
if date.kind_of?(Time) | |
date.strftime("%d-%b-%y") | |
else | |
raise "Invalid parameter, expected Time Object." | |
end | |
end | |
def self.scrub(message_text) | |
print "s" | |
message_text.gsub(/([\-\=]+)_mimepart_([a-z0-9\-]+)/, "***MIMEPART_SPLIT***") | |
message_array = message_text.split("***MIMEPART_SPLIT***") | |
message_text = message_array.last.to_s.split("<body ").last.split("</body>").first | |
message_text = "<body "+message_text | |
message_text = message_text.gsub("\r\n", "") | |
message_text | |
end | |
def scrub | |
unless base64? | |
self.class.scrub(raw_message) | |
else | |
raise "I refuse to scrub a base64 encoded message, as it will do very bad things." | |
end | |
end | |
def self.base64?(message_text) | |
message_text.include?("Content-Transfer-Encoding: base64") | |
end | |
def base64? | |
self.class.base64?(raw_message) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Current scrub isn't working correctly AT ALL.