2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2008, 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
39 #if defined(_WIN32) && defined(__GNUC__)
47 static int copy_and_expire(FILE *annfp, FILE *tmpfp,
48 char *tmp_filename, time_t expiry_time);
51 delete_old_announcements(void)
57 char tmp_filename[1024];
60 if (anno_keep_days < 0)
64 old = now - days(anno_keep_days);
65 logerror("Deleting annos older than %s", ctime(&old));
67 if ((annfp = fopen(annfil, "rb")) == NULL) {
68 logerror("can't open telegram file %s for reading", annfil);
71 sprintf(tmp_filename, "%s.tmp", annfil);
72 if ((tmpfp = fopen(tmp_filename, "wb")) == NULL) {
73 logerror("can't open telegram file %s for writing",
75 if (fclose(annfp) != 0)
76 logerror("can't close telegram file %s", annfil);
79 copy_file = copy_and_expire(annfp, tmpfp, tmp_filename, old);
81 if (fclose(annfp) != 0) {
82 logerror("can't close telegram file %s", annfil);
85 if (fclose(tmpfp) != 0) {
86 logerror("can't close temporary telegram file %s",
92 if (unlink(annfil) != 0) {
93 logerror("can't delete telegram file %s", annfil);
99 if (rename(tmp_filename, annfil) != 0)
100 logerror("can't move temporary telegram file %s "
101 "to telegram file %s", tmp_filename, annfil);
103 if (remove(tmp_filename) < 0)
104 logerror("can't delete telegram file %s", tmp_filename);
109 copy_and_expire(FILE *annfp, FILE *tmpfp, char *tmp_filename,
114 char message[MAXTELSIZE]; /* UTF-8 */
119 while (fread(&tgm, sizeof(tgm), 1, annfp) == 1) {
121 if (tgm.tel_length < 0 || tgm.tel_length > MAXTELSIZE) {
122 logerror("bad telegram file header (length=%ld)",
126 if (tgm.tel_type < 0 || tgm.tel_type > TEL_LAST) {
127 logerror("bad telegram file header (type=%d)",
134 if (tgm.tel_date >= expiry_time)
137 if (tgm.tel_date < expiry_time)
141 if (fwrite(&tgm, sizeof(tgm), 1, tmpfp) != 1) {
142 logerror("error writing header to temporary "
143 "telegram file %s", tmp_filename);
149 if (fread(message, 1, tgm.tel_length, annfp) !=
150 (size_t)tgm.tel_length) {
151 logerror("error reading body from telegram file %s",
156 if (fwrite(message, 1, tgm.tel_length, tmpfp) !=
157 (size_t)tgm.tel_length) {
158 logerror("error writing body to temporary telegram "
159 "file %s", tmp_filename);
164 logerror("%d announcements deleted; %d announcements saved",