Improve stability.

This commit is contained in:
Glenn Y. Rolland 2015-11-24 19:05:52 +01:00
parent f127dfd276
commit 6e3dd9e38a
3 changed files with 74 additions and 51 deletions

View file

@ -2,8 +2,10 @@ source "https://rubygems.org"
gem 'mail' gem 'mail'
gem 'json' gem 'json'
#gem 'bson'
gem 'thor' gem 'thor'
gem 'pry' gem 'pry'
gem 'pry-rescue'
# manage remote API/web page # manage remote API/web page
gem 'mechanize' gem 'mechanize'

View file

@ -10,8 +10,9 @@ GEM
http-cookie (1.0.2) http-cookie (1.0.2)
domain_name (~> 0.5) domain_name (~> 0.5)
i18n (0.6.0) i18n (0.6.0)
json (1.6.2) interception (0.5)
json (1.6.2-java) json (1.8.3)
json (1.8.3-java)
mail (2.3.0) mail (2.3.0)
i18n (>= 0.4.0) i18n (>= 0.4.0)
mime-types (~> 1.16) mime-types (~> 1.16)
@ -44,6 +45,9 @@ GEM
method_source (~> 0.8.1) method_source (~> 0.8.1)
slop (~> 3.4) slop (~> 3.4)
spoon (~> 0.0) spoon (~> 0.0)
pry-rescue (1.4.2)
interception (>= 0.5)
pry
slop (3.5.0) slop (3.5.0)
spoon (0.0.4) spoon (0.0.4)
ffi ffi
@ -68,4 +72,8 @@ DEPENDENCIES
mail mail
mechanize mechanize
pry pry
pry-rescue
thor thor
BUNDLED WITH
1.10.6

View file

@ -1,15 +1,20 @@
#!/usr/bin/env ruby #!/usr/bin/env ruby
require 'bundler/setup' #require 'bundler/setup'
Bundler.require #Bundler.require
require 'pry'
require 'zlib' require 'zlib'
require 'net/imap' require 'net/imap'
require 'pp' require 'pp'
require 'pry'
require 'mechanize' require 'mechanize'
require 'yaml' require 'yaml'
require 'hash_validator' require 'hash_validator'
require 'uri' require 'uri'
require 'thor'
require 'json'
require 'mail'
require 'colorize'
#Net::IMAP.debug = true #Net::IMAP.debug = true
@ -64,13 +69,13 @@ module Epafi
def connect! def connect!
@browser.get(@config[:crm][:baseurl] + CRM_LOGIN_URL) do |page| @browser.get(@config[:crm][:baseurl] + CRM_LOGIN_URL) do |page|
my_page = page.form_with(:action => '/authentication') do |f| page.form_with(action: '/authentication') do |f|
f['authentication[username]'] = @config[:crm][:login] f['authentication[username]'] = @config[:crm][:login]
f['authentication[password]'] = @config[:crm][:password] f['authentication[password]'] = @config[:crm][:password]
end.click_button end.click_button
end end
rescue Mechanize::ResponseCodeError => e rescue Mechanize::ResponseCodeError
raise "Authentication error. Verify your credentials." raise "Authentication error. Verify your credentials."
end end
@ -153,10 +158,11 @@ module Epafi
def connect! def connect!
@imap = Net::IMAP.new( @imap = Net::IMAP.new(
@config[:imap][:server], @config[:imap][:server],
:ssl => {:verify_mode => OpenSSL::SSL::VERIFY_NONE}, ssl: {verify_mode: OpenSSL::SSL::VERIFY_NONE},
:port => 993 port: 993
) )
@imap.login(@config[:imap][:login], @config[:imap][:password]) @imap.login(@config[:imap][:login], @config[:imap][:password])
#@imap.select(SOURCE_MAILBOX)
end end
def disconnect! def disconnect!
@ -173,9 +179,13 @@ module Epafi
emails = Set.new emails = Set.new
begin
emails.merge m.from emails.merge m.from
emails.merge m.to if m.to emails.merge [m.to].flatten if m.to
emails.merge m.cc if m.cc emails.merge [m.cc].flatten if m.cc
rescue => e
binding.pry
end
body_emails = Set.new body_emails = Set.new
m.body.parts.each do |part| m.body.parts.each do |part|
@ -252,7 +262,7 @@ module Epafi
system "formail < #{TMPMAIL_FILE}.2 > #{TMPMAIL_FILE}" system "formail < #{TMPMAIL_FILE}.2 > #{TMPMAIL_FILE}"
system "mutt -R -f #{TMPMAIL_FILE}" system "mutt -R -f #{TMPMAIL_FILE}"
end end
rescue Encoding::ConverterNotFoundError => e rescue Encoding::ConverterNotFoundError
STDERR.puts "ERROR: encoding problem in email. Unable to convert." STDERR.puts "ERROR: encoding problem in email. Unable to convert."
end end
end end
@ -262,33 +272,35 @@ module Epafi
def examine_all def examine_all
@imap.list('', '*').each do |mailbox| @imap.list('', '*').each do |mailbox|
puts "\nMAILBOX #{mailbox.name}" puts "\nMAILBOX #{mailbox.name}".yellow
next unless mailbox.name =~ /#{@config[:imap][:pattern]}/ next unless mailbox.name =~ /#{@config[:imap][:pattern]}/
@imap.examine mailbox.name @imap.examine mailbox.name
puts "Searching #{mailbox.name}" puts "Searching #{mailbox.name}"
messages_in_mailbox = @imap.responses['EXISTS'][0] messages_in_mailbox = @imap.responses['EXISTS'][0]
unless messages_in_mailbox if not messages_in_mailbox then
say "#{mailbox.name} does not have any messages" say "#{mailbox.name} does not have any messages"
next next
end end
@imap.select mailbox.name #GYR: TEST
ids = @imap.search('SINCE 1-Jan-2001') ids = @imap.search('SINCE 1-Jan-2001')
# NOT OR TO "@agilefant.org" CC "@agilefant.org"') # NOT OR TO "@agilefant.org" CC "@agilefant.org"')
if ids.empty? if ids.empty?
puts "\tFound no messages" puts "\tFound no messages"
else else
examine_message_list ids examine_message_list mailbox.name, ids
end end
end end
end end
def examine_message_list ids def examine_message_list mailbox_name, ids
ids.each do |id| ids.each do |id|
@imap.select mailbox_name #GYR: TEST
message = imap.fetch(id, [@saved_key])[0] message = imap.fetch(id, [@saved_key])[0]
examine_message message examine_message message
end end
rescue IOError => e rescue IOError
# re-connect and try again # re-connect and try again
connect! connect!
retry retry
@ -305,7 +317,7 @@ module Epafi
desc 'crawl', 'Crawls email to save mails' desc 'crawl', 'Crawls email to save mails'
def crawl def crawl
saved_info = [] #saved_info = []
parse_configuration parse_configuration
## Run application ## Run application
@ -351,3 +363,4 @@ module Epafi
end end
Epafi::Crawler.start Epafi::Crawler.start