pjstadig-easy_imap 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,4 @@
1
+ == 0.0.1 2008-12-10
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
data/Manifest.txt ADDED
@@ -0,0 +1,17 @@
1
+ History.txt
2
+ Manifest.txt
3
+ PostInstall.txt
4
+ README.rdoc
5
+ Rakefile
6
+ lib/easy_imap.rb
7
+ lib/easy_imap/attachment.rb
8
+ lib/easy_imap/folder.rb
9
+ lib/easy_imap/message.rb
10
+ lib/easy_imap/server.rb
11
+ script/console
12
+ script/destroy
13
+ script/generate
14
+ spec/easy_imap_spec.rb
15
+ spec/spec.opts
16
+ spec/spec_helper.rb
17
+ tasks/rspec.rake
data/PostInstall.txt ADDED
@@ -0,0 +1,7 @@
1
+
2
+ For more information on easy_imap, see http://easy_imap.rubyforge.org
3
+
4
+ NOTE: Change this information in PostInstall.txt
5
+ You can also delete it if you don't want it.
6
+
7
+
data/README.rdoc ADDED
@@ -0,0 +1,57 @@
1
+ = easy_imap
2
+
3
+ * http://easy-imap.rubyforge.org/
4
+
5
+ == DESCRIPTION:
6
+
7
+ A simple interface to proccessing e-mail messages using IMAP, including handling multipart messages and attachments.
8
+
9
+ == SYNOPSIS:
10
+
11
+ Create a connection using +EasyIMAP.connect+, and with the connection you can access Folders and Messages. To
12
+ get a list of all Folders use +Server.folders+, to access the messages in a folder use +Folder.messages+, and to
13
+ access the Attachments on a Message use +Message.attachments+.
14
+
15
+ EasyIMAP::Server.connect('mail.example.com', :username => 'username, :password => 'password) do |conn|
16
+ inbox = conn.folders.first
17
+ inbox.messages.each do |m|
18
+ if m.attachments.any? && m.attachments.first.content_type == "application/vnd.ms-excel"
19
+ File.open("/tmp/first_attachment.xls", "w") do |f|
20
+ f.write(m.attachments.first.body)
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+ == REQUIREMENTS:
27
+
28
+ * tmail
29
+
30
+ == INSTALL:
31
+
32
+ * sudo gem install tmail easy_imap
33
+
34
+ == LICENSE:
35
+
36
+ (The MIT License)
37
+
38
+ Copyright (c) 2008 Paul Stadig
39
+
40
+ Permission is hereby granted, free of charge, to any person obtaining
41
+ a copy of this software and associated documentation files (the
42
+ 'Software'), to deal in the Software without restriction, including
43
+ without limitation the rights to use, copy, modify, merge, publish,
44
+ distribute, sublicense, and/or sell copies of the Software, and to
45
+ permit persons to whom the Software is furnished to do so, subject to
46
+ the following conditions:
47
+
48
+ The above copyright notice and this permission notice shall be
49
+ included in all copies or substantial portions of the Software.
50
+
51
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
52
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
53
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
54
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
55
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
56
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
57
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ %w[rubygems rake rake/clean fileutils newgem rubigen].each { |f| require f }
2
+ require File.dirname(__FILE__) + '/lib/easy_imap'
3
+
4
+ # Generate all the Rake tasks
5
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
6
+ $hoe = Hoe.new('easy_imap', EasyIMAP::VERSION) do |p|
7
+ p.developer('Paul Stadig', 'paul@stadig.name')
8
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
9
+ p.post_install_message = 'PostInstall.txt' # TODO remove if post-install message not required
10
+ p.rubyforge_name = p.name # TODO this is default value
11
+ # p.extra_deps = [
12
+ # ['activesupport','>= 2.0.2'],
13
+ # ]
14
+ p.extra_dev_deps = [
15
+ ['newgem', ">= #{::Newgem::VERSION}"]
16
+ ]
17
+
18
+ p.clean_globs |= %w[**/.DS_Store tmp *.log]
19
+ path = (p.rubyforge_name == p.name) ? p.rubyforge_name : "\#{p.rubyforge_name}/\#{p.name}"
20
+ p.remote_rdoc_dir = File.join(path.gsub(/^#{p.rubyforge_name}\/?/,''), 'rdoc')
21
+ p.rsync_args = '-av --delete --ignore-errors'
22
+ end
23
+
24
+ require 'newgem/tasks' # load /tasks/*.rake
25
+ Dir['tasks/**/*.rake'].each { |t| load t }
26
+
27
+ task :default => [:spec, :features]
@@ -0,0 +1,22 @@
1
+ module EasyIMAP
2
+ class Attachment
3
+ def initialize(body)
4
+ @body = body
5
+ end
6
+
7
+ # The mime type of this attachment.
8
+ def content_type
9
+ @body.content_type
10
+ end
11
+
12
+ # The body of this attachment.
13
+ def body
14
+ @body.body
15
+ end
16
+
17
+ def to_s
18
+ "<#{self.class}: @content_type='#{content_type}'>"
19
+ end
20
+ alias_method(:inspect, :to_s)
21
+ end
22
+ end
@@ -0,0 +1,33 @@
1
+ module EasyIMAP
2
+ # A Folder on the IMAP server.
3
+ class Folder
4
+ # the name of the folder.
5
+ attr_reader :name
6
+
7
+ # Creates an instance representing a folder with +name+, on
8
+ # the server connection +conn+, with the folder delimiter +delim+.
9
+ #
10
+ # Normally this class is only instantiated internally.
11
+ def initialize(conn, name, delim)
12
+ @conn = conn
13
+ @full_name = name
14
+ @name = name.split(delim).last
15
+ @delim = delim
16
+ end
17
+
18
+ # An array of messages in this folder.
19
+ def messages
20
+ @conn.examine(@full_name)
21
+ @conn.uid_search(['ALL']).map do |uid|
22
+ Message.new(@conn, uid)
23
+ end
24
+ end
25
+
26
+ # An array of folders in this folder.
27
+ def folders
28
+ @conn.list("#{@full_name}#{@delim}", '%').map do |f|
29
+ Folder.new(@conn, f.name, f.delim)
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,116 @@
1
+ gem 'tmail'
2
+ require 'tmail'
3
+ require 'time'
4
+
5
+ module EasyIMAP
6
+ class Message
7
+ def initialize(conn, uid)
8
+ @conn = conn
9
+ @uid = uid
10
+ end
11
+
12
+ # The date this message was sent (an instance of Time).
13
+ def date
14
+ Time.parse(envelope[:date])
15
+ end
16
+
17
+ # The subject of this message.
18
+ def subject
19
+ envelope[:subject]
20
+ end
21
+
22
+ # An array of from e-mail addresses (as Strings).
23
+ def from
24
+ (envelope[:from] || []).map {|a| address(a)}
25
+ end
26
+
27
+ # An array of sender e-mail addresses (as Strings).
28
+ def sender
29
+ (envelope[:sender] || []).map{|a| address(a)}
30
+ end
31
+
32
+ # An array of reply_to e-mail addresses (as Strings).
33
+ def reply_to
34
+ (envelope[:reply_to] || []).map{|a| address(a)}
35
+ end
36
+
37
+ # An array of to e-mail addresses (as Strings).
38
+ def to
39
+ (envelope[:to] || []).map {|a| address(a)}
40
+ end
41
+
42
+ # An array of cc e-mail addresses (as Strings).
43
+ def cc
44
+ (envelope[:cc] || []).map {|a| address(a)}
45
+ end
46
+
47
+ # An array of bcc e-mail addresses (as Strings).
48
+ def bcc
49
+ (envelope[:bcc] || []).map {|a| address(a)}
50
+ end
51
+
52
+ # The in_reply_to attribute (as a String).
53
+ def in_reply_to
54
+ envelope[:in_reply_to]
55
+ end
56
+
57
+ # Returns true if this message is a multipart message.
58
+ def multipart?
59
+ body.multipart?
60
+ end
61
+
62
+ # Returns the text/plain portion of this message
63
+ # (which could be the body, or one of the parts
64
+ # if the message is multipart), or the empty
65
+ # string if there is no text/plain portion.
66
+ def text
67
+ text = find_part("text/plain")
68
+ text ? text.body : ""
69
+ end
70
+
71
+ # Returns the text/html portion of this message
72
+ # (which could be the body, or one of the parts
73
+ # if the message is multipart), or the empty
74
+ # string if there is no text/html portion.
75
+ def html
76
+ html = find_part("text/html")
77
+ html ? html.body : ""
78
+ end
79
+
80
+ # An array of Attachment instances if this
81
+ # message is a multipart message with
82
+ # attachments, or an empty array otherwise.
83
+ def attachments
84
+ @attachments ||= if body.multipart?
85
+ body.parts.find_all{|p| p.disposition == "attachment"}.map{|a| Attachment.new(a)}
86
+ else
87
+ []
88
+ end
89
+ end
90
+
91
+ private
92
+ def address(a)
93
+ if a[:name]
94
+ "\"#{a[:name]}\" <#{a[:mailbox]}@#{a[:host]}>"
95
+ else
96
+ "#{a[:mailbox]}@#{a[:host]}"
97
+ end
98
+ end
99
+
100
+ def envelope
101
+ @conn.uid_fetch(@uid, "ENVELOPE").first.attr["ENVELOPE"]
102
+ end
103
+
104
+ def body
105
+ @body ||= TMail::Mail.parse(@conn.uid_fetch(@uid, "BODY[]").first.attr["BODY[]"])
106
+ end
107
+
108
+ def find_part(mime_type)
109
+ if !body.multipart? && body.content_type == mime_type
110
+ body
111
+ elsif body.multipart?
112
+ body.parts.find{|p| p.content_type == mime_type}
113
+ end
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,43 @@
1
+ require 'net/imap'
2
+
3
+ module EasyIMAP
4
+ # A connection to an IMAP server.
5
+ class Server
6
+ # Makes a connection to +host+ and returns an instance of Server.
7
+ #
8
+ # If given a block it will yield the Server instance to the
9
+ # block, and then close the connection to the IMAP server
10
+ # after the block has finished. Otherwise you must close the connection
11
+ # yourself.
12
+ def self.connect(host, options = nil)
13
+ conn = Server.new(host, options)
14
+ if block_given?
15
+ begin
16
+ yield conn
17
+ ensure
18
+ conn.close
19
+ end
20
+ end
21
+ end
22
+
23
+ def initialize(host, options = nil)
24
+ options ||= {}
25
+ @host = host
26
+ @conn = Net::IMAP.new(host)#, options[:port])
27
+ @conn.authenticate(options[:auth_type] || "LOGIN", options[:username], options[:password])
28
+ end
29
+
30
+ # Returns an array of Folder instances, one for each
31
+ # root level folder on the server.
32
+ def folders
33
+ @conn.list('', '%').map do |f|
34
+ Folder.new(@conn, f.name, f.delim)
35
+ end
36
+ end
37
+
38
+ # Closes the connection to the IMAP server.
39
+ def close
40
+ @conn.disconnect
41
+ end
42
+ end
43
+ end
data/lib/easy_imap.rb ADDED
@@ -0,0 +1,11 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ module EasyIMAP
5
+ VERSION = '0.0.1'
6
+ end
7
+
8
+ require 'easy_imap/server'
9
+ require 'easy_imap/folder'
10
+ require 'easy_imap/message'
11
+ require 'easy_imap/attachment'
data/script/console ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # File: script/console
3
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
+
5
+ libs = " -r irb/completion"
6
+ # Perhaps use a console_lib to store any extra methods I may want available in the cosole
7
+ # libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
8
+ libs << " -r #{File.dirname(__FILE__) + '/../lib/easy_imap.rb'}"
9
+ puts "Loading easy_imap gem"
10
+ exec "#{irb} #{libs} --simple-prompt"
data/script/destroy ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
data/script/generate ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)
@@ -0,0 +1,11 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+
3
+ # Time to add your specs!
4
+ # http://rspec.info/
5
+ describe "Place your specs here" do
6
+
7
+ it "find this spec in spec directory" do
8
+ violated "Be sure to write your specs"
9
+ end
10
+
11
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --colour
@@ -0,0 +1,10 @@
1
+ begin
2
+ require 'spec'
3
+ rescue LoadError
4
+ require 'rubygems'
5
+ gem 'rspec'
6
+ require 'spec'
7
+ end
8
+
9
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
10
+ require 'easy_imap'
data/tasks/rspec.rake ADDED
@@ -0,0 +1,21 @@
1
+ begin
2
+ require 'spec'
3
+ rescue LoadError
4
+ require 'rubygems'
5
+ require 'spec'
6
+ end
7
+ begin
8
+ require 'spec/rake/spectask'
9
+ rescue LoadError
10
+ puts <<-EOS
11
+ To use rspec for testing you must install rspec gem:
12
+ gem install rspec
13
+ EOS
14
+ exit(0)
15
+ end
16
+
17
+ desc "Run the specs under spec/models"
18
+ Spec::Rake::SpecTask.new do |t|
19
+ t.spec_opts = ['--options', "spec/spec.opts"]
20
+ t.spec_files = FileList['spec/**/*_spec.rb']
21
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pjstadig-easy_imap
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Paul Stadig
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-12-10 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: newgem
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.1.0
23
+ version:
24
+ - !ruby/object:Gem::Dependency
25
+ name: hoe
26
+ version_requirement:
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: 1.8.0
32
+ version:
33
+ description: A simple interface to proccessing e-mail messages using IMAP, including handling multipart messages and attachments.
34
+ email:
35
+ - paul@stadig.name
36
+ executables: []
37
+
38
+ extensions: []
39
+
40
+ extra_rdoc_files:
41
+ - History.txt
42
+ - Manifest.txt
43
+ - PostInstall.txt
44
+ - README.rdoc
45
+ files:
46
+ - History.txt
47
+ - Manifest.txt
48
+ - PostInstall.txt
49
+ - README.rdoc
50
+ - Rakefile
51
+ - lib/easy_imap.rb
52
+ - lib/easy_imap/attachment.rb
53
+ - lib/easy_imap/folder.rb
54
+ - lib/easy_imap/message.rb
55
+ - lib/easy_imap/server.rb
56
+ - script/console
57
+ - script/destroy
58
+ - script/generate
59
+ - spec/easy_imap_spec.rb
60
+ - spec/spec.opts
61
+ - spec/spec_helper.rb
62
+ - tasks/rspec.rake
63
+ has_rdoc: true
64
+ homepage: http://easy-imap.rubyforge.org/
65
+ post_install_message: PostInstall.txt
66
+ rdoc_options:
67
+ - --main
68
+ - README.rdoc
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: "0"
76
+ version:
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: "0"
82
+ version:
83
+ requirements: []
84
+
85
+ rubyforge_project: easy_imap
86
+ rubygems_version: 1.2.0
87
+ signing_key:
88
+ specification_version: 2
89
+ summary: A simple interface to proccessing e-mail messages using IMAP, including handling multipart messages and attachments.
90
+ test_files: []
91
+