Debian Codegraph @

src/bnetd/news.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2000 Alexey Belyaev ([email protected])
3 * Copyright (C) 2004 Dizzy
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19 #include "common/setup_before.h"
20 #define NEWS_INTERNAL_ACCESS
21 #include "news.h"
22
23 #include <cstring>
24 #include <cstdlib>
25 #include <cstdio>
26 #include <cassert>
27
28 #include "common/eventlog.h"
29 #include "common/xalloc.h"
30 #include "common/tag.h"
31 #include "i18n.h"
32
33 #include "common/setup_after.h"
34
35 namespace pvpgn
36 {
37
38 namespace bnetd
39 {
40
41 static t_elist news_head;
42
43 static int _news_parsetime(char *buff, struct std::tm *date, unsigned line)
44 {
45 char *p;
46
47 date->tm_hour = 6;
48 date->tm_min = 6; // need to set non-zero values or else date is displayed wrong
49 date->tm_sec = 6;
50 date->tm_isdst = -1;
51
52 if (!(p = std::strchr(buff, '/'))) return -1;
53 *p = '\0';
54
55 date->tm_mon = std::atoi(buff) - 1;
56 if ((date->tm_mon<0) || (date->tm_mon>11)) {
57 eventlog(eventlog_level_error, __FUNCTION__, "found invalid month ({}) in news date. (format: {MM/DD/YYYY}) on line {}", date->tm_mon, line);
58 }
59
60 buff = p + 1;
61 if (!(p = std::strchr(buff, '/'))) return -1;
62 *p = '\0';
63
64 date->tm_mday = std::atoi(buff);
65 if ((date->tm_mday<1) || (date->tm_mday>31)) {
66 eventlog(eventlog_level_error, __FUNCTION__, "found invalid month day ({}) in news date. (format: {MM/DD/YYYY}) on line {}", date->tm_mday, line);
67 return -1;
68 }
69
70 buff = p + 1;
71 if (!(p = std::strchr(buff, '}'))) return -1;
72 *p = '\0';
73
74 date->tm_year = std::atoi(buff) - 1900;
75 if (date->tm_year > 137) //limited due to 32bit t_time
76 {
77 eventlog(eventlog_level_error, __FUNCTION__, "found invalid year ({}) (>2037) in news date. on line {}", date->tm_year + 1900, line);
78 return -1;
79 }
80
81 return 0;
82 }
83
84 static void _news_insert_index(t_news_index *ni, const char *buff, unsigned len, int date_set)
85 {
86 t_elist *curr;
87 t_news_index *cni;
88
89 elist_for_each(curr, &news_head) {
90 cni = elist_entry(curr, t_news_index, list);
91 if (cni->date <= ni->date) break;
92 }
93
94 if (curr != &news_head && cni->date == ni->date) {
95 if (date_set == 1)
96 eventlog(eventlog_level_warn, __FUNCTION__, "found another news item for same date, trying to join both");
97
98 if ((lstr_get_len(&cni->body) + len + 2) > 1023)
99 eventlog(eventlog_level_error, __FUNCTION__, "failed in joining news, cause news too long - skipping");
100 else {
101 lstr_set_str(&cni->body, (char*)xrealloc(lstr_get_str(&cni->body), lstr_get_len(&cni->body) + len + 1 + 1));
102 std::strcpy(lstr_get_str(&cni->body) + lstr_get_len(&cni->body), buff);
103 *(lstr_get_str(&cni->body) + lstr_get_len(&cni->body) + len) = '\n';
104 *(lstr_get_str(&cni->body) + lstr_get_len(&cni->body) + len + 1) = '\0';
105 lstr_set_len(&cni->body, lstr_get_len(&cni->body) + len + 1);
106 }
107 xfree((void *)ni);
108 }
109 else {
110 /* adding new index entry */
111 lstr_set_str(&ni->body, (char*)xmalloc(len + 2));
112 std::strcpy(lstr_get_str(&ni->body), buff);
113 std::strcat(lstr_get_str(&ni->body), "\n");
114 lstr_set_len(&ni->body, len + 1);
115 elist_add_tail(curr, &ni->list);
116 }
117 }
118
119 static void _news_insert_default(void)
120 {
121 const char * deftext = "No news today";
122 t_news_index *ni;
123
124 ni = (t_news_index*)xmalloc(sizeof(t_news_index));
125 ni->date = std::time(NULL);
126 _news_insert_index(ni, deftext, std::strlen(deftext), 1);
127 }
128
129 extern int news_load(const char *filename)
130 {
131 std::FILE * fp;
132 unsigned int line;
133 unsigned int len;
134 char buff[256];
135 struct std::tm date;
136 char date_set;
137 t_news_index *ni;
138
139 elist_init(&news_head);
140
141 date_set = 0;
142
143 if (!filename) {
144 eventlog(eventlog_level_error, __FUNCTION__, "got NULL fullname");
145 return -1;
146 }
147
148 // FIXME: (HarpyWar) change news loading when user log on to send a localized version
149 if ((fp = std::fopen(i18n_filename(filename, GAMELANG_ENGLISH_UINT).c_str(), "rt")) == NULL)
150 {
151 eventlog(eventlog_level_warn, __FUNCTION__, "can't open news file");
152 _news_insert_default();
153 return 0;
154 }
155
156 for (line = 1; std::fgets(buff, sizeof(buff), fp); line++)
157 {
158 len = std::strlen(buff);
159 while (len && (buff[len - 1] == '\n' || buff[len - 1] == '\r')) len--;
160 if (!len) continue; /* empty line */
161 buff[len] = '\0';
162
163 if (buff[0] == '{')
164 {
165 if (_news_parsetime(buff + 1, &date, line))
166 {
167 eventlog(eventlog_level_error, __FUNCTION__, "error parsing news date on line {}", line);
168 std::fclose(fp);
169 return -1;
170 }
171 date_set = 1;
172 }
173 else
174 {
175 ni = (t_news_index*)xmalloc(sizeof(t_news_index));
176 if (date_set)
177 {
178 ni->date = std::mktime(&date);
179 }
180 else
181 {
182 ni->date = std::time(NULL);
183 eventlog(eventlog_level_warn, __FUNCTION__, "(first) news entry seems to be missing a timestamp, please check your news file on line {}", line);
184 }
185 _news_insert_index(ni, buff, len, date_set);
186 date_set = 2;
187 }
188 }
189 std::fclose(fp);
190
191 if (elist_empty(&news_head))
192 {
193 eventlog(eventlog_level_warn, __FUNCTION__, "no news configured");
194 _news_insert_default();
195 }
196
197 return 0;
198 }
199
200 /* Free up all of the elements in the linked list */
201 extern int news_unload(void)
202 {
203 t_elist * curr, *save;
204 t_news_index * ni;
205
206 elist_for_each_safe(curr, &news_head, save)
207 {
208 ni = elist_entry(curr, t_news_index, list);
209 elist_del(&ni->list);
210 xfree((void *)lstr_get_str(&ni->body));
211 xfree((void *)ni);
212 }
213
214 elist_init(&news_head);
215
216 return 0;
217 }
218
219 extern unsigned int news_get_lastnews(void)
220 {
221 if (elist_empty(&news_head)) return 0;
222 return ((elist_entry(news_head.next, t_news_index, list))->date);
223 }
224
225 extern unsigned int news_get_firstnews(void)
226 {
227 if (elist_empty(&news_head)) return 0;
228 return ((elist_entry(news_head.prev, t_news_index, list))->date);
229 }
230
231 extern void news_traverse(t_news_cb cb, void *data)
232 {
233 t_elist *curr;
234 t_news_index *cni;
235
236 assert(cb);
237
238 elist_for_each(curr, &news_head)
239 {
240 cni = elist_entry(curr, t_news_index, list);
241 if (cb(cni->date, &cni->body, data)) break;
242 }
243 }
244
245 }
246
247 }