Module: Client
- Included in:
- NaviClient::Cloud, NaviClient::Local
- Defined in:
- lib/client.rb
Overview
This module provides the common functionality that will be needed for local and cloud module.
Instance Method Summary collapse
- #depcrecated_encrypt(data) ⇒ Object
- #encrypt(data) ⇒ Object
- #errors ⇒ Object
-
#imap_connection(server, username, password, exitOnFail = true) ⇒ Object
imap_connection.
- #logger ⇒ Object
- #logToLoggly(messageBody) ⇒ Object
-
#process_email(mail, uid) ⇒ Object
Process each email downloaded from imap-server and creates meta file that will be sent over to the navi-ai service.
-
#retrieve_emails(imap, search_condition, folder, &process_email_block) ⇒ Object
retrieve_emails.
- #setupLoggly(tag) ⇒ Object
-
#shutdown(imap) ⇒ Object
If the gem is being used for local-lockbox mode, after fetching all emails, the process will go idle mode.
- #time_now ⇒ Object
Instance Method Details
#depcrecated_encrypt(data) ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/client.rb', line 171 def depcrecated_encrypt(data) cipher = OpenSSL::Cipher::AES.new(256, :CFB) cipher.encrypt yml_config = config key_iv_exists = (yml_config['key'] && yml_config['iv']) ? (!yml_config['key'].empty? && !yml_config['iv'].empty? ) : false if key_iv_exists # this condition must be true for cloud version cipher.key = Base64.decode64(File.read(yml_config['key'])) cipher.iv = Base64.decode64(File.read(yml_config['iv'])) else cipher.key = key = cipher.random_key cipher.iv = iv = cipher.random_iv key_path, iv_path = save_aes_key_iv(key, iv) yml_config['key'] = key_path yml_config['iv'] = iv_path update_config(yml_config) end encrypted = cipher.update(data) Base64.encode64(encrypted) end |
#encrypt(data) ⇒ Object
167 168 169 |
# File 'lib/client.rb', line 167 def encrypt(data) Base64.encode64(data) end |
#errors ⇒ Object
27 28 29 |
# File 'lib/client.rb', line 27 def errors @errors end |
#imap_connection(server, username, password, exitOnFail = true) ⇒ Object
imap_connection
connect the app with imap server
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/client.rb', line 36 def imap_connection(server, username, password, exitOnFail = true) # connect to IMAP server imap = Net::IMAP.new server, ssl: true, certs: nil, verify: false Net::IMAP.debug = @net_imap_debug # http://ruby-doc.org/stdlib-2.1.5/libdoc/net/imap/rdoc/Net/IMAP.html#method-i-capability capabilities = imap.capability @logger.debug("imap capabilities: #{capabilities.join(',')}") if @debug if @client_type == 'local' unless capabilities.include? "IDLE" || @debug @logger.info "'IDLE' IMAP capability not available in server: #{server}" imap.disconnect exit if exitOnFail end end begin # login imap.login username, password rescue Net::IMAP::NoResponseError => e # mostly due to credentials error @errors = {exception: e} rescue Net::IMAP::BadResponseError => e @errors = {exception: e} rescue @errors = {exception: e} end # return IMAP connection handler imap end |
#logger ⇒ Object
23 24 25 |
# File 'lib/client.rb', line 23 def logger @logger end |
#logToLoggly(messageBody) ⇒ Object
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/client.rb', line 215 def logToLoggly() if(@logglyTag) begin HTTParty.post("http://logs-01.loggly.com/bulk/0d67f93b-6568-4b00-9eca-97d0ea0bd5a1/tag/#{@logglyTag}/", body: .to_json, headers: { 'Content-Type' => 'application/json' } ) rescue true end else if @debug && !@logger.nil? @logger.info "Logging to Loggly disabled" @logger.info end end end |
#process_email(mail, uid) ⇒ Object
Process each email downloaded from imap-server and creates meta file that will be sent over to the navi-ai service. Meta will content information like: [‘from’, ‘to’, ‘cc’, …, ‘body_url’]. This information is then used by navi-ai to parse the body content.
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/client.rb', line 134 def process_email(mail, uid) = Hash.new custom_uid = (Time.now.to_f * 1000).to_s + "_" + mail.__id__.to_s invalid_to_email = mail.to.nil? || !mail.to.is_a?(Array) invalid_cc_email = mail.cc.nil? || !mail.cc.is_a?(Array) unless mail.from.nil? if defined? mail.from.first ["from"] = mail.from.first else ["from"] = mail.from end end ["to"] = invalid_to_email ? mail.to : mail.to.join(";") ["cc"] = mail.cc.join(";") unless invalid_cc_email ["subject"] = mail.subject ["date"] = mail.date.to_s if mail.multipart? for i in 0...mail.parts.length m = download(mail.parts[i], custom_uid) .merge!(m) unless m.nil? end else m = download(mail, custom_uid) .merge!(m) unless m.nil? end save(, "meta/#{uid.to_s + '_' + custom_uid}") end |
#retrieve_emails(imap, search_condition, folder, &process_email_block) ⇒ Object
retrieve_emails
retrieve any mail from a folder, followin specified serach condition for any mail retrieved call a specified block
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/client.rb', line 77 def retrieve_emails(imap, search_condition, folder, &process_email_block) # examine will read email and sets flags unread # https://stackoverflow.com/questions/16516464/read-gmail-xoauth-mails-without-marking-it-read imap.examine folder = imap.uid_search(search_condition) = @download_path + 'meta/' = getMessageUUIds() = - if @debug if .empty? @logger.info "No new emails found..." elsif .any? @logger.info "Found emails saved in your #{@client_type}. Downloading only #{message_ids.count} new emails..." else @logger.info "Downloading #{message_ids.count} emails." end end .each_with_index do |, i| begin # fetch all the email contents data = imap.uid_fetch(, "RFC822") data.each do |d| msg = d.attr['RFC822'] # instantiate a Mail object to avoid further IMAP parameters nightmares mail = Mail.read_from_string msg # call the block with mail object as param process_email_block.call mail, i, i == .length-1, # mark as read if @mark_as_read imap.store(, "+FLAGS", [:Seen]) end end rescue => e unless logger.nil? logger.info "Issue processing email for uuid ##{message_id}, #{e.message}" end logToLoggly({event:"EMAIL_SYNC_FAILED", env: @env, storage: @client_type, email: @current_user_email, uuid: , error: e.}) raise e end end end |
#setupLoggly(tag) ⇒ Object
235 236 237 |
# File 'lib/client.rb', line 235 def setupLoggly(tag) @logglyTag = tag end |
#shutdown(imap) ⇒ Object
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
206 207 208 209 210 211 212 213 |
# File 'lib/client.rb', line 206 def shutdown(imap) imap.idle_done imap.logout unless imap.disconnected? imap.disconnect @logger.info "#{$0} has ended (crowd applauds)" exit 0 end |
#time_now ⇒ Object
199 200 201 |
# File 'lib/client.rb', line 199 def time_now Time.now.utc.iso8601(3) end |