X-Git-Url: https://git.sthu.org/?p=smailq.git;a=blobdiff_plain;f=smailq;h=f0ad9094641499d1057e96ba892438784e622119;hp=7d5286ac122f3cf6d838d20e5f60c9a5917e24a4;hb=280ba097560432392861b3237102a677a95d7da1;hpb=6cb15cebfa30c025d5996bc3015b4285b25a59cf diff --git a/smailq b/smailq index 7d5286a..f0ad909 100755 --- a/smailq +++ b/smailq @@ -20,6 +20,7 @@ import subprocess import sys import time import socket +import syslog verbose = False @@ -163,8 +164,8 @@ class MailQueue: info['to'] = "" info['subject'] = "" - with open(mailfn, "r") as f: - mail = f.readlines() + with open(mailfn, "rb") as f: + mail = f.read().decode('utf8', 'replace').splitlines() for l in mail: if l.startswith("Subject:"): @@ -234,7 +235,7 @@ class MailQueue: # Read the mail mailfn = self.conf.getmailfn(id) - mailf = open(mailfn, "r") + mailf = open(mailfn, "rb") # Read the options msaargsfn = self.conf.getmsaargsfn(id) @@ -284,7 +285,7 @@ class MailQueue: # Write the mail mailfn = self.conf.getmailfn(id) - with open(mailfn, "w") as f: + with open(mailfn, "wb") as f: f.write(mail) # Write the options @@ -302,13 +303,19 @@ class MailQueue: def log(conf, msg, id=None): """Write message to log file""" + # Prepend ID to msg + if id is not None: + msg = ("ID %s: " % id) + msg + + if conf.getlogdir() == 'syslog': + syslog.syslog(msg) + return + fn = conf.getlogdir() + "/smailq.log" with open(fn, 'a') as f: fcntl.lockf(f, fcntl.LOCK_EX) - # Prepend ID to msg - if id is not None: - msg = ("ID %s: " % id) + msg + # Prepend time to msg msg = time.strftime("%Y-%m-%d %H:%M:%S: ", time.localtime()) + msg @@ -395,7 +402,7 @@ OPTIONS: if __name__ == "__main__": - conffn = os.path.expanduser("~/.smailq.conf") + conffn_list = [os.path.expanduser("~/.smailq.conf"), "/etc/smailq.conf"] cmd = "--list" nooptargs = [] @@ -416,7 +423,7 @@ if __name__ == "__main__": '--deliver']: cmd = opt elif opt in ['-C', '--config']: - conffn = arg + conffn_list = [arg] elif opt in ['-v', '--verbose']: verbose = True quiet = False @@ -427,13 +434,14 @@ if __name__ == "__main__": assert(False) except getopt.GetoptError as e: - printerr("Error parsing arguments:", e) + printerr("Error parsing arguments: " + str(e)) usage() sys.exit(os.EX_USAGE) # Reading config file - if not os.path.isfile(conffn): - printerr("No such config file:", conffn) + conffn = next((f for f in conffn_list if os.path.isfile(f)), None) + if conffn is None: + printerr("No config file found: " + str(conffn_list)) sys.exit(os.EX_IOERR) conf = None try: @@ -443,12 +451,14 @@ if __name__ == "__main__": printerr("Data directory does not exist: " + conf.getdatadir()) sys.exit(os.EX_IOERR) - if not os.path.isdir(conf.getlogdir()): - printerr("Log directory does not exist: " + conf.getlogdir()) - sys.exit(os.EX_IOERR) + if conf.getlogdir() == 'syslog': + syslog.openlog('smailq', 0, syslog.LOG_MAIL) + elif not os.path.isdir(conf.getlogdir()): + printinfo('Creating logdir: ' + conf.getlogdir()) + os.mkdir(conf.getlogdir()) except Exception as e: - printerr("Error reading config file:", e) + printerr("Error reading config file: " + str(e)) sys.exit(os.EX_IOERR) try: @@ -458,7 +468,7 @@ if __name__ == "__main__": mq = MailQueue(conf) if cmd == "--send": - mail = sys.stdin.read() + mail = sys.stdin.buffer.read() mq.sendmail(mail, nooptargs) elif cmd == "--list": mq.listqueue()