Merge pull request #852 from ennorehling/master

WIP: implement correct orders.db handling
This commit is contained in:
Enno Rehling 2019-05-08 18:43:30 +02:00 committed by GitHub
commit 4efe63d3e2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 28 deletions

View file

@ -1,21 +1,19 @@
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from email.Utils import parseaddr
from email.Parser import Parser
import os import os
import os.path import os.path
import ConfigParser import ConfigParser
from re import compile, IGNORECASE import string
from stat import ST_MTIME
from string import upper, split, replace
import logging import logging
import sys import sys
import subprocess import subprocess
from sys import stdin
import time import time
from socket import gethostname import socket
from rfc822 import parsedate_tz, mktime_tz import rfc822
from stat import ST_MTIME
from email.Utils import parseaddr
from email.Parser import Parser
if 'ERESSEA' in os.environ: if 'ERESSEA' in os.environ:
dir = os.environ['ERESSEA'] dir = os.environ['ERESSEA']
@ -50,16 +48,13 @@ else:
sender = "%s Server <%s>" % (gamename, frommail) sender = "%s Server <%s>" % (gamename, frommail)
config = None config = None
prefix = 'turn-' prefix = 'turn-'
hostname = gethostname() hostname = socket.gethostname()
orderbase = "orders.dir" orderbase = "orders.dir"
sendmail = True sendmail = True
# maximum number of reports per sender: # maximum number of reports per sender:
maxfiles = 30 maxfiles = 30
# write headers to file? # write headers to file?
writeheaders = True writeheaders = True
# write received files to datrabase?
tooldir = os.path.join(rootdir, 'orders-php')
writedb = os.path.exists(tooldir)
# reject all html email? # reject all html email?
rejecthtml = True rejecthtml = True
@ -180,7 +175,7 @@ def available_file(dirname, basename):
return maxdate, filename return maxdate, filename
def formatpar(string, l=76, indent=2): def formatpar(string, l=76, indent=2):
words = split(string) words = string.split(string)
res = "" res = ""
ll = 0 ll = 0
first = 1 first = 1
@ -237,13 +232,6 @@ def copy_orders(message, filename, sender, mtime):
outfile.write(name + ": " + value + "\n") outfile.write(name + ": " + value + "\n")
outfile.close() outfile.close()
if writedb:
dirname, basename = os.path.split(filename)
cli = os.path.join(tooldir, 'cli.php');
dbname = os.path.join(dirname, 'orders.db')
datestr = time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(mtime))
subprocess.call(['php', cli, '-d', dbname, 'insert', basename, sender, datestr])
found = False found = False
outfile = open(filename, "w") outfile = open(filename, "w")
if message.is_multipart(): if message.is_multipart():
@ -264,6 +252,7 @@ def copy_orders(message, filename, sender, mtime):
charset = message.get_content_charset() charset = message.get_content_charset()
logger.error("could not write text/plain message (charset=%s) for %s" % (charset, sender)) logger.error("could not write text/plain message (charset=%s) for %s" % (charset, sender))
outfile.close() outfile.close()
return found return found
# create a file, containing: # create a file, containing:
@ -298,7 +287,7 @@ def accept(game, locale, stream, extend=None):
if maildate is None: if maildate is None:
turndate = time.time() turndate = time.time()
else: else:
turndate = mktime_tz(parsedate_tz(maildate)) turndate = rfc822.mktime_tz(rfc822.parsedate_tz(maildate))
text_ok = copy_orders(message, filename, email, turndate) text_ok = copy_orders(message, filename, email, turndate)
warning, msg, fail = None, "", False warning, msg, fail = None, "", False
@ -314,6 +303,9 @@ def accept(game, locale, stream, extend=None):
warning = " (" + messages["warning-" + locale] + ")" warning = " (" + messages["warning-" + locale] + ")"
msg = msg + formatpar(messages["nodate-" + locale], 76, 2) + "\n" msg = msg + formatpar(messages["nodate-" + locale], 76, 2) + "\n"
print('ACCEPT_MAIL=' + email)
print('ACCEPT_FILE="' + filename + '"')
if not text_ok: if not text_ok:
warning = " (" + messages["error-" + locale] + ")" warning = " (" + messages["error-" + locale] + ")"
msg = msg + formatpar(messages["multipart-" + locale], 76, 2) + "\n" msg = msg + formatpar(messages["multipart-" + locale], 76, 2) + "\n"
@ -354,10 +346,10 @@ logging.basicConfig(level=logging.DEBUG, filename=LOG_FILENAME)
logger = logging logger = logging
delay = None # TODO: parse the turn delay delay = None # TODO: parse the turn delay
locale = sys.argv[2] locale = sys.argv[2]
infile = stdin infile = sys.stdin
if len(sys.argv)>3: if len(sys.argv)>3:
infile = open(sys.argv[3], "r") infile = open(sys.argv[3], "r")
retval = accept(game, locale, infile, delay) retval = accept(game, locale, infile, delay)
if infile!=stdin: if infile!=sys.stdin:
infile.close() infile.close()
sys.exit(retval) sys.exit(retval)

View file

@ -1,8 +1,25 @@
#!/bin/sh #!/bin/sh
SCRIPT=$(readlink -f $0) # example: orders-accept 2 de < mail.txt
cd $(dirname $SCRIPT)
lockfile -r3 -l120 orders.queue.lock game="$1"
python accept-orders.py "$@"
rm -f orders.queue.lock
[ -z "$ERESSEA" ] && ERESSEA="$HOME/eressea"
SCRIPT=$(readlink -f "$0")
BIN=$(dirname "$SCRIPT")
LOCKFILE="$ERESSEA/game-$game/orders.queue.lock"
set -e
trap 'rm -f "$LOCKFILE"' EXIT
cd "$ERESSEA/game-$game"
mkdir -p orders.dir
cd orders.dir
lockfile -r3 -l120 "$LOCKFILE"
eval "$(python "$BIN/accept-orders.py" "$@")"
filename=$(basename "$ACCEPT_FILE")
email="$ACCEPT_MAIL"
if [ -d "$ERESSEA/orders-php" ]
then
php "$ERESSEA/orders-php/cli.php" insert "$filename" "$email"
fi
rm -f "$LOCKFILE"