]> git.pond.sub.org Git - empserver/blob - src/lib/update/anno.c
Break inclusion cycle: prototypes.h and commands.h included each
[empserver] / src / lib / update / anno.c
1 /*
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
5  *
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.
10  *
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.
15  *
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
19  *
20  *  ---
21  *
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.
25  *
26  *  ---
27  *
28  *  anno.c: Delete announcements older than ANNO_KEEP_DAYS
29  * 
30  *  Known contributors to this file:
31  *     Ken Stevens, 1995
32  *     Doug Hay, 1998
33  *     Steve McClure, 2000
34  *     Ron Koenderink, 2004
35  */
36
37 #include <config.h>
38
39 #include <stdio.h>
40 #include <time.h>
41 #include "tel.h"
42 #include "update.h"
43
44 static int copy_and_expire(FILE *annfp, FILE *tmpfp,
45                            char *tmp_filename, time_t expiry_time);
46
47 void
48 delete_old_announcements(void)
49 {
50     time_t now;
51     time_t old;
52     FILE *annfp;
53     FILE *tmpfp;
54     char tmp_filename[1024];
55     int copy_file;
56
57     if (anno_keep_days < 0)
58         return;
59
60     time(&now);
61     old = now - days(anno_keep_days);
62     logerror("Deleting annos older than %s", ctime(&old));
63
64     if ((annfp = fopen(annfil, "rb")) == NULL) {
65         logerror("can't open telegram file %s for reading", annfil);
66         return;
67     }
68     sprintf(tmp_filename, "%s.tmp", annfil);
69     if ((tmpfp = fopen(tmp_filename, "wb")) == NULL) {
70         logerror("can't open telegram file %s for writing",
71                  tmp_filename);
72         if (fclose(annfp) != 0)
73             logerror("can't close telegram file %s", annfil);
74         return;
75     }
76     copy_file = copy_and_expire(annfp, tmpfp, tmp_filename, old);
77
78     if (fclose(annfp) != 0) {
79         logerror("can't close telegram file %s", annfil);
80         copy_file = 0;
81     }
82     if (fclose(tmpfp) != 0) {
83         logerror("can't close temporary telegram file %s",
84                  tmp_filename);
85         copy_file = 0;
86     }
87 #if defined(_WIN32)
88     if (copy_file) {
89         if (unlink(annfil) != 0) {
90             logerror("can't delete telegram file %s", annfil);
91             copy_file = 0;
92         }
93     }
94 #endif
95     if (copy_file) {
96         if (rename(tmp_filename, annfil) != 0)
97             logerror("can't move temporary telegram file %s "
98                      "to telegram file %s", tmp_filename, annfil);
99     }
100 }
101
102 static int
103 copy_and_expire(FILE *annfp, FILE *tmpfp, char *tmp_filename,
104                 time_t expiry_time)
105 {
106     struct telstr tgm;
107     int writeit;
108     char message[MAXTELSIZE];   /* UTF-8 */
109     int deleted = 0;
110     int saved = 0;
111     int first = 1;
112
113     while (fread(&tgm, sizeof(tgm), 1, annfp) == 1) {
114         writeit = 1;
115         if (tgm.tel_length < 0 || tgm.tel_length > MAXTELSIZE) {
116             logerror("bad telegram file header (length=%ld)",
117                      tgm.tel_length);
118             return 0;
119         }
120         if (tgm.tel_type < 0 || tgm.tel_type > TEL_LAST) {
121             logerror("bad telegram file header (type=%d)",
122                      tgm.tel_type);
123             return 0;
124         }
125
126         if (first) {
127             first = 0;
128             if (tgm.tel_date >= expiry_time)
129                 return 0;
130         }
131         if (tgm.tel_date < expiry_time)
132             writeit = 0;
133
134         if (writeit) {
135             if (fwrite(&tgm, sizeof(tgm), 1, tmpfp) != 1) {
136                 logerror("error writing header to temporary "
137                          "telegram file %s", tmp_filename);
138                 return 0;
139             }
140             ++saved;
141         } else
142             ++deleted;
143         if (fread(message, 1, tgm.tel_length, annfp) !=
144             (size_t)tgm.tel_length) {
145             logerror("error reading body from telegram file %s",
146                      annfil);
147             return 0;
148         }
149         if (writeit) {
150             if (fwrite(message, 1, tgm.tel_length, tmpfp) !=
151                 (size_t)tgm.tel_length) {
152                 logerror("error writing body to temporary telegram "
153                          "file %s", tmp_filename);
154                 return 0;
155             }
156         }
157     }
158     logerror("%d announcements deleted; %d announcements saved",
159              deleted, saved);
160     return 1;
161 }