diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6fd04be --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +allowed_ids.txt diff --git a/bot.sh b/bot.sh new file mode 100755 index 0000000..cb4c371 --- /dev/null +++ b/bot.sh @@ -0,0 +1,81 @@ +#!/bin/bash +[ "${1}" = "stop" ] && { + kill -SIGTERM "$(cat /tmp/dskbot.pid)" 2>/dev/null + exit 0 +} +printf "%s" "${$}" > /tmp/dskbot.pid + +api_url="https://api.telegram.org/bot${TELEGRAM_DSK_TOKEN}/" +bot_tmpdir="/tmp/dskbot/" +[ -d "${bot_tmpdir}" ] || { + mkdir "${bot_tmpdir}" || { + printf "error: can't write to /tmp\n" + exit 2 + } +} + +files=( "updates.txt" "sentmsgs.txt" "error.log" "checked.txt" ) +for i in "${files[@]}"; do + touch "${bot_tmpdir}${i}" +done +unset files + +exec 2>>"${bot_tmpdir}/error.log" + +. ./bashbot-lib/bashbot-lib.sh +. ./bashbot-lib/viewer.sh + +declare -a kbd_rows=( 2 ) +declare -a kbd_text=( "remount", "fix disk" ) +declare -a kbd_data=( "remount", "fixdisk" ) +inline_kbd=$(mkinline_kbd kbd_rows kbd_text kbd_data) +unset kbd_rows +unset kbd_text +unset kbd_data + +dsk_status(){ + local BUF="$(dmesg | grep unmounting | tail -n1)" + local TIMECODE="$(awk '{print $1}' <<< "${BUF}")" + for i in $(awk 'print $1' ./checked.txt); do + [ ! "${i}" = "${TIMECODE}" ] && { + printf "%s\n" "${TIMECODE}" >> /tmp/diskconnectbot/checked.txt + local TIME="$(date +'%a %d/%m - %T')" + local DISK="$(grep -o "sd[a-d][0-9]" <<< "${BUF}")" + [ ! -z "${DISK}" ] && { + local MOUNTED="$(mount | grep "${DISK}")" + [ -z "${MOUNTED}" ] && printf "%s is not mounted\n" "${DISK}" || { + local STATUS="$(grep -o "r[ow]")" + case "${STATUS}" in + "ro") printf "%s is mounted read-only\n" "${DISK}";; + "rw") printf "%s is mounted read-write\n" "${DISK}";; + *) printf "%s is mounted as neither read-write nor read-only" "${DISK}";; + esac + } + } + } + done +} + +while [ "${1}" = "start" ]; do + PANIC="$(dsk_status)" + [ ! -z "${PANIC}" ] && { + for i in $(awk '{print $1}' ./allowed_ids.txt); do + sendmsg "${i}" "${PANIC}" "${inline_kbd}" + done + } + getupd >/dev/null || continue + declare -A curmsg + getmsg_content curmsg updates.txt + view_content curmsg + [ "curmsg[callback]" = "remount" ] && { + for i in $(awk '{print $1}' ./allowed_ids.txt); do + editmsg "curmsg[chat_id]" "curmsg[msg_id]" "remounting." + editmsg "curmsg[chat_id]" "curmsg[msg_id]" "remounting.." + editmsg "curmsg[chat_id]" "curmsg[msg_id]" "remounting..." + declare -A curmsg + getmsg_content curmsg sentmsgs.txt + tput setaf 3; view_content curmsg; tput sgr0 + done + } + unset curmsg PANIC +done