/[LeafOK_CVS]/pvpgn-1.7.4/src/bnetd/output.c
ViewVC logotype

Annotation of /pvpgn-1.7.4/src/bnetd/output.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (hide annotations)
Tue Jun 6 03:41:37 2006 UTC (19 years, 9 months ago) by sysadm
Branch point for: GNU, MAIN
Content type: text/x-csrc
Initial revision

1 sysadm 1.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 <stdio.h>
19     #ifdef HAVE_STDDEF_H
20     # include <stddef.h>
21     #else
22     # ifndef NULL
23     # define NULL ((void *)0)
24     # endif
25     #endif
26     #ifdef STDC_HEADERS
27     # include <stdlib.h>
28     #endif
29     #ifdef HAVE_STRING_H
30     # include <string.h>
31     #else
32     # ifdef HAVE_STRINGS_H
33     # include <strings.h>
34     # endif
35     #endif
36     #include <errno.h>
37     #include "output.h"
38     #include "prefs.h"
39     #include "connection.h"
40     #include "game.h"
41     #include "ladder.h"
42     #include "server.h"
43     #include "channel.h"
44     #include "account.h"
45     #include "common/util.h"
46     #include "common/bnettime.h"
47     #include "common/eventlog.h"
48     #include "common/list.h"
49     #include "common/proginfo.h"
50     #include "compat/strerror.h"
51     #include "common/xalloc.h"
52     #include "common/tag.h"
53    
54     char * status_filename;
55    
56     int output_standard_writer(FILE * fp);
57    
58     /*
59     * Initialisation Output *
60     */
61    
62     extern void output_init(void)
63     {
64     eventlog(eventlog_level_info,__FUNCTION__,"initializing output file");
65    
66     if (prefs_get_XML_status_output())
67     status_filename = create_filename(prefs_get_outputdir(),"server",".xml"); // WarCraft III
68     else
69     status_filename = create_filename(prefs_get_outputdir(),"server",".dat"); // WarCraft III
70    
71     return;
72     }
73    
74     /*
75     * Write Functions *
76     */
77    
78     static int _glist_cb_xml(t_game *game, void *data)
79     {
80     char clienttag_str[5];
81    
82     fprintf((FILE*)data,"\t\t<game><name>%s</name><clienttag>%s</clienttag></game>\n",game_get_name(game),tag_uint_to_str(clienttag_str,game_get_clienttag(game)));
83    
84     return 0;
85     }
86    
87     static int _glist_cb_simple(t_game *game, void *data)
88     {
89     static int number;
90     char clienttag_str[5];
91    
92     if (!data) {
93     number = 1;
94     return 0;
95     }
96    
97     fprintf((FILE*)data,"game%d=%s,%s\n",number,tag_uint_to_str(clienttag_str,game_get_clienttag(game)),game_get_name(game));
98     number++;
99    
100     return 0;
101     }
102    
103     int output_standard_writer(FILE * fp)
104     {
105     t_elem const *curr;
106     t_connection *conn;
107     t_channel const *channel;
108     char const *channel_name;
109     int number;
110     char clienttag_str[5];
111    
112     if (prefs_get_XML_status_output())
113     {
114     fprintf(fp,"<?xml version=\"1.0\"?>\n<status>\n");
115     fprintf(fp,"\t\t<Version>%s</Version>\n",PVPGN_VERSION);
116     fprintf(fp,"\t\t<Uptime>%s</Uptime>\n",seconds_to_timestr(server_get_uptime()));
117     fprintf(fp,"\t\t<Users>\n");
118     fprintf(fp,"\t\t<Number>%d</Number>\n",connlist_login_get_length());
119    
120     LIST_TRAVERSE_CONST(connlist(),curr)
121     {
122     conn = elem_get_data(curr);
123     if (conn_get_account(conn))
124     fprintf(fp,"\t\t<user><name>%s</name><clienttag>%s</clienttag><version>%s</version></user>\n",conn_get_username(conn),tag_uint_to_str(clienttag_str,conn_get_clienttag(conn)),conn_get_clientver(conn));
125     }
126    
127     fprintf(fp,"\t\t</Users>\n");
128     fprintf(fp,"\t\t<Games>\n");
129     fprintf(fp,"\t\t<Number>%d</Number>\n",gamelist_get_length());
130    
131     gamelist_traverse(_glist_cb_xml,fp);
132    
133     fprintf(fp,"\t\t</Games>\n");
134     fprintf(fp,"\t\t<Channels>\n");
135     fprintf(fp,"\t\t<Number>%d</Number>\n",channellist_get_length());
136    
137     LIST_TRAVERSE_CONST(channellist(),curr)
138     {
139     channel = elem_get_data(curr);
140     channel_name = channel_get_name(channel);
141     fprintf(fp,"\t\t<channel>%s</channel>\n",channel_name);
142     }
143    
144     fprintf(fp,"\t\t</Channels>\n");
145     fprintf(fp,"</status>\n");
146     return 0;
147     }
148     else
149     {
150     fprintf(fp,"[STATUS]\nVersion=%s\nUptime=%s\nGames=%d\nUsers=%d\nChannels=%d\nUserAccounts=%d\n",PVPGN_VERSION,seconds_to_timestr(server_get_uptime()),gamelist_get_length(),connlist_login_get_length(),channellist_get_length(),accountlist_get_length()); // Status
151     fprintf(fp,"[CHANNELS]\n");
152     number=1;
153     LIST_TRAVERSE_CONST(channellist(),curr)
154     {
155     channel = elem_get_data(curr);
156     channel_name = channel_get_name(channel);
157     fprintf(fp,"channel%d=%s\n",number,channel_name);
158     number++;
159     }
160    
161     fprintf(fp,"[GAMES]\n");
162     _glist_cb_simple(NULL,NULL); /* init number */
163     gamelist_traverse(_glist_cb_simple,fp);
164    
165     fprintf(fp,"[USERS]\n");
166     number=1;
167     LIST_TRAVERSE_CONST(connlist(),curr)
168     {
169     conn = elem_get_data(curr);
170     if (conn_get_account(conn))
171     {
172     fprintf(fp,"user%d=%s,%s\n",number,tag_uint_to_str(clienttag_str,conn_get_clienttag(conn)),conn_get_username(conn));
173     number++;
174     }
175     }
176    
177     return 0;
178     }
179     }
180    
181     extern int output_write_to_file(void)
182     {
183     FILE * fp;
184    
185     if (!status_filename)
186     {
187     eventlog(eventlog_level_error,__FUNCTION__,"got NULL filename");
188     return -1;
189     }
190    
191     if (!(fp = fopen(status_filename,"w")))
192     {
193     eventlog(eventlog_level_error,__FUNCTION__,"could not open file \"%s\" for writing (fopen: %s)",status_filename,pstrerror(errno));
194     return -1;
195     }
196    
197     output_standard_writer(fp);
198     fclose(fp);
199     return 0;
200     }
201    
202     extern void output_dispose_filename(void)
203     {
204     if (status_filename) xfree(status_filename);
205     }

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1