<!--
OpenVAS Manager
$Id$
Description: OpenVAS Manager Protocol (OMP) task modification wizard.

Authors:
Timo Pollmeier <timo.pollmeier@greenbone.net>

Copyright:
Copyright (C) 2014 Greenbone Networks GmbH

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

<!--
  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 adress 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="/">
          <commands>
            <xsl:if test="/wizard/params/param[name='task_id']/value != ''">
              <get_tasks task_id="{/wizard/params/param[name='task_id']/value}"/>
            </xsl:if>
          </commands>
        </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 match="/">
          <commands>
            <xsl:if test="/wizard/previous/response/commands_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/commands_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 != '')">
                <create_schedule>
                  <name>
                    <xsl:text>Schedule for </xsl:text>
                    <xsl:value-of select="/wizard/previous/response/commands_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>
                  <first_time>
                    <year><xsl:value-of select="/wizard/params/param[name='start_year']/value"/></year>
                    <month><xsl:value-of select="/wizard/params/param[name='start_month']/value"/></month>
                    <day_of_month><xsl:value-of select="/wizard/params/param[name='start_day']/value"/></day_of_month>
                    <hour><xsl:value-of select="/wizard/params/param[name='start_hour']/value"/></hour>
                    <minute><xsl:value-of select="/wizard/params/param[name='start_minute']/value"/></minute>
                  </first_time>
                  <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>
          </commands>
        </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="/">
          <commands>
            <xsl:if test="/wizard/previous/response/commands_response/get_tasks_response/task != ''">
              <modify_task task_id="{/wizard/params/param[name='task_id']/value}">
                <xsl:if test="/wizard/previous/response/commands_response/create_alert_response/@id != ''">
                  <xsl:for-each select="/wizard/previous/response/commands_response/get_tasks_response/task/alert">
                    <alert id="{@id}"/>
                  </xsl:for-each>
                  <alert id="{/wizard/previous/response/commands_response/create_alert_response/@id}"/>
                </xsl:if>

                <xsl:if test="/wizard/previous/response/commands_response/create_schedule_response/@id != ''">
                  <schedule id="{/wizard/previous/response/commands_response/create_schedule_response/@id}"/>
                </xsl:if>
              </modify_task>
            </xsl:if>
          </commands>
        </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="/">
            <commands>
              <get_tasks details="0"/>
              <get_settings/>
            </commands>
          </xsl:template>
        </xsl:stylesheet>
      </command>
    </step>
  </mode>
</wizard>
