/*
 * Copyright (c) 2011-2012 Balabit
 * Copyright (c) 2011-2012 Gergely Nagy <algernon@balabit.hu>
 *
 * 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.
 *
 */

%code requires {

#include "afsmtp-parser.h"

}

%code {

#include "cfg-parser.h"
#include "cfg-grammar-internal.h"
#include "plugin.h"

extern LogDriver *last_driver;

}

%define api.prefix {afsmtp_}
%lex-param {CfgLexer *lexer}
%parse-param {CfgLexer *lexer}
%parse-param {LogDriver **instance}
%parse-param {gpointer arg}

/* INCLUDE_DECLS */

%token KW_SMTP
%token KW_SUBJECT
%token KW_FROM
%token KW_TO
%token KW_BODY
%token KW_HEADER
%token KW_CC
%token KW_BCC
%token KW_SENDER
%token KW_REPLY_TO

%%

start
        : LL_CONTEXT_DESTINATION KW_SMTP
          {
            last_driver = *instance = afsmtp_dd_new(configuration);
          }
          '(' afsmtp_options ')'         { YYACCEPT; }
        ;

afsmtp_options
        : afsmtp_option afsmtp_options
        |
        ;

afsmtp_option
        : KW_HOST '(' string ')'
          {
            afsmtp_dd_set_host(last_driver, $3);
            free($3);
          }
        | KW_PORT '(' positive_integer ')' { afsmtp_dd_set_port(last_driver, $3); }
        | KW_SUBJECT '(' template_content ')'
          {
            afsmtp_dd_set_subject(last_driver, $3);
            log_template_unref($3);
          }
        | KW_BODY '(' template_content ')'
          {
            afsmtp_dd_set_body(last_driver, $3);
            log_template_unref($3);
          }
        | KW_HEADER '(' string template_content ')'
          {
            afsmtp_dd_add_header(last_driver, $3, $4);
            free($3);
            log_template_unref($4);
          }
        | KW_FROM '(' template_content ')'
          {
            afsmtp_dd_set_from(last_driver, $3, $3);
            log_template_unref($3);
          }
        | KW_FROM '(' template_content template_content ')'
          {
            afsmtp_dd_set_from(last_driver, $3, $4);
            log_template_unref($3);
            log_template_unref($4);
          }
        | KW_SENDER '(' template_content ')'
          {
            afsmtp_dd_set_from(last_driver, $3, $3);
            log_template_unref($3);
          }
        | KW_SENDER '(' template_content template_content ')'
          {
            afsmtp_dd_set_from(last_driver, $3, $4);
            log_template_unref($3);
            log_template_unref($4);
          }
        | KW_TO '(' template_content ')'
          {
            afsmtp_dd_add_rcpt(last_driver, AFSMTP_RCPT_TYPE_TO, $3, $3);
            log_template_unref($3);
          }
        | KW_TO '(' template_content template_content ')'
          {
            afsmtp_dd_add_rcpt(last_driver, AFSMTP_RCPT_TYPE_TO, $3, $4);
            log_template_unref($3);
            log_template_unref($4);
          }
        | KW_CC '(' template_content ')'
          {
            afsmtp_dd_add_rcpt(last_driver, AFSMTP_RCPT_TYPE_CC, $3, $3);
            log_template_unref($3);
          }
        | KW_CC '(' template_content template_content ')'
          {
            afsmtp_dd_add_rcpt(last_driver, AFSMTP_RCPT_TYPE_CC, $3, $4);
            log_template_unref($3);
            log_template_unref($4);
          }
        | KW_BCC '(' template_content ')'
          {
            afsmtp_dd_add_rcpt(last_driver, AFSMTP_RCPT_TYPE_BCC, $3, $3);
            log_template_unref($3);
          }
        | KW_BCC '(' template_content template_content ')'
          {
            afsmtp_dd_add_rcpt(last_driver, AFSMTP_RCPT_TYPE_BCC, $3, $4);
            log_template_unref($3);
            log_template_unref($4);
          }
        | KW_REPLY_TO '(' template_content ')'
          {
            afsmtp_dd_add_rcpt(last_driver, AFSMTP_RCPT_TYPE_REPLY_TO, $3, $3);
            log_template_unref($3);
          }
        | KW_REPLY_TO '(' template_content template_content ')'
          {
            afsmtp_dd_add_rcpt(last_driver, AFSMTP_RCPT_TYPE_REPLY_TO, $3, $4);
            log_template_unref($3);
            log_template_unref($4);
          }
        | threaded_dest_driver_general_option
        | { last_template_options = afsmtp_dd_get_template_options(last_driver); } template_option
        ;

/* INCLUDE_RULES */

%%
