gmail 0.6.0 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.coveralls.yml +1 -0
- data/.gitignore +27 -27
- data/.rspec +1 -1
- data/.rubocop.yml +53 -13
- data/.rubocop_todo.yml +299 -239
- data/.travis.yml +19 -19
- data/CHANGELOG.md +152 -145
- data/Gemfile +5 -3
- data/LICENSE +21 -21
- data/README.md +380 -355
- data/Rakefile +44 -46
- data/gmail.gemspec +32 -34
- data/lib/gmail.rb +72 -78
- data/lib/gmail/client.rb +35 -34
- data/lib/gmail/client/base.rb +244 -229
- data/lib/gmail/client/plain.rb +24 -24
- data/lib/gmail/client/xoauth.rb +67 -68
- data/lib/gmail/client/xoauth2.rb +39 -39
- data/lib/gmail/imap_extensions.rb +156 -159
- data/lib/gmail/labels.rb +80 -79
- data/lib/gmail/mailbox.rb +178 -175
- data/lib/gmail/message.rb +212 -207
- data/lib/gmail/version.rb +3 -3
- data/spec/account.yml.example +1 -1
- data/spec/account.yml.obfus +2 -2
- data/spec/gmail/client/base_spec.rb +5 -5
- data/spec/gmail/client/plain_spec.rb +169 -169
- data/spec/gmail/client/xoauth2_spec.rb +186 -186
- data/spec/gmail/client/xoauth_spec.rb +5 -5
- data/spec/gmail/client_spec.rb +5 -5
- data/spec/gmail/imap_extensions_spec.rb +47 -47
- data/spec/gmail/labels_spec.rb +27 -27
- data/spec/gmail/mailbox_spec.rb +84 -84
- data/spec/gmail/message_spec.rb +181 -181
- data/spec/gmail_spec.rb +40 -39
- data/spec/recordings/gmail/_new_connects_with_client_and_give_it_context_when_block_given.yml +61 -28
- data/spec/recordings/gmail/_new_connects_with_gmail_service_and_return_valid_connection_object.yml +43 -28
- data/spec/recordings/gmail/_new_does_not_raise_error_when_couldn_t_connect_with_given_account.yml +21 -13
- data/spec/recordings/gmail/_new_raises_error_when_couldn_t_connect_with_given_account.yml +21 -13
- data/spec/recordings/gmail_client_plain/instance/delivers_inline_composed_email.yml +61 -42
- data/spec/recordings/gmail_client_plain/instance/does_not_log_in_when_given_gmail_account_is_invalid.yml +21 -13
- data/spec/recordings/gmail_client_plain/instance/does_not_raise_error_even_though_gmail_account_is_invalid.yml +21 -13
- data/spec/recordings/gmail_client_plain/instance/labels/checks_if_there_is_given_label_defined.yml +409 -196
- data/spec/recordings/gmail_client_plain/instance/labels/creates_given_label.yml +280 -151
- data/spec/recordings/gmail_client_plain/instance/labels/removes_existing_label.yml +271 -146
- data/spec/recordings/gmail_client_plain/instance/labels/returns_list_of_all_available_labels.yml +227 -113
- data/spec/recordings/gmail_client_plain/instance/properly_logs_in_to_valid_gmail_account.yml +61 -42
- data/spec/recordings/gmail_client_plain/instance/properly_logs_out_from_gmail.yml +61 -42
- data/spec/recordings/gmail_client_plain/instance/properly_switches_to_given_mailbox.yml +164 -109
- data/spec/recordings/gmail_client_plain/instance/properly_switches_to_given_mailbox_using_block_style.yml +164 -109
- data/spec/recordings/gmail_client_plain/instance/raises_error_when_given_gmail_account_is_invalid_and_errors_enabled.yml +21 -13
- data/spec/recordings/gmail_client_xo_auth2/instance/does_not_log_in_when_given_gmail_account_is_invalid.yml +24 -13
- data/spec/recordings/gmail_client_xo_auth2/instance/labels/checks_if_there_is_given_label_defined.yml +39 -27
- data/spec/recordings/gmail_client_xo_auth2/instance/labels/creates_given_label.yml +52 -39
- data/spec/recordings/gmail_client_xo_auth2/instance/labels/removes_existing_label.yml +52 -39
- data/spec/recordings/gmail_client_xo_auth2/instance/labels/returns_list_of_all_available_labels.yml +39 -27
- data/spec/recordings/gmail_client_xo_auth2/instance/properly_logs_in_to_valid_gmail_account.yml +26 -15
- data/spec/recordings/gmail_client_xo_auth2/instance/properly_logs_out_from_gmail.yml +26 -15
- data/spec/recordings/gmail_client_xo_auth2/instance/properly_switches_to_given_mailbox.yml +63 -40
- data/spec/recordings/gmail_client_xo_auth2/instance/properly_switches_to_given_mailbox_using_block_style.yml +63 -40
- data/spec/recordings/gmail_client_xo_auth2/instance/raises_error_when_given_gmail_account_is_invalid_and_errors_enabled.yml +24 -13
- data/spec/recordings/gmail_labels/localize/when_given_the_xl_is_tflag/all/localizes_into_the_appropriate_label.yml +229 -116
- data/spec/recordings/gmail_labels/localize/when_given_the_xl_is_tflag/and_the_mailbox_does_not_exist/returns_the_mailbox_name_as_a_string.yml +229 -110
- data/spec/recordings/gmail_labels/localize/when_given_the_xl_is_tflag/drafts/localizes_into_the_appropriate_label.yml +229 -116
- data/spec/recordings/gmail_labels/localize/when_given_the_xl_is_tflag/flagged/localizes_into_the_appropriate_label.yml +229 -116
- data/spec/recordings/gmail_labels/localize/when_given_the_xl_is_tflag/important/localizes_into_the_appropriate_label.yml +229 -116
- data/spec/recordings/gmail_labels/localize/when_given_the_xl_is_tflag/inbox/localizes_into_the_appropriate_label.yml +61 -42
- data/spec/recordings/gmail_labels/localize/when_given_the_xl_is_tflag/junk/localizes_into_the_appropriate_label.yml +229 -116
- data/spec/recordings/gmail_labels/localize/when_given_the_xl_is_tflag/sent/localizes_into_the_appropriate_label.yml +229 -116
- data/spec/recordings/gmail_labels/localize/when_given_the_xl_is_tflag/trash/localizes_into_the_appropriate_label.yml +229 -116
- data/spec/recordings/gmail_mailbox/instance/counts_all_emails.yml +595 -277
- data/spec/recordings/gmail_mailbox/instance/finds_messages.yml +1049 -586
- data/spec/recordings/gmail_mailbox/instance/waits_once.yml +191 -136
- data/spec/recordings/gmail_mailbox/instance/waits_repeatedly.yml +217 -141
- data/spec/recordings/gmail_mailbox/instance/waits_with_29-minute_re-issue.yml +188 -136
- data/spec/recordings/gmail_mailbox/instance/waits_with_an_unblocked_connection.yml +644 -207
- data/spec/recordings/gmail_mailbox/on_initialize/sets_client_and_name.yml +61 -42
- data/spec/recordings/gmail_mailbox/on_initialize/works_in_inbox_by_default.yml +61 -42
- data/spec/recordings/gmail_message/initialize/sets_prefetch_attrs.yml +1068 -578
- data/spec/recordings/gmail_message/initialize/sets_uid_and_mailbox.yml +1068 -580
- data/spec/recordings/gmail_message/instance_methods/deletes_itself.yml +1084 -637
- data/spec/recordings/gmail_message/instance_methods/marks_itself_read.yml +1137 -682
- data/spec/recordings/gmail_message/instance_methods/marks_itself_unread.yml +1153 -686
- data/spec/recordings/gmail_message/instance_methods/moves_from_one_tag_to_other.yml +1447 -862
- data/spec/recordings/gmail_message/instance_methods/removes_a_given_label.yml +1288 -776
- data/spec/recordings/gmail_message/instance_methods/removes_a_given_label_with_old_method.yml +1288 -776
- data/spec/recordings/gmail_message/instance_methods/sets_given_label.yml +1165 -690
- data/spec/recordings/gmail_message/instance_methods/sets_given_label_with_old_method.yml +1157 -691
- data/spec/spec_helper.rb +56 -53
- data/spec/support/imap_mock.rb +181 -181
- data/spec/support/obfuscation.rb +49 -52
- metadata +21 -90
- data/spec/recordings/gmail_client_plain/instance/_connection_automatically_logs_in_to_gmail_account_when_it_s_called.yml +0 -42
data/Rakefile
CHANGED
@@ -1,46 +1,44 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
require 'rdoc/
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
rdoc.
|
19
|
-
rdoc.
|
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
|
-
puts Spec::Obfuscation.decrypt_file(File.join(File.dirname(__FILE__), 'spec', 'account.yml.obfus'))
|
46
|
-
end
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
begin
|
8
|
+
require 'rdoc/task'
|
9
|
+
rescue LoadError
|
10
|
+
require 'rdoc/rdoc'
|
11
|
+
require 'rake/rdoctask'
|
12
|
+
RDoc::Task = Rake::RDocTask
|
13
|
+
end
|
14
|
+
|
15
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
16
|
+
rdoc.rdoc_dir = 'rdoc'
|
17
|
+
rdoc.title = "Gmail for Ruby #{Gmail::VERSION}"
|
18
|
+
rdoc.rdoc_files.include('README*')
|
19
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
20
|
+
end
|
21
|
+
|
22
|
+
Bundler::GemHelper.install_tasks
|
23
|
+
|
24
|
+
begin
|
25
|
+
require 'rspec/core/rake_task'
|
26
|
+
RSpec::Core::RakeTask.new(:spec)
|
27
|
+
rescue LoadError
|
28
|
+
task :spec do
|
29
|
+
abort 'Run `gem install rspec` to install RSpec'
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
task :default => :spec
|
34
|
+
|
35
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
36
|
+
require 'spec/support/obfuscation'
|
37
|
+
desc 'Obfuscates account.yml file.'
|
38
|
+
task :obfuscate do
|
39
|
+
Spec::Obfuscation.encrypt_file(File.join(File.dirname(__FILE__), 'spec', 'account.yml'))
|
40
|
+
end
|
41
|
+
|
42
|
+
task :deobfuscate do
|
43
|
+
puts Spec::Obfuscation.decrypt_file(File.join(File.dirname(__FILE__), 'spec', 'account.yml.obfus'))
|
44
|
+
end
|
data/gmail.gemspec
CHANGED
@@ -1,34 +1,32 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
s.
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
s.
|
14
|
-
s.
|
15
|
-
s.
|
16
|
-
s.
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
s.add_development_dependency "
|
26
|
-
s.add_development_dependency "
|
27
|
-
|
28
|
-
s.
|
29
|
-
|
30
|
-
s.
|
31
|
-
s.
|
32
|
-
|
33
|
-
s.require_paths = ["lib"]
|
34
|
-
end
|
1
|
+
$:.push File.expand_path('../lib', __FILE__)
|
2
|
+
require 'gmail/version'
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "gmail"
|
6
|
+
s.summary = "A Rubyesque interface to Gmail, with all the tools you will need."
|
7
|
+
s.description = "A Rubyesque interface to Gmail, with all the tools you will need.
|
8
|
+
Search, read and send multipart emails; archive, mark as read/unread,
|
9
|
+
delete emails; and manage labels.
|
10
|
+
"
|
11
|
+
s.version = Gmail::VERSION
|
12
|
+
s.platform = Gem::Platform::RUBY
|
13
|
+
s.authors = ["Chris Kowalik"]
|
14
|
+
s.email = ["chris@nu7hat.ch"]
|
15
|
+
s.homepage = "http://github.com/gmailgem/gmail"
|
16
|
+
s.licenses = ["MIT"]
|
17
|
+
|
18
|
+
# runtime dependencies
|
19
|
+
s.add_dependency "gmail_xoauth", ">= 0.3.0"
|
20
|
+
s.add_dependency "mail", ">= 2.2.1"
|
21
|
+
|
22
|
+
# development dependencies
|
23
|
+
s.add_development_dependency "gem-release"
|
24
|
+
s.add_development_dependency "rake"
|
25
|
+
s.add_development_dependency "rspec", ">= 3.1"
|
26
|
+
s.add_development_dependency "rubocop", ">= 0.34.2"
|
27
|
+
|
28
|
+
s.files = `git ls-files`.split("\n")
|
29
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
30
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
|
31
|
+
s.require_paths = ["lib"]
|
32
|
+
end
|
data/lib/gmail.rb
CHANGED
@@ -1,78 +1,72 @@
|
|
1
|
-
require 'net/imap'
|
2
|
-
require 'net/smtp'
|
3
|
-
require 'mail'
|
4
|
-
require 'date'
|
5
|
-
require 'time'
|
6
|
-
|
7
|
-
if RUBY_VERSION < "1.8.7"
|
8
|
-
require "smtp_tls"
|
9
|
-
end
|
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
|
-
def
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
client
|
75
|
-
end
|
76
|
-
alias_method :connect!, :new!
|
77
|
-
end # << self
|
78
|
-
end # Gmail
|
1
|
+
require 'net/imap'
|
2
|
+
require 'net/smtp'
|
3
|
+
require 'mail'
|
4
|
+
require 'date'
|
5
|
+
require 'time'
|
6
|
+
|
7
|
+
if RUBY_VERSION < "1.8.7"
|
8
|
+
require "smtp_tls"
|
9
|
+
end
|
10
|
+
|
11
|
+
module Gmail
|
12
|
+
autoload :Version, "gmail/version"
|
13
|
+
autoload :Client, "gmail/client"
|
14
|
+
autoload :Labels, "gmail/labels"
|
15
|
+
autoload :Mailbox, "gmail/mailbox"
|
16
|
+
autoload :Message, "gmail/message"
|
17
|
+
|
18
|
+
class << self
|
19
|
+
# Creates new Gmail connection using given authorization options.
|
20
|
+
#
|
21
|
+
# ==== Examples
|
22
|
+
#
|
23
|
+
# Gmail.new(:plain, "foo@gmail.com", "password")
|
24
|
+
# Gmail.new(:xoauth, "foo@gmail.com",
|
25
|
+
# :consumer_key => "",
|
26
|
+
# :consumer_secret => "",
|
27
|
+
# :token => "",
|
28
|
+
# :secret => "")
|
29
|
+
#
|
30
|
+
# To use plain authentication method you can also call:
|
31
|
+
#
|
32
|
+
# Gmail.new("foo@gmail.com", "password")
|
33
|
+
#
|
34
|
+
# You can also use block-style call:
|
35
|
+
#
|
36
|
+
# Gmail.new("foo@gmail.com", "password") do |client|
|
37
|
+
# # ...
|
38
|
+
# end
|
39
|
+
#
|
40
|
+
|
41
|
+
def new(*args, &block)
|
42
|
+
args.unshift(:plain) unless args.first.is_a?(Symbol)
|
43
|
+
client = Gmail::Client.new(*args)
|
44
|
+
client.connect
|
45
|
+
client.login
|
46
|
+
|
47
|
+
check_with_block(client, &block)
|
48
|
+
end
|
49
|
+
alias_method :connect, :new
|
50
|
+
|
51
|
+
def new!(*args, &block)
|
52
|
+
args.unshift(:plain) unless args.first.is_a?(Symbol)
|
53
|
+
client = Gmail::Client.new(*args)
|
54
|
+
client.connect!
|
55
|
+
client.login!
|
56
|
+
|
57
|
+
check_with_block(client, &block)
|
58
|
+
end
|
59
|
+
alias_method :connect!, :new!
|
60
|
+
|
61
|
+
private
|
62
|
+
|
63
|
+
def check_with_block(client, &block)
|
64
|
+
if block_given?
|
65
|
+
yield client
|
66
|
+
client.logout
|
67
|
+
end
|
68
|
+
|
69
|
+
client
|
70
|
+
end
|
71
|
+
end # << self
|
72
|
+
end # Gmail
|
data/lib/gmail/client.rb
CHANGED
@@ -1,34 +1,35 @@
|
|
1
|
-
module Gmail
|
2
|
-
module Client
|
3
|
-
# Raised when connection with Gmail IMAP service couldn't be established.
|
4
|
-
class ConnectionError < SocketError; end
|
5
|
-
# Raised when given username or password are invalid.
|
6
|
-
class AuthorizationError < Net::IMAP::NoResponseError; end
|
7
|
-
# Raised when delivered email is invalid.
|
8
|
-
class DeliveryError < ArgumentError; end
|
9
|
-
# Raised when given client is not registered
|
10
|
-
class UnknownClient < ArgumentError; end
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
end
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
require 'gmail/
|
30
|
-
require 'gmail/client/
|
31
|
-
require 'gmail/client/
|
32
|
-
require 'gmail/client/
|
33
|
-
|
34
|
-
end #
|
1
|
+
module Gmail
|
2
|
+
module Client
|
3
|
+
# Raised when connection with Gmail IMAP service couldn't be established.
|
4
|
+
class ConnectionError < SocketError; end
|
5
|
+
# Raised when given username or password are invalid.
|
6
|
+
class AuthorizationError < Net::IMAP::NoResponseError; end
|
7
|
+
# Raised when delivered email is invalid.
|
8
|
+
class DeliveryError < ArgumentError; end
|
9
|
+
# Raised when given client is not registered
|
10
|
+
class UnknownClient < ArgumentError; end
|
11
|
+
# Raised when email not found
|
12
|
+
class EmailNotFound < ArgumentError; end
|
13
|
+
|
14
|
+
def self.clients
|
15
|
+
@clients ||= {}
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.register(name, klass)
|
19
|
+
clients[name] = klass
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.new(name, *args)
|
23
|
+
if client = clients[name]
|
24
|
+
return client.new(*args)
|
25
|
+
end
|
26
|
+
raise UnknownClient, "No such client: #{name}"
|
27
|
+
end
|
28
|
+
|
29
|
+
require 'gmail/imap_extensions'
|
30
|
+
require 'gmail/client/base'
|
31
|
+
require 'gmail/client/plain'
|
32
|
+
require 'gmail/client/xoauth'
|
33
|
+
require 'gmail/client/xoauth2'
|
34
|
+
end # Client
|
35
|
+
end # Gmail
|
data/lib/gmail/client/base.rb
CHANGED
@@ -1,229 +1,244 @@
|
|
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
|
-
@imap
|
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
|
-
# mail
|
88
|
-
#
|
89
|
-
#
|
90
|
-
#
|
91
|
-
#
|
92
|
-
#
|
93
|
-
#
|
94
|
-
#
|
95
|
-
#
|
96
|
-
#
|
97
|
-
#
|
98
|
-
#
|
99
|
-
#
|
100
|
-
#
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
mail = Mail.new
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
mail
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
#
|
117
|
-
#
|
118
|
-
#
|
119
|
-
#
|
120
|
-
#
|
121
|
-
#
|
122
|
-
#
|
123
|
-
#
|
124
|
-
#
|
125
|
-
#
|
126
|
-
#
|
127
|
-
#
|
128
|
-
#
|
129
|
-
# gmail.
|
130
|
-
#
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
#
|
146
|
-
#
|
147
|
-
#
|
148
|
-
#
|
149
|
-
# mailbox
|
150
|
-
#
|
151
|
-
#
|
152
|
-
#
|
153
|
-
#
|
154
|
-
#
|
155
|
-
#
|
156
|
-
#
|
157
|
-
#
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
mailbox_stack
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
alias :
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
end
|
1
|
+
module Gmail
|
2
|
+
module Client
|
3
|
+
class Base
|
4
|
+
# Gmail IMAP defaults
|
5
|
+
GMAIL_IMAP_HOST = 'imap.gmail.com'.freeze
|
6
|
+
GMAIL_IMAP_PORT = 993
|
7
|
+
|
8
|
+
# Gmail SMTP defaults
|
9
|
+
GMAIL_SMTP_HOST = "smtp.gmail.com".freeze
|
10
|
+
GMAIL_SMTP_PORT = 587
|
11
|
+
|
12
|
+
attr_reader :username
|
13
|
+
attr_reader :options
|
14
|
+
|
15
|
+
def initialize(username, options = {})
|
16
|
+
defaults = {}
|
17
|
+
@username = fill_username(username)
|
18
|
+
@options = defaults.merge(options)
|
19
|
+
@mailbox_mutex = Mutex.new
|
20
|
+
end
|
21
|
+
|
22
|
+
# Connect to gmail service.
|
23
|
+
def connect(raise_errors = false)
|
24
|
+
@imap = Net::IMAP.new(GMAIL_IMAP_HOST, GMAIL_IMAP_PORT, true, nil, false)
|
25
|
+
Gmail::ImapExtensions.patch_net_imap_response_parser
|
26
|
+
@imap
|
27
|
+
rescue SocketError
|
28
|
+
raise_errors and raise ConnectionError, "Couldn't establish connection with Gmail IMAP service"
|
29
|
+
end
|
30
|
+
|
31
|
+
# This version of connect will raise error on failure...
|
32
|
+
def connect!
|
33
|
+
connect(true)
|
34
|
+
end
|
35
|
+
|
36
|
+
# Return current connection. Log in automaticaly to specified account if
|
37
|
+
# it is necessary.
|
38
|
+
def connection
|
39
|
+
login and at_exit { logout } unless logged_in?
|
40
|
+
@imap
|
41
|
+
end
|
42
|
+
alias :conn :connection
|
43
|
+
|
44
|
+
# Login to specified account.
|
45
|
+
def login(*args)
|
46
|
+
raise NotImplementedError, "The `#{self.class.name}#login` method is not implemented."
|
47
|
+
end
|
48
|
+
alias :sign_in :login
|
49
|
+
|
50
|
+
# This version of login will raise error on failure...
|
51
|
+
def login!
|
52
|
+
login(true)
|
53
|
+
end
|
54
|
+
alias :sign_in! :login!
|
55
|
+
|
56
|
+
# Returns +true+ when you are logged in to specified account.
|
57
|
+
def logged_in?
|
58
|
+
!!@logged_in
|
59
|
+
end
|
60
|
+
alias :signed_in? :logged_in?
|
61
|
+
|
62
|
+
# Logout from Gmail service.
|
63
|
+
def logout
|
64
|
+
@imap && logged_in? and @imap.logout
|
65
|
+
ensure
|
66
|
+
@logged_in = false
|
67
|
+
end
|
68
|
+
alias :sign_out :logout
|
69
|
+
|
70
|
+
# Disconnect from Gmail service.
|
71
|
+
def disconnect
|
72
|
+
@imap && @imap.disconnect
|
73
|
+
end
|
74
|
+
|
75
|
+
# Return labels object, which helps you with managing your Gmail labels.
|
76
|
+
# See <tt>Gmail::Labels</tt> for details.
|
77
|
+
def labels
|
78
|
+
@labels ||= Labels.new(conn)
|
79
|
+
end
|
80
|
+
|
81
|
+
# Compose new e-mail.
|
82
|
+
#
|
83
|
+
# ==== Examples
|
84
|
+
#
|
85
|
+
# mail = gmail.compose
|
86
|
+
# mail.from "test@gmail.org"
|
87
|
+
# mail.to "friend@gmail.com"
|
88
|
+
#
|
89
|
+
# ... or block style:
|
90
|
+
#
|
91
|
+
# mail = gmail.compose do
|
92
|
+
# from "test@gmail.org"
|
93
|
+
# to "friend@gmail.com"
|
94
|
+
# subject "Hello!"
|
95
|
+
# body "Hello my friend! long time..."
|
96
|
+
# end
|
97
|
+
#
|
98
|
+
# Now you can deliver your mail:
|
99
|
+
#
|
100
|
+
# gmail.deliver(mail)
|
101
|
+
def compose(mail = nil, &block)
|
102
|
+
if block_given?
|
103
|
+
mail = Mail.new(&block)
|
104
|
+
elsif !mail
|
105
|
+
mail = Mail.new
|
106
|
+
end
|
107
|
+
|
108
|
+
mail.delivery_method(*smtp_settings)
|
109
|
+
mail.from = username unless mail.from
|
110
|
+
mail
|
111
|
+
end
|
112
|
+
alias :message :compose
|
113
|
+
|
114
|
+
# Compose (optionaly) and send given email.
|
115
|
+
#
|
116
|
+
# ==== Examples
|
117
|
+
#
|
118
|
+
# gmail.deliver do
|
119
|
+
# to "friend@gmail.com"
|
120
|
+
# subject "Hello friend!"
|
121
|
+
# body "Hi! How are you?"
|
122
|
+
# end
|
123
|
+
#
|
124
|
+
# ... or with already created message:
|
125
|
+
#
|
126
|
+
# mail = Mail.new { ... }
|
127
|
+
# gmail.deliver(mail)
|
128
|
+
#
|
129
|
+
# mail = gmail.compose { ... }
|
130
|
+
# gmail.deliver(mail)
|
131
|
+
def deliver(mail = nil, raise_errors = false, &block)
|
132
|
+
mail = compose(mail, &block)
|
133
|
+
mail.deliver!
|
134
|
+
rescue Object => ex
|
135
|
+
raise_errors and raise DeliveryError, "Couldn't deliver email: #{ex.to_s}"
|
136
|
+
end
|
137
|
+
|
138
|
+
# This version of deliver will raise error on failure...
|
139
|
+
def deliver!(mail = nil, &block)
|
140
|
+
deliver(mail, true, &block)
|
141
|
+
end
|
142
|
+
|
143
|
+
# Do something with given mailbox or within it context.
|
144
|
+
#
|
145
|
+
# ==== Examples
|
146
|
+
#
|
147
|
+
# mailbox = gmail.mailbox("INBOX")
|
148
|
+
# mailbox.emails(:all)
|
149
|
+
# mailbox.count(:unread, :before => Time.now-(20*24*3600))
|
150
|
+
#
|
151
|
+
# ... or block style:
|
152
|
+
#
|
153
|
+
# gmail.label("Work") do |mailbox|
|
154
|
+
# mailbox.emails(:unread)
|
155
|
+
# mailbox.count(:all)
|
156
|
+
# ...
|
157
|
+
# end
|
158
|
+
def mailbox(name, &block)
|
159
|
+
@mailbox_mutex.synchronize do
|
160
|
+
name = labels.localize(name)
|
161
|
+
mailbox = (mailboxes[name] ||= Mailbox.new(self, name))
|
162
|
+
switch_to_mailbox(mailbox) if @current_mailbox != mailbox
|
163
|
+
|
164
|
+
if block_given?
|
165
|
+
mailbox_stack << @current_mailbox
|
166
|
+
result = block.arity == 1 ? yield(mailbox) : yield
|
167
|
+
mailbox_stack.pop
|
168
|
+
switch_to_mailbox(mailbox_stack.last)
|
169
|
+
return result
|
170
|
+
end
|
171
|
+
|
172
|
+
return mailbox
|
173
|
+
end
|
174
|
+
end
|
175
|
+
alias :in_mailbox :mailbox
|
176
|
+
alias :in_label :mailbox
|
177
|
+
alias :label :mailbox
|
178
|
+
|
179
|
+
# Alias for <tt>mailbox("INBOX")</tt>. See <tt>Gmail::Mailbox</tt>
|
180
|
+
# for details.
|
181
|
+
def inbox
|
182
|
+
mailbox("INBOX")
|
183
|
+
end
|
184
|
+
|
185
|
+
# Functionality like rails #find method
|
186
|
+
# https://support.google.com/mail/answer/7190?hl=en
|
187
|
+
# Messages with a certain message-id header
|
188
|
+
# Rfc822msgid:
|
189
|
+
# Example: rfc822msgid:200503292@example.com
|
190
|
+
def find(rfc822msgid)
|
191
|
+
message = :message_before_built
|
192
|
+
|
193
|
+
mailbox(:all) do |mailbox|
|
194
|
+
uid = conn.uid_search(['X-GM-RAW', "rfc822msgid:#{rfc822msgid.to_s.strip}"]).first
|
195
|
+
raise EmailNotFound, "Can't find message with ID #{rfc822msgid}" unless uid
|
196
|
+
message = Message.new(mailbox, uid)
|
197
|
+
end
|
198
|
+
|
199
|
+
message
|
200
|
+
end
|
201
|
+
|
202
|
+
def mailboxes
|
203
|
+
@mailboxes ||= {}
|
204
|
+
end
|
205
|
+
|
206
|
+
def inspect
|
207
|
+
"#<Gmail::Client#{'0x%04x' % (object_id << 1)} (#{username}) #{'dis' if !logged_in?}connected>"
|
208
|
+
end
|
209
|
+
|
210
|
+
def fill_username(username)
|
211
|
+
username =~ /@/ ? username : "#{username}@gmail.com"
|
212
|
+
end
|
213
|
+
|
214
|
+
def mail_domain
|
215
|
+
username.split('@').last
|
216
|
+
end
|
217
|
+
|
218
|
+
private
|
219
|
+
|
220
|
+
def switch_to_mailbox(mailbox)
|
221
|
+
if mailbox
|
222
|
+
conn.select(mailbox.encoded_name)
|
223
|
+
end
|
224
|
+
@current_mailbox = mailbox
|
225
|
+
end
|
226
|
+
|
227
|
+
def mailbox_stack
|
228
|
+
@mailbox_stack ||= []
|
229
|
+
end
|
230
|
+
|
231
|
+
def smtp_settings
|
232
|
+
[:smtp, {
|
233
|
+
:address => GMAIL_SMTP_HOST,
|
234
|
+
:port => GMAIL_SMTP_PORT,
|
235
|
+
:domain => mail_domain,
|
236
|
+
:user_name => username,
|
237
|
+
:password => password,
|
238
|
+
:authentication => 'plain',
|
239
|
+
:enable_starttls_auto => true
|
240
|
+
}]
|
241
|
+
end
|
242
|
+
end # Base
|
243
|
+
end # Client
|
244
|
+
end # Gmail
|