Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/pi-hole/pi-hole.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMMotti <matthew.w.motti@gmail.com>2019-06-04 08:06:17 +0300
committerMMotti <matthew.w.motti@gmail.com>2019-06-04 08:06:17 +0300
commit7613e94ef6f987d12203dc13e26b5637a5e31d48 (patch)
treec728611ba892d27010a0032408c0872ade9bb65d /advanced/Scripts/query.sh
parentb49c702f331c5bfecc2b4622039740f8b32aa247 (diff)
Minor tweaks
Mainly for consistency Signed-off-by: MMotti <matthew.w.motti@gmail.com>
Diffstat (limited to 'advanced/Scripts/query.sh')
-rwxr-xr-xadvanced/Scripts/query.sh46
1 files changed, 20 insertions, 26 deletions
diff --git a/advanced/Scripts/query.sh b/advanced/Scripts/query.sh
index f4aed05b..5b8baa63 100755
--- a/advanced/Scripts/query.sh
+++ b/advanced/Scripts/query.sh
@@ -12,7 +12,6 @@
# Globals
piholeDir="/etc/pihole"
gravityDBfile="${piholeDir}/gravity.db"
-regexlist="/etc/pihole/regex.list"
options="$*"
adlist=""
all=""
@@ -24,7 +23,6 @@ colfile="/opt/pihole/COL_TABLE"
source "${colfile}"
# Scan an array of files for matching strings
-# Scan an array of files for matching strings
scanList(){
# Escape full stops
local domain="${1}" esc_domain="${1//./\\.}" lists="${2}" type="${3:-}"
@@ -39,7 +37,12 @@ scanList(){
# shellcheck disable=SC2086
case "${type}" in
"exact" ) grep -i -E -l "(^|(?<!#)\\s)${esc_domain}($|\\s|#)" ${lists} /dev/null 2>/dev/null;;
- "rx" ) awk 'NR==FNR{regexps[$0]}{for (r in regexps)if($0 ~ r)print r}' <(echo "${lists}") <(echo "${domain}") 2>/dev/null;;
+ # Create array of regexps
+ # Iterate through each regexp and check whether it matches the domainQuery
+ # If it does, print the matching regexp and continue looping
+ # Input 1 - regexps | Input 2 - domainQuery
+ "regex" ) awk 'NR==FNR{regexps[$0]}{for (r in regexps)if($0 ~ r)print r}' \
+ <(echo "${lists}") <(echo "${domain}") 2>/dev/null;;
* ) grep -i "${esc_domain}" ${lists} /dev/null 2>/dev/null;;
esac
}
@@ -97,9 +100,8 @@ scanDatabaseTable() {
# behavior. The "ESCAPE '\'" clause specifies that an underscore preceded by an '\' should be matched
# as a literal underscore character. We pretreat the $domain variable accordingly to escape underscores.
case "${type}" in
- "exact" ) querystr="SELECT domain FROM vw_${table} WHERE domain = '${domain}'";;
- "retrievetable" ) querystr="SELECT domain FROM vw_${table}";;
- * ) querystr="SELECT domain FROM vw_${table} WHERE domain LIKE '%${domain//_/\\_}%' ESCAPE '\\'";;
+ "exact" ) querystr="SELECT domain FROM vw_${table} WHERE domain = '${domain}'";;
+ * ) querystr="SELECT domain FROM vw_${table} WHERE domain LIKE '%${domain//_/\\_}%' ESCAPE '\\'";;
esac
# Send prepared query to gravity database
@@ -109,13 +111,6 @@ scanDatabaseTable() {
return
fi
- # If we are only retrieving the table
- # Just output and return
- if [[ "${type}" == "retrievetable" ]]; then
- echo "${result[*]}"
- return
- fi
-
# Mark domain as having been white-/blacklist matched (global variable)
wbMatch=true
@@ -138,20 +133,19 @@ scanDatabaseTable "${domainQuery}" "whitelist" "${exact}"
scanDatabaseTable "${domainQuery}" "blacklist" "${exact}"
# Scan Regex table
-regexlist=$(scanDatabaseTable "" "regex" "retrievetable")
-
-if [[ -n "${regexlist}" ]]; then
- # Return portion(s) of string that is found in the regex list
- mapfile -t results <<< "$(scanList "${domainQuery}" "${regexlist}" "rx")"
-
- # If a result is found
+mapfile -t regexlist <<< "$(sqlite3 "${gravityDBfile}" "SELECT domain FROM vw_regex" 2> /dev/null)"
+# Split results over new line and store in a string
+# ready for processing
+str_regexlist=$(IFS=$'\n'; echo "${regexlist[*]}")
+# If there are regexps in the DB
+if [[ -n "${str_regexlist}" ]]; then
+ # Return any regexps that match the domainQuery
+ mapfile -t results <<< "$(scanList "${domainQuery}" "${str_regexlist}" "regex")"
+
+ # If there are matches to the domain query
if [[ -n "${results[*]}" ]]; then
- # Count the matches
- regexCount=${#results[@]}
- # Determine plural string
- [[ $regexCount -gt 1 ]] && plu="es"
# Form output strings
- str="${COL_BOLD}${regexCount}${COL_NC} ${matchType}${plu:-} found in ${COL_BOLD}regex${COL_NC} table"
+ str="${matchType^} found in ${COL_BOLD}regex list${COL_NC}"
result="${COL_BOLD}$(IFS=$'\n'; echo "${results[*]}")${COL_NC}"
if [[ -z "${blockpage}" ]]; then
@@ -160,7 +154,7 @@ if [[ -n "${regexlist}" ]]; then
fi
case "${blockpage}" in
- true ) echo "π ${regexlist##*/}"; exit 0;;
+ true ) echo "π regex list"; exit 0;;
* ) awk '{print " "$0}' <<< "${result}";;
esac
fi