-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathstatus.c
executable file
·262 lines (215 loc) · 6.35 KB
/
status.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
* This file is part of PRO ONLINE.
* PRO ONLINE is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* PRO ONLINE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PRO ONLINE. If not, see <http://www.gnu.org/licenses/ .
*/
#include <stdio.h>
#include <string.h>
#include <user.h>
#include <status.h>
#include <config.h>
#include <sqlite3.h>
// Function Prototypes
const char * strcpyxml(char * out, const char * in, uint32_t size);
/**
* Update Status Logfile
*/
void update_status(void)
{
// Open Logfile
FILE * log = fopen(SERVER_STATUS_XMLOUT, "w");
// Opened Logfile
if(log != NULL)
{
// Write XML Header
fprintf(log, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
// Write XSL Processor Information
fprintf(log, "<?xml-stylesheet type=\"text/xsl\" href=\"status.xsl\"?>\n");
// Output Root Tag + User Count
fprintf(log, "<prometheus usercount=\"%u\">\n", _db_user_count);
// Database Handle
sqlite3 * db = NULL;
// Open Database
if(sqlite3_open(SERVER_DATABASE, &db) == SQLITE_OK)
{
// Iterate Games
SceNetAdhocctlGameNode * game = _db_game; for(; game != NULL; game = game->next)
{
// Safe Product ID
char productid[PRODUCT_CODE_LENGTH + 1];
strncpy(productid, game->game.data, PRODUCT_CODE_LENGTH);
productid[PRODUCT_CODE_LENGTH] = 0;
// Display Name
char displayname[128];
memset(displayname, 0, sizeof(displayname));
// SQL Statement
const char * sql = "SELECT name FROM productids WHERE id=?;";
// Prepared SQL Statement
sqlite3_stmt * statement = NULL;
// Prepare SQL Statement
if(sqlite3_prepare_v2(db, sql, strlen(sql) + 1, &statement, NULL) == SQLITE_OK)
{
// Bind SQL Statement Data
if(sqlite3_bind_text(statement, 1, productid, strlen(productid), SQLITE_STATIC) == SQLITE_OK)
{
// Found Matching Row
if(sqlite3_step(statement) == SQLITE_ROW)
{
// Fetch Game Name from Database
const char * gamename = (const char *)sqlite3_column_text(statement, 0);
// Copy Game Name
strcpyxml(displayname, gamename, sizeof(displayname));
}
// Game not in Database
else
{
// Use Product Code as Name
strcpyxml(displayname, productid, sizeof(displayname));
}
}
// Destroy Prepared SQL Statement
sqlite3_finalize(statement);
}
// Output Game Tag + Game Name
fprintf(log, "\t<game name=\"%s\" usercount=\"%u\">\n", displayname, game->playercount);
// Activate User Count
uint32_t activecount = 0;
// Iterate Game Groups
SceNetAdhocctlGroupNode * group = game->group; for(; group != NULL; group = group->next)
{
// Safe Group Name
char groupname[ADHOCCTL_GROUPNAME_LEN + 1];
strncpy(groupname, (const char *)group->group.data, ADHOCCTL_GROUPNAME_LEN);
groupname[ADHOCCTL_GROUPNAME_LEN] = 0;
// Output Group Tag + Group Name + User Count
fprintf(log, "\t\t<group name=\"%s\" usercount=\"%u\">\n", strcpyxml(displayname, groupname, sizeof(displayname)), group->playercount);
// Iterate Users
SceNetAdhocctlUserNode * user = group->player; for(; user != NULL; user = user->group_next)
{
// Output User Tag + Username
fprintf(log, "\t\t\t<user>%s</user>\n", strcpyxml(displayname, (const char *)user->resolver.name.data, sizeof(displayname)));
}
// Output Closing Group Tag
fprintf(log, "\t\t</group>\n");
// Increase Active Game User Count
activecount += group->playercount;
}
// Output Idle Game Group
if(game->playercount > activecount)
{
// Output Group Tag + Group Name + Idle User Count
fprintf(log, "\t\t<group name=\"Groupless\" usercount=\"%u\" />\n", game->playercount - activecount);
}
// Output Closing Game Tag
fprintf(log, "\t</game>\n");
}
// Close Database
sqlite3_close(db);
}
// Output Closing Root Tag
fprintf(log, "</prometheus>");
// Close Logfile
fclose(log);
}
}
/**
* Escape XML Sequences to avoid malformed XML files.
* @param out Out Buffer
* @param in In Buffer
* @param size Size of Out Buffer
* @return Reference to Out Buffer
*/
const char * strcpyxml(char * out, const char * in, uint32_t size)
{
// Valid Arguments
if(out != NULL && in != NULL && size > 0)
{
// Clear Memory
memset(out, 0, size);
// Written Size Pointer
uint32_t written = 0;
// Iterate In-Buffer Symbols
uint32_t i = 0; for(; i < strlen(in); i++)
{
// " Symbol
if(in[i] == '"')
{
// Enough Space in Out-Buffer (6B for ")
if((size - written) > 6)
{
// Write Escaped Sequence
strcpy(out + written, """);
// Move Pointer
written += 6;
}
// Truncate required
else break;
}
// < Symbol
else if(in[i] == '<')
{
// Enough Space in Out-Buffer (4B for <)
if((size - written) > 4)
{
// Write Escaped Sequence
strcpy(out + written, "<");
// Move Pointer
written += 4;
}
// Truncate required
else break;
}
// > Symbol
else if(in[i] == '>')
{
// Enough Space in Out-Buffer (4B for >)
if((size - written) > 4)
{
// Write Escaped Sequence
strcpy(out + written, ">");
// Move Pointer
written += 4;
}
// Truncate required
else break;
}
// & Symbol
else if(in[i] == '&')
{
// Enough Space in Out-Buffer (5B for &)
if((size - written) > 5)
{
// Write Escaped Sequence
strcpy(out + written, "&");
// Move Pointer
written += 5;
}
// Truncate required
else break;
}
// Normal Character
else
{
// Enough Space in Out-Buffer (1B)
if((size - written) > 1)
{
// Write Character
out[written++] = in[i];
}
}
}
// Return Reference
return out;
}
// Invalid Arguments
return NULL;
}