]> git.pond.sub.org Git - empserver/blob - src/lib/update/anno.c
Update copyright notice.
[empserver] / src / lib / update / anno.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2007, 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 #if defined(_WIN32) && defined(__GNUC__)
40 #include <io.h>
41 #endif
42 #include <stdio.h>
43 #include <time.h>
44 #include "tel.h"
45 #include "update.h"
46
47 static int copy_and_expire(FILE *annfp, FILE *tmpfp,
48                            char *tmp_filename, time_t expiry_time);
49
50 void
51 delete_old_announcements(void)
52 {
53     time_t now;
54     time_t old;
55     FILE *annfp;
56     FILE *tmpfp;
57     char tmp_filename[1024];
58     int copy_file;
59
60     if (anno_keep_days < 0)
61         return;
62
63     time(&now);
64     old = now - days(anno_keep_days);
65     logerror("Deleting annos older than %s", ctime(&old));
66
67     if ((annfp = fopen(annfil, "rb")) == NULL) {
68         logerror("can't open telegram file %s for reading", annfil);
69         return;
70     }
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",
74                  tmp_filename);
75         if (fclose(annfp) != 0)
76             logerror("can't close telegram file %s", annfil);
77         return;
78     }
79     copy_file = copy_and_expire(annfp, tmpfp, tmp_filename, old);
80
81     if (fclose(annfp) != 0) {
82         logerror("can't close telegram file %s", annfil);
83         copy_file = 0;
84     }
85     if (fclose(tmpfp) != 0) {
86         logerror("can't close temporary telegram file %s",
87                  tmp_filename);
88         copy_file = 0;
89     }
90 #if defined(_WIN32)
91     if (copy_file) {
92         if (unlink(annfil) != 0) {
93             logerror("can't delete telegram file %s", annfil);
94             copy_file = 0;
95         }
96     }
97 #endif
98     if (copy_file) {
99         if (rename(tmp_filename, annfil) != 0)
100             logerror("can't move temporary telegram file %s "
101                      "to telegram file %s", tmp_filename, annfil);
102     }
103 }
104
105 static int
106 copy_and_expire(FILE *annfp, FILE *tmpfp, char *tmp_filename,
107                 time_t expiry_time)
108 {
109     struct telstr tgm;
110     int writeit;
111     char message[MAXTELSIZE];   /* UTF-8 */
112     int deleted = 0;
113     int saved = 0;
114     int first = 1;
115
116     while (fread(&tgm, sizeof(tgm), 1, annfp) == 1) {
117         writeit = 1;
118         if (tgm.tel_length < 0 || tgm.tel_length > MAXTELSIZE) {
119             logerror("bad telegram file header (length=%ld)",
120                      tgm.tel_length);
121             return 0;
122         }
123         if (tgm.tel_type < 0 || tgm.tel_type > TEL_LAST) {
124             logerror("bad telegram file header (type=%d)",
125                      tgm.tel_type);
126             return 0;
127         }
128
129         if (first) {
130             first = 0;
131             if (tgm.tel_date >= expiry_time)
132                 return 0;
133         }
134         if (tgm.tel_date < expiry_time)
135             writeit = 0;
136
137         if (writeit) {
138             if (fwrite(&tgm, sizeof(tgm), 1, tmpfp) != 1) {
139                 logerror("error writing header to temporary "
140                          "telegram file %s", tmp_filename);
141                 return 0;
142             }
143             ++saved;
144         } else
145             ++deleted;
146         if (fread(message, 1, tgm.tel_length, annfp) !=
147             (size_t)tgm.tel_length) {
148             logerror("error reading body from telegram file %s",
149                      annfil);
150             return 0;
151         }
152         if (writeit) {
153             if (fwrite(message, 1, tgm.tel_length, tmpfp) !=
154                 (size_t)tgm.tel_length) {
155                 logerror("error writing body to temporary telegram "
156                          "file %s", tmp_filename);
157                 return 0;
158             }
159         }
160     }
161     logerror("%d announcements deleted; %d announcements saved",
162              deleted, saved);
163     return 1;
164 }