1.1 --- a/extensions/clientprefs/cookie.cpp Mon May 12 02:05:57 2008 +0000
1.2 +++ b/extensions/clientprefs/cookie.cpp Mon May 12 08:06:47 2008 +0000
1.3 @@ -30,6 +30,7 @@
1.4 */
1.5
1.6 #include "cookie.h"
1.7 +#include "menus.h"
1.8 #include "query.h"
1.9
1.10 CookieManager g_CookieManager;
1.11 @@ -42,7 +43,8 @@
1.12 statsLoaded[i] = false;
1.13 }
1.14
1.15 - cookiesLoadedForward = NULL;
1.16 + cookieDataLoadedForward = NULL;
1.17 + clientMenu = NULL;
1.18 }
1.19 CookieManager::~CookieManager(){}
1.20
1.21 @@ -97,26 +99,45 @@
1.22 }
1.23
1.24
1.25 -Cookie *CookieManager::CreateCookie(const char *name, const char *description)
1.26 +Cookie *CookieManager::CreateCookie(const char *name, const char *description, CookieAccess access)
1.27 {
1.28 Cookie *pCookie = FindCookie(name);
1.29
1.30 /* Check if cookie already exists */
1.31 if (pCookie != NULL)
1.32 {
1.33 + /* Update data fields to the provided values */
1.34 + strncpy(pCookie->description, description, MAX_DESC_LENGTH);
1.35 + pCookie->description[MAX_DESC_LENGTH-1] = '\0';
1.36 +
1.37 + pCookie->access = access;
1.38 +
1.39 return pCookie;
1.40 }
1.41
1.42 /* First time cookie - Create from scratch */
1.43 - pCookie = new Cookie(name, description);
1.44 + pCookie = new Cookie(name, description, access);
1.45 cookieTrie.insert(name, pCookie);
1.46 cookieList.push_back(pCookie);
1.47
1.48 + char quotedname[2 * MAX_NAME_LENGTH + 1];
1.49 + char quoteddesc[2 * MAX_DESC_LENGTH + 1];
1.50 +
1.51 + g_ClientPrefs.Database->QuoteString(pCookie->name, quotedname, sizeof(quotedname), NULL);
1.52 + g_ClientPrefs.Database->QuoteString(pCookie->description, quoteddesc, sizeof(quoteddesc), NULL);
1.53
1.54 - /* Attempt to load cookie from the db and get its ID num */
1.55 - g_ClientPrefs.Query_InsertCookie->BindParamString(0, name, true);
1.56 - g_ClientPrefs.Query_InsertCookie->BindParamString(1, description, true);
1.57 - TQueryOp *op = new TQueryOp(g_ClientPrefs.Database, g_ClientPrefs.Query_InsertCookie, Query_InsertCookie, pCookie);
1.58 + /* Attempt to insert cookie into the db and get its ID num */
1.59 + char query[300];
1.60 + if (driver == DRIVER_SQLITE)
1.61 + {
1.62 + UTIL_Format(query, sizeof(query), "INSERT OR IGNORE INTO sm_cookies(name, description, access) VALUES('%s', '%s', %i)", quotedname, quoteddesc, access);
1.63 + }
1.64 + else
1.65 + {
1.66 + UTIL_Format(query, sizeof(query), "INSERT IGNORE INTO sm_cookies(name, description, access) VALUES('%s', '%s', %i)", quotedname, quoteddesc, access);
1.67 + }
1.68 +
1.69 + TQueryOp *op = new TQueryOp(g_ClientPrefs.Database, query, Query_InsertCookie, pCookie);
1.70 dbi->AddToThreadQueue(op, PrioQueue_Normal);
1.71
1.72 return pCookie;
1.73 @@ -177,8 +198,10 @@
1.74 {
1.75 connected[client] = true;
1.76
1.77 - g_ClientPrefs.Query_SelectData->BindParamString(0, authstring, true);
1.78 - TQueryOp *op = new TQueryOp(g_ClientPrefs.Database, g_ClientPrefs.Query_SelectData, Query_SelectData, client);
1.79 + char query[300];
1.80 + /* Assume that the authstring doesn't need to be quoted */
1.81 + 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.82 + TQueryOp *op = new TQueryOp(g_ClientPrefs.Database, query, Query_SelectData, client);
1.83 dbi->AddToThreadQueue(op, PrioQueue_Normal);
1.84 }
1.85
1.86 @@ -220,18 +243,20 @@
1.87 return;
1.88 }
1.89
1.90 - g_ClientPrefs.Query_InsertData->BindParamString(0, player->GetAuthString(), true);
1.91 - g_ClientPrefs.Query_InsertData->BindParamInt(1, dbId, false);
1.92 - g_ClientPrefs.Query_InsertData->BindParamString(2, current->value, true);
1.93 - g_ClientPrefs.Query_InsertData->BindParamInt(3, time(NULL), false);
1.94 + char quotedvalue[2 * MAX_VALUE_LENGTH + 1];
1.95 + g_ClientPrefs.Database->QuoteString(current->value, quotedvalue, sizeof(quotedvalue), NULL);
1.96
1.97 - if (driver == DRIVER_MYSQL)
1.98 + char query[300];
1.99 + if (driver == DRIVER_SQLITE)
1.100 {
1.101 - g_ClientPrefs.Query_InsertData->BindParamString(4, current->value, true);
1.102 - g_ClientPrefs.Query_InsertData->BindParamInt(5, time(NULL), false);
1.103 + 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.104 + }
1.105 + else
1.106 + {
1.107 + 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.108 }
1.109
1.110 - TQueryOp *op = new TQueryOp(g_ClientPrefs.Database, g_ClientPrefs.Query_InsertData, Query_InsertData, client);
1.111 + TQueryOp *op = new TQueryOp(g_ClientPrefs.Database, query, Query_InsertData, client);
1.112 dbi->AddToThreadQueue(op, PrioQueue_Normal);
1.113
1.114 current->parent->data[client] = NULL;
1.115 @@ -241,7 +266,7 @@
1.116 }
1.117 }
1.118
1.119 -void CookieManager::ClientConnectCallback(int client, IPreparedQuery *data)
1.120 +void CookieManager::ClientConnectCallback(int client, IQuery *data)
1.121 {
1.122 IResultSet *results = data->GetResultSet();
1.123 if (results == NULL)
1.124 @@ -274,7 +299,10 @@
1.125 const char *desc;
1.126 row->GetString(2, &desc, NULL);
1.127
1.128 - parent = CreateCookie(name, desc);
1.129 + CookieAccess access = CookieAccess_Public;
1.130 + row->GetInt(3, (int *)&access);
1.131 +
1.132 + parent = CreateCookie(name, desc, access);
1.133 cookieTrie.insert(name, parent);
1.134 cookieList.push_back(parent);
1.135 }
1.136 @@ -286,15 +314,47 @@
1.137
1.138 } while (results->MoreRows());
1.139
1.140 +
1.141 statsLoaded[client] = true;
1.142
1.143 - cookiesLoadedForward->PushCell(client);
1.144 - cookiesLoadedForward->Execute(NULL);
1.145 + cookieDataLoadedForward->PushCell(client);
1.146 + cookieDataLoadedForward->Execute(NULL);
1.147 }
1.148
1.149 void CookieManager::InsertCookieCallback(Cookie *pCookie, int dbId)
1.150 {
1.151 - pCookie->dbid = dbId;
1.152 + if (dbId > 0)
1.153 + {
1.154 + pCookie->dbid = dbId;
1.155 + return;
1.156 + }
1.157 +
1.158 + char quotedname[2 * MAX_NAME_LENGTH + 1];
1.159 + g_ClientPrefs.Database->QuoteString(pCookie->name, quotedname, sizeof(quotedname), NULL);
1.160 +
1.161 + char query[300];
1.162 + UTIL_Format(query, sizeof(query), "SELECT id FROM sm_cookies WHERE name='%s'", quotedname);
1.163 +
1.164 + TQueryOp *op = new TQueryOp(g_ClientPrefs.Database, query, Query_SelectId, pCookie);
1.165 + dbi->AddToThreadQueue(op, PrioQueue_Normal);
1.166 +}
1.167 +
1.168 +void CookieManager::SelectIdCallback(Cookie *pCookie, IQuery *data)
1.169 +{
1.170 + IResultSet *results = data->GetResultSet();
1.171 + if (results == NULL)
1.172 + {
1.173 + return;
1.174 + }
1.175 +
1.176 + IResultRow *row = results->FetchRow();
1.177 +
1.178 + if (row == NULL)
1.179 + {
1.180 + return;
1.181 + }
1.182 +
1.183 + row->GetInt(0, &pCookie->dbid);
1.184 }
1.185
1.186 bool CookieManager::AreClientCookiesCached(int client)
1.187 @@ -302,3 +362,51 @@
1.188 return statsLoaded[client];
1.189 }
1.190
1.191 +void CookieManager::OnPluginDestroyed(IPlugin *plugin)
1.192 +{
1.193 + SourceHook::List<char *> *pList;
1.194 +
1.195 + if (plugin->GetProperty("SettingsMenuItems", (void **)&pList, true))
1.196 + {
1.197 + SourceHook::List<char *>::iterator p_iter = pList->begin();
1.198 + char *name;
1.199 +
1.200 + while (p_iter != pList->end())
1.201 + {
1.202 + name = (char *)*p_iter;
1.203 +
1.204 + p_iter = pList->erase(p_iter); //remove from this plugins list
1.205 +
1.206 + ItemDrawInfo draw;
1.207 +
1.208 + for (unsigned int i=0; i<clientMenu->GetItemCount(); i++)
1.209 + {
1.210 + const char *info = clientMenu->GetItemInfo(i, &draw);
1.211 +
1.212 + if (info == NULL)
1.213 + {
1.214 + continue;
1.215 + }
1.216 +
1.217 + if (strcmp(draw.display, name) == 0)
1.218 + {
1.219 + ItemDrawInfo draw;
1.220 + const char *info = clientMenu->GetItemInfo(i, &draw);
1.221 + AutoMenuData *data = (AutoMenuData *)strtol(info, NULL, 16);
1.222 +
1.223 + if (data->handler->forward != NULL)
1.224 + {
1.225 + forwards->ReleaseForward(data->handler->forward);
1.226 + }
1.227 + delete data->handler;
1.228 + delete data;
1.229 +
1.230 + clientMenu->RemoveItem(i);
1.231 + break;
1.232 + }
1.233 + }
1.234 +
1.235 + delete name;
1.236 + }
1.237 + }
1.238 +}