gammu: Initial version of module.

This commit is contained in:
Timo Mkinen 2013-05-27 15:15:25 +03:00
parent 173791faef
commit fa1867ad40
4 changed files with 252 additions and 0 deletions

54
gammu/files/gammu-smsd-receive Executable file
View file

@ -0,0 +1,54 @@
#!/usr/bin/env python
import codecs
import os
import re
import sys
import syslog
from ConfigParser import ConfigParser
from email.mime.text import MIMEText
from subprocess import Popen, PIPE
from time import strftime, strptime
def inboxpath():
c = ConfigParser()
c.read('/etc/gammu-smsdrc')
return c.get('smsd', 'InboxPath')
syslog.openlog('gammu-smsd-receive', syslog.LOG_PID, syslog.LOG_DAEMON)
if len(sys.argv) == 1:
syslog.syslog('Invalid arguments, no messages to process')
sys.exit(1)
idx = 1
for message in sys.argv[1:]:
m = re.match('^IN(\d+)_(\d+)_\d+_([^_]+)_\d\d\.(txt|bin)$', message)
if m is None:
syslog.syslog('Cannot parse message file %s' % message)
continue
date = strptime('%s %s' % (m.group(1), m.group(2)), '%Y%m%d %H%M%S')
number = m.group(3)
message = os.path.join(inboxpath(), message)
if not os.path.exists(message):
syslog.syslog('Cannot find message file %s' % message)
continue
text = codecs.open(message, 'r', encoding='utf-16').read()
mail = MIMEText(text.encode('utf-8'), 'plain', 'utf-8')
mail['Subject'] = 'SMS Received from %s' % number
mail['Date'] = strftime('%a, %d %b %Y %H:%M:%S %Z', date)
mail['From'] = number
mail['To'] = 'root'
p = Popen(["/usr/sbin/sendmail", "-t"], stdin=PIPE)
p.communicate(mail.as_string())
idx = idx + 1
os.unlink(message)
syslog.closelog()

80
gammu/files/smsd.init Executable file
View file

@ -0,0 +1,80 @@
#!/bin/bash
#
# /etc/rc.d/init.d/smsd
#
# Starts the smsd daemon
#
# chkconfig: 345 70 30
# description: Send and receive SMS messages
# processname: gammu-smsd
### BEGIN INIT INFO
# Provides: acpid
# Required-Start: $syslog $local_fs
# Required-Stop: $syslog $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: start and stop smsd
# Description: Send and receive SMS messages
### END INIT INFO
# Source function library.
. /etc/rc.d/init.d/functions
RETVAL=0
OPTIONS="-d -p /var/run/smsd.pid -U smsd -G smsd -c /etc/gammu-smsdrc"
#
# See how we were called.
#
start() {
echo -n $"Starting gammu sms daemon: "
umask 007
daemon /usr/bin/gammu-smsd ${OPTIONS}
RETVAL=$?
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/smsd
echo
return $RETVAL
}
stop() {
echo -n $"Stopping gammu sms daemon: "
killproc /usr/bin/gammu-smsd
RETVAL=$?
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/smsd
echo
return $RETVAL
}
restart() {
stop
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
condrestart)
if [ -f /var/lock/subsys/smsd ]; then
restart
fi
;;
status)
status smsd
RETVAL=$?
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart}"
RETVAL=2
esac
exit $RETVAL

97
gammu/manifests/init.pp Normal file
View file

@ -0,0 +1,97 @@
# Install Gammu packages
#
class gammu {
package { "gammu":
ensure => installed,
}
}
# Install Gammu SMS daemon
#
# === Parameters
#
# $port:
# Serial port where modem is located.
# $pin:
# PIN code for SIM card, defaults to no PIN.
# $receivecmd:
# Source for command to run for received messages. Defaults to
# "gammu-smsd-receive" script from module.
#
class gammu::smsd($port,
$pin=undef,
$receivecmd="puppet:///modules/gammu/gammu-smsd-receive") {
require gammu
user { "smsd":
ensure => present,
comment => "Service SMS",
gid => "smsd",
groups => "dialout",
home => "/var/empty",
shell => "/sbin/nologin",
system => true,
require => Group["smsd"],
}
group { "smsd":
ensure => present,
system => true,
}
file { "/var/spool/smsd":
ensure => directory,
mode => "0770",
owner => "smsd",
group => "smsd",
require => User["smsd"],
}
if $receivecmd {
file { "/usr/local/sbin/gammu-smsd-receive":
ensure => present,
source => $receivecmd,
mode => "0755",
owner => "root",
group => "root",
before => Service["smsd"],
}
}
file { "/etc/gammu-smsdrc":
ensure => present,
content => template("gammu/gammu-smsdrc.erb"),
mode => "0640",
owner => "root",
group => "smsd",
require => Group["smsd"],
notify => Service["smsd"],
}
file { "/etc/init.d/smsd":
ensure => present,
source => "puppet:///modules/gammu/smsd.init",
mode => "0755",
owner => "root",
group => "root",
notify => [ Exec["chkconfig --add smsd"], Service["smsd"], ],
}
exec { "chkconfig --add smsd":
user => "root",
path => "/bin:/usr/bin:/sbin:/usr/sbin",
refreshonly => true,
require => File["/etc/init.d/smsd"],
before => Service["smsd"],
}
service { "smsd":
ensure => running,
enable => true,
hasstatus => true,
}
}

View file

@ -0,0 +1,21 @@
[gammu]
Connection = at
Port = <%= @port %>
[smsd]
Service = FILES
<% if @pin -%>
PIN = <%= @pin %>
<% end -%>
CheckBattery = 0
CheckSecurity = 0
CheckSignal = 0
CommTimeout = 10
LogFile = syslog
<% if @receivecmd -%>
RunOnReceive = /usr/local/sbin/gammu-smsd-receive
<% end -%>
InboxFormat = unicode
InboxPath = /var/spool/smsd/
OutboxPath = /var/spool/smsd/