]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is provided for use with the unix-socket-faq. It is public | |
3 | * domain, and may be copied freely. There is no copyright on it. The | |
4 | * original work was by Vic Metcalfe (vic@brutus.tlug.org), and any | |
5 | * modifications made to that work were made with the understanding that | |
6 | * the finished work would be in the public domain. | |
7 | * | |
8 | * If you have found a bug, please pass it on to me at the above address | |
9 | * acknowledging that there will be no copyright on your work. | |
10 | * | |
11 | * The most recent version of this file, and the unix-socket-faq can be | |
12 | * found at http://www.interlog.com/~vic/sock-faq/. | |
13 | */ | |
14 | ||
15 | #include "sockhelp.h" | |
16 | #include "options.h" | |
17 | #include "list.h" | |
18 | #include "aClient.h" | |
19 | #include "extern.h" | |
20 | #include <stdio.h> | |
21 | #include <unistd.h> | |
22 | #include <string.h> | |
23 | #include <iostream.h> | |
24 | #include <fstream.h> | |
25 | #include <iomanip.h> | |
26 | #include <stdlib.h> | |
27 | ||
28 | char *PACKAGE = "GameServ"; | |
29 | char *VERSION = "1.1.4"; | |
30 | ||
31 | int sock; | |
32 | long timestamp; | |
33 | ||
34 | List<aClient> clients; | |
35 | ||
36 | void save_timestamp(); | |
37 | void load_timestamp(); | |
38 | ||
39 | int main(int argc, char *argv[]) | |
40 | { | |
41 | char buffer[1024], buf[1024]; | |
42 | int connected = 1; | |
43 | char *cmd, *source = NULL; | |
44 | srand(time(NULL)); | |
45 | ||
46 | load_config_file(); | |
47 | ||
48 | if (argc == 1) { | |
49 | argc = 3; | |
50 | argv[1] = remoteserver; | |
51 | argv[2] = remoteport; | |
52 | } | |
53 | if (argc != 3) { | |
54 | fprintf(stderr,"Usage: tcpclient host port\n"); | |
55 | fprintf(stderr,"where host is the machine which is running the\n"); | |
56 | fprintf(stderr,"tcpserver program, and port is the port it is\n"); | |
57 | fprintf(stderr,"listening on.\n"); | |
58 | exit(EXIT_FAILURE); | |
59 | } | |
60 | ignore_pipe(); | |
61 | sock = make_connection(argv[2], SOCK_STREAM, argv[1]); | |
62 | if (sock == -1) { | |
63 | fprintf(stderr,"make_connection failed.\n"); | |
64 | unload_config_file(); | |
65 | return -1; | |
66 | } | |
67 | ||
68 | #ifdef UNREAL | |
69 | raw("PROTOCTL NICKv2 VHP"); | |
70 | raw("PASS :%s", remotepass); | |
71 | raw("SERVER %s 1 :%s", servername, servername); | |
72 | raw("NICK %S 1 %d %S %s %s %d +owghraAxNt %s :%s v%s", time(NULL), gshost, | |
73 | servername, time(NULL), gshost, PACKAGE, VERSION); | |
74 | raw(":%S JOIN %s", c_Forest); | |
75 | raw(":%S MODE %s +mtn", c_Forest); | |
76 | #elif defined(BAHAMUT) | |
77 | raw("PASS %s :TS", remotepass); | |
78 | raw("SERVER %s 1 :%s", servername, servername); | |
79 | raw("NICK %S 1 %d +o %s %s %s 0 :GameServ", time(NULL), gsident, gshost, | |
80 | servername); | |
81 | raw(":%s SJOIN %d %d %s +mnt :@%S", servername, time(NULL), time(NULL), c_Forest); | |
82 | #endif | |
83 | raw(":%S MODE %s +o %S", c_Forest); | |
84 | raw(":%S TOPIC %s :%s", c_Forest, c_ForestTopic); | |
85 | ||
86 | sock_gets(sock,buffer,sizeof(buffer)-1); /* -1 added thanks to | |
87 | David Duchene <dave@ltd.com> for pointing out the possible | |
88 | buffer overflow resulting from the linefeed added below. */ | |
89 | ||
90 | ||
91 | printf("Server: %s\n",buffer); | |
92 | init_masters(); | |
93 | load_gs_dbase(); | |
94 | load_timestamp(); | |
95 | long int loadtime = time(NULL); | |
96 | long int currentTime; | |
97 | long int oldTime = loadtime; | |
98 | bool loaded = false; | |
99 | ||
100 | if (load_monsters() == false) | |
101 | goto end; | |
102 | ||
103 | while (connected) { | |
104 | if (sock_gets(sock,buffer,sizeof(buffer)) == -1) { | |
105 | connected = 0; | |
106 | } | |
107 | strcpy(buf, buffer); | |
108 | ||
109 | if (buffer[0] == ':') | |
110 | { | |
111 | source = strtok(buf, " "); | |
112 | cmd = strtok(NULL, " "); | |
113 | } | |
114 | else | |
115 | cmd = strtok(buf, " "); | |
116 | ||
117 | cout << "Server: " << buffer << endl << flush; | |
118 | ||
119 | // Wait N seconds then we're loaded. | |
120 | if (!loaded) | |
121 | { | |
122 | if (time(NULL) >= welcomedelay + loadtime) | |
123 | loaded = true; | |
124 | } | |
125 | ||
126 | // Save the player data every updateperiod seconds | |
127 | currentTime = time(NULL); | |
128 | if (currentTime - oldTime >= updateperiod) | |
129 | { | |
130 | oldTime = currentTime; | |
131 | save_gs_dbase(); | |
132 | } | |
133 | ||
134 | if (stricmp(cmd, "PING") == 0) { | |
135 | char *timestamp; | |
136 | timestamp = strtok(NULL, ""); | |
137 | raw("PONG %s", timestamp); | |
138 | } else if (stricmp(cmd, "VERSION") == 0) { | |
139 | char *server; | |
140 | server = strtok(NULL, " "); | |
141 | server++; | |
142 | raw(":%s 351 %s %s %s. %s", servername, source+1, PACKAGE, VERSION, servername); | |
143 | } else if (strncmp(cmd, "NICK", 4) == 0) { | |
144 | if (buffer[0] == ':') | |
145 | { | |
146 | aClient *tempPtr; | |
147 | if ((tempPtr = find((source + 1)))) | |
148 | { | |
149 | char *nick; | |
150 | nick = strtok(NULL, " "); | |
151 | tempPtr->setNick(nick); | |
152 | } | |
153 | } | |
154 | else | |
155 | { | |
156 | char *nick; | |
157 | aClient *newuser; | |
158 | nick = strtok(NULL, " "); | |
159 | newuser = new aClient(nick); | |
160 | if (loaded) | |
161 | notice(s_GameServ, nick, welcomemsg, nick); | |
162 | ||
163 | clients.insertAtBack(newuser); | |
164 | delete newuser; | |
165 | } | |
166 | } else if (stricmp(cmd, "QUIT") == 0) { | |
167 | aClient *quitter; | |
168 | if ((quitter = find(source + 1))) | |
169 | clients.remove(quitter); | |
170 | if ((quitter = findIRCplayer(source + 1))) | |
171 | { | |
172 | quitter->setNick("!NULL!"); | |
173 | quitter->stats->user = NULL; // Unidentify them | |
174 | } | |
175 | ||
176 | } else if (stricmp(cmd, "PRIVMSG") == 0) { | |
177 | char *rest, *dest; | |
178 | dest = strtok(NULL, " "); | |
179 | rest = strtok(NULL, ""); | |
180 | if (strnicmp(dest, s_GameServ, strlen(s_GameServ)) == 0) | |
181 | gameserv(source, rest); | |
182 | else if (stricmp(dest, c_Forest) == 0) | |
183 | forest(source, rest); | |
184 | } else if (stricmp(cmd, "JOIN") == 0) { | |
185 | char *channel; | |
186 | channel = strtok(NULL, " "); | |
187 | if (stricmp(channel, c_Forest) == 0 && is_playing(source + 1)) | |
188 | raw(":%S MODE %s +v %s", c_Forest, (source + 1)); | |
189 | ||
190 | #if defined(BAHAMUT) | |
191 | } else if (stricmp(cmd, "SJOIN") == 0) { | |
192 | char *channel, *nick; | |
193 | strtok(NULL, " "); // Ignore the TS | |
194 | strtok(NULL, " "); // Ignore the TS | |
195 | channel = strtok(NULL, " "); | |
196 | strtok(NULL, " "); | |
197 | nick = strtok(NULL, " "); | |
198 | nick++; // Get rid of the : | |
199 | if (stricmp(channel, c_Forest) == 0 && is_playing(nick)) | |
200 | raw(":%S MODE %s +v %s", channel, nick); | |
201 | #endif | |
202 | ||
203 | } else { | |
204 | // cout << "Unrecognized Message: cmd = " << cmd << setw(30) << "source = " << | |
205 | // source << endl; | |
206 | } | |
207 | } | |
208 | ||
209 | end: | |
210 | ||
211 | save_gs_dbase(); | |
212 | save_timestamp(); | |
213 | ||
214 | delete_monsters(); | |
215 | delete_masters(); | |
216 | ||
217 | printf("<CLOSED>\n"); | |
218 | close(sock); | |
219 | unload_config_file(); | |
220 | return 0; | |
221 | } | |
222 | ||
223 | aClient *find(char *nick) | |
224 | { | |
225 | return findbynick(nick); | |
226 | } | |
227 | ||
228 | aClient *find(const char *nick) | |
229 | { | |
230 | return findbynick(nick); | |
231 | } | |
232 | ||
233 | ||
234 | aClient *findbynick(char *nick) | |
235 | { | |
236 | ListNode <aClient> *newPtr; | |
237 | newPtr = clients.First(); | |
238 | ||
239 | aClient *client = NULL; | |
240 | ||
241 | while (newPtr) | |
242 | { | |
243 | client = newPtr->getData(); | |
244 | if (stricmp(client->getNick(), nick) == 0) | |
245 | return client; | |
246 | client = NULL; | |
247 | newPtr = newPtr->Next(); | |
248 | } | |
249 | return client; | |
250 | } | |
251 | ||
252 | aClient *findIRCplayer(const char *nick) | |
253 | { | |
254 | ListNode <aClient> *newPtr; | |
255 | aClient *p = NULL; | |
256 | ||
257 | for (newPtr = players.First(); newPtr; newPtr = newPtr->Next()) | |
258 | { | |
259 | p = newPtr->getData(); | |
260 | if (stricmp(p->getNick(), nick) == 0) | |
261 | return p; | |
262 | p = NULL; | |
263 | } | |
264 | return NULL; | |
265 | } | |
266 | aClient *findplayer(const char *name) | |
267 | { | |
268 | ListNode <aClient> *newPtr; | |
269 | Player *p = NULL; | |
270 | ||
271 | for (newPtr = players.First(); newPtr; newPtr = newPtr->Next()) | |
272 | { | |
273 | p = newPtr->getData()->stats; | |
274 | if (stricmp(p->name, name) == 0) | |
275 | return newPtr->getData(); | |
276 | p = NULL; | |
277 | } | |
278 | return NULL; | |
279 | } | |
280 | ||
281 | aClient *findbynick(const char *nick) | |
282 | { | |
283 | ListNode <aClient> *newPtr; | |
284 | newPtr = clients.First(); | |
285 | ||
286 | aClient *client = NULL; | |
287 | ||
288 | while (newPtr) | |
289 | { | |
290 | client = newPtr->getData(); | |
291 | if (stricmp(client->getNick(), nick) == 0) | |
292 | return client; | |
293 | client = NULL; | |
294 | newPtr = newPtr->Next(); | |
295 | } | |
296 | return client; | |
297 | } | |
298 | ||
299 | void load_timestamp() | |
300 | { | |
301 | ifstream infile; | |
302 | ||
303 | infile.open(".gstimestamp"); | |
304 | ||
305 | if (infile.fail()) | |
306 | { | |
307 | cout << "Error opening .gstimestamp" << endl; | |
308 | cout << "Generating new timestamp" << endl; | |
309 | generate: | |
310 | timestamp = midnight(); | |
311 | save_timestamp(); | |
312 | return; | |
313 | } | |
314 | ||
315 | infile >> timestamp; | |
316 | infile.close(); | |
317 | if (timestamp < 1000000) | |
318 | goto generate; | |
319 | } | |
320 | ||
321 | void save_timestamp() | |
322 | { | |
323 | ofstream outfile; | |
324 | ||
325 | outfile.open(".gstimestamp"); | |
326 | ||
327 | if (outfile.fail()) | |
328 | { | |
329 | cout << "Error creating new file." << endl; | |
330 | return; | |
331 | } | |
332 | ||
333 | outfile << timestamp << endl; | |
334 | ||
335 | outfile.close(); | |
336 | } | |
337 | ||
338 | long int midnight(long int offset) | |
339 | { | |
340 | return (time(NULL) - (time(NULL) % 86400)) + (offset * 3600); | |
341 | } |