]>
Commit | Line | Data |
---|---|---|
85ce9d3e | 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" | |
c7340cbd | 16 | #include "options.h" |
85ce9d3e | 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> | |
44ea29f7 | 24 | #include <fstream.h> |
85ce9d3e | 25 | #include <iomanip.h> |
85ce9d3e | 26 | #include <stdlib.h> |
27 | ||
91c0b563 | 28 | char *PACKAGE = "GameServ"; |
29 | char *VERSION = "1.1.2"; | |
173302fe | 30 | |
85ce9d3e | 31 | int sock; |
44ea29f7 | 32 | long timestamp; |
33 | ||
85ce9d3e | 34 | List<aClient> clients; |
35 | ||
44ea29f7 | 36 | void save_timestamp(); |
37 | void load_timestamp(); | |
38 | ||
85ce9d3e | 39 | int main(int argc, char *argv[]) |
40 | { | |
28f552b8 | 41 | char buffer[1024], buf[1024]; |
85ce9d3e | 42 | int connected = 1; |
43 | char *cmd, *source = NULL; | |
44 | srand(time(NULL)); | |
44ea29f7 | 45 | |
46 | load_config_file(); | |
85ce9d3e | 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 | ||
c7340cbd | 68 | #ifdef UNREAL |
85ce9d3e | 69 | raw("PROTOCTL NICKv2 VHP"); |
70 | raw("PASS :%s", remotepass); | |
c7340cbd | 71 | raw("SERVER %s 1 :%s", servername, servername); |
173302fe | 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); | |
85ce9d3e | 74 | raw(":%S JOIN %s", c_Forest); |
c7340cbd | 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 | |
85ce9d3e | 83 | raw(":%S MODE %s +o %S", c_Forest); |
c7340cbd | 84 | raw(":%S TOPIC %s :%s", c_Forest, c_ForestTopic); |
85ce9d3e | 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); | |
ab4f4ec0 | 92 | init_masters(); |
ad7dfaa0 | 93 | load_gs_dbase(); |
44ea29f7 | 94 | load_timestamp(); |
5963944b | 95 | int loadtime = time(NULL); |
96 | bool loaded = false; | |
44ea29f7 | 97 | |
4dde2ed9 | 98 | if (load_monsters() == false) |
99 | goto end; | |
100 | ||
85ce9d3e | 101 | while (connected) { |
102 | if (sock_gets(sock,buffer,sizeof(buffer)) == -1) { | |
103 | connected = 0; | |
104 | } | |
105 | strcpy(buf, buffer); | |
106 | ||
107 | if (buffer[0] == ':') | |
108 | { | |
109 | source = strtok(buf, " "); | |
110 | cmd = strtok(NULL, " "); | |
111 | } | |
112 | else | |
113 | cmd = strtok(buf, " "); | |
114 | ||
0a1518fa | 115 | cout << "Server: " << buffer << endl << flush; |
5963944b | 116 | |
bf2cabcd | 117 | // Wait N seconds then we're loaded. |
5963944b | 118 | if (!loaded) |
119 | { | |
bf2cabcd | 120 | if (time(NULL) >= confloadtime + loadtime) |
5963944b | 121 | loaded = true; |
122 | } | |
123 | ||
85ce9d3e | 124 | if (stricmp(cmd, "PING") == 0) { |
0a1518fa | 125 | char *timestamp; |
126 | timestamp = strtok(NULL, ""); | |
127 | raw("PONG %s", timestamp); | |
0501fe18 | 128 | } else if (stricmp(cmd, "VERSION") == 0) { |
129 | char *server; | |
130 | server = strtok(NULL, " "); | |
131 | server++; | |
0501fe18 | 132 | raw(":%s 351 %s %s %s. %s", servername, source+1, PACKAGE, VERSION, servername); |
85ce9d3e | 133 | } else if (strncmp(cmd, "NICK", 4) == 0) { |
134 | if (buffer[0] == ':') | |
135 | { | |
136 | aClient *tempPtr; | |
28f552b8 | 137 | if ((tempPtr = find((source + 1)))) |
85ce9d3e | 138 | { |
139 | char *nick; | |
140 | nick = strtok(NULL, " "); | |
141 | tempPtr->setNick(nick); | |
142 | } | |
143 | } | |
144 | else | |
145 | { | |
146 | char *nick; | |
147 | aClient *newuser; | |
148 | nick = strtok(NULL, " "); | |
149 | newuser = new aClient(nick); | |
5963944b | 150 | if (loaded) |
bf2cabcd | 151 | notice(s_GameServ, nick, welcomemsg, nick); |
b0a5c536 | 152 | |
85ce9d3e | 153 | clients.insertAtBack(newuser); |
154 | delete newuser; | |
155 | } | |
156 | } else if (stricmp(cmd, "QUIT") == 0) { | |
157 | aClient *quitter; | |
28f552b8 | 158 | if ((quitter = find(source + 1))) |
85ce9d3e | 159 | clients.remove(quitter); |
28f552b8 | 160 | if ((quitter = findplayer(source + 1))) |
dff2bbf4 | 161 | quitter->setNick("NULL"); |
85ce9d3e | 162 | |
163 | } else if (stricmp(cmd, "PRIVMSG") == 0) { | |
164 | char *rest, *dest; | |
165 | dest = strtok(NULL, " "); | |
166 | rest = strtok(NULL, ""); | |
ad7dfaa0 | 167 | if (strnicmp(dest, s_GameServ, strlen(s_GameServ)) == 0) |
85ce9d3e | 168 | gameserv(source, rest); |
169 | else if (stricmp(dest, c_Forest) == 0) | |
170 | forest(source, rest); | |
171 | } else if (stricmp(cmd, "JOIN") == 0) { | |
172 | char *channel; | |
173 | channel = strtok(NULL, " "); | |
174 | if (stricmp(channel, c_Forest) == 0 && is_playing(source + 1)) | |
175 | raw(":%S MODE %s +v %s", c_Forest, (source + 1)); | |
c7340cbd | 176 | |
177 | #if defined(BAHAMUT) | |
178 | } else if (stricmp(cmd, "SJOIN") == 0) { | |
179 | char *channel, *nick; | |
180 | strtok(NULL, " "); // Ignore the TS | |
181 | strtok(NULL, " "); // Ignore the TS | |
182 | channel = strtok(NULL, " "); | |
183 | strtok(NULL, " "); | |
184 | nick = strtok(NULL, " "); | |
185 | nick++; // Get rid of the : | |
186 | if (stricmp(channel, c_Forest) == 0 && is_playing(nick)) | |
187 | raw(":%S MODE %s +v %s", channel, nick); | |
188 | #endif | |
189 | ||
85ce9d3e | 190 | } else { |
191 | // cout << "Unrecognized Message: cmd = " << cmd << setw(30) << "source = " << | |
192 | // source << endl; | |
193 | } | |
194 | } | |
4dde2ed9 | 195 | |
196 | end: | |
197 | ||
c8ada07e | 198 | save_gs_dbase(); |
44ea29f7 | 199 | save_timestamp(); |
4dde2ed9 | 200 | |
c8ada07e | 201 | delete_monsters(); |
202 | delete_masters(); | |
203 | ||
85ce9d3e | 204 | printf("<CLOSED>\n"); |
205 | close(sock); | |
206 | unload_config_file(); | |
207 | return 0; | |
208 | } | |
209 | ||
210 | aClient *find(char *nick) | |
211 | { | |
212 | return findbynick(nick); | |
213 | } | |
214 | ||
215 | aClient *find(const char *nick) | |
216 | { | |
217 | return findbynick(nick); | |
218 | } | |
219 | ||
220 | ||
221 | aClient *findbynick(char *nick) | |
222 | { | |
223 | ListNode <aClient> *newPtr; | |
224 | newPtr = clients.First(); | |
225 | ||
226 | aClient *client = NULL; | |
227 | ||
228 | while (newPtr) | |
229 | { | |
230 | client = newPtr->getData(); | |
231 | if (stricmp(client->getNick(), nick) == 0) | |
232 | return client; | |
233 | client = NULL; | |
234 | newPtr = newPtr->Next(); | |
235 | } | |
236 | return client; | |
237 | } | |
238 | ||
0a1518fa | 239 | aClient *findplayer(const char *name) |
240 | { | |
241 | ListNode <aClient> *newPtr; | |
242 | Player *p = NULL; | |
243 | ||
244 | for (newPtr = players.First(); newPtr; newPtr = newPtr->Next()) | |
245 | { | |
246 | p = newPtr->getData()->stats; | |
247 | if (stricmp(p->name, name) == 0) | |
248 | return newPtr->getData(); | |
249 | p = NULL; | |
250 | } | |
251 | return NULL; | |
252 | } | |
253 | ||
85ce9d3e | 254 | aClient *findbynick(const char *nick) |
255 | { | |
256 | ListNode <aClient> *newPtr; | |
257 | newPtr = clients.First(); | |
258 | ||
259 | aClient *client = NULL; | |
260 | ||
261 | while (newPtr) | |
262 | { | |
263 | client = newPtr->getData(); | |
264 | if (stricmp(client->getNick(), nick) == 0) | |
265 | return client; | |
266 | client = NULL; | |
267 | newPtr = newPtr->Next(); | |
268 | } | |
269 | return client; | |
270 | } | |
271 | ||
44ea29f7 | 272 | void load_timestamp() |
273 | { | |
274 | ifstream infile; | |
275 | ||
276 | infile.open(".gstimestamp"); | |
277 | ||
278 | if (infile.fail()) | |
279 | { | |
9cc5ab57 | 280 | cout << "Error opening .gstimestamp" << endl; |
281 | cout << "Generating new timestamp" << endl; | |
44ea29f7 | 282 | generate: |
283 | timestamp = midnight(); | |
284 | save_timestamp(); | |
285 | return; | |
286 | } | |
287 | ||
288 | infile >> timestamp; | |
289 | infile.close(); | |
290 | if (timestamp < 1000000) | |
291 | goto generate; | |
292 | } | |
293 | ||
294 | void save_timestamp() | |
295 | { | |
296 | ofstream outfile; | |
297 | ||
298 | outfile.open(".gstimestamp"); | |
299 | ||
300 | if (outfile.fail()) | |
301 | { | |
9cc5ab57 | 302 | cout << "Error creating new file." << endl; |
44ea29f7 | 303 | return; |
304 | } | |
305 | ||
306 | outfile << timestamp << endl; | |
307 | ||
308 | outfile.close(); | |
309 | } | |
310 | ||
311 | long int midnight(long int offset) | |
312 | { | |
313 | return (time(NULL) - (time(NULL) % 86400)) + (offset * 3600); | |
314 | } |