Rework log system and send_sms method

This commit is contained in:
Valentin Deville 2017-12-06 17:11:38 +01:00
parent 4c93dc7bb3
commit 2b43461f76
No known key found for this signature in database
GPG Key ID: F37EBA152ABA41D8
2 changed files with 42 additions and 23 deletions

View File

@ -5,6 +5,7 @@ smssize = 160
smstemplate = "Nouveau mail de %s. Object %s. Message ..." smstemplate = "Nouveau mail de %s. Object %s. Message ..."
smsformat = "ascii" smsformat = "ascii"
mailboxes = "/home/paul/PycharmProjects/smsgateway/mailbox.csv" mailboxes = "mailbox.csv"
pidfile = "/run/lock/smsgateway.pid" pidfile = "/run/lock/smsgateway.pid"
log = True

View File

@ -8,11 +8,20 @@ import telnetlib
import config import config
import fcntl import fcntl
import sys import sys
import datetime
from email.header import decode_header from email.header import decode_header
from messaging.sms import SmsSubmit 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): def csv_config_parser(mailboxes):
""" """
Reads CSV config, returns as a list Reads CSV config, returns as a list
@ -85,7 +94,7 @@ def clear_all_sms():
tn.read_until("\r\n") tn.read_until("\r\n")
tn.close() tn.close()
except: except:
print("Unexpected error:", sys.exc_info()[0]) log(("Error clear sms %s" % sys.exc_info()[0]), 'ERROR')
raise raise
@ -144,16 +153,26 @@ def imap2sms(conf):
for mail in mails: for mail in mails:
sender = mail[0] sender = mail[0]
subject = mail[1] subject = mail[1]
if config.smsformat == "pdu": send_sms(number, subject, sender)
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 send_sms(number, subject, sender):
if config.smsformat == 'pdu':
sms = resize_pdu_sms(sms_template(sender, subject))
pdustring, pdulength, phonenumber, message = pdu_format(number, sms)
send_pdu_sms(pdustring, pdulength)
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 pdu_format(phonenumber, message):
""" """
Formats SMS using pdu encoding Formats SMS using pdu encoding
:param phonenumber: Phone number to insert in pdu :param phonenumber: Phone number to insert in pdu
@ -164,13 +183,10 @@ def pduformat(phonenumber, message):
pdu = sms.to_pdu()[0] pdu = sms.to_pdu()[0]
pdustring = pdu.pdu pdustring = pdu.pdu
pdulength = pdu.length pdulength = pdu.length
# debug output
# print(phonenumber, message)
# print(pdu.length, pdu.pdu)
return pdustring, pdulength, phonenumber, message 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 Send SMS using telnetlib, returns exception when issues with telnet communication
:param phonenumber: Phone number to insert in pdu :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.write("%s\x1A" % decoded_sms)
tn.read_until("+CMGS") tn.read_until("+CMGS")
tn.close() tn.close()
print ("SMS send to telnet to %s with message %s" % phonenumber, message)
except: except:
print("Unexpected error :", sys.exc_info()[0]) log(("Error when send SMS with Telnet: %s" % sys.exc_info()[0]), 'ERROR')
raise 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 Send SMS using telnetlib, returns exception when issues with telnet communication
:param pdustring: is the converted sms to pdu format :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.write("%s\r\n\x1A" % pdustring)
tn.read_until("+CMGS") tn.read_until("+CMGS")
tn.close() tn.close()
print ("SMS send to telnet to %s with message %s" % phonenumber, message)
except: except:
print("Unexpected error:", sys.exc_info()[0]) log(("Error when send SMS with Telnet: %s" % sys.exc_info()[0]), 'ERROR')
raise raise
@ -254,7 +268,7 @@ try:
fcntl.lockf(fh, fcntl.LOCK_EX | fcntl.LOCK_NB) fcntl.lockf(fh, fcntl.LOCK_EX | fcntl.LOCK_NB)
except IOError: except IOError:
# another instance is running # another instance is running
print 'Error: Another instance is running...' log("Error: Another instance is running...", 'ERROR')
sys.exit(0) sys.exit(0)
if len(sys.argv) > 1: if len(sys.argv) > 1:
@ -265,11 +279,11 @@ if len(sys.argv) > 1:
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, phonenumber, message = pduformat(phonenumber, sms)
if config.smsformat == "pdu": 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": elif config.smsformat == "ascii":
send_ascii_sms(phonenumber, sms, phoneumber, message) send_ascii_sms(phonenumber, sms)
else: else:
print(usage()) print(usage())
@ -279,6 +293,10 @@ if len(sys.argv) > 1:
elif sys.argv[1] == "debug": elif sys.argv[1] == "debug":
print debug() print debug()
else:
print(usage())
exit(1)
else: else:
print(usage()) print(usage())
exit(1) exit(1)