· 8 years ago · Jan 16, 2018, 11:00 PM
1/* Include File generated by Pawn Studio */
2
3#include <sourcemod>
4#include <sdktools>
5#include <sdkhooks>
6
7char g_szPassword[96]
8char g_DBConnectionOld[64]
9
10ConVar g_Cvar_SvPassword
11
12Database db = null;
13
14public Plugin myinfo =
15{
16 name = "Change Password",
17 author = "Whitesmith",
18 description = "",
19 version = "1.0.0",
20 url = ""
21};
22
23public void OnConfigsExecuted()
24{
25 if(g_DBConnectionOld[0] != EOS && strcmp(g_DBConnectionOld, "storage-local") != 0 && db != null)
26 {
27 delete db;
28 db = null;
29 }
30
31 if(db == null)
32 {
33 Database.Connect(SQLConnectCallback, "storage-local");
34 }
35
36 FormatEx(g_DBConnectionOld, sizeof(g_DBConnectionOld), "storage-local");
37}
38
39public void OnPluginStart()
40{
41 AddCommandListener(ChatListener, "say");
42 AddCommandListener(ChatListener, "say2");
43}
44
45public void SQLConnectCallback(Database database, const char[] error, any data)
46{
47 if (database == null)
48 {
49 LogError("Database failure: %s", error);
50 }
51 else
52 {
53 db = database;
54 char createQuery[10240];
55 char dbIdentifier[10];
56
57 int index = 0;
58
59 index += FormatEx(createQuery[index], sizeof(createQuery) - index, "CREATE TABLE IF NOT EXISTS CSGOservers (server varchar(32) NOT NULL PRIMARY KEY, password varchar(64) NOT NULL)")
60
61 db.Driver.GetIdentifier(dbIdentifier, sizeof(dbIdentifier));
62 bool mysql = StrEqual(dbIdentifier, "mysql");
63 if (mysql)
64 {
65 index += FormatEx(createQuery[index], sizeof(createQuery) - index, " ENGINE=InnoDB CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;");
66 }
67
68 db.Query(T_CreateTableCallback, createQuery, mysql, DBPrio_High);
69 }
70}
71
72public void T_CreateTableCallback(Database database, DBResultSet results, const char[] error, bool mysql)
73{
74 if (results == null)
75 {
76 LogError("[PUG-Password by Whitesmith] %s Create table failed! %s", (mysql ? "MySQL" : "SQLite"), error);
77
78 }
79 else
80 {
81 LogMessage("%s DB connection successful [PUG-Password by Whitesmith]", (mysql ? "MySQL" : "SQLite"));
82 Check_PUGPassword()
83 }
84}
85
86void Create_PUGinTable() {
87 char query[255];
88 FormatEx(query, sizeof(query), "INSERT INTO CSGOservers (server, password) VALUES('PUG','1346')");
89 DataPack pack = new DataPack();
90 pack.WriteString(query);
91 db.Query(T_PUGinTable, query, pack);
92}
93
94public void T_PUGinTable(Database database, DBResultSet results, const char[] error, DataPack pack)
95{
96 if (results == null)
97 {
98 pack.Reset();
99 char buffer[1024];
100 pack.ReadString(buffer, 1024);
101 LogError("Query failed: \"%s\" error: \"%s\"", buffer, error);
102 }
103 else
104 {
105 Check_PUGPassword()
106 LogMessage("[PUG-Password by Whitesmith] Inserted 'PUG' succesfully!");
107 }
108}
109
110void Check_PUGPassword() {
111 char query[255];
112 FormatEx(query, sizeof(query), "SELECT password FROM CSGOservers WHERE server = 'PUG'");
113 DataPack pack = new DataPack();
114 pack.WriteString(query);
115 db.Query(T_PUGPassword, query, pack);
116}
117
118public void T_PUGPassword(Database database, DBResultSet results, const char[] error, DataPack pack)
119{
120 if (results == null)
121 {
122 pack.Reset();
123 char buffer[1024];
124 pack.ReadString(buffer, 1024);
125 LogError("Update server failed! query: \"%s\" error: \"%s\"", buffer, error);
126 }
127 else if (results.RowCount == 0)
128 {
129 Create_PUGinTable()
130 LogMessage("[PUG-Password by Whitesmith] Attempting to insert 'PUG' in CSGOservers table!");
131 }
132 else
133 {
134 if(results.FetchRow()) {
135 results.FetchString(0, g_szPassword, 95);
136 LogMessage("%s is the password", g_szPassword)
137 Set_PUGPassword("1346")
138 if(!StrEqual(g_szPassword, "1346")) {
139 Set_curMatchPassword(g_szPassword)
140 }
141 }
142 }
143}
144
145void Set_PUGPassword(const char[] pw)
146{
147 LogMessage("Attempting to set %s as a password", pw)
148 char query[255];
149 FormatEx(query, sizeof(query), "UPDATE CSGOservers SET password = '%s' WHERE server = 'PUG'", pw);
150 DataPack pack = new DataPack();
151 pack.WriteString(query);
152 db.Query(T_UpdatePUGPassword, query, pack);
153}
154
155public void T_UpdatePUGPassword(Database database, DBResultSet results, const char[] error, DataPack pack)
156{
157 if (results == null)
158 {
159 pack.Reset();
160 char buffer[1024];
161 pack.ReadString(buffer, 1024);
162 LogError("Update server failed! query: \"%s\" error: \"%s\"", buffer, error);
163 }
164 CloseHandle(pack);
165}
166
167public void Set_curMatchPassword(const char[] pw)
168{
169
170 SetConVarInt(FindConVar("sv_hibernate_postgame_delay"), 0, false, false); // # of seconds to wait after final client leaves before hibernating.
171
172 for(int i = 1; i <= MaxClients; i++)
173 {
174 if(IsClientConnected(i))
175 {
176 LogAction(0, i, "Kicked by PUG-Password");
177 LogMessage("Should kick %N", i)
178 KickClient(i, "[PUG-PW by Whitesmith] Please use the password to join the server.");
179 }
180 }
181 Handle pack;
182 CreateDataTimer(1.0, delay, pack);
183 WritePackString(pack, pw);
184 ResetPack(pack);
185
186 return Plugin_Handled;
187}
188
189public Action delay(Handle timer, Handle pack)
190{
191 char buffer[34];
192 ReadPackString(pack, buffer, sizeof(buffer));
193
194 LogMessage("[PUG-Password by Whitesmith] - setting server password to \"%s\"", buffer);
195
196 SetConVarString(FindConVar("sv_password"), buffer);
197 LogMessage("[PUG-Password by Whitesmith] Password should be set now!");
198}
199
200public Action ChatListener(int client, const char[] command, int args)
201{
202 if (!GetAdminFlag(GetUserAdmin(client), Admin_Changemap)) {
203 return Plugin_Continue
204 }
205
206 char szChat[128], szStrings[2][64]
207 GetCmdArgString(szChat, sizeof(szChat));
208 StripQuotes(szChat);
209 ExplodeString(szChat, " ", szStrings, 2, 63);
210 PrintToChatAll("(%s) (%s) (%s)", szChat, szStrings[0], szStrings[1])
211 StripQuotes(szStrings[1])
212 if (StrEqual(szStrings[0], "!pugpw"))
213 {
214 if(strlen(szStrings[1]) > 3)
215 {
216 Set_PUGPassword(szStrings[1])
217 PrintToChat(client, "\x04[BOURAK-PUG] \x01The password for nextmap is: \x05%s", szStrings[1])
218 return Plugin_Handled;
219 }
220 else
221 {
222 PrintToChat(client, "\x04[BOURAK-PUG] \x01The password must be more than \x053 characters!")
223 return Plugin_Handled
224 }
225 }
226
227 return Plugin_Continue;
228}
229
230stock bool IsValidClient(int client)
231{
232 if (!(1 <= client <= MaxClients) || !IsClientInGame(client) || IsFakeClient(client) || IsClientSourceTV(client) || IsClientReplay(client))
233 {
234 return false;
235 }
236 return true;
237}