move orders-accept configuration to eressea.ini

install lunit, too
This commit is contained in:
Enno Rehling 2017-01-20 08:32:31 +01:00
parent 5b78578939
commit d7dd281a6a
3 changed files with 61 additions and 54 deletions

View File

@ -23,4 +23,5 @@ add_subdirectory (src eressea)
install(DIRECTORY res conf DESTINATION ${CMAKE_INSTALL_PREFIX} FILES_MATCHING PATTERN "*.xml") install(DIRECTORY res conf DESTINATION ${CMAKE_INSTALL_PREFIX} FILES_MATCHING PATTERN "*.xml")
install(DIRECTORY res conf DESTINATION ${CMAKE_INSTALL_PREFIX} FILES_MATCHING PATTERN "*.json") install(DIRECTORY res conf DESTINATION ${CMAKE_INSTALL_PREFIX} FILES_MATCHING PATTERN "*.json")
install(DIRECTORY scripts DESTINATION ${CMAKE_INSTALL_PREFIX} FILES_MATCHING PATTERN "*.lua") install(DIRECTORY scripts DESTINATION ${CMAKE_INSTALL_PREFIX} FILES_MATCHING PATTERN "*.lua")
install(DIRECTORY lunit DESTINATION ${CMAKE_INSTALL_PREFIX} FILES_MATCHING PATTERN "*.lua")
install(DIRECTORY share DESTINATION ${CMAKE_INSTALL_PREFIX}) install(DIRECTORY share DESTINATION ${CMAKE_INSTALL_PREFIX})

View File

@ -1,3 +1,6 @@
[game]
email = Eressea Server <eressea-server@kn-bremen.de>
name = Eressea
[eressea] [eressea]
base = . base = .

View File

@ -3,22 +3,50 @@
from email.Utils import parseaddr from email.Utils import parseaddr
from email.Parser import Parser from email.Parser import Parser
from os import mkdir, rename, stat, utime, unlink, symlink import os
from os.path import exists import os.path
import ConfigParser
from re import compile, IGNORECASE from re import compile, IGNORECASE
from stat import ST_MTIME from stat import ST_MTIME
from string import upper, split, replace from string import upper, split, replace
import logging import logging
from sys import argv, stdin, exit import sys
from sys import stdin
from time import ctime, sleep, time from time import ctime, sleep, time
from socket import gethostname from socket import gethostname
from rfc822 import parsedate_tz, mktime_tz from rfc822 import parsedate_tz, mktime_tz
LOG_FILENAME='/home/eressea/log/orders.log' if 'ERESSEA' in os.environ:
dir = os.environ['ERESSEA']
elif 'HOME' in os.environ:
dir = os.path.join(os.environ['HOME'], '/eressea')
else: # WTF? No HOME?
dir = "/home/eressea/eressea"
if not os.path.isdir(dir):
print "please set the ERESSEA environment variable to the install path"
sys.exit(1)
rootdir = dir
try:
game = int(sys.argv[1])
except:
game = sys.argv[1]
gamedir = os.path.join(rootdir, "game-%d" % (game, ))
frommail = 'Eressea Server <eressea-server@eressea.kn-bremen.de>'
gamename = 'Eressea'
inifile = os.path.join(gamedir, 'eressea.ini')
if not os.path.exists(inifile):
print "no such file: " . inifile
else:
config = ConfigParser.ConfigParser()
config.read(inifile)
if config.has_option('game', 'email'):
frommail = config.get('game', 'email')
if config.has_option('game', 'name'):
gamename = config.get('game', 'name')
config = None
prefix = 'turn-' prefix = 'turn-'
hostname = gethostname() hostname = gethostname()
# base directory for all your games:
rootdir = "/home/eressea"
orderbase = "orders.dir" orderbase = "orders.dir"
sendmail = True sendmail = True
# maximum number of reports per sender: # maximum number of reports per sender:
@ -28,36 +56,21 @@ writeheaders = True
# reject all html email? # reject all html email?
rejecthtml = True rejecthtml = True
games = [
{
"from" : "Eressea Server <eressea-server@eressea.kn-bremen.de>",
"prefix" : "Eressea"
},
{
"from" : "Eressea Server <eressea-server@eressea.kn-bremen.de>",
"prefix": "E3"
},
{
"from" : "Eressea Server <eressea-server@eressea.kn-bremen.de>",
"prefix": "E4"
},
]
def unlock_file(filename): def unlock_file(filename):
try: try:
unlink(filename+".lock") os.unlink(filename+".lock")
except: except:
print "could not unlock %s.lock, file not found" % filename print "could not unlock %s.lock, file not found" % filename
def lock_file(filename): def lock_file(filename):
i = 0 i = 0
wait = 1 wait = 1
if not exists(filename): if not os.path.exists(filename):
file=open(filename, "w") file=open(filename, "w")
file.close() file.close()
while True: while True:
try: try:
symlink(filename, filename+".lock") os.symlink(filename, filename+".lock")
return return
except: except:
i = i+1 i = i+1
@ -74,17 +87,17 @@ messages = {
"software and re-send the orders.", "software and re-send the orders.",
"multipart-de" : "multipart-de" :
"FEHLER: Die von dir eingeschickte Mail enthält keinen " \ "FEHLER: Die von dir eingeschickte Mail enth<EFBFBD>lt keinen " \
"Text. Evtl. hast Du den Zug als HTML oder als anderweitig " \ "Text. Evtl. hast Du den Zug als HTML oder als anderweitig " \
"ungültig formatierte Mail ingeschickt. Wir können ihn " \ "ung<EFBFBD>ltig formatierte Mail ingeschickt. Wir k<>nnen ihn " \
"deshalb nicht berücksichtigen. Schicke den Zug nochmals " \ "deshalb nicht ber<EFBFBD>cksichtigen. Schicke den Zug nochmals " \
"als reinen Text ohne Formatierungen ein.", "als reinen Text ohne Formatierungen ein.",
"maildate-de": "maildate-de":
"Es erreichte uns bereits ein Zug mit einem späteren " \ "Es erreichte uns bereits ein Zug mit einem sp<EFBFBD>teren " \
"Absendedatum (%s > %s). Entweder ist deine " \ "Absendedatum (%s > %s). Entweder ist deine " \
"Systemzeit verstellt, oder ein Zug hat einen anderen Zug von " \ "Systemzeit verstellt, oder ein Zug hat einen anderen Zug von " \
"dir auf dem Transportweg überholt. Entscheidend für die " \ "dir auf dem Transportweg <EFBFBD>berholt. Entscheidend f<>r die " \
"Auswertungsreihenfolge ist das Absendedatum, d.h. der Date:-Header " \ "Auswertungsreihenfolge ist das Absendedatum, d.h. der Date:-Header " \
"deiner Mail.", "deiner Mail.",
@ -173,8 +186,8 @@ def available_file(dirname, basename):
ver = 0 ver = 0
maxdate = 0 maxdate = 0
filename = "%s/%s,%s,%d" % (dirname, basename, hostname, ver) filename = "%s/%s,%s,%d" % (dirname, basename, hostname, ver)
while exists(filename): while os.path.exists(filename):
maxdate = max(stat(filename)[ST_MTIME], maxdate) maxdate = max(os.stat(filename)[ST_MTIME], maxdate)
ver = ver + 1 ver = ver + 1
filename = "%s/%s,%s,%d" % (dirname, basename, hostname, ver) filename = "%s/%s,%s,%d" % (dirname, basename, hostname, ver)
if ver >= maxfiles: if ver >= maxfiles:
@ -234,7 +247,7 @@ def copy_orders(message, filename, sender):
from os.path import split from os.path import split
dirname, basename = split(filename) dirname, basename = split(filename)
dirname = dirname + '/headers' dirname = dirname + '/headers'
if not exists(dirname): mkdir(dirname) if not os.path.exists(dirname): os.mkdir(dirname)
outfile = open(dirname + '/' + basename, "w") outfile = open(dirname + '/' + basename, "w")
for name, value in message.items(): for name, value in message.items():
outfile.write(name + ": " + value + "\n") outfile.write(name + ": " + value + "\n")
@ -265,16 +278,14 @@ def copy_orders(message, filename, sender):
# create a file, containing: # create a file, containing:
# game=0 locale=de file=/path/to/filename email=rcpt@domain.to # game=0 locale=de file=/path/to/filename email=rcpt@domain.to
def accept(game, locale, stream, extend=None): def accept(game, locale, stream, extend=None):
global rootdir, orderbase global rootdir, orderbase, gamedir, gamename, frommail
if extend is not None: if extend is not None:
orderbase = orderbase + ".pre-" + extend orderbase = orderbase + ".pre-" + extend
gamename = games[game-2]["prefix"] savedir = os.path.join(gamedir, orderbase)
gamedir = rootdir+"/eressea/game-%d" % (game, )
savedir = gamedir+"/"+orderbase
# check if it's one of the pre-sent orders. # check if it's one of the pre-sent orders.
# create the save-directories if they don't exist # create the save-directories if they don't exist
if not exists(gamedir): mkdir(gamedir) if not os.path.exists(gamedir): os.mkdir(gamedir)
if not exists(savedir): mkdir(savedir) if not os.path.exists(savedir): os.mkdir(savedir)
# parse message # parse message
message = Parser().parse(stream) message = Parser().parse(stream)
sender = get_sender(message) sender = get_sender(message)
@ -298,7 +309,7 @@ def accept(game, locale, stream, extend=None):
maildate = message.get("Date") maildate = message.get("Date")
if maildate != None: if maildate != None:
turndate = mktime_tz(parsedate_tz(maildate)) turndate = mktime_tz(parsedate_tz(maildate))
utime(filename, (turndate, turndate)) os.utime(filename, (turndate, turndate))
logger.debug("mail date is '%s' (%d)" % (maildate, turndate)) logger.debug("mail date is '%s' (%d)" % (maildate, turndate))
if turndate < maxdate: if turndate < maxdate:
logger.warning("inconsistent message date " + sender) logger.warning("inconsistent message date " + sender)
@ -313,9 +324,9 @@ def accept(game, locale, stream, extend=None):
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"
logger.warning("rejected - no text/plain in orders from " + sender) logger.warning("rejected - no text/plain in orders from " + sender)
unlink(filename) os.unlink(filename)
savedir = savedir + "/rejected" savedir = savedir + "/rejected"
if not exists(savedir): mkdir(savedir) if not os.path.exists(savedir): os.mkdir(savedir)
lock_file(gamedir + "/orders.queue") lock_file(gamedir + "/orders.queue")
maxdate, filename = available_file(savedir, prefix + sender) maxdate, filename = available_file(savedir, prefix + sender)
store_message(message, filename) store_message(message, filename)
@ -323,7 +334,6 @@ def accept(game, locale, stream, extend=None):
fail = True fail = True
if sendmail and warning is not None: if sendmail and warning is not None:
frommail = games[key]["from"]
subject = gamename + " " + messages["subject-"+locale] + warning subject = gamename + " " + messages["subject-"+locale] + warning
mail = "Subject: %s\nFrom: %s\nTo: %s\n\n" % (subject, frommail, sender) + msg mail = "Subject: %s\nFrom: %s\nTo: %s\n\n" % (subject, frommail, sender) + msg
from smtplib import SMTP from smtplib import SMTP
@ -347,22 +357,15 @@ def accept(game, locale, stream, extend=None):
return 0 return 0
# the main body of the script: # the main body of the script:
LOG_FILENAME=os.path.join(rootdir, 'log/orders.log')
logging.basicConfig(level=logging.DEBUG, filename=LOG_FILENAME) 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
try: locale = sys.argv[2]
game = int(argv[1])
except:
game = argv[1]
if game[:3]=='e3a':
game = 3
elif game[:7]=='eressea':
game = 2
locale = argv[2]
infile = stdin infile = stdin
if len(argv)>3: if len(sys.argv)>3:
infile = open(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!=stdin:
infile.close() infile.close()
exit(retval) sys.exit(retval)