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()