From e03f3c95a3ac3f3a41d0ad963a5a863b0dc414cd Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Thu, 6 Dec 2018 20:04:45 +0100 Subject: [PATCH] BUG 2531: new factions get a fresh password every turn until they send their first orders, which is shown in the report. --- res/translations/messages.de.po | 2 +- src/gmtool.c | 7 +++---- src/kernel/faction.c | 8 +++++++- src/kernel/faction.h | 1 + src/kernel/faction.test.c | 6 +++--- src/laws.c | 33 ++++++++++++++++++++++----------- src/laws.h | 1 + src/modules/autoseed.c | 7 ++----- src/reports.c | 3 +++ src/reports.test.c | 23 +++++++++++++++++++++++ 10 files changed, 66 insertions(+), 25 deletions(-) diff --git a/res/translations/messages.de.po b/res/translations/messages.de.po index 2230341c5..a9a87662e 100644 --- a/res/translations/messages.de.po +++ b/res/translations/messages.de.po @@ -2331,7 +2331,7 @@ msgid "curseinfo::magicboost" msgstr "Der Magier besitzt die Gabe des Chaos. ($int36($id))" msgid "illegal_password" -msgstr "\"Dein Passwort enthält Zeichen, die bei der Nachsendung von Reports Probleme bereiten können. Bitte beachte, dass Passwortenur aus Buchstaben von A bis Z und Zahlen bestehen dürfen. Dein neues Passwort ist '${newpass}'.\"" +msgstr "\"Das Passwort enthielt Zeichen, die bei der Nachsendung von Reports Probleme bereiten können. Bitte beachte, dass Passworte nur aus Ziffern und Buchstaben von A bis Z bestehen dürfen. Dein neues Passwort ist '${newpass}'.\"" msgid "curseinfo::sparkle_12" msgstr "\"Leuchtende Blumen erblühen rund um das Lager von $unit($unit). ($int36($id))\"" diff --git a/src/gmtool.c b/src/gmtool.c index 3b7aed36a..0ab31854e 100644 --- a/src/gmtool.c +++ b/src/gmtool.c @@ -1065,10 +1065,9 @@ static void seed_player(state *st, const newfaction *player) { pnormalize(&nx, &ny, st->cursor.pl); r = findregion(nx, ny); if (r) { - faction *f; - const char *password = player->password ? player->password : itoa36(rng_int()); - addplayer(r, f = addfaction(player->email, password, - player->race, player->lang)); + faction *f = addfaction(player->email, player->password, + player->race, player->lang); + addplayer(r, f); } } } diff --git a/src/kernel/faction.c b/src/kernel/faction.c index c0c908186..fe1c5daa5 100755 --- a/src/kernel/faction.c +++ b/src/kernel/faction.c @@ -227,6 +227,12 @@ static int unused_faction_id(void) return id; } +void faction_genpassword(faction *f) { + const char * password = itoa36(rng_int()); + faction_setpassword(f, password_hash(password, PASSWORD_DEFAULT)); + ADDMSG(&f->msgs, msg_message("changepasswd", "value", password)); +} + faction *addfaction(const char *email, const char *password, const struct race * frace, const struct locale * loc) { @@ -241,7 +247,7 @@ faction *addfaction(const char *email, const char *password, } f->alliance_joindate = turn; - f->lastorders = turn; + f->lastorders = 0; f->_alive = true; f->password_id = 0; f->age = 0; diff --git a/src/kernel/faction.h b/src/kernel/faction.h index 0c579603a..05790a865 100644 --- a/src/kernel/faction.h +++ b/src/kernel/faction.h @@ -151,6 +151,7 @@ extern "C" { const char *faction_getemail(const struct faction *self); void faction_setemail(struct faction *self, const char *email); + void faction_genpassword(struct faction *f); void faction_setpassword(struct faction *self, const char *pwhash); const char *faction_getpassword(const struct faction *f); bool valid_race(const struct faction *f, const struct race *rc); diff --git a/src/kernel/faction.test.c b/src/kernel/faction.test.c index 66e3c3945..65310ee3a 100644 --- a/src/kernel/faction.test.c +++ b/src/kernel/faction.test.c @@ -110,7 +110,7 @@ static void test_addfaction(CuTest *tc) { test_setup(); rc = rc_get_or_create("human"); lang = test_create_locale(); - f = addfaction("test@eressea.de", NULL, rc, lang); + f = addfaction("test@example.com", NULL, rc, lang); CuAssertPtrNotNull(tc, f); CuAssertPtrNotNull(tc, f->name); CuAssertPtrEquals(tc, NULL, (void *)f->units); @@ -119,14 +119,14 @@ static void test_addfaction(CuTest *tc) { CuAssertPtrEquals(tc, NULL, (void *)f->spellbook); CuAssertPtrEquals(tc, NULL, (void *)f->origin); CuAssertPtrEquals(tc, (void *)factions, (void *)f); - CuAssertStrEquals(tc, "test@eressea.de", f->email); + CuAssertStrEquals(tc, "test@example.com", f->email); CuAssertTrue(tc, checkpasswd(f, "hurrdurr")); CuAssertPtrEquals(tc, (void *)lang, (void *)f->locale); CuAssertIntEquals(tc, FFL_ISNEW|FFL_PWMSG, f->flags); CuAssertIntEquals(tc, 0, f->age); CuAssertTrue(tc, faction_alive(f)); CuAssertIntEquals(tc, M_GRAY, f->magiegebiet); - CuAssertIntEquals(tc, turn, f->lastorders); + CuAssertIntEquals(tc, 0, f->lastorders); CuAssertPtrEquals(tc, f, findfaction(f->no)); test_teardown(); } diff --git a/src/laws.c b/src/laws.c index 4a153fc16..fc1cae7e7 100644 --- a/src/laws.c +++ b/src/laws.c @@ -154,8 +154,11 @@ bool IsImmune(const faction * f) int NMRTimeout(void) { - int nmr_timeout = config_get_int("nmr.timeout", 0); - int ini_timeout = config_get_int("game.maxnmr", 0); + static int config, nmr_timeout, ini_timeout; + if (config_changed(&config)) { + nmr_timeout = config_get_int("nmr.timeout", 0); + ini_timeout = config_get_int("game.maxnmr", 0); + } if (nmr_timeout > 0) { if (ini_timeout > nmr_timeout) { return nmr_timeout; @@ -1192,17 +1195,26 @@ void do_enter(struct region *r, bool is_final_attempt) int dropouts[2]; int *age = NULL; -static void nmr_death(faction * f) +bool nmr_death(const faction * f, int turn, int timeout) { - int rule = config_get_int("rules.nmr.destroy", 0) != 0; - if (rule) { - unit *u; - for (u = f->units; u; u = u->nextF) { - if (u->building && building_owner(u->building) == u) { - remove_building(&u->region->buildings, u->building); + if (f->age >= timeout && turn - f->lastorders >= timeout) { + static bool rule_destroy; + static int config; + + if (config_changed(&config)) { + rule_destroy = config_get_int("rules.nmr.destroy", 0) != 0; + } + if (rule_destroy) { + unit *u; + for (u = f->units; u; u = u->nextF) { + if (u->building && building_owner(u->building) == u) { + remove_building(&u->region->buildings, u->building); + } } } + return true; } + return false; } static void remove_idle_players(void) @@ -1215,8 +1227,7 @@ static void remove_idle_players(void) for (fp = &factions; *fp;) { faction *f = *fp; - if (timeout > 0 && turn - f->lastorders >= timeout) { - nmr_death(f); + if (timeout > 0 && nmr_death(f, turn, timeout)) { destroyfaction(fp); } else { if (fval(f, FFL_NOIDLEOUT)) { diff --git a/src/laws.h b/src/laws.h index c30479b38..1f9632c60 100755 --- a/src/laws.h +++ b/src/laws.h @@ -95,6 +95,7 @@ extern "C" { int claim_cmd(struct unit *u, struct order *ord); void nmr_warnings(void); + bool nmr_death(const struct faction * f, int turn, int timeout); bool cansee(const struct faction * f, const struct region * r, const struct unit *u, int modifier); diff --git a/src/modules/autoseed.c b/src/modules/autoseed.c index bb31d3f4e..c16bb7d79 100644 --- a/src/modules/autoseed.c +++ b/src/modules/autoseed.c @@ -549,15 +549,13 @@ int autoseed(newfaction ** players, int nsize, int max_agediff) newfaction **nfp, *nextf = *players; faction *f; unit *u; - const char * password; isize += REGIONS_PER_FACTION; terraform_region(r, preferred_terrain(nextf->race)); prepare_starting_region(r); ++tsize; assert(r->land && r->units == 0); - password = nextf->password ? nextf->password : itoa36(rng_int()); - u = addplayer(r, addfaction(nextf->email, password, nextf->race, + u = addplayer(r, addfaction(nextf->email, nextf->password, nextf->race, nextf->lang)); f = u->faction; fset(f, FFL_ISNEW); @@ -857,8 +855,7 @@ static void starting_region(newfaction ** players, region * r, region * rn[]) newfaction *nf = *players; const struct race *rc = nf->race ? nf->race : races; const struct locale *lang = nf->lang ? nf->lang : default_locale; - const char * passwd = nf->password ? nf->password : itoa36(rng_int()); - addplayer(r, addfaction(nf->email, passwd, rc, lang)); + addplayer(r, addfaction(nf->email, nf->password, rc, lang)); *players = nf->next; free_newfaction(nf); } diff --git a/src/reports.c b/src/reports.c index 89f05572e..b7279f7ef 100644 --- a/src/reports.c +++ b/src/reports.c @@ -1606,6 +1606,9 @@ int write_reports(faction * f) if (noreports) { return false; } + if (f->lastorders == 0) { + faction_genpassword(f); + } prepare_report(&ctx, f); get_addresses(&ctx); log_debug("Reports for %s", factionname(f)); diff --git a/src/reports.test.c b/src/reports.test.c index db9f9634f..0ea5c0944 100644 --- a/src/reports.test.c +++ b/src/reports.test.c @@ -948,6 +948,28 @@ static void test_eval_functions(CuTest *tc) test_teardown(); } +static void test_reports_genpassword(CuTest *tc) { + faction *f; + int pwid; + + test_setup(); + mt_create_va(mt_new("changepasswd", NULL), "value:string", MT_NEW_END); + f = test_create_faction(NULL); + CuAssertIntEquals(tc, 0, f->lastorders); + CuAssertIntEquals(tc, 0, f->password_id); + f->options = 0; + write_reports(f); + CuAssertPtrNotNull(tc, test_find_messagetype(f->msgs, "changepasswd")); + CuAssertTrue(tc, f->password_id != 0); + test_clear_messagelist(&f->msgs); + f->lastorders = 1; + pwid = f->password_id; + write_reports(f); + CuAssertIntEquals(tc, pwid, f->password_id); + CuAssertPtrEquals(tc, NULL, test_find_messagetype(f->msgs, "changepasswd")); + test_teardown(); +} + CuSuite *get_reports_suite(void) { CuSuite *suite = CuSuiteNew(); @@ -979,5 +1001,6 @@ CuSuite *get_reports_suite(void) SUITE_ADD_TEST(suite, test_newbie_warning); SUITE_ADD_TEST(suite, test_visible_unit); SUITE_ADD_TEST(suite, test_eval_functions); + SUITE_ADD_TEST(suite, test_reports_genpassword); return suite; }