biff 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 10baa1580d984257513cc494648360b858da4d2c
4
+ data.tar.gz: 80e17475020465078acc4186394f08d401b2cabe
5
+ SHA512:
6
+ metadata.gz: 461e2fd31358b45231fcccd512338a43b63b53e60a6a2e117202221a433870c3f251c0b11fddc68e1140e9c07b8b41400257d68b37398db3547d6a4eb3de4294
7
+ data.tar.gz: 205fa341d6cd04c01664e8d78cbae0333ce551a55951c320a5d92656b8d8e9f06723c3f105052bacb71e4d8d224a78110cf96eb34061cc10cd816d438ff16e91
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ /*.gem
12
+ # rspec failure tracking
13
+ .rspec_status
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in biff.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,58 @@
1
+ # Biff
2
+
3
+ Ruby Imap IDLE based biff utility.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'biff'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install biff
20
+
21
+ ## Usage
22
+
23
+ The gem provides two command-line scripts, `biffer` (to avoid conflict w/ system `biff` command) and `biff.5m.rb`. `biffer` uses `net/imap#listen` to wait for INBOX changes and print "{server name}:{unseen}/{all}".
24
+
25
+ `biff.1m.rb` is a [BitBar](https://getbitbar.com) script, which will run every minute to update the menubar entry.
26
+
27
+ Both scripts use (by default) the configuration in ~/.biff.yaml, which should be in the following format, multiple top-level keys (servers) are allowd. Note that one of either `password` or `passcmd` is required.
28
+
29
+ ```yaml
30
+ Name:
31
+ host: required.host.address
32
+ user: required_user_name
33
+ password: optional_password
34
+ passcmd: shell_command_if_password_not_specified
35
+ run: optional_command_to_run_after_inbox_changes
36
+ cmd: optional_email_command_for_bitbar_menu_hot_link
37
+ ```
38
+
39
+ Note that if you are using, e.g., `rbenv` to manage your ruby versions and gems, you can't use `biff.5m.rb` directly in your plugin directory. A shell script like the following will work:
40
+
41
+ ``` zsh
42
+ #!/bin/zsh --login
43
+
44
+ export RBENV_ROOT=/usr/local/var/rbenv
45
+ export RBENV_GEMSETS=global
46
+ export RBENV_VERSION=2.4.1
47
+
48
+ exec $RBENV_ROOT/shims/biff.5m.rb
49
+ ```
50
+ ## Development
51
+
52
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
53
+
54
+ To install this gem onto your local machine, run `bundle exec rake install`.
55
+
56
+ ## Contributing
57
+
58
+ Bug reports and pull requests are welcome on GitHub at https://github.com/cybercode/Biff.
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+ require 'rake/version_task'
4
+
5
+ Rake::VersionTask.new
6
+
7
+ RSpec::Core::RakeTask.new(:spec)
8
+
9
+ task default: :spec
10
+ task test: :spec
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/biff.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = 'biff'
6
+ spec.version = File.read('VERSION').chomp # file managed by version gem...
7
+
8
+ spec.authors = ['Rick Frankel']
9
+ spec.email = ['biff@cybercode.nyc']
10
+
11
+ spec.summary = 'Imap IDLE based biff client'
12
+ # spec.description = %q{TODO: Write a longer description or delete this line.}
13
+ spec.homepage = 'https://github.com/cybercode/biff'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
16
+ f.match(%r{^(test|spec|features)/})
17
+ end
18
+ spec.test_files = `git ls-files -z -- spec/*`.split("\x0")
19
+ spec.bindir = 'bin'
20
+ spec.executables = %w[biffer biff.5m.rb]
21
+ spec.require_paths = ['lib']
22
+
23
+ spec.add_dependency 'version', '~> 1.1'
24
+ spec.add_dependency 'rfc2047', '>= 0.3'
25
+ spec.add_development_dependency 'bundler', '~> 1.15'
26
+ spec.add_development_dependency 'rake', '~> 10.0'
27
+ spec.add_development_dependency 'rspec', '~> 3.0'
28
+ spec.add_development_dependency 'pry', '~> 0.10'
29
+ end
data/bin/biff.5m.rb ADDED
@@ -0,0 +1,47 @@
1
+ #!/usr/bin/env ruby
2
+ # <bitbar.title>IMAP Biff</bitbar.title>
3
+ # <bitbar.version>v0.1</bitbar.version>
4
+ # <bitbar.author>Rick Frankel</bitbar.author>
5
+ # <bitbar.author.github>cybercode</bitbar.author.github>
6
+ # <bitbar.desc>
7
+ # Get Inbox counts (unseen/all) for multiple mailboxes.
8
+ # Expects ~/.biff.yaml config file
9
+ # </bitbar.desc>
10
+ # <bitbar.image></bitbar.image>
11
+ # <bitbar.dependencies>ruby, biff rubygem (gem install biff)</bitbar.dependencies>
12
+ # <bitbar.abouturl></bitbar.abouturl>
13
+
14
+ require 'biff/cli'
15
+
16
+ def report(cli)
17
+ statii = cli.status
18
+ all = statii.sum(&:all)
19
+ unseen = statii.sum(&:unseen)
20
+ icon = ':envelope:'
21
+ attrs = ''
22
+ if unseen.positive?
23
+ icon = ':mailbox:'
24
+ attrs = '|color=green'
25
+ end
26
+ puts "#{icon}#{unseen}/#{all}#{attrs}"
27
+ puts '---'
28
+ statii.each do |s|
29
+ cmd = cli.servers[s.name].cmd
30
+ attrs = '|'
31
+ attrs += 'color=green' if s.unseen.positive?
32
+ attrs += " bash=#{cmd} terminal=false" if cmd
33
+ puts "#{s.name} #{s.unseen}/#{s.all}#{attrs}"
34
+ if s.unseen.positive?
35
+ cli.servers[s.name].unseen_headers.map do |h|
36
+ puts h
37
+ end
38
+ end
39
+ puts '---'
40
+ end
41
+ puts 'Check Now…|refresh=true'
42
+
43
+ cli.servers.values.each(&:run)
44
+ end
45
+
46
+ cli = Biff::CLI.new
47
+ report(cli)
data/bin/biffer ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ require 'biff/cli'
3
+
4
+ cli = Biff::CLI.new
5
+
6
+ cli.update
7
+ threads = cli.listen
8
+
9
+ %w[TERM INT QUIT].each do |sig|
10
+ Signal.trap(sig) do
11
+ threads.each(&:exit)
12
+ end
13
+ end
14
+
15
+ threads.each(&:join)
data/bin/console ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'biff'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ require 'pry'
11
+ Pry.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/lib/biff/cli.rb ADDED
@@ -0,0 +1,59 @@
1
+ require 'biff'
2
+ require 'biff/options'
3
+
4
+ class Biff::CLI
5
+ attr_reader :servers
6
+
7
+ def initialize
8
+ @servers = {}
9
+
10
+ options = Biff::Options.new
11
+ options.parse!
12
+
13
+ options.config.each do |name, config|
14
+ config['debug'] ||= options.debug
15
+ config['verbose'] ||= options.verbose
16
+ config['name'] = name
17
+ $stderr.puts("INFO #{name}") if options.verbose
18
+
19
+ b = @servers[name] = Biff.new(config)
20
+ b.open
21
+ b.count
22
+ end
23
+ end
24
+
25
+ def disconnect
26
+ map_servers(&:disconnect)
27
+ end
28
+
29
+ def listen(&block)
30
+ threads = []
31
+ map_servers do |obj|
32
+ threads << Thread.new do
33
+ obj.listen(&block)
34
+ end
35
+ end
36
+
37
+ threads
38
+ end
39
+
40
+ def update
41
+ count
42
+ map_servers(&:notify)
43
+ map_servers(&:run)
44
+ end
45
+
46
+ def count
47
+ map_servers(&:count)
48
+ end
49
+
50
+ def status
51
+ map_servers(&:status)
52
+ end
53
+
54
+ def map_servers
55
+ @servers.values.map do |obj|
56
+ yield obj
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,58 @@
1
+ require 'optparse'
2
+ require 'yaml'
3
+
4
+ class Biff::Options
5
+ ARGS = {
6
+ verbose: 'Be noisy',
7
+ debug: 'Debug IMAP connection',
8
+ file: ['Config file', 'FILE', '~/.biff.yaml']
9
+ }.freeze
10
+
11
+ attr_accessor(*ARGS.keys)
12
+
13
+ def parse!
14
+ parser = OptionParser.new do |o|
15
+ o.banner = 'Usage: biff [options]'
16
+
17
+ ARGS.each do |k, v|
18
+ default = false
19
+
20
+ if v.is_a?(Array)
21
+ default = v[2]
22
+ v = ["#{v[0]} (default #{v[2]})", v[1]]
23
+ end
24
+
25
+ set(k, default)
26
+ on(o, k, *v)
27
+ end
28
+
29
+ on(o, :help, 'Print help') do
30
+ puts parser
31
+ exit
32
+ end
33
+ end
34
+
35
+ parser.parse!(ARGV)
36
+ end
37
+
38
+ def config
39
+ @config ||= YAML.parse(File.open(File.expand_path(file))).transform
40
+ end
41
+
42
+ private
43
+ def set(arg, v)
44
+ send("#{arg}=", v)
45
+ end
46
+
47
+ def on(o, arg, doc, name=nil)
48
+ long = name ? "#{arg} #{name}" : arg
49
+
50
+ o.on("-#{arg[0]}", "--#{long}", doc) do |v|
51
+ if block_given?
52
+ yield v
53
+ else
54
+ set(arg, v)
55
+ end
56
+ end
57
+ end
58
+ end
data/lib/biff.rb ADDED
@@ -0,0 +1,129 @@
1
+ require 'net/imap'
2
+ require 'rfc2047'
3
+ require 'version'
4
+
5
+ Status = Struct.new(:name, :unseen, :all, :cmd)
6
+
7
+ class Biff
8
+ is_versioned
9
+
10
+ def initialize(config)
11
+ @config = config
12
+
13
+ @imap = Net::IMAP.new(
14
+ @config['host'],
15
+ @config['port'] || 993,
16
+ @config['tls'] || true
17
+ )
18
+ Net::IMAP.debug = @config['debug']
19
+
20
+ return if @imap.capability.include?('IDLE')
21
+
22
+ $stderr.puts "ERR #{config['name']} #{@config['host']} unsupported"
23
+ exit 1
24
+ end
25
+
26
+ def open
27
+ pass = @config['password'] || `#{@config['passcmd']}`.chomp
28
+ log('login', @config['user'])
29
+
30
+ @imap.login(@config['user'], pass)
31
+ @imap.examine('INBOX')
32
+ end
33
+
34
+ def status
35
+ Status.new(@config['name'], @unseen.count, @all.count)
36
+ end
37
+
38
+ def listen
39
+ @idling = true
40
+
41
+ while @idling
42
+ begin
43
+ idle # wait
44
+
45
+ count
46
+ if block_given?
47
+ yield status
48
+ else
49
+ notify
50
+ end
51
+
52
+ run
53
+ rescue ThreadError
54
+ # thread killed (exited app?)
55
+ @imap.disconnect
56
+ end
57
+ end
58
+
59
+ @idling = false
60
+ end
61
+
62
+ def run
63
+ return unless cmd = @config['run'] # rubocop:disable Lint/AssignmentInCondition
64
+
65
+ log('running', cmd)
66
+ system(cmd)
67
+ end
68
+
69
+ def count
70
+ @all = @imap.search('ALL')
71
+ @unseen = @imap.search('UNSEEN')
72
+
73
+ [@unseen, @all]
74
+ end
75
+
76
+ def notify
77
+ i = status
78
+ puts "#{i.name}:#{i.unseen}/#{i.all}"
79
+ end
80
+
81
+ def cmd
82
+ @config['cmd']
83
+ end
84
+
85
+ def disconnect
86
+ if @idling
87
+ @idling = false
88
+ @imap.send(:put_string, "DONE\r\n")
89
+ end
90
+
91
+ @imap.logout
92
+ @imap.disconnect unless @imap.disconnected?
93
+ end
94
+
95
+ NAME_MAX = 15
96
+ SUBJ_MAX = 40
97
+
98
+ def unseen_headers
99
+ return unless @unseen
100
+ @imap.fetch(@unseen, 'ENVELOPE').map { |e| e.attr['ENVELOPE'] }.map do |e|
101
+ from = e.from[0]
102
+ name = Rfc2047.decode(from.name || from.mailbox)
103
+ subject = Rfc2047.decode(e.subject)
104
+ nl = name.length > NAME_MAX ? NAME_MAX - 1 : NAME_MAX
105
+ sl = subject.length > SUBJ_MAX ? SUBJ_MAX - 1 : SUBJ_MAX
106
+
107
+ # rubocop doesn't understand '*' width/precision
108
+ # rubocop:disable Lint/FormatParameterMismatch
109
+ sprintf(
110
+ '%*.*s%s: %-*.*s%s',
111
+ nl, nl, name, name.length > NAME_MAX ? '…' : '',
112
+ sl, sl, subject, subject.length > SUBJ_MAX ? '…' : '',
113
+ )
114
+ end
115
+ end
116
+
117
+ private
118
+ def log(*args)
119
+ return unless @config['verbose']
120
+ $stderr.puts(['INFO', *args].join(' '))
121
+ end
122
+
123
+ def idle
124
+ @imap.idle(@config['timeout'] || 29 * 60) do |resp|
125
+ @imap.idle_done if resp.is_a?(Net::IMAP::UntaggedResponse) &&
126
+ resp.name != 'OK'
127
+ end
128
+ end
129
+ end
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: biff
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Rick Frankel
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-07-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: version
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rfc2047
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0.3'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.15'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.15'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.10'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.10'
97
+ description:
98
+ email:
99
+ - biff@cybercode.nyc
100
+ executables:
101
+ - biffer
102
+ - biff.5m.rb
103
+ extensions: []
104
+ extra_rdoc_files: []
105
+ files:
106
+ - ".gitignore"
107
+ - Gemfile
108
+ - README.md
109
+ - Rakefile
110
+ - VERSION
111
+ - biff.gemspec
112
+ - bin/biff.5m.rb
113
+ - bin/biffer
114
+ - bin/console
115
+ - bin/setup
116
+ - lib/biff.rb
117
+ - lib/biff/cli.rb
118
+ - lib/biff/options.rb
119
+ homepage: https://github.com/cybercode/biff
120
+ licenses: []
121
+ metadata: {}
122
+ post_install_message:
123
+ rdoc_options: []
124
+ require_paths:
125
+ - lib
126
+ required_ruby_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ required_rubygems_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ requirements: []
137
+ rubyforge_project:
138
+ rubygems_version: 2.6.12
139
+ signing_key:
140
+ specification_version: 4
141
+ summary: Imap IDLE based biff client
142
+ test_files: []