From 7307e410d42d53b11be624bf23af7db7cb0fb953 Mon Sep 17 00:00:00 2001 From: F16Gaming Date: Tue, 22 May 2012 00:50:12 +0200 Subject: [PATCH] Added AuthManager and commands: auth, authme. --- AuthManager.lua | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++++ CommandManager.lua | 33 ++++++++++++++++ PlayerManager.lua | 18 ++++++--- load.xml | 9 +++-- locales/enUS.lua | 30 ++++++++++++++ locales/svSE.lua | 30 ++++++++++++++ 6 files changed, 223 insertions(+), 9 deletions(-) create mode 100644 AuthManager.lua diff --git a/AuthManager.lua b/AuthManager.lua new file mode 100644 index 0000000..b19d0b3 --- /dev/null +++ b/AuthManager.lua @@ -0,0 +1,112 @@ +--[[ + * Copyright (c) 2011-2012 by Adam Hellberg. + * + * This file is part of Command. + * + * Command is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Command is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Command. If not, see . +--]] + +local C = Command + +C.AuthManager = { + Users = {} +} + +local AM = C.AuthManager +local CES = C.Extensions.String +local CET = C.Extensions.Table + +local function GeneratePassword(user, pass) + local pass = ("%s:%s"):format(tostring(user):upper(), tostring(pass)) + -- TODO: Make generation better + return pass +end + +local function Verify(user, pass) + user = tostring(user):upper() + if not AM.Users[user] then return false end + if not AM.Users[user].Enabled then return false end + if AM.Users[user].Authed then return true end + if not pass:match("^%w+:%w+$") then + pass = ("%s:%s"):format(user, pass) + end + local uPass = tostring(AM.Users[user].Pass) + local split = CES:Split(pass, ":") + local u = tostring(split[1]) + local p = tostring(split[2]) + pass = ("%s:%s"):format(u:upper(), p) + return pass == uPass +end + +function AM:Authenticate(user, pass) + user = tostring(user):upper() + if not self.Users[user] then + return false, "AM_ERR_NOEXISTS", {user} + elseif not self.Users[user].Enabled then + return false, "AM_ERR_DISABLED", {user} + elseif self.Users[user].Authed then + return false, "AM_ERR_AUTHED", {user} + end + if Verify(user, pass) then + self.Users[user].Authed = true + return "AM_AUTH_SUCCESS", {user, self.Users[user].Level} + end + self.Users[user].Authed = false + return false, "AM_AUTH_ERR_INVALIDPASS" +end + +function AM:Add(user, level, pass, notify) + user = tostring(user):upper() + level = tonumber(level) + if self.Users[user] then + return false, "AM_ERR_USEREXISTS", {user} + elseif not level then + return false, "AM_ERR_NOLEVEL" + end + pass = pass or tostring(math.random(123, 9999)) -- Lame default password + pass = GeneratePassword(user, pass) + local userTable = { + Name = user, + Pass = pass, + Enabled = true, + Authed = false, + Level = level + } + self.Users[user] = CET:Copy(userTable) + if notify then + C.ChatManager:SendMessage(C.LocaleManager("AM_ADD_WHISPER"):format(level, pass), "WHISPER", user) + end + return "AM_ADD_SUCCESS", {user, level, pass} +end + +function AM:Remove(user) + user = tostring(user):upper() + if not self.Users[user] then return false, "AM_ERR_NOEXISTS", {user} end + self.Users[user] = nil + return "AM_REMOVE_SUCCESS", {user} +end + +function AM:Enable(user) + user = tostring(user):upper() + if not self.Users[user] then return false, "AM_ERR_NOEXISTS", {user} end + self.Users[user].Enabled = true + return "AM_ENABLE_SUCCESS", {user} +end + +function AM:Disable(user) + user = tostring(user):upper() + if not self.Users[user] then return false, "AM_ERR_NOEXISTS", {user} end + self.Users[user].Enabled = false + return "AM_DISABLE_SUCCESS", {user} +end diff --git a/CommandManager.lua b/CommandManager.lua index 69e578a..92b3886 100644 --- a/CommandManager.lua +++ b/CommandManager.lua @@ -49,6 +49,7 @@ local QM = C.QueueManager local RM = C.RollManager local LM = C.LootManager local GT = C.GroupTools +local AM = C.AuthManager local Chat local CES = C.Extensions.String local CET = C.Extensions.Table @@ -372,6 +373,38 @@ CM:Register({"ban"}, PM.Access.Groups.Admin.Level, function(args, sender, isChat return PM:BanUser(player) end, "CM_BAN_HELP") +CM:Register({"auth", "authenticate", "a"}, PM.Access.Local, function(args, sender, isChat) + if isChat and isChat ~= "WHISPER" then return false, "CM_ERR_NOCHAT" end + if #args < 2 then return false, "CM_AUTH_USAGE" end + local arg = tostring(args[1]):lower() + local target = tostring(args[2]):upper() + local notify = true + if target == UnitName("player"):upper() then return false, "CM_AUTH_ERR_SELF" end + if sender.Info.Name:upper() == target then notify = false end + if arg:match("^a") then -- Add + local level = tonumber(args[3]) + local pass = args[4] + if not level then + return false, "CM_AUTH_ADDUSAGE" + end + return AM:Add(target, level, pass, notify) + elseif arg:match("^r") then -- Remove + return AM:Remove(target) + elseif arg:match("^e") then -- Enable + return AM:Enable(target) + elseif arg:match("^d") then -- Disable + return AM:Disable(target) + end + return false, "CM_AUTH_USAGE" +end, "CM_AUTH_HELP") + +CM:Register({"authme", "authenticateme", "am"}, PM.Access.Groups.User.Level, function(args, sender, isChat) + if not isChat then return false, "CM_ERR_CHATONLY" end + if #args <= 0 then return false, "CM_AUTHME_USAGE" end + local pass = tostring(args[1]) + return AM:Authenticate(sender.Info.Name, pass) +end, "CM_AUTHME_HELP") + CM:Register({"accept", "acceptinvite", "acceptinv", "join", "joingroup"}, PM.Access.Groups.User.Level, function(args, sender, isChat) if not StaticPopup_Visible("PARTY_INVITE") then return false, "CM_ACCEPTINVITE_NOTACTIVE" diff --git a/PlayerManager.lua b/PlayerManager.lua index e2ef7ae..2bc6af9 100644 --- a/PlayerManager.lua +++ b/PlayerManager.lua @@ -1,18 +1,18 @@ --[[ * Copyright (c) 2011-2012 by Adam Hellberg. - * + * * This file is part of Command. - * + * * Command is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. - * + * * Command is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public License * along with Command. If not, see . --]] @@ -32,6 +32,7 @@ local C = Command local L = C.LocaleManager local CM local GT = C.GroupTools +local AM = C.AuthManager local BNT = C.BattleNetTools local CCM local CET = C.Extensions.Table @@ -455,12 +456,19 @@ function PM:HasAccess(player, command) return true end local hasAccess = self:GetAccess(player) <= command.Access + local auth = AM.Users[player.Info.Name:upper()] + if auth then + local authLevel = tonumber(auth.Level) + if authLevel and auth.Enabled and auth.Authed then + if authLevel <= command.Access then hasAccess = true end + end + end local group = self.Access.Groups[player.Info.Group] if CET:HasValue(group.Allow, command.Name) then hasAccess = true end if CET:HasValue(group.Deny, command.Name) then hasAccess = false end if CET:HasValue(player.Access.Allow, command.Name) then hasAccess = true end if CET:HasValue(player.Access.Deny, command.Name) then hasAccess = false end - if (List[command.Name] and self:GetListMode() == MODE_BLACKLIST) or (not List[command.Name] and self:GetListMode() == MODE_WHITELIST) then + if not self:IsCommandAllowed(command) then hasAccess = false end return hasAccess diff --git a/load.xml b/load.xml index d1498bf..0c197c8 100644 --- a/load.xml +++ b/load.xml @@ -1,18 +1,18 @@  @@ -26,6 +26,7 @@