aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md2
-rwxr-xr-xbot.zsh1
-rw-r--r--files/help.txt.default1
-rw-r--r--modules/commands.zsh3
-rw-r--r--submodules/callback_stop.zsh2
-rw-r--r--submodules/command_export.zsh137
6 files changed, 145 insertions, 1 deletions
diff --git a/README.md b/README.md
index f5aaf49..5c8b485 100644
--- a/README.md
+++ b/README.md
@@ -49,6 +49,7 @@ For distribution specific installation commands follow [command-not-found](https
* sort
* sha1sum
* sleep
+* tar
* tr
* wc
@@ -141,6 +142,7 @@ Cache modes:
* `/shorts` - Manage saved shortcuts
* `/ping` - Measure bot latency
* `/donate` - Send donation details
+* `/export` - Export all user data
* `/stop` - Remove all user data
### Inline aliases
diff --git a/bot.zsh b/bot.zsh
index e0df300..33c389f 100755
--- a/bot.zsh
+++ b/bot.zsh
@@ -58,6 +58,7 @@ busybox=(
sort
sha1sum
sleep
+ tar
tr
wc
)
diff --git a/files/help.txt.default b/files/help.txt.default
index a1b2a7d..eaaee3f 100644
--- a/files/help.txt.default
+++ b/files/help.txt.default
@@ -47,6 +47,7 @@
/shorts - Manage saved shortcuts
/ping - Measure bot latency
/donate - Send donation details
+/export - Export all your data
/stop - Remove all your data
<b>Inline aliases:</b>
diff --git a/modules/commands.zsh b/modules/commands.zsh
index 2992bd5..b8a6c08 100644
--- a/modules/commands.zsh
+++ b/modules/commands.zsh
@@ -39,6 +39,9 @@ case "${command}" in
("/donate" | "/donate@${username}")
. "${submods}/command_donate.zsh"
;;
+ ("/export" | "/export@${username}")
+ . "${submods}/command_export.zsh"
+ ;;
("/hash" | "/hash@${username}")
. "${submods}/command_hash.zsh"
;;
diff --git a/submodules/callback_stop.zsh b/submodules/callback_stop.zsh
index 2edb9e5..d966d4e 100644
--- a/submodules/callback_stop.zsh
+++ b/submodules/callback_stop.zsh
@@ -8,7 +8,7 @@ then
return 0
fi
-locks=(auth saucenao short)
+locks=(auth export saucenao short)
for lock in ${locks[@]}
do
diff --git a/submodules/command_export.zsh b/submodules/command_export.zsh
new file mode 100644
index 0000000..d035da2
--- /dev/null
+++ b/submodules/command_export.zsh
@@ -0,0 +1,137 @@
+# Copyright (C) 2024-2025 Maria Lisina
+# Copyright (C) 2024-2025 Danil Lisin
+# SPDX-License-Identifier: Apache-2.0
+
+until mkdir "${user_config}_export.lock"
+do
+ sleep 1
+done
+
+if ! [[ -d "${user_config}" ]]
+then
+ output_text="No user data found"
+
+ rmdir "${user_config}_export.lock"
+ return 0
+fi
+
+rm -f "${cache}/${user_id}.tar"
+
+if ! tar c -hf "${cache}/${user_id}.tar" "${user_config}"
+then
+ output_text="Something went wrong, try again later"
+
+ rmdir "${user_config}_export.lock"
+ return 0
+fi
+
+output_file="${cache}/${update_id}_sendChatAction.json"
+dump+=(${output_file##*/})
+
+if ! curl --connect-timeout ${connrefused_timeout} \
+ --data-urlencode "chat_id=${chat_id}" \
+ --data-urlencode "message_thread_id=${message_thread_id}" \
+ --data-urlencode "action=upload_document" \
+ --get \
+ --max-time ${internal_timeout} \
+ --output "${output_file}" \
+ --proxy "${internal_proxy}" \
+ --retry 1 \
+ --retry-connrefused \
+ --retry-max-time $((internal_timeout - connrefused_timeout)) \
+ --silent \
+ --user-agent "${useragent}" \
+ "${api_address}/bot${api_token}/sendChatAction"
+then
+ log_text="sendChatAction (${update_id}): Failed to access Telegram Bot API"
+
+ . "${units}/log.zsh"
+ . "${units}/dump.zsh"
+fi
+
+if [[ -z "${log_text}" ]] && ! jq -e '.' "${output_file}" > /dev/null
+then
+ log_text="sendChatAction (${update_id}): An unknown error occurred"
+
+ . "${units}/log.zsh"
+ . "${units}/dump.zsh"
+fi
+
+if [[ -z "${log_text}" && "$(jq -r '.ok' "${output_file}")" != "true" ]]
+then
+ error_description="$(jq -r '.description' "${output_file}")"
+
+ if [[ "${error_description}" != "null" ]]
+ then
+ log_text="sendChatAction (${update_id}): ${error_description}"
+ else
+ log_text="sendChatAction (${update_id}): An unknown error occurred"
+ fi
+
+ . "${units}/log.zsh"
+ . "${units}/dump.zsh"
+fi
+
+output_file="${cache}/${update_id}_sendDocument.json"
+dump+=(${output_file##*/})
+
+if ! curl --connect-timeout ${connrefused_timeout} \
+ --form "chat_id=${chat_id}" \
+ --form "message_thread_id=${message_thread_id}" \
+ --form "document=@${cache}/${user_id}.tar" \
+ --form "reply_parameters=${reply_parameters}" \
+ --form "reply_markup=${reply_markup}" \
+ --get \
+ --max-time ${internal_timeout} \
+ --output "${output_file}" \
+ --proxy "${internal_proxy}" \
+ --retry 1 \
+ --retry-connrefused \
+ --retry-max-time $((internal_timeout - connrefused_timeout)) \
+ --silent \
+ --user-agent "${useragent}" \
+ "${api_address}/bot${api_token}/sendDocument"
+then
+ output_text="Failed to send user data"
+ log_text="sendDocument (${update_id}): Failed to access Telegram Bot API"
+
+ . "${units}/log.zsh"
+ . "${units}/dump.zsh"
+
+ rmdir "${user_config}_export.lock"
+ return 0
+fi
+
+if ! jq -e '.' "${output_file}" > /dev/null
+then
+ output_text="An unknown error occurred"
+ log_text="sendDocument (${update_id}): An unknown error occurred"
+
+ . "${units}/log.zsh"
+ . "${units}/dump.zsh"
+
+ rmdir "${user_config}_export.lock"
+ return 0
+fi
+
+if [[ "$(jq -r '.ok' "${output_file}")" != "true" ]]
+then
+ output_text="Failed to send user data"
+ error_description="$(jq -r '.description' "${output_file}")"
+
+ if [[ "${error_description}" != "null" ]]
+ then
+ log_text="sendDocument (${update_id}): ${error_description}"
+ else
+ log_text="sendDocument (${update_id}): An unknown error occurred"
+ fi
+
+ . "${units}/log.zsh"
+ . "${units}/dump.zsh"
+
+ rmdir "${user_config}_export.lock"
+ return 0
+fi
+
+rmdir "${user_config}_export.lock"
+exit 0