navi_client 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/client.rb +211 -0
- data/lib/cloud/navi_cloud_client.rb +5 -3
- data/lib/local/navi_local_client.rb +4 -3
- data/lib/navi_client/version.rb +1 -1
- data/lib/navi_client.rb +0 -209
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 27fdd770cf2ab7dd013f05970b021df3aab21b89
|
4
|
+
data.tar.gz: fc2a9e260b5d2f3ec20f98ee35e2d44575bab9ba
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a7cac08693d224e364448e0a7123a70c908c7663e99e175a9e5376f8d8aa665c4bf367d0135d23b3da87bffd1a0d692f2c8fa59ffd3c2b2bbe59dd5f138cc259
|
7
|
+
data.tar.gz: 3f7468e74fd7ca33a8154db47cb405211d5a81cba9a40f3c95ca73ba57d049e479713d9d447ff900eac9f535845558c3303366af9a67a08619621201ea91fdf9
|
data/lib/client.rb
ADDED
@@ -0,0 +1,211 @@
|
|
1
|
+
module Client
|
2
|
+
def logger
|
3
|
+
@logger
|
4
|
+
end
|
5
|
+
|
6
|
+
#
|
7
|
+
# login
|
8
|
+
#
|
9
|
+
# login to the navi-cloud and get the authentication token
|
10
|
+
#
|
11
|
+
def login
|
12
|
+
provider_url = "http://localhost:3008/oauth/token"
|
13
|
+
@token = HTTParty.post(provider_url,
|
14
|
+
body: {
|
15
|
+
client_id: config["uid"], # get from sso_web application
|
16
|
+
client_secret: config["secret_key"],
|
17
|
+
grant_type: "client_credentials"
|
18
|
+
}
|
19
|
+
)['access_token']
|
20
|
+
end
|
21
|
+
|
22
|
+
#
|
23
|
+
# imap_connection
|
24
|
+
#
|
25
|
+
# connect the app with imap server
|
26
|
+
#
|
27
|
+
def imap_connection(server, username, password)
|
28
|
+
# connect to IMAP server
|
29
|
+
imap = Net::IMAP.new server, ssl: true, certs: nil, verify: false
|
30
|
+
|
31
|
+
Net::IMAP.debug = @net_imap_debug
|
32
|
+
|
33
|
+
# http://ruby-doc.org/stdlib-2.1.5/libdoc/net/imap/rdoc/Net/IMAP.html#method-i-capability
|
34
|
+
capabilities = imap.capability
|
35
|
+
|
36
|
+
@logger.debug("imap capabilities: #{capabilities.join(',')}") if @debug
|
37
|
+
|
38
|
+
unless capabilities.include? "IDLE"
|
39
|
+
@logger.info "'IDLE' IMAP capability not available in server: #{server}"
|
40
|
+
imap.disconnect
|
41
|
+
exit
|
42
|
+
end
|
43
|
+
|
44
|
+
# login
|
45
|
+
imap.login username, password
|
46
|
+
|
47
|
+
# return IMAP connection handler
|
48
|
+
imap
|
49
|
+
end
|
50
|
+
|
51
|
+
#
|
52
|
+
# retrieve_emails
|
53
|
+
#
|
54
|
+
# retrieve any mail from a folder, followin specified serach condition
|
55
|
+
# for any mail retrieved call a specified block
|
56
|
+
#
|
57
|
+
def retrieve_emails(imap, search_condition, folder, &process_email_block)
|
58
|
+
|
59
|
+
# select folder
|
60
|
+
imap.select folder
|
61
|
+
|
62
|
+
message_ids = imap.search(search_condition)
|
63
|
+
|
64
|
+
if @debug
|
65
|
+
if message_ids.empty?
|
66
|
+
@logger.debug "\nno messages found.\n"
|
67
|
+
return
|
68
|
+
else
|
69
|
+
@logger.debug "\nProcessing #{message_ids.count} mails.\n"
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
message_ids.each_with_index do |message_id, i|
|
74
|
+
# fetch all the email contents
|
75
|
+
data = imap.fetch(message_id, "RFC822")
|
76
|
+
|
77
|
+
data.each do |d|
|
78
|
+
msg = d.attr['RFC822']
|
79
|
+
# instantiate a Mail object to avoid further IMAP parameters nightmares
|
80
|
+
mail = Mail.read_from_string msg
|
81
|
+
|
82
|
+
# call the block with mail object as param
|
83
|
+
start = (i == 0)
|
84
|
+
last = (i == message_ids-1)
|
85
|
+
process_email_block.call mail, start, last
|
86
|
+
|
87
|
+
# mark as read
|
88
|
+
if @mark_as_read
|
89
|
+
imap.store(message_id, "+FLAGS", [:Seen])
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
#
|
96
|
+
# idle_loop
|
97
|
+
#
|
98
|
+
# check for any further mail with "real-time" responsiveness.
|
99
|
+
# retrieve any mail from a folder, following specified search condition
|
100
|
+
# for any mail retrieved call a specified block
|
101
|
+
#
|
102
|
+
def idle_loop(imap, search_condition, folder, server, username, password)
|
103
|
+
|
104
|
+
@logger.info "\nwaiting new mails (IDLE loop)..."
|
105
|
+
|
106
|
+
loop do
|
107
|
+
begin
|
108
|
+
imap.select folder
|
109
|
+
imap.idle do |resp|
|
110
|
+
|
111
|
+
# You'll get all the things from the server. For new emails (EXISTS)
|
112
|
+
if resp.kind_of?(Net::IMAP::UntaggedResponse) and resp.name == "EXISTS"
|
113
|
+
|
114
|
+
@logger.debug resp.inspect if @debug
|
115
|
+
# Got something. Send DONE. This breaks you out of the blocking call
|
116
|
+
imap.idle_done
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
# We're out, which means there are some emails ready for us.
|
121
|
+
# Go do a search for UNSEEN and fetch them.
|
122
|
+
retrieve_emails(imap, search_condition, folder) { |mail| process_email mail }
|
123
|
+
@logger.debug "Process Completed." if @debug
|
124
|
+
|
125
|
+
rescue SignalException => e
|
126
|
+
# http://stackoverflow.com/questions/2089421/capturing-ctrl-c-in-ruby
|
127
|
+
@logger.info "Signal received at #{time_now}: #{e.class}. #{e.message}"
|
128
|
+
shutdown imap
|
129
|
+
|
130
|
+
rescue Net::IMAP::Error => e
|
131
|
+
@logger.error "Net::IMAP::Error at #{time_now}: #{e.class}. #{e.message}"
|
132
|
+
|
133
|
+
# timeout ? reopen connection
|
134
|
+
imap = imap_connection(server, username, password) #if e.message == 'connection closed'
|
135
|
+
@logger.info "reconnected to server: #{server}"
|
136
|
+
|
137
|
+
rescue Exception => e
|
138
|
+
@logger.error "Something went wrong at #{time_now}: #{e.class}. #{e.message}"
|
139
|
+
|
140
|
+
imap = imap_connection(server, username, password)
|
141
|
+
@logger.info "reconnected to server: #{server}"
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
def process_email(mail, start, last)
|
147
|
+
meta = Hash.new
|
148
|
+
custom_uid = (Time.now.to_f * 1000).to_s + "_" + mail.__id__.to_s
|
149
|
+
|
150
|
+
meta["from"] = mail.from.first
|
151
|
+
meta["to"] = mail.to.join(";") unless mail.to.nil?
|
152
|
+
meta["cc"] = mail.cc.join(";") unless mail.cc.nil?
|
153
|
+
meta["subject"] = mail.subject
|
154
|
+
meta["date"] = mail.date.to_s
|
155
|
+
|
156
|
+
if mail.multipart?
|
157
|
+
for i in 0...mail.parts.length
|
158
|
+
m = @local_flag ? download_local(mail, custom_uid) : download_s3(mail, custom_uid)
|
159
|
+
meta.merge!(m) unless m.nil?
|
160
|
+
end
|
161
|
+
else
|
162
|
+
m = @local_flag ? download_local(mail, custom_uid) : download_s3(mail, custom_uid)
|
163
|
+
meta.merge!(m) unless m.nil?
|
164
|
+
end
|
165
|
+
|
166
|
+
meta_file_path = save(meta, "meta/#{custom_uid}")
|
167
|
+
pid = Process.spawn(@cmd+" -f=#{meta_file_path} -t=#{@token}")
|
168
|
+
|
169
|
+
HTTPService::NaviAI.start(start, last)
|
170
|
+
end
|
171
|
+
|
172
|
+
private
|
173
|
+
|
174
|
+
def save(data={}, filename)
|
175
|
+
download_path = config['download_path']
|
176
|
+
filepath = download_path + filename + ".yml"
|
177
|
+
|
178
|
+
mkdir_if_not_exist(filepath)
|
179
|
+
|
180
|
+
File.write(filepath, data.to_yaml)
|
181
|
+
return filepath
|
182
|
+
end
|
183
|
+
|
184
|
+
def encrypt(data)
|
185
|
+
Base64.encode64(data)
|
186
|
+
end
|
187
|
+
|
188
|
+
def mkdir_if_not_exist(filepath)
|
189
|
+
dirname = File.dirname(filepath)
|
190
|
+
unless File.directory?(dirname)
|
191
|
+
FileUtils.mkdir_p(dirname)
|
192
|
+
end
|
193
|
+
end
|
194
|
+
|
195
|
+
def time_now
|
196
|
+
Time.now.utc.iso8601(3)
|
197
|
+
end
|
198
|
+
|
199
|
+
def shutdown(imap)
|
200
|
+
imap.idle_done
|
201
|
+
imap.logout unless imap.disconnected?
|
202
|
+
imap.disconnect
|
203
|
+
|
204
|
+
@logger.info "#{$0} has ended (crowd applauds)"
|
205
|
+
exit 0
|
206
|
+
end
|
207
|
+
|
208
|
+
def config
|
209
|
+
YAML.load_file('/var/navi/config.yml')
|
210
|
+
end
|
211
|
+
end
|
data/lib/navi_client/version.rb
CHANGED
data/lib/navi_client.rb
CHANGED
@@ -16,213 +16,4 @@ require "http_service/naviai"
|
|
16
16
|
|
17
17
|
module NaviClient
|
18
18
|
|
19
|
-
def logger
|
20
|
-
@logger
|
21
|
-
end
|
22
|
-
|
23
|
-
#
|
24
|
-
# login
|
25
|
-
#
|
26
|
-
# login to the navi-cloud and get the authentication token
|
27
|
-
#
|
28
|
-
def login
|
29
|
-
provider_url = "http://localhost:3008/oauth/token"
|
30
|
-
@token = HTTParty.post(provider_url,
|
31
|
-
body: {
|
32
|
-
client_id: config["uid"], # get from sso_web application
|
33
|
-
client_secret: config["secret_key"],
|
34
|
-
grant_type: "client_credentials"
|
35
|
-
}
|
36
|
-
)['access_token']
|
37
|
-
end
|
38
|
-
|
39
|
-
#
|
40
|
-
# imap_connection
|
41
|
-
#
|
42
|
-
# connect the app with imap server
|
43
|
-
#
|
44
|
-
def imap_connection(server, username, password)
|
45
|
-
# connect to IMAP server
|
46
|
-
imap = Net::IMAP.new server, ssl: true, certs: nil, verify: false
|
47
|
-
|
48
|
-
Net::IMAP.debug = @net_imap_debug
|
49
|
-
|
50
|
-
# http://ruby-doc.org/stdlib-2.1.5/libdoc/net/imap/rdoc/Net/IMAP.html#method-i-capability
|
51
|
-
capabilities = imap.capability
|
52
|
-
|
53
|
-
@logger.debug("imap capabilities: #{capabilities.join(',')}") if @debug
|
54
|
-
|
55
|
-
unless capabilities.include? "IDLE"
|
56
|
-
@logger.info "'IDLE' IMAP capability not available in server: #{server}"
|
57
|
-
imap.disconnect
|
58
|
-
exit
|
59
|
-
end
|
60
|
-
|
61
|
-
# login
|
62
|
-
imap.login username, password
|
63
|
-
|
64
|
-
# return IMAP connection handler
|
65
|
-
imap
|
66
|
-
end
|
67
|
-
|
68
|
-
#
|
69
|
-
# retrieve_emails
|
70
|
-
#
|
71
|
-
# retrieve any mail from a folder, followin specified serach condition
|
72
|
-
# for any mail retrieved call a specified block
|
73
|
-
#
|
74
|
-
def retrieve_emails(imap, search_condition, folder, &process_email_block)
|
75
|
-
|
76
|
-
# select folder
|
77
|
-
imap.select folder
|
78
|
-
|
79
|
-
message_ids = imap.search(search_condition)
|
80
|
-
|
81
|
-
if @debug
|
82
|
-
if message_ids.empty?
|
83
|
-
@logger.debug "\nno messages found.\n"
|
84
|
-
return
|
85
|
-
else
|
86
|
-
@logger.debug "\nProcessing #{message_ids.count} mails.\n"
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
message_ids.each_with_index do |message_id, i|
|
91
|
-
# fetch all the email contents
|
92
|
-
data = imap.fetch(message_id, "RFC822")
|
93
|
-
|
94
|
-
data.each do |d|
|
95
|
-
msg = d.attr['RFC822']
|
96
|
-
# instantiate a Mail object to avoid further IMAP parameters nightmares
|
97
|
-
mail = Mail.read_from_string msg
|
98
|
-
|
99
|
-
# call the block with mail object as param
|
100
|
-
start = (i == 0)
|
101
|
-
last = (i == message_ids-1)
|
102
|
-
process_email_block.call mail, start, last
|
103
|
-
|
104
|
-
# mark as read
|
105
|
-
if @mark_as_read
|
106
|
-
imap.store(message_id, "+FLAGS", [:Seen])
|
107
|
-
end
|
108
|
-
end
|
109
|
-
end
|
110
|
-
end
|
111
|
-
|
112
|
-
#
|
113
|
-
# idle_loop
|
114
|
-
#
|
115
|
-
# check for any further mail with "real-time" responsiveness.
|
116
|
-
# retrieve any mail from a folder, following specified search condition
|
117
|
-
# for any mail retrieved call a specified block
|
118
|
-
#
|
119
|
-
def idle_loop(imap, search_condition, folder, server, username, password)
|
120
|
-
|
121
|
-
@logger.info "\nwaiting new mails (IDLE loop)..."
|
122
|
-
|
123
|
-
loop do
|
124
|
-
begin
|
125
|
-
imap.select folder
|
126
|
-
imap.idle do |resp|
|
127
|
-
|
128
|
-
# You'll get all the things from the server. For new emails (EXISTS)
|
129
|
-
if resp.kind_of?(Net::IMAP::UntaggedResponse) and resp.name == "EXISTS"
|
130
|
-
|
131
|
-
@logger.debug resp.inspect if @debug
|
132
|
-
# Got something. Send DONE. This breaks you out of the blocking call
|
133
|
-
imap.idle_done
|
134
|
-
end
|
135
|
-
end
|
136
|
-
|
137
|
-
# We're out, which means there are some emails ready for us.
|
138
|
-
# Go do a search for UNSEEN and fetch them.
|
139
|
-
retrieve_emails(imap, search_condition, folder) { |mail| process_email mail }
|
140
|
-
@logger.debug "Process Completed." if @debug
|
141
|
-
|
142
|
-
rescue SignalException => e
|
143
|
-
# http://stackoverflow.com/questions/2089421/capturing-ctrl-c-in-ruby
|
144
|
-
@logger.info "Signal received at #{time_now}: #{e.class}. #{e.message}"
|
145
|
-
shutdown imap
|
146
|
-
|
147
|
-
rescue Net::IMAP::Error => e
|
148
|
-
@logger.error "Net::IMAP::Error at #{time_now}: #{e.class}. #{e.message}"
|
149
|
-
|
150
|
-
# timeout ? reopen connection
|
151
|
-
imap = imap_connection(server, username, password) #if e.message == 'connection closed'
|
152
|
-
@logger.info "reconnected to server: #{server}"
|
153
|
-
|
154
|
-
rescue Exception => e
|
155
|
-
@logger.error "Something went wrong at #{time_now}: #{e.class}. #{e.message}"
|
156
|
-
|
157
|
-
imap = imap_connection(server, username, password)
|
158
|
-
@logger.info "reconnected to server: #{server}"
|
159
|
-
end
|
160
|
-
end
|
161
|
-
end
|
162
|
-
|
163
|
-
def process_email(mail, start, last)
|
164
|
-
meta = Hash.new
|
165
|
-
custom_uid = (Time.now.to_f * 1000).to_s + "_" + mail.__id__.to_s
|
166
|
-
|
167
|
-
meta["from"] = mail.from.first
|
168
|
-
meta["to"] = mail.to.join(";") unless mail.to.nil?
|
169
|
-
meta["cc"] = mail.cc.join(";") unless mail.cc.nil?
|
170
|
-
meta["subject"] = mail.subject
|
171
|
-
meta["date"] = mail.date.to_s
|
172
|
-
|
173
|
-
if mail.multipart?
|
174
|
-
for i in 0...mail.parts.length
|
175
|
-
m = @local_flag ? download_local(mail, custom_uid) : download_s3(mail, custom_uid)
|
176
|
-
meta.merge!(m) unless m.nil?
|
177
|
-
end
|
178
|
-
else
|
179
|
-
m = @local_flag ? download_local(mail, custom_uid) : download_s3(mail, custom_uid)
|
180
|
-
meta.merge!(m) unless m.nil?
|
181
|
-
end
|
182
|
-
|
183
|
-
meta_file_path = save(meta, "meta/#{custom_uid}")
|
184
|
-
pid = Process.spawn(@cmd+" -f=#{meta_file_path} -t=#{@token}")
|
185
|
-
|
186
|
-
HTTPService::NaviAI.start(start, last)
|
187
|
-
end
|
188
|
-
|
189
|
-
private
|
190
|
-
|
191
|
-
def save(data={}, filename)
|
192
|
-
download_path = config['download_path']
|
193
|
-
filepath = download_path + filename + ".yml"
|
194
|
-
|
195
|
-
mkdir_if_not_exist(filepath)
|
196
|
-
|
197
|
-
File.write(filepath, data.to_yaml)
|
198
|
-
return filepath
|
199
|
-
end
|
200
|
-
|
201
|
-
def encrypt(data)
|
202
|
-
Base64.encode64(data)
|
203
|
-
end
|
204
|
-
|
205
|
-
def mkdir_if_not_exist(filepath)
|
206
|
-
dirname = File.dirname(filepath)
|
207
|
-
unless File.directory?(dirname)
|
208
|
-
FileUtils.mkdir_p(dirname)
|
209
|
-
end
|
210
|
-
end
|
211
|
-
|
212
|
-
def time_now
|
213
|
-
Time.now.utc.iso8601(3)
|
214
|
-
end
|
215
|
-
|
216
|
-
def shutdown(imap)
|
217
|
-
imap.idle_done
|
218
|
-
imap.logout unless imap.disconnected?
|
219
|
-
imap.disconnect
|
220
|
-
|
221
|
-
@logger.info "#{$0} has ended (crowd applauds)"
|
222
|
-
exit 0
|
223
|
-
end
|
224
|
-
|
225
|
-
def config
|
226
|
-
YAML.load_file('/var/navi/config.yml')
|
227
|
-
end
|
228
19
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: navi_client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Surya
|
@@ -99,6 +99,7 @@ files:
|
|
99
99
|
- bin/.DS_Store
|
100
100
|
- bin/console
|
101
101
|
- bin/setup
|
102
|
+
- lib/client.rb
|
102
103
|
- lib/cloud/navi_cloud_client.rb
|
103
104
|
- lib/http_service/naviai.rb
|
104
105
|
- lib/local/navi_local_client.rb
|