1.1 --- a/extensions/clientprefs/cookie.cpp Fri Jul 11 06:51:21 2008 +0000
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,413 +0,0 @@
1.4 -/**
1.5 - * vim: set ts=4 :
1.6 - * =============================================================================
1.7 - * SourceMod Client Preferences Extension
1.8 - * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
1.9 - * =============================================================================
1.10 - *
1.11 - * This program is free software; you can redistribute it and/or modify it under
1.12 - * the terms of the GNU General Public License, version 3.0, as published by the
1.13 - * Free Software Foundation.
1.14 - *
1.15 - * This program is distributed in the hope that it will be useful, but WITHOUT
1.16 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.17 - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.18 - * details.
1.19 - *
1.20 - * You should have received a copy of the GNU General Public License along with
1.21 - * this program. If not, see <http://www.gnu.org/licenses/>.
1.22 - *
1.23 - * As a special exception, AlliedModders LLC gives you permission to link the
1.24 - * code of this program (as well as its derivative works) to "Half-Life 2," the
1.25 - * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
1.26 - * by the Valve Corporation. You must obey the GNU General Public License in
1.27 - * all respects for all other code used. Additionally, AlliedModders LLC grants
1.28 - * this exception to all derivative works. AlliedModders LLC defines further
1.29 - * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
1.30 - * or <http://www.sourcemod.net/license.php>.
1.31 - *
1.32 - * Version: $Id$
1.33 - */
1.34 -
1.35 -#include "cookie.h"
1.36 -#include "menus.h"
1.37 -#include "query.h"
1.38 -
1.39 -CookieManager g_CookieManager;
1.40 -
1.41 -CookieManager::CookieManager()
1.42 -{
1.43 - for (int i=0; i<=MAXCLIENTS; i++)
1.44 - {
1.45 - connected[i] = false;
1.46 - statsLoaded[i] = false;
1.47 - }
1.48 -
1.49 - cookieDataLoadedForward = NULL;
1.50 - clientMenu = NULL;
1.51 -}
1.52 -CookieManager::~CookieManager(){}
1.53 -
1.54 -void CookieManager::Unload()
1.55 -{
1.56 - /* If clients are connected we should try save their data */
1.57 -
1.58 - int maxclients = playerhelpers->GetMaxClients();
1.59 -
1.60 - for (int i=1; i<=maxclients; i++)
1.61 - {
1.62 - if (connected[i])
1.63 - {
1.64 - OnClientDisconnecting(i);
1.65 - }
1.66 - }
1.67 -
1.68 - /* Find all cookies and delete them */
1.69 -
1.70 - SourceHook::List<Cookie *>::iterator _iter;
1.71 -
1.72 - Cookie *current;
1.73 -
1.74 - _iter = cookieList.begin();
1.75 -
1.76 - while (_iter != cookieList.end())
1.77 - {
1.78 - current = (Cookie *)*_iter;
1.79 -
1.80 - if (current == NULL)
1.81 - {
1.82 - _iter++;
1.83 - continue;
1.84 - }
1.85 -
1.86 - delete current;
1.87 -
1.88 - _iter = cookieList.erase(_iter);
1.89 - }
1.90 -}
1.91 -
1.92 -Cookie *CookieManager::FindCookie(const char *name)
1.93 -{
1.94 - Cookie **pCookie = cookieTrie.retrieve(name);
1.95 -
1.96 - if (pCookie == NULL)
1.97 - {
1.98 - return NULL;
1.99 - }
1.100 -
1.101 - return *pCookie;
1.102 -}
1.103 -
1.104 -
1.105 -Cookie *CookieManager::CreateCookie(const char *name, const char *description, CookieAccess access)
1.106 -{
1.107 - Cookie *pCookie = FindCookie(name);
1.108 -
1.109 - /* Check if cookie already exists */
1.110 - if (pCookie != NULL)
1.111 - {
1.112 - /* Update data fields to the provided values */
1.113 - strncpy(pCookie->description, description, MAX_DESC_LENGTH);
1.114 - pCookie->description[MAX_DESC_LENGTH-1] = '\0';
1.115 -
1.116 - pCookie->access = access;
1.117 -
1.118 - return pCookie;
1.119 - }
1.120 -
1.121 - /* First time cookie - Create from scratch */
1.122 - pCookie = new Cookie(name, description, access);
1.123 - cookieTrie.insert(name, pCookie);
1.124 - cookieList.push_back(pCookie);
1.125 -
1.126 - char quotedname[2 * MAX_NAME_LENGTH + 1];
1.127 - char quoteddesc[2 * MAX_DESC_LENGTH + 1];
1.128 -
1.129 - g_ClientPrefs.Database->QuoteString(pCookie->name, quotedname, sizeof(quotedname), NULL);
1.130 - g_ClientPrefs.Database->QuoteString(pCookie->description, quoteddesc, sizeof(quoteddesc), NULL);
1.131 -
1.132 - /* Attempt to insert cookie into the db and get its ID num */
1.133 - char query[300];
1.134 - if (driver == DRIVER_SQLITE)
1.135 - {
1.136 - UTIL_Format(query, sizeof(query), "INSERT OR IGNORE INTO sm_cookies(name, description, access) VALUES('%s', '%s', %i)", quotedname, quoteddesc, access);
1.137 - }
1.138 - else
1.139 - {
1.140 - UTIL_Format(query, sizeof(query), "INSERT IGNORE INTO sm_cookies(name, description, access) VALUES('%s', '%s', %i)", quotedname, quoteddesc, access);
1.141 - }
1.142 -
1.143 - TQueryOp *op = new TQueryOp(g_ClientPrefs.Database, query, Query_InsertCookie, pCookie);
1.144 - dbi->AddToThreadQueue(op, PrioQueue_Normal);
1.145 -
1.146 - return pCookie;
1.147 -}
1.148 -
1.149 -bool CookieManager::GetCookieValue(Cookie *pCookie, int client, char **value)
1.150 -{
1.151 - if (pCookie == NULL)
1.152 - {
1.153 - return false;
1.154 - }
1.155 -
1.156 - CookieData *data = pCookie->data[client];
1.157 -
1.158 - /* Check if a value has been set before */
1.159 - if (data == NULL)
1.160 - {
1.161 - data = new CookieData("");
1.162 - data->parent = pCookie;
1.163 - clientData[client].push_back(data);
1.164 - pCookie->data[client] = data;
1.165 - data->changed = true;
1.166 - }
1.167 -
1.168 - *value = &data->value[0];
1.169 -
1.170 - return true;
1.171 -}
1.172 -
1.173 -bool CookieManager::SetCookieValue(Cookie *pCookie, int client, char *value)
1.174 -{
1.175 - if (pCookie == NULL)
1.176 - {
1.177 - return false;
1.178 - }
1.179 -
1.180 - CookieData *data = pCookie->data[client];
1.181 -
1.182 - if (data == NULL)
1.183 - {
1.184 - data = new CookieData(value);
1.185 - data->parent = pCookie;
1.186 - clientData[client].push_back(data);
1.187 - pCookie->data[client] = data;
1.188 - }
1.189 - else
1.190 - {
1.191 - strncpy(data->value, value, MAX_VALUE_LENGTH);
1.192 - data->value[MAX_VALUE_LENGTH-1] = '\0';
1.193 - }
1.194 -
1.195 - data->changed = true;
1.196 -
1.197 - return true;
1.198 -}
1.199 -
1.200 -void CookieManager::OnClientAuthorized(int client, const char *authstring)
1.201 -{
1.202 - connected[client] = true;
1.203 -
1.204 - char query[300];
1.205 - /* Assume that the authstring doesn't need to be quoted */
1.206 - UTIL_Format(query, sizeof(query), "SELECT sm_cookies.name, sm_cookie_cache.value, sm_cookies.description, sm_cookies.access FROM sm_cookies JOIN sm_cookie_cache ON sm_cookies.id = sm_cookie_cache.cookie_id WHERE player = '%s'", authstring);
1.207 - TQueryOp *op = new TQueryOp(g_ClientPrefs.Database, query, Query_SelectData, client);
1.208 - dbi->AddToThreadQueue(op, PrioQueue_Normal);
1.209 -}
1.210 -
1.211 -void CookieManager::OnClientDisconnecting(int client)
1.212 -{
1.213 - connected[client] = false;
1.214 - statsLoaded[client] = false;
1.215 -
1.216 - SourceHook::List<CookieData *>::iterator _iter;
1.217 -
1.218 - CookieData *current;
1.219 -
1.220 - _iter = clientData[client].begin();
1.221 -
1.222 - while (_iter != clientData[client].end())
1.223 - {
1.224 - current = (CookieData *)*_iter;
1.225 -
1.226 - if (!current->changed)
1.227 - {
1.228 - current->parent->data[client] = NULL;
1.229 - delete current;
1.230 - _iter = clientData[client].erase(_iter);
1.231 - continue;
1.232 - }
1.233 -
1.234 - /* Save this cookie to the database */
1.235 - IGamePlayer *player = playerhelpers->GetGamePlayer(client);
1.236 -
1.237 - if (player == NULL)
1.238 - {
1.239 - /* panic! */
1.240 - return;
1.241 - }
1.242 -
1.243 - int dbId = current->parent->dbid;
1.244 -
1.245 - if (dbId == -1)
1.246 - {
1.247 - /* Insert/Find Query must be still running or failed. */
1.248 - return;
1.249 - }
1.250 -
1.251 - char quotedvalue[2 * MAX_VALUE_LENGTH + 1];
1.252 - g_ClientPrefs.Database->QuoteString(current->value, quotedvalue, sizeof(quotedvalue), NULL);
1.253 -
1.254 - char query[300];
1.255 - if (driver == DRIVER_SQLITE)
1.256 - {
1.257 - UTIL_Format(query, sizeof(query), "INSERT OR REPLACE INTO sm_cookie_cache(player,cookie_id, value, timestamp) VALUES('%s', %i, '%s', %i)", player->GetAuthString(), dbId, quotedvalue, time(NULL));
1.258 - }
1.259 - else
1.260 - {
1.261 - UTIL_Format(query, sizeof(query), "INSERT INTO sm_cookie_cache(player,cookie_id, value, timestamp) VALUES('%s', %i, '%s', %i) ON DUPLICATE KEY UPDATE value = '%s', timestamp = %i", player->GetAuthString(), dbId, quotedvalue, time(NULL), quotedvalue, time(NULL));
1.262 - }
1.263 -
1.264 - TQueryOp *op = new TQueryOp(g_ClientPrefs.Database, query, Query_InsertData, client);
1.265 - dbi->AddToThreadQueue(op, PrioQueue_Normal);
1.266 -
1.267 - current->parent->data[client] = NULL;
1.268 - delete current;
1.269 -
1.270 - _iter = clientData[client].erase(_iter);
1.271 - }
1.272 -}
1.273 -
1.274 -void CookieManager::ClientConnectCallback(int client, IQuery *data)
1.275 -{
1.276 - IResultSet *results = data->GetResultSet();
1.277 - if (results == NULL)
1.278 - {
1.279 - return;
1.280 - }
1.281 -
1.282 - do
1.283 - {
1.284 - IResultRow *row = results->FetchRow();
1.285 -
1.286 - if (row == NULL)
1.287 - {
1.288 - continue;
1.289 - }
1.290 -
1.291 - const char *name;
1.292 - row->GetString(0, &name, NULL);
1.293 -
1.294 - const char *value;
1.295 - row->GetString(1, &value, NULL);
1.296 -
1.297 - CookieData *pData = new CookieData(value);
1.298 - pData->changed = false;
1.299 -
1.300 - Cookie *parent = FindCookie(name);
1.301 -
1.302 - if (parent == NULL)
1.303 - {
1.304 - const char *desc;
1.305 - row->GetString(2, &desc, NULL);
1.306 -
1.307 - CookieAccess access = CookieAccess_Public;
1.308 - row->GetInt(3, (int *)&access);
1.309 -
1.310 - parent = CreateCookie(name, desc, access);
1.311 - cookieTrie.insert(name, parent);
1.312 - cookieList.push_back(parent);
1.313 - }
1.314 -
1.315 - pData->parent = parent;
1.316 - parent->data[client] = pData;
1.317 -
1.318 - clientData[client].push_back(pData);
1.319 -
1.320 - } while (results->MoreRows());
1.321 -
1.322 - statsLoaded[client] = true;
1.323 -
1.324 - cookieDataLoadedForward->PushCell(client);
1.325 - cookieDataLoadedForward->Execute(NULL);
1.326 -}
1.327 -
1.328 -void CookieManager::InsertCookieCallback(Cookie *pCookie, int dbId)
1.329 -{
1.330 - if (dbId > 0)
1.331 - {
1.332 - pCookie->dbid = dbId;
1.333 - return;
1.334 - }
1.335 -
1.336 - char quotedname[2 * MAX_NAME_LENGTH + 1];
1.337 - g_ClientPrefs.Database->QuoteString(pCookie->name, quotedname, sizeof(quotedname), NULL);
1.338 -
1.339 - char query[300];
1.340 - UTIL_Format(query, sizeof(query), "SELECT id FROM sm_cookies WHERE name='%s'", quotedname);
1.341 -
1.342 - TQueryOp *op = new TQueryOp(g_ClientPrefs.Database, query, Query_SelectId, pCookie);
1.343 - dbi->AddToThreadQueue(op, PrioQueue_Normal);
1.344 -}
1.345 -
1.346 -void CookieManager::SelectIdCallback(Cookie *pCookie, IQuery *data)
1.347 -{
1.348 - IResultSet *results = data->GetResultSet();
1.349 - if (results == NULL)
1.350 - {
1.351 - return;
1.352 - }
1.353 -
1.354 - IResultRow *row = results->FetchRow();
1.355 -
1.356 - if (row == NULL)
1.357 - {
1.358 - return;
1.359 - }
1.360 -
1.361 - row->GetInt(0, &pCookie->dbid);
1.362 -}
1.363 -
1.364 -bool CookieManager::AreClientCookiesCached(int client)
1.365 -{
1.366 - return statsLoaded[client];
1.367 -}
1.368 -
1.369 -void CookieManager::OnPluginDestroyed(IPlugin *plugin)
1.370 -{
1.371 - SourceHook::List<char *> *pList;
1.372 -
1.373 - if (plugin->GetProperty("SettingsMenuItems", (void **)&pList, true))
1.374 - {
1.375 - SourceHook::List<char *>::iterator p_iter = pList->begin();
1.376 - char *name;
1.377 -
1.378 - while (p_iter != pList->end())
1.379 - {
1.380 - name = (char *)*p_iter;
1.381 -
1.382 - p_iter = pList->erase(p_iter); //remove from this plugins list
1.383 -
1.384 - ItemDrawInfo draw;
1.385 -
1.386 - for (unsigned int i=0; i<clientMenu->GetItemCount(); i++)
1.387 - {
1.388 - const char *info = clientMenu->GetItemInfo(i, &draw);
1.389 -
1.390 - if (info == NULL)
1.391 - {
1.392 - continue;
1.393 - }
1.394 -
1.395 - if (strcmp(draw.display, name) == 0)
1.396 - {
1.397 - ItemDrawInfo draw;
1.398 - const char *info = clientMenu->GetItemInfo(i, &draw);
1.399 - AutoMenuData *data = (AutoMenuData *)strtol(info, NULL, 16);
1.400 -
1.401 - if (data->handler->forward != NULL)
1.402 - {
1.403 - forwards->ReleaseForward(data->handler->forward);
1.404 - }
1.405 - delete data->handler;
1.406 - delete data;
1.407 -
1.408 - clientMenu->RemoveItem(i);
1.409 - break;
1.410 - }
1.411 - }
1.412 -
1.413 - delete name;
1.414 - }
1.415 - }
1.416 -}