mailchain_connector_imap 0.1.4

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,189 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'tty-prompt'
4
+ require 'optparse'
5
+
6
+ require_relative 'connection/imap'
7
+ require_relative 'connection/mailchain'
8
+ require_relative 'connection_configuration/imap'
9
+ require_relative 'connection_configuration/mailchain'
10
+
11
+ module MailchainConnectorImap
12
+ class Error < StandardError; end
13
+
14
+ class Connector
15
+ STORE_PATH = "#{ENV['HOME']}/.mailchain_connector/imap/"
16
+ CONFIG_FILE = File.join(STORE_PATH, 'config.json')
17
+ LOG_FILE = File.join(STORE_PATH, 'mailchain_connector_imap.log')
18
+ attr_reader :imap_conn
19
+ attr_reader :mailchain_conn
20
+ attr_reader :config
21
+
22
+ def initialize
23
+ parse_config_file
24
+ @imap_conn = ConnectionImap.new(@config, CONFIG_FILE)
25
+ @mailchain_conn = ConnectionMailchain.new(@config, CONFIG_FILE)
26
+ @config_json = {}
27
+ end
28
+
29
+ def start
30
+ # Run OptionsParser to interpret user input
31
+ run_options_parse
32
+ end
33
+
34
+ # Handle missing config file error
35
+ def missing_config_file
36
+ puts "Invalid or missing config.\n" \
37
+ 'Run `mailchain_connector_imap --configure`'
38
+ end
39
+
40
+ # Run the script and parse input arguments
41
+ def run_options_parse
42
+ OptionParser.new do |opts|
43
+ ARGV << '-r' if ARGV.empty?
44
+
45
+ opts.banner = 'Usage: mailchain_connector_imap [options]'
46
+
47
+ opts.on('-r', '--run', 'Run and sync messages') do
48
+ unless valid_config
49
+ missing_config_file
50
+ exit
51
+ end
52
+ sync_messages
53
+ end
54
+
55
+ opts.on('-c', '--configure', 'Configure connector settings') do
56
+ op_configure
57
+ exit
58
+ end
59
+
60
+ opts.on('-t', '--test-connection', 'Test connection to IMAP server and Mailchain API') do
61
+ op_test_connection
62
+ exit
63
+ end
64
+
65
+ opts.on('-p', '--print-config', 'Print connector settings to screen') do
66
+ op_print_configuration
67
+ exit
68
+ end
69
+
70
+ opts.on_tail('-h', '--help', 'Show this message') do
71
+ puts opts
72
+ exit
73
+ end
74
+ end.parse!
75
+ end
76
+
77
+ # Runs the configuration wizards and tests connections
78
+ def op_configure
79
+ @imap_conn.configure_and_connect
80
+ @mailchain_conn.configure_and_connect
81
+ exit
82
+ end
83
+
84
+ # Run connection tests
85
+ def op_test_connection
86
+ begin
87
+ @imap_conn.test_connection
88
+ rescue StandardError => e
89
+ puts "IMAP error: #{e}"
90
+ end
91
+ begin
92
+ @mailchain_conn.test_connection
93
+ rescue StandardError => e
94
+ puts "API error: #{e}"
95
+ end
96
+ end
97
+
98
+ # Outputs the configuration to screen
99
+ def op_print_configuration
100
+ # TODO: fix this
101
+ ConnectionConfigurationImap.new(@config).print_settings
102
+ puts "\n"
103
+ # TODO: fix this
104
+ ConnectionConfigurationMailchain.new(@config).print_settings
105
+ end
106
+
107
+ # Parse the config file as JSON
108
+ def parse_config_file
109
+ check_or_create_config_file
110
+ config_json = File.read(CONFIG_FILE)
111
+ @config = JSON.parse(config_json)
112
+ true
113
+ rescue StandardError => e
114
+ puts "Error parsing configuration: #{e}"
115
+ missing_config_file
116
+ exit
117
+ end
118
+
119
+ # Check for an existing config file, otherwise create a new one with minimum requirements to be parsed.
120
+ def check_or_create_config_file
121
+ (FileUtils.mkdir_p(STORE_PATH) unless File.exist?(CONFIG_FILE))
122
+ File.write(CONFIG_FILE, '{"imap": {}, "mailchain": {}}') unless File.exist?(CONFIG_FILE)
123
+ end
124
+
125
+ # sync_messages calls ConnectionMailchain.messages_by_network, before setting a timer to run again.
126
+ # Minimum timer interval is 60 seconds.
127
+ # It logs at the beginning and end of each polling interval.
128
+ def sync_messages
129
+ if @imap_conn.connect
130
+ puts 'Connected to IMAP'
131
+ if @mailchain_conn.test_connection(true)
132
+ puts 'Connected to Mailchain client'
133
+ loop do
134
+ if @imap_conn.connect && @mailchain_conn.test_connection(true)
135
+ interval = @config['mailchain']['interval'].to_i > 60 ? @config['mailchain']['interval'].to_i : 60
136
+ log_to_file('Checking messages')
137
+ @mailchain_conn.addresses_by_network.each do |abn|
138
+ protocol = abn['protocol']
139
+ network = abn['network']
140
+ # TODO: - Simplify this:
141
+ addr_with_messages = @mailchain_conn.messages_by_network(abn)
142
+ addr_with_messages.each do |addr_msg|
143
+ addr = addr_msg[0]
144
+ msg = addr_msg[1]
145
+ converted_messages = @mailchain_conn.convert_messages(msg)
146
+ converted_messages.each do |cm|
147
+ @imap_conn.append_message(protocol, network, addr, cm['message'], cm['message_id'], nil, cm['message_date'])
148
+ end
149
+ end
150
+ end
151
+ end
152
+
153
+ log_to_file('Done')
154
+ sleep interval
155
+ end
156
+ end
157
+ end
158
+ rescue StandardError => e
159
+ log_to_file("Error: #{e}")
160
+ puts "Error: #{e}"
161
+ end
162
+
163
+ # Checks values are present for config options
164
+ # Returns true if valid; false if invalid
165
+ def valid_config
166
+ [
167
+ @config['imap']['server'],
168
+ @config['imap']['username'],
169
+ @config['imap']['port'],
170
+ @config['imap']['ssl'],
171
+ @config['mailchain']['hostname'],
172
+ @config['mailchain']['ssl'],
173
+ @config['mailchain']['port'],
174
+ @config['mailchain']['folders'],
175
+ @config['mailchain']['mainnet_to_inbox'],
176
+ @config['mailchain']['interval']
177
+ ].none? { |e| e.to_s.empty? }
178
+ end
179
+
180
+ # Output message to the log file, adding the date and time
181
+ # `message` (String): the message text
182
+ def log_to_file(message)
183
+ open(LOG_FILE, 'a') do |f|
184
+ f << "\n"
185
+ f << "#{Time.now} #{message}"
186
+ end
187
+ end
188
+ end
189
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/mailchain_connector_imap/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'mailchain_connector_imap'
7
+ spec.version = MailchainConnectorImap::VERSION
8
+ spec.authors = ['Tim B']
9
+ spec.email = ['team@mailchain.xyz']
10
+
11
+ spec.summary = 'An IMAP connector for the Mailchain API'
12
+ spec.description = 'Send your Mailchain messages to your email inbox with the IMAP connector for the Mailchain API'
13
+ spec.homepage = 'https://mailchain.xyz/mailchain-connectors/imap'
14
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
15
+ spec.license = 'Apache2.0'
16
+
17
+ spec.metadata['homepage_uri'] = spec.homepage
18
+ spec.metadata['source_code_uri'] = 'https://github.com/mailchain/mailchain-connector-imap'
19
+ spec.metadata['changelog_uri'] = 'https://github.com/mailchain/mailchain-connector-imap/releases'
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ end
26
+ spec.bindir = 'exe'
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ['lib']
29
+
30
+ spec.add_development_dependency 'bundle', '~> 2.0'
31
+ spec.add_development_dependency 'pry', '~> 0.13'
32
+ spec.add_development_dependency 'rake', '~> 12.0'
33
+ spec.add_development_dependency 'rspec', '~> 3.0'
34
+
35
+ spec.add_runtime_dependency 'httparty', '~> 0.18', '>= 0.18.0'
36
+ spec.add_runtime_dependency 'mail', '~> 2.7', '>= 2.7.1'
37
+ spec.add_runtime_dependency 'tty-prompt', '~> 0.21', '>= 0.21.0'
38
+ end
metadata ADDED
@@ -0,0 +1,184 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mailchain_connector_imap
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.4
5
+ platform: ruby
6
+ authors:
7
+ - Tim B
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-06-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundle
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.13'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.13'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '12.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '12.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: httparty
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.18'
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: 0.18.0
79
+ type: :runtime
80
+ prerelease: false
81
+ version_requirements: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - "~>"
84
+ - !ruby/object:Gem::Version
85
+ version: '0.18'
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: 0.18.0
89
+ - !ruby/object:Gem::Dependency
90
+ name: mail
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: '2.7'
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: 2.7.1
99
+ type: :runtime
100
+ prerelease: false
101
+ version_requirements: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - "~>"
104
+ - !ruby/object:Gem::Version
105
+ version: '2.7'
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: 2.7.1
109
+ - !ruby/object:Gem::Dependency
110
+ name: tty-prompt
111
+ requirement: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - "~>"
114
+ - !ruby/object:Gem::Version
115
+ version: '0.21'
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: 0.21.0
119
+ type: :runtime
120
+ prerelease: false
121
+ version_requirements: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - "~>"
124
+ - !ruby/object:Gem::Version
125
+ version: '0.21'
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: 0.21.0
129
+ description: Send your Mailchain messages to your email inbox with the IMAP connector
130
+ for the Mailchain API
131
+ email:
132
+ - team@mailchain.xyz
133
+ executables:
134
+ - mailchain_connector_imap
135
+ extensions: []
136
+ extra_rdoc_files: []
137
+ files:
138
+ - ".gitignore"
139
+ - ".rspec"
140
+ - ".travis.yml"
141
+ - CODE_OF_CONDUCT.md
142
+ - Gemfile
143
+ - Gemfile.lock
144
+ - LICENSE
145
+ - README.md
146
+ - Rakefile
147
+ - bin/console
148
+ - bin/setup
149
+ - exe/mailchain_connector_imap
150
+ - lib/api/mailchain.rb
151
+ - lib/connection/imap.rb
152
+ - lib/connection/mailchain.rb
153
+ - lib/connection_configuration/imap.rb
154
+ - lib/connection_configuration/mailchain.rb
155
+ - lib/mailchain_connector_imap.rb
156
+ - lib/mailchain_connector_imap/version.rb
157
+ - mailchain_connector_imap.gemspec
158
+ homepage: https://mailchain.xyz/mailchain-connectors/imap
159
+ licenses:
160
+ - Apache2.0
161
+ metadata:
162
+ homepage_uri: https://mailchain.xyz/mailchain-connectors/imap
163
+ source_code_uri: https://github.com/mailchain/mailchain-connector-imap
164
+ changelog_uri: https://github.com/mailchain/mailchain-connector-imap/releases
165
+ post_install_message:
166
+ rdoc_options: []
167
+ require_paths:
168
+ - lib
169
+ required_ruby_version: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: 2.3.0
174
+ required_rubygems_version: !ruby/object:Gem::Requirement
175
+ requirements:
176
+ - - ">="
177
+ - !ruby/object:Gem::Version
178
+ version: '0'
179
+ requirements: []
180
+ rubygems_version: 3.1.2
181
+ signing_key:
182
+ specification_version: 4
183
+ summary: An IMAP connector for the Mailchain API
184
+ test_files: []