server/process/orders-process

242 lines
6.8 KiB
Plaintext
Raw Normal View History

2014-04-27 03:03:14 +02:00
#!/usr/bin/env python
# -*- coding: iso-8859-1 -*-
from os import unlink, symlink, rename, popen, tmpfile
2017-01-20 18:44:58 +01:00
import os
import os.path
import ConfigParser
2014-04-27 03:03:14 +02:00
from re import compile, IGNORECASE
from string import split, join, upper, strip
from sys import argv, exit
from time import sleep, time, ctime
from syslog import openlog, closelog, syslog
from epasswd import EPasswd
def pwd_get_email(faction, pwd, pwdfile=None):
return None
2017-01-20 18:44:58 +01:00
def split_filename(filename):
return os.path.split(filename)
2014-04-27 03:03:14 +02:00
def unlock_file(filename):
try:
unlink(filename+".lock")
except:
print "could not unlock %s.lock, file not found" % filename
raise
def lock_file(filename):
i = 0
wait = 1
2017-01-20 18:44:58 +01:00
if not os.path.exists(filename):
2014-04-27 03:03:14 +02:00
file=open(filename, "w")
file.close()
while True:
try:
symlink(filename, filename+".lock")
return
except:
i = i+1
if i == 5:
raise
sleep(wait)
wait = wait*2
messages = {
"subject-de": "Befehle angekommen",
"subject-en": "orders received",
"validate-en": "Validating",
"validate-de": "Verarbeite",
"faction-en": "Faction",
"faction-de": "Partei",
"unknown-de": "WARNUNG: Die Partei ist nicht bekannt, oder das Passwort falsch!",
"unknown-en": "WARNING: This faction is unknown, or the password is incorrect!",
"warning-de": "Warnung",
"warning-en": "Warning",
"error-de": "Fehler",
"error-en": "Error",
}
2017-01-20 18:44:58 +01:00
game = int(sys.argv[1])
echeck_cmd = "/home/eressea/echeck/echeck.sh"
maxlines = 25
2014-04-27 03:03:14 +02:00
# base directory for all your games:
2017-01-20 18:44:58 +01:00
install_dir = "/home/eressea/eressea"
if 'ERESSEA' in os.environ:
install_dir = os.environ['ERESSEA']
elif 'HOME' in os.environ:
install_dir = os.path.join(os.environ['HOME'], '/eressea')
if not os.path.isdir(install_dir):
print "please set the ERESSEA environment variable to the install path"
sys.exit(1)
game_dir = os.path.join(install_dir, "game-%d" % (game, ))
frommail = 'eressea-server@kn-bremen.de'
gamename = 'Eressea'
sender = '%s Server <%s>' % (gamename, frommail)
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')
if config.has_option('game', 'sender'):
sender = config.get('game', 'sender')
else:
sender = "%s Server <%s>" % (gamename, frommail)
config = None
queue_file = os.path.join(game_dir, "orders.queue")
if not os.path.exists(queue_file):
exit(0)
2014-04-27 03:03:14 +02:00
# regular expression that finds the start of a faction
2017-01-20 18:44:58 +01:00
fact_re = compile("^\s*(eressea|partei|faction)\s+([a-zA-Z0-9]+)\s+\"?([^\"]*)\"?", IGNORECASE)
2014-04-27 03:03:14 +02:00
def check_pwd(filename, email, pw_data):
results = []
try:
file = open(filename, "r")
except:
print "could not open file", filename
return results
for line in file.readlines():
mo = fact_re.search(strip(line))
if mo != None:
fact_nr = str(mo.group(2))
fact_pw = str(mo.group(3))
if pw_data.fac_exists(fact_nr):
2016-02-11 20:39:56 +01:00
if not pw_data.check(fact_nr, fact_pw):
2014-04-27 03:03:14 +02:00
game_email = pw_data.get_email(fact_nr)
results = results + [ (fact_nr, game_email, False, fact_pw) ]
else:
game_email = pw_data.get_email(fact_nr)
results = results + [ (fact_nr, game_email, True, fact_pw) ]
else:
results = results + [ (fact_nr, None, False, fact_pw) ]
return results
def echeck(filename, locale, rules):
2017-01-20 18:44:58 +01:00
dirname, filename = split_filename(filename)
2014-04-27 03:03:14 +02:00
stream = popen("%s %s %s %s %s" % (echeck_cmd, locale, filename, dirname, rules), 'r')
lines = stream.readlines()
if len(lines)==0:
stream.close()
return None
if len(lines)>maxlines:
mail = join(lines[:maxlines-3] + ["...", "\n"] + lines[-3:], '')
else:
mail = join(lines[:maxlines], '')
stream.close()
return mail
# parse the queue file -
#print "connecting to SMTP..."
from smtplib import SMTP
try:
server = SMTP("localhost")
except:
print "could not connect to SMTP server"
exit(0)
2017-01-20 18:44:58 +01:00
2014-04-27 03:03:14 +02:00
#print "reading password file..."
2017-01-20 18:44:58 +01:00
pw_data = EPasswd(os.path.join(game_dir,"passwd"))
2014-04-27 03:03:14 +02:00
#print "reading orders.queue..."
# move the queue file to a save space while locking it:
try:
2017-01-20 18:44:58 +01:00
lock_file(queue_file)
2014-04-27 03:03:14 +02:00
except:
exit(0)
2017-01-20 18:44:58 +01:00
queuefile = open(queue_file, "r")
2014-04-27 03:03:14 +02:00
lines = queuefile.readlines()
queuefile.close()
# copy to a temp file
tname="/tmp/orders.queue.%s" % str(time())
try:
lock_file(tname)
except:
exit(0)
tmpfile=open(tname, "w")
for line in lines:
tmpfile.write(line)
tmpfile.close()
openlog("orders")
2017-01-20 18:44:58 +01:00
unlink(queue_file)
2014-04-27 03:03:14 +02:00
try:
2017-01-20 18:44:58 +01:00
unlock_file(queue_file)
2014-04-27 03:03:14 +02:00
except:
pass
for line in lines:
tokens = split(line[:-1], ' ')
dict = {}
for token in tokens:
name, value = split(token, '=')
dict[name] = value
email = dict["email"]
locale = dict["locale"]
game = int(dict["game"])
2017-01-20 18:44:58 +01:00
infile = dict["file"]
2014-04-27 03:03:14 +02:00
gamename='[E%d]' % game
rules='e%d' % game
warning = ""
failed = True
2017-01-20 18:44:58 +01:00
results = check_pwd(infile, email, pw_data)
logfile = open(os.path.join(game_dir, "zug.log"), "a")
dirname, filename = split_filename(infile)
msg = messages["validate-"+locale] + " " + infilename + "\n\n"
2014-04-27 03:03:14 +02:00
for faction, game_email, success, pwd in results:
msg = msg + messages["faction-"+locale] + " " + faction + "\n"
if success: failed = False
else: msg = msg + messages["unknown-"+locale] + "\n"
msg = msg + "\n"
logfile.write("%s:%s:%s:%s:%s:%s\n" % (ctime(time()), email, game_email, faction, pwd, success))
logfile.close()
if failed:
warning = " (" + messages["warning-" + locale] + ")"
2017-01-20 18:44:58 +01:00
syslog("failed - no valid password in " + infile)
2014-04-27 03:03:14 +02:00
else:
2017-01-20 18:44:58 +01:00
result = None
if os.path.exists(echeck_cmd):
result = echeck(infile, locale, rules)
if result is None:
2014-04-27 03:03:14 +02:00
# echeck did not finish
2017-01-20 18:44:58 +01:00
msg = msg + "Echeck is broken. Your turn was accepted, but could not be verified.\n"
2014-04-27 03:03:14 +02:00
warning = " (" + messages["warning-" + locale] + ")"
2017-01-20 18:44:58 +01:00
syslog("process - echeck broken, " + infile)
2014-04-27 03:03:14 +02:00
else:
msg = msg + result
2017-01-20 18:44:58 +01:00
syslog("process - checked orders in " + infile)
2014-04-27 03:03:14 +02:00
subject = gamename + " " + messages["subject-" + locale] + warning
2017-01-20 18:44:58 +01:00
msg = "Subject: %s\nFrom: %s\nTo: %s\nContent-Type: text/plain; charset=utf-8\n\n" % (subject, sender, email) + msg
2014-04-27 03:03:14 +02:00
try:
2017-01-20 18:44:58 +01:00
server.sendmail(sender, email, msg)
2014-04-27 03:03:14 +02:00
except:
syslog("failed - cannot send to " + email)
server.close()
closelog()
unlink(tname)
unlock_file(tname)