extensions/clientprefs/cookie.h
author Nicholas Hastings <skamonkey@gmail.com>
Thu Jul 04 11:28:32 2013 -0400 (2013-07-04)
changeset 3931 576fa42454dc
parent 3804 90159516acd3
child 4029 cf8bdfcf6503
permissions -rw-r--r--
Fixed build for real.
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod Client Preferences Extension
     5  * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or modify it under
     9  * the terms of the GNU General Public License, version 3.0, as published by the
    10  * Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful, but WITHOUT
    13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    15  * details.
    16  *
    17  * You should have received a copy of the GNU General Public License along with
    18  * this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to link the
    21  * code of this program (as well as its derivative works) to "Half-Life 2," the
    22  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    23  * by the Valve Corporation.  You must obey the GNU General Public License in
    24  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    25  * this exception to all derivative works.  AlliedModders LLC defines further
    26  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    27  * or <http://www.sourcemod.net/license.php>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #ifndef _INCLUDE_SOURCEMOD_CLIENTPREFS_COOKIE_H_
    33 #define _INCLUDE_SOURCEMOD_CLIENTPREFS_COOKIE_H_
    34 
    35 #include "extension.h"
    36 #include "sh_list.h"
    37 #include "sm_trie_tpl.h"
    38 
    39 
    40 #define MAX_NAME_LENGTH 30
    41 #define MAX_DESC_LENGTH 255
    42 #define MAX_VALUE_LENGTH 100
    43 
    44 enum CookieAccess
    45 {
    46 	CookieAccess_Public,			/**< Visible and Changeable by users */
    47 	CookieAccess_Protected,			/**< Read only to users */
    48 	CookieAccess_Private,			/**< Completely hidden cookie */
    49 };
    50 
    51 struct Cookie;
    52 
    53 struct CookieData
    54 {
    55 	CookieData(const char *value)
    56 	{
    57 		UTIL_strncpy(this->value, value, MAX_VALUE_LENGTH);
    58 	}
    59 
    60 	char value[MAX_VALUE_LENGTH+1];
    61 	bool changed;
    62 	time_t timestamp;
    63 	Cookie *parent;
    64 };
    65 
    66 struct Cookie
    67 {
    68 	Cookie(const char *name, const char *description, CookieAccess access)
    69 	{
    70 		UTIL_strncpy(this->name, name, MAX_NAME_LENGTH);
    71 		UTIL_strncpy(this->description, description, MAX_DESC_LENGTH);
    72 
    73 		this->access = access;
    74 
    75 		dbid = -1;
    76 
    77 		for (int i=0; i<=MAXCLIENTS; i++)
    78 		{
    79 			data[i] = NULL;
    80 		}
    81 
    82 		shouldDelete = false;
    83 		usedInQuery = 0;
    84 	}
    85 
    86 	~Cookie()
    87 	{
    88 		for (int i=0; i<=MAXCLIENTS; i++)
    89 		{
    90 			if (data[i] != NULL)
    91 			{
    92 				delete data[i];
    93 			}
    94 		}
    95 	}
    96 
    97 	char name[MAX_NAME_LENGTH+1];
    98 	char description[MAX_DESC_LENGTH+1];
    99 	int dbid;
   100 	CookieData *data[MAXCLIENTS+1];
   101 	CookieAccess access;
   102 
   103 	/* Reference counting stuff */
   104 	bool shouldDelete;
   105 	int usedInQuery;
   106 };
   107 
   108 class CookieManager : public IClientListener, public IPluginsListener
   109 {
   110 public:
   111 	CookieManager();
   112 	~CookieManager();
   113 
   114 	void OnClientAuthorized(int client, const char *authstring);
   115 	void OnClientDisconnecting(int client);
   116 	
   117 	bool GetCookieValue(Cookie *pCookie, int client, char **value);
   118 	bool SetCookieValue(Cookie *pCookie, int client, const char *value);
   119 	bool GetCookieTime(Cookie *pCookie, int client, time_t *value);
   120 
   121 	void Unload();
   122 
   123 	void ClientConnectCallback(int serial, IQuery *data);
   124 	void InsertCookieCallback(Cookie *pCookie, int dbId);
   125 	void SelectIdCallback(Cookie *pCookie, IQuery *data);
   126 
   127 	Cookie *FindCookie(const char *name);
   128 	Cookie *CreateCookie(const char *name, const char *description, CookieAccess access);
   129 
   130 	bool AreClientCookiesCached(int client);
   131 
   132 	void OnPluginDestroyed(IPlugin *plugin);
   133 	
   134 	bool AreClientCookiesPening(int client);
   135 
   136 public:
   137 	IForward *cookieDataLoadedForward;
   138 	SourceHook::List<Cookie *> cookieList;
   139 	IBaseMenu *clientMenu;
   140 
   141 private:
   142 	KTrie<Cookie *> cookieTrie;
   143 	SourceHook::List<CookieData *> clientData[MAXCLIENTS+1];
   144 
   145 	bool connected[MAXCLIENTS+1];
   146 	bool statsLoaded[MAXCLIENTS+1];
   147 	bool statsPending[MAXCLIENTS+1];
   148 };
   149 
   150 extern CookieManager g_CookieManager;
   151 
   152 #endif // _INCLUDE_SOURCEMOD_CLIENTPREFS_COOKIE_H_