From 8f0b4a38f1ba8fdabed555032c3905ad8c4bc1d9 Mon Sep 17 00:00:00 2001 From: Paul Lecuq Date: Fri, 15 Jan 2016 11:38:38 +0100 Subject: [PATCH] initial commit --- config.py.sample | 12 ++++ smsgateway.py | 139 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 151 insertions(+) create mode 100644 config.py.sample create mode 100755 smsgateway.py diff --git a/config.py.sample b/config.py.sample new file mode 100644 index 0000000..38ea29d --- /dev/null +++ b/config.py.sample @@ -0,0 +1,12 @@ +smshost="192.168.1.100" +smsusername="admin" +smspassword="admin" +smsrecipients=["0660066006","0620022002"] +smssize=160 +smstemplate="Nouveau mail de %s. Object %s. Message ..." + +mailboxserver="smtp.domain.net" +mailboxlogin="charlie@domain.net" +mailboxpassword="charlieroot" + +pidfile='/run/lock/smsgateway.pid' \ No newline at end of file diff --git a/smsgateway.py b/smsgateway.py new file mode 100755 index 0000000..1199d81 --- /dev/null +++ b/smsgateway.py @@ -0,0 +1,139 @@ +#!/usr/bin/env python +# -*- encoding: utf-8 -*- + +import imaplib +import email +import time +import telnetlib +import config +import fcntl +import sys +import unicodedata + +from email.header import decode_header +from messaging.sms import SmsSubmit + +def fetch_unread_mails(): + mail = imaplib.IMAP4_SSL(config.mailboxserver) + mail.login(config.mailboxlogin,config.mailboxpassword) + mail.list() + status,messages = mail.select("INBOX") + + mails=[] + + n=0 + retcode, messages = mail.search(None, '(UNSEEN)') + if retcode == 'OK': + for num in messages[0].split(): + n=n+1 + typ,data = mail.fetch(num,'RFC822') + for response_part in data: + if isinstance(response_part,tuple): + original = email.message_from_string(response_part[1]) + mailfrom = original['From'] + if "<" in mailfrom: + mailfrom = mailfrom.split('<')[1].rstrip('>') + mailsubject = original['Subject'] + mailsubject = decode_header(mailsubject) + default_charset = 'ASCII' + mailsubject=''.join([ unicode(t[0], t[1] or default_charset) for t in mailsubject ]) + mails.append([mailfrom,mailsubject]) + return mails + +def clearallsms(): + try: + count=0 + tn = telnetlib.Telnet(config.smshost,23) + tn.read_until("username: ") + tn.write(config.smsusername + "\r\n") + tn.write(config.smspassword + "\r\n") + tn.read_until("user level = admin.") + tn.write("module1\r\n") + tn.read_until("got!! press 'ctrl-x' to release module 1.") + while count<100: + tn.write("AT+CMGD="+str(count)+"\r\n") + count=count+1 + tn.read_until("\r\n") + tn.close() + except: + print("Unexpected error:", sys.exc_info()[0]) + raise + + +def formatsms(message): + if len(message) > config.smssize: + message = message[:config.smssize] + return message + +def imap2sms(sender,subject): + sms=config.smstemplate % (sender,subject) + return sms + +def pduformat(phonenumber,message): + sms = SmsSubmit(phonenumber, message) + pdu = sms.to_pdu()[0] + pdustring=pdu.pdu + pdulength=pdu.length + # debug output + #print(phonenumber, message) + #print(pdu.length, pdu.pdu) + return pdustring,pdulength + +def sendsms(pdustring,pdulenght): + try: + time.sleep(2) + tn = telnetlib.Telnet(config.smshost,23) + tn.read_until("username: ") + tn.write(config.smsusername + "\r\n") + tn.write(config.smspassword + "\r\n") + tn.read_until("user level = admin.") + tn.write("state1\r\n") + tn.read_until("module 1: free.\r\n]") + tn.write("module1\r\n") + tn.read_until("got!! press 'ctrl-x' to release module 1.") + tn.write("AT+CMGF=0\r\n") + tn.read_until("0\r\n") + tn.write('AT+CMGS=%s\r\n' % pdulength) + tn.read_until("> ") + tn.write("%s\x1A" % pdustring) + tn.read_until("+CMGS") + tn.close() + except: + print("Unexpected error:", sys.exc_info()[0]) + raise + +def usage(): + usage="smsgateway.py subcommands : \n\n%s imap2sms\n%s sms \n%s clearallsms\n" % (sys.argv[0],sys.argv[0],sys.argv[0]) + return usage + +fh = open(config.pidfile, 'w') +try: + fcntl.lockf(fh, fcntl.LOCK_EX | fcntl.LOCK_NB) +except IOError: + # another instance is running + print 'Error: Another instance is running...' + sys.exit(0) + +if len(sys.argv) > 1: + if sys.argv[1] == "clearallsms": + clearallsms() + elif sys.argv[1] == "sms": + if (len(sys.argv)==4): + phonenumber=sys.argv[2] + sms=sys.argv[3] + pdustring,pdulength=pduformat(phonenumber,sms) + sendsms(pdustring,pdulength) + else: + print(usage()) + elif sys.argv[1] == "imap2sms": + mails = fetch_unread_mails() + for phonenumber in config.smsrecipients: + for mail in mails: + sender=mail[0] + subject=mail[1] + sms=formatsms(imap2sms(sender,subject)) + pdustring,pdulength=pduformat(phonenumber,sms) + sendsms(pdustring,pdulength) +else: + print(usage()) + exit(1)