From 1b2bfc79952c38b1886e3d38be6de8951b57a72c Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Fri, 20 Jan 2017 18:44:58 +0100 Subject: [PATCH] rename [eressea] section to [game] --- conf/eressea.ini | 5 +- process/orders-accept | 49 ++++++++++--------- process/orders-process | 107 +++++++++++++++++++++++++---------------- s/setup | 4 +- src/main.c | 14 +++--- 5 files changed, 102 insertions(+), 77 deletions(-) diff --git a/conf/eressea.ini b/conf/eressea.ini index c2f7bbb7f..b6db180ca 100644 --- a/conf/eressea.ini +++ b/conf/eressea.ini @@ -1,8 +1,7 @@ [game] -email = Eressea Server +email = eressea-server@kn-bremen.de +sender = Eressea Server name = Eressea - -[eressea] base = . report = reports verbose = 0 diff --git a/process/orders-accept b/process/orders-accept index 49bdc3cb0..78af0a056 100755 --- a/process/orders-accept +++ b/process/orders-accept @@ -27,13 +27,12 @@ if not os.path.isdir(dir): sys.exit(1) rootdir = dir -try: - game = int(sys.argv[1]) -except: - game = sys.argv[1] +game = int(sys.argv[1]) gamedir = os.path.join(rootdir, "game-%d" % (game, )) -frommail = 'Eressea Server ' +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 @@ -44,6 +43,10 @@ else: 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 prefix = 'turn-' hostname = gethostname() @@ -278,7 +281,7 @@ def copy_orders(message, filename, sender): # create a file, containing: # game=0 locale=de file=/path/to/filename email=rcpt@domain.to def accept(game, locale, stream, extend=None): - global rootdir, orderbase, gamedir, gamename, frommail + global rootdir, orderbase, gamedir, gamename, sender if extend is not None: orderbase = orderbase + ".pre-" + extend savedir = os.path.join(gamedir, orderbase) @@ -288,21 +291,21 @@ def accept(game, locale, stream, extend=None): if not os.path.exists(savedir): os.mkdir(savedir) # parse message message = Parser().parse(stream) - sender = get_sender(message) - logger = logging.getLogger(sender) + email = get_sender(message) + logger = logging.getLogger(email) # write syslog - if sender is None or valid_email(sender)==0: - logger.warning("invalid email address: " + str(sender)) + if email is None or valid_email(email)==0: + logger.warning("invalid email address: " + str(email)) return -1 - logger.info("received orders from " + sender) + logger.info("received orders from " + email) # get an available filename lock_file(gamedir + "/orders.queue") - maxdate, filename = available_file(savedir, prefix + sender) + maxdate, filename = available_file(savedir, prefix + email) if filename is None: - logger.warning("more than " + str(maxfiles) + " orders from " + sender) + logger.warning("more than " + str(maxfiles) + " orders from " + email) return -1 # copy the orders to the file - text_ok = copy_orders(message, filename, sender) + text_ok = copy_orders(message, filename, email) unlock_file(gamedir + "/orders.queue") warning, msg, fail = None, "", False @@ -312,47 +315,47 @@ def accept(game, locale, stream, extend=None): os.utime(filename, (turndate, turndate)) logger.debug("mail date is '%s' (%d)" % (maildate, turndate)) if turndate < maxdate: - logger.warning("inconsistent message date " + sender) + logger.warning("inconsistent message date " + email) warning = " (" + messages["warning-" + locale] + ")" msg = msg + formatpar(messages["maildate-" + locale] % (ctime(maxdate),ctime(turndate)), 76, 2) + "\n" else: - logger.warning("missing message date " + sender) + logger.warning("missing message date " + email) warning = " (" + messages["warning-" + locale] + ")" msg = msg + formatpar(messages["nodate-" + locale], 76, 2) + "\n" if not text_ok: warning = " (" + messages["error-" + locale] + ")" 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 " + email) os.unlink(filename) savedir = savedir + "/rejected" if not os.path.exists(savedir): os.mkdir(savedir) lock_file(gamedir + "/orders.queue") - maxdate, filename = available_file(savedir, prefix + sender) + maxdate, filename = available_file(savedir, prefix + email) store_message(message, filename) unlock_file(gamedir + "/orders.queue") fail = True if sendmail and warning is not None: 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, sender, email) + msg from smtplib import SMTP server = SMTP("localhost") - server.sendmail(frommail, sender, mail) + server.sendmail(sender, email, mail) server.close() if not sendmail: - print text_ok, fail, sender + print text_ok, fail, email print filename if not fail: lock_file(gamedir + "/orders.queue") queue = open(gamedir + "/orders.queue", "a") - queue.write("email=%s file=%s locale=%s game=%s\n" % (sender, filename, locale, game)) + queue.write("email=%s file=%s locale=%s game=%s\n" % (email, filename, locale, game)) queue.close() unlock_file(gamedir + "/orders.queue") - logger.info("done - accepted orders from " + sender) + logger.info("done - accepted orders from " + email) return 0 diff --git a/process/orders-process b/process/orders-process index 753385e2e..b33cc05bf 100755 --- a/process/orders-process +++ b/process/orders-process @@ -2,7 +2,9 @@ # -*- coding: iso-8859-1 -*- from os import unlink, symlink, rename, popen, tmpfile -from os.path import exists +import os +import os.path +import ConfigParser from re import compile, IGNORECASE from string import split, join, upper, strip from sys import argv, exit @@ -14,9 +16,8 @@ from epasswd import EPasswd def pwd_get_email(faction, pwd, pwdfile=None): return None -def splitfilename(filename): - from os.path import split - return split(filename) +def split_filename(filename): + return os.path.split(filename) def unlock_file(filename): try: @@ -28,7 +29,7 @@ def unlock_file(filename): def lock_file(filename): i = 0 wait = 1 - if not exists(filename): + if not os.path.exists(filename): file=open(filename, "w") file.close() while True: @@ -62,17 +63,47 @@ messages = { "error-en": "Error", } +game = int(sys.argv[1]) +echeck_cmd = "/home/eressea/echeck/echeck.sh" +maxlines = 25 # base directory for all your games: -rootdir = "/home/eressea/eressea" -frommail = "Eressea Server " -orderbase = "orders.dir" -sendmail = True -maxlines = 25 -echeck_cmd = "/home/eressea/echeck/echeck.sh" +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) # regular expression that finds the start of a faction -fact_re = compile("^\s*(eressea|vinyambar|partei|faction)\s+([a-zA-Z0-9]+)\s+\"?([^\"]*)\"?", IGNORECASE) +fact_re = compile("^\s*(eressea|partei|faction)\s+([a-zA-Z0-9]+)\s+\"?([^\"]*)\"?", IGNORECASE) def check_pwd(filename, email, pw_data): results = [] @@ -98,7 +129,7 @@ def check_pwd(filename, email, pw_data): return results def echeck(filename, locale, rules): - dirname, filename = splitfilename(filename) + dirname, filename = split_filename(filename) stream = popen("%s %s %s %s %s" % (echeck_cmd, locale, filename, dirname, rules), 'r') lines = stream.readlines() if len(lines)==0: @@ -111,14 +142,6 @@ def echeck(filename, locale, rules): stream.close() return mail -## the main body of the script -game = int(argv[1]) - -basedir = rootdir + "/game-%d" % (game, ) -queuename = basedir + "/orders.queue" -if not exists(queuename): - exit(0) - # parse the queue file - #print "connecting to SMTP..." from smtplib import SMTP @@ -127,16 +150,17 @@ try: except: print "could not connect to SMTP server" exit(0) + #print "reading password file..." -pw_data = EPasswd(basedir + "/passwd") +pw_data = EPasswd(os.path.join(game_dir,"passwd")) #print "reading orders.queue..." # move the queue file to a save space while locking it: try: - lock_file(queuename) + lock_file(queue_file) except: exit(0) -queuefile = open(queuename, "r") +queuefile = open(queue_file, "r") lines = queuefile.readlines() queuefile.close() @@ -154,9 +178,9 @@ tmpfile.close() openlog("orders") -unlink(queuename) +unlink(queue_file) try: - unlock_file(queuename) + unlock_file(queue_file) except: pass @@ -170,15 +194,15 @@ for line in lines: email = dict["email"] locale = dict["locale"] game = int(dict["game"]) - file = dict["file"] + infile = dict["file"] gamename='[E%d]' % game rules='e%d' % game warning = "" failed = True - results = check_pwd(file, email, pw_data) - logfile = open(basedir+"/zug.log", "a") - dirname, filename = splitfilename(file) - msg = messages["validate-"+locale] + " " + filename + "\n\n" + 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" for faction, game_email, success, pwd in results: msg = msg + messages["faction-"+locale] + " " + faction + "\n" if success: failed = False @@ -189,25 +213,24 @@ for line in lines: if failed: warning = " (" + messages["warning-" + locale] + ")" - syslog("failed - no valid password in " + file) + syslog("failed - no valid password in " + infile) else: - result = echeck(file, locale, rules) - if email=='eressea': - print result - continue - elif result is None: + result = None + if os.path.exists(echeck_cmd): + result = echeck(infile, locale, rules) + if result is None: # echeck did not finish - msg = msg + "Echeck was killed. Your turn was accepted, but could not be verified.\n" + msg = msg + "Echeck is broken. Your turn was accepted, but could not be verified.\n" warning = " (" + messages["warning-" + locale] + ")" - syslog("process - echeck got killed, " + file) + syslog("process - echeck broken, " + infile) else: msg = msg + result - syslog("process - checked orders in " + file) + syslog("process - checked orders in " + infile) subject = gamename + " " + messages["subject-" + locale] + warning - msg = "Subject: %s\nFrom: %s\nTo: %s\nContent-Type: text/plain; charset=utf-8\n\n" % (subject, frommail, email) + msg + msg = "Subject: %s\nFrom: %s\nTo: %s\nContent-Type: text/plain; charset=utf-8\n\n" % (subject, sender, email) + msg try: - server.sendmail(frommail, email, msg) + server.sendmail(sender, email, msg) except: syslog("failed - cannot send to " + email) diff --git a/s/setup b/s/setup index fc5fb9874..d8ffa74ad 100755 --- a/s/setup +++ b/s/setup @@ -94,8 +94,8 @@ touch eressea.ini } ini_start -ini_sec eressea -ini_add eressea locales de,en +ini_sec game +ini_add game locales de,en ini_sec lua ini_add lua install $SOURCE ini_add lua paths $SOURCE/scripts:$SOURCE/lunit diff --git a/src/main.c b/src/main.c index 5e998f835..2d81041ec 100644 --- a/src/main.c +++ b/src/main.c @@ -54,23 +54,23 @@ static void load_inifile(dictionary * d) assert(d); - str = iniparser_getstring(d, "eressea:base", basedir); + str = iniparser_getstring(d, "game:base", basedir); if (str != basedir) { set_basepath(str); } - str = iniparser_getstring(d, "eressea:report", reportdir); + str = iniparser_getstring(d, "game:report", reportdir); if (str != reportdir) { set_reportpath(str); } - str = iniparser_getstring(d, "eressea:data", datadir); + str = iniparser_getstring(d, "game:data", datadir); if (str != datadir) { set_datapath(str); } - lomem = iniparser_getint(d, "eressea:lomem", lomem) ? 1 : 0; + lomem = iniparser_getint(d, "game:lomem", lomem) ? 1 : 0; - verbosity = iniparser_getint(d, "eressea:verbose", 2); - str = iniparser_getstring(d, "eressea:locales", "de,en"); + verbosity = iniparser_getint(d, "game:verbose", 2); + str = iniparser_getstring(d, "game:locales", "de,en"); make_locales(str); if (global.inifile) iniparser_freedict(global.inifile); @@ -84,7 +84,7 @@ static void parse_config(const char *filename) load_inifile(d); log_debug("reading from configuration file %s\n", filename); - memdebug = iniparser_getint(d, "eressea:memcheck", memdebug); + memdebug = iniparser_getint(d, "game:memcheck", memdebug); #ifdef USE_CURSES /* only one value in the [editor] section */ force_color = iniparser_getint(d, "editor:color", force_color);