package main

import (
	"strings"

	"codeberg.org/miekg/dns"
)

type PluginBlockUnqualified struct{}

func (plugin *PluginBlockUnqualified) Name() string {
	return "block_unqualified"
}

func (plugin *PluginBlockUnqualified) Description() string {
	return "Block unqualified DNS names"
}

func (plugin *PluginBlockUnqualified) Init(proxy *Proxy) error {
	return nil
}

func (plugin *PluginBlockUnqualified) Drop() error {
	return nil
}

func (plugin *PluginBlockUnqualified) Reload() error {
	return nil
}

func (plugin *PluginBlockUnqualified) Eval(pluginsState *PluginsState, msg *dns.Msg) error {
	question := msg.Question[0]
	qtype := dns.RRToType(question)
	if question.Header().Class != dns.ClassINET || (qtype != dns.TypeA && qtype != dns.TypeAAAA) {
		return nil
	}
	if strings.IndexByte(pluginsState.qName, '.') >= 0 {
		return nil
	}
	synth := EmptyResponseFromMessage(msg)
	synth.Rcode = dns.RcodeNameError
	pluginsState.synthResponse = synth
	pluginsState.action = PluginsActionSynth
	pluginsState.returnCode = PluginsReturnCodeSynth

	return nil
}
