--[[-------------------------------------- fCon - MySQLOO Forum Integration Serverside functions and networking Coded by: Dark Ember Updated: 1/23/2014 Version 1.3 --------------------------------------]]-- util.AddNetworkString("fCon.Check") util.AddNetworkString("fCon.linkAccount") util.AddNetworkString("fCon.ChangePass") util.AddNetworkString("fCon.UpdatePass") util.AddNetworkString("fCon.UpdatePassCL") util.AddNetworkString("fCon.RegisterCL") fCon.Provider = fCon.Providers[fCon.dbConfig.fType] function fCon.Create(pl, accInfo) if !pl:IsValid() then return end if pl.HasAccount then return end local name = pl:Nick() if fCon.Config.UseAltName and pl.SteamName and pl:SteamName() then name = pl:SteamName() end local query = fCon.Provider.CreateUser(accInfo, pl) fCon.Query(query, function(res, ret) if res then local RF = RecipientFilter() RF:AddPlayer(pl) if !ret or ret[1] == nil or table.Count(ret) == 0 then umsg.Start("AccountCreated", RF) umsg.End() pl.ForumGroup = fCon.Ranks.GroupID[pl:GetUserGroup()] pl.HasAccount = true pl.ForumUsername = accInfo.user fCon.UpdateInfo(pl) else umsg.Start("AccountProblem", RF) umsg.End() end end end) end function fCon.linkAccount(len, pl) if !pl:IsValid() then return end if pl.HasAccount then return end local accInfo = net.ReadTable() local RF = RecipientFilter() RF:AddPlayer(pl) local query = fCon.Provider.CheckAuth(accInfo) fCon.Query(query, function(res, ret) if res and table.Count(ret) ~= 0 then if fCon.Provider.CheckPassword then local authed = fCon.Provider.CheckPassword(accInfo,ret) if authed then local query2 = fCon.Provider.LinkUser(accInfo,pl) else umsg.Start("ProblemLinking",RF) umsg.End() return end end local query2 = fCon.Provider.LinkUser(accInfo,pl) fCon.Query(query2, function(res2, ret2) if res2 then pl.HasAccount = true pl.ForumUsername = ret[1]["member_name"] umsg.Start("AccountLinked",RF) umsg.End() fCon.UpdateInfo(pl) else umsg.Start("ProblemLinking3",RF) umsg.End() end end) else umsg.Start("ProblemLinking2",RF) umsg.End() return end end) end net.Receive("fCon.linkAccount",fCon.linkAccount) function fCon.CheckUName(len, pl) if !pl:IsValid() then return end if pl.HasAccount then return end local accInfo = net.ReadTable() local query = fCon.Provider.CheckUName(accInfo) fCon.Query(query, function(res, ret) if res then local RF = RecipientFilter() RF:AddPlayer(pl) if !ret or ret[1] == nil or table.Count(ret) == 0 then fCon.Create(pl, accInfo) else umsg.Start("UsernameExists", RF) umsg.End() end end end) end net.Receive("fCon.Check",fCon.CheckUName) function fCon.UpdateInfo(pl) if !pl:IsValid() then return end local query = fCon.Provider.UpdateInfo(pl) fCon.Query(query) end function fCon.UpdatePass(len, pl) if !pl:IsValid() then return end local accInfo = net.ReadTable() local query = fCon.Provider.LinkUser(accInfo, pl) fCon.Query(query,function(res,ret) if res then umsg.Start("PasswordUpdated",RF) umsg.End() else umsg.Start("ProblemUpdating",RF) umsg.End() end end) end net.Receive("fCon.UpdatePass",fCon.UpdatePass) function fCon.getIP(pl) return string.Explode(":", tostring(pl:IPAddress()))[1] end function fCon.RegisterClient(ply) net.Start("fCon.RegisterCL") net.Send(ply) end function fCon.ChangePass(len,pl) if !pl.HasAccount then pl:PrintMessage(HUD_PRINTTALK,"You do not appear to have a forum account. Please register one before attempting to change the password.") return end net.Start("fCon.UpdatePassCL") net.WriteString(pl.ForumUsername) net.Send(pl) end net.Receive("fCon.ChangePass",fCon.ChangePass) function fCon.LoadPlayerInfo(ply) if !ply:IsValid() then return end if !ply.IsLoaded then ply.HasAccount = false local query = fCon.Provider.LoadPlayer(ply) local usern, groupid = fCon.Provider.UserNColumn, fCon.Provider.GroupColumn fCon.Query(query, function(res, ret) if res then if ret and (!ret[1] or !ret[1][usern]) then if fCon.Config.ForceRegistration then fCon.RegisterClient(ply) else ply:PrintMessage(HUD_PRINTTALK,"You do not seem to have a forum account! Type "..fCon.Config.RegisterCmd.." to create a forum account!") end return end fCon.UpdateInfo(ply) ply.ForumGroup = ret[1][groupid] ply.ForumUsername = ret[1][usern] if fCon.Config.UseForumRank then if fCon.Ranks.ForumID[ply.ForumGroup] then ply:SetUserGroup(fCon.Ranks.ForumID[ply.ForumGroup]) else ply:SetUserGroup("user") end end ply.HasAccount = true end ply.IsLoaded = true end) end end hook.Add("PlayerInitialSpawn", "LoadRankAuthed", fCon.LoadPlayerInfo) function fCon.UpdateRank( ply ) if !ply.HasAccount then return end local query = fCon.Provider.UpdateRank(ply) fCon.Query(query) end if fCon.Config.UpdateRank then hook.Add("UCLAuthed", "fCon.UpdateRank", fCon.UpdateRank) end function fCon.ChatHook( ply, text, tc ) if string.sub(text, 1, fCon.Config.RegisterCmd:len()) == fCon.Config.RegisterCmd then if !ply.HasAccount then fCon.RegisterClient(ply) else ply:PrintMessage(HUD_PRINTTALK, "You already have a forum account! To change your password please type "..fCon.Config.PassCmd) end return '' end if string.sub(text, 1, fCon.Config.PassCmd:len()) == fCon.Config.PassCmd then if ply.ForumUsername and ply.HasAccount then fCon.ChangePass(nil, ply) else ply:PrintMessage(HUD_PRINTTALK,"You do not seem to have a forum account! Type "..fCon.Config.RegisterCmd.." to create a forum account!") end return '' end end hook.Add("PlayerSay","fCon.ChatHook",fCon.ChatHook) if fCon.Config.Announce then function fCon.ChatAnnounce() for k,v in pairs(player.GetAll()) do if v.HasAccount then v:PrintMessage(HUD_PRINTTALK, "Thanks for being a registered member of our forums! Type "..fCon.Config.PassCmd.." to change your password if you forgot it!") else v:PrintMessage(HUD_PRINTTALK, "Please type "..fCon.Config.RegisterCmd.." to register for an account on our forums!") end end end timer.Create("fCon.ChatAnnoucement", fCon.Config.AnnounceEvery, 0, fCon.ChatAnnounce) end