· 8 years ago · Apr 15, 2018, 09:32 PM
1#pragma semicolon 1
2
3#include <sourcemod>
4#include <sdktools>
5#include <cstrike>
6
7#include <autoexecconfig>
8
9#undef REQUIRE_PLUGIN
10#undef REQUIRE_EXTENSIONS
11#tryinclude <sourcetvmanager>
12
13#pragma newdecls required
14
15// sql stuff
16#define CNAME "Chat Logs"
17Database g_dDB = null;
18bool bLogMessage = true;
19bool bDebug = true;
20
21ConVar g_cEnable = null;
22ConVar g_cSay = null;
23ConVar g_cChat = null;
24ConVar g_cCsay = null;
25ConVar g_cTsay = null;
26ConVar g_cMsay = null;
27ConVar g_cHsay = null;
28
29bool g_bSourceTV = false;
30
31public Plugin myinfo =
32{
33 name = "SQL ChatLogs",
34 author = "Bara (Credits: McFlurry, Keith Warren (Drixevel)",
35 description = "",
36 version = "2.0",
37 url = "github.com/Bara"
38}
39
40public void OnPluginStart()
41{
42 AutoExecConfig_SetCreateDirectory(true);
43 AutoExecConfig_SetCreateFile(true);
44 AutoExecConfig_SetFile("plugin.chatlogs");
45 g_cEnable = AutoExecConfig_CreateConVar("sql_chatlogs_enable", "1", "Enable Chat Logging", _, true, 0.0, true, 1.0);
46 g_cSay = AutoExecConfig_CreateConVar("sql_chatlogs_sm_say", "1", "Log sm_say in chat log?", _, true, 0.0, true, 1.0);
47 g_cChat = AutoExecConfig_CreateConVar("sql_chatlogs_sm_chat", "1", "Log sm_chat in chat log?", _, true, 0.0, true, 1.0);
48 g_cCsay = AutoExecConfig_CreateConVar("sql_chatlogs_sm_csay", "1", "Log sm_csay in chat log?", _, true, 0.0, true, 1.0);
49 g_cTsay = AutoExecConfig_CreateConVar("sql_chatlogs_sm_tsay", "1", "Log sm_tsay in chat log?", _, true, 0.0, true, 1.0);
50 g_cMsay = AutoExecConfig_CreateConVar("sql_chatlogs_sm_msay", "1", "Log sm_msay in chat log?", _, true, 0.0, true, 1.0);
51 g_cHsay = AutoExecConfig_CreateConVar("sql_chatlogs_sm_hsay", "1", "Log sm_hsay in chat log?", _, true, 0.0, true, 1.0);
52 AutoExecConfig_ExecuteFile();
53 AutoExecConfig_CleanFile();
54
55 AddCommandListener(Command_Say, "sm_say");
56 AddCommandListener(Command_CSay, "sm_csay");
57 AddCommandListener(Command_TSay, "sm_tsay");
58 AddCommandListener(Command_MSay, "sm_msay");
59 AddCommandListener(Command_HSay, "sm_hsay");
60
61 SQL_TConnect(sqlConnect, "chatlogs");
62
63 g_bSourceTV = LibraryExists("sourcetvmanager");
64}
65
66public void OnLibraryAdded(const char[] name)
67{
68 if (StrEqual(name, "sourcetvmanager"))
69 {
70 g_bSourceTV = true;
71 }
72}
73
74public void OnLibraryRemoved(const char[] name)
75{
76 if (StrEqual(name, "sourcetvmanager"))
77 {
78 g_bSourceTV = false;
79 }
80}
81
82public void sqlConnect(Handle owner, Handle hndl, const char[] error, any data)
83{
84 if(hndl == null)
85 {
86 SetFailState("[%s] (sqlConnect) Can't connect to mysql", CNAME);
87 return;
88 }
89
90 g_dDB = view_as<Database>(CloneHandle(hndl));
91
92 char sQuery[1024];
93 Format(sQuery, sizeof(sQuery), "CREATE TABLE IF NOT EXISTS `chat_logs` (`id` INT NOT NULL AUTO_INCREMENT, `time` INT NOT NULL, `ip` varchar(18) COLLATE utf8mb4_unicode_ci NOT NULL, `port` int(6) NOT NULL, `map` varchar(24) COLLATE utf8mb4_unicode_ci NOT NULL, `command` varchar(24) COLLATE utf8mb4_unicode_ci NOT NULL, `name` varchar(256) COLLATE utf8mb4_unicode_ci NOT NULL, `communityid` varchar(24) COLLATE utf8mb4_unicode_ci NOT NULL, `alive` tinyint(1) COLLATE utf8mb4_unicode_ci NOT NULL, `team` varchar(12) COLLATE utf8mb4_unicode_ci NOT NULL, `text` varchar(256) COLLATE utf8mb4_unicode_ci NOT NULL, PRIMARY KEY (`id`), `tick` int(12) DEFAULT NULL, KEY `communityid` (`communityid`), KEY `text` (`text`), KEY `communityid_2` (`communityid`,`text`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;");
94
95 if(bLogMessage && bDebug)
96 LogMessage(sQuery);
97
98 g_dDB.Query(sqlCreateTable, sQuery);
99}
100
101public void sqlCreateTable(Database db, DBResultSet results, const char[] error, any data)
102{
103 if(db == null || strlen(error) > 0)
104 {
105 SetFailState("[%s] (sqlCreateTable) Fail at Query: %s", CNAME, error);
106 return;
107 }
108}
109
110public void OnClientSayCommand_Post(int client, const char[] command, const char[] sArgs)
111{
112 if (!g_cEnable.BoolValue || !g_cChat.BoolValue)
113 {
114 return;
115 }
116
117 if (!IsClientValid(client, true))
118 {
119 return;
120 }
121
122 if (strlen(sArgs) == 0)
123 {
124 return;
125 }
126
127 if (sArgs[0] == '@')
128 {
129 if (!CheckCommandAccess(client, "sm_say", ADMFLAG_CHAT))
130 {
131 return;
132 }
133
134 addToSQL(command, client, sArgs[1], true);
135 return;
136 }
137
138 addToSQL(command, client, sArgs);
139}
140
141public Action Command_Say(int client, const char[] command, int args)
142{
143 if(!g_cEnable.BoolValue || !g_cSay.BoolValue)
144 {
145 return;
146 }
147
148 if (!IsClientValid(client, true))
149 {
150 return;
151 }
152
153 char Chat[256];
154 GetCmdArgString(Chat, sizeof(Chat));
155
156 if (strlen(Chat) == 0)
157 {
158 return;
159 }
160
161 addToSQL(command, client, Chat, true);
162}
163
164public Action Command_CSay(int client, const char[] command, int args)
165{
166 if(!g_cEnable.BoolValue || !g_cCsay.BoolValue)
167 {
168 return;
169 }
170
171 if (!IsClientValid(client, true))
172 {
173 return;
174 }
175
176 char Chat[256];
177 GetCmdArgString(Chat, sizeof(Chat));
178
179 if (strlen(Chat) == 0)
180 {
181 return;
182 }
183
184 addToSQL(command, client, Chat, true);
185}
186
187public Action Command_TSay(int client, const char[] command, int args)
188{
189 if(!g_cEnable.BoolValue || !g_cTsay.BoolValue)
190 {
191 return;
192 }
193
194 if (!IsClientValid(client, true))
195 {
196 return;
197 }
198
199 char Chat[256];
200 GetCmdArgString(Chat, sizeof(Chat));
201
202 if (strlen(Chat) == 0)
203 {
204 return;
205 }
206
207 addToSQL(command, client, Chat, true);
208}
209
210public Action Command_MSay(int client, const char[] command, int args)
211{
212 if(!g_cEnable.BoolValue || !g_cMsay.BoolValue)
213 {
214 return;
215 }
216
217 if (!IsClientValid(client, true))
218 {
219 return;
220 }
221
222 char Chat[256];
223 GetCmdArgString(Chat, sizeof(Chat));
224
225 if (strlen(Chat) == 0)
226 {
227 return;
228 }
229
230 if (client == 0)
231 {
232 return;
233 }
234
235 addToSQL(command, client, Chat, true);
236}
237
238public Action Command_HSay(int client, const char[] command, int args)
239{
240 if(!g_cEnable.BoolValue || !g_cHsay.BoolValue)
241 {
242 return;
243 }
244
245 if (!IsClientValid(client, true))
246 {
247 return;
248 }
249
250 char Chat[256];
251 GetCmdArgString(Chat, sizeof(Chat));
252
253 if (strlen(Chat) == 0)
254 {
255 return;
256 }
257
258 addToSQL(command, client, Chat, true);
259}
260
261void addToSQL(const char[] command, int client, const char[] text, bool adminText = false)
262{
263 char sTeam[12], sAuth[18];
264
265 if (client > 0 && client <= MaxClients)
266 {
267 GetClientAuthId(client, AuthId_SteamID64, sAuth, sizeof(sAuth));
268
269 int team = GetClientTeam(client);
270
271 if (team == CS_TEAM_T)
272 {
273 Format(sTeam, sizeof(sTeam), "t");
274 }
275 else if (team == CS_TEAM_CT)
276 {
277 Format(sTeam, sizeof(sTeam), "ct");
278 }
279 else
280 {
281 Format(sTeam, sizeof(sTeam), "spec");
282 }
283
284 if (adminText)
285 {
286 Format(sTeam, sizeof(sTeam), "admin");
287 }
288 }
289 else
290 {
291 Format(sTeam, sizeof(sTeam), "console");
292 }
293
294 char sMap[24];
295 GetCurrentMap(sMap, sizeof(sMap));
296
297 int iPort = GetConVarInt(FindConVar("hostport"));
298
299 char sIP[18];
300 int ips[4];
301 int iIP = GetConVarInt(FindConVar("hostip"));
302 ips[0] = (iIP >> 24) & 0x000000FF;
303 ips[1] = (iIP >> 16) & 0x000000FF;
304 ips[2] = (iIP >> 8) & 0x000000FF;
305 ips[3] = iIP & 0x000000FF;
306 Format(sIP, sizeof(sIP), "%d.%d.%d.%d", ips[0], ips[1], ips[2], ips[3]);
307
308 int iTick = 0;
309
310 if (g_bSourceTV)
311 {
312 iTick = SourceTV_GetBroadcastTick();
313 }
314
315 char sQuery[1024], sQueryEscaped[1024];
316 // g_dDB.Format(sQuery, sizeof(sQuery), "INSERT INTO `chat_logs` (`time`, `tick`, `ip`, `port`, `map`, `command`, `name`, `communityid`, `alive`, `team`, `text`) VALUES (UNIX_TIMESTAMP(), %d, \"%s\", %d, \"%s\", \"%s\", \"%N\", \"%s\", %d, \"%s\", \"%s\")", iTick, sIP, iPort, sMap, command, client, sAuth, IsPlayerAlive(client), sTeam, text);
317 Format(sQuery, sizeof(sQuery), "INSERT INTO `chat_logs` (`time`, `tick`, `ip`, `port`, `map`, `command`, `name`, `communityid`, `alive`, `team`, `text`) VALUES (UNIX_TIMESTAMP(), %d, \"%s\", %d, \"%s\", \"%s\", \"%N\", \"%s\", %d, \"%s\", \"%s\")", iTick, sIP, iPort, sMap, command, client, sAuth, IsPlayerAlive(client), sTeam, text);
318 g_dDB.Escape(sQuery, sQueryEscaped, sizeof(sQueryEscaped));
319
320 if(bLogMessage && bDebug)
321 LogMessage(sQuery);
322
323 g_dDB.Query(sqlInsert, sQuery);
324}
325
326public void sqlInsert(Database db, DBResultSet results, const char[] error, any data)
327{
328 if(db == null || strlen(error) > 0)
329 {
330 SetFailState("[%s] (sqlInsert) Fail at Query: %s", CNAME, error);
331 return;
332 }
333 delete results;
334}
335
336stock bool IsClientValid(int client, bool bots = false)
337{
338 if (client > 0 && client <= MaxClients)
339 {
340 if(IsClientInGame(client) && (bots || (!IsFakeClient(client)) && !IsClientSourceTV(client)))
341 {
342 return true;
343 }
344 }
345
346 return false;
347}