Debian Codegraph @

src/bnetd/output.cpp

raw symbols SCIP
1 /*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version 2
5 * of the License, or (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15 */
16
17 #include "common/setup_before.h"
18 #include "output.h"
19
20 #include <cstdio>
21 #include <cerrno>
22 #include <cstring>
23 #include <string>
24
25 #include "common/eventlog.h"
26 #include "common/xalloc.h"
27 #include "common/tag.h"
28 #include "common/list.h"
29 #include "common/util.h"
30
31 #include "prefs.h"
32 #include "game.h"
33 #include "channel.h"
34 #include "connection.h"
35 #include "account.h"
36 #include "server.h"
37 #include "common/setup_after.h"
38
39 namespace pvpgn
40 {
41
42 namespace bnetd
43 {
44
45 std::string status_filename;
46
47 int output_standard_writer(std::FILE * fp);
48
49 /*
50 * Initialisation Output *
51 */
52
53 extern void output_init()
54 {
55 eventlog(eventlog_level_info, __FUNCTION__, "initializing output file");
56
57 if (prefs_get_XML_status_output())
58 status_filename = fmt::format("{}/{}", prefs_get_outputdir(), "server.xml");
59 else
60 status_filename = fmt::format("{}/{}", prefs_get_outputdir(), "server.dat");
61
62 return;
63 }
64
65 /*
66 * Write Functions *
67 */
68
69 static int _glist_cb_xml(t_game *game, void *data)
70 {
71 char clienttag_str[5];
72
73 std::fprintf((std::FILE*)data, "\t\t<game><id>%u</id><name>%s</name><clienttag>%s</clienttag></game>\n", game_get_id(game), game_get_name(game), tag_uint_to_str(clienttag_str, game_get_clienttag(game)));
74
75 return 0;
76 }
77
78 static int _glist_cb_simple(t_game *game, void *data)
79 {
80 static int number;
81 char clienttag_str[5];
82
83 if (!data) {
84 number = 1;
85 return 0;
86 }
87
88 std::fprintf((std::FILE*)data, "game%d=%s,%u,%s\n", number, tag_uint_to_str(clienttag_str, game_get_clienttag(game)), game_get_id(game), game_get_name(game));
89 number++;
90
91 return 0;
92 }
93
94 int output_standard_writer(std::FILE * fp)
95 {
96 t_elem const *curr;
97 t_connection *conn;
98 t_channel const *channel;
99 t_game *game;
100
101 char const *channel_name;
102 int number;
103 char clienttag_str[5];
104 int uptime = server_get_uptime();
105
106
107 if (prefs_get_XML_status_output())
108 {
109 int seconds;
110 int minutes;
111 int hours;
112 int days;
113
114 days = (uptime / (60 * 60 * 24));
115 hours = (uptime / (60 * 60)) % 24;
116 minutes = (uptime / 60) % 60;
117 seconds = uptime % 60;
118
119 std::fprintf(fp, "<?xml version=\"1.0\"?>\n<status>\n");
120 std::fprintf(fp, "\t\t<Version>%s</Version>\n", PVPGN_VERSION);
121 std::fprintf(fp, "\t\t<Uptime>\n");
122 std::fprintf(fp, "\t\t\t<Days>%d</Days>\n", days);
123 std::fprintf(fp, "\t\t\t<Hours>%d</Hours>\n", hours);
124 std::fprintf(fp, "\t\t\t<Minutes>%d</Minutes>\n", minutes);
125 std::fprintf(fp, "\t\t\t<Seconds>%d</Seconds>\n", seconds);
126 std::fprintf(fp, "\t\t</Uptime>\n");
127 std::fprintf(fp, "\t\t<Users>\n");
128 std::fprintf(fp, "\t\t<Number>%d</Number>\n", connlist_login_get_length());
129
130 LIST_TRAVERSE_CONST(connlist(), curr)
131 {
132 conn = (t_connection*)elem_get_data(curr);
133 if (conn_get_account(conn))
134 {
135 std::fprintf(fp, "\t\t<user><name>%s</name><clienttag>%s</clienttag><version>%s</version>", conn_get_username(conn), tag_uint_to_str(clienttag_str, conn_get_clienttag(conn)), conn_get_clientver(conn));
136
137 const char * country = conn_get_country(conn);
138 if (!country) country = "-";
139 std::fprintf(fp, "<country>%s</country>", country);
140
141 if ((game = conn_get_game(conn)))
142 std::fprintf(fp, "<gameid>%u</gameid>", game_get_id(game));
143 std::fprintf(fp, "</user>\n");
144 }
145 }
146
147 std::fprintf(fp, "\t\t</Users>\n");
148 std::fprintf(fp, "\t\t<Games>\n");
149 std::fprintf(fp, "\t\t<Number>%d</Number>\n", gamelist_get_length());
150
151 gamelist_traverse(_glist_cb_xml, fp, gamelist_source_none);
152
153 std::fprintf(fp, "\t\t</Games>\n");
154 std::fprintf(fp, "\t\t<Channels>\n");
155 std::fprintf(fp, "\t\t<Number>%d</Number>\n", channellist_get_length());
156
157 LIST_TRAVERSE_CONST(channellist(), curr)
158 {
159 channel = (t_channel*)elem_get_data(curr);
160 channel_name = channel_get_name(channel);
161 std::fprintf(fp, "\t\t<channel>%s</channel>\n", channel_name);
162 }
163
164 std::fprintf(fp, "\t\t</Channels>\n");
165 std::fprintf(fp, "</status>\n");
166 return 0;
167 }
168 else
169 {
170 std::fprintf(fp, "[STATUS]\nVersion=%s\nUptime=%s\nGames=%d\nUsers=%d\nChannels=%d\nUserAccounts=%d\n", PVPGN_VERSION, seconds_to_timestr(uptime), gamelist_get_length(), connlist_login_get_length(), channellist_get_length(), accountlist_get_length()); // Status
171 std::fprintf(fp, "[CHANNELS]\n");
172 number = 1;
173 LIST_TRAVERSE_CONST(channellist(), curr)
174 {
175 channel = (t_channel*)elem_get_data(curr);
176 channel_name = channel_get_name(channel);
177 std::fprintf(fp, "channel%d=%s\n", number, channel_name);
178 number++;
179 }
180
181 std::fprintf(fp, "[GAMES]\n");
182 _glist_cb_simple(NULL, NULL); /* init number */
183 gamelist_traverse(_glist_cb_simple, fp, gamelist_source_none);
184
185 std::fprintf(fp, "[USERS]\n");
186 number = 1;
187 LIST_TRAVERSE_CONST(connlist(), curr)
188 {
189 conn = (t_connection*)elem_get_data(curr);
190 if (conn_get_account(conn))
191 {
192 std::fprintf(fp, "user%d=%s,%s,%s", number, tag_uint_to_str(clienttag_str, conn_get_clienttag(conn)), conn_get_username(conn), conn_get_clientver(conn));
193
194 const char * country = conn_get_country(conn);
195 if (!country) country = "-";
196 std::fprintf(fp, ",%s", country);
197
198 if ((game = conn_get_game(conn)))
199 std::fprintf(fp, ",%u", game_get_id(game));
200 std::fprintf(fp, "\n");
201 number++;
202 }
203 }
204
205 return 0;
206 }
207 }
208
209 extern int output_write_to_file()
210 {
211 if (status_filename.empty())
212 {
213 eventlog(eventlog_level_error, __FUNCTION__, "got empty filename");
214 return -1;
215 }
216
217 std::FILE* fp = std::fopen(status_filename.c_str(), "w");
218 if (!fp)
219 {
220 eventlog(eventlog_level_error, __FUNCTION__, "could not open file \"{}\" for writing (std::fopen: {})", status_filename, std::strerror(errno));
221 return -1;
222 }
223
224 output_standard_writer(fp);
225 std::fclose(fp);
226
227 return 0;
228 }
229
230 }
231
232 }