Merge pull request #1 from MyTheValentinus/master
Log system, multiples fixes
This commit is contained in:
commit
ac5a7984f0
@ -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
|
||||||
|
61
smsgateway.py
Executable file → Normal file
61
smsgateway.py
Executable file → Normal 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)
|
||||||
|
|
||||||
|
|
||||||
|
def send_sms(number, subject, sender):
|
||||||
|
if config.smsformat == 'pdu':
|
||||||
sms = resize_pdu_sms(sms_template(sender, subject))
|
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)
|
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))
|
sms = resize_ascii_sms(sms_template(sender, subject))
|
||||||
send_ascii_sms(number, sms)
|
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
|
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)
|
||||||
|
Loading…
Reference in New Issue
Block a user