Add support for multiple backends
parent
f2e78c476a
commit
31932325f9
|
@ -1,9 +1,11 @@
|
|||
require 'syslog/logger'
|
||||
|
||||
require 'smsd/version'
|
||||
require 'smsd/sms'
|
||||
require 'smsd/cli'
|
||||
require 'smsd/cli/options'
|
||||
require 'smsd/answering_machine'
|
||||
require 'smsd/answering_machine/action'
|
||||
require 'smsd/util/multi_io'
|
||||
require 'smsd/util/sms'
|
||||
|
||||
require 'smsd/backend/biju'
|
||||
|
|
|
@ -0,0 +1,69 @@
|
|||
module SMSd
|
||||
module Backend
|
||||
class Biju
|
||||
attr_accessor :modem
|
||||
|
||||
def initialize(modem, options)
|
||||
@modem = Biju::Hayes.new(modem, options)
|
||||
end
|
||||
|
||||
def close
|
||||
modem.close
|
||||
end
|
||||
|
||||
def messages
|
||||
modem.messages.map do |sms|
|
||||
if sms.is_a?(Biju::Sms)
|
||||
sms.to_smsd
|
||||
else
|
||||
modem.delete sms.id
|
||||
#logger.error sms.to_s
|
||||
|
||||
nil
|
||||
end
|
||||
end.compact
|
||||
end
|
||||
|
||||
def send(sms)
|
||||
modem.send(sms.to_biju)
|
||||
end
|
||||
|
||||
def delete(sms)
|
||||
modem.delete(sms.id)
|
||||
end
|
||||
|
||||
def phone_numbers
|
||||
@phone_numbers ||= modem.phone_numbers
|
||||
rescue Biju::AT::CmeError
|
||||
[]
|
||||
end
|
||||
|
||||
def phone_number
|
||||
@phone_number ||= (!phone_numbers.empty? ? phone_numbers.first[:number] : nil)
|
||||
end
|
||||
|
||||
module Convert
|
||||
module Biju
|
||||
def to_smsd
|
||||
::SMSd::SMS.new(
|
||||
id: id,
|
||||
from: phone_number,
|
||||
message: message,
|
||||
datetime: datetime)
|
||||
end
|
||||
end
|
||||
|
||||
module SMSd
|
||||
def to_biju
|
||||
::Biju::Sms.new(
|
||||
phone_number: self.to,
|
||||
message: self.message.to_s[0, 160])
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Biju::Sms.include SMSd::Backend::Biju::Convert::Biju
|
||||
SMSd::SMS.include SMSd::Backend::Biju::Convert::SMSd
|
|
@ -0,0 +1,29 @@
|
|||
module SMSd
|
||||
module Backend
|
||||
class Gammu
|
||||
def initialize
|
||||
end
|
||||
|
||||
def close
|
||||
end
|
||||
|
||||
def message
|
||||
end
|
||||
|
||||
def send(sms)
|
||||
IO.popen("gammu-sms-inject TEXT #{sms.to}", 'w') do |cmd|
|
||||
cmd.write sms.message
|
||||
end
|
||||
end
|
||||
|
||||
def delete(sms)
|
||||
end
|
||||
|
||||
def phone_numbers
|
||||
end
|
||||
|
||||
def phone_number
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -5,7 +5,7 @@ require 'date'
|
|||
module SMSd
|
||||
class CLI
|
||||
attr_accessor :machine, :last_pong
|
||||
attr_reader :modem, :options, :logger, :running_since
|
||||
attr_reader :backend, :options, :logger, :running_since
|
||||
|
||||
def initialize(args = [], &block)
|
||||
@options = Options.parse(args)
|
||||
|
@ -14,7 +14,7 @@ module SMSd
|
|||
|
||||
init_logger
|
||||
redirect_output
|
||||
@modem = Biju::Hayes.new(options[:modem], pin: options[:pin])
|
||||
@backend = Backend::Biju.new(options[:modem], pin: options[:pin])
|
||||
rescue Errno::ENOENT => e
|
||||
logger.warn e.message
|
||||
exit
|
||||
|
@ -32,17 +32,11 @@ module SMSd
|
|||
sleep 5
|
||||
break if @terminate
|
||||
|
||||
modem.messages.each do |sms|
|
||||
if sms.is_a?(Biju::Sms)
|
||||
handle_message sms unless check_number(
|
||||
sms.phone_number, sms.type_of_address)
|
||||
else
|
||||
modem.delete sms.id
|
||||
logger.error sms.to_s
|
||||
end
|
||||
backend.messages.each do |sms|
|
||||
handle_message sms unless check_number(sms.from)
|
||||
end
|
||||
end
|
||||
modem.close
|
||||
backend.close
|
||||
end
|
||||
|
||||
private
|
||||
|
@ -75,19 +69,8 @@ module SMSd
|
|||
Signal.trap('SIGINT', signal_term)
|
||||
end
|
||||
|
||||
def check_number(number, type_of_address)
|
||||
phone_numbers.each do |phone_number|
|
||||
return true if phone_number[:number] == number &&
|
||||
phone_number[:type_of_address] == type_of_address
|
||||
end unless phone_numbers.nil?
|
||||
|
||||
false
|
||||
end
|
||||
|
||||
def phone_numbers
|
||||
@phone_numbers ||= modem.phone_numbers
|
||||
rescue Biju::AT::CmeError
|
||||
[]
|
||||
def check_number(number)
|
||||
backend.phone_number == number
|
||||
end
|
||||
|
||||
def handle_message(sms)
|
||||
|
@ -96,12 +79,11 @@ module SMSd
|
|||
else
|
||||
send_errors(sms)
|
||||
end
|
||||
modem.delete(sms.id)
|
||||
backend.delete sms
|
||||
end
|
||||
|
||||
def send_answer(sms)
|
||||
to = (!phone_numbers.empty? ? phone_numbers.first[:number] : nil)
|
||||
message = machine.execute(sms.phone_number, to, sms.message)
|
||||
message = machine.execute(sms.from, backend.phone_number, sms.message)
|
||||
|
||||
send_sms(sms, message)
|
||||
end
|
||||
|
@ -109,10 +91,8 @@ module SMSd
|
|||
def send_errors(sms)
|
||||
logger.warn "#{sms}: #{sms.errors.values.join(',')}"
|
||||
|
||||
to = (!phone_numbers.empty? ? phone_numbers.first[:number] : nil)
|
||||
|
||||
errors = sms.errors.keys.map do |error|
|
||||
machine.execute_action(error, sms.phone_number, to, sms.message)
|
||||
machine.execute_action(error, sms.from, backend.phone_number, sms.message)
|
||||
end
|
||||
message = errors.compact.join(' ')
|
||||
|
||||
|
@ -123,8 +103,7 @@ module SMSd
|
|||
if message.nil? || message == ''
|
||||
log = 'Empty answer'
|
||||
else
|
||||
modem.send(Biju::Sms.new(
|
||||
phone_number: sms.phone_number, message: message[0, 160]))
|
||||
backend.send(SMS.new(to: sms.from, message: message))
|
||||
log = "Sent answer \"#{message}\""
|
||||
end
|
||||
|
||||
|
|
|
@ -0,0 +1,22 @@
|
|||
module SMSd
|
||||
class SMS
|
||||
attr_accessor :id, :from, :to, :message, :datetime
|
||||
attr_reader :errors
|
||||
|
||||
def initialize(attributes)
|
||||
attributes.each do |attr, value|
|
||||
setter = "#{attr}="
|
||||
send(setter, value) if respond_to?(setter)
|
||||
end
|
||||
end
|
||||
|
||||
def valid?
|
||||
@errors = {}
|
||||
|
||||
@errors[:too_old] = 'Message too old' if datetime.to_time < Time.now - (5 * 60)
|
||||
@errors[:short_number] = 'Phone number too short' if phone_number.length < 6
|
||||
|
||||
@errors.empty?
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,12 +0,0 @@
|
|||
class Biju::Sms
|
||||
attr_reader :errors
|
||||
|
||||
def valid?
|
||||
@errors = {}
|
||||
|
||||
@errors[:too_old] = 'Message too old' if datetime.to_time < Time.now - (5 * 60)
|
||||
@errors[:short_number] = 'Phone number too short' if phone_number.length < 6
|
||||
|
||||
@errors.empty?
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue