#############################################################################
# Copyright (c) 2021 One Identity
# Copyright (c) 2021 László Várady
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 as published
# by the Free Software Foundation, or (at your option) any later version.
#
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
# As an additional exemption you are allowed to compile & link against the
# OpenSSL libraries as published by the OpenSSL project. See the file
# COPYING for details.
#
#############################################################################

# Rate limits: https://discord.com/developers/docs/topics/rate-limits#global-rate-limit
# Max message length: https://discord.com/developers/docs/resources/webhook#execute-webhook

@requires json-plugin
@requires basicfuncs

block destination discord(
  url()
  template("${MSG:-[empty message]}")
  max-msg-length(2000)
  throttle(5)
  username("")
  avatar-url("")
  tts(false)
  use-system-cert-store(yes)
  ...)
{

@requires http "The discord() driver depends on the syslog-ng http module, please install the syslog-ng-mod-http (Debian & derivatives) or the syslog-ng-http (RHEL & co) package"

  http(
    url('`url`')
    method("POST")
    headers("Content-type: application/json")
    body("$(format-json
      content=$(substr '`template`' 0 `max-msg-length`)
      username='`username`'
      avatar_url='`avatar-url`'
      tts=`tts`)")
    throttle(`throttle`)
    use-system-cert-store(`use-system-cert-store`)
    `__VARARGS__`
  );
};
