From fa1867ad40923e4d9b156d8ff08b63be15dc30e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20M=E4kinen?= Date: Mon, 27 May 2013 15:15:25 +0300 Subject: [PATCH] gammu: Initial version of module. --- gammu/files/gammu-smsd-receive | 54 ++++++++++++++++++ gammu/files/smsd.init | 80 ++++++++++++++++++++++++++ gammu/manifests/init.pp | 97 ++++++++++++++++++++++++++++++++ gammu/templates/gammu-smsdrc.erb | 21 +++++++ 4 files changed, 252 insertions(+) create mode 100755 gammu/files/gammu-smsd-receive create mode 100755 gammu/files/smsd.init create mode 100644 gammu/manifests/init.pp create mode 100644 gammu/templates/gammu-smsdrc.erb diff --git a/gammu/files/gammu-smsd-receive b/gammu/files/gammu-smsd-receive new file mode 100755 index 0000000..2cc9f52 --- /dev/null +++ b/gammu/files/gammu-smsd-receive @@ -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() diff --git a/gammu/files/smsd.init b/gammu/files/smsd.init new file mode 100755 index 0000000..513e302 --- /dev/null +++ b/gammu/files/smsd.init @@ -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 diff --git a/gammu/manifests/init.pp b/gammu/manifests/init.pp new file mode 100644 index 0000000..7b4255e --- /dev/null +++ b/gammu/manifests/init.pp @@ -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, + } + +} + diff --git a/gammu/templates/gammu-smsdrc.erb b/gammu/templates/gammu-smsdrc.erb new file mode 100644 index 0000000..f80c78e --- /dev/null +++ b/gammu/templates/gammu-smsdrc.erb @@ -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/