<!--
Copyright (C) 2014-2022 Greenbone AG

SPDX-License-Identifier: AGPL-3.0-or-later

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, 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 Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
-->

<!-- Greenbone Manager Protocol (GMP) task modification wizard. -->

<!--
  This wizard changes the schedule and alerts of a task.
 -->

<wizard>
  <params>
    <!-- The id of the task -->
    <param>
      <optional>0</optional>
      <name>task_id</name>
      <regex>^[-_[:alnum:], \\./]{1,80}$</regex>
    </param>

    <!-- Email address for alert -->
    <param>
      <optional>1</optional>
      <name>alert_email</name>
      <regex>^[^@ ]{1,150}@[^@ ]{1,150}$</regex>
    </param>

    <!-- Whether to add a new schedule.
         0: Do not change
         1: Add a new schedule
    -->
    <param>
      <optional>1</optional>
      <name>reschedule</name>
      <regex>^[0-1]$</regex>
    </param>

    <!-- Start time (optional) -->
    <param>
      <optional>1</optional>
      <name>start_year</name>
      <regex>^([\d]{4})$</regex>
    </param>
    <param>
      <optional>1</optional>
      <name>start_month</name>
      <regex>^0?[1-9]|1[012]$</regex>
    </param>
    <param>
      <optional>1</optional>
      <name>start_day</name>
      <regex>^0?[1-9]|[12][0-9]|3[01]$</regex>
    </param>
    <param>
      <optional>1</optional>
      <name>start_hour</name>
      <regex>^[01]?[0-9]|2[0-3]$</regex>
    </param>
    <param>
      <optional>1</optional>
      <name>start_minute</name>
      <regex>^[0-5]?[0-9]$</regex>
    </param>
    <param>
      <optional>1</optional>
      <name>start_timezone</name>
      <regex>^.{0,1000}$</regex>
    </param>
  </params>

  <step>
    <command>
      <xsl:stylesheet
          version="1.0"
          xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
          xmlns:date="http://exslt.org/dates-and-times"
          extension-element-prefixes="date">
        <xsl:output
          method="xml"
          encoding="UTF-8"/>
        <xsl:template match="/">
          <xsl:if test="/wizard/params/param[name='task_id']/value != ''">
            <get_tasks task_id="{/wizard/params/param[name='task_id']/value}"/>
          </xsl:if>
        </xsl:template>
      </xsl:stylesheet>
    </command>
  </step>


  <step>
    <command>
      <xsl:stylesheet
          version="1.0"
          xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
          xmlns:date="http://exslt.org/dates-and-times"
          extension-element-prefixes="date">
        <xsl:output
          method="xml"
          encoding="UTF-8"/>

        <xsl:template name="newline">
          <xsl:text>
</xsl:text>
        </xsl:template>

        <xsl:template match="/">
          <xsl:if test="/wizard/previous/response/get_tasks_response/task != ''">
            <get_tasks task_id="{/wizard/params/param[name='task_id']/value}"/>

            <xsl:if test="/wizard/params/param[name='alert_email']/value != ''">
              <create_alert>
                <name>
                  <xsl:text>Email Alert for </xsl:text>
                  <xsl:value-of select="/wizard/previous/response/get_tasks_response/task/name"/>
                  <xsl:text> </xsl:text>
                  <xsl:value-of select="translate(substring (date:date-time (), 1, 19), '-:T', '')"/>
                </name>
                <comment>Automatically generated by wizard</comment>
                <condition>Always</condition>
                <event>
                  <xsl:text>Task run status changed</xsl:text>
                  <data>
                    <xsl:text>Done</xsl:text>
                    <name>status</name>
                  </data>
                </event>
                <method>
                  <xsl:text>Email</xsl:text>
                  <data>
                    <xsl:value-of select="/wizard/params/param[name='alert_email']/value"/>
                    <name>to_address</name>
                  </data>
                  <data>
                    <xsl:value-of select="/wizard/params/param[name='alert_email']/value"/>
                    <name>from_address</name>
                  </data>
                  <data>
                    <xsl:text>a3810a62-1f62-11e1-9219-406186ea4fc5</xsl:text>
                    <name>notice_report_format</name>
                  </data>
                  <data>
                    <xsl:text>0</xsl:text>
                    <name>notice</name>
                  </data>
                </method>
              </create_alert>
            </xsl:if>

            <xsl:if test="(/wizard/params/param[name='reschedule']/value = 1) and (/wizard/params/param[name='start_year']/value != '') and (/wizard/params/param[name='start_month']/value != '') and (/wizard/params/param[name='start_day']/value != '') and (/wizard/params/param[name='start_hour']/value != '') and (/wizard/params/param[name='start_minute']/value != '')">
              <xsl:variable name="start">
                <xsl:text>TZID=/freeassociation.sourceforge.net/</xsl:text>
                <xsl:value-of select="/wizard/params/param[name='start_timezone']/value"/>
                <xsl:text>:</xsl:text>
                <xsl:value-of select="format-number (/wizard/params/param[name='start_year']/value, '0000')"/>
                <xsl:value-of select="format-number (/wizard/params/param[name='start_month']/value, '00')"/>
                <xsl:value-of select="format-number (/wizard/params/param[name='start_day']/value, '00')"/>
                <xsl:text>T</xsl:text>
                <xsl:value-of select="format-number (/wizard/params/param[name='start_hour']/value, '00')"/>
                <xsl:value-of select="format-number (/wizard/params/param[name='start_minute']/value, '00')"/>
                <xsl:text>00</xsl:text>
              </xsl:variable>
              <create_schedule>
                <name>
                  <xsl:text>Schedule for </xsl:text>
                  <xsl:value-of select="/wizard/previous/response/get_tasks_response/task/name"/>
                  <xsl:text> </xsl:text>
                  <xsl:value-of select="translate(substring (date:date-time (), 1, 19), '-:T', '')"/>
                </name>
                <comment>Automatically generated by wizard</comment>
                <icalendar>
                  <xsl:text>PRODID:-//Greenbone.net//NONSGML Greenbone Security Manager</xsl:text>
                  <xsl:call-template name="newline"/>
                  <xsl:text>VERSION:2.0</xsl:text>
                  <xsl:call-template name="newline"/>
                  <xsl:text>BEGIN:VEVENT</xsl:text>
                  <xsl:call-template name="newline"/>
                  <xsl:text>DTSTART;</xsl:text>
                  <xsl:value-of select="$start"/>
                  <xsl:call-template name="newline"/>
                  <xsl:text>END:VEVENT</xsl:text>
                  <xsl:call-template name="newline"/>
                  <xsl:text>END:VCALENDAR</xsl:text>
                  <xsl:call-template name="newline"/>
                </icalendar>
                <xsl:if test="(/wizard/params/param[name='start_timezone']/value != '')">
                  <timezone><xsl:value-of select="/wizard/params/param[name='start_timezone']/value"/></timezone>
                </xsl:if>
              </create_schedule>
            </xsl:if>
          </xsl:if>
        </xsl:template>
      </xsl:stylesheet>
    </command>
  </step>

  <step>
    <command>
      <xsl:stylesheet
          version="1.0"
          xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output
          method="xml"
          encoding="UTF-8"/>
        <xsl:template match="/">
          <xsl:if test="/wizard/previous/response/get_tasks_response/task != ''">
            <modify_task task_id="{/wizard/params/param[name='task_id']/value}">
              <xsl:if test="/wizard/previous/response/create_alert_response/@id != ''">
                <xsl:for-each select="/wizard/previous/response/get_tasks_response/task/alert">
                  <alert id="{@id}"/>
                </xsl:for-each>
                <alert id="{/wizard/previous/response/create_alert_response/@id}"/>
              </xsl:if>

              <xsl:if test="/wizard/previous/response/create_schedule_response/@id != ''">
                <schedule id="{/wizard/previous/response/create_schedule_response/@id}"/>
              </xsl:if>
            </modify_task>
          </xsl:if>
        </xsl:template>
      </xsl:stylesheet>
    </command>
  </step>

  <mode>
    <name>init</name>
    <read_only>1</read_only>
    <step>
      <command>
        <xsl:stylesheet
            version="1.0"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
          <xsl:output
            method="xml"
            encoding="UTF-8"/>
          <xsl:template match="/">
            <help format="xml" type="brief"/>
          </xsl:template>
        </xsl:stylesheet>
      </command>
    </step>
    <step>
      <command>
        <xsl:stylesheet
            version="1.0"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
          <xsl:output
            method="xml"
            encoding="UTF-8"/>
          <xsl:template match="/">
            <get_tasks details="0"/>
            <get_settings/>
          </xsl:template>
        </xsl:stylesheet>
      </command>
    </step>
  </mode>
</wizard>
