2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future projects/authors will amend these files as needed.
28 * anno.c: Delete announcements older than ANNO_KEEP_DAYS
30 * Known contributors to this file:
34 * Ron Koenderink, 2004
51 static int copy_and_expire(FILE *annfp, FILE *tmpfp,
52 char *tmp_filename, time_t expiry_time);
55 delete_old_announcements(void)
61 char tmp_filename[1024];
64 if (anno_keep_days < 0)
68 old = now - days(anno_keep_days);
69 logerror("Deleting annos older than %s", ctime(&old));
71 if ((annfp = fopen(annfil, "rb")) == NULL) {
72 logerror("can't open telegram file %s for reading", annfil);
75 sprintf(tmp_filename, "%s.tmp", annfil);
76 if ((tmpfp = fopen(tmp_filename, "wb")) == NULL) {
77 logerror("can't open telegram file %s for writing",
79 if (fclose(annfp) != 0)
80 logerror("can't close telegram file %s", annfil);
83 copy_file = copy_and_expire(annfp, tmpfp, tmp_filename, old);
85 if (fclose(annfp) != 0) {
86 logerror("can't close telegram file %s", annfil);
89 if (fclose(tmpfp) != 0) {
90 logerror("can't close temporary telegram file %s",
96 if (unlink(annfil) != 0) {
97 logerror("can't delete telegram file %s", annfil);
103 if (rename(tmp_filename, annfil) != 0)
104 logerror("can't move temporary telegram file %s "
105 "to telegram file %s", tmp_filename, annfil);
110 copy_and_expire(FILE *annfp, FILE *tmpfp, char *tmp_filename,
115 char message[MAXTELSIZE]; /* UTF-8 */
120 while (fread(&tgm, sizeof(tgm), 1, annfp) == 1) {
122 if (tgm.tel_length < 0 || tgm.tel_length > MAXTELSIZE) {
123 logerror("bad telegram file header (length=%ld)",
127 if (tgm.tel_type < 0 || tgm.tel_type > TEL_LAST) {
128 logerror("bad telegram file header (type=%d)",
135 if (tgm.tel_date >= expiry_time)
138 if (tgm.tel_date < expiry_time)
142 if (fwrite(&tgm, sizeof(tgm), 1, tmpfp) != 1) {
143 logerror("error writing header to temporary "
144 "telegram file %s", tmp_filename);
150 if (fread(message, sizeof(char), tgm.tel_length, annfp) !=
151 (size_t)tgm.tel_length) {
152 logerror("error reading body from telegram file %s",
157 if (fwrite(message, sizeof(char), tgm.tel_length, tmpfp) !=
158 (size_t)tgm.tel_length) {
159 logerror("error writing body to temporary telegram "
160 "file %s", tmp_filename);
165 logerror("%d announcements deleted; %d announcements saved",