easy_imap 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,10 @@
1
+ == 0.0.1 2008-12-10
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
5
+
6
+ == 0.0.2 2008-12-17
7
+
8
+ * 1 minor enhancement:
9
+ * Added support for simple LOGIN authentication. (intabulas)
10
+
data/Manifest.txt ADDED
@@ -0,0 +1,18 @@
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
+ script/txt2html
15
+ spec/easy_imap_spec.rb
16
+ spec/spec.opts
17
+ spec/spec_helper.rb
18
+ 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,62 @@
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::Server.connect, and with the connection you can access Folders and Messages. To
12
+ get a list of all Folders use EasyIMAP::Server#folders, to access the messages in a folder use EasyIMAP::Folder#messages, and to
13
+ access the Attachments on a Message use EasyIMAP::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 easy_imap
33
+
34
+ == CONTRIBUTORS:
35
+
36
+ * Paul Stadig (pjstadig)
37
+ * Mark Lussier (intabulas)
38
+
39
+ == LICENSE:
40
+
41
+ (The MIT License)
42
+
43
+ Copyright (c) 2008 Paul Stadig
44
+
45
+ Permission is hereby granted, free of charge, to any person obtaining
46
+ a copy of this software and associated documentation files (the
47
+ 'Software'), to deal in the Software without restriction, including
48
+ without limitation the rights to use, copy, modify, merge, publish,
49
+ distribute, sublicense, and/or sell copies of the Software, and to
50
+ permit persons to whom the Software is furnished to do so, subject to
51
+ the following conditions:
52
+
53
+ The above copyright notice and this permission notice shall be
54
+ included in all copies or substantial portions of the Software.
55
+
56
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
57
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
58
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
59
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
60
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
61
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
62
+ 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
+ ['tmail','>= 1.2.3.1'],
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,26 @@
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
+ def filename
13
+ @body.disposition_param("filename")
14
+ end
15
+
16
+ # The body of this attachment.
17
+ def body
18
+ @body.body
19
+ end
20
+
21
+ def to_s
22
+ "<#{self.class}: @content_type='#{content_type}'>"
23
+ end
24
+ alias_method(:inspect, :to_s)
25
+ end
26
+ 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,59 @@
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
+ # It is recommended that you use this method to connect instead
9
+ # of Server#new, because at some time in the future there may be
10
+ # multiple server classes to smooth over "quirks" in IMAP
11
+ # implementations. This method should automatically select the
12
+ # right server class for you.
13
+ #
14
+ # If given a block it will yield the Server instance to the
15
+ # block, and then close the connection to the IMAP server
16
+ # after the block has finished. Otherwise you must close the connection
17
+ # yourself.
18
+ #
19
+ # Valid options are:
20
+ # port:: the server port on which to connect (defaults to 143)
21
+ # username:: the username with which to connect
22
+ # password:: the password with which to connect
23
+ # auth_type:: the authentication type with which to connect (defaults to LOGIN)
24
+ def self.connect(host, options = nil)
25
+ conn = Server.new(host, options)
26
+ if block_given?
27
+ begin
28
+ yield conn
29
+ ensure
30
+ conn.close
31
+ end
32
+ end
33
+ end
34
+
35
+ def initialize(host, options = nil)
36
+ options ||= {}
37
+ @host = host
38
+ @conn = Net::IMAP.new(host, options[:port] || 143)
39
+ if options[:auth_type].to_s.downcase == "plain"
40
+ @conn.login(options[:username], options[:password])
41
+ else
42
+ @conn.authenticate(options[:auth_type] || "LOGIN", options[:username], options[:password])
43
+ end
44
+ end
45
+
46
+ # Returns an array of Folder instances, one for each
47
+ # root level folder on the server.
48
+ def folders
49
+ @conn.list('', '%').map do |f|
50
+ Folder.new(@conn, f.name, f.delim)
51
+ end
52
+ end
53
+
54
+ # Closes the connection to the IMAP server.
55
+ def close
56
+ @conn.disconnect
57
+ end
58
+ end
59
+ 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.2'
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)
data/script/txt2html ADDED
@@ -0,0 +1,71 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ load File.dirname(__FILE__) + "/../Rakefile"
4
+ require 'rubyforge'
5
+ require 'redcloth'
6
+ require 'syntax/convertors/html'
7
+ require 'erb'
8
+
9
+ download = "http://rubyforge.org/projects/#{$hoe.rubyforge_name}"
10
+ version = $hoe.version
11
+
12
+ def rubyforge_project_id
13
+ RubyForge.new.configure.autoconfig["group_ids"][$hoe.rubyforge_name]
14
+ end
15
+
16
+ class Fixnum
17
+ def ordinal
18
+ # teens
19
+ return 'th' if (10..19).include?(self % 100)
20
+ # others
21
+ case self % 10
22
+ when 1: return 'st'
23
+ when 2: return 'nd'
24
+ when 3: return 'rd'
25
+ else return 'th'
26
+ end
27
+ end
28
+ end
29
+
30
+ class Time
31
+ def pretty
32
+ return "#{mday}#{mday.ordinal} #{strftime('%B')} #{year}"
33
+ end
34
+ end
35
+
36
+ def convert_syntax(syntax, source)
37
+ return Syntax::Convertors::HTML.for_syntax(syntax).convert(source).gsub(%r!^<pre>|</pre>$!,'')
38
+ end
39
+
40
+ if ARGV.length >= 1
41
+ src, template = ARGV
42
+ template ||= File.join(File.dirname(__FILE__), '/../website/template.html.erb')
43
+ else
44
+ puts("Usage: #{File.split($0).last} source.txt [template.html.erb] > output.html")
45
+ exit!
46
+ end
47
+
48
+ template = ERB.new(File.open(template).read)
49
+
50
+ title = nil
51
+ body = nil
52
+ File.open(src) do |fsrc|
53
+ title_text = fsrc.readline
54
+ body_text_template = fsrc.read
55
+ body_text = ERB.new(body_text_template).result(binding)
56
+ syntax_items = []
57
+ body_text.gsub!(%r!<(pre|code)[^>]*?syntax=['"]([^'"]+)[^>]*>(.*?)</\1>!m){
58
+ ident = syntax_items.length
59
+ element, syntax, source = $1, $2, $3
60
+ syntax_items << "<#{element} class='syntax'>#{convert_syntax(syntax, source)}</#{element}>"
61
+ "syntax-temp-#{ident}"
62
+ }
63
+ title = RedCloth.new(title_text).to_html.gsub(%r!<.*?>!,'').strip
64
+ body = RedCloth.new(body_text).to_html
65
+ body.gsub!(%r!(?:<pre><code>)?syntax-temp-(\d+)(?:</code></pre>)?!){ syntax_items[$1.to_i] }
66
+ end
67
+ stat = File.stat(src)
68
+ created = stat.ctime
69
+ modified = stat.mtime
70
+
71
+ $stdout << template.result(binding)
@@ -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,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: easy_imap
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Paul Stadig
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-12-17 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: tmail
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.2.3.1
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: newgem
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.1.0
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: hoe
37
+ type: :development
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 1.8.0
44
+ version:
45
+ description: A simple interface to proccessing e-mail messages using IMAP, including handling multipart messages and attachments.
46
+ email:
47
+ - paul@stadig.name
48
+ executables: []
49
+
50
+ extensions: []
51
+
52
+ extra_rdoc_files:
53
+ - History.txt
54
+ - Manifest.txt
55
+ - PostInstall.txt
56
+ - README.rdoc
57
+ files:
58
+ - History.txt
59
+ - Manifest.txt
60
+ - PostInstall.txt
61
+ - README.rdoc
62
+ - Rakefile
63
+ - lib/easy_imap.rb
64
+ - lib/easy_imap/attachment.rb
65
+ - lib/easy_imap/folder.rb
66
+ - lib/easy_imap/message.rb
67
+ - lib/easy_imap/server.rb
68
+ - script/console
69
+ - script/destroy
70
+ - script/generate
71
+ - script/txt2html
72
+ - spec/easy_imap_spec.rb
73
+ - spec/spec.opts
74
+ - spec/spec_helper.rb
75
+ - tasks/rspec.rake
76
+ has_rdoc: true
77
+ homepage: http://easy-imap.rubyforge.org/
78
+ post_install_message: PostInstall.txt
79
+ rdoc_options:
80
+ - --main
81
+ - README.rdoc
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: "0"
89
+ version:
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: "0"
95
+ version:
96
+ requirements: []
97
+
98
+ rubyforge_project: easy_imap
99
+ rubygems_version: 1.3.1
100
+ signing_key:
101
+ specification_version: 2
102
+ summary: A simple interface to proccessing e-mail messages using IMAP, including handling multipart messages and attachments.
103
+ test_files: []
104
+