navilocal 1.1.4 → 1.1.5

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ef6e41bf0bb13451d762991d8f0b5917fafa76431b5e8dd9dc517205b9af053b
4
- data.tar.gz: 33065bfdfbe9136e815e63afa5553262e68a3f62adb942fcb7d965643b5246db
3
+ metadata.gz: 98103c3ff7d60501b98f0d0dfc7226f2160f5ec8bf1353edc6c99cc5e7288bd9
4
+ data.tar.gz: 9bb3577d9448c190d0c9c79968c29f15abf07b35b501055c686ef38afcc60acd
5
5
  SHA512:
6
- metadata.gz: fb2567489414b15d5ccd01a0f3069403e870acd5657357843bf6ca1b1aa2ff7156f421a23c45c5cd84a3f941dbdd9b233ff039787cbec5e35f9897083d4d914a
7
- data.tar.gz: 50877afc3c0b887d6668dbdb9656420a253657381ec81a9e282a9375e2aae2c92e3db206fb2084b17f5bd6bddb9785f4f0d44d8dfa1e93eb609a9c51e2b5ffd7
6
+ metadata.gz: 590631360504966bece25ee644c7efb3f65d385f00e91654428f1aae8656bd8631863b3e9f26fd527cfded21c473258445cf001915edbd256d8e8ffd73337bbd
7
+ data.tar.gz: 46bfea049da5c9091bbd13b34f40f0dbf58d1079f1f321ff6a988bcd2a3f55106d06dbdfb168d32b2cdb5cd7e522a4a553737152f82c79b2cc2a03c28b90aab1
@@ -3,12 +3,15 @@ require 'json'
3
3
 
4
4
  module NaviClientInstaller
5
5
  class Settings
6
- PROPERTIES = [:provider, :username, :password, :agreement, :location, :creation_datetime, :last_modified, :download_path, :install, :hash, :client_log_file, :common_words_file, :wikipedia_topic_file, :ai_log_file, :logout, :env].freeze
6
+ PROPERTIES = [:provider, :username, :password, :agreement, :location, :creation_datetime, :last_modified, :download_path, :install, :hash, :client_log_file, :common_words_file, :wikipedia_topic_file, :ai_log_file, :logout, :env, :method, :identifier].freeze
7
7
 
8
8
  PRIORITIES = ['high', 'medium', 'normal', 'low'].freeze
9
9
 
10
10
  DEFAULT_STORAGE = File.expand_path File.join('~', '.navi')
11
11
 
12
+ GMAIL_CLIENT_PATH = File.join(File.expand_path('../../', __dir__), 'resources', 'config', 'gmail_api_secret.json')
13
+ GMAIL_TOKEN_PATH = File.expand_path File.join('~', '.navi', 'bin', 'token.yaml')
14
+
12
15
  DEFAULT_ENV = "staging"
13
16
 
14
17
  attr_accessor *PROPERTIES
@@ -1,12 +1,16 @@
1
1
  require_relative '../../lib/common_functions'
2
+ require 'gmail_client'
3
+ require "net/imap"
4
+
2
5
  module NaviClientInstaller
3
6
  class AccountConfiguration
4
7
  attr_accessor :window_ui
5
8
  include CommonFunctions
9
+ include GmailClient
6
10
  attr_accessor :next_btn
7
11
  attr_accessor :cancel_btn
8
12
 
9
- def initialize(settings, next_btn=nil, show_cancel_btn = nil)
13
+ def initialize(settings, next_btn = nil, show_cancel_btn = nil)
10
14
  builder = Gtk::Builder.new(:resource => '/com/navi/navi-client/ui/account_configuration.ui')
11
15
  css_provider = Gtk::CssProvider.new
12
16
  css_provider.load(:resource_path => '/com/navi/navi-client/css/main.css')
@@ -16,6 +20,16 @@ module NaviClientInstaller
16
20
  @combo_text_provider = builder.get_object("combo_text_ac_provider")
17
21
  @entry_email = builder.get_object("entry_email")
18
22
  @entry_password = builder.get_object("entry_password")
23
+ @rb_method = builder.get_object("imap_method")
24
+ @gmail_method = builder.get_object("gmail_method")
25
+ @stack = builder.get_object("account_stack")
26
+ @imap_child = builder.get_object("imap_child")
27
+ @gmail_child = builder.get_object("gmail_child")
28
+ @google_btn = builder.get_object("google_btn")
29
+ @code_entry = builder.get_object("code_entry")
30
+ @error_ind = builder.get_object("error")
31
+ @user_holder = builder.get_object("user_holder")
32
+
19
33
  @next_btn = next_btn
20
34
 
21
35
  if(@next_btn.nil?)
@@ -25,8 +39,32 @@ module NaviClientInstaller
25
39
  end
26
40
  @next_btn.visible = true
27
41
 
28
- # @settings = settings
29
- #
42
+ if(settings.method == 'gmail')
43
+ @gmail_method.active = true
44
+ @stack.set_visible_child @gmail_child
45
+ else
46
+ @rb_method.active = true
47
+ @stack.set_visible_child @imap_child
48
+ end
49
+
50
+ if(settings.method == "gmail")
51
+ @user_holder.text = "Signed In as #{settings.username}"
52
+ @google_btn.label = "Use another gmail account"
53
+ end
54
+
55
+ @rb_method.signal_connect("toggled") {|imap_btn_active|
56
+
57
+ prepare_ui @next_btn
58
+ if imap_btn_active.active?
59
+ @stack.set_visible_child @imap_child
60
+ else
61
+ @stack.set_visible_child @gmail_child
62
+ end
63
+
64
+ }
65
+
66
+ @settings = settings
67
+
30
68
  @entry_email.set_text settings.username unless settings.username.nil?
31
69
  @entry_password.set_text settings.password unless settings.password.nil?
32
70
 
@@ -37,11 +75,25 @@ module NaviClientInstaller
37
75
 
38
76
  prepare_ui @next_btn
39
77
 
78
+ @google_btn.signal_connect "clicked" do |widget|
79
+ @user_holder.text = ""
80
+ authorize_user
81
+ url = authorization_url
82
+ system("open", url)
83
+ @settings.method = nil
84
+ @code_entry.visible = true
85
+ prepare_ui @next_btn
86
+ end
87
+
40
88
  @combo_text_provider.signal_connect "changed" do |widget|
41
89
  settings.provider = widget.active_iter.get_value(1)
42
90
  prepare_ui @next_btn
43
91
  end
44
92
 
93
+ @code_entry.signal_connect "changed" do |widget|
94
+ prepare_ui @next_btn
95
+ end
96
+
45
97
  @entry_password.signal_connect "changed" do |widget|
46
98
  settings.password= widget.text
47
99
  prepare_ui @next_btn
@@ -55,16 +107,77 @@ module NaviClientInstaller
55
107
  white = Gdk::RGBA::new(1, 1, 1, 1.0)
56
108
  @window_ui.override_background_color(:normal, white)
57
109
 
58
- apply_css @combo_text_provider, css_provider
110
+ apply_css @window_ui, css_provider
59
111
 
60
- label_top.style_context.add_provider(css_provider, Gtk::StyleProvider::PRIORITY_USER)
61
- @window_ui.style_context.add_provider(css_provider, Gtk::StyleProvider::PRIORITY_USER)
112
+ end
113
+
114
+ def authorize_user
115
+ application_root_path = File.expand_path('../../../', __dir__)
62
116
 
117
+ init File.join(application_root_path, 'resources', 'config', 'gmail_api_secret.json'), NaviClientInstaller::Settings::DEFAULT_STORAGE + "/bin/token.yaml", "user"
63
118
  end
64
119
 
65
120
  def prepare_ui(nxt_btn, prev_btn = nil)
66
- valid = !@entry_email.text.empty? && !@entry_password.text.empty? && !@combo_text_provider.active_iter.get_value(1).nil?
121
+ @error_ind.text = ""
122
+ valid = (!@entry_email.text.empty? && !@entry_password.text.empty? && !@combo_text_provider.active_iter.get_value(1).nil? && @rb_method.active?) ||
123
+ ((!@code_entry.text.empty? || @settings.method == "api") && !@rb_method.active?)
67
124
  nxt_btn&.set_sensitive valid
68
125
  end
126
+
127
+
128
+ def post_conditions
129
+ success = false
130
+ @next_btn.set_sensitive success
131
+
132
+ if @rb_method.active?
133
+
134
+ begin
135
+ imap = Net::IMAP.new @settings.provider, ssl: true, certs: nil, verify: false
136
+ imap.login @settings.username, @settings.password
137
+ @settings.method = "imap"
138
+ success = true
139
+ rescue
140
+ @error_ind.text = "Wrong email or password."
141
+ end
142
+
143
+ else
144
+ begin
145
+ if @settings.method == "api"
146
+ success = true
147
+ else
148
+ cred = authorize @code_entry.text, "user"
149
+
150
+ unless cred.nil?
151
+ user = get_user_info "me"
152
+ update_user user.email_address
153
+ @settings.username = user.email_address
154
+ @settings.password = nil
155
+ @settings.provider = nil
156
+ @settings.method = "api"
157
+ @code_entry.text = ""
158
+ @code_entry.visible = false
159
+ @user_holder.text = "Signed In as #{user.email_address}"
160
+ @google_btn.label = "Use another gmail"
161
+
162
+ success = true
163
+ end
164
+ end
165
+
166
+ rescue
167
+ @error_ind.text = "Authorization Code invalid or expired."
168
+ end
169
+
170
+ end
171
+
172
+ return success
173
+ end
174
+
175
+ def update_user user_email
176
+ file_path = NaviClientInstaller::Settings::DEFAULT_STORAGE + "/bin/token.yaml"
177
+ data = YAML.load_file file_path
178
+ newData = { user_email => data["user"] }
179
+ File.open(file_path, 'w') { |f| YAML.dump(newData, f) }
180
+ end
181
+
69
182
  end
70
183
  end
@@ -8,7 +8,7 @@ module NaviClientInstaller
8
8
 
9
9
  def initialize(values, btn_next = nil)
10
10
  builder = Gtk::Builder.new(:resource => '/com/navi/navi-client/ui/agreement.ui')
11
- agreementTextContent = Gio::Resources.lookup_data("/com/navi/navi-client/texts/agreement_main_content.html", 0)
11
+ agreementTextContent = Gio::Resources.lookup_data("/com/navi/navi-client/config/agreement_main_content.html", 0)
12
12
  css_provider = Gtk::CssProvider.new
13
13
  css_provider.load(:resource_path => '/com/navi/navi-client/css/main.css')
14
14
 
@@ -28,7 +28,7 @@ module NaviClientInstaller
28
28
  values&.agreement = button.active?
29
29
  }
30
30
 
31
- agreementTextContent = Gio::Resources.lookup_data("/com/navi/navi-client/texts/agreement_main_content.html", 0)
31
+ agreementTextContent = Gio::Resources.lookup_data("/com/navi/navi-client/config/agreement_main_content.html", 0)
32
32
 
33
33
 
34
34
  apply_css tv_agreement_main_content, css_provider
@@ -49,7 +49,7 @@ module NaviClientInstaller
49
49
 
50
50
  def load_env env = 'production'
51
51
  application_root_path = File.expand_path('../../../', __dir__)
52
- envs = YAML.load_file(File.join(application_root_path, 'resources', 'texts', 'env.yml'))[env]
52
+ envs = YAML.load_file(File.join(application_root_path, 'resources', 'config', 'env.yml'))[env]
53
53
  ENV['env'] = env
54
54
  envs.each {|k,v| ENV[k] = v }
55
55
  end
@@ -118,10 +118,13 @@ module NaviClientInstaller
118
118
 
119
119
  @step_account_configuration.next_btn.signal_connect 'clicked' do |button|
120
120
 
121
- @settings.save_settings
122
- main_stack.set_visible_child(@splash.window_ui)
123
- # @initiate_login.call
124
- @splash.initiate_signup @signup_gauss, @logout_control
121
+ if @step_account_configuration.post_conditions
122
+ @settings.save_settings
123
+ main_stack.set_visible_child(@splash.window_ui)
124
+ # @initiate_login.call
125
+ @splash.initiate_signup @signup_gauss, @logout_control
126
+ end
127
+
125
128
  end
126
129
 
127
130
  @step_account_configuration.cancel_btn&.signal_connect 'clicked' do |button|
@@ -153,6 +156,7 @@ module NaviClientInstaller
153
156
 
154
157
  if(resp && @settings.hash && resp.code == 200)
155
158
  @settings.logout = false
159
+ @settings.identifier = JSON.parse(Base64.decode64(resp["user"]))["resource_name"]
156
160
  @settings.save_settings
157
161
  @current_window = @runner_window
158
162
  @runner_window.set_token resp["access_token"]
@@ -219,6 +223,12 @@ module NaviClientInstaller
219
223
  if index == @steps.length
220
224
  initiate_splash
221
225
  elsif index < @steps.length && index > -1
226
+
227
+ if factor == 1 && defined? @current_window.post_conditions
228
+
229
+ return if !@current_window.post_conditions
230
+ end
231
+
222
232
  main_stack.set_transition_type(transistion_type)
223
233
  @current_window = @steps[index]
224
234
 
@@ -234,7 +244,7 @@ module NaviClientInstaller
234
244
  def recover_state
235
245
  if @settings.agreement != true
236
246
  @step_licence_agreement
237
- elsif @settings.username.nil? || @settings.password.nil? || @settings.provider.nil?
247
+ elsif @settings.method.nil? || ((@settings.username.nil? || @settings.password.nil? || @settings.provider.nil?) && @settings.method == "imap") || (@settings.username.nil? && @settings.method == "gmail")
238
248
  @step_account_configuration
239
249
  elsif @settings.location.nil? || @settings.download_path.nil?
240
250
  @step_destination_select
@@ -1,6 +1,7 @@
1
1
  require_relative '../../lib/common_functions'
2
2
  require 'navi_client'
3
3
  require 'httparty'
4
+ require 'base64'
4
5
 
5
6
  module NaviClientInstaller
6
7
  class NaviRunner
@@ -25,7 +26,7 @@ module NaviClientInstaller
25
26
  @settings = settings
26
27
  stop_naviai
27
28
 
28
- @email_display.label = settings&.username if settings&.username
29
+ @email_display.label = settings&.username ? settings&.username : ''
29
30
 
30
31
  image = builder.get_object("app_logo")
31
32
  image.set_from_resource('/com/navi/navi-client/images/navi-logo-small.png')
@@ -72,10 +73,12 @@ module NaviClientInstaller
72
73
  @action_logout.signal_connect("activate") do |_action, parameter|
73
74
  @settings.logout = true
74
75
  @settings.username = nil
76
+ @settings.method = nil
75
77
  @settings.password = nil
76
78
  @settings.hash = nil
77
79
  @settings.save_settings
78
80
  terminate_worker_thread
81
+ FileUtils.rm_f(NaviClientInstaller::Settings::GMAIL_TOKEN_PATH) if File.file?(NaviClientInstaller::Settings::GMAIL_TOKEN_PATH)
79
82
  logout_control.call
80
83
  @action_logout.set_enabled false
81
84
  end
@@ -87,8 +90,10 @@ module NaviClientInstaller
87
90
  @settings.username = nil
88
91
  @settings.password = nil
89
92
  @settings.hash = nil
93
+ @settings.method = nil
90
94
  @settings.save_settings
91
95
  terminate_worker_thread
96
+ FileUtils.rm_f(NaviClientInstaller::Settings::GMAIL_TOKEN_PATH) if File.file?(NaviClientInstaller::Settings::GMAIL_TOKEN_PATH)
92
97
  logout_control.call
93
98
  @action_logout.set_enabled false
94
99
  end
@@ -104,7 +109,7 @@ module NaviClientInstaller
104
109
  @spinner_indicator.active = true
105
110
  @refresh_btn.sensitive = true
106
111
  @stop_btn.sensitive = true
107
- start_fetching
112
+ start_fetchings
108
113
  end
109
114
 
110
115
  end
@@ -128,7 +133,7 @@ module NaviClientInstaller
128
133
 
129
134
  def start_fetching
130
135
 
131
- @client = NaviClient::Local.new
136
+ @client = NaviClient::Local.new ""
132
137
  @client.set_token "Token: #{@token}##{@settings.username}"
133
138
  @logger = CustomLogger.new @log_display.buffer, @iter_log, @client.logger
134
139
  @client.override_logger @logger
@@ -254,7 +259,7 @@ module NaviClientInstaller
254
259
  end
255
260
 
256
261
  @notifier.label = "Waiting for the new emails.."
257
- @client.idle_loop(@imap, ['UNSEEN'], folder, @settings.provider, @settings.username, @settings.password, callback, naviai_control)
262
+ @client.idle_loop(['UNSEEN'], folder, @settings.provider, @settings.username, @settings.password, callback, naviai_control)
258
263
 
259
264
  else
260
265
  @logger.debug "Failed to connect with your email. Please recheck the configuration."
@@ -267,6 +272,155 @@ module NaviClientInstaller
267
272
  end
268
273
 
269
274
 
275
+ def start_fetchings
276
+
277
+ @logger = CustomLogger.new @log_display.buffer, @iter_log
278
+ @logger.debug "Running the process.."
279
+ @logger.debug "Initializing the client."
280
+
281
+ begin
282
+
283
+ if @settings.method == "imap"
284
+ @client = NaviClient::ImapApi.new @settings.method
285
+ @logger.set_custom_logger @client.logger
286
+ @client.override_logger @logger
287
+ @controller = @client.imap_connection(@settings.provider, @settings.username, @settings.password, false)
288
+ @controller = nil unless @client.errors.nil?
289
+ else
290
+ @client = NaviClient::GmailApi.new @settings.method
291
+ @logger.set_custom_logger @client.logger
292
+ @client.override_logger @logger
293
+ @controller = @client.init(NaviClientInstaller::Settings::GMAIL_CLIENT_PATH, NaviClientInstaller::Settings::GMAIL_TOKEN_PATH, @settings.username)
294
+ end
295
+
296
+
297
+ #logging to loggly
298
+ logToLoggly = true
299
+ if logToLoggly
300
+ @client.setupLoggly("navi-client")
301
+ end
302
+
303
+ @client.set_token "Token: #{@token}##{@settings.username}"
304
+
305
+ unless @controller.nil?
306
+ filenames = []
307
+ dateStarted = DateTime.now
308
+ @logger.debug "Syncing started at #{dateStarted}"
309
+
310
+ sendEmailCompletedEvent = true
311
+ folder = "INBOX"
312
+
313
+ @notifier.label = "Checking emails.."
314
+ @logger.debug "Looking up your emails.."
315
+ @client.init_messages folder
316
+
317
+ downloaded_mails = @client.downloaded
318
+ @email_indicator.label = "#{downloaded_mails}/#{@client.total_emails}"
319
+ @client.logToLoggly({event:"EMAIL_SYNC_STARTED", env: ENV['env'], storage: "local", email: @settings.username, total_email: total, already_downloaded: downloaded})
320
+
321
+ @notifier.label = "Starting up.."
322
+ @logger.debug "Starting the system"
323
+ start_naviai
324
+ @notifier.label = "Checking for previous failures.."
325
+ @logger.debug "Parsing previously fetched emails"
326
+ @client.parse_prev_failed_emails
327
+ @logger.debug "Complete. OK..."
328
+
329
+ @notifier.label = "Fetching emails..."
330
+ @logger.debug "Fetching emails"
331
+ stamp = (Time.now.to_f * 1000).to_s
332
+ @client.fetch_emails do |email_content, index, isLast, id|
333
+
334
+ @logger.debug "processing email##{index + 1} with id #{id}"
335
+ filenames << @client.process_email(email_content, id, stamp)
336
+ downloaded_mails+=1
337
+ downloaded_mails = [downloaded_mails, @client.total_emails].min
338
+ @email_indicator.label = "#{downloaded_mails}/#{@client.total_emails}"
339
+
340
+ @logger.debug "processing Complete. OK..."
341
+
342
+ if filenames.size == 50 || isLast
343
+ sendEmailCompletedEvent = !sendEmailCompletedEvent
344
+
345
+ @logger.debug "Sending Bulk Request for #{isLast ? filenames.size : 50} emails to parser."
346
+ @notifier.label = "Parsing the emails..."
347
+
348
+ start_naviai
349
+ @client.send_request(filenames, stamp, is_last: isLast)
350
+
351
+
352
+ @logger.debug "Complete Ok... continue email sync"
353
+ @notifier.label = "Fetching emails..."
354
+ # verify
355
+ filenames = []
356
+ if sendEmailCompletedEvent && !isLast
357
+ @client.logToLoggly({event:"EMAIL_SYNC_IN_PROGRESS", env: ENV['env'], storage: "local", email: @settings.username, emailCount: index + 1})
358
+ elsif isLast
359
+
360
+ dateEnded = DateTime.now
361
+ totalTimeInSecs = (dateEnded - dateStarted) * 24 * 60 * 60
362
+
363
+ @client.logToLoggly({event:"EMAIL_SYNC_FINISHED", env: ENV['env'], storage: "local", email: @settings.username, emailCount: index + 1, time_elapsed: totalTimeInSecs.to_f.round(2), total_email: @client.total_emails, already_downloaded: @client.downloaded})
364
+ @logger.debug "#{index + 1} emails from the #{folder} has been downloaded. OK"
365
+ @logger.debug "started: #{dateStarted.to_s} , ended: #{dateEnded.to_s}, Total time: #{totalTimeInSecs.to_f.round(2)} secs"
366
+ end
367
+
368
+ stamp = (Time.now.to_f * 1000).to_s
369
+
370
+ end
371
+
372
+ end
373
+
374
+ @logger.debug "All emails sync and parse completed."
375
+
376
+ if @settings.method == "imap"
377
+ @notifier.label = "Finalizing..."
378
+ callback = Proc.new do |mail, index, id, complete|
379
+ # complete = args.first || false
380
+
381
+ if complete
382
+ downloaded+=1
383
+ @logger.debug "processing Complete. OK..."
384
+ else
385
+ @notifier.label = "Fetching emails..."
386
+ @logger.debug "processing email##{index + 1} with id #{mail.__id__.to_s} and uuid #{id}"
387
+ total+=1
388
+ end
389
+ downloaded = [downloaded, total].min
390
+ @email_indicator.label = "#{downloaded}/#{total}"
391
+
392
+ end
393
+
394
+ naviai_control = Proc.new do |start|
395
+ if start
396
+ @notifier.label = "Parsing the emails..."
397
+ start_naviai
398
+ else
399
+ # stop_naviai
400
+ @logger.debug "Waiting new mails (IDLE loop)..."
401
+ @notifier.label = "Waiting for the new emails.."
402
+ end
403
+ end
404
+
405
+ @notifier.label = "Waiting for the new emails.."
406
+ @client.idle_loop(['UNSEEN'], folder, @settings.provider, @settings.username, @settings.password, callback, naviai_control)
407
+ else
408
+ @notifier.label = "Done.."
409
+ @spinner_indicator.active = false
410
+ end
411
+
412
+ else
413
+ @logger.debug "Failed to connect with your email. Please recheck the configuration."
414
+ end
415
+
416
+ rescue => e
417
+ @logger.debug "#{e.message}"
418
+ restart_process false
419
+ end
420
+
421
+ end
422
+
423
+
270
424
  def prepare_ui btn_next, btn_back
271
425
 
272
426
  end
@@ -324,24 +478,28 @@ class CustomLogger
324
478
  @iter = iterator
325
479
  end
326
480
 
481
+ def set_custom_logger logger
482
+ @logger_custom = logger
483
+ end
484
+
327
485
  def info msg
328
486
  # p msg
329
487
  @buffer.insert(@iter, "#{msg}", :tags => %w(small_gap_below normal-size))
330
488
  @buffer.insert_markup(@iter, "\n")
331
- @logger_custom.info msg
489
+ @logger_custom&.info msg
332
490
  end
333
491
 
334
492
  def debug msg
335
493
  # p msg
336
494
  @buffer.insert(@iter, "#{msg}", :tags => %w(small_gap_below normal-size))
337
495
  @buffer.insert_markup(@iter, "\n")
338
- @logger_custom.debug msg
496
+ @logger_custom&.debug msg
339
497
  end
340
498
 
341
499
  def error msg
342
500
  # p msg
343
501
  @buffer.insert(@iter, "#{msg}", :tags => %w(small_gap_below normal-size))
344
502
  @buffer.insert_markup(@iter, "\n")
345
- @logger_custom.error msg
503
+ @logger_custom&.error msg
346
504
  end
347
505
  end
@@ -31,16 +31,30 @@ module NaviClientInstaller
31
31
 
32
32
  end
33
33
 
34
+ def get_current_user url: "#{ENV['api_url']}/get_current_user", token:
35
+ HTTParty.get(url, headers: {"Authorization": token})
36
+ end
37
+
34
38
  def validate_email(email, pass)
35
39
  # need to remove this hardcoded code
36
40
  begin
37
- HTTParty.post(ENV['api_url'] + "/oauth/token",
41
+ response = HTTParty.post(ENV['api_url'] + "/oauth/token",
38
42
  body: {
39
43
  email: email, # get from sso_web application
40
44
  password: pass,
41
45
  grant_type: "password"
42
46
  }
43
47
  )
48
+
49
+ if response.code == 200
50
+ user_resp = get_current_user token: "Token: #{response["access_token"]}##{email}"
51
+
52
+ return user_resp
53
+ else
54
+ return nil
55
+ end
56
+
57
+
44
58
  rescue
45
59
  false
46
60
  end
@@ -81,8 +95,11 @@ module NaviClientInstaller
81
95
 
82
96
  if response && response.code == 200
83
97
  error_label.set_opacity 0
98
+
84
99
  @settings.hash = password_entry.text
100
+ @settings.identifier = JSON.parse(Base64.decode64(response["user"]))["resource_name"]
85
101
  @settings.save_settings
102
+
86
103
  callback.call response["access_token"]
87
104
  else
88
105
  error_label.set_opacity 1
@@ -105,6 +122,7 @@ module NaviClientInstaller
105
122
  if response && response.code == 200
106
123
  error_label.set_opacity 0
107
124
  @settings.hash = widget.text
125
+ @settings.identifier = JSON.parse(Base64.decode64(response["user"]))["resource_name"]
108
126
  @settings.save_settings
109
127
  callback.call response["access_token"]
110
128
  else
data/bin/test.rb ADDED
@@ -0,0 +1,90 @@
1
+ require 'google/apis/gmail_v1'
2
+ require 'googleauth'
3
+ require 'googleauth/stores/file_token_store'
4
+ require 'fileutils'
5
+
6
+ OOB_URI = 'urn:ietf:wg:oauth:2.0:oob'.freeze
7
+ APPLICATION_NAME = 'Gmail Api Sync'.freeze
8
+ CLIENT_SECRETS_PATH = 'client_secret.json'.freeze
9
+ CREDENTIALS_PATH = 'token.yaml'.freeze
10
+ SCOPE = Google::Apis::GmailV1::AUTH_GMAIL_READONLY
11
+
12
+ ##
13
+ # Ensure valid credentials, either by restoring from the saved credentials
14
+ # files or intitiating an OAuth2 authorization. If authorization is required,
15
+ # the user's default browser will be launched to approve the request.
16
+ #
17
+ # @return [Google::Auth::UserRefreshCredentials] OAuth2 credentials
18
+ def authorize
19
+ client_id = Google::Auth::ClientId.from_file(CLIENT_SECRETS_PATH)
20
+ token_store = Google::Auth::Stores::FileTokenStore.new(file: CREDENTIALS_PATH)
21
+ authorizer = Google::Auth::UserAuthorizer.new(client_id, SCOPE, token_store)
22
+ user_id = 'woot'
23
+ credentials = authorizer.get_credentials(user_id)
24
+ if credentials.nil?
25
+ url = authorizer.get_authorization_url(base_url: OOB_URI)
26
+ puts 'Open the following URL in the browser and enter the ' \
27
+ 'resulting code after authorization:\n' + url
28
+ code = gets
29
+ credentials = authorizer.get_and_store_credentials_from_code(
30
+ user_id: user_id, code: code, base_url: OOB_URI
31
+ )
32
+ end
33
+ credentials
34
+ end
35
+
36
+
37
+ # Initialize the API
38
+ service = Google::Apis::GmailV1::GmailService.new
39
+ service.client_options.application_name = APPLICATION_NAME
40
+ service.authorization = authorize
41
+
42
+ # Show the user's labels
43
+ user_id = 'me'
44
+ result = service.list_user_labels(user_id)
45
+
46
+
47
+ messages = []
48
+ next_page = nil
49
+ begin
50
+ result = service.list_user_messages('me', max_results: 1, page_token: next_page, label_ids: ["INBOX"])
51
+ messages += result.messages
52
+ p messages.length
53
+ break if messages.size >= 1
54
+ next_page = result.next_page_token
55
+ end while next_page
56
+
57
+ payload = nil
58
+ messages.each { |e|
59
+ puts "- #{e.id}"
60
+ result = service.get_user_message('me', e.id)
61
+ payload = result.payload
62
+
63
+ }
64
+ headers = payload.headers
65
+ date = headers.any? { |h| h.name == 'Date' } ? headers.find { |h| h.name == 'Date' }.value : ''
66
+ from = headers.any? { |h| h.name == 'From' } ? headers.find { |h| h.name == 'From' }.value : ''
67
+ to = headers.any? { |h| h.name == 'To' } ? headers.find { |h| h.name == 'To' }.value : ''
68
+ subject = headers.any? { |h| h.name == 'Subject' } ? headers.find { |h| h.name == 'Subject' }.value : ''
69
+ cc = headers.any? { |h| h.name == 'Cc' } ? headers.find { |h| h.name == 'Cc' }.value : ''
70
+ p date
71
+ p from
72
+ p to
73
+ p subject
74
+ p cc
75
+
76
+ body = payload.body.data
77
+ if body.nil? && payload.parts.any?
78
+ body = payload.parts.map { |part| part.body.data }.join
79
+ end
80
+
81
+ p date
82
+ p DateTime.parse date
83
+
84
+ p payload.parts[0].mime_type
85
+ p payload.parts[1].mime_type
86
+
87
+
88
+
89
+ # system("open", "http://stackoverflow.com/")
90
+
data/gresource.bin CHANGED
Binary file
@@ -0,0 +1,14 @@
1
+ {
2
+ "installed": {
3
+ "client_id": "1088148226633-9utdj3h84e688huqjhkm5to7voob7mvj.apps.googleusercontent.com",
4
+ "project_id": "daring-research-206204",
5
+ "auth_uri": "https://accounts.google.com/o/oauth2/auth",
6
+ "token_uri": "https://accounts.google.com/o/oauth2/token",
7
+ "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
8
+ "client_secret": "iqED0l3KDUaB9xNSNp2YiysY",
9
+ "redirect_uris": [
10
+ "urn:ietf:wg:oauth:2.0:oob",
11
+ "http://localhost"
12
+ ]
13
+ }
14
+ }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: navilocal
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.4
4
+ version: 1.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Msanjib
@@ -39,19 +39,19 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: 0.11.3
41
41
  - !ruby/object:Gem::Dependency
42
- name: navi_client
42
+ name: navi-email-sync
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: 1.4.7
47
+ version: 0.0.1
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: 1.4.7
54
+ version: 0.0.1
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: httparty
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -86,8 +86,10 @@ files:
86
86
  - application/ui/navi/prefs.rb
87
87
  - application/ui/navi/splash.rb
88
88
  - bin/navilocal
89
+ - bin/test.rb
89
90
  - gresource.bin
90
- - resources/texts/env.yml
91
+ - resources/config/env.yml
92
+ - resources/config/gmail_api_secret.json
91
93
  homepage: http://rubygems.org/gems/navilocal
92
94
  licenses:
93
95
  - NAVI
File without changes