]>
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 | ||
85ce9d3e | 28 | int sock; |
44ea29f7 | 29 | long timestamp; |
30 | ||
85ce9d3e | 31 | List<aClient> clients; |
32 | ||
44ea29f7 | 33 | void save_timestamp(); |
34 | void load_timestamp(); | |
35 | ||
85ce9d3e | 36 | int main(int argc, char *argv[]) |
37 | { | |
28f552b8 | 38 | char buffer[1024], buf[1024]; |
85ce9d3e | 39 | int connected = 1; |
40 | char *cmd, *source = NULL; | |
41 | srand(time(NULL)); | |
44ea29f7 | 42 | |
43 | load_config_file(); | |
85ce9d3e | 44 | |
45 | if (argc == 1) { | |
46 | argc = 3; | |
47 | argv[1] = remoteserver; | |
48 | argv[2] = remoteport; | |
49 | } | |
50 | if (argc != 3) { | |
51 | fprintf(stderr,"Usage: tcpclient host port\n"); | |
52 | fprintf(stderr,"where host is the machine which is running the\n"); | |
53 | fprintf(stderr,"tcpserver program, and port is the port it is\n"); | |
54 | fprintf(stderr,"listening on.\n"); | |
55 | exit(EXIT_FAILURE); | |
56 | } | |
57 | ignore_pipe(); | |
58 | sock = make_connection(argv[2], SOCK_STREAM, argv[1]); | |
59 | if (sock == -1) { | |
60 | fprintf(stderr,"make_connection failed.\n"); | |
61 | unload_config_file(); | |
62 | return -1; | |
63 | } | |
64 | ||
c7340cbd | 65 | #ifdef UNREAL |
85ce9d3e | 66 | raw("PROTOCTL NICKv2 VHP"); |
67 | raw("PASS :%s", remotepass); | |
c7340cbd | 68 | raw("SERVER %s 1 :%s", servername, servername); |
85ce9d3e | 69 | raw("NICK %S 1 %d %S %s %s %d +owghraAxNt %s :GameServ", time(NULL), gshost, |
70 | servername, time(NULL), gshost); | |
71 | raw(":%S JOIN %s", c_Forest); | |
c7340cbd | 72 | raw(":%S MODE %s +mtn", c_Forest); |
73 | #elif defined(BAHAMUT) | |
74 | raw("PASS %s :TS", remotepass); | |
75 | raw("SERVER %s 1 :%s", servername, servername); | |
76 | raw("NICK %S 1 %d +o %s %s %s 0 :GameServ", time(NULL), gsident, gshost, | |
77 | servername); | |
78 | raw(":%s SJOIN %d %d %s +mnt :@%S", servername, time(NULL), time(NULL), c_Forest); | |
79 | #endif | |
85ce9d3e | 80 | raw(":%S MODE %s +o %S", c_Forest); |
c7340cbd | 81 | raw(":%S TOPIC %s :%s", c_Forest, c_ForestTopic); |
85ce9d3e | 82 | |
83 | sock_gets(sock,buffer,sizeof(buffer)-1); /* -1 added thanks to | |
84 | David Duchene <dave@ltd.com> for pointing out the possible | |
85 | buffer overflow resulting from the linefeed added below. */ | |
86 | ||
87 | ||
88 | printf("Server: %s\n",buffer); | |
89 | init_monsters(); | |
ab4f4ec0 | 90 | init_masters(); |
ad7dfaa0 | 91 | load_gs_dbase(); |
44ea29f7 | 92 | load_timestamp(); |
93 | ||
85ce9d3e | 94 | while (connected) { |
95 | if (sock_gets(sock,buffer,sizeof(buffer)) == -1) { | |
96 | connected = 0; | |
97 | } | |
98 | strcpy(buf, buffer); | |
99 | ||
100 | if (buffer[0] == ':') | |
101 | { | |
102 | source = strtok(buf, " "); | |
103 | cmd = strtok(NULL, " "); | |
104 | } | |
105 | else | |
106 | cmd = strtok(buf, " "); | |
107 | ||
0a1518fa | 108 | cout << "Server: " << buffer << endl << flush; |
85ce9d3e | 109 | if (stricmp(cmd, "PING") == 0) { |
0a1518fa | 110 | char *timestamp; |
111 | timestamp = strtok(NULL, ""); | |
112 | raw("PONG %s", timestamp); | |
85ce9d3e | 113 | } else if (strncmp(cmd, "NICK", 4) == 0) { |
114 | if (buffer[0] == ':') | |
115 | { | |
116 | aClient *tempPtr; | |
28f552b8 | 117 | if ((tempPtr = find((source + 1)))) |
85ce9d3e | 118 | { |
119 | char *nick; | |
120 | nick = strtok(NULL, " "); | |
121 | tempPtr->setNick(nick); | |
122 | } | |
123 | } | |
124 | else | |
125 | { | |
126 | char *nick; | |
127 | aClient *newuser; | |
128 | nick = strtok(NULL, " "); | |
129 | newuser = new aClient(nick); | |
130 | clients.insertAtBack(newuser); | |
131 | delete newuser; | |
132 | } | |
133 | } else if (stricmp(cmd, "QUIT") == 0) { | |
134 | aClient *quitter; | |
28f552b8 | 135 | if ((quitter = find(source + 1))) |
85ce9d3e | 136 | clients.remove(quitter); |
28f552b8 | 137 | if ((quitter = findplayer(source + 1))) |
dff2bbf4 | 138 | quitter->setNick("NULL"); |
85ce9d3e | 139 | |
140 | } else if (stricmp(cmd, "PRIVMSG") == 0) { | |
141 | char *rest, *dest; | |
142 | dest = strtok(NULL, " "); | |
143 | rest = strtok(NULL, ""); | |
ad7dfaa0 | 144 | if (strnicmp(dest, s_GameServ, strlen(s_GameServ)) == 0) |
85ce9d3e | 145 | gameserv(source, rest); |
146 | else if (stricmp(dest, c_Forest) == 0) | |
147 | forest(source, rest); | |
148 | } else if (stricmp(cmd, "JOIN") == 0) { | |
149 | char *channel; | |
150 | channel = strtok(NULL, " "); | |
151 | if (stricmp(channel, c_Forest) == 0 && is_playing(source + 1)) | |
152 | raw(":%S MODE %s +v %s", c_Forest, (source + 1)); | |
c7340cbd | 153 | |
154 | #if defined(BAHAMUT) | |
155 | } else if (stricmp(cmd, "SJOIN") == 0) { | |
156 | char *channel, *nick; | |
157 | strtok(NULL, " "); // Ignore the TS | |
158 | strtok(NULL, " "); // Ignore the TS | |
159 | channel = strtok(NULL, " "); | |
160 | strtok(NULL, " "); | |
161 | nick = strtok(NULL, " "); | |
162 | nick++; // Get rid of the : | |
163 | if (stricmp(channel, c_Forest) == 0 && is_playing(nick)) | |
164 | raw(":%S MODE %s +v %s", channel, nick); | |
165 | #endif | |
166 | ||
85ce9d3e | 167 | } else { |
168 | // cout << "Unrecognized Message: cmd = " << cmd << setw(30) << "source = " << | |
169 | // source << endl; | |
170 | } | |
171 | } | |
c8ada07e | 172 | save_gs_dbase(); |
44ea29f7 | 173 | save_timestamp(); |
c8ada07e | 174 | delete_monsters(); |
175 | delete_masters(); | |
176 | ||
85ce9d3e | 177 | printf("<CLOSED>\n"); |
178 | close(sock); | |
179 | unload_config_file(); | |
180 | return 0; | |
181 | } | |
182 | ||
183 | aClient *find(char *nick) | |
184 | { | |
185 | return findbynick(nick); | |
186 | } | |
187 | ||
188 | aClient *find(const char *nick) | |
189 | { | |
190 | return findbynick(nick); | |
191 | } | |
192 | ||
193 | ||
194 | aClient *findbynick(char *nick) | |
195 | { | |
196 | ListNode <aClient> *newPtr; | |
197 | newPtr = clients.First(); | |
198 | ||
199 | aClient *client = NULL; | |
200 | ||
201 | while (newPtr) | |
202 | { | |
203 | client = newPtr->getData(); | |
204 | if (stricmp(client->getNick(), nick) == 0) | |
205 | return client; | |
206 | client = NULL; | |
207 | newPtr = newPtr->Next(); | |
208 | } | |
209 | return client; | |
210 | } | |
211 | ||
0a1518fa | 212 | aClient *findplayer(const char *name) |
213 | { | |
214 | ListNode <aClient> *newPtr; | |
215 | Player *p = NULL; | |
216 | ||
217 | for (newPtr = players.First(); newPtr; newPtr = newPtr->Next()) | |
218 | { | |
219 | p = newPtr->getData()->stats; | |
220 | if (stricmp(p->name, name) == 0) | |
221 | return newPtr->getData(); | |
222 | p = NULL; | |
223 | } | |
224 | return NULL; | |
225 | } | |
226 | ||
85ce9d3e | 227 | aClient *findbynick(const char *nick) |
228 | { | |
229 | ListNode <aClient> *newPtr; | |
230 | newPtr = clients.First(); | |
231 | ||
232 | aClient *client = NULL; | |
233 | ||
234 | while (newPtr) | |
235 | { | |
236 | client = newPtr->getData(); | |
237 | if (stricmp(client->getNick(), nick) == 0) | |
238 | return client; | |
239 | client = NULL; | |
240 | newPtr = newPtr->Next(); | |
241 | } | |
242 | return client; | |
243 | } | |
244 | ||
44ea29f7 | 245 | void load_timestamp() |
246 | { | |
247 | ifstream infile; | |
248 | ||
249 | infile.open(".gstimestamp"); | |
250 | ||
251 | if (infile.fail()) | |
252 | { | |
253 | cerr << "Error opening .gstimestamp" << endl; | |
254 | cerr << "Generating new timestamp" << endl; | |
255 | generate: | |
256 | timestamp = midnight(); | |
257 | save_timestamp(); | |
258 | return; | |
259 | } | |
260 | ||
261 | infile >> timestamp; | |
262 | infile.close(); | |
263 | if (timestamp < 1000000) | |
264 | goto generate; | |
265 | } | |
266 | ||
267 | void save_timestamp() | |
268 | { | |
269 | ofstream outfile; | |
270 | ||
271 | outfile.open(".gstimestamp"); | |
272 | ||
273 | if (outfile.fail()) | |
274 | { | |
275 | cerr << "Error creating new file." << endl; | |
276 | return; | |
277 | } | |
278 | ||
279 | outfile << timestamp << endl; | |
280 | ||
281 | outfile.close(); | |
282 | } | |
283 | ||
284 | long int midnight(long int offset) | |
285 | { | |
286 | return (time(NULL) - (time(NULL) % 86400)) + (offset * 3600); | |
287 | } |