-
Notifications
You must be signed in to change notification settings - Fork 0
/
gertraud.rb
executable file
·232 lines (199 loc) · 6.73 KB
/
gertraud.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
70
71
72
73
74
75
76
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
128
129
130
131
132
133
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
166
167
168
169
170
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/usr/bin/ruby
##############################################################################
#
# gertraud - a RunOnReceive script being used with gammu-smsd for forwarding
# SMS to SMS and/or email
# Version 0.99
#
# Copyright (C) 2012-2013 Christopher Hofmann <cwh@webeve.de>
#
# ---------------------------------------------------------------------------
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin St, Fifth Floor, Boston, MA 02110, USA
#
###############################################################################
$LOAD_PATH << File.join(File.dirname(__FILE__))
require 'person'
require 'logger'
require 'yaml'
require 'optparse'
require 'rubygems'
require 'pony'
require 'clickatell'
# try to check whether we are called by gammu-smsd
if ENV['SMS_MESSAGES'] == nil
puts "Environment variable SMS_MESSAGES not set."
puts "This script is only useful to be called by gammu-smsd."
exit 1
end
# Helper to convert config keys to symbols
class Hash
def symbolize_keys!
t=self.dup
self.clear
t.each_pair do |k,v|
if v.kind_of?(Hash)
v.symbolize_keys!
end
self[k.to_sym] = v
self
end
self
end
end
# Command line options
options = {}
optparse = OptionParser.new do|opts|
opts.banner = "Usage: #{File.basename $0} [options] messagefile"
options[:configfile] = "/etc/#{File.basename $0, ".rb"}.conf"
opts.on( '-c', '--configfile FILE', 'Read configuration from FILE' ) do|file|
options[:configfile] = file
end
options[:debug] = false
opts.on( '-d', '--debug', 'Be more verbose.' ) do
options[:debug] = true
end
options[:test] = false
opts.on( '-t', '--test', 'Test mode: do not send SMS messages.' ) do
options[:test] = true
end
opts.on( '-h', '--help', 'Display this screen' ) do
puts opts
exit
end
end
optparse.parse!
# Read config
begin
conf = YAML.load_file( options[:configfile] )
rescue
puts "Opening #{options[:configfile]} failed."
exit 1
end
conf.symbolize_keys!
# Init
#logger = Logger.new(STDOUT)
logger = Logger.new(conf[:global][:logfile])
logger.datetime_format = "%Y-%m-%d %H:%M:%S"
logger.level = Logger::INFO
if options[:debug]
logger.level = Logger::DEBUG
end
Pony.options = {
:from => conf[:email][:from],
:via => :smtp,
:via_options => {
:address => conf[:email][:host],
:port => conf[:email][:port],
:user_name => conf[:email][:user],
:password => conf[:email][:pass],
:domain => conf[:email][:domain],
:authentication => :plain,
:enable_starttls_auto => true
}
}
clickatell = Clickatell::API.authenticate( conf[:clickatell][:apiid],
conf[:clickatell][:user],
conf[:clickatell][:pass] )
if options[:debug]
Clickatell::API.debug_mode = true if options[:debug]
end
if options[:test]
Clickatell::API.test_mode = true if options[:debug]
end
# read people
people = Array.new
begin
YAML.load_file( conf[:global][:peoplefile] ).each{ |data|
people.push Person.new(data)
}
rescue
puts "Opening #{conf[:global][:peoplefile]} failed: #{$!}"
puts $@ if options[:debug]
exit 1
end
# Do what has to be done
ENV['SMS_MESSAGES'].to_i.times { |msg_count|
msg_sender = ENV["SMS_#{msg_count+1}_NUMBER"]
msg_text = ENV["SMS_#{msg_count+1}_TEXT"]
logger.info 'Received from ' + msg_sender + ': ' + msg_text
logger.debug 'Encoding: ' + msg_text.encoding.to_s
trigger = conf[:global][:trigger].class == 'Array' ? conf[:global][:trigger] : [conf[:global][:trigger]];
if conf[:global][:trigger].include?(msg_sender)
logger.info 'From trigger'
# send email (if any recipients)
email_recipients = Array.new
#p Person.inspect_all
if conf[:global][:enable_email]
logger.debug 'Email sending globally enabled'
# get all email addresses except of those who have an explicit "enable_email: no"
email_recipients = Person.get_if('email') { |x| x.enable_email != false }
else
logger.debug 'Email sending globally disabled'
# get only email addresses who have an explicit "enable_email: yes"
email_recipients = Person.get_if('email') { |x| x.enable_email }
end
if !email_recipients.empty?
logger.info "Sending email to #{email_recipients.size} recipients"
begin
Pony.mail(:to => email_recipients,
:subject => conf[:email][:subject],
:body => msg_text)
rescue
logger.fatal "Sending email failed: #{$!}"
end
end
# send SMS (if any recipients)
sms_recipients = Array.new
#p Person.inspect_all
if conf[:global][:enable_sms]
logger.debug 'Sms sending globally enabled'
# get all phone numbers except of those who have an explicit "enable_sms: no"
sms_recipients = Person.get_if('phone') { |x| x.enable_sms != false }
else
logger.debug 'Sms sending globally disabled'
# get only phone numbers who have an explicit "enable_sms: yes"
sms_recipients = Person.get_if('phone') { |x| x.enable_sms }
end
if !sms_recipients.empty?
logger.info "Sending sms to #{sms_recipients.size} recipients"
logger.debug "Message length: #{msg_text.length} characters"
begin
result = clickatell.send_message( sms_recipients,
msg_text.encode("ISO-8859-1", :invalid => :replace, :undef => :replace),
{:from => conf[:sms][:from]} )
logger.debug( "Clickatell result: #{result}" )
logger.info("Remaining Clickatell balance: #{clickatell.account_balance}")
rescue
logger.fatal "Sending sms failed: #{$!}"
end
end
else
logger.info 'Unknown sender; forwarding to admin via email'
begin
Pony.mail(:to => conf[:global][:admin_email],
:subject => "Message from #{msg_sender}",
:body => msg_text)
rescue
logger.fatal "Sending email to admin failed: #{$!}"
end
end
}
#logger.debug("just a debug message")
#logger.info("important information")
#logger.warn("you better be prepared")
#logger.error("now you are in trouble")
#logger.fatal("this is the end...")
logger.close