]> git.pond.sub.org Git - empserver/blob - src/lib/update/anno.c
Import of Empire 4.2.12
[empserver] / src / lib / update / anno.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, 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 "common.h"
46
47 void
48 delete_old_announcements(void)
49 {
50         time_t  now;
51         time_t  old;
52         struct  telstr tgm;
53         FILE   *oldfp;
54         int     tmpfd;
55         s_char  tmp_filename[1024];
56         int     writeit;
57         s_char  message[MAXTELSIZE];
58         int     deleted = 0;
59         int     saved = 0;
60         int     length;
61         int     nbytes;
62         int     first = 1;
63
64         time(&now);
65         old = now - days(ANNO_KEEP_DAYS);
66         logerror("Deleting annos older than %s", ctime(&old));
67
68 #if !defined(_WIN32)
69         if ((oldfp = fopen(annfil, "r+")) == 0) {
70 #else
71         if ((oldfp = fopen(annfil, "r+b")) == 0) {
72 #endif
73                 logerror("can't read telegram file %s", annfil);
74                 return;
75         }
76         sprintf(tmp_filename, "%s.tmp", annfil);
77 #if !defined(_WIN32)
78         if ((tmpfd = open(tmp_filename, O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0) {
79 #else
80         if ((tmpfd = open(tmp_filename, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0666)) < 0) {
81 #endif
82                 logerror("can't write telegram file %s", tmp_filename);
83                 return;
84         }
85         while (fread((s_char *) &tgm, sizeof(tgm), 1, oldfp) == 1) {
86                 writeit = 1;
87                 if (tgm.tel_length < 0) {
88                         logerror("bad telegram file header (length)");
89                         return;
90                 }
91                 if (tgm.tel_type < 0 || tgm.tel_type > TEL_LAST) {
92                         logerror("bad telegram file header (type)");
93                         writeit = 0;
94                 }
95                 if (first) {
96                         first = 0;
97                         if (tgm.tel_date >= old) {
98                                 fclose(oldfp);
99                                 return;
100                         }
101                 }
102         
103                 if (tgm.tel_date < old) {
104                         writeit = 0;
105                 }
106
107                 if (writeit) {
108                         if (write(tmpfd, &tgm, sizeof(tgm)) < (int)sizeof(tgm)) {
109                                 logerror("error writing to ann.tmp");
110                                 return;
111                         }
112                         ++saved;
113                 } else {
114                         ++deleted;
115                 }
116                 length = tgm.tel_length;
117                 while (length > 0) {
118                         nbytes = length;
119                         if (nbytes > (int)sizeof(message))
120                                 nbytes = sizeof(message);
121                         (void) fread(message, sizeof(s_char), nbytes, oldfp);
122                         if (writeit) {
123                                 if (write(tmpfd, message, nbytes) < nbytes) {
124                                         logerror("Error writing to ann.tmp");
125                                         return;
126                                 }                                       
127                         }
128                         length -= nbytes;
129                 }
130         }
131         logerror("%d announcements deleted; %d announcements saved",
132                  deleted, saved);
133         fclose(oldfp);
134         close(tmpfd);
135         unlink(annfil);
136         rename(tmp_filename, annfil);
137 }