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