]> git.mxchange.org Git - quix0rs-blobwars.git/blob - src/CMedalServer.cpp
Update copyrights to 2011.
[quix0rs-blobwars.git] / src / CMedalServer.cpp
1 /*
2 Copyright (C) 2010-2011 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/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         strlcpy(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         strlcpy(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 out[1024];
94         
95         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);
96         
97         //printf("%s\n", out);
98         
99         int len = strlen(out) + 1;
100         
101         if (SDLNet_TCP_Send(socket, (void*)out, len) < len)
102         {
103                 printf("SDLNet_TCP_Send: %s\n", SDLNet_GetError());
104                 printf("Disconnected\n");
105                 SDLNet_TCP_Close(socket);
106                 close();
107                 return 0;
108         }
109         
110         char *in = new char[1024];
111
112         SDLNet_TCP_Recv(socket, in, 512);
113         
114         //printf("%s\n", in);
115         
116         int response = 0;
117         char *token = strtok_r(in, "\n", &store);
118         
119         while (token != NULL)
120         {
121                 if (strstr(token, "MEDAL_RESPONSE"))
122                 {
123                         sscanf(token, "%*s %d %[^\n\r]", &response, message);
124                         
125                         if (response == 4)
126                         {
127                                 strlcpy(rubyMessage, message, sizeof rubyMessage);
128                                 gotRuby = true;
129                         }
130                         else
131                         {
132                                 break;
133                         }
134                 }
135                 
136                 token = strtok_r(NULL, "\n", &store);
137         }
138         
139         debug(("MedalServer Response: %d '%s'\n", response, message))
140         
141         delete[] in;
142         
143         SDLNet_TCP_Close(socket);
144         
145         return response;
146 }
147
148 const char *MedalServer::getMessage()
149 {
150         return message;
151 }
152
153 bool MedalServer::hasRuby()
154 {
155         return gotRuby;
156 }
157
158 const char *MedalServer::getRubyMessage()
159 {
160         gotRuby = false;
161         
162         return rubyMessage;
163 }
164
165 void MedalServer::close()
166 {
167         connected = false;
168         
169         SDL_DestroyMutex(lock);
170 }