]>
jfr.im git - irc/gameservirc.git/blob - gameserv/tcpclient.cpp
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.
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.
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/.
37 List
<aClient
> clients
;
39 void save_timestamp();
40 void load_timestamp();
42 int main(int argc
, char *argv
[])
44 char buffer
[1024], buf
[1024];
46 char *cmd
, *source
= NULL
;
53 argv
[1] = remoteserver
;
57 fprintf(stderr
,"Usage: tcpclient host port\n");
58 fprintf(stderr
,"where host is the machine which is running the\n");
59 fprintf(stderr
,"tcpserver program, and port is the port it is\n");
60 fprintf(stderr
,"listening on.\n");
64 sock
= make_connection(argv
[2], SOCK_STREAM
, argv
[1]);
66 fprintf(stderr
,"make_connection failed.\n");
72 raw("PROTOCTL NICKv2 VHP");
73 raw("PASS :%s", remotepass
);
74 raw("SERVER %s 1 :%s", servername
, servername
);
75 raw("NICK %S 1 %d %S %s %s %d +owghraAxNt %s :%s v%s", time(NULL
), gshost
,
76 servername
, time(NULL
), gshost
, PACKAGE
, VERSION
);
77 raw(":%S JOIN %s", c_Forest
);
78 raw(":%S MODE %s +mtn", c_Forest
);
79 #elif defined(BAHAMUT)
80 raw("PASS %s :TS", remotepass
);
81 raw("SERVER %s 1 :%s", servername
, servername
);
82 raw("NICK %S 1 %d +o %s %s %s 0 :GameServ", time(NULL
), gsident
, gshost
,
84 raw(":%s SJOIN %d %d %s +mnt :@%S", servername
, time(NULL
), time(NULL
), c_Forest
);
86 raw(":%S MODE %s +o %S", c_Forest
);
87 raw(":%S TOPIC %s :%s", c_Forest
, c_ForestTopic
);
89 sock_gets(sock
,buffer
,sizeof(buffer
)-1); /* -1 added thanks to
90 David Duchene <dave@ltd.com> for pointing out the possible
91 buffer overflow resulting from the linefeed added below. */
94 printf("Server: %s\n",buffer
);
99 int loadtime
= time(NULL
);
103 if (sock_gets(sock
,buffer
,sizeof(buffer
)) == -1) {
108 if (buffer
[0] == ':')
110 source
= strtok(buf
, " ");
111 cmd
= strtok(NULL
, " ");
114 cmd
= strtok(buf
, " ");
116 cout
<< "Server: " << buffer
<< endl
<< flush
;
118 // Wait five seconds then we're loaded.
121 if (time(NULL
) >= 5 + loadtime
)
125 if (stricmp(cmd
, "PING") == 0) {
127 timestamp
= strtok(NULL
, "");
128 raw("PONG %s", timestamp
);
129 } else if (strncmp(cmd
, "NICK", 4) == 0) {
130 if (buffer
[0] == ':')
133 if ((tempPtr
= find((source
+ 1))))
136 nick
= strtok(NULL
, " ");
137 tempPtr
->setNick(nick
);
144 nick
= strtok(NULL
, " ");
145 newuser
= new aClient(nick
);
147 notice(s_GameServ
, nick
, "Hello, %s! This network utilizes a services package called GameServ. For info on how to play the game, type /msg %S help.");
149 clients
.insertAtBack(newuser
);
152 } else if (stricmp(cmd
, "QUIT") == 0) {
154 if ((quitter
= find(source
+ 1)))
155 clients
.remove(quitter
);
156 if ((quitter
= findplayer(source
+ 1)))
157 quitter
->setNick("NULL");
159 } else if (stricmp(cmd
, "PRIVMSG") == 0) {
161 dest
= strtok(NULL
, " ");
162 rest
= strtok(NULL
, "");
163 if (strnicmp(dest
, s_GameServ
, strlen(s_GameServ
)) == 0)
164 gameserv(source
, rest
);
165 else if (stricmp(dest
, c_Forest
) == 0)
166 forest(source
, rest
);
167 } else if (stricmp(cmd
, "JOIN") == 0) {
169 channel
= strtok(NULL
, " ");
170 if (stricmp(channel
, c_Forest
) == 0 && is_playing(source
+ 1))
171 raw(":%S MODE %s +v %s", c_Forest
, (source
+ 1));
174 } else if (stricmp(cmd
, "SJOIN") == 0) {
175 char *channel
, *nick
;
176 strtok(NULL
, " "); // Ignore the TS
177 strtok(NULL
, " "); // Ignore the TS
178 channel
= strtok(NULL
, " ");
180 nick
= strtok(NULL
, " ");
181 nick
++; // Get rid of the :
182 if (stricmp(channel
, c_Forest
) == 0 && is_playing(nick
))
183 raw(":%S MODE %s +v %s", channel
, nick
);
187 // cout << "Unrecognized Message: cmd = " << cmd << setw(30) << "source = " <<
196 printf("<CLOSED>\n");
198 unload_config_file();
202 aClient
*find(char *nick
)
204 return findbynick(nick
);
207 aClient
*find(const char *nick
)
209 return findbynick(nick
);
213 aClient
*findbynick(char *nick
)
215 ListNode
<aClient
> *newPtr
;
216 newPtr
= clients
.First();
218 aClient
*client
= NULL
;
222 client
= newPtr
->getData();
223 if (stricmp(client
->getNick(), nick
) == 0)
226 newPtr
= newPtr
->Next();
231 aClient
*findplayer(const char *name
)
233 ListNode
<aClient
> *newPtr
;
236 for (newPtr
= players
.First(); newPtr
; newPtr
= newPtr
->Next())
238 p
= newPtr
->getData()->stats
;
239 if (stricmp(p
->name
, name
) == 0)
240 return newPtr
->getData();
246 aClient
*findbynick(const char *nick
)
248 ListNode
<aClient
> *newPtr
;
249 newPtr
= clients
.First();
251 aClient
*client
= NULL
;
255 client
= newPtr
->getData();
256 if (stricmp(client
->getNick(), nick
) == 0)
259 newPtr
= newPtr
->Next();
264 void load_timestamp()
268 infile
.open(".gstimestamp");
272 cout
<< "Error opening .gstimestamp" << endl
;
273 cout
<< "Generating new timestamp" << endl
;
275 timestamp
= midnight();
282 if (timestamp
< 1000000)
286 void save_timestamp()
290 outfile
.open(".gstimestamp");
294 cout
<< "Error creating new file." << endl
;
298 outfile
<< timestamp
<< endl
;
303 long int midnight(long int offset
)
305 return (time(NULL
) - (time(NULL
) % 86400)) + (offset
* 3600);