Skip to content

Commit

Permalink
Merge pull request #4654 from sojourn-13/tgs-tweaks
Browse files Browse the repository at this point in the history
This has been up for so long it's become the de-facto codebase anyway. I'll... config it sooner or later.
  • Loading branch information
king5327 authored May 25, 2024
2 parents 067abfd + d75a990 commit d80c1dd
Show file tree
Hide file tree
Showing 21 changed files with 712 additions and 198 deletions.
3 changes: 3 additions & 0 deletions code/_global_vars/misc.dm
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,6 @@ GLOBAL_PROTECT(log_directory)

// TGUI MOVE ME //
GLOBAL_DATUM(changelog_tgui, /datum/changelog)

// TGS //
GLOBAL_REAL_VAR(client_count)
2 changes: 2 additions & 0 deletions code/controllers/master.dm
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ GLOBAL_REAL(Master, /datum/controller/master) = new

world.TgsInitializationComplete()

world.TgsTargetedChatBroadcast(new /datum/tgs_message_content(text = "A new round has begun!"))

// Sort subsystems by display setting for easy access.
sortTim(subsystems, /proc/cmp_subsystem_display)
// Set world options.
Expand Down
3 changes: 3 additions & 0 deletions code/game/world.dm
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ var/global/datum/global_init/init = new ()
Pre-map initialization stuff should go here.
*/

#define TGS_TOPIC var/tgs_topic_return = TgsTopic(args[1]); if(tgs_topic_return) return tgs_topic_return

/datum/global_init/New()
generate_gameid()
load_configuration()
Expand Down Expand Up @@ -166,6 +168,7 @@ var/world_topic_spam_protect_ip = "0.0.0.0"
var/world_topic_spam_protect_time = world.timeofday

/world/Topic(T, addr, master, key)
TGS_TOPIC
var/list/topic_handlers = WorldTopicHandlers()

var/list/input = params2list(T)
Expand Down
4 changes: 3 additions & 1 deletion code/modules/client/client_procs.dm
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ GLOBAL_LIST_INIT(blacklisted_builds, list(


. = ..() //calls mob.Login()
++global.client_count
if (byond_version >= 512)
if (!byond_build || byond_build < 1386)
message_admins(span_adminnotice("[key_name(src)] has been detected as spoofing their byond version. Connection rejected."))
Expand Down Expand Up @@ -336,7 +337,8 @@ GLOBAL_LIST_INIT(blacklisted_builds, list(
/client/Del()
if(!gc_destroyed)
Destroy() //Clean up signals and timers.
return ..()
. = ..()
--global.client_count

/client/Destroy()
clients -= src
Expand Down
119 changes: 112 additions & 7 deletions tgs_dmapi/tgs.dm
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@
var/is_private_channel
/// Tag string associated with the channel in TGS
var/custom_tag
/// [TRUE]/[FALSE] if the channel supports embeds
var/embeds_supported

// Represents a chat user
/datum/tgs_chat_user
Expand Down Expand Up @@ -256,16 +258,119 @@
var/help_text = ""
/// If this command should be available to game administrators only
var/admin_only = FALSE
/// A subtype of [/datum/tgs_chat_command] that is ignored when enumerating available commands. Use this to create shared base /datums for commands.
var/ignore_type

/**
* Process command activation. Should return a string to respond to the issuer with.
* Process command activation. Should return a [/datum/tgs_message_content] to respond to the issuer with.
*
* sender - The [/datum/tgs_chat_user] who issued the command.
* params - The trimmed string following the command `/datum/tgs_chat_command/var/name].
*/
/datum/tgs_chat_command/proc/Run(datum/tgs_chat_user/sender, params)
CRASH("[type] has no implementation for Run()")

/// User definable chat message
/datum/tgs_message_content
/// The tring content of the message. Must be provided in New().
var/text

/// The [/datum/tgs_chat_embed] to embed in the message. Not supported on all chat providers.
var/datum/tgs_chat_embed/structure/embed

/datum/tgs_message_content/New(text)
if(!istext(text))
TGS_ERROR_LOG("[/datum/tgs_message_content] created with no text!")
text = null

src.text = text

/// User definable chat embed. Currently mirrors Discord chat embeds. See https://discord.com/developers/docs/resources/channel#embed-object-embed-structure for details.
/datum/tgs_chat_embed/structure
var/title
var/description
var/url

/// Timestamp must be encoded as: time2text(world.timeofday, "YYYY-MM-DD hh:mm:ss"). Use the active timezone.
var/timestamp

/// Colour must be #AARRGGBB or #RRGGBB hex string
var/colour

/// See https://discord.com/developers/docs/resources/channel#embed-object-embed-image-structure for details.
var/datum/tgs_chat_embed/media/image

/// See https://discord.com/developers/docs/resources/channel#embed-object-embed-thumbnail-structure for details.
var/datum/tgs_chat_embed/media/thumbnail

/// See https://discord.com/developers/docs/resources/channel#embed-object-embed-image-structure for details.
var/datum/tgs_chat_embed/media/video

var/datum/tgs_chat_embed/footer/footer
var/datum/tgs_chat_embed/provider/provider
var/datum/tgs_chat_embed/provider/author/author

var/list/datum/tgs_chat_embed/field/fields

/// Common datum for similar discord embed medias
/datum/tgs_chat_embed/media
/// Must be set in New().
var/url
var/width
var/height
var/proxy_url

/datum/tgs_chat_embed/media/New(url)
if(!istext(url))
CRASH("[/datum/tgs_chat_embed/media] created with no url!")

src.url = url

/// See https://discord.com/developers/docs/resources/channel#embed-object-embed-footer-structure for details.
/datum/tgs_chat_embed/footer
/// Must be set in New().
var/text
var/icon_url
var/proxy_icon_url

/datum/tgs_chat_embed/footer/New(text)
if(!istext(text))
CRASH("[/datum/tgs_chat_embed/footer] created with no text!")

src.text = text

/// See https://discord.com/developers/docs/resources/channel#embed-object-embed-provider-structure for details.
/datum/tgs_chat_embed/provider
var/name
var/url

/// See https://discord.com/developers/docs/resources/channel#embed-object-embed-author-structure for details. Must have name set in New().
/datum/tgs_chat_embed/provider/author
var/icon_url
var/proxy_icon_url

/datum/tgs_chat_embed/provider/author/New(name)
if(!istext(name))
CRASH("[/datum/tgs_chat_embed/provider/author] created with no name!")

src.name = name

/// See https://discord.com/developers/docs/resources/channel#embed-object-embed-field-structure for details. Must have name and value set in New().
/datum/tgs_chat_embed/field
var/name
var/value
var/is_inline

/datum/tgs_chat_embed/field/New(name, value)
if(!istext(name))
CRASH("[/datum/tgs_chat_embed/field] created with no name!")

if(!istext(value))
CRASH("[/datum/tgs_chat_embed/field] created with no value!")

src.name = name
src.value = value

// API FUNCTIONS

/// Returns the maximum supported [/datum/tgs_version] of the DMAPI.
Expand Down Expand Up @@ -296,30 +401,30 @@
/**
* Send a message to connected chats.
*
* message - The string to send.
* message - The [/datum/tgs_message_content] to send.
* admin_only: If [TRUE], message will be sent to admin connected chats. Vice-versa applies.
*/
/world/proc/TgsTargetedChatBroadcast(message, admin_only = FALSE)
/world/proc/TgsTargetedChatBroadcast(datum/tgs_message_content/message, admin_only = FALSE)
return

/**
* Send a private message to a specific user.
*
* message - The string to send.
* message - The [/datum/tgs_message_content] to send.
* user: The [/datum/tgs_chat_user] to PM.
*/
/world/proc/TgsChatPrivateMessage(message, datum/tgs_chat_user/user)
/world/proc/TgsChatPrivateMessage(datum/tgs_message_content/message, datum/tgs_chat_user/user)
return

// The following functions will sleep if a call to TgsNew() is sleeping

/**
* Send a message to connected chats that are flagged as game-related in TGS.
*
* message - The string to send.
* message - The [/datum/tgs_message_content] to send.
* channels - Optional list of [/datum/tgs_chat_channel]s to restrict the message to.
*/
/world/proc/TgsChatBroadcast(message, list/channels = null)
/world/proc/TgsChatBroadcast(datum/tgs_message_content/message, list/channels = null)
return

/// Returns the current [/datum/tgs_version] of TGS if it is running the server, null otherwise.
Expand Down
2 changes: 2 additions & 0 deletions tgs_dmapi/tgs/core/datum.dm
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ TGS_DEFINE_AND_SET_GLOBAL(tgs, null)
var/datum/tgs_version/version
var/datum/tgs_event_handler/event_handler

var/list/warned_deprecated_command_runs

/datum/tgs_api/New(datum/tgs_event_handler/event_handler, datum/tgs_version/version)
. = ..()
src.event_handler = event_handler
Expand Down
4 changes: 4 additions & 0 deletions tgs_dmapi/tgs/includes.dm
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@

#include "v5\_defines.dm"
#include "v5\api.dm"
#include "v5\bridge.dm"
#include "v5\chunking.dm"
#include "v5\commands.dm"
#include "v5\serializers.dm"
#include "v5\topic.dm"
#include "v5\undefs.dm"
9 changes: 6 additions & 3 deletions tgs_dmapi/tgs/v3210/api.dm
Original file line number Diff line number Diff line change
Expand Up @@ -193,16 +193,19 @@
/datum/tgs_api/v3210/ChatChannelInfo()
return list() // :omegalul:

/datum/tgs_api/v3210/ChatBroadcast(message, list/channels)
/datum/tgs_api/v3210/ChatBroadcast(datum/tgs_message_content/message, list/channels)
if(channels)
return TGS_UNIMPLEMENTED
message = UpgradeDeprecatedChatMessage(message)
ChatTargetedBroadcast(message, TRUE)
ChatTargetedBroadcast(message, FALSE)

/datum/tgs_api/v3210/ChatTargetedBroadcast(message, admin_only)
ExportService("[admin_only ? SERVICE_REQUEST_IRC_ADMIN_CHANNEL_MESSAGE : SERVICE_REQUEST_IRC_BROADCAST] [message]")
/datum/tgs_api/v3210/ChatTargetedBroadcast(datum/tgs_message_content/message, admin_only)
message = UpgradeDeprecatedChatMessage(message)
ExportService("[admin_only ? SERVICE_REQUEST_IRC_ADMIN_CHANNEL_MESSAGE : SERVICE_REQUEST_IRC_BROADCAST] [message.text]")

/datum/tgs_api/v3210/ChatPrivateMessage(message, datum/tgs_chat_user/user)
UpgradeDeprecatedChatMessage(message)
return TGS_UNIMPLEMENTED

/datum/tgs_api/v3210/SecurityLevel()
Expand Down
10 changes: 8 additions & 2 deletions tgs_dmapi/tgs/v3210/commands.dm
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
var/warned_about_the_dangers_of_robutussin = !warnings_only
for(var/I in typesof(/datum/tgs_chat_command) - /datum/tgs_chat_command)
if(!warned_about_the_dangers_of_robutussin)
TGS_ERROR_LOG("Custom chat commands in [ApiVersion()] lacks the /datum/tgs_chat_user/sender.channel field!")
TGS_WARNING_LOG("Custom chat commands in [ApiVersion()] lacks the /datum/tgs_chat_user/sender.channel field!")
warned_about_the_dangers_of_robutussin = TRUE
var/datum/tgs_chat_command/stc = I
if(stc.ignore_type == I)
continue

var/command_name = initial(stc.name)
if(!command_name || findtext(command_name, " ") || findtext(command_name, "'") || findtext(command_name, "\""))
if(warnings_only && !warned_command_names[command_name])
Expand Down Expand Up @@ -49,4 +52,7 @@
sender = "<@[sender]>"

user.mention = sender
return stc.Run(user, params) || TRUE
var/datum/tgs_message_content/result = stc.Run(user, params)
result = UpgradeDeprecatedCommandResponse(result, command)

return result?.text || TRUE
15 changes: 9 additions & 6 deletions tgs_dmapi/tgs/v4/api.dm
Original file line number Diff line number Diff line change
Expand Up @@ -256,33 +256,36 @@
/datum/tgs_api/v4/Revision()
return cached_revision

/datum/tgs_api/v4/ChatBroadcast(message, list/channels)
/datum/tgs_api/v4/ChatBroadcast(datum/tgs_message_content/message, list/channels)
var/list/ids
if(length(channels))
ids = list()
for(var/I in channels)
var/datum/tgs_chat_channel/channel = I
ids += channel.id
message = list("message" = message, "channelIds" = ids)
message = UpgradeDeprecatedChatMessage(message)
message = list("message" = message.text, "channelIds" = ids)
if(intercepted_message_queue)
intercepted_message_queue += list(message)
else
Export(TGS4_COMM_CHAT, message)

/datum/tgs_api/v4/ChatTargetedBroadcast(message, admin_only)
/datum/tgs_api/v4/ChatTargetedBroadcast(datum/tgs_message_content/message, admin_only)
var/list/channels = list()
for(var/I in ChatChannelInfo())
var/datum/tgs_chat_channel/channel = I
if (!channel.is_private_channel && ((channel.is_admin_channel && admin_only) || (!channel.is_admin_channel && !admin_only)))
channels += channel.id
message = list("message" = message, "channelIds" = channels)
message = UpgradeDeprecatedChatMessage(message)
message = list("message" = message.text, "channelIds" = channels)
if(intercepted_message_queue)
intercepted_message_queue += list(message)
else
Export(TGS4_COMM_CHAT, message)

/datum/tgs_api/v4/ChatPrivateMessage(message, datum/tgs_chat_user/user)
message = list("message" = message, "channelIds" = list(user.channel.id))
/datum/tgs_api/v4/ChatPrivateMessage(datum/tgs_message_content/message, datum/tgs_chat_user/user)
message = UpgradeDeprecatedChatMessage(message)
message = list("message" = message.text, "channelIds" = list(user.channel.id))
if(intercepted_message_queue)
intercepted_message_queue += list(message)
else
Expand Down
11 changes: 7 additions & 4 deletions tgs_dmapi/tgs/v4/commands.dm
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
custom_commands = list()
for(var/I in typesof(/datum/tgs_chat_command) - /datum/tgs_chat_command)
var/datum/tgs_chat_command/stc = new I
if(stc.ignore_type == I)
continue

var/command_name = stc.name
if(!command_name || findtext(command_name, " ") || findtext(command_name, "'") || findtext(command_name, "\""))
TGS_ERROR_LOG("Custom command [command_name] ([I]) can't be used as it is empty or contains illegal characters!")
Expand Down Expand Up @@ -34,8 +37,8 @@

var/datum/tgs_chat_command/sc = custom_commands[command]
if(sc)
var/result = sc.Run(u, params)
if(result == null)
result = ""
return result
var/datum/tgs_message_content/result = sc.Run(u, params)
result = UpgradeDeprecatedCommandResponse(result, command)

return result?.text
return "Unknown command: [command]!"
5 changes: 5 additions & 0 deletions tgs_dmapi/tgs/v5/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

This DMAPI implements bridge requests using HTTP GET requests to TGS. It has no security restrictions.

- [__interop_version.dm](./__interop_version.dm) contains the version of the API used between the DMAPI and TGS.
- [_defines.dm](./_defines.dm) contains constant definitions.
- [api.dm](./api.dm) contains the bulk of the API code.
- [bridge.dm](./bridge.dm) contains functions related to making bridge requests.
- [chunking.dm](./chunking.dm) contains common function for splitting large raw data sets into chunks BYOND can natively process.
- [commands.dm](./commands.dm) contains functions relating to `/datum/tgs_chat_command`s.
- [serializers.dm](./serializers.dm) contains function to help convert interop `/datum`s into a JSON encodable `list()` format.
- [topic.dm](./topic.dm) contains functions related to processing topic requests.
- [undefs.dm](./undefs.dm) Undoes the work of `_defines.dm`.
1 change: 1 addition & 0 deletions tgs_dmapi/tgs/v5/__interop_version.dm
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"5.6.0"
Loading

0 comments on commit d80c1dd

Please sign in to comment.