class MailchainConnectorImap::Connector
Constants
- CONFIG_FILE
- LOG_FILE
- STORE_PATH
Attributes
config[R]
imap_conn[R]
mailchain_conn[R]
Public Class Methods
new()
click to toggle source
# File lib/mailchain_connector_imap.rb, line 22 def initialize parse_config_file @imap_conn = ConnectionImap.new(@config, CONFIG_FILE) @mailchain_conn = ConnectionMailchain.new(@config, CONFIG_FILE) @config_json = {} end
Public Instance Methods
check_or_create_config_file()
click to toggle source
Check for an existing config file, otherwise create a new one with minimum requirements to be parsed.
# File lib/mailchain_connector_imap.rb, line 120 def check_or_create_config_file (FileUtils.mkdir_p(STORE_PATH) unless File.exist?(CONFIG_FILE)) File.write(CONFIG_FILE, '{"imap": {}, "mailchain": {}}') unless File.exist?(CONFIG_FILE) end
log_to_file(message)
click to toggle source
Output message to the log file, adding the date and time `message` (String): the message text
# File lib/mailchain_connector_imap.rb, line 182 def log_to_file(message) open(LOG_FILE, 'a') do |f| f << "\n" f << "#{Time.now} #{message}" end end
missing_config_file()
click to toggle source
Handle missing config file error
# File lib/mailchain_connector_imap.rb, line 35 def missing_config_file puts "Invalid or missing config.\n" \ 'Run `mailchain_connector_imap --configure`' end
op_configure()
click to toggle source
Runs the configuration wizards and tests connections
# File lib/mailchain_connector_imap.rb, line 78 def op_configure @imap_conn.configure_and_connect @mailchain_conn.configure_and_connect exit end
op_print_configuration()
click to toggle source
Outputs the configuration to screen
# File lib/mailchain_connector_imap.rb, line 99 def op_print_configuration # TODO: fix this ConnectionConfigurationImap.new(@config).print_settings puts "\n" # TODO: fix this ConnectionConfigurationMailchain.new(@config).print_settings end
op_test_connection()
click to toggle source
Run connection tests
# File lib/mailchain_connector_imap.rb, line 85 def op_test_connection begin @imap_conn.test_connection rescue StandardError => e puts "IMAP error: #{e}" end begin @mailchain_conn.test_connection rescue StandardError => e puts "API error: #{e}" end end
parse_config_file()
click to toggle source
Parse the config file as JSON
# File lib/mailchain_connector_imap.rb, line 108 def parse_config_file check_or_create_config_file config_json = File.read(CONFIG_FILE) @config = JSON.parse(config_json) true rescue StandardError => e puts "Error parsing configuration: #{e}" missing_config_file exit end
run_options_parse()
click to toggle source
Run the script and parse input arguments
# File lib/mailchain_connector_imap.rb, line 41 def run_options_parse OptionParser.new do |opts| ARGV << '-r' if ARGV.empty? opts.banner = 'Usage: mailchain_connector_imap [options]' opts.on('-r', '--run', 'Run and sync messages') do unless valid_config missing_config_file exit end sync_messages end opts.on('-c', '--configure', 'Configure connector settings') do op_configure exit end opts.on('-t', '--test-connection', 'Test connection to IMAP server and Mailchain API') do op_test_connection exit end opts.on('-p', '--print-config', 'Print connector settings to screen') do op_print_configuration exit end opts.on_tail('-h', '--help', 'Show this message') do puts opts exit end end.parse! end
start()
click to toggle source
# File lib/mailchain_connector_imap.rb, line 29 def start # Run OptionsParser to interpret user input run_options_parse end
sync_messages()
click to toggle source
sync_messages
calls ConnectionMailchain.messages_by_network
, before setting a timer to run again. Minimum timer interval is 60 seconds. It logs at the beginning and end of each polling interval.
# File lib/mailchain_connector_imap.rb, line 128 def sync_messages if @imap_conn.connect puts 'Connected to IMAP' if @mailchain_conn.test_connection(true) puts 'Connected to Mailchain client' loop do if @imap_conn.connect && @mailchain_conn.test_connection(true) interval = @config['mailchain']['interval'].to_i > 60 ? @config['mailchain']['interval'].to_i : 60 log_to_file('Checking messages') @mailchain_conn.addresses_by_network.each do |abn| protocol = abn['protocol'] network = abn['network'] # TODO: - Simplify this: addr_with_messages = @mailchain_conn.messages_by_network(abn) addr_with_messages.each do |addr_msg| addr = addr_msg[0] msg = addr_msg[1] converted_messages = @mailchain_conn.convert_messages(msg) converted_messages.each do |cm| @imap_conn.append_message(protocol, network, addr, cm['message'], cm['message_id'], nil, cm['message_date']) end end end end log_to_file('Done') sleep interval end end end rescue StandardError => e log_to_file("Error: #{e}") puts "Error: #{e}" end
valid_config()
click to toggle source
Checks values are present for config options Returns true if valid; false if invalid
# File lib/mailchain_connector_imap.rb, line 165 def valid_config [ @config['imap']['server'], @config['imap']['username'], @config['imap']['port'], @config['imap']['ssl'], @config['mailchain']['hostname'], @config['mailchain']['ssl'], @config['mailchain']['port'], @config['mailchain']['folders'], @config['mailchain']['mainnet_to_inbox'], @config['mailchain']['interval'] ].none? { |e| e.to_s.empty? } end