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

Contents of /pvpgn-1.7.4/src/bnetd/autoupdate.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1.1.1 - (show annotations) (vendor branch)
Tue Jun 6 03:41:37 2006 UTC (19 years, 9 months ago) by sysadm
Branch: GNU, MAIN
CVS Tags: pvpgn_1-7-4-0_MIL, arelease, HEAD
Changes since 1.1: +0 -0 lines
Content type: text/x-csrc
no message

1 /*
2 * Copyright (C) 2000 Rob Crittenden (rcrit@greyoak.com)
3 * Copyright (C) 2002 Gianluigi Tiesi (sherpya@netfarm.it)
4 * Copyright (C) 2004 CreepLord (creeplord@pvpgn.org)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (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 #define AUTOUPDATE_INTERNAL_ACCESS
21 #include "common/setup_before.h"
22 #include <stdio.h>
23 #ifdef HAVE_STDDEF_H
24 # include <stddef.h>
25 #else
26 # ifndef NULL
27 # define NULL ((void *)0)
28 # endif
29 #endif
30 #ifdef STDC_HEADERS
31 # include <stdlib.h>
32 #else
33 # ifdef HAVE_MALLOC_H
34 # include <malloc.h>
35 # endif
36 #endif
37 #include "compat/strtoul.h"
38 #ifdef HAVE_STRING_H
39 # include <string.h>
40 #else
41 # ifdef HAVE_STRINGS_H
42 # include <strings.h>
43 # endif
44 #endif
45 #include "compat/strchr.h"
46 #include "compat/strdup.h"
47 #include <errno.h>
48 #include "compat/strerror.h"
49 #include "common/eventlog.h"
50 #include "common/list.h"
51 #include "common/util.h"
52 #include "common/proginfo.h"
53 #include "common/tag.h"
54 #include "common/xalloc.h"
55 #include "autoupdate.h"
56 #include "common/setup_after.h"
57
58
59 static t_list * autoupdate_head=NULL;
60 static FILE * fp = NULL;
61
62
63 /*
64 * Open the autoupdate configuration file, create a linked list of the
65 * clienttag and the update file for it. The format of the file is:
66 * archtag<tab>clienttag<tab>versiontag<tab>update file
67 *
68 * Comments begin with # and are ignored.
69 *
70 * The server assumes that the update file is in the "files" directory
71 * so do not include "/" in the filename - it won't be sent
72 * (because it is a security risk).
73 */
74
75 extern int autoupdate_load(char const * filename)
76 {
77 unsigned int line;
78 unsigned int pos;
79 char * buff;
80 char * temp;
81 char const * archtag;
82 char const * clienttag;
83 char const * mpqfile;
84 char const * versiontag;
85 t_autoupdate * entry;
86
87 if (!filename) {
88 eventlog(eventlog_level_error,__FUNCTION__,"got NULL filename");
89 return -1;
90 }
91
92 if (!(fp = fopen(filename,"r"))) {
93 eventlog(eventlog_level_error,__FUNCTION__,"could not open file \"%s\" for reading (fopen: %s)",filename,pstrerror(errno));
94 return -1;
95 }
96
97 autoupdate_head = list_create();
98
99 for (line=1; (buff = file_get_line(fp)); line++) {
100 for (pos=0; buff[pos]=='\t' || buff[pos]==' '; pos++);
101
102 if (buff[pos]=='\0' || buff[pos]=='#') {
103 continue;
104 }
105
106 if ((temp = strrchr(buff,'#'))) {
107 unsigned int len;
108 unsigned int endpos;
109
110 *temp = '\0';
111 len = strlen(buff)+1;
112 for (endpos=len-1; buff[endpos]=='\t' || buff[endpos]==' '; endpos--);
113 buff[endpos+1] = '\0';
114 }
115
116 /* FIXME: use next_token instead of strtok */
117 if (!(archtag = strtok(buff, " \t"))) { /* strtok modifies the string it is passed */
118 eventlog(eventlog_level_error,__FUNCTION__,"missing archtag on line %u of file \"%s\"",line,filename);
119 continue;
120 }
121 if (!(clienttag = strtok(NULL," \t"))) {
122 eventlog(eventlog_level_error,__FUNCTION__,"missing clienttag on line %u of file \"%s\"",line,filename);
123 continue;
124 }
125 if (!(versiontag = strtok(NULL, " \t"))) {
126 eventlog(eventlog_level_error,__FUNCTION__,"missing versiontag on line %u of file \"%s\"",line,filename);
127 continue;
128 }
129 if (!(mpqfile = strtok(NULL," \t"))) {
130 eventlog(eventlog_level_error,__FUNCTION__,"missing mpqfile on line %u of file \"%s\"",line,filename);
131 continue;
132 }
133
134 entry = xmalloc(sizeof(t_autoupdate));
135
136 if (!tag_check_arch((entry->archtag = tag_str_to_uint(archtag)))) {
137 eventlog(eventlog_level_error,__FUNCTION__,"got unknown archtag");
138 xfree(entry);
139 continue;
140 }
141 if (!tag_check_client((entry->clienttag = tag_str_to_uint(clienttag)))) {
142 eventlog(eventlog_level_error,__FUNCTION__,"got unknown clienttag");
143 xfree(entry);
144 continue;
145 }
146 entry->versiontag = xstrdup(versiontag);
147 entry->mpqfile = xstrdup(mpqfile);
148
149 eventlog(eventlog_level_debug,__FUNCTION__,"update '%s' version '%s' with file %s",clienttag,versiontag,mpqfile);
150
151 list_append_data(autoupdate_head,entry);
152 }
153 file_get_line(NULL); // clear file_get_line buffer
154 fclose(fp);
155 return 0;
156 }
157
158 /*
159 * Free up all of the elements in the linked list
160 */
161
162 extern int autoupdate_unload(void)
163 {
164 if (autoupdate_head) {
165 t_elem * curr;
166 t_autoupdate * entry;
167 LIST_TRAVERSE(autoupdate_head,curr)
168 {
169 if (!(entry = elem_get_data(curr)))
170 eventlog(eventlog_level_error,__FUNCTION__,"found NULL entry in list");
171 else {
172 xfree((void *)entry->versiontag); /* avoid warning */
173 xfree((void *)entry->mpqfile); /* avoid warning */
174 xfree(entry);
175 }
176 list_remove_elem(autoupdate_head,&curr);
177 }
178
179 if (list_destroy(autoupdate_head)<0) return -1;
180 autoupdate_head = NULL;
181 }
182 return 0;
183 }
184
185 /*
186 * Check to see if an update exists for the clients version
187 * return file name if there is one
188 * retrun NULL if no update exists
189 */
190
191 extern char * autoupdate_check(t_tag archtag, t_tag clienttag, t_tag gamelang, char const * versiontag)
192 {
193 if (autoupdate_head) {
194 t_elem const * curr;
195 t_autoupdate * entry;
196 char * temp;
197
198 LIST_TRAVERSE_CONST(autoupdate_head,curr)
199 {
200 if (!(entry = elem_get_data(curr))) {
201 eventlog(eventlog_level_error,__FUNCTION__,"found NULL entry in list");
202 continue;
203 }
204
205 if (entry->archtag != archtag)
206 continue;
207 if (entry->clienttag != clienttag)
208 continue;
209 if (strcmp(entry->versiontag, versiontag) != 0)
210 continue;
211
212 /* if we have a gamelang then add it to the mpq file name */
213 if (gamelang) {
214 char gltag[5];
215 char * tempmpq;
216 char * extention;
217
218 tag_uint_to_str(gltag,gamelang);
219 tempmpq = xstrdup(entry->mpqfile);
220
221 temp = xmalloc(strlen(tempmpq)+6);
222
223 extention = strrchr(tempmpq,'.');
224 *extention = '\0';
225 extention++;
226
227 sprintf(temp, "%s_%s.%s", tempmpq, gltag, extention);
228
229 xfree((void *)tempmpq);
230 return temp;
231 }
232 temp = xstrdup(entry->mpqfile);
233 return temp;
234 }
235 }
236 return NULL;
237 }

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