/* voice_plugin by Stillsetzhut */
 
#include <core>
#include <console>
#include <string>
#include <admin>
#include <adminlib>
 
#define ACCESS_MUTE 256
#define ACCESS_VOICEMODE 65536
#define ACCESS_VOTE_MUTE 1
 
#define FLAG_INVALID 0
#define FLAG_MUTE 1
 
new UserFlags[MAX_PLAYERS] = {FLAG_INVALID,...};
 
new STRING_VERSION[MAX_DATA_LENGTH] = "0.7";
 
AddUserFlag(UserIndex,Flag) {
    new Name[MAX_NAME_LENGTH];
    new VaultData[MAX_DATA_LENGTH];
    new sAuthID[MAX_AUTHID_LENGTH];
    UserFlags[UserIndex] = UserFlags[UserIndex] | Flag;
    if (Flag == FLAG_MUTE && getvar("sv_lan") == 0) {
        if (playerinfo(UserIndex,Name,MAX_NAME_LENGTH,_,_,_,_,sAuthID) != 0) {
            if(get_vaultdata(sAuthID,VaultData,MAX_DATA_LENGTH) != 0) {
                if (strcasestr(VaultData,"mute") == -1) {
                    strcat(VaultData," mute",MAX_DATA_LENGTH);
                    set_vaultdata(sAuthID,VaultData);
                    }
                }
            else {
                strcpy(VaultData,"mute",MAX_DATA_LENGTH);
                set_vaultdata(sAuthID,VaultData);
                }
            }
        }
    }
 
CheckVaultFlags(UserIndex) {
    new Name[MAX_NAME_LENGTH];
    new sAuthID[MAX_AUTHID_LENGTH];
    new VaultData[MAX_DATA_LENGTH];
    new TargetName[MAX_NAME_LENGTH];
    if(getvar("sv_lan") == 0) {
        if(playerinfo(UserIndex,Name,MAX_NAME_LENGTH,_,_,_,_,sAuthID) != 0) {
            if(get_vaultdata(sAuthID,VaultData,MAX_DATA_LENGTH) != 0) {
                if(strcasestr(VaultData,"mute") != -1) {
                    UserFlags[UserIndex] = UserFlags[UserIndex] | FLAG_MUTE;
                    execclient(TargetName,"voice_enable 0");
                    }
                }
            }
        }
    }
 
RemoveUserFlag(UserIndex,Flag) {
    new Name[MAX_NAME_LENGTH];
    new sAuthID[MAX_AUTHID_LENGTH];
    new VaultData[MAX_DATA_LENGTH];
    UserFlags[UserIndex] = UserFlags[UserIndex] & ~Flag;
    if (Flag == FLAG_MUTE && getvar("sv_lan") == 0) {
        if (playerinfo(UserIndex,Name,MAX_NAME_LENGTH,_,_,_,_,sAuthID) != 0) {
            if(get_vaultdata(sAuthID,VaultData,MAX_DATA_LENGTH) != 0) {
                if (strcasestr(VaultData,"mute") != -1) {
                    strsubst(VaultData,"mute","", MAX_DATA_LENGTH);
                    set_vaultdata(sAuthID,VaultData);
                    }
                }
            }
        }
    }
 
public admin_voiceenable(HLCommand,HLData,HLUserName,UserIndex) {
    new Command[MAX_COMMAND_LENGTH];
    new Data[MAX_DATA_LENGTH];
    new User[MAX_NAME_LENGTH];
    convert_string(HLCommand,Command,MAX_COMMAND_LENGTH);
    convert_string(HLData,Data,MAX_DATA_LENGTH);
    convert_string(HLUserName,User,MAX_NAME_LENGTH);
    strstripquotes(Data);
    if(strcmp(Data,"0") && strcmp(Data,"1")) {
        selfmessage("Invalid data:");
        selfmessage(Data);
    } else {
        execute_command(User,Command,"sv_voiceenable",Data);
    }
    return PLUGIN_HANDLED;
}
 
public admin_alltalk(HLCommand,HLData,HLUserName,UserIndex) {
    new Command[MAX_COMMAND_LENGTH];
    new Data[MAX_DATA_LENGTH];
    new User[MAX_NAME_LENGTH];
    convert_string(HLCommand,Command,MAX_COMMAND_LENGTH);
    convert_string(HLData,Data,MAX_DATA_LENGTH);
    convert_string(HLUserName,User,MAX_NAME_LENGTH);
    strstripquotes(Data);
    if(strcmp(Data,"0") && strcmp(Data,"1")) {
        selfmessage("Invalid data:");
        selfmessage(Data);
    } else {
        execute_command(User,Command,"sv_alltalk",Data);
    }
    return PLUGIN_HANDLED;
}
 
print_status(console) {
    if(getvar("sv_voiceenable")) {
        if(console) selfmessage("Voice communicaton is enabled");
        else say("Voice communicaton is enabled");
        if(getvar("sv_alltalk")) {
            if(console) selfmessage("Teams can't hear each other");
            else say("Teams can't hear each other");
        } else {
            if(console) selfmessage("Everybody can hear everybody");
            else say("Everybody can hear everybody");
        }
    } else {
        if(console) selfmessage("Voice communicaton is disabled");
        else say("Voice communicaton is disabled");
    }
    return PLUGIN_HANDLED;
}
 
 
public admin_voice(HLCommand,HLData,HLUserName,UserIndex) {
    print_status(1);
}
 
public SayCommand(HLCommand,HLData,HLUserName,UserIndex) {
    static Data[MAX_DATA_LENGTH];
    convert_string(HLData,Data,MAX_DATA_LENGTH);
    strstripquotes(Data);
    if(!strcmp(Data,"voice")) {
        print_status(0);
    }
    return PLUGIN_CONTINUE;
}
 
public admin_mute(HLCommand,HLData,HLUserName,UserIndex) {
    new Command[MAX_COMMAND_LENGTH];
    new Data[MAX_DATA_LENGTH];
    new iIndex;
    new TargetName[MAX_NAME_LENGTH];
    new Text[MAX_TEXT_LENGTH];
    new User[MAX_NAME_LENGTH];
    convert_string(HLCommand,Command,MAX_COMMAND_LENGTH);
    convert_string(HLData,Data,MAX_DATA_LENGTH);
    convert_string(HLUserName,User,MAX_NAME_LENGTH);
    strstripquotes(Data);
    if(check_user(Data) == 1) {
        get_username(Data,TargetName,MAX_NAME_LENGTH);
        if(check_immunity(TargetName)) {
            snprintf(Text,MAX_TEXT_LENGTH,"You don't have access to mute %s, body.",TargetName);
            messageex(User,Text,print_tty);
        } else {
            get_userindex(TargetName,iIndex);
            AddUserFlag(iIndex,FLAG_MUTE);
            execclient(TargetName,"voice_enable 0");
            snprintf(Text,MAX_TEXT_LENGTH,"%s is now muted from admin",TargetName);
            typesay(Text,10,255,40,44);
            messageex(TargetName,"[ADMIN] You have been muted, so SHUT UP!",print_tty);
            selfmessage("Mute Succeeded for player: ");
            selfmessage(TargetName);
            }
        } else {
        selfmessage("Unrecognized player:");
        selfmessage(Data);
    }
    return PLUGIN_HANDLED;
}
 
public admin_unmute(HLCommand,HLData,HLUserName,UserIndex) {
    new Command[MAX_COMMAND_LENGTH];
    new Data[MAX_DATA_LENGTH];
    new iIndex;
    new TargetName[MAX_NAME_LENGTH];
    new User[MAX_NAME_LENGTH];
    convert_string(HLCommand,Command,MAX_COMMAND_LENGTH);
    convert_string(HLData,Data,MAX_DATA_LENGTH);
    convert_string(HLUserName,User,MAX_NAME_LENGTH);
    strstripquotes(Data);
    if(check_user(Data) == 1) {
        get_username(Data,TargetName,MAX_NAME_LENGTH);
        get_userindex(TargetName,iIndex);
        if((UserFlags[iIndex] & FLAG_MUTE)) {
            RemoveUserFlag(iIndex,FLAG_MUTE);
            execclient(TargetName,"voice_enable 1");
            say_command(User,Command,TargetName);
            messageex(TargetName,"You have been unmuted. Restart your CS to enable voicecommunication.",print_chat);
            selfmessage("Unmuted player:");
            selfmessage(TargetName);
        } else {
            selfmessage("Player is not muted:");
            selfmessage(TargetName);
        }
    } else {
        selfmessage("Unrecognized player:");
        selfmessage(Data);
    }
    return PLUGIN_HANDLED;
}
 
public admin_vote_mute(HLCommand,HLData,HLUserName,UserIndex) {
    new Data[MAX_DATA_LENGTH];
    new Text[MAX_TEXT_LENGTH];
    if(vote_allowed()) {
        selfmessage("Vote not allowed at this time.");
        return PLUGIN_HANDLED;
    }
    convert_string(HLData,Data,MAX_DATA_LENGTH);
    strstripquotes(Data);
    if(check_user(Data)) {
        new real_user[MAX_NAME_LENGTH];
        get_username(Data,real_user,MAX_NAME_LENGTH);
        if(check_immunity(real_user)) {
            snprintf(Text,MAX_TEXT_LENGTH,"Laf. You can't start a vote to mute %s, you silly bear.",real_user);
            selfmessage(Text);
        } else {
            new TargetIndex;
            new AuthID[MAX_AUTHID_LENGTH];
            get_userindex(real_user,TargetIndex);
            get_userAuthID(real_user,AuthID,MAX_AUTHID_LENGTH);
            snprintf(Text,MAX_TEXT_LENGTH,"Mute %s?",real_user);
            Data[0] = TargetIndex;
            strcpy(Data[1],AuthID,MAX_DATA_LENGTH);
            vote(Text,"Yes","No","HandleMuteVote",Data);
        }
    } else {
        selfmessage("Unrecognized player:");
        selfmessage(Data);
    }
    return PLUGIN_HANDLED;
}
 
public HandleMuteVote(WinningOption,HLParam,VoteCount,UserCount) {
    new Text[MAX_TEXT_LENGTH];
    new TargetName[MAX_NAME_LENGTH];
    new TargetAuthID[MAX_AUTHID_LENGTH];
    new TargetIndex;
    new UserAuthID[MAX_AUTHID_LENGTH];
    new Param[MAX_AUTHID_LENGTH+1];
    convert_string(HLParam,Param,MAX_AUTHID_LENGTH+1);
    TargetIndex = Param[0];
    strcpy(TargetAuthID,Param[1],MAX_AUTHID_LENGTH);
    if(playerinfo(TargetIndex,TargetName,MAX_NAME_LENGTH,_,_,_,_,UserAuthID) && !strcmp(UserAuthID,TargetAuthID)) {
        if(WinningOption == 1) {
            new Ratio = getvar("kick_ratio");
            if(VoteCount >= Ratio*UserCount/100) {
                snprintf(Text,MAX_TEXT_LENGTH,"%s was muted due to vote.",TargetName);
                say(Text);
                messageex(TargetName,"You have been muted due to vote.",print_chat);
                AddUserFlag(TargetIndex,FLAG_MUTE);
                execclient(TargetName,"voice_enable 0");
            } else {
                snprintf(Text,MAX_TEXT_LENGTH,"Mute vote succeeded, but not enough votes to mute %s (needed %s)",TargetName,Ratio*UserCount/100);
                say(Text);
            }
        } else {
            say("Mutevote failed.");
        }
    } else {
        say("The user that was voted on left the server.");
    }
    return PLUGIN_CONTINUE;
}
 
public plugin_connect(HLUserName,HLIP,UserIndex) {
    if(UserIndex >= 1 && UserIndex <= MAX_PLAYERS) {
        UserFlags[UserIndex] = FLAG_INVALID;
        CheckVaultFlags(UserIndex);
    }
    return PLUGIN_CONTINUE;
}
 
public plugin_disconnect(HLUserName,UserIndex) {
    if (UserIndex >= 1 && UserIndex <= MAX_PLAYERS) {
        UserFlags[UserIndex] = FLAG_INVALID;
    }
    return PLUGIN_CONTINUE;
}
 
public plugin_init() {
    plugin_registerinfo("Stills's Voice Plugin","Control servers and clients voicesettings",STRING_VERSION);
    plugin_registercmd("admin_voice","admin_voice",ACCESS_ALL,"admin_voice: Display the server's voicesettings");
    plugin_registercmd("admin_vote_mute","admin_vote_mute",ACCESS_VOTE_MUTE,"admin_vote_mute <target>: Starts a vote to mute target.");
    plugin_registercmd("admin_voiceenable","admin_voiceenable",ACCESS_VOICEMODE ,"admin_voiceenable <1|0>: Enables or disables voice communication");
    plugin_registercmd("admin_alltalk","admin_alltalk",ACCESS_VOICEMODE ,"admin_alltalk <1|0>: sv_alltalk on or off");
    plugin_registercmd("admin_mute","admin_mute",ACCESS_MUTE,"admin_mute <target>: Mutes a players voice communication");
    plugin_registercmd("admin_unmute","admin_unmute",ACCESS_MUTE,"admin_unmute <target>: Unmutes a muted player.");
    plugin_registercmd("say","SayCommand",ACCESS_ALL,"say voice: Display the server's voicesettings");
    return PLUGIN_CONTINUE;
}