Skip to content
Browse files

* controller now supports imap

  • Loading branch information...
1 parent 2323041 commit 206fb5fa90f42cb99877a0416e52867cd4893d5d @hone committed Jan 12, 2009
View
38 app/controllers/backups_controller.rb
@@ -11,11 +11,20 @@ def create
@remote_mail = RemoteMail.new( params[:remote_mail] )
# hack needed to display errors
@remote_mail.save
- @pop3 = Pop3.new( params[:remote_mail] )
+
respond_to do |format|
- if @pop3.valid?
- spawn do
- mail_download( @pop3 )
+ if @remote_mail.valid?
+ case @remote_mail.mail_type
+ when RemoteMail::POP3
+ @pop3 = Pop3.new( params[:remote_mail] )
+ spawn do
+ mail_download_pop3( @pop3 )
+ end
+ when RemoteMail::IMAP
+ @imap = Imap.new( params[:remote_mail] )
+ spawn do
+ mail_download_imap( @imap )
+ end
end
format.html { redirect_to :action => :show }
else
@@ -38,16 +47,29 @@ def check_files_exist( files )
end
end
- def mail_download( pop3 )
+ def mail_download_pop3( pop3 )
pop3.setup_mailer
result = pop3.download
case result[:status]
when Pop3::OK_FLAG
- Notify.deliver_success( @pop3.email_address, result[:mbox_name] )
+ Notify.deliver_success( pop3.email_address, result[:mbox_name] )
+ when Pop3::AUTHENTICATION_ERROR_FLAG
+ Notify.deliver_authentication_problem( pop3.email_address )
+ when Pop3::TIMEOUT_ERROR_FLAG
+ Notify.deliver_timeout_problem( pop3.email_address )
+ end
+ end
+
+ def mail_download_imap( imap )
+ status = imap.setup_mailer
+ mbox_name = imap.download
+ case status
+ when Pop3::OK_FLAG
+ Notify.deliver_success( imap.email_address, mbox_name )
when Pop3::AUTHENTICATION_ERROR_FLAG
- Notify.deliver_authentication_problem( @pop3.email_address )
+ Notify.deliver_authentication_problem( imap.email_address )
when Pop3::TIMEOUT_ERROR_FLAG
- Notify.deliver_timeout_problem( @pop3.email_address )
+ Notify.deliver_timeout_problem( imap.email_address )
end
end
View
17 app/models/imap.rb
@@ -10,6 +10,7 @@ class Imap < RemoteMail
attr_reader :mailer
def setup_mailer
+ Imap.new
if self.ssl
self.port = DEFAULT_SSL_PORT if self.port.nil?
else
@@ -44,8 +45,18 @@ def download
end
parent_dir = File.join( mbox_folder, folders.first.name )
parent_mbox = "#{parent_dir}.mbox"
+ folders_to_zip = folders.inject(Array.new) do |sum, folder|
+ folder_path = File.join( mbox_folder, folder.name.split(folder.delim).join("/") )
+ folder_mbox = "#{folder_path}.mbox"
+
+ files_to_add = Array.new
+ files_to_add += [folder_path] if File.exist?( folder_path )
+ files_to_add += [folder_mbox] if File.exist?( folder_mbox )
+
+ sum + files_to_add
+ end
zip_output = File.join( FILE_DIR, "#{mbox_name}.zip" )
- zip( [parent_mbox, parent_dir], zip_output )
+ zip( folders_to_zip, zip_output )
remove_file_dir( mbox_folder )
zip_output
@@ -70,8 +81,8 @@ def download_folder( folder, mbox_name )
def write_mbox( mbox_name, uids )
File.open( mbox_name, 'w' ) do |file|
- @mailer.uid_fetch( uids, ['ENVELOPE'] ) do |msg|
- file.puts( source.uid_fetch( msg.attr['UID'], ['RFC822'] ).first.attr['RFC822'] )
+ @mailer.uid_fetch( uids, ['ENVELOPE'] ).each do |msg|
+ file.puts( @mailer.uid_fetch( msg.attr['UID'], ['RFC822'] ).first.attr['RFC822'] )
end
end
end
View
9 app/models/remote_mail.rb
@@ -22,6 +22,8 @@ class RemoteMail < ActiveRecord::BaseWithoutTable
PORT_MIN = 1
PORT_MAX = 65535
MAX_CHAR_LENGTH = 50
+ POP3 = 1
+ IMAP = 2
OK_FLAG = :ok
AUTHENTICATION_ERROR_FLAG = :authentication_error
@@ -31,13 +33,16 @@ class RemoteMail < ActiveRecord::BaseWithoutTable
TMP_DIR = File.join( RAILS_ROOT, 'public', 'tmp' )
RemoteMailHelper::setup_columns( self )
+ column :mail_type, :integer
# validations
validates_presence_of :email_address
validates_presence_of :server
validates_presence_of :username
validates_presence_of :password
- validates_numericality_of :port, :greater_than_or_equal_to => PORT_MIN, :less_than_or_equal_to => PORT_MAX
+ validates_presence_of :mail_type
+ validates_numericality_of :port, :greater_than_or_equal_to => PORT_MIN, :less_than_or_equal_to => PORT_MAX, :allow_nil => true
+ validates_numericality_of :mail_type, :greater_than_or_equal_to => 1, :less_than_or_equal_to => 2
validates_format_of :email_address, :with => /^[\w.]+@\w+\.(\w+\.)*\w+$/
validates_format_of :server, :with => /^\w+\.\w+\.(\w+\.)*\w+$/
validates_length_of :email_address, :maximum => MAX_CHAR_LENGTH
@@ -72,7 +77,7 @@ def remove_file_dir( path )
if File.exist?( path )
if File.directory?( path )
Dir[ "#{path}/*" ].each do |file|
- remove_dir( file )
+ remove_file_dir( file )
end
FileUtils.rmdir( path )
View
5 app/views/backups/new.html.erb
@@ -28,6 +28,11 @@
<%= f.text_field :port %>
</p>
<p>
+ <%= f.label :mail_type %>
+ <%= f.radio_button :mail_type, RemoteMail::POP3 %> POP3
+ <%= f.radio_button :mail_type, RemoteMail::IMAP %> IMAP
+ </p>
+ <p>
<%= f.submit "Backup!" %>
</p>
<% end %>
View
25 spec/controllers/backups_controller_spec.rb
@@ -3,7 +3,6 @@
module BackupsControllerSpecHelper
def setup_create( result )
Pop3.should_receive(:new).once.and_return(@pop3)
- @pop3.should_receive(:valid?).once.and_return(true)
end
end
@@ -14,7 +13,6 @@ def setup_create( result )
controller.should be_an_instance_of(BackupsController)
end
-
describe "GET 'new'" do
before(:each) do
@remote_mail = mock_model( RemoteMail )
@@ -47,24 +45,39 @@ def do_get
include BackupsControllerSpecHelper
before do
+ @remote_mail = mock_model( RemoteMail )
+ RemoteMail.stub!(:new).and_return(@remote_mail)
+ @remote_mail.stub!(:save)
+ @remote_mail.stub!(:valid?).and_return(true)
+ @remote_mail.stub!(:mail_type).and_return(RemoteMail::POP3)
@pop3 = mock_model( Pop3 )
Pop3.stub!(:new).and_return(@pop3)
- @pop3.stub!(:valid).and_return(true)
@params = {
:email_address => 'test.otherinbox@gmail.com',
:server => 'pop.gmail.com',
:username => 'test.otherinbox@gmail.com',
:password => '0th3r1nb0x', # TODO need to encrypt this
:ssl => true,
- :port => 995
+ :port => 995,
+ :mail_type => RemoteMail::POP3
}
end
def do_post
post :create, :remote_mail => @params
end
- it "should redirect to page to show page" do
+ it "should redirect to page to show page for imap" do
+ @imap_mock = mock_model( Imap )
+ Imap.should_receive(:new).and_return(@imap_mock)
+ @remote_mail.stub!(:mail_type).and_return(2)
+ @params[:mail_type] = RemoteMail::IMAP
+
+ do_post
+ response.should redirect_to( :action => :show )
+ end
+
+ it "should redirect to page to show page for pop3" do
result =
{
:mail_count => 3,
@@ -79,7 +92,7 @@ def do_post
it "should render new page if invalid RemoteMail" do
@params = nil
- @pop3.should_receive(:valid?).once.and_return(false)
+ @remote_mail.should_receive(:valid?).once.and_return(false)
do_post
response.should be_success
View
17 spec/models/remote_mail_spec.rb
@@ -3,7 +3,7 @@
module RemoteMailSpecHelper
def setup_remote_mail( options = {} )
@remote_mail = RemoteMail.new
- @remote_mail.attributes = valid_pop3_attributes.merge( options )
+ @remote_mail.attributes = valid_pop3_attributes.merge( :mail_type => 1 ).merge( options )
end
def should_have_error_on_attribute( attribute, value = nil, error_num = 1 )
@@ -86,6 +86,12 @@ def should_have_error_on_attribute( attribute, value = nil, error_num = 1 )
@remote_mail.should be_valid
end
+ it "should create a valid RemoteMail with an empty port" do
+ setup_remote_mail( :port => nil )
+
+ @remote_mail.should be_valid
+ end
+
it "should require an email address" do
should_have_error_on_attribute( :email_address, nil, 3 )
end
@@ -140,4 +146,13 @@ def should_have_error_on_attribute( attribute, value = nil, error_num = 1 )
long_password = setup_long_variable( "password", RemoteMail::MAX_CHAR_LENGTH )
should_have_error_on_attribute( :password, long_password )
end
+
+ it "should require mail type" do
+ should_have_error_on_attribute( :mail_type, nil, 2 )
+ end
+
+ it "should require mail type to be 1 or 2" do
+ should_have_error_on_attribute( :mail_type, 0 )
+ should_have_error_on_attribute( :mail_type, 3 )
+ end
end
View
2 spec/views/backups/new.html.erb_spec.rb
@@ -6,6 +6,7 @@
valid_pop3_attributes.each do |key, value|
@remote_mail.stub!(key).and_return( value )
end
+ @remote_mail.stub!(:mail_type).and_return(RemoteMail::POP3)
assigns[:remote_mail] = @remote_mail
end
@@ -18,6 +19,7 @@
with_tag( "input#remote_mail_password[name=?][type=password]", "remote_mail[password]" )
with_tag( "input#remote_mail_ssl[name=?]", "remote_mail[ssl]" )
with_tag( "input#remote_mail_port[name=?]", "remote_mail[port]" )
+ with_tag( "input#remote_mail_mail_type_1[name=?]", "remote_mail[mail_type]" )
with_tag( "input[type=submit]" )
end
end

0 comments on commit 206fb5f

Please sign in to comment.
Something went wrong with that request. Please try again.