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