navi_email_sync 0.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: eb23348504b8e7954bdf7093b22ed565df930c368131827938098ab926aec925
4
+ data.tar.gz: 9232cb2fea5c1dbcb207ed450a827f8864f5094824d578ebee179a3eba94df32
5
+ SHA512:
6
+ metadata.gz: 61d2b62886f6c3873721f5abe46ba3ba0733cf72cb6c26a656cf4b2a4cec6f8bad3c4dda5a47c6624de0336d4405c4876e5190042362e8eb4f6e5a11c5e9c0c0
7
+ data.tar.gz: 247f7c139a5d5beaec3b7e1a070f0c8bf115ba6a8a8e74d760b0301a571cd16c7acfac0d09d5ef34437a9149e7e03811b4794a1a01ea77be02ac639281ee81fe
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .idea/
11
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.0
4
+ before_install: gem install bundler -v 1.11.2
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at surya@whitehatengineering.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in navi_email_sync.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Surya
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,46 @@
1
+ # NaviEmailSync
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/navi_client`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Generate Documentation
8
+ ```
9
+ $ rdoc
10
+ ```
11
+ Browse html file inside the ```doc``` folder
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ ```ruby
18
+ gem 'navi_email_sync'
19
+ ```
20
+
21
+ And then execute:
22
+
23
+ $ bundle
24
+
25
+ Or install it yourself as:
26
+
27
+ $ gem install navi_email_sync
28
+
29
+ ## Usage
30
+
31
+ TODO: Write usage instructions here
32
+
33
+ ## Development
34
+
35
+ 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.
36
+
37
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
38
+
39
+ ## Contributing
40
+
41
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/navi_client. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
42
+
43
+
44
+ ## License
45
+
46
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/.DS_Store ADDED
Binary file
data/bin/console ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ # You can add fixtures and/or initialization code here to make experimenting
5
+ # with your gem easier. You can also use a different console, if you like.
6
+
7
+ # (If you use this, don't forget to add pry to your Gemfile!)
8
+ # require "pry"
9
+ # Pry.start
10
+
11
+ require "irb"
12
+ IRB.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
@@ -0,0 +1,9 @@
1
+ # Just copy paste the following format in application.yml and configure accordingly
2
+
3
+ development:
4
+ api_url: http://localhost:3020/
5
+ parser_url: http://localhost:9090
6
+
7
+ production:
8
+ parser_url: http://34.214.134.104:9090
9
+
data/lib/client.rb ADDED
@@ -0,0 +1,259 @@
1
+ require "net/imap"
2
+ require "mail"
3
+ require "time"
4
+
5
+ require 'base64'
6
+ require 'fileutils'
7
+ require 'yaml'
8
+
9
+ require "logger"
10
+
11
+ require "httparty"
12
+ require "http_service/naviai"
13
+ require 'openssl'
14
+
15
+ ##
16
+ # This module provides the common functionality that will be needed for local and cloud module.
17
+ # @client_type defines if the gem is being used for local-lockbox or cloud-version.
18
+ #
19
+ module Client
20
+
21
+ attr_accessor :downloaded
22
+
23
+ attr_accessor :message_ids
24
+
25
+ attr_accessor :total_emails
26
+
27
+ @logglyTag
28
+
29
+ def logger
30
+ @logger
31
+ end
32
+
33
+ def errors
34
+ @errors
35
+ end
36
+
37
+ #
38
+ # imap_connection
39
+ #
40
+ # connect the app with imap server
41
+ #
42
+ def imap_connection(server, username, password, exitOnFail = true)
43
+ # connect to IMAP server
44
+ @imap = Net::IMAP.new server, ssl: true, certs: nil, verify: false
45
+
46
+ Net::IMAP.debug = @net_imap_debug
47
+
48
+ # http://ruby-doc.org/stdlib-2.1.5/libdoc/net/imap/rdoc/Net/IMAP.html#method-i-capability
49
+ capabilities = @imap.capability
50
+
51
+ @logger.debug("imap capabilities: #{capabilities.join(',')}") if @debug
52
+
53
+ if @client_type == 'local'
54
+ unless capabilities.include? "IDLE" || @debug
55
+ @logger.info "'IDLE' IMAP capability not available in server: #{server}"
56
+ @imap.disconnect
57
+ exit if exitOnFail
58
+ end
59
+ end
60
+
61
+ begin
62
+ # login
63
+ @imap.login username, password
64
+ @errors = nil
65
+ rescue Net::IMAP::NoResponseError => e
66
+ # mostly due to credentials error
67
+ @errors = {exception: e}
68
+ rescue Net::IMAP::BadResponseError => e
69
+ @errors = {exception: e}
70
+ rescue
71
+ @errors = {exception: e}
72
+ end
73
+
74
+ # return IMAP connection handler
75
+ @imap
76
+ end
77
+
78
+ #
79
+ # retrieve_emails
80
+ #
81
+ # retrieve any mail from a folder, followin specified serach condition
82
+ # for any mail retrieved call a specified block
83
+ #
84
+ def fetch_emails(&process_email_block)
85
+
86
+ @message_ids.each_with_index do |message_id, i|
87
+ begin
88
+ # fetch all the email contents
89
+ data = @imap.uid_fetch(message_id, "RFC822")
90
+ data.each do |d|
91
+ msg = d.attr['RFC822']
92
+ # instantiate a Mail object to avoid further IMAP parameters nightmares
93
+ mail = Mail.read_from_string msg
94
+
95
+ # call the block with mail object as param
96
+ process_email_block.call mail, i, i == @message_ids.length-1, message_id
97
+
98
+ # mark as read
99
+ if @mark_as_read
100
+ @imap.store(message_id, "+FLAGS", [:Seen])
101
+ end
102
+ end
103
+ rescue => e
104
+ unless logger.nil?
105
+ logger.info "Issue processing email for uuid ##{message_id}, #{e.message}"
106
+ end
107
+
108
+ logToLoggly({event:"EMAIL_SYNC_FAILED", env: @env, storage: @client_type, email: @current_user_email, uuid: message_id, error: e.message})
109
+ raise e
110
+ end
111
+ end
112
+ end
113
+
114
+
115
+ def init_messages label, search_condition = ['ALL']
116
+
117
+ # examine will read email and sets flags unread
118
+ # https://stackoverflow.com/questions/16516464/read-gmail-xoauth-mails-without-marking-it-read
119
+ @imap.examine label
120
+
121
+ @message_ids = @imap.uid_search(search_condition)
122
+
123
+ if search_condition.include?('ALL')
124
+ @all_emails = @message_ids if search_condition.include? 'ALL'
125
+ else
126
+ @all_emails = @all_emails | @message_ids
127
+ end
128
+
129
+ @total_emails = @message_ids.length if search_condition.include?('ALL')
130
+ downloaded_email = getMessageUUIds("#{@download_path}meta/")
131
+ downloaded_email = downloaded_email.collect{|i| i.to_i}
132
+
133
+ downloaded_email = @all_emails & downloaded_email
134
+
135
+ @downloaded = downloaded_email.length
136
+ # @downloaded = @downloaded - 2 if @downloaded > 1
137
+
138
+ @downloaded = [@downloaded, @total_emails].min
139
+
140
+ @message_ids = @message_ids - downloaded_email
141
+ @message_ids = @message_ids&.sort {|msg_a, msg_b| msg_b <=> msg_a}
142
+
143
+ if @debug
144
+ if @message_ids.empty?
145
+ @logger.info "No new emails found..."
146
+ elsif downloaded_email.any?
147
+ @logger.info "Found emails saved in your #{@client_type}. Downloading only #{@message_ids.count} new emails..."
148
+ else
149
+ @logger.info "Downloading #{@message_ids.count} emails."
150
+ end
151
+ end
152
+
153
+ end
154
+
155
+ ##
156
+ # Process each email downloaded from imap-server and creates meta file that will be sent over to the navi-ai service.
157
+ # Meta will content information like: ['from', 'to', 'cc', ..., 'body_url']. This information is then used by navi-ai to parse the body content.
158
+ #
159
+ def process_email(mail, uid, stamp = nil)
160
+ meta = Hash.new
161
+ custom_uid = (Time.now.to_f * 1000).to_s + "_" + mail.__id__.to_s
162
+
163
+ meta["from"] = mail[:from].to_s
164
+ meta["to"] = mail[:to].to_s
165
+ meta["cc"] = mail[:cc].to_s
166
+ meta["subject"] = mail.subject
167
+ meta["date"] = mail.date.to_s
168
+
169
+ if mail.multipart?
170
+ for i in 0...mail.parts.length
171
+ m = download(mail.parts[i], custom_uid)
172
+ meta.merge!(m) unless m.nil?
173
+ end
174
+ else
175
+ m = download(mail, custom_uid)
176
+ meta.merge!(m) unless m.nil?
177
+ end
178
+
179
+ if stamp.nil?
180
+ save(meta, "meta/#{uid.to_s + '_' + custom_uid}")
181
+ else
182
+ save(meta, "meta/#{uid.to_s + '_' + custom_uid}", stamp)
183
+ end
184
+
185
+ end
186
+
187
+ def encrypt(data)
188
+ Base64.encode64(data)
189
+ end
190
+
191
+ def depcrecated_encrypt(data)
192
+ cipher = OpenSSL::Cipher::AES.new(256, :CFB)
193
+ cipher.encrypt
194
+
195
+ yml_config = config
196
+
197
+ key_iv_exists = (yml_config['key'] && yml_config['iv']) ? (!yml_config['key'].empty? && !yml_config['iv'].empty? ) : false
198
+
199
+ if key_iv_exists
200
+ # this condition must be true for cloud version
201
+ cipher.key = Base64.decode64(File.read(yml_config['key']))
202
+ cipher.iv = Base64.decode64(File.read(yml_config['iv']))
203
+ else
204
+ cipher.key = key = cipher.random_key
205
+ cipher.iv = iv = cipher.random_iv
206
+
207
+ key_path, iv_path = save_aes_key_iv(key, iv)
208
+
209
+ yml_config['key'] = key_path
210
+ yml_config['iv'] = iv_path
211
+
212
+ update_config(yml_config)
213
+ end
214
+
215
+ encrypted = cipher.update(data)
216
+ Base64.encode64(encrypted)
217
+ end
218
+
219
+ def time_now
220
+ Time.now.utc.iso8601(3)
221
+ end
222
+
223
+ ##
224
+ # If the gem is being used for local-lockbox mode, after fetching all emails, the process will go idle mode. If user send the interrupt command, it will call shutdown to disconnect the connection with imap server
225
+ #
226
+ def shutdown(imap)
227
+ imap.idle_done
228
+ imap.logout unless imap.disconnected?
229
+ imap.disconnect
230
+
231
+ @logger.info "#{$0} has ended (crowd applauds)"
232
+ exit 0
233
+ end
234
+
235
+ def logToLoggly(messageBody)
236
+
237
+ if(@logglyTag)
238
+ begin
239
+ HTTParty.post("http://logs-01.loggly.com/bulk/0d67f93b-6568-4b00-9eca-97d0ea0bd5a1/tag/#{@logglyTag}/",
240
+ body: messageBody.to_json,
241
+ headers: { 'Content-Type' => 'application/json' } )
242
+ rescue
243
+ true
244
+ end
245
+
246
+ else
247
+ if @debug && !@logger.nil?
248
+ @logger.info "Logging to Loggly disabled"
249
+ @logger.info messageBody
250
+ end
251
+ end
252
+
253
+ end
254
+
255
+ def setupLoggly(tag)
256
+ @logglyTag = tag
257
+ end
258
+
259
+ end
@@ -0,0 +1,140 @@
1
+ require Gem::Specification.find_by_name("navi_email_sync").gem_dir+"/lib/client"
2
+ require 'concurrent'
3
+
4
+ module NaviEmailSync
5
+
6
+ ##
7
+ # This class represents the client for cloud version.
8
+ # In cloud version, all the content will be saved and also being used from the s3
9
+ #
10
+ class Cloud
11
+ include Concurrent::Async
12
+ include Client
13
+
14
+ attr_reader :id
15
+
16
+ def initialize(sso_web_url = ENV['api_url'], env = Rails.env)
17
+ super()
18
+
19
+ # client-id used to track the process
20
+ @id = SecureRandom.uuid
21
+
22
+ # flag to print Ruby library debug info (very detailed)
23
+ @net_imap_debug = false
24
+
25
+ # flag to mark email as read after gets downloaded.
26
+ @mark_as_read = false
27
+ # flag to turn on/off debug mode.
28
+ @debug = false
29
+
30
+ @logger = nil
31
+
32
+ # sso_web (authentication) config.
33
+ @sso_web_url = sso_web_url
34
+ # authentication token received from sso_web used to authenticate the request to database_api
35
+ @token = nil
36
+
37
+ # client_type
38
+ @client_type = "cloud"
39
+
40
+ @download_path = config[:s3_download_folder] + '/'
41
+
42
+ # set email_address of current_user for s3 folder name
43
+ @current_user_email = nil
44
+ @env = env
45
+ end
46
+
47
+ def override_logger(logger)
48
+ @logger = logger
49
+ end
50
+
51
+ #
52
+ # login
53
+ #
54
+ # login to the navi-cloud and get the authentication token
55
+ #
56
+ def login(session_token)
57
+ @token = session_token
58
+ end
59
+
60
+ def set_current_user_email(email)
61
+ @current_user_email = email
62
+ @download_path = config[:s3_download_folder] + '/' + email + "/"
63
+ end
64
+
65
+ ##
66
+ # send bulk request to navi-ai service with list of input files downloaded from imap server.
67
+ #
68
+ def send_request(in_filenames = [], is_last: false)
69
+ unless in_filenames.blank?
70
+ download_path = config['s3_download_folder'] + "/" + @current_user_email
71
+ filepath = download_path + "/inputs/" + (Time.now.to_f * 1000).to_s
72
+ filename = upload_to_s3(filepath, in_filenames.join("\n"))
73
+
74
+ HTTPService::NaviAI.start(filepath: filename, client_id: @id, client_type: @client_type, token: @token, email: @current_user_email, is_last: is_last)
75
+ end
76
+ end
77
+
78
+ ##
79
+ # Downloads the email content from imap-server and save it to the download_path
80
+ #
81
+ def download(message, custom_uid)
82
+ download_path = config[:s3_download_folder] + "/" + @current_user_email
83
+ if ['text/plain', 'text/html'].include? message.mime_type
84
+
85
+ h = Hash.new
86
+ out_file = download_path + "/" + message.mime_type + "/"+custom_uid
87
+
88
+ s3_filepath = upload_to_s3(out_file, encrypt(message.decoded))
89
+ key = message.mime_type.split("/").join("_")
90
+
91
+ h[key] = s3_filepath
92
+ return h
93
+ end
94
+ end
95
+
96
+ ##
97
+ # save data to download_path with file named by filename params.
98
+ # Input is hashmap, and it save the hashmap as yml format.
99
+ #
100
+ def save(data={}, filename)
101
+ download_path = config[:s3_download_folder] + "/" + @current_user_email
102
+ filepath = download_path + "/" + filename + ".yml"
103
+
104
+ return upload_to_s3(filepath, data.to_yaml)
105
+ end
106
+
107
+ ##
108
+ # Helper function to upload the content to the s3
109
+ def upload_to_s3(file_path, content)
110
+ credentials = Aws::Credentials.new(config[:aws_key], config[:aws_secret])
111
+ s3 = Aws::S3::Client.new(credentials: credentials, region: config[:aws_region])
112
+ obj = s3.put_object({
113
+ body: content,
114
+ bucket: config[:s3_bucket],
115
+ key: file_path
116
+ })
117
+ return file_path if obj.successful?
118
+ return ""
119
+ end
120
+
121
+ def s3_resource_files(bucket_name, prefix)
122
+ files = []
123
+ credentials = Aws::Credentials.new(config[:aws_key], config[:aws_secret])
124
+ s3 = Aws::S3::Resource.new(credentials: credentials, region: config[:aws_region])
125
+ s3.bucket(bucket_name).objects(prefix: prefix).each do |obj|
126
+ files << obj.key
127
+ end
128
+ return files
129
+ end
130
+
131
+ def getMessageUUIds(prefix)
132
+ files = s3_resource_files(config[:s3_bucket], prefix)
133
+ files.empty? ? [] : files.map { |i| i.empty? ? 0 : i.split('/').last.split("_").first.to_i }
134
+ end
135
+
136
+ def config
137
+ YAML.load_file(Rails.root.join("config/navi_client.yml")).with_indifferent_access
138
+ end
139
+ end
140
+ end
@@ -0,0 +1,224 @@
1
+ require "mail"
2
+ require "time"
3
+
4
+ require 'base64'
5
+ require 'fileutils'
6
+ require 'yaml'
7
+
8
+ require "logger"
9
+
10
+ require "httparty"
11
+ require "http_service/naviai"
12
+ require 'openssl'
13
+ require 'google/apis/gmail_v1'
14
+ require 'googleauth'
15
+ require 'googleauth/stores/file_token_store'
16
+
17
+ ##
18
+ # This module provides the common functionality that will be needed for local and cloud module.
19
+ # @client_type defines if the gem is being used for local-lockbox or cloud-version.
20
+ #
21
+ module GmailClient
22
+
23
+ OOB_URI = 'urn:ietf:wg:oauth:2.0:oob'.freeze
24
+ APPLICATION_NAME = 'Gmail Api Sync'.freeze
25
+
26
+ attr_accessor :downloaded
27
+
28
+ attr_accessor :message_ids
29
+
30
+ attr_accessor :total_emails
31
+
32
+ @logglyTag
33
+
34
+ def logger
35
+ @logger
36
+ end
37
+
38
+ def errors
39
+ @errors
40
+ end
41
+
42
+ def init client_path, user_cred_path, user_id
43
+
44
+ client_id = Google::Auth::ClientId.from_file(client_path)
45
+ token_store = Google::Auth::Stores::FileTokenStore.new(file: user_cred_path)
46
+ @authorizer = Google::Auth::UserAuthorizer.new(client_id, Google::Apis::GmailV1::AUTH_GMAIL_READONLY, token_store)
47
+
48
+ credentials = @authorizer.get_credentials(user_id)
49
+
50
+ unless credentials.nil?
51
+ init_apis credentials
52
+ end
53
+ credentials
54
+ end
55
+
56
+ def authorize code, user_id
57
+ credentials = @authorizer.get_and_store_credentials_from_code(
58
+ user_id: user_id, code: code, base_url: OOB_URI
59
+ )
60
+ unless credentials.nil?
61
+ init_apis credentials
62
+ end
63
+ credentials
64
+ end
65
+
66
+ def init_apis cred
67
+ @service = Google::Apis::GmailV1::GmailService.new
68
+ @service.client_options.application_name = APPLICATION_NAME
69
+ @service.authorization = cred
70
+ end
71
+
72
+ def authorization_url
73
+ @authorizer.get_authorization_url(base_url: OOB_URI)
74
+ end
75
+
76
+ #
77
+ # retrieve_emails
78
+ #
79
+ # retrieve any mail from a folder, followin specified serach condition
80
+ # for any mail retrieved call a specified block
81
+ #
82
+ def fetch_emails(&process_email_block)
83
+
84
+ @message_ids.each_with_index do |message_id, i|
85
+ begin
86
+ # fetch all the email contents
87
+ # data = imap.uid_fetch(message_id, "RFC822")
88
+ result = @service.get_user_message('me', message_id)
89
+
90
+
91
+ process_email_block.call result, i, i == @message_ids.length-1, message_id
92
+
93
+ rescue => e
94
+ unless logger.nil?
95
+ logger.info "Issue processing email for uuid ##{message_id}, #{e.message}"
96
+ end
97
+
98
+ logToLoggly({event:"EMAIL_SYNC_FAILED", env: @env, storage: @client_type, email: @current_user_email, uuid: message_id, error: e.message})
99
+ raise e
100
+ end
101
+ end
102
+
103
+ end
104
+
105
+
106
+ ##
107
+ # Process each email downloaded from imap-server and creates meta file that will be sent over to the navi-ai service.
108
+ # Meta will content information like: ['from', 'to', 'cc', ..., 'body_url']. This information is then used by navi-ai to parse the body content.
109
+ #
110
+ def process_email(mail, uid, stamp = nil)
111
+ meta = Hash.new
112
+ custom_uid = (Time.now.to_f * 1000).to_s + "_" + uid
113
+
114
+ payload = mail.payload
115
+
116
+ headers = payload.headers
117
+ date = headers.any? { |h| h.name == 'Date' } ? headers.find { |h| h.name == 'Date' }.value : ''
118
+ from = headers.any? { |h| h.name == 'From' } ? headers.find { |h| h.name == 'From' }.value : ''
119
+ to = headers.any? { |h| h.name == 'To' } ? headers.find { |h| h.name == 'To' }.value : ''
120
+ subject = headers.any? { |h| h.name == 'Subject' } ? headers.find { |h| h.name == 'Subject' }.value : ''
121
+ cc = headers.any? { |h| h.name == 'Cc' } ? headers.find { |h| h.name == 'Cc' }.value : ''
122
+
123
+ meta["from"] = from
124
+ meta["to"] = to
125
+ meta["cc"] = cc
126
+ meta["subject"] = subject
127
+ meta["date"] = date
128
+
129
+ if payload.parts&.any?
130
+ payload.parts.map { |part|
131
+ m = download({"decoded" => part.body.data, "mime_type" => part.mime_type}, custom_uid)
132
+ meta.merge!(m) unless m.nil?
133
+ }
134
+ else
135
+ m = download({"decoded" => payload.body.data, "mime_type" => "text/html"}, custom_uid)
136
+ meta.merge!(m) unless m.nil?
137
+ end
138
+
139
+ if stamp.nil?
140
+ save(meta, "meta/#{uid.to_s + '_' + custom_uid}")
141
+ else
142
+ save(meta, "meta/#{uid.to_s + '_' + custom_uid}", stamp)
143
+ end
144
+
145
+ end
146
+
147
+ def all_messages label
148
+ messages = []
149
+ next_page = nil
150
+
151
+ begin
152
+ result = @service.list_user_messages('me', max_results: 1000, page_token: next_page, label_ids: [label])
153
+ messages += result.messages
154
+ # break if messages.size >= 1
155
+ next_page = result.next_page_token
156
+ end while next_page
157
+
158
+ messages
159
+ end
160
+
161
+ def get_user_info user_id
162
+ @service.get_user_profile user_id
163
+ end
164
+
165
+ def all_message_ids label
166
+ mids = []
167
+ messages = all_messages label
168
+ messages.each { |m| mids << m.id }
169
+ mids
170
+ end
171
+
172
+ def init_messages label
173
+ @message_ids = all_message_ids label
174
+ @total_emails = @message_ids.length
175
+ downloaded_email = getMessageUUIds("#{@download_path}meta/")
176
+
177
+ downloaded_email = @message_ids & downloaded_email
178
+
179
+ @downloaded = downloaded_email.length
180
+
181
+ @downloaded = [@downloaded, @total_emails].min
182
+
183
+ @message_ids = @message_ids - downloaded_email
184
+
185
+ if @debug
186
+ if @message_ids.empty?
187
+ @logger.info "No new emails found..."
188
+ elsif downloaded_email.any?
189
+ @logger.info "Found emails saved in your #{@client_type}. Downloading only #{@message_ids.count} new emails..."
190
+ else
191
+ @logger.info "Downloading #{@message_ids.count} emails."
192
+ end
193
+ end
194
+
195
+ end
196
+
197
+ def logToLoggly(messageBody)
198
+
199
+ if(@logglyTag)
200
+ begin
201
+ HTTParty.post("http://logs-01.loggly.com/bulk/0d67f93b-6568-4b00-9eca-97d0ea0bd5a1/tag/#{@logglyTag}/",
202
+ body: messageBody.to_json,
203
+ headers: { 'Content-Type' => 'application/json' } )
204
+ rescue
205
+ true
206
+ end
207
+
208
+ else
209
+ if @debug && !@logger.nil?
210
+ @logger.info "Logging to Loggly disabled"
211
+ @logger.info messageBody
212
+ end
213
+ end
214
+
215
+ end
216
+
217
+ def setupLoggly(tag)
218
+ @logglyTag = tag
219
+ end
220
+
221
+ def encrypt(data)
222
+ Base64.encode64(data)
223
+ end
224
+ end
@@ -0,0 +1,15 @@
1
+ require "httparty"
2
+
3
+ module HTTPService
4
+ class NaviAI
5
+
6
+ def self.start(filepath:, client_id: nil, client_type:, token:, email:, is_last: false)
7
+ go_url = "#{ENV['parser_url']}/v2/metas"
8
+ HTTParty.post(go_url, body: { client_id: client_id, client_type: client_type, list_meta_path: filepath, token: token, email: email, is_last: is_last }.to_json, timeout: 400)
9
+ end
10
+
11
+ def self.generate_csv(url, token)
12
+ HTTParty.post(url, headers: {"Authorization": token})
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,246 @@
1
+ require_relative '../client'
2
+ require_relative '../gmail_client'
3
+
4
+ module NaviEmailSync
5
+
6
+ ##
7
+ # This class represents the client for local lockbox mode.
8
+ # In local-lockbox mode, all the content will be saved and also being used from the local file system
9
+ #
10
+ class Local
11
+
12
+ CONFIG_PATH = File.join(ENV['HOME'], '/.navi/config.yml')
13
+
14
+ def initialize(method, client_type = "local", debug = true)
15
+ # flag to print Ruby library debug info (very detailed)
16
+ @net_imap_debug = false
17
+
18
+ # flag to mark email as read after gets downloaded.
19
+ @mark_as_read = false
20
+
21
+ # flag to turn on/off debug mode.
22
+ @debug = debug
23
+
24
+ # override the log file
25
+ mkdir_if_not_exist(config['client_log_file'])
26
+ @logger = Logger.new(config['client_log_file'])
27
+
28
+ # sso_web (authentication) config.
29
+ @sso_web_url = ENV['api_url']
30
+ # authentication token received from sso_web used to authenticate the request to database_api
31
+ @token = nil
32
+
33
+ @download_path = config['download_path'] + config['identifier'] + "/#{method}/"
34
+ @current_user_email = config['username']
35
+
36
+ # client_type
37
+ @client_type = client_type
38
+ @gen_csv_url = "#{ENV['api_url']}/v2/generate_csv_input"
39
+ @env = ENV['env']?ENV['env']:Rails.env
40
+ end
41
+
42
+ #
43
+ # login
44
+ #
45
+ # login to the navi-cloud and get the authentication token
46
+ #
47
+ def login
48
+ url = "#{@sso_web_url}/oauth/token"
49
+ provider_url = url
50
+ token = HTTParty.post(provider_url,
51
+ body: {
52
+ client_id: config["uid"], # get from sso_web application
53
+ client_secret: config["secret_key"],
54
+ grant_type: "client_credentials"
55
+ }
56
+ )['access_token']
57
+ @token = "Token: #{token}##{config['username']}"
58
+ end
59
+
60
+ def override_logger(logger)
61
+ @logger = logger
62
+ end
63
+
64
+ def set_token(token)
65
+ @token = token
66
+ end
67
+
68
+ ##
69
+ # Downloads the email content from imap-server and save it to the download_path
70
+ #
71
+ def download(message, custom_uid)
72
+
73
+ mime_type = message["mime_type"].nil? ? message.mime_type : message["mime_type"]
74
+
75
+ if ['text/plain', 'text/html', 'text/alternative'].include? mime_type
76
+
77
+ h = Hash.new
78
+ out_file = @download_path + mime_type + "/"+custom_uid
79
+ mkdir_if_not_exist(out_file)
80
+
81
+ File.open(out_file, 'w') { |file| file.write(encrypt(message["decoded"].nil? ? message.decoded : message["decoded"])) }
82
+ key = mime_type.split("/").join("_")
83
+
84
+ h[key] = out_file
85
+ return h
86
+ end
87
+ end
88
+
89
+ ##
90
+ # save data to download_path with file named by filename params.
91
+ # Input is hashmap, and it save the hashmap as yml format.
92
+ #
93
+ def save(data={}, filename, stamp)
94
+ temp_path = @download_path + "inputs/temp-#{stamp}"
95
+ filepath = @download_path + filename + ".yml"
96
+
97
+ mkdir_if_not_exist(filepath)
98
+ mkdir_if_not_exist(temp_path)
99
+ File.open(filepath, 'w') do |f|
100
+ f.puts(data.to_yaml)
101
+ end
102
+
103
+ File.open(temp_path, 'a') do |f|
104
+ f.puts(filepath)
105
+ end
106
+
107
+ return filepath
108
+ end
109
+
110
+ ##
111
+ # send bulk request to navi-ai service with list of input files downloaded from imap server.
112
+ #
113
+ def send_request(in_filenames = [], stamp, is_last: false)
114
+ unless in_filenames.empty?
115
+ filename = @download_path + "inputs/#{stamp}"
116
+
117
+ mkdir_if_not_exist(filename)
118
+
119
+ File.open(filename, 'w') do |f|
120
+ in_filenames.each { |element| f.puts(element) }
121
+ end
122
+
123
+ response = HTTPService::NaviAI.start(filepath: filename, client_type: @client_type, token: @token, email: config['username'], is_last: is_last)
124
+ return response
125
+ end
126
+ end
127
+
128
+ def mkdir_if_not_exist(filepath)
129
+ dirname = File.dirname(filepath)
130
+ unless File.directory?(dirname)
131
+ FileUtils.mkdir_p(dirname)
132
+ end
133
+ end
134
+
135
+ #
136
+ # idle_loop
137
+ #
138
+ # check for any further mail with "real-time" responsiveness.
139
+ # retrieve any mail from a folder, following specified search condition
140
+ # for any mail retrieved call a specified block
141
+ #
142
+ def idle_loop(search_condition, folder, server, username, password, callback = nil, navi_control = nil)
143
+
144
+ @logger.info "\nwaiting new mails (IDLE loop)..."
145
+
146
+ loop do
147
+ begin
148
+ @imap.select folder
149
+ @imap.idle do |resp|
150
+
151
+ # You'll get all the things from the server. For new emails (EXISTS)
152
+ if resp.kind_of?(Net::IMAP::UntaggedResponse) and resp.name == "EXISTS"
153
+
154
+ # @logger.debug resp.inspect if @debug
155
+ # Got something. Send DONE. This breaks you out of the blocking call
156
+ @logger.debug "New mail received" if @debug
157
+ @imap.idle_done
158
+ end
159
+ end
160
+
161
+ # We're out, which means there are some emails ready for us.
162
+ # Go do a search for UNSEEN and fetch them.
163
+ filenames = []
164
+ stamp = (Time.now.to_f * 1000).to_s
165
+ init_messages folder, search_condition
166
+
167
+ fetch_emails() { |mail, i, isLast, id|
168
+ callback.call(mail, i, id, false) unless callback.nil?
169
+ filenames << process_email(mail, id, stamp)
170
+ callback.call(mail, i, id, true) unless callback.nil?
171
+ }
172
+
173
+ @logger.info "Sending Request for #{filenames.size} emails to Navi AI."
174
+ navi_control.call(true) unless navi_control.nil?
175
+ self.send_request(filenames, stamp, is_last: true)
176
+
177
+ # HTTPService::NaviAI.generate_csv("#{@gen_csv_url}?email=#{username}", @token)
178
+
179
+ @logger.debug "Process Completed." if @debug
180
+ navi_control.call(false) unless navi_control.nil?
181
+
182
+ rescue SignalException => e
183
+ # http://stackoverflow.com/questions/2089421/capturing-ctrl-c-in-ruby
184
+ @logger.info "Signal received at #{time_now}: #{e.class}. #{e.message}"
185
+ shutdown @imap
186
+
187
+ rescue Net::IMAP::Error => e
188
+ @logger.error "Net::IMAP::Error at #{time_now}: #{e.class}. #{e.message}"
189
+ @imap = imap_connection(server, username, password) #if e.message == 'connection closed'
190
+ @logger.info "reconnected to server: #{server}"
191
+
192
+ # rescue Exception => e
193
+ # @logger.error "Something went wrong at #{time_now}: #{e.class}. #{e.message}"
194
+ #
195
+ # @imap = imap_connection(server, username, password)
196
+ # @logger.info "reconnected to server: #{server}"
197
+ end
198
+ end
199
+ end
200
+
201
+ def initate_csv_generation(username)
202
+ HTTPService::NaviAI.generate_csv("#{@gen_csv_url}?email=#{username}", @token)
203
+ end
204
+
205
+ def config
206
+ YAML.load_file(CONFIG_PATH)
207
+ end
208
+
209
+ def save_aes_key_iv(key, iv)
210
+ aes_key_path = File.join(ENV['HOME'] + '/.navi/.aes.key')
211
+ iv_path = File.join(ENV['HOME'] + '/.navi/.aes.iv')
212
+
213
+ File.open(aes_key_path, "w") { |f| f.write(Base64.encode64(key)) }
214
+ File.open(iv_path, "w") { |f| f.write(Base64.encode64(iv)) }
215
+ return aes_key_path, iv_path
216
+ end
217
+
218
+ def getMessageUUIds(path)
219
+ File.directory?(path) ? (Dir.entries path).map { |i| i.split("_").first } : []
220
+ end
221
+
222
+ def update_config(data)
223
+ File.write(CONFIG_PATH, YAML.dump(data))
224
+ end
225
+
226
+ def parse_prev_failed_emails
227
+ changes = false
228
+ Dir[@download_path + 'inputs/temp*'].each do |file_name|
229
+ if File.file? file_name
230
+ HTTPService::NaviAI.start(filepath: file_name, client_type: @client_type, token: @token, email: @current_user_email )
231
+ changes = true
232
+ end
233
+ end
234
+ return changes
235
+ end
236
+
237
+ end
238
+
239
+ class GmailApi < Local
240
+ include GmailClient
241
+ end
242
+
243
+ class ImapApi < Local
244
+ include Client
245
+ end
246
+ end
@@ -0,0 +1,3 @@
1
+ module NaviEmailSync
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,9 @@
1
+ require "local/navi_local_client"
2
+ require "cloud/navi_cloud_client"
3
+
4
+ ##
5
+ # This module represents the client that helps connection between navi and the imap server.
6
+ #
7
+ module NaviEmailSync
8
+
9
+ end
@@ -0,0 +1,37 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'navi_client/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "navi_email_sync"
8
+ spec.version = NaviEmailSync::VERSION
9
+ spec.authors = ["Sanjib"]
10
+ spec.email = ["sanjib@whitehatengineering.com"]
11
+
12
+ spec.summary = %q{ Write a short summary, because Rubygems requires one.}
13
+ spec.description = %q{ Write a longer description or delete this line.}
14
+ spec.homepage = "http://navihq.com"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ if spec.respond_to?(:metadata)
20
+ # spec.metadata['allowed_push_host'] = "Set to 'https://rubygems.org'"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
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 "bundler", "~> 1.11"
31
+ spec.add_development_dependency "rake", "~> 10.0"
32
+ spec.add_development_dependency "rspec", "~> 3.0"
33
+ spec.add_runtime_dependency "httparty"
34
+ spec.add_runtime_dependency "mail"
35
+ spec.add_runtime_dependency "google-api-client", ">=0.23.0"
36
+ spec.add_runtime_dependency "concurrent-ruby", "~> 1.0.5"
37
+ end
metadata ADDED
@@ -0,0 +1,162 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: navi_email_sync
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Sanjib
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-06-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: httparty
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: mail
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: google-api-client
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: 0.23.0
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: 0.23.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: concurrent-ruby
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 1.0.5
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 1.0.5
111
+ description: " Write a longer description or delete this line."
112
+ email:
113
+ - sanjib@whitehatengineering.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".travis.yml"
121
+ - CODE_OF_CONDUCT.md
122
+ - Gemfile
123
+ - LICENSE.txt
124
+ - README.md
125
+ - Rakefile
126
+ - bin/.DS_Store
127
+ - bin/console
128
+ - bin/setup
129
+ - config/navi_client.sample.yml
130
+ - lib/client.rb
131
+ - lib/cloud/navi_cloud_client.rb
132
+ - lib/gmail_client.rb
133
+ - lib/http_service/naviai.rb
134
+ - lib/local/navi_local_client.rb
135
+ - lib/navi_client/version.rb
136
+ - lib/navi_email_sync.rb
137
+ - navi_email_sync.gemspec
138
+ homepage: http://navihq.com
139
+ licenses:
140
+ - MIT
141
+ metadata: {}
142
+ post_install_message:
143
+ rdoc_options: []
144
+ require_paths:
145
+ - lib
146
+ required_ruby_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ required_rubygems_version: !ruby/object:Gem::Requirement
152
+ requirements:
153
+ - - ">="
154
+ - !ruby/object:Gem::Version
155
+ version: '0'
156
+ requirements: []
157
+ rubyforge_project:
158
+ rubygems_version: 2.7.3
159
+ signing_key:
160
+ specification_version: 4
161
+ summary: Write a short summary, because Rubygems requires one.
162
+ test_files: []