mailchekka 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f3bfb24c5f80f0ad1aeea6848fa55abd17b3c839
4
+ data.tar.gz: d3fa1572284a518e23306f2d09e0881ab8f2ed93
5
+ SHA512:
6
+ metadata.gz: b62d8d5a74906825693172048cc6172c9cd10614be0604a21fb43483f807d0b85884caace488f2f18c9d4d39e7e03571ba9b3a31d66d06e947d19cab9c6bba8e
7
+ data.tar.gz: ac8325fc634e3b2c9cc871df1f9822824091f69129313d78e3c82a72294590ae33114647a7494538449b3f793f07b2c39039eb8a8b85f5ebb89a72c3b9b79e20
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+ require 'mailchekka'
3
+
4
+ begin
5
+ Mailchecker.check
6
+ rescue => error
7
+ puts error.message
8
+ end
9
+
@@ -0,0 +1,18 @@
1
+ %w(mailchekka/parser mailchekka/mailbox
2
+ mailchekka/checker).each { |req| require_relative req }
3
+
4
+ %w(net/imap net/pop optparse).each { |req| require req }
5
+
6
+ trap('INT') do
7
+ puts 'Exiting...'
8
+ exit
9
+ end
10
+
11
+ class Mailchecker
12
+ def self.check
13
+ @info = Parser::Parse(ARGV)
14
+ Checker.new(Mailbox.new(@info), @info.interval).run
15
+ end
16
+ end
17
+
18
+
@@ -0,0 +1,28 @@
1
+ class Checker
2
+ def initialize(mailbox, interval)
3
+ @interval = interval
4
+ @mailbox = mailbox
5
+ @unread = nil
6
+ end
7
+
8
+ def run
9
+ rotate do
10
+ @mailbox.prepare
11
+ present(@mailbox.getnum)
12
+ end
13
+ end
14
+
15
+ private
16
+
17
+ def present(number)
18
+ puts "You have got #{number} unread messages." if @unread != number
19
+ @unread = number
20
+ end
21
+
22
+ def rotate
23
+ loop do
24
+ yield
25
+ sleep @interval
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,48 @@
1
+ # container for mail methods
2
+ class Mailbox
3
+ def initialize(info)
4
+ @info = info
5
+ @box = nil
6
+ end
7
+
8
+ def prepare
9
+ host = @info.host
10
+ port = @info.port
11
+ ssl = @info.ssl
12
+ user = [@info.login, @info.password]
13
+ if @info.pop
14
+ @box = Net::POP3.new(host, port)
15
+ @box.use_ssl if ssl
16
+ @box.start(*user)
17
+ else
18
+ @box = Net::IMAP.new(host, port, ssl: ssl)
19
+ @box.login(*user)
20
+ end
21
+ end
22
+
23
+ def getnum
24
+ if @info.pop
25
+ num_pop
26
+ else
27
+ num_imap
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ def num_pop
34
+ @box.mails.length
35
+ end
36
+
37
+ def num_imap
38
+ @box.examine('INBOX')
39
+ number = @box.search('UNSEEN').length
40
+ imap_end
41
+ number
42
+ end
43
+
44
+ def imap_end
45
+ @box.logout
46
+ @box.disconnect
47
+ end
48
+ end
@@ -0,0 +1,66 @@
1
+ require 'optparse'
2
+
3
+ module Parser
4
+ class Options
5
+ attr_accessor :host, :port, :login, :password, :ssl, :interval, :pop
6
+
7
+ def initialize
8
+ @host = '127.0.0.1'
9
+ @ssl = false
10
+ @interval = 60
11
+ @pop = false
12
+ end
13
+ end
14
+
15
+ attr_writer :options
16
+
17
+ def self.Parse(args)
18
+ @options = Options.new
19
+ args << '-h' if args.empty?
20
+ opt_parser.parse!(args)
21
+ @options
22
+ end
23
+
24
+ def self.opt_parser
25
+ @parser ||= OptionParser.new do |parser|
26
+ parser.banner = 'Usage: mailchecker [options]'
27
+ parser.separator ''
28
+ parser.on('-t', '--host HOST', String, '#Mail service host.') do |host|
29
+ @options.host = host
30
+ end
31
+
32
+ parser.on('-p', '--port PORT', Integer,
33
+ '#Port on which service runs. Default: 143.') do |port|
34
+ @options.port = port
35
+ end
36
+
37
+ parser.on('-l', '--login LOGIN', String,
38
+ '#Login needed to execute the script.') do |login|
39
+ @options.login = login
40
+ end
41
+
42
+ parser.on('-x', '--password PASSWORD', String,
43
+ '#Password needed to execute script.') do |pass|
44
+ @options.password = pass
45
+ end
46
+
47
+ parser.on('-s', '--secure', TrueClass, '#Enable ssl.') do
48
+ @options.ssl = true
49
+ end
50
+
51
+ parser.on('-i', '--interval INTERVAL', Integer, "#Interval at which program checks new mail. Default: 30 seconds.") do |interv|
52
+ @options.interval = interv unless interv < 30
53
+ end
54
+
55
+ parser.on('-o', '--pop', '#Enables POP3. Default is IMAP.') do
56
+ @options.pop = true
57
+ end
58
+
59
+ parser.on('-h', '--help', 'Prints this help.') do
60
+ puts parser
61
+ exit
62
+ end
63
+ end
64
+ end
65
+ end
66
+
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mailchekka
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - drunkdrop
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-10-14 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email: drunkdrop@users.noreply.github.com
15
+ executables:
16
+ - mailchekka
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - bin/mailchekka
21
+ - lib/mailchekka.rb
22
+ - lib/mailchekka/checker.rb
23
+ - lib/mailchekka/mailbox.rb
24
+ - lib/mailchekka/parser.rb
25
+ homepage:
26
+ licenses:
27
+ - MIT
28
+ metadata: {}
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubyforge_project:
45
+ rubygems_version: 2.5.1
46
+ signing_key:
47
+ specification_version: 4
48
+ summary: Simple unread mail checker gem.
49
+ test_files: []