Rework log system and send_sms method
This commit is contained in:
parent
4c93dc7bb3
commit
2b43461f76
@ -5,6 +5,7 @@ smssize = 160
|
||||
smstemplate = "Nouveau mail de %s. Object %s. Message ..."
|
||||
smsformat = "ascii"
|
||||
|
||||
mailboxes = "/home/paul/PycharmProjects/smsgateway/mailbox.csv"
|
||||
mailboxes = "mailbox.csv"
|
||||
|
||||
pidfile = "/run/lock/smsgateway.pid"
|
||||
log = True
|
||||
|
@ -8,11 +8,20 @@ import telnetlib
|
||||
import config
|
||||
import fcntl
|
||||
import sys
|
||||
import datetime
|
||||
|
||||
from email.header import decode_header
|
||||
from messaging.sms import SmsSubmit
|
||||
|
||||
|
||||
def log(message, level = 'INFO'):
|
||||
if config.log:
|
||||
date = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
||||
print ("%s [ %s ] %s" % (date, level, message))
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def csv_config_parser(mailboxes):
|
||||
"""
|
||||
Reads CSV config, returns as a list
|
||||
@ -85,7 +94,7 @@ def clear_all_sms():
|
||||
tn.read_until("\r\n")
|
||||
tn.close()
|
||||
except:
|
||||
print("Unexpected error:", sys.exc_info()[0])
|
||||
log(("Error clear sms %s" % sys.exc_info()[0]), 'ERROR')
|
||||
raise
|
||||
|
||||
|
||||
@ -144,16 +153,26 @@ def imap2sms(conf):
|
||||
for mail in mails:
|
||||
sender = mail[0]
|
||||
subject = mail[1]
|
||||
if config.smsformat == "pdu":
|
||||
send_sms(number, subject, sender)
|
||||
|
||||
|
||||
def send_sms(number, subject, sender):
|
||||
if config.smsformat == 'pdu':
|
||||
sms = resize_pdu_sms(sms_template(sender, subject))
|
||||
pdustring, pdulength = pduformat(number, sms)
|
||||
pdustring, pdulength, phonenumber, message = pdu_format(number, sms)
|
||||
send_pdu_sms(pdustring, pdulength)
|
||||
elif config.smsformat == "ascii":
|
||||
log(("Message sent to %s with text %s using PDU method" % (number, message)))
|
||||
return True
|
||||
elif config.smsformat == 'ascii':
|
||||
sms = resize_ascii_sms(sms_template(sender, subject))
|
||||
send_ascii_sms(number, sms)
|
||||
log(("Message sent to %s with text %s using ASCII method" % (number, sms)))
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
def pduformat(phonenumber, message):
|
||||
def pdu_format(phonenumber, message):
|
||||
"""
|
||||
Formats SMS using pdu encoding
|
||||
:param phonenumber: Phone number to insert in pdu
|
||||
@ -164,13 +183,10 @@ def pduformat(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, phonenumber, message
|
||||
|
||||
|
||||
def send_ascii_sms(phonenumber, sms, phonenumbre, message):
|
||||
def send_ascii_sms(phonenumber, sms):
|
||||
"""
|
||||
Send SMS using telnetlib, returns exception when issues with telnet communication
|
||||
:param phonenumber: Phone number to insert in pdu
|
||||
@ -195,13 +211,12 @@ def send_ascii_sms(phonenumber, sms, phonenumbre, message):
|
||||
tn.write("%s\x1A" % decoded_sms)
|
||||
tn.read_until("+CMGS")
|
||||
tn.close()
|
||||
print ("SMS send to telnet to %s with message %s" % phonenumber, message)
|
||||
except:
|
||||
print("Unexpected error :", sys.exc_info()[0])
|
||||
log(("Error when send SMS with Telnet: %s" % sys.exc_info()[0]), 'ERROR')
|
||||
raise
|
||||
|
||||
|
||||
def send_pdu_sms(pdustring, pdulength, phonenumber, message):
|
||||
def send_pdu_sms(pdustring, pdulength):
|
||||
"""
|
||||
Send SMS using telnetlib, returns exception when issues with telnet communication
|
||||
:param pdustring: is the converted sms to pdu format
|
||||
@ -225,9 +240,8 @@ def send_pdu_sms(pdustring, pdulength, phonenumber, message):
|
||||
tn.write("%s\r\n\x1A" % pdustring)
|
||||
tn.read_until("+CMGS")
|
||||
tn.close()
|
||||
print ("SMS send to telnet to %s with message %s" % phonenumber, message)
|
||||
except:
|
||||
print("Unexpected error:", sys.exc_info()[0])
|
||||
log(("Error when send SMS with Telnet: %s" % sys.exc_info()[0]), 'ERROR')
|
||||
raise
|
||||
|
||||
|
||||
@ -254,7 +268,7 @@ try:
|
||||
fcntl.lockf(fh, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
||||
except IOError:
|
||||
# another instance is running
|
||||
print 'Error: Another instance is running...'
|
||||
log("Error: Another instance is running...", 'ERROR')
|
||||
sys.exit(0)
|
||||
|
||||
if len(sys.argv) > 1:
|
||||
@ -265,11 +279,11 @@ if len(sys.argv) > 1:
|
||||
if len(sys.argv) == 4:
|
||||
phonenumber = sys.argv[2]
|
||||
sms = sys.argv[3]
|
||||
pdustring, pdulength, phonenumber, message = pduformat(phonenumber, sms)
|
||||
if config.smsformat == "pdu":
|
||||
send_pdu_sms(pdustring, pdulength, phonenumber, message)
|
||||
pdustring, pdulength, phonenumber, message = pdu_format(phonenumber, sms)
|
||||
send_pdu_sms(pdustring, pdulength)
|
||||
elif config.smsformat == "ascii":
|
||||
send_ascii_sms(phonenumber, sms, phoneumber, message)
|
||||
send_ascii_sms(phonenumber, sms)
|
||||
else:
|
||||
print(usage())
|
||||
|
||||
@ -279,6 +293,10 @@ if len(sys.argv) > 1:
|
||||
|
||||
elif sys.argv[1] == "debug":
|
||||
print debug()
|
||||
|
||||
else:
|
||||
print(usage())
|
||||
exit(1)
|
||||
else:
|
||||
print(usage())
|
||||
exit(1)
|
||||
|
Loading…
Reference in New Issue
Block a user