Files
CHOMPStation2/code/controllers/subsystems/webhooks.dm
CHOMPStation2StaffMirrorBot d82d0807a5 [MIRROR] Polaris Hook Removal Part 1 (#11887)
Co-authored-by: Will <7099514+Willburd@users.noreply.github.com>
Co-authored-by: C.L. <killer65311@gmail.com>
2025-10-30 21:40:35 +01:00

93 lines
3.0 KiB
Plaintext

SUBSYSTEM_DEF(webhooks)
name = "Webhooks"
dependencies = list(
/datum/controller/subsystem/server_maint,
)
flags = SS_NO_FIRE
var/list/webhook_decls = list()
/datum/controller/subsystem/webhooks/Initialize()
load_webhooks()
return SS_INIT_SUCCESS
/datum/controller/subsystem/webhooks/proc/load_webhooks()
if(!fexists(HTTP_POST_DLL_LOCATION))
log_world("Unable to locate HTTP POST lib at [HTTP_POST_DLL_LOCATION], webhooks will not function on this run.")
return
var/list/all_webhooks_by_id = list()
var/list/all_webhooks = decls_repository.get_decls_of_subtype(/decl/webhook)
for(var/wid in all_webhooks)
var/decl/webhook/webhook = all_webhooks[wid]
if(webhook.id)
all_webhooks_by_id[webhook.id] = webhook
webhook_decls.Cut()
var/webhook_config = safe_file2text("config/webhooks.json")
if(webhook_config)
for(var/webhook_data in cached_json_decode(webhook_config))
var/wid = webhook_data["id"]
var/wurl = webhook_data["url"]
var/list/wmention = webhook_data["mentions"]
if(wmention && !islist(wmention))
wmention = list(wmention)
log_world("Setting up webhook [wid].")
if(wid && wurl && all_webhooks_by_id[wid])
var/decl/webhook/webhook = all_webhooks_by_id[wid]
webhook.urls = islist(wurl) ? wurl : list(wurl)
for(var/url in webhook.urls)
if(!webhook.urls[url])
webhook.urls[url] = list()
else if(!islist(webhook.urls[url]))
webhook.urls[url] = list(webhook.urls[url])
if(wmention)
webhook.mentions = wmention?.Copy()
webhook_decls[wid] = webhook
log_world("Webhook [wid] ready.")
else
log_world("Failed to set up webhook [wid].")
/datum/controller/subsystem/webhooks/proc/send(var/wid, var/wdata)
var/decl/webhook/webhook = webhook_decls[wid]
if(webhook)
if(webhook.send(wdata))
log_world("Sent webhook [webhook.id].")
// to_chat(world, "Webhook sent: [webhook.id].")
else
log_world("Failed to send webhook [webhook.id].")
// to_chat(world, "Webhook failed to send: [webhook.id].")
/client/proc/reload_webhooks()
set name = "Reload Webhooks"
set category = "Debug"
if(!check_rights_for(src, R_HOLDER))
return
if(!SSwebhooks.initialized)
to_chat(usr, span_warning("Let the webhook subsystem initialize before trying to reload it."))
return
log_world("[usr.key] has reloaded webhooks.")
log_and_message_admins("has reloaded webhooks.")
SSwebhooks.load_webhooks()
/client/proc/ping_webhook()
set name = "Ping Webhook"
set category = "Debug"
if(!check_rights_for(src, R_HOLDER))
return
if(!length(SSwebhooks.webhook_decls))
to_chat(usr, span_warning("Webhook list is empty; either webhooks are disabled, webhooks aren't configured, or the subsystem hasn't initialized."))
return
var/choice = tgui_input_list(usr, "Select a webhook to ping.", "Ping Webhook", SSwebhooks.webhook_decls)
if(choice && SSwebhooks.webhook_decls[choice])
var/decl/webhook/webhook = SSwebhooks.webhook_decls[choice]
log_and_message_admins("has pinged webhook [choice].", usr)
log_world("[usr.key] has pinged webhook [choice].")
webhook.send()