]>
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/.
31 List
<aClient
> clients
;
33 void save_timestamp();
34 void load_timestamp();
36 int main(int argc
, char *argv
[])
38 char buffer
[1024], buf
[1024];
40 char *cmd
, *source
= NULL
;
47 argv
[1] = remoteserver
;
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");
58 sock
= make_connection(argv
[2], SOCK_STREAM
, argv
[1]);
60 fprintf(stderr
,"make_connection failed.\n");
66 raw("PROTOCTL NICKv2 VHP");
67 raw("PASS :%s", remotepass
);
68 raw("SERVER %s 1 :%s", servername
, servername
);
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
);
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
,
78 raw(":%s SJOIN %d %d %s +mnt :@%S", servername
, time(NULL
), time(NULL
), c_Forest
);
80 raw(":%S MODE %s +o %S", c_Forest
);
81 raw(":%S TOPIC %s :%s", c_Forest
, c_ForestTopic
);
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. */
88 printf("Server: %s\n",buffer
);
93 int loadtime
= time(NULL
);
97 if (sock_gets(sock
,buffer
,sizeof(buffer
)) == -1) {
102 if (buffer
[0] == ':')
104 source
= strtok(buf
, " ");
105 cmd
= strtok(NULL
, " ");
108 cmd
= strtok(buf
, " ");
110 cout
<< "Server: " << buffer
<< endl
<< flush
;
112 // Wait five seconds then we're loaded.
115 if (time(NULL
) >= 5 + loadtime
)
119 if (stricmp(cmd
, "PING") == 0) {
121 timestamp
= strtok(NULL
, "");
122 raw("PONG %s", timestamp
);
123 } else if (strncmp(cmd
, "NICK", 4) == 0) {
124 if (buffer
[0] == ':')
127 if ((tempPtr
= find((source
+ 1))))
130 nick
= strtok(NULL
, " ");
131 tempPtr
->setNick(nick
);
138 nick
= strtok(NULL
, " ");
139 newuser
= new aClient(nick
);
142 notice(s_GameServ
, nick
, "Hello, %s! This network utilizes a services package called GameServ.", nick
);
143 notice(s_GameServ
, nick
, "For info on how to play the game, type /msg %S help.");
145 clients
.insertAtBack(newuser
);
148 } else if (stricmp(cmd
, "QUIT") == 0) {
150 if ((quitter
= find(source
+ 1)))
151 clients
.remove(quitter
);
152 if ((quitter
= findplayer(source
+ 1)))
153 quitter
->setNick("NULL");
155 } else if (stricmp(cmd
, "PRIVMSG") == 0) {
157 dest
= strtok(NULL
, " ");
158 rest
= strtok(NULL
, "");
159 if (strnicmp(dest
, s_GameServ
, strlen(s_GameServ
)) == 0)
160 gameserv(source
, rest
);
161 else if (stricmp(dest
, c_Forest
) == 0)
162 forest(source
, rest
);
163 } else if (stricmp(cmd
, "JOIN") == 0) {
165 channel
= strtok(NULL
, " ");
166 if (stricmp(channel
, c_Forest
) == 0 && is_playing(source
+ 1))
167 raw(":%S MODE %s +v %s", c_Forest
, (source
+ 1));
170 } else if (stricmp(cmd
, "SJOIN") == 0) {
171 char *channel
, *nick
;
172 strtok(NULL
, " "); // Ignore the TS
173 strtok(NULL
, " "); // Ignore the TS
174 channel
= strtok(NULL
, " ");
176 nick
= strtok(NULL
, " ");
177 nick
++; // Get rid of the :
178 if (stricmp(channel
, c_Forest
) == 0 && is_playing(nick
))
179 raw(":%S MODE %s +v %s", channel
, nick
);
183 // cout << "Unrecognized Message: cmd = " << cmd << setw(30) << "source = " <<
192 printf("<CLOSED>\n");
194 unload_config_file();
198 aClient
*find(char *nick
)
200 return findbynick(nick
);
203 aClient
*find(const char *nick
)
205 return findbynick(nick
);
209 aClient
*findbynick(char *nick
)
211 ListNode
<aClient
> *newPtr
;
212 newPtr
= clients
.First();
214 aClient
*client
= NULL
;
218 client
= newPtr
->getData();
219 if (stricmp(client
->getNick(), nick
) == 0)
222 newPtr
= newPtr
->Next();
227 aClient
*findplayer(const char *name
)
229 ListNode
<aClient
> *newPtr
;
232 for (newPtr
= players
.First(); newPtr
; newPtr
= newPtr
->Next())
234 p
= newPtr
->getData()->stats
;
235 if (stricmp(p
->name
, name
) == 0)
236 return newPtr
->getData();
242 aClient
*findbynick(const char *nick
)
244 ListNode
<aClient
> *newPtr
;
245 newPtr
= clients
.First();
247 aClient
*client
= NULL
;
251 client
= newPtr
->getData();
252 if (stricmp(client
->getNick(), nick
) == 0)
255 newPtr
= newPtr
->Next();
260 void load_timestamp()
264 infile
.open(".gstimestamp");
268 cout
<< "Error opening .gstimestamp" << endl
;
269 cout
<< "Generating new timestamp" << endl
;
271 timestamp
= midnight();
278 if (timestamp
< 1000000)
282 void save_timestamp()
286 outfile
.open(".gstimestamp");
290 cout
<< "Error creating new file." << endl
;
294 outfile
<< timestamp
<< endl
;
299 long int midnight(long int offset
)
301 return (time(NULL
) - (time(NULL
) % 86400)) + (offset
* 3600);