Skip to content
This repository has been archived by the owner on Dec 5, 2023. It is now read-only.

Commit

Permalink
Rebrand as EM::IMAP
Browse files Browse the repository at this point in the history
  • Loading branch information
ConradIrwin committed May 1, 2011
1 parent 9fcec34 commit 8efd8be
Show file tree
Hide file tree
Showing 13 changed files with 56 additions and 54 deletions.
4 changes: 2 additions & 2 deletions lib/imap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
$:.shift

module EventMachine
module Imap
module IMAP
# Connect to the specified IMAP server, using ssl if applicable.
#
# Returns a deferrable that will succeed or fail based on the
# success of the connection setup phase.
#
def self.connect(host, port, ssl=false)
Client.new(EventMachine::Imap::Connection.connect(host, port, ssl))
Client.new(EventMachine::IMAP::Connection.connect(host, port, ssl))
end

class Command < Listener
Expand Down
8 changes: 4 additions & 4 deletions lib/imap/authenticators.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
module EventMachine
# Makes Net::IMAP.add_authenticator accessible through EM::Imap and instances thereof.
# Also provides the authenticator method to EM::Imap::Client to get authenticators
# Makes Net::IMAP.add_authenticator accessible through EM::IMAP and instances thereof.
# Also provides the authenticator method to EM::IMAP::Client to get authenticators
# for use in the authentication exchange.
#
module Imap
module IMAP
def self.add_authenticator(klass)
Net::IMAP.add_authenticator(*args)
end

module Authenticators
def add_authenticator(*args)
EventMachine::Imap.add_authenticator(*args)
EventMachine::IMAP.add_authenticator(*args)
end

private
Expand Down
8 changes: 4 additions & 4 deletions lib/imap/client.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module EventMachine
module Imap
module IMAP
# TODO: Anything that accepts or returns a mailbox name should have UTF7 support.
class Client
include EM::Deferrable
DG.enhance!(self)

include Imap::Authenticators
include IMAP::Authenticators

def initialize(connection)
@connection = connection.errback{ |e| fail e }.callback{ |response| succeed response }
Expand Down Expand Up @@ -54,7 +54,7 @@ def logout
## 6.2 Client Commands - Not Authenticated State

# This would start tls negotiations, until this is implemented,
# simply pass true as the first parameter to EM::Imap.connect.
# simply pass true as the first parameter to EM::IMAP.connect.
#
def starttls
raise NotImplementedError
Expand All @@ -67,7 +67,7 @@ def starttls
# 'LOGIN', username, password
# 'CRAM-MD5', username, password (see RFC 2195)
#
# Though you can add new mechanisms using EM::Imap.add_authenticator,
# Though you can add new mechanisms using EM::IMAP.add_authenticator,
# see for example the gmail_xoauth gem.
#
def authenticate(auth_type, *args)
Expand Down
10 changes: 5 additions & 5 deletions lib/imap/command_sender.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module EventMachine
module Imap
module IMAP
# Provides a send_command_object method that serializes command objects
# and uses send_data on them. This is the ugly sister to ResponseParser.
module CommandSender
# Ugly hack to get at the Net::IMAP string formatting routines.
# (FIXME: Extract into its own module and rewrite)
class FakeNetImap < Net::IMAP
class FakeNetIMAP < Net::IMAP
def initialize(command, imap_connection)
@command = command
@connection = imap_connection
Expand All @@ -28,7 +28,7 @@ def send_literal(str)
# If you pass something that cannot be serialized, an exception will be raised.
# If however, something fails at the socket level, the command will be failed.
def send_command_object(command)
sender = FakeNetImap.new(command, self)
sender = FakeNetIMAP.new(command, self)

sender.put_string "#{command.tag} #{command.cmd}"
command.args.each do |arg|
Expand Down Expand Up @@ -111,8 +111,8 @@ def send_line_buffered(str)
end
end
end
include Imap::CommandSender::LineBuffer
include Imap::ContinuationSynchronisation
include IMAP::CommandSender::LineBuffer
include IMAP::ContinuationSynchronisation
end
end
end
16 changes: 9 additions & 7 deletions lib/imap/connection.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
module EventMachine
module Imap
module IMAP
CRLF = "\r\n"
module Connection
include EM::Deferrable
DG.enhance!(self)

include Imap::CommandSender
include Imap::ResponseParser
include IMAP::CommandSender
include IMAP::ResponseParser

# Create a new connection to an IMAP server.
#
Expand Down Expand Up @@ -110,7 +110,7 @@ def add_to_listener_pool(listener)
end

# receive_response is a higher-level receive_data provided by
# EM::Imap::ResponseParser. Each response is a Net::IMAP response
# EM::IMAP::ResponseParser. Each response is a Net::IMAP response
# object. (FIXME)
def receive_response(response)
@listeners.each{ |listener| listener.receive_event response }
Expand Down Expand Up @@ -144,7 +144,7 @@ def listen_for_bye_response(listener)
end

# Provides a next_tag! method to generate unique tags
# for an Imap session.
# for an IMAP session.
module TagSequence
def post_init
super
Expand Down Expand Up @@ -172,8 +172,10 @@ def receive_data(data)
super
end
end
include Imap::Connection::TagSequence
include Imap::Connection::Debug
include IMAP::Connection::TagSequence
def self.debug!
include IMAP::Connection::Debug
end
end
end
end
2 changes: 1 addition & 1 deletion lib/imap/continuation_synchronisation.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module EventMachine
module Imap
module IMAP
# The basic IMAP protocol is an unsynchronised exchange of lines,
# however under some circumstances it is necessary to synchronise
# so that the server acknowledges each item sent by the client.
Expand Down
2 changes: 1 addition & 1 deletion lib/imap/listener.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module EventMachine
module Imap
module IMAP
# A Listener is a cancellable subscriber to an event stream, they are used
# to provide control-flow abstraction throughout em-imap.
#
Expand Down
4 changes: 2 additions & 2 deletions lib/imap/response_parser.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module EventMachine
module Imap
module IMAP
# Intercepts the receive_data event and generates receive_response events
# with parsed data.
module ResponseParser
Expand All @@ -9,7 +9,7 @@ def post_init
@buffer = ""
end

# This is a translation of Net::Imap#get_response
# This is a translation of Net::IMAP#get_response
def receive_data(data)
@buffer += data

Expand Down
16 changes: 8 additions & 8 deletions spec/client_spec.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
require 'spec_helper'

describe EM::Imap::Client do
describe EM::IMAP::Client do

before :each do
@connection = Class.new(EMStub) do
include EM::Imap::Connection
include EM::IMAP::Connection
end.new
end

describe "connection" do
it "should succeed if the connection receives a successful greeting" do
a = false
EM::Imap::Client.new(@connection).callback do |response|
EM::IMAP::Client.new(@connection).callback do |response|
a = true
end
@connection.receive_data "* OK Welcome, test IMAP!\r\n"
Expand All @@ -20,16 +20,16 @@

it "should fail if the connection receives a BYE" do
a = false
EM::Imap::Client.new(@connection).errback do |e|
EM::IMAP::Client.new(@connection).errback do |e|
a = true
end
@connection.receive_data "* BYE Test Imap\r\n"
@connection.receive_data "* BYE Test IMAP\r\n"
a.should be_true
end

it "should fail if the connection receives gibberish" do
a = false
EM::Imap::Client.new(@connection).errback do |e|
EM::IMAP::Client.new(@connection).errback do |e|
a = true
end
@connection.receive_data "HTTP 1.1 GET /\r\n"
Expand All @@ -38,7 +38,7 @@

it "should fail if the connection does not complete" do
a = false
EM::Imap::Client.new(@connection).errback do |e|
EM::IMAP::Client.new(@connection).errback do |e|
a = true
end
@connection.unbind
Expand All @@ -48,7 +48,7 @@

describe "commands" do
before :each do
@client = EM::Imap::Client.new(@connection)
@client = EM::IMAP::Client.new(@connection)
@connection.receive_data "* OK Ready to test!\r\n"
end

Expand Down
18 changes: 9 additions & 9 deletions spec/command_sender_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
require 'spec_helper'

describe EM::Imap::CommandSender do
describe EM::IMAP::CommandSender do

before :each do
@command_sender = Class.new(EMStub) do
include EM::Imap::Connection
include EM::IMAP::Connection
end.new
end

Expand All @@ -15,7 +15,7 @@
def process; end
end.new

@command = EM::Imap::Command.new("AUTHENTICATE", "XDUMMY")
@command = EM::IMAP::Command.new("AUTHENTICATE", "XDUMMY")

@command_sender.send_authentication_data(@authenticator, @command)
end
Expand Down Expand Up @@ -69,7 +69,7 @@ def process; end

describe "#send_literal" do
before :each do
@command = EM::Imap::Command.new("RUBY0001", "SELECT", ["AHLO"])
@command = EM::IMAP::Command.new("RUBY0001", "SELECT", ["AHLO"])
end

it "should initially only send the size" do
Expand Down Expand Up @@ -105,33 +105,33 @@ def @bomb.send_data(connection)

it "should raise errors if the command cannot be serialized" do
lambda {
@command_sender.send_command_object(EM::Imap::Command.new("RUBY0001", "IDLE", [@bomb]))
@command_sender.send_command_object(EM::IMAP::Command.new("RUBY0001", "IDLE", [@bomb]))
}.should raise_exception "bomb"
end

it "should raise errors even if the unserializable object is after a literal" do
lambda {
@command_sender.send_command_object(EM::Imap::Command.new("RUBY0001", "IDLE", ["Literal\r\nString", @bomb]))
@command_sender.send_command_object(EM::IMAP::Command.new("RUBY0001", "IDLE", ["Literal\r\nString", @bomb]))
}.should raise_exception "bomb"
end

it "should not raise errors if the send_data fails" do
@command_sender.should_receive(:send_data).and_raise(Errno::ECONNRESET)
lambda {
@command_sender.send_command_object(EM::Imap::Command.new("RUBY0001", "IDLE"))
@command_sender.send_command_object(EM::IMAP::Command.new("RUBY0001", "IDLE"))
}.should_not raise_exception
end

it "should fail the command if send_data fails" do
@command_sender.should_receive(:send_data).and_raise(Errno::ECONNRESET)
a = []
command = EM::Imap::Command.new("RUBY0001", "IDLE").errback{ |e| a << e }
command = EM::IMAP::Command.new("RUBY0001", "IDLE").errback{ |e| a << e }
@command_sender.send_command_object(command)
a.map(&:class).should == [Errno::ECONNRESET]
end
end

describe EM::Imap::CommandSender::LineBuffer do
describe EM::IMAP::CommandSender::LineBuffer do

it "should not send anything until the buffer is full" do
@command_sender.should_not_receive(:send_data)
Expand Down
4 changes: 2 additions & 2 deletions spec/continuation_synchronisation_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require 'spec_helper'

describe EM::Imap::ContinuationSynchronisation do
describe EM::IMAP::ContinuationSynchronisation do
before :each do
@connection = Class.new(EMStub) do
include EM::Imap::Connection
include EM::IMAP::Connection
end.new
end

Expand Down
14 changes: 7 additions & 7 deletions spec/listener_spec.rb
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
require 'spec_helper'

describe EM::Imap::Listener do
describe EM::IMAP::Listener do

it "should pass events to listeners" do
a = []
listener = EM::Imap::Listener.new do |event| a << event; end
listener = EM::IMAP::Listener.new do |event| a << event; end
listener.receive_event 55
a.should == [55]
end

it "should pass events to multiple listeners in order" do
a = []
listener = EM::Imap::Listener.new.listen do |event| a << [0, event]; end.
listener = EM::IMAP::Listener.new.listen do |event| a << [0, event]; end.
listen do |event| a << [1, event]; end
listener.receive_event 55
a.should == [[0, 55], [1, 55]]
end

it "should pass multiple events to listeners" do
a = []
listener = EM::Imap::Listener.new do |event| a << event; end
listener = EM::IMAP::Listener.new do |event| a << event; end
listener.receive_event 55
listener.receive_event 56
a.should == [55, 56]
end

it "should call the stopbacks when stopped" do
a = []
listener = EM::Imap::Listener.new.stopback do a << "stopped" end
listener = EM::IMAP::Listener.new.stopback do a << "stopped" end
listener.stop
a.should == ["stopped"]
end

it "should permit succeed to be called form within a stopback" do
a = []
listener = EM::Imap::Listener.new.callback do a << "callback" end.
listener = EM::IMAP::Listener.new.callback do a << "callback" end.
errback do a << "errback" end.
stopback do listener.succeed end
listener.stop
Expand All @@ -43,7 +43,7 @@

describe "transform" do
before :each do
@bottom = EM::Imap::Listener.new
@bottom = EM::IMAP::Listener.new
@top = @bottom.transform{ |result| :transformed }
end

Expand Down
4 changes: 2 additions & 2 deletions spec/response_parser_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
require 'spec_helper'

describe EventMachine::Imap::ResponseParser do
describe EventMachine::IMAP::ResponseParser do

before :each do
@response_parser = Class.new(EMStub) do
include EventMachine::Imap::ResponseParser
include EventMachine::IMAP::ResponseParser
end.new
end

Expand Down

0 comments on commit 8efd8be

Please sign in to comment.