]> git.mxchange.org Git - quix0rs-blobwars.git/blob - src/CMedalServer.cpp
Use snprintf() and strncpy().
[quix0rs-blobwars.git] / src / CMedalServer.cpp
1 /*
2 Copyright (C) 2010 Parallel Realities
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20
21 #include "headers.h"
22 #include "SDL_mutex.h"
23
24 MedalServer::MedalServer()
25 {
26         lock = SDL_CreateMutex();
27         
28         connected = false;
29         gotRuby = false;
30         
31         message[0] = 0;
32         rubyMessage[0] = 0;
33 }
34
35 MedalServer::~MedalServer()
36 {
37         if (connected)
38         {
39                 close();
40         }
41 }
42
43 bool MedalServer::connect(const char *privateKey)
44 {
45         if (SDLNet_ResolveHost(&ip, MEDAL_SERVER_HOST, MEDAL_SERVER_PORT) == -1)
46         {
47                 printf("ERROR: SDLNet_ResolveHost: %s\n", SDLNet_GetError());
48                 return false;
49         }
50         
51         debug(("Connected %s to %s:%d\n", privateKey, MEDAL_SERVER_HOST, MEDAL_SERVER_PORT));
52         
53         strncpy(this->privateKey, privateKey, sizeof this->privateKey);
54         connected = true;
55         
56         return true;
57 }
58
59 #if !UNIX
60 extern char *strtok_r(char *s1, const char *s2, char **lasts);
61 #endif
62                 
63 int MedalServer::postMedal(const char *str)
64 {
65         if (!connected)
66         {
67                 return 0;
68         }
69         
70         char *store;
71         
72         char medal[128];
73         strncpy(medal, str, sizeof medal);
74         
75         for (unsigned int i = 0 ; i < strlen(medal) ; i++)
76         {
77                 if (medal[i] == ' ' || medal[i] == '#' || medal[i] == ',')
78                 {
79                         medal[i] = '_';
80                 }
81         }
82         
83         debug(("Attempting to post medal 'MBS_%s'\n", medal));
84         
85         TCPsocket socket = SDLNet_TCP_Open(&ip);
86         
87         if (!socket)
88         {
89                 printf("ERROR: SDLNet_TCP_Open: %s\n", SDLNet_GetError());
90                 return 0;
91         }
92         
93         char *in = new char[1024];
94         char out[1024];
95         
96         snprintf(out, sizeof out, "GET /addMedal/%s/MBS_%s HTTP/1.1\nHost: %s\nUser-Agent:BWMBS%.2f-%d\n\n", privateKey, medal, MEDAL_SERVER_HOST, VERSION, RELEASE);
97         
98         //printf("%s\n", out);
99         
100         int len = strlen(out) + 1;
101         
102         if (SDLNet_TCP_Send(socket, (void*)out, len) < len)
103         {
104                 printf("SDLNet_TCP_Send: %s\n", SDLNet_GetError());
105                 printf("Disconnected\n");
106                 SDLNet_TCP_Close(socket);
107                 close();
108                 return 0;
109         }
110         
111         SDLNet_TCP_Recv(socket, in, 512);
112         
113         //printf("%s\n", in);
114         
115         int response = 0;
116         char *token = strtok_r(in, "\n", &store);
117         
118         while (token != NULL)
119         {
120                 if (strstr(token, "MEDAL_RESPONSE"))
121                 {
122                         sscanf(token, "%*s %d %[^\n\r]", &response, message);
123                         
124                         if (response == 4)
125                         {
126                                 strncpy(rubyMessage, message, sizeof rubyMessage);
127                                 gotRuby = true;
128                         }
129                         else
130                         {
131                                 break;
132                         }
133                 }
134                 
135                 token = strtok_r(NULL, "\n", &store);
136         }
137         
138         debug(("MedalServer Response: %d '%s'\n", response, message))
139         
140         delete in;
141         
142         SDLNet_TCP_Close(socket);
143         
144         return response;
145 }
146
147 const char *MedalServer::getMessage()
148 {
149         return message;
150 }
151
152 bool MedalServer::hasRuby()
153 {
154         return gotRuby;
155 }
156
157 const char *MedalServer::getRubyMessage()
158 {
159         gotRuby = false;
160         
161         return rubyMessage;
162 }
163
164 void MedalServer::close()
165 {
166         connected = false;
167         
168         SDL_DestroyMutex(lock);
169 }