PHP Code:
#include <sourcemod>
new Handle:db = INVALID_HANDLE;
public OnPluginStart()
{
LoadTranslations("common.phrases")
SQL_TConnect(callback, "joinpunishments");
RegAdminCmd("sm_punish", command_punish, ADMFLAG_BAN);
new String:path[128];
BuildPath(Path_SM, path, sizeof(path), "logs/joinpunishments");
if(!DirExists(path))
{
CreateDirectory(path, 511);
PrintToServer("[JOINPUNISHMENTS] Directory created.");
}
BuildPath(Path_SM, path, sizeof(path), "logs/joinpunishments/punishments.txt");
if(!FileExists(path))
{
OpenFile(path, "a+");
PrintToServer("[JOINPUNISHMENTS] Log file created.");
}
}
public callback(Handle:owner, Handle:hndl, const String:error[], any:data)
{
db = hndl;
if(hndl != INVALID_HANDLE)
{
PrintToServer("JOINPUNISHMENTS connection successful.");
}
else
{
PrintToServer("JOINPUNISHMENTS connection error. ERROR: %s", error);
}
}
public Action command_punish(client, args)
{
if(args < 4)
{
PrintToChat(client, "[SM] Usage: sm_punish <steamid> <punishment, ban/gag/mute> <time> <reason>. The reason must be written with underscores, it wont read spaces(for example: mic_spam_and_chat_spam).");
return Plugin_Handled;
}
new String:arg_string[256];
new String:authid[128];
new String:punishment[128];
new String:time[128];
new String:reason[128];
int len, total_len;
GetCmdArgString(arg_string, sizeof(arg_string));
if ((len = BreakString(arg_string[total_len], authid, sizeof(authid))) != -1)
{
total_len += len;
}
if ((len = BreakString(arg_string[total_len], punishment, sizeof(punishment))) != -1)
{
total_len += len;
}
if ((len = BreakString(arg_string[total_len], time, sizeof(time))) != -1)
{
total_len += len;
}
if ((len = BreakString(arg_string[total_len], reason, sizeof(reason))) != -1)
{
total_len += len;
}
bool idValid = false;
if (!strncmp(authid, "STEAM_", 6) && authid[7] == ':')
idValid = true;
if (!idValid)
{
ReplyToCommand(client, "[SM] Invalid SteamID.");
return Plugin_Handled;
}
if(StrContains(punishment, "ban", false) == -1 && StrContains(punishment, "gag", false) == -1 && StrContains(punishment, "mute", false) == -1)
{
PrintToChat(client, "[SM] Invalid punishment. Only the following punishments can be used: ban/gag/mute");
return Plugin_Handled;
}
decl String:authid2[128];
decl String:punishment2[128];
decl String:time2[128];
decl String:reason2[128];
SQL_EscapeString(db, authid, authid2, sizeof(authid2));
SQL_EscapeString(db, punishment, punishment2, sizeof(punishment2));
SQL_EscapeString(db, time, time2, sizeof(time2));
SQL_EscapeString(db, reason, reason2, sizeof(reason2));
new String:query[200];
Format(query, 200, "INSERT INTO list (steamid, punishment, time, reason) VALUES ('%s', '%s', '%s', '%s')", authid2, punishment2, time2, reason2);
new Handle:SQLQuery = SQL_Query(db, query);
if(SQLQuery != INVALID_HANDLE)
{
PrintToChat(client, "[SM] User succesfully added to database. If you want to remove that user, please contact the owner.");
LogToFile("addons/sourcemod/logs/joinpunishments/punishments.txt", "Admin %N added info to DB. Steamid: %s, punishment: %s, time: %s, reason: %s.", client, authid, punishment, time, reason);
}
else
{
new String:Error[255];
SQL_GetError(db, Error, sizeof(Error));
PrintToChat(client, "[SM] Unable to ban client, contact the owner if this happened. ERROR: %s", Error);
}
return Plugin_Handled;
}
public OnClientAuthorized(client)
{
new String:steamid[48];
GetClientAuthString(client, steamid, sizeof(steamid));
int id = GetClientUserId(client);
new String:query[200];
decl String:steamid2[48];
SQL_EscapeString(db, steamid, steamid2, sizeof(steamid2));
Format(query, sizeof(query), "SELECT steamid, punishment, time, reason FROM list WHERE steamid='%s'", steamid2);
new Handle:hndl = SQL_Query(db, query);
if(hndl != INVALID_HANDLE && SQL_FetchRow(hndl))
{
new String:reason[128];
new String:time[128];
new String:punishment[128];
SQL_FetchString(hndl, 1, punishment, sizeof(punishment));
SQL_FetchString(hndl, 2, time, sizeof(time));
SQL_FetchString(hndl, 3, reason, sizeof(reason));
if(StrContains(punishment, "ban", false) != -1)
{
ServerCommand("sm_ban #%s %s %s", id, time, reason);
PrintToServer("ban");
}
if(StrContains(punishment, "gag", false) != -1)
{
ServerCommand("sm_gag #%s %s %s", id, time, reason);
PrintToServer("gag");
}
if(StrContains(punishment, "mute", false) != -1)
{
ServerCommand("sm_mute #%s %s %s", id, time, reason);
PrintToServer("mute");
}
}
else
{
new String:error[255];
SQL_GetError(db, error, sizeof(error));
PrintToServer("JOINPUNISHMENTS query error. ERROR: %s", error);
}
}