Merge pull request #1 from MyTheValentinus/master

Log system, multiples fixes
This commit is contained in:
Paul Lecuq 2017-12-11 16:49:25 +01:00 committed by GitHub
commit ac5a7984f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 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

69
smsgateway.py Executable file → Normal file
View File

@ -8,10 +8,25 @@ 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
instanceSeparator = True
def log(message, level = 'INFO'):
global instanceSeparator
if instanceSeparator:
print ""
print "=== (INSTANCE) ==="
instanceSeparator = False
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):
""" """
@ -85,7 +100,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
@ -130,30 +145,39 @@ def imap2sms(conf):
:return: :return:
""" """
for l in conf: for l in conf:
username = l[0] username = l[1]
password = l[1] password = l[2]
mailserver = l[2] mailserver = l[0]
numbers = [] numbers = []
i = 3 i = 3
while i < len(l): while i < len(l):
numbers.append(l[i]) numbers.append(l[i])
i += 1 i += 1
mails = fetch_unread_mails(mailserver, username, password)
mails = fetch_unread_mails(username, password, mailserver)
for number in numbers: for number in numbers:
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,10 +188,7 @@ 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 return pdustring, pdulength, phonenumber, message
# print(phonenumber, message)
# print(pdu.length, pdu.pdu)
return pdustring, pdulength
def send_ascii_sms(phonenumber, sms): def send_ascii_sms(phonenumber, sms):
@ -196,7 +217,7 @@ def send_ascii_sms(phonenumber, sms):
tn.read_until("+CMGS") tn.read_until("+CMGS")
tn.close() tn.close()
except: except:
print("Unexpected error :", sys.exc_info()[0]) log(("Error when send SMS with Telnet: %s" % sys.exc_info()[0]), 'ERROR')
raise raise
@ -225,7 +246,7 @@ def send_pdu_sms(pdustring, pdulength):
tn.read_until("+CMGS") tn.read_until("+CMGS")
tn.close() tn.close()
except: except:
print("Unexpected error:", sys.exc_info()[0]) log(("Error when send SMS with Telnet: %s" % sys.exc_info()[0]), 'ERROR')
raise raise
@ -252,7 +273,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:
@ -263,8 +284,8 @@ 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 = pduformat(phonenumber, sms)
if config.smsformat == "pdu": if config.smsformat == "pdu":
pdustring, pdulength, phonenumber, message = pdu_format(phonenumber, sms)
send_pdu_sms(pdustring, pdulength) send_pdu_sms(pdustring, pdulength)
elif config.smsformat == "ascii": elif config.smsformat == "ascii":
send_ascii_sms(phonenumber, sms) send_ascii_sms(phonenumber, sms)
@ -277,6 +298,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)