· 7 years ago · Dec 01, 2018, 02:58 PM
1-- refractionservers.net, Chewgum - chewgumtj@gmail.com --
2
3atlaschat = atlaschat or {}
4
5atlaschat.ranks = {}
6atlaschat.restrictions = {}
7
8AddCSLuaFile("sh_utilities.lua")
9AddCSLuaFile("sh_config.lua")
10AddCSLuaFile("cl_expression.lua")
11AddCSLuaFile("cl_theme.lua")
12AddCSLuaFile("cl_panel.lua")
13
14AddCSLuaFile("gui/frame.lua")
15AddCSLuaFile("gui/config.lua")
16AddCSLuaFile("gui/slider.lua")
17AddCSLuaFile("gui/expression_list.lua")
18AddCSLuaFile("gui/form.lua")
19AddCSLuaFile("gui/rank_list.lua")
20AddCSLuaFile("gui/editor.lua")
21AddCSLuaFile("gui/mysql.lua")
22AddCSLuaFile("gui/restrictions.lua")
23AddCSLuaFile("gui/chatroom.lua")
24
25include("sh_utilities.lua")
26include("sh_config.lua")
27include("sv_sql.lua")
28include("cl_theme.lua") -- Nothing is actually used serverside.
29
30resource.AddFile("materials/atlaschat/plus.png")
31resource.AddFile("materials/atlaschat/cross.png")
32resource.AddFile("materials/atlaschat/check.png")
33resource.AddFile("materials/atlaschat/users.png")
34resource.AddFile("materials/atlaschat/emotes.png")
35resource.AddFile("materials/atlaschat/settings.png")
36
37resource.AddFile("materials/atlaschat/emoticons/overrustle.png")
38resource.AddFile("materials/atlaschat/emoticons/garry.png")
39resource.AddFile("materials/atlaschat/emoticons/gaben.png")
40
41resource.AddFile("resource/fonts/opensans_bold.ttf")
42resource.AddFile("resource/fonts/opensans_bolditalic.ttf")
43resource.AddFile("resource/fonts/opensans_extrabold.ttf")
44resource.AddFile("resource/fonts/opensans_extrabolditalic.ttf")
45resource.AddFile("resource/fonts/opensans_italic.ttf")
46resource.AddFile("resource/fonts/opensans_light.ttf")
47resource.AddFile("resource/fonts/opensans_lightitalic.ttf")
48resource.AddFile("resource/fonts/opensans_regular.ttf")
49resource.AddFile("resource/fonts/opensans_semibold.ttf")
50resource.AddFile("resource/fonts/opensans_semibolditalic.ttf")
51
52-- configuration ot add usergroups to a whitelist of who can edit stuff
53
54----------------------------------------------------------------------
55-- Purpose:
56--
57----------------------------------------------------------------------
58
59hook.Add("Initialize", "atlaschat.Initialize", function()
60 local version = file.Read("atlaschat_version.txt", "DATA")
61
62 if (version) then
63 ATLASCHAT_VERSION_PREVIOUS = tonumber(version)
64 end
65
66 file.Write("atlaschat_version.txt", tostring(ATLASCHAT_VERSION), "DATA")
67
68 atlaschat.sql.Initialize()
69end)
70
71----------------------------------------------------------------------
72-- Purpose:
73-- Called when the database has connected.
74----------------------------------------------------------------------
75
76hook.Add("atlaschat.DatabaseConnected", "1", function(remote, firstTime)
77 if (firstTime) then
78 atlaschat.sql.Query("CREATE TABLE IF NOT EXISTS atlaschat_players(id INTEGER PRIMARY KEY " .. (remote and "AUTO_INCREMENT" or "") .. ", steamID TEXT, title TEXT)")
79 atlaschat.sql.Query("CREATE TABLE IF NOT EXISTS atlaschat_ranks(id INTEGER PRIMARY KEY " .. (remote and "AUTO_INCREMENT" or "") .. ", usergroup TEXT, icon TEXT, tag TEXT)")
80 atlaschat.sql.Query("CREATE TABLE IF NOT EXISTS atlaschat_restrictions(id INTEGER PRIMARY KEY " .. (remote and "AUTO_INCREMENT" or "") .. ", expression TEXT, usergroups TEXT)")
81
82 atlaschat.sql.Query("SELECT * FROM atlaschat_ranks", function(data, query)
83 if (data) then
84 for i = 1, #data do
85 local info = data[i]
86
87 atlaschat.ranks[info.usergroup] = {tag = info.tag, icon = info.icon}
88 end
89 end
90 end)
91
92 atlaschat.sql.Query("SELECT * FROM atlaschat_restrictions", function(data, query)
93 if (data) then
94 for i = 1, #data do
95 local info = data[i]
96
97 atlaschat.restrictions[info.expression] = util.JSONToTable(info.usergroups)
98 end
99 end
100 end)
101 end
102end)
103
104----------------------------------------------------------------------
105-- Purpose:
106-- Called when a player connects to the server.
107----------------------------------------------------------------------
108
109util.AddNetworkString("atlaschat.plcnt")
110
111gameevent.Listen("player_connect")
112
113hook.Add("player_connect", "atlaschat.PlayerConnect", function(data)
114 net.Start("atlaschat.plcnt")
115 net.WriteString(data.name)
116 net.WriteString(data.networkid)
117 net.Broadcast()
118end)
119
120----------------------------------------------------------------------
121-- Purpose:
122-- Called when a player wants to load their data.
123----------------------------------------------------------------------
124
125util.AddNetworkString("atlaschat.plload")
126
127net.Receive("atlaschat.plload", function(bits, player)
128 local loaded = player.atlaschatLoaded
129
130 player.nextMessageAtlas = 0
131
132 if (!loaded) then
133 local steamID = sql.SQLStr(player:SteamID())
134
135 atlaschat.sql.Query("SELECT * FROM atlaschat_players WHERE steamID=" .. steamID, function(data, query)
136 if (IsValid(player)) then
137 if (data and #data > 0) then
138 data = data[1]
139
140 if (data.title and data.title != "" and data.title != "NULL") then
141 player:SetNetworkedString("ac_title", data.title)
142 end
143 else
144 atlaschat.sql.Query("INSERT INTO atlaschat_players(steamID) VALUES(" .. steamID .. ")")
145 end
146 end
147 end)
148
149 timer.Simple(0.2, function()
150 if (IsValid(player)) then
151 atlaschat.config.SyncVariables(player)
152 end
153 end)
154
155 for unique, data in pairs(atlaschat.ranks) do
156 local tag = data.tag != NULL and data.tag or ""
157
158 net.Start("atlaschat.crtrnkgt")
159 net.WriteString(unique)
160 net.WriteString(tag)
161 net.WriteString(data.icon)
162 net.Send(player)
163 end
164
165 for expression, usergroups in pairs(atlaschat.restrictions) do
166 local len = table.Count(usergroups)
167
168 net.Start("atlaschat.rstcs")
169 net.WriteString(expression)
170 net.WriteUInt(len, 8)
171
172 for k, v in pairs(usergroups) do
173 net.WriteString(k)
174 end
175
176 net.WriteBit(false)
177 net.Send(player)
178 end
179
180 player.atlaschatLoaded = true
181 end
182end)
183
184----------------------------------------------------------------------
185-- Purpose:
186-- Setting a players title.
187----------------------------------------------------------------------
188
189util.AddNetworkString("atlaschat.stplttl")
190
191net.Receive("atlaschat.stplttl", function(bits, player)
192 local isAdmin = player:IsSuperAdmin()
193
194 if (isAdmin) then
195 local target = net.ReadString()
196
197 target = util.FindPlayerAtlaschat(target, player)
198
199 if (IsValid(target)) then
200 local title = net.ReadString()
201 local steamID = sql.SQLStr(target:SteamID())
202
203 target:SetNetworkedString("ac_title", title)
204
205 atlaschat.sql.Query("UPDATE atlaschat_players SET title=" .. sql.SQLStr(title) .. " WHERE steamID=" .. steamID)
206 end
207 end
208end)
209
210----------------------------------------------------------------------
211-- Purpose:
212-- Creating a new usergroup.
213----------------------------------------------------------------------
214
215util.AddNetworkString("atlaschat.crtrnk")
216util.AddNetworkString("atlaschat.crtrnkex")
217util.AddNetworkString("atlaschat.crtrnkgt")
218
219net.Receive("atlaschat.crtrnk", function(bits, player)
220 local isAdmin = player:IsSuperAdmin()
221
222 if (isAdmin) then
223 local userGroup = net.ReadString()
224
225 if (atlaschat.ranks[userGroup] != nil) then
226 atlaschat.Notify(":exclamation: Could not create the usergroup: The usergroup already exist!", player)
227 else
228 atlaschat.ranks[userGroup] = {tag = "", icon = "icon16/user.png"}
229
230 atlaschat.sql.Query("INSERT INTO atlaschat_ranks(id, usergroup, icon, tag) VALUES(NULL, " .. sql.SQLStr(userGroup) .. ", " .. sql.SQLStr("icon16/user.png") .. ", " .. sql.SQLStr("") .. ")")
231
232 net.Start("atlaschat.crtrnkgt")
233 net.WriteString(userGroup)
234 net.WriteString("")
235 net.WriteString("icon16/user.png")
236 net.Broadcast()
237
238 atlaschat.Notify(":information: Successfully created the usergroup '" .. userGroup .. "'!", player)
239 end
240 end
241end)
242
243----------------------------------------------------------------------
244-- Purpose:
245-- Removing a usergroup.
246----------------------------------------------------------------------
247
248util.AddNetworkString("atlaschat.rmvrnk")
249
250net.Receive("atlaschat.rmvrnk", function(bits, player)
251 local isAdmin = player:IsSuperAdmin()
252
253 if (isAdmin) then
254 local userGroup = net.ReadString()
255
256 if (atlaschat.ranks[userGroup]) then
257 atlaschat.ranks[userGroup] = nil
258
259 atlaschat.sql.Query("DELETE FROM atlaschat_ranks WHERE usergroup = " .. sql.SQLStr(userGroup))
260
261 net.Start("atlaschat.crtrnkgt")
262 net.WriteString(userGroup)
263 net.WriteString("")
264 net.WriteString("")
265 net.WriteUInt(1, 8)
266 net.Broadcast()
267
268 atlaschat.Notify(":information: Successfully removed the usergroup '" .. userGroup .. "'!", player)
269 else
270 atlaschat.Notify(":exclamation: Could not remove the usergroup: The usergroup does not exist!", player)
271 end
272 end
273end)
274
275----------------------------------------------------------------------
276-- Purpose:
277-- Changing the icon & title of a usergroup.
278----------------------------------------------------------------------
279
280util.AddNetworkString("atlaschat.chnric")
281
282net.Receive("atlaschat.chnric", function(bits, player)
283 local isAdmin = player:IsSuperAdmin()
284
285 if (isAdmin) then
286 local userGroup = net.ReadString()
287
288 if (atlaschat.ranks[userGroup]) then
289 local tag = net.ReadString()
290 local icon = net.ReadString()
291
292 atlaschat.ranks[userGroup] = {tag = tag, icon = icon}
293
294 atlaschat.sql.Query("UPDATE atlaschat_ranks SET icon = " .. sql.SQLStr(icon) .. ", tag = " .. sql.SQLStr(tag) .. " WHERE usergroup = " .. sql.SQLStr(userGroup))
295
296 net.Start("atlaschat.crtrnkgt")
297 net.WriteString(userGroup)
298 net.WriteString(tag)
299 net.WriteString(icon)
300 net.WriteUInt(2, 8)
301 net.Broadcast()
302
303 atlaschat.Notify(":information: Successfully changed the icon/tag of usergroup '" .. userGroup .. "' to '" .. icon .. " - " .. tag .. "'!", player)
304 else
305 atlaschat.Notify(":exclamation: Could not change the icon/tag: The usergroup does not exist!", player)
306 end
307 end
308end)
309
310----------------------------------------------------------------------
311-- Purpose:
312-- Adding & removing expression restrictions.
313----------------------------------------------------------------------
314
315util.AddNetworkString("atlaschat.rstcs")
316
317net.Receive("atlaschat.rstcs", function(bits, player)
318 local isAdmin = player:IsSuperAdmin()
319
320 if (isAdmin) then
321 local remove = util.tobool(net.ReadBit())
322 local expression = net.ReadString()
323 local usergroup = net.ReadString()
324
325 atlaschat.restrictions[expression] = atlaschat.restrictions[expression] or {}
326
327 if (remove) then
328 atlaschat.restrictions[expression][usergroup] = nil
329 else
330 atlaschat.restrictions[expression][usergroup] = true
331 end
332
333 net.Start("atlaschat.rstcs")
334 net.WriteString(expression)
335 net.WriteUInt(1, 8)
336 net.WriteString(usergroup)
337 net.WriteBit(remove)
338 net.Broadcast()
339
340 -- Lazy way of storing data.
341 local info = util.TableToJSON(atlaschat.restrictions[expression])
342
343 if (atlaschat.sql.IsRemote()) then
344 atlaschat.sql.Query("SELECT id FROM atlaschat_restrictions WHERE expression = " .. sql.SQLStr(expression), function(data, query)
345 if (#data > 0) then
346 atlaschat.sql.Query("UPDATE atlaschat_restrictions SET usergroups = " .. sql.SQLStr(info) .. " WHERE expression = " .. sql.SQLStr(expression))
347 else
348 atlaschat.sql.Query("INSERT INTO atlaschat_restrictions(id, expression, usergroups) VALUES(NULL, " .. sql.SQLStr(expression) .. ", " .. sql.SQLStr(info) .. ")")
349 end
350 end)
351 else
352 atlaschat.sql.Query("INSERT OR REPLACE INTO atlaschat_restrictions(id, expression, usergroups) VALUES((SELECT id FROM atlaschat_restrictions WHERE expression = " .. sql.SQLStr(expression) .. "), " .. sql.SQLStr(expression) .. ", " .. sql.SQLStr(info) .. ")")
353 end
354 end
355end)
356
357---------------------------------------------------------
358-- Private chatting.
359---------------------------------------------------------
360
361local privateMessages = {}
362
363---------------------------------------------------------
364-- Creating a private chat.
365---------------------------------------------------------
366
367util.AddNetworkString("atlaschat.stpm")
368util.AddNetworkString("atlaschat.nwpm")
369
370net.Receive("atlaschat.stpm", function(bits, player)
371 local key, count = nil, table.Count(privateMessages)
372
373 for i = 1, count do
374 if (privateMessages[i] == nil) then
375 key = i
376
377 break
378 end
379 end
380
381 if (!key) then key = count +1 end
382
383 privateMessages[key] = {players = {player}, creator = player}
384
385 net.Start("atlaschat.nwpm")
386 net.WriteUInt(key, 8)
387 net.Send(player)
388
389 local data = privateMessages[key]
390
391 net.Start("atlaschat.gtplpm")
392 net.WriteUInt(key, 8)
393 net.WriteUInt(#data.players, 8)
394
395 for i = 1, #data.players do
396 net.WriteEntity(data.players[i])
397 end
398
399 net.WriteEntity(data.creator)
400 net.Send(player)
401end)
402
403---------------------------------------------------------
404-- Sending a text message in a private chat.
405---------------------------------------------------------
406
407util.AddNetworkString("atlaschat.rxpm")
408util.AddNetworkString("atlaschat.txpm")
409
410net.Receive("atlaschat.txpm", function(bits, player)
411 if (player.nextMessageAtlas > CurTime()) then return end
412
413 player.nextMessageAtlas = CurTime() +0.25
414
415 -- Don't allow dead players in TTT to private chat!
416 if (TellTraitorsAboutTraitors) then
417 if (player:IsSpec() and GAMEMODE.round_state == ROUND_ACTIVE and DetectiveMode()) then
418 player:ChatPrint("You cannot talk in private chats in this state.")
419
420 return
421 end
422 end
423
424 local key = net.ReadUInt(8)
425 local text = net.ReadString()
426 local receivers = privateMessages[key]
427
428 if (receivers) then
429 receivers = receivers.players
430
431 text = string.sub(text, 0, 127)
432
433 net.Start("atlaschat.rxpm")
434 net.WriteUInt(key, 8)
435 net.WriteString(text)
436 net.WriteEntity(player)
437 net.Send(receivers)
438
439 ServerLog("[atlaschat] (PM) " .. player:Nick() .. ": " .. text .. "\n")
440 end
441end)
442
443---------------------------------------------------------
444-- Leaving a private chat.
445---------------------------------------------------------
446
447util.AddNetworkString("atlaschat.lvpm")
448
449net.Receive("atlaschat.lvpm", function(bits, player)
450 local key = net.ReadUInt(8)
451 local data = privateMessages[key]
452
453 if (data) then
454 for i = 1, #data.players do
455 local value = data.players[i]
456
457 if (value == player) then
458 table.remove(data.players, i)
459
460 break
461 end
462 end
463
464 net.Start("atlaschat.nkickpm")
465 net.WriteUInt(key, 8)
466 net.WriteEntity(player)
467 net.WriteBit(true)
468 net.Send(data.players)
469
470 if (#data.players <= 0) then
471 privateMessages[key] = nil
472 end
473 end
474end)
475
476---------------------------------------------------------
477-- Joining a private chat.
478---------------------------------------------------------
479
480util.AddNetworkString("atlaschat.jnpm")
481util.AddNetworkString("atlaschat.gtplpm")
482
483net.Receive("atlaschat.jnpm", function(bits, player)
484 local key = net.ReadUInt(8)
485 local data = privateMessages[key]
486
487 if (data) then
488 local exists = false
489
490 for i = 1, #data.players do
491 local info = data.players[i]
492
493 if (info == player) then
494 exists = true
495
496 break
497 end
498 end
499
500 if (!exists) then
501 table.insert(data.players, player)
502
503 -- Send information about the chat room to the player.
504 net.Start("atlaschat.gtplpm")
505 net.WriteUInt(key, 8)
506 net.WriteUInt(#data.players, 8)
507
508 for i = 1, #data.players do
509 net.WriteEntity(data.players[i])
510 end
511
512 net.WriteEntity(data.creator)
513 net.Send(player)
514
515 -- Network that this player has joined the chat room.
516 net.Start("atlaschat.gtplpm")
517 net.WriteUInt(key, 8)
518 net.WriteUInt(1, 8)
519 net.WriteEntity(player)
520 net.Send(data.players)
521 end
522 end
523end)
524
525---------------------------------------------------------
526-- Kicking a player from a private chat.
527---------------------------------------------------------
528
529util.AddNetworkString("atlaschat.kickpm")
530util.AddNetworkString("atlaschat.nkickpm")
531
532net.Receive("atlaschat.kickpm", function(bits, player)
533 local key = net.ReadUInt(8)
534 local data = privateMessages[key]
535
536 if (data) then
537 if (data.creator == player) then
538 local target = net.ReadEntity()
539
540 for i = 1, #data.players do
541 local info = data.players[i]
542
543 if (info == target) then
544 net.Start("atlaschat.nkickpm")
545 net.WriteUInt(key, 8)
546 net.WriteEntity(target)
547 net.WriteBit(false)
548 net.Send(data.players)
549
550 table.remove(data.players, i)
551
552 break
553 end
554 end
555 end
556 end
557end)
558
559---------------------------------------------------------
560-- Inviting a player to a private chat.
561---------------------------------------------------------
562
563util.AddNetworkString("atlaschat.invpm")
564util.AddNetworkString("atlaschat.sinvpm")
565
566net.Receive("atlaschat.invpm", function(bits, player)
567 local key = net.ReadUInt(8)
568 local target = net.ReadString()
569
570 target = util.FindPlayerAtlaschat(target, player)
571
572 if (IsValid(target)) then
573 net.Start("atlaschat.sinvpm")
574 net.WriteUInt(key, 8)
575 net.WriteEntity(player)
576 net.Send(target)
577 end
578end)
579
580---------------------------------------------------------
581-- Clears your configuration.
582---------------------------------------------------------
583
584util.AddNetworkString("atlaschat.clrcfg")
585util.AddNetworkString("atlaschat.rqclrcfg")
586
587net.Receive("atlaschat.rqclrcfg", function(bits, player)
588 local target = net.ReadString()
589
590 if (target != "") then
591 target = util.FindPlayerAtlaschat(target, player)
592
593 if (IsValid(target)) then
594 if (target != player and !player:IsAdmin()) then return end
595
596 net.Start("atlaschat.clrcfg")
597 net.Send(target)
598
599 if (target == player) then
600 atlaschat.Notify("Successfully reset your atlaschat configuration! Close and open your chatbox to apply.", target)
601 else
602 atlaschat.Notify(player:Nick() .. " has reset your atlaschat configuration! Close and open your chatbox to apply.", target)
603 atlaschat.Notify("You have reset " .. target:Nick() .. "'s atlaschat configuration! Close and open your chatbox to apply.", player)
604 end
605 end
606 else
607 if (player:IsAdmin()) then
608 net.Start("atlaschat.clrcfg")
609 net.Broadcast()
610
611 atlaschat.Notify(player:Nick() .. " has reset everyone's atlaschat configuration! Close and open your chatbox to apply.")
612 end
613 end
614end)
615
616---------------------------------------------------------
617-- Atlas chat messages.
618---------------------------------------------------------
619
620util.AddNetworkString("atlaschat.msg")
621
622function atlaschat.Notify(text, player)
623 net.Start("atlaschat.msg")
624 net.WriteString(text)
625 if (IsValid(player)) then net.Send(player) else net.Broadcast() end
626end
627
628---------------------------------------------------------
629-- Net message for larger text!
630---------------------------------------------------------
631
632util.AddNetworkString("atlaschat.chat")
633util.AddNetworkString("atlaschat.chatText")
634
635local isstring = isstring
636
637net.Receive("atlaschat.chat", function(bits, player)
638 if (player.nextMessageAtlas > CurTime()) then return end
639
640 player.nextMessageAtlas = CurTime() +0.25
641
642 local text = net.ReadString()
643 local team = util.tobool(net.ReadBit())
644
645 text = string.sub(text, 0, 127)
646
647 local newText = hook.Run("PlayerSay", player, text, team, !player:Alive())
648
649 -- A workaround for dumb coders that return a boolean in the PlayerSay hook.
650 if (isstring(newText)) then
651 text = newText
652 elseif (not newText) then
653 text = ""
654 end
655
656 if (text != "") then
657 if (game.IsDedicated()) then
658 ServerLog(player:Nick() .. ": " .. text .. "\n")
659 end
660
661 local filter = {}
662 local players = util.GetPlayers()
663
664 for i = 1, #players do
665 local target = players[i]
666
667 if (IsValid(target)) then
668 local canSee = hook.Run("PlayerCanSeePlayersChat", text, team, target, player)
669
670 if (canSee or target == player) then
671 table.insert(filter, target)
672 end
673 end
674 end
675
676 net.Start("atlaschat.chatText")
677 net.WriteString(text)
678 net.WriteEntity(player)
679 net.WriteBit(team)
680 net.Send(filter)
681 end
682end)
683
684----------------------------------------------------------------------
685-- Purpose:
686-- Sets "Player.IsTyping"
687----------------------------------------------------------------------
688
689util.AddNetworkString("atlaschat.istyping")
690
691net.Receive("atlaschat.istyping", function(bits, player)
692 local bool = util.tobool(net.ReadBit())
693
694 player:SetNetworkedBool("atlaschat.istyping", bool)
695end)
696
697----------------------------------------------------------------------
698-- Purpose:
699-- Returns true/false if the player is/isn't typing.
700----------------------------------------------------------------------
701
702function PLAYER_META:IsTyping()
703 return self:GetNetworkedBool("atlaschat.istyping")
704end