Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make PostOffice more idiomatic to use as a library #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions bin/post_office
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ $LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
require 'optparse'
require 'logger'
require 'thread'
require 'smtp_server.rb'
require 'pop_server.rb'
require 'config_file.rb'
require 'post_office/smtp_server'
require 'post_office/pop_server'
require 'post_office/config_file'

options = ConfigFile.detect.read

Expand Down Expand Up @@ -57,30 +57,30 @@ optparse.parse!
# OS X Startup Item
#
if options[:startup_item]
require 'startup_item.rb'
require 'post_office/startup_item'
case options[:startup_item]
when :install then StartupItem.install
when :remove then StartupItem.remove
when :install then PostOffice::StartupItem.install
when :remove then PostOffice::StartupItem.remove
end
exit
end

#
# Create our logger
#
$log = Logger.new(options[:logfile] || STDOUT)
$log.level = options[:verbose] ? Logger::DEBUG : Logger::INFO
$log.datetime_format = "%H:%M:%S"
log = Logger.new(options[:logfile] || STDOUT)
log.level = options[:verbose] ? Logger::DEBUG : Logger::INFO
log.datetime_format = "%H:%M:%S"

begin
smtp_server = Thread.new{ SMTPServer.new(options[:smtp_port]) }
pop_server = Thread.new{ POPServer.new(options[:pop3_port]) }
smtp_server = Thread.new{ PostOffice::SMTPServer.new(options[:smtp_port], logger: log).run }
pop_server = Thread.new{ PostOffice::POPServer.new(options[:pop3_port], logger: log) }

smtp_server.join
pop_server.join
rescue Interrupt
$log.info "Interrupt..."
log.info "Interrupt..."
rescue Errno::EACCES
$log.error "I need root access to open ports #{options[:smtp_port]} and / or #{options[:pop3_port]}. Please sudo #{__FILE__}"
log.error "I need root access to open ports #{options[:smtp_port]} and / or #{options[:pop3_port]}. Please sudo #{__FILE__}"
end

24 changes: 0 additions & 24 deletions lib/config_file.rb

This file was deleted.

75 changes: 0 additions & 75 deletions lib/generic_server.rb

This file was deleted.

182 changes: 0 additions & 182 deletions lib/pop_server.rb

This file was deleted.

26 changes: 26 additions & 0 deletions lib/post_office/config_file.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'json'

module PostOffice
class ConfigFile
USER_CONFIG_DIR = ENV.fetch('XDG_CONFIG_HOME', ENV['HOME'] + '/.config')
SYSTEM_CONFIG_DIR = '/etc'
CONFIG_DIRS = [USER_CONFIG_DIR, SYSTEM_CONFIG_DIR]
attr_reader :filename

def initialize(filename)
@filename = filename
end

def self.detect
filename =
CONFIG_DIRS.map { |dir| "#{dir}/post_office/config.json" }
.detect { |file| File.exist? file }
new(filename)
end

def read
return {} if @filename.nil?
JSON.parse(File.read(@filename), symbolize_names: true)
end
end
end
Loading