extensions/clientprefs/cookie.h
author Matt Woodrow <pred@alliedmods.net>
Sun Jul 06 03:30:06 2008 +0000 (2008-07-06)
changeset 2165 ca3757e676f6
parent 2048 2454ff4d6afb
child 2207 3a97a56ded62
permissions -rw-r--r--
Fixed requiring dependencies at the wrong time.
     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 		strncpy(this->value, value, MAX_VALUE_LENGTH);
    58 		this->value[MAX_VALUE_LENGTH-1] = '\0';
    59 	}
    60 
    61 	char value[MAX_VALUE_LENGTH];
    62 	bool changed;
    63 	Cookie *parent;
    64 };
    65 
    66 struct Cookie
    67 {
    68 	Cookie(const char *name, const char *description, CookieAccess access)
    69 	{
    70 		strncpy(this->name, name, MAX_NAME_LENGTH);
    71 		this->name[MAX_NAME_LENGTH-1] = '\0';
    72 		strncpy(this->description, description, MAX_DESC_LENGTH);
    73 		this->description[MAX_DESC_LENGTH-1] = '\0';
    74 
    75 		this->access = access;
    76 
    77 		dbid = -1;
    78 
    79 		for (int i=0; i<=MAXCLIENTS; i++)
    80 		{
    81 			data[i] = NULL;
    82 		}
    83 	}
    84 
    85 	~Cookie()
    86 	{
    87 		for (int i=0; i<=MAXCLIENTS; i++)
    88 		{
    89 			if (data[i] != NULL)
    90 			{
    91 				delete data[i];
    92 			}
    93 		}
    94 	}
    95 
    96 	char name[MAX_NAME_LENGTH];
    97 	char description[MAX_DESC_LENGTH];
    98 	int dbid;
    99 	CookieData *data[MAXCLIENTS+1];
   100 	CookieAccess access;
   101 };
   102 
   103 class CookieManager : public IClientListener, public IPluginsListener
   104 {
   105 public:
   106 	CookieManager();
   107 	~CookieManager();
   108 
   109 	void OnClientAuthorized(int client, const char *authstring);
   110 	void OnClientDisconnecting(int client);
   111 	
   112 	bool GetCookieValue(Cookie *pCookie, int client, char **value);
   113 	bool SetCookieValue(Cookie *pCookie, int client, char *value);
   114 
   115 	void Unload();
   116 
   117 	void ClientConnectCallback(int client, IQuery *data);
   118 	void InsertCookieCallback(Cookie *pCookie, int dbId);
   119 	void SelectIdCallback(Cookie *pCookie, IQuery *data);
   120 
   121 	Cookie *FindCookie(const char *name);
   122 	Cookie *CreateCookie(const char *name, const char *description, CookieAccess access);
   123 
   124 	bool AreClientCookiesCached(int client);
   125 
   126 	IForward *cookieDataLoadedForward;
   127 
   128 	SourceHook::List<Cookie *> cookieList;
   129 
   130 	IBaseMenu *clientMenu;
   131 
   132 	void OnPluginDestroyed(IPlugin *plugin);
   133 
   134 private:
   135 	KTrie<Cookie *> cookieTrie;
   136 	SourceHook::List<CookieData *> clientData[MAXCLIENTS];
   137 
   138 	bool connected[MAXCLIENTS+1];
   139 	bool statsLoaded[MAXCLIENTS+1];
   140 };
   141 
   142 extern CookieManager g_CookieManager;
   143 
   144 #endif // _INCLUDE_SOURCEMOD_CLIENTPREFS_COOKIE_H_