Multi-lists support
Code refactor and clean
This commit is contained in:
parent
d29fb44172
commit
61d9186789
1
.gitignore
vendored
1
.gitignore
vendored
@ -60,3 +60,4 @@ target/
|
|||||||
|
|
||||||
# Config files
|
# Config files
|
||||||
config.py
|
config.py
|
||||||
|
mailbox.csv
|
@ -1,12 +1,10 @@
|
|||||||
smshost = "192.168.1.100"
|
smshost = "192.168.1.100"
|
||||||
smsusername = "admin"
|
smsusername = "admin"
|
||||||
smspassword = "admin"
|
smspassword = "admin"
|
||||||
smsrecipients=["0660066006","0620022002"]
|
|
||||||
smssize = 160
|
smssize = 160
|
||||||
smstemplate = "Nouveau mail de %s. Object %s. Message ..."
|
smstemplate = "Nouveau mail de %s. Object %s. Message ..."
|
||||||
|
smsformat = "ascii"
|
||||||
|
|
||||||
mailboxserver="smtp.domain.net"
|
mailboxes = "/home/paul/PycharmProjects/smsgateway/mailbox.csv"
|
||||||
mailboxlogin="charlie@domain.net"
|
|
||||||
mailboxpassword="charlieroot"
|
|
||||||
|
|
||||||
pidfile='/run/lock/smsgateway.pid'
|
pidfile = "/run/lock/smsgateway.pid"
|
||||||
|
3
mailbox.csv.sample
Normal file
3
mailbox.csv.sample
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
# mail, password, number1, number2, number3, ...
|
||||||
|
mailhost.test.com,test1@test.com,passwordtest1,0606060606,0602020202
|
||||||
|
mailhost.test.com,test2@test.com,passwordtest2,0604040404,0603030303
|
190
smsgateway.py
190
smsgateway.py
@ -8,25 +8,43 @@ import telnetlib
|
|||||||
import config
|
import config
|
||||||
import fcntl
|
import fcntl
|
||||||
import sys
|
import sys
|
||||||
import unicodedata
|
|
||||||
|
|
||||||
from email.header import decode_header
|
from email.header import decode_header
|
||||||
from messaging.sms import SmsSubmit
|
from messaging.sms import SmsSubmit
|
||||||
|
|
||||||
def fetch_unread_mails():
|
|
||||||
"""Fetch unread emails on specific mailbox, and returns some fields"""
|
def csv_config_parser(mailboxes):
|
||||||
mail = imaplib.IMAP4_SSL(config.mailboxserver)
|
params = []
|
||||||
mail.login(config.mailboxlogin,config.mailboxpassword)
|
with open(mailboxes) as f:
|
||||||
|
for line in f:
|
||||||
|
if "#" in line:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
tmp_str = line.strip("\n").split(",")
|
||||||
|
params.append(tmp_str)
|
||||||
|
return params
|
||||||
|
|
||||||
|
|
||||||
|
def fetch_unread_mails(mailboxserver, mailboxlogin, mailboxpassword):
|
||||||
|
"""
|
||||||
|
Fetch unread emails on specific mailbox, and returns some fields
|
||||||
|
:param mailboxserver:
|
||||||
|
:param mailboxlogin:
|
||||||
|
:param mailboxpassword:
|
||||||
|
:return str:
|
||||||
|
"""
|
||||||
|
mail = imaplib.IMAP4_SSL(mailboxserver)
|
||||||
|
mail.login(mailboxlogin, mailboxpassword)
|
||||||
mail.list()
|
mail.list()
|
||||||
status,messages = mail.select("INBOX")
|
mail.select("INBOX")
|
||||||
|
|
||||||
mails = []
|
mails = []
|
||||||
|
|
||||||
n = 0
|
n = 0
|
||||||
retcode, messages = mail.search(None, '(UNSEEN)')
|
returncode, messages = mail.search(None, '(UNSEEN)')
|
||||||
if retcode == 'OK':
|
if returncode == 'OK':
|
||||||
for num in messages[0].split():
|
for num in messages[0].split():
|
||||||
n=n+1
|
n += 1
|
||||||
typ, data = mail.fetch(num, 'RFC822')
|
typ, data = mail.fetch(num, 'RFC822')
|
||||||
for response_part in data:
|
for response_part in data:
|
||||||
if isinstance(response_part, tuple):
|
if isinstance(response_part, tuple):
|
||||||
@ -41,8 +59,12 @@ def fetch_unread_mails():
|
|||||||
mails.append([mailfrom, mailsubject])
|
mails.append([mailfrom, mailsubject])
|
||||||
return mails
|
return mails
|
||||||
|
|
||||||
def clearallsms():
|
|
||||||
"""Clears all stored SMS on Portech like gateways"""
|
def clear_all_sms():
|
||||||
|
"""
|
||||||
|
Clears all stored SMS on Portech like gateways
|
||||||
|
:return: None
|
||||||
|
"""
|
||||||
try:
|
try:
|
||||||
count = 0
|
count = 0
|
||||||
tn = telnetlib.Telnet(config.smshost, 23)
|
tn = telnetlib.Telnet(config.smshost, 23)
|
||||||
@ -54,26 +76,74 @@ def clearallsms():
|
|||||||
tn.read_until("got!! press 'ctrl-x' to release module 1.")
|
tn.read_until("got!! press 'ctrl-x' to release module 1.")
|
||||||
while count < 100:
|
while count < 100:
|
||||||
tn.write("AT+CMGD=" + str(count) + "\r\n")
|
tn.write("AT+CMGD=" + str(count) + "\r\n")
|
||||||
count=count+1
|
count += 1
|
||||||
tn.read_until("\r\n")
|
tn.read_until("\r\n")
|
||||||
tn.close()
|
tn.close()
|
||||||
except:
|
except:
|
||||||
print("Unexpected error:", sys.exc_info()[0])
|
print("Unexpected error:", sys.exc_info()[0])
|
||||||
raise
|
raise
|
||||||
|
|
||||||
def formatsms(message):
|
|
||||||
"""Strip SMS if longer than config.smssize"""
|
def resize_ascii_sms(message):
|
||||||
|
"""
|
||||||
|
Strip message if longer than config.smssize
|
||||||
|
"""
|
||||||
|
value = config.smssize-3
|
||||||
|
if len(message) > value:
|
||||||
|
message = message[:value]
|
||||||
|
return message
|
||||||
|
|
||||||
|
|
||||||
|
def resize_pdu_sms(message):
|
||||||
|
"""
|
||||||
|
Strip SMS if longer than config.smssize
|
||||||
|
:param message:
|
||||||
|
:return: str
|
||||||
|
"""
|
||||||
if len(message) > config.smssize:
|
if len(message) > config.smssize:
|
||||||
message = message[:config.smssize]
|
message = message[:config.smssize]
|
||||||
return message
|
return message
|
||||||
|
|
||||||
def imap2sms(sender,subject):
|
|
||||||
"""Uses a template to make a short message from email fields"""
|
def sms_template(sender, subject):
|
||||||
sms=config.smstemplate % (sender,subject)
|
"""
|
||||||
return sms
|
Uses a template to make a short message from email fields
|
||||||
|
:param sender: str
|
||||||
|
:param subject: str
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
|
text = config.smstemplate % (sender, subject)
|
||||||
|
return text
|
||||||
|
|
||||||
|
|
||||||
|
def imap2sms(conf):
|
||||||
|
for list in conf:
|
||||||
|
username = list[0]
|
||||||
|
password = list[1]
|
||||||
|
mailserver = list[2]
|
||||||
|
numbers = []
|
||||||
|
i = 3
|
||||||
|
while i < len(list):
|
||||||
|
numbers.append(list[i])
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
mails = fetch_unread_mails(username, password, mailserver)
|
||||||
|
for number in numbers:
|
||||||
|
for mail in mails:
|
||||||
|
sender = mail[0]
|
||||||
|
subject = mail[1]
|
||||||
|
if config.smsformat == "pdu":
|
||||||
|
sms = resize_pdu_sms(sms_template(sender, subject))
|
||||||
|
pdustring, pdulength = pduformat(number, sms)
|
||||||
|
send_pdu_sms(pdustring, pdulength)
|
||||||
|
elif config.smsformat == "ascii":
|
||||||
|
sms = resize_ascii_sms(sms_template(sender, subject))
|
||||||
|
send_ascii_sms(number, sms)
|
||||||
|
|
||||||
def pduformat(phonenumber, message):
|
def pduformat(phonenumber, message):
|
||||||
"""Formats SMS using pdu encoding"""
|
"""
|
||||||
|
Formats SMS using pdu encoding
|
||||||
|
"""
|
||||||
sms = SmsSubmit(phonenumber, message)
|
sms = SmsSubmit(phonenumber, message)
|
||||||
pdu = sms.to_pdu()[0]
|
pdu = sms.to_pdu()[0]
|
||||||
pdustring = pdu.pdu
|
pdustring = pdu.pdu
|
||||||
@ -83,8 +153,41 @@ def pduformat(phonenumber,message):
|
|||||||
# print(pdu.length, pdu.pdu)
|
# print(pdu.length, pdu.pdu)
|
||||||
return pdustring, pdulength
|
return pdustring, pdulength
|
||||||
|
|
||||||
def sendsms(pdustring,pdulenght):
|
|
||||||
"""Send SMS using telnetlib, returns exception when issues with telnet communication"""
|
def send_ascii_sms(phonenumber, sms):
|
||||||
|
"""
|
||||||
|
Send SMS using telnetlib, returns exception when issues with telnet communication
|
||||||
|
"""
|
||||||
|
decoded_sms = sms.encode("ascii", "ignore")
|
||||||
|
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=1\r\n")
|
||||||
|
tn.read_until("0\r\n")
|
||||||
|
tn.write('AT+CMGS=%s\r\n' % phonenumber)
|
||||||
|
tn.read_until("> ")
|
||||||
|
tn.write("%s\x1A" % decoded_sms)
|
||||||
|
tn.read_until("+CMGS")
|
||||||
|
tn.close()
|
||||||
|
except:
|
||||||
|
print("Unexpected error :", sys.exc_info()[0])
|
||||||
|
raise
|
||||||
|
|
||||||
|
|
||||||
|
def send_pdu_sms(pdustring, pdulenght):
|
||||||
|
"""
|
||||||
|
Send SMS using telnetlib, returns exception when issues with telnet communication
|
||||||
|
:param pdustring: is the converted sms to pdu format
|
||||||
|
:param pdulenght: is the size of the pdustring
|
||||||
|
"""
|
||||||
try:
|
try:
|
||||||
time.sleep(2)
|
time.sleep(2)
|
||||||
tn = telnetlib.Telnet(config.smshost, 23)
|
tn = telnetlib.Telnet(config.smshost, 23)
|
||||||
@ -100,17 +203,30 @@ def sendsms(pdustring,pdulenght):
|
|||||||
tn.read_until("0\r\n")
|
tn.read_until("0\r\n")
|
||||||
tn.write('AT+CMGS=%s\r\n' % pdulength)
|
tn.write('AT+CMGS=%s\r\n' % pdulength)
|
||||||
tn.read_until("> ")
|
tn.read_until("> ")
|
||||||
tn.write("%s\x1A" % pdustring)
|
tn.write("%s\r\n\x1A" % pdustring)
|
||||||
tn.read_until("+CMGS")
|
tn.read_until("+CMGS")
|
||||||
tn.close()
|
tn.close()
|
||||||
except:
|
except:
|
||||||
print("Unexpected error:", sys.exc_info()[0])
|
print("Unexpected error:", sys.exc_info()[0])
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
|
||||||
def usage():
|
def usage():
|
||||||
"""Prints usage"""
|
"""
|
||||||
usage="smsgateway.py subcommands : \n\n%s imap2sms\n%s sms <number> <message>\n%s clearallsms\n" % (sys.argv[0],sys.argv[0],sys.argv[0])
|
Prints usage
|
||||||
return usage
|
"""
|
||||||
|
usagetext = "smsgateway.py subcommands : \n\n%s imap2sms\n%s sms <number> <message>\n%s clearallsms\n" % (
|
||||||
|
sys.argv[0], sys.argv[0], sys.argv[0])
|
||||||
|
return usagetext
|
||||||
|
|
||||||
|
|
||||||
|
def debug():
|
||||||
|
"""
|
||||||
|
Debug Function
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
fh = open(config.pidfile, 'w')
|
fh = open(config.pidfile, 'w')
|
||||||
try:
|
try:
|
||||||
@ -122,24 +238,26 @@ except IOError:
|
|||||||
|
|
||||||
if len(sys.argv) > 1:
|
if len(sys.argv) > 1:
|
||||||
if sys.argv[1] == "clearallsms":
|
if sys.argv[1] == "clearallsms":
|
||||||
clearallsms()
|
clear_all_sms()
|
||||||
|
|
||||||
elif sys.argv[1] == "sms":
|
elif sys.argv[1] == "sms":
|
||||||
if (len(sys.argv)==4):
|
if len(sys.argv) == 4:
|
||||||
phonenumber = sys.argv[2]
|
phonenumber = sys.argv[2]
|
||||||
sms = sys.argv[3]
|
sms = sys.argv[3]
|
||||||
pdustring, pdulength = pduformat(phonenumber, sms)
|
pdustring, pdulength = pduformat(phonenumber, sms)
|
||||||
sendsms(pdustring,pdulength)
|
if config.smsformat == "pdu":
|
||||||
|
send_pdu_sms(pdustring, pdulength)
|
||||||
|
elif config.smsformat == "ascii":
|
||||||
|
send_ascii_sms(phonenumber, sms)
|
||||||
else:
|
else:
|
||||||
print(usage())
|
print(usage())
|
||||||
|
|
||||||
elif sys.argv[1] == "imap2sms":
|
elif sys.argv[1] == "imap2sms":
|
||||||
mails = fetch_unread_mails()
|
config_params = csv_config_parser(config.mailboxes)
|
||||||
for phonenumber in config.smsrecipients:
|
imap2sms(config_params)
|
||||||
for mail in mails:
|
|
||||||
sender=mail[0]
|
elif sys.argv[1] == "debug":
|
||||||
subject=mail[1]
|
print debug()
|
||||||
sms=formatsms(imap2sms(sender,subject))
|
|
||||||
pdustring,pdulength=pduformat(phonenumber,sms)
|
|
||||||
sendsms(pdustring,pdulength)
|
|
||||||
else:
|
else:
|
||||||
print(usage())
|
print(usage())
|
||||||
exit(1)
|
exit(1)
|
||||||
|
Loading…
Reference in New Issue
Block a user