From 0aa5a64c632537dae8b682b07b3a9db9ee9748d8 Mon Sep 17 00:00:00 2001 From: Steffen Mecke Date: Tue, 14 Nov 2017 16:20:31 +0100 Subject: [PATCH] allow empty faction email --- src/creport.c | 6 +++--- src/json.c | 2 +- src/kernel/faction.c | 11 ++++++++--- src/kernel/faction.test.c | 31 +++++++++++++++++++++---------- src/kernel/save.c | 10 ++++++---- src/laws.c | 8 ++++---- src/modules/autoseed.c | 7 +++++-- src/monsters.c | 2 +- src/report.c | 21 +++++++++++++-------- src/reports.c | 8 ++++++-- src/sqlite.c | 5 +++-- src/util/goodies.c | 10 +++------- src/util/goodies.h | 2 +- 13 files changed, 75 insertions(+), 48 deletions(-) diff --git a/src/creport.c b/src/creport.c index 15e1791f6..9ef2b1e65 100644 --- a/src/creport.c +++ b/src/creport.c @@ -1055,8 +1055,8 @@ static void cr_find_address(FILE * F, const faction * uf, selist * addresses) if (uf != f) { fprintf(F, "PARTEI %d\n", f->no); fprintf(F, "\"%s\";Parteiname\n", f->name); - if (f->email) - fprintf(F, "\"%s\";email\n", f->email); + if (strcmp(faction_getemail(f), "") != 0) + fprintf(F, "\"%s\";email\n", faction_getemail(f)); if (f->banner) fprintf(F, "\"%s\";banner\n", f->banner); fprintf(F, "\"%s\";locale\n", locale_name(f->locale)); @@ -1604,7 +1604,7 @@ report_computer(const char *filename, report_context * ctx, const char *bom) } fprintf(F, "\"%s\";Parteiname\n", f->name); - fprintf(F, "\"%s\";email\n", f->email); + fprintf(F, "\"%s\";email\n", faction_getemail(f)); if (f->banner) fprintf(F, "\"%s\";banner\n", f->banner); print_items(F, f->items, f->locale); diff --git a/src/json.c b/src/json.c index e6eacced9..127d74f70 100644 --- a/src/json.c +++ b/src/json.c @@ -102,7 +102,7 @@ int json_export(stream * out, int flags) { cJSON *data; cJSON_AddItemToObject(json, itoa36(f->no), data = cJSON_CreateObject()); cJSON_AddStringToObject(data, "name", f->name); - cJSON_AddStringToObject(data, "email", f->email); + cJSON_AddStringToObject(data, "email", faction_getemail(f)); cJSON_AddNumberToObject(data, "score", (double)f->score); } } diff --git a/src/kernel/faction.c b/src/kernel/faction.c index 98e49bd84..00b62627f 100755 --- a/src/kernel/faction.c +++ b/src/kernel/faction.c @@ -229,8 +229,11 @@ faction *addfaction(const char *email, const char *password, faction *f = calloc(sizeof(faction), 1); char buf[128]; - if (set_email(&f->email, email) != 0) { - log_warning("Invalid email address for faction %s: %s\n", itoa36(f->no), email); + if (check_email(email) == 0) { + faction_setemail(f, email); + } else { + log_warning("Invalid email address for faction %s: %s\n", itoa36(f->no), email?email:""); + faction_setemail(f, NULL); } f->alliance_joindate = turn; @@ -544,6 +547,8 @@ void faction_setemail(faction * self, const char *email) free(self->email); if (email) self->email = strdup(email); + else + self->email = NULL; } const char *faction_getbanner(const faction * self) @@ -797,7 +802,7 @@ int writepasswd(void) for (f = factions; f; f = f->next) { fprintf(F, "%s:%s:%s:%d\n", - itoa36(f->no), f->email, f->_password, f->subscription); + itoa36(f->no), faction_getemail(f), f->_password, f->subscription); } fclose(F); return 0; diff --git a/src/kernel/faction.test.c b/src/kernel/faction.test.c index 7901cd96f..251f11bb9 100644 --- a/src/kernel/faction.test.c +++ b/src/kernel/faction.test.c @@ -225,17 +225,28 @@ static void test_valid_race(CuTest *tc) { } static void test_set_email(CuTest *tc) { - char * email = NULL; + faction *f; + char email[10]; test_setup(); - CuAssertIntEquals(tc, 0, set_email(&email, "enno@eressea.de")); - CuAssertStrEquals(tc, "enno@eressea.de", email); - CuAssertIntEquals(tc, 0, set_email(&email, "bugs@eressea.de")); - CuAssertStrEquals(tc, "bugs@eressea.de", email); - CuAssertIntEquals(tc, -1, set_email(&email, "bad@@eressea.de")); - CuAssertIntEquals(tc, -1, set_email(&email, "eressea.de")); - CuAssertIntEquals(tc, -1, set_email(&email, "eressea@")); - CuAssertStrEquals(tc, "bugs@eressea.de", email); - free(email); + CuAssertIntEquals(tc, 0, check_email("enno@eressea.de")); + CuAssertIntEquals(tc, 0, check_email("bugs@eressea.de")); + CuAssertIntEquals(tc, -1, check_email("bad@@eressea.de")); + CuAssertIntEquals(tc, -1, check_email("eressea.de")); + CuAssertIntEquals(tc, -1, check_email("eressea@")); + CuAssertIntEquals(tc, -1, check_email("")); + CuAssertIntEquals(tc, -1, check_email(NULL)); + f = test_create_faction(0); + + sprintf(email, "enno"); + faction_setemail(f, email); + email[0] = 0; + CuAssertStrEquals(tc, "enno", f->email); + CuAssertStrEquals(tc, "enno", faction_getemail(f)); + faction_setemail(f, "bugs@eressea.de"); + CuAssertStrEquals(tc, "bugs@eressea.de", f->email); + faction_setemail(f, NULL); + CuAssertPtrEquals(tc, 0, f->email); + CuAssertStrEquals(tc, "", faction_getemail(f)); test_cleanup(); } diff --git a/src/kernel/save.c b/src/kernel/save.c index aa09a27be..5617bc252 100644 --- a/src/kernel/save.c +++ b/src/kernel/save.c @@ -1067,9 +1067,11 @@ faction *read_faction(gamedata * data) log_debug(" - Lese Partei %s (%s)", f->name, itoa36(f->no)); READ_STR(data->store, name, sizeof(name)); - if (set_email(&f->email, name) != 0) { - log_warning("Invalid email address for faction %s: %s", itoa36(f->no), name); - set_email(&f->email, ""); + if (check_email(name) == 0) { + faction_setemail(f, name); + } else { + log_warning("Invalid email address for faction %s: %s", itoa36(f->no), name); + faction_setemail(f, NULL); } read_password(data, f); @@ -1168,7 +1170,7 @@ void write_faction(gamedata *data, const faction * f) WRITE_STR(data->store, f->name); WRITE_STR(data->store, f->banner); - WRITE_STR(data->store, f->email); + WRITE_STR(data->store, f->email?f->email:""); write_password(data, f); WRITE_TOK(data->store, locale_name(f->locale)); WRITE_INT(data->store, f->lastorders); diff --git a/src/laws.c b/src/laws.c index 1f3633d14..215dbf0ec 100644 --- a/src/laws.c +++ b/src/laws.c @@ -2126,13 +2126,13 @@ int email_cmd(unit * u, struct order *ord) } else { faction *f = u->faction; - if (set_email(&f->email, s) != 0) { + if (check_email(s) == 0) { + faction_setemail(f, s); + ADDMSG(&f->msgs, msg_message("changemail", "value", faction_getemail(f))); + } else { log_error("Invalid email address for faction %s: %s\n", itoa36(f->no), s); ADDMSG(&f->msgs, msg_message("changemail_invalid", "value", s)); } - else { - ADDMSG(&f->msgs, msg_message("changemail", "value", f->email)); - } } return 0; } diff --git a/src/modules/autoseed.c b/src/modules/autoseed.c index 52a5344cc..fd9ef2f19 100644 --- a/src/modules/autoseed.c +++ b/src/modules/autoseed.c @@ -171,7 +171,7 @@ newfaction *read_newfactions(const char *filename) sz += strlcat(password, itoa36(rng_int()), sizeof(password)); } for (f = factions; f; f = f->next) { - if (strcmp(f->email, email) == 0 && f->age < MINAGE_MULTI) { + if (strcmp(faction_getemail(f), email) == 0 && f->age < MINAGE_MULTI) { log_warning("email %s already in use by %s", email, factionname(f)); break; } @@ -188,7 +188,9 @@ newfaction *read_newfactions(const char *filename) continue; } nf = calloc(sizeof(newfaction), 1); - if (set_email(&nf->email, email) != 0) { + if (check_email(email) == 0) { + nf->email = strdup(email); + } else { log_error("Invalid email address for subscription %s: %s\n", itoa36(subscription), email); free(nf); continue; @@ -619,6 +621,7 @@ int autoseed(newfaction ** players, int nsize, int max_agediff) while (*nfp) { newfaction *nf = *nfp; if (strcmp(nextf->email, nf->email) == 0) { + log_warning("Duplicate email %s\n", nf->email?nf->email:""); *nfp = nf->next; free_newfaction(nf); } diff --git a/src/monsters.c b/src/monsters.c index 115759d29..6c0304f55 100644 --- a/src/monsters.c +++ b/src/monsters.c @@ -1132,7 +1132,7 @@ faction *get_or_create_monsters(void) if (!f) { const race *rc = rc_get_or_create("dragon"); const char *email = config_get("monster.email"); - f = addfaction(email ? email : NULL, NULL, rc, default_locale, 0); + f = addfaction(email, NULL, rc, default_locale, 0); renumber_faction(f, MONSTER_ID); faction_setname(f, "Monster"); fset(f, FFL_NPC | FFL_NOIDLEOUT); diff --git a/src/report.c b/src/report.c index 4e5eef98b..f99d6db12 100644 --- a/src/report.c +++ b/src/report.c @@ -1753,7 +1753,7 @@ static void list_address(struct stream *out, const faction * uf, selist * seenfa char buf[8192]; char label = '-'; - sprintf(buf, "%s: %s; %s", factionname(f), f->email, + sprintf(buf, "%s: %s; %s", factionname(f), faction_getemail(f), f->banner ? f->banner : ""); if (uf == f) label = '*'; @@ -2078,7 +2078,7 @@ report_plaintext(const char *filename, report_context * ctx, newline(out); sprintf(buf, "%s, %s/%s (%s)", factionname(f), LOC(f->locale, rc_name_s(f->race, NAME_PLURAL)), - LOC(f->locale, mkname("school", magic_school[f->magiegebiet])), f->email); + LOC(f->locale, mkname("school", magic_school[f->magiegebiet])), faction_getemail(f)); centre(out, buf, true); if (f_get_alliance(f)) { centre(out, alliancename(f->alliance), true); @@ -2088,12 +2088,17 @@ report_plaintext(const char *filename, report_context * ctx, const char *email; const char *subject; email = config_get("game.email"); - subject = get_mailcmd(f->locale); - m = msg_message("newbie_info_game", "email subject", email, subject); - if (m) { - nr_render(m, f->locale, buf, sizeof(buf), f); - msg_release(m); - centre(out, buf, true); + if (!email) + log_error("game.email not set"); + else { + subject = get_mailcmd(f->locale); + + m = msg_message("newbie_info_game", "email subject", email, subject); + if (m) { + nr_render(m, f->locale, buf, sizeof(buf), f); + msg_release(m); + centre(out, buf, true); + } } if ((f->options & want(O_COMPUTER)) == 0) { const char *s; diff --git a/src/reports.c b/src/reports.c index 1591de637..b474f32ed 100644 --- a/src/reports.c +++ b/src/reports.c @@ -1560,8 +1560,12 @@ static void write_script(FILE * F, const faction * f) { report_type *rtype; char buf[1024]; - - fprintf(F, "faction=%s:email=%s:lang=%s", itoa36(f->no), f->email, + + if (check_email(faction_getemail(f)) != 0) { + return; + } + + fprintf(F, "faction=%s:email=%s:lang=%s", itoa36(f->no), faction_getemail(f), locale_name(f->locale)); if (f->options & (1 << O_BZIP2)) fputs(":compression=bz2", F); diff --git a/src/sqlite.c b/src/sqlite.c index edb5bfc62..970032f35 100644 --- a/src/sqlite.c +++ b/src/sqlite.c @@ -144,7 +144,8 @@ static bool db_faction_cb(void *el, void *arg) { struct cb_data *cb = (struct cb_data *)arg; const faction *f = cb->f; - if (f->no == df->no || strcmp(f->email, df->email) == 0 || strcmp(f->name, df->name) == 0) { + /* FIXME comparing name and email is inaccurate ... */ + if (f->no == df->no || strcmp(faction_getemail(f), df->email?df->email:"") == 0 || strcmp(f->name, df->name) == 0) { cb->dbf = df; } if (f->subscription == df->uid) { @@ -172,7 +173,7 @@ int db_update_factions(sqlite3 * db, bool force, int game_id) { log_warning("faction %s(%d) not found in database, but matches %d\n", itoa36(f->no), f->subscription, dbf->uid); f->subscription = dbf->uid; } - update = (dbf->no != f->no) || (strcmp(f->email, dbf->email) != 0) || (strcmp(f->name, dbf->name) != 0); + update = (dbf->no != f->no) || (strcmp(faction_getemail(f), dbf->email?dbf->email:"") != 0) || (strcmp(f->name, dbf->name) != 0); } else { f->subscription = insert_faction(db, game_id, f); diff --git a/src/util/goodies.c b/src/util/goodies.c index 0794240d5..fcfaa1013 100644 --- a/src/util/goodies.c +++ b/src/util/goodies.c @@ -109,17 +109,13 @@ static int spc_email_isvalid(const char *address) return (count >= 1); } -int set_email(char **pemail, const char *newmail) +int check_email(const char *newmail) { if (newmail && *newmail) { if (spc_email_isvalid(newmail) <= 0) return -1; - } - if (*pemail) - free(*pemail); - *pemail = 0; - if (newmail) { - *pemail = strdup(newmail); + } else { + return -1; } return 0; } diff --git a/src/util/goodies.h b/src/util/goodies.h index c619c4958..bb3a372f6 100644 --- a/src/util/goodies.h +++ b/src/util/goodies.h @@ -23,7 +23,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. extern "C" { #endif - int set_email(char **pemail, const char *newmail); + int check_email(const char *newmail); int *intlist_init(void); int *intlist_add(int *i_p, int i);